Skip to content
Snippets Groups Projects
Commit 3292408c authored by David Rouquet's avatar David Rouquet
Browse files

Make quiz.js work with several questions

parent 12460eec
No related branches found
No related tags found
No related merge requests found
/**
* Checks whether a question is correct, and update the correct indicator accordingly.
* Checks whether all questions are correctly answered and displays the overall feedback.
*/
function checkQuestion(question) {
if (question.isAnswerCorrect()) {
correctIndic.node.className = "correct"
// FIXME: this inserts raw HTML (see onTextMarkdown callback)
correctIndic.commentNode.innerHTML = question.getPropertyValue("correctComment", "");
function checkAllQuestions(questions, surveyConfig) {
let allCorrect = true;
questions.forEach((question) => {
if (!question.isAnswerCorrect()) {
allCorrect = false;
}
});
if (allCorrect) {
correctIndic.node.className = "correct";
correctIndic.commentNode.innerHTML = surveyConfig.correctComment || "All answers are correct!";
} else {
correctIndic.node.className = "incorrect";
correctIndic.commentNode.innerHTML = question.getPropertyValue("incorrectComment", "");
correctIndic.commentNode.innerHTML = surveyConfig.incorrectComment || "Some answers are incorrect!";
}
console.log("question = ", question);
console.log(" correct = ", question.isAnswerCorrect());
}
function clearQuestion(question) {
/**
* Clears all answers and resets the feedback indicator.
*/
function clearAllQuestions(questions) {
questions.forEach((question) => {
question.value = undefined;
});
correctIndic.node.className = "hidden";
correctIndic.commentNode.innerHTML = "";
}
let correctIndic;
......@@ -27,66 +39,34 @@ $(function () {
commentNode: document.querySelector("#correct-indic .comment"),
};
// Register our custom question types
Survey.CustomWidgetCollection.Instance.add(gapfillSelectWidget, gapfillSelectWidget.name);
Survey.CustomWidgetCollection.Instance.add(gapfillOpenWidget, gapfillOpenWidget.name);
// Register custom properties for quiz correction comments
Survey.Serializer.addProperty("question", {
name: "correctComment",
type: "text"
});
Survey.Serializer.addProperty("question", {
name: "incorrectComment",
type: "text"
});
// Load survey model from JSON
const survey = new Survey.Model(jsonStatic);
// Instantiate `markdown-it`
const converter = markdownit({
html: true // Support HTML tags in the source (unsafe, see documentation)
});
survey.onTextMarkdown.add((_, options) => {
console.log("On text markdown")
// Convert Markdown to HTML
let str = converter.renderInline(options.text);
// ...
// Sanitize the HTML markup using a third-party library here
// ...
// Set HTML markup to render
options.html = str;
});
// Add "Check answers" button
survey.addNavigationItem({
id: "sv-nav-check",
title: "Vérifier",
action: () => {
checkQuestion(survey.currentPage.questions[0]);
checkAllQuestions(survey.currentPage.questions, jsonStatic);
},
css: "nav-button",
innerCss: "sd-btn sd-btn--action nav-input"
});
// Add "Clear answers" button
survey.addNavigationItem({
id: "sv-nav-clear",
title: "Effacer",
action: () => {
clearQuestion(survey.currentPage.questions[0]);
clearAllQuestions(survey.currentPage.questions);
},
css: "nav-button",
innerCss: "sd-btn nav-input"
});
survey.onValueChanged.add((_, options) => {
console.log(options)
});
// Apply theme
survey.applyTheme(SurveyTheme.DefaultLightPanelless);
// Inflate the survey in the page
$("#surveyContainer").Survey({ model: survey });
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment