Merge pull request #228 from fazlulShanto/master

update: quiz_template view
This commit is contained in:
Puyodead1 2024-07-08 17:10:14 -04:00 committed by GitHub
commit c60f4e44a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 479 additions and 225 deletions

View File

@ -1676,10 +1676,12 @@ def process_normal_quiz(quiz, lecture, chapter_dir):
lecture_path = os.path.join(chapter_dir, lecture_file_name)
logger.info(f" > Processing quiz {lecture_index}")
with open("quiz_template.html", "r") as f:
html = f.read()
quiz_data = {
"quiz_id":lecture["data"].get("id"),
"quiz_description":lecture["data"].get("description"),
"quiz_title": lecture["data"].get("title"),
"pass_percent": lecture.get("data").get("pass_percent"),
"questions": quiz["contents"],
}

View File

@ -1,263 +1,515 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Quiz</title>
<title> Quiz </title>
<style>
body {
font-family: sf pro text, -apple-system, BlinkMacSystemFont, Roboto,
segoe ui, Helvetica, Arial, sans-serif, apple color emoji,
segoe ui emoji, segoe ui symbol;
font-weight: 400;
line-height: 22.4px;
font-size: 16px;
}
p,
h1,
h2,
h3,
h4,
h5,
h6,
ul,
ol {
font-size: 16px;
font-weight: 400;
}
ul {
list-style: none;
* {
font-family: system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans",
"Helvetica Neue", sans-serif;
margin: 0;
padding: 0;
max-width: none;
}
code {
background-color: #fff;
border: 1px solid #d1d7dc;
color: #b4690e;
font-size: 90%;
padding: 0.2rem 0.4rem;
}
.quiz-content {
padding: 2.4rem;
word-break: break-word;
max-width: 86rem;
margin: 0 auto;
}
.quiz-container {
margin: 0 auto;
max-width: 84rem;
padding: 0;
}
.question {
margin-bottom: 5rem;
}
.question span {
font-size: 24px;
font-weight: 700;
}
.question-prompt {
margin-top: 0.8rem;
font-weight: 700;
}
.question-answer {
margin-top: 1.6rem;
padding-left: 0;
box-sizing: border-box;
font-size: 16px;
}
.question-answer label {
max-width: 80rem;
main {
padding-top: 48px;
}
:root {
--large-device-width: 850px;
--primary-color: #0f172a;
--secondary-color: #020617;
--primary-text-color: #c7d1dd;
--secondary-text-color: #061602;
--success-background: hsl(159, 82%, 24%);
--success-foreground: hsl(164, 86%, 16%);
--success: hsl(160, 84%, 39%);
--danger: #ef4444;
--warning: #f59e0b;
--info-background: hsl(218, 81%, 8%);
--info-foreground: hsl(217, 91%, 85%);
--border-color: #d1d7dc;
--check-box-size: 20px;
/* control the size */
--check-box-color: var(--info-foreground);
/* the active color */
}
body {
position: relative;
cursor: pointer;
display: flex;
min-width: 18rem;
border: solid #1c1d1f 2px;
background-color: #020617;
color: var(--primary-text-color);
}
.question-answer div {
#score-stats-container {
position: fixed;
z-index: 10;
top: 0;
height: 40px;
width: 100%;
background-color: var(--info-background);
padding: 0px 16px;
color: var(--info-foreground);
font-weight: 600;
display: flex;
align-items: center;
padding: 0.5rem;
justify-content: space-between;
}
#quiz-container {
border-radius: 8px;
display: flex;
gap: 16px;
flex-direction: column;
}
input[type="radio"] {
height: var(--check-box-size);
aspect-ratio: 1;
border: calc(var(--check-box-size) / 8) solid #939393;
padding: calc(var(--check-box-size) / 8);
background: radial-gradient(farthest-side,
var(--check-box-color) 94%,
#0000) 50%/0 0 no-repeat content-box;
border-radius: 50%;
outline-offset: calc(var(--check-box-size) / 10);
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
cursor: pointer;
font-size: inherit;
transition: 0.3s;
}
input[type="radio"]:checked {
border-color: var(--check-box-color);
background-size: 100% 100%;
}
input[type="radio"]:disabled {
background: linear-gradient(#939393 0 0) 50%/100% 20% no-repeat content-box;
opacity: 0.5;
cursor: not-allowed;
}
label {
display: inline-flex;
align-items: center;
gap: 10px;
cursor: pointer;
padding: 4px 6px;
border-radius: 4px;
}
@media (max-width: 767px) {
input[type="radio"],
label {
cursor: default;
}
#quiz-container {
margin-left: 8px;
margin-right: 8px;
}
}
/* PC (Desktop devices) */
@media (min-width: 768px) {
body {
display: flex;
justify-content: center;
}
main {
max-width: var(--large-device-width);
}
#score-stats-container {
max-width: var(--large-device-width);
}
dialog {
max-width: var(--large-device-width);
}
}
@media print {
input[type="radio"] {
background: none !important;
border-color: #939393 !important;
}
input[type="radio"]:checked {
border-color: #939393 !important;
}
}
.question-lable {
display: flex;
align-items: center;
gap: 8px;
}
button {
-webkit-tap-highlight-color: transparent;
-webkit-touch-callout: none;
-webkit-user-select: none;
user-select: none;
outline: none;
position: relative;
overflow: hidden;
cursor: pointer;
}
.button {
background-color: var(--success-background);
border: none;
color: #f4f5f7;
opacity: 0.8;
font-size: 18px;
flex-grow: 1;
padding: 8px 16px;
border-radius: 8px;
}
.button:hover {
opacity: 1;
}
.explanation-btn {
border: none;
color: var(--success);
background-color: transparent;
}
#submit-button:active::after {
background-color: #ef4444;
}
.single-question-container {
background-color: var(--primary-color);
display: flex;
flex-direction: column;
gap: 8px;
padding: 16px;
border-radius: 8px;
}
#modal-content {
padding: 16px;
line-height: 24px;
}
dialog::backdrop {
background: rgba(0, 0, 0, 0.5);
}
dialog {
position: fixed;
border: 1px solid var(--border-color);
background-color: var(--primary-color);
color: rgb(240, 241, 248);
padding: 16px;
border-radius: 8px;
width: 80vw;
max-height: 80vh;
overflow: auto;
top: 50%;
left: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
}
#close-modal-btn {
position: absolute;
top: 4px;
right: 4px;
padding: 2px 8px;
border-radius: 2px;
border: none;
background-color: var(--danger);
}
.correct-answer label {
border: 2px solid var(--success);
width: 100%;
}
.question-answer div:hover {
background-color: #f5f5f5;
.incorrect-answer label {
border: 2px solid var(--danger);
width: 100%;
}
.question-answer input {
display: none;
.options-container {
display: flex;
flex-direction: column;
gap: 4px;
}
.question-answer span {
width: 1rem;
height: 1rem;
margin-right: 1.6rem;
top: 0;
border-radius: 50%;
display: inline-block;
flex-shrink: 0;
border: 0.2rem solid #1c1d1f;
position: relative;
#quiz-meta-container {
border-radius: 8px;
background-color: var(--primary-color);
margin-bottom: 12px;
padding: 8px;
}
.selected {
background: #1c1d1f;
box-shadow: 0 0 0 0.4rem #fff inset;
#quiz-title {
text-align: center;
font-size: 24px;
margin-bottom: 8px;
}
#quiz-description {
line-height: 1.5;
padding: 2px 6px;
}
.score {
position: fixed;
}
</style>
</head>
<body onload="main()">
<div id="score" class="score">
<span>Score: N/A of N/A</span>
</div>
<div id="quiz-container" class="quiz-content"></div>
<main>
<section id="quiz-meta-container">
<h1 id="quiz-title"></h1>
<p id="quiz-description"></p>
</section>
<section id="score-stats-container">
<div id="score-card">
Score: <span id="current-score">999</span> of
<span id="pass-percent">999%</span>
</div>
<div>Correct: <span id="correct-answers">999</span></div>
<div>Incorrect: <span id="wrong-answers">999</span></div>
</section>
<section id="quiz-container"></section>
<dialog id="modal" class="modal-container">
<div id="modal-content">
<p id="modal-text"></p>
</div>
</dialog>
</main>
<script>
const quizData = __data_placeholder__;
var correct = 0;
var total = 0;
const questionData = quizData.questions
const passPercent = quizData.pass_percent
let correct = new Set();
let incorrect = new Set();
let totalNumberOfQuestions = 0;
const quizTitle = quizData.quiz_title;
const quizDescription = quizData.quiz_description;
const questionData = quizData.questions;
const passPercent = quizData.pass_percent;
const modalTextElement = document.getElementById("modal-text");
const quizContainerElement =
document.getElementById("quiz-container");
const dialog = document.querySelector("dialog");
const showButton = document.getElementById("view-explanatin");
const closeButton = document.getElementById("close-modal-btn");
const quizTitleElement = document.getElementById("quiz-title");
const quizDescriptionElement =
document.getElementById("quiz-description");
function main() {
total = questionData.length;
// update quiz meta data
document.title = quizTitle;
quizTitleElement.innerHTML = quizTitle;
quizDescriptionElement.innerHTML = quizDescription;
var questions = [];
for (var i = 0; i < questionData.length; i++) {
var question = questionData[i];
var questionText = question.prompt.question;
var answers = question.prompt.answers;
var correctAnswer = question.correct_response[0];
var correctAnswerText = answers[correctAnswer.charCodeAt(0) - 97];
var questionObj = {
question: questionText,
correctAnswer: correctAnswerText,
answers: answers,
id: question.id,
};
questions.push(questionObj);
const passPercentElement =
document.getElementById("pass-percent");
passPercentElement.innerHTML = passPercent + "%";
totalNumberOfQuestions = questionData.length;
// shuffle the questionData to randomize the order of the questions
for (let i = questionData.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[questionData[i], questionData[j]] = [
questionData[j],
questionData[i],
];
}
let formattedQuestions = questionData.map(
formatSingleQuestionData
);
updateScore();
// display the formattedQuestions
formattedQuestions.forEach((question, idx) => {
renderSingleQuestion(question, idx + 1);
});
}
/**
* Formats the question data from the given QuizData object.
*
* @param {Object} singleQuizData - The singleQuizData object containing prompt and correct_response.
* @return {Object} The formatted question object with the following properties:
* - id: The ID of the question.
* - question: The text of the question.
* - answers: The array of answer options.
* - correctAnswer: The text of the correct answer.
* - explanation: The explanation of the correct answer.
*/
function formatSingleQuestionData(singleQuizData = null) {
const { prompt, correct_response, id } = singleQuizData;
const questionText = prompt.question;
const answers = prompt.answers;
const correctAnswer = correct_response[0];
const correctAnswerText =
answers[correctAnswer.toLowerCase().charCodeAt(0) - 97];
const questionObj = {
id: id,
question: questionText,
answers: answers,
correctAnswer: correctAnswerText,
explanation: prompt?.explanation || "",
};
return questionObj;
}
/**
* Renders a single question with its options and submit button.
*
* @param {Object} singleQuestionData - The data of the question to render.
* @param {number} rootIndex - The index of the question in the quiz.
* @return {void} return nothing.
*/
const renderSingleQuestion = (
singleQuestionData = {},
rootIndex = 1
) => {
const { id, explanation, answers, correctAnswer, question } =
singleQuestionData;
// shuffle the answers to randomize the order of the answers
for (let i = answers.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[answers[i], answers[j]] = [answers[j], answers[i]];
}
const optionsHTML = answers
.map((option, index) => {
const optionId = `${id}_${index}`;
return `
<div class="question-lable">
<input type="radio" id="${optionId}" name="${"answer"}" value="${option}" />
<label for="${optionId}">${option}</label>
</div>
`;
})
.join("");
const container = document.createElement("div");
container.innerHTML = `
<form data-correct-answer="${correctAnswer}" data-question-id="${id}" class="single-question-container" onsubmit="submitButtonListener(event)">
<div style="display: flex;justify-content: space-between;">
<p style="font-weight: 600">Question ${rootIndex}:</p>
<button type="button" onclick="renderExplanation(event)" id="${`explanation-${id}`}" data-explanation="${explanation}" class="explanation-btn">View Explanation</button>
</div>
<p style="margin-bottom: 8px;line-height: 1.5">${question}</p>
<div class="options-container">
${optionsHTML}
</div>
<div style="display: flex; gap: 8px;">
<button type="submit" id="submit-button" class="button">Submit</button>
</div>
</form>
`;
quizContainerElement.appendChild(container);
};
/**
* Updates the score on the page based on the number of correct and incorrect answers.
*
* @return {void} This function does not return a value.
*/
function updateScore() {
const currentParcentageElement =
document.getElementById("current-score");
const correctAnswerElement =
document.getElementById("correct-answers");
const wrongAnswerElement =
document.getElementById("wrong-answers");
correctAnswerElement.innerHTML = correct.size;
wrongAnswerElement.innerHTML = incorrect.size;
const score = Number(
(correct.size / totalNumberOfQuestions) * 100
).toFixed(2);
currentParcentageElement.innerHTML = score;
}
/**
* Handles the event when the submit button is clicked.
*
* @param {Event} e - The event object.
* @return {void} This function does not return anything.
*/
const submitButtonListener = (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const form = e.target;
const selectedOption = e.target.querySelector(
'input[type="radio"]:checked'
);
if (!selectedOption) {
alert("Please select an answer!");
return;
}
let isCorrect = false;
const { answer: userAnswer } = Object.fromEntries(
formData.entries()
);
const correctAnswer = e.target.dataset.correctAnswer;
const questionId = e.target.dataset.questionId;
if (userAnswer == correctAnswer) {
correct.add(questionId);
incorrect.delete(questionId);
isCorrect = true;
} else {
incorrect.add(e.target.dataset.questionId);
correct.delete(questionId);
}
updateScore();
// display the questions
var questionsContainer = document.getElementById("quiz-container");
for (var i = 0; i < questions.length; i++) {
var question = questions[i];
var questionElement = document.createElement("form");
questionElement.className = "question";
questionElement.innerHTML =
"<span>Question " +
(i + 1) +
":</span>" +
'<div class="question-prompt">' +
question.question +
"</div>";
questionElement.id = question.id;
questionElement.classList.add("quiz-container");
var answersElement = document.createElement("ul");
answersElement.className = "answers";
for (var j = 0; j < question.answers.length; j++) {
var answer = question.answers[j];
var answerElement = document.createElement("li");
answerElement.className = "answer";
answerElement.innerHTML =
'<label for="input-' +
question.id +
"-" +
j +
'"><div onclick="select(' +
question.id +
", " +
j +
')"><span></span><input id="input-' +
question.id +
"-" +
j +
'" type="radio" name="question' +
i +
'" value="' +
j +
'">' +
answer +
"</input></div></label>";
answerElement.classList.add("question-answer");
answersElement.appendChild(answerElement);
const resultClass = isCorrect
? "correct-answer"
: "incorrect-answer";
form.querySelectorAll(".question-lable").forEach((label) => {
label.classList.remove(
"correct-answer",
"incorrect-answer"
);
});
selectedOption
.closest(".question-lable")
.classList.add(resultClass);
};
function renderExplanation(ev) {
modalTextElement.innerHTML =
ev.target.dataset?.explanation || "no explanation found";
dialog.showModal();
dialog.addEventListener("click", (event) => {
if (event.target === dialog) {
dialog.close();
}
questionElement.appendChild(answersElement);
// add a submit button
var submitButton = document.createElement("button");
submitButton.className = "submit";
submitButton.innerHTML = "Submit";
submitButton.classList.add("submit-button");
submitButton.addEventListener("click", function (e) {
e.preventDefault();
var questionElement = e.target.parentElement;
var questionId = questionElement.id;
var question = questions.filter(function (q) {
return q.id == questionId;
})[0];
// set the border color of all answers to black
var answers = questionElement.getElementsByClassName("answer");
for (var i = 0; i < answers.length; i++) {
console.log(answers[i]);
answers[i].children[0].style.borderColor = "#1c1d1f";
}
var answer = questionElement.querySelector(
'input[type="radio"]:checked'
);
if (answer) {
var answerIndex = answer.value;
var answerText = question.answers[answerIndex];
if (answerText == question.correctAnswer) {
answer.parentElement.parentElement.style.borderColor =
"limegreen";
// alert("Correct!");
correct++;
updateScore();
} else {
answer.parentElement.parentElement.style.borderColor = "red";
// alert("Incorrect!");
}
} else {
alert("Please select an answer.");
}
});
// add button
questionElement.appendChild(submitButton);
questionsContainer.appendChild(questionElement);
}
}
function select(question, answer) {
var questionElement = document.getElementById(question);
var answers = questionElement.getElementsByClassName("answer");
for (var i = 0; i < answers.length; i++) {
var span = answers[i].children[0].children[0].children[0];
var input = answers[i].children[0].children[0].children[1];
span.classList.remove("selected");
input.checked = false;
}
var span = answers[answer].children[0].children[0].children[0];
var input = answers[answer].children[0].children[0].children[1];
span.classList.add("selected");
input.checked = true;
}
function updateScore() {
var scoreElem = document.getElementById("score");
const score = (correct / total) * 100;
scoreElem.innerHTML = "Score: " + score + "/" + passPercent + "%";
});
}
</script>
</body>
</html>
</html>