WikiLerni/models/Question.js

60 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
const striptags = require("striptags");
const tool = require("../tools/main");
const config = require("../config/main.js");
const txt = require("../lang/"+config.adminLang+"/question");
module.exports = (sequelize, DataTypes) =>
{
const Question = sequelize.define('Question',
{
text:
{
type:DataTypes.STRING(255), allowNull: false,
set(value) { this.setDataValue('text', tool.trimIfNotNull(striptags(value))); },
validate:
{
notNull: { msg: txt.needText },
len:
{
args: [1, 255],
msg: txt.needNotTooLongText
}
}
},
explanation:
{
type: DataTypes.TEXT,
set(value) { this.setDataValue('explanation', tool.trimIfNotNull(striptags(value))); },
comment: "Allows you to display explanation for this question after checking the user's answers."
},
rank:
{
type: DataTypes.INTEGER(2).UNSIGNED, allowNull: false, defaultValue:1,
comment: "Ranking of the answer among those proposed.",
validate:
{
notNull: { msg: txt.needNumberForRank },
isInt: { msg: txt.needNumberForRank },
min:
{
args: [1],
msg: txt.needNumberForRank
}
}
}
},
{
charset: "utf8mb4",
collate: "utf8mb4_unicode_ci"
});
Question.associate = function(models)
{
Question.hasMany(models.Choice);
Question.belongsTo(models.Questionnaire, { foreignKey: { name: "QuestionnaireId", allowNull: false }, onDelete: 'CASCADE', onUpdate: 'CASCADE' });
};
return Question;
};