mirror of
https://cdm-project.com/Download-Tools/udemy-downloader.git
synced 2025-04-30 00:54:25 +02:00
update: quiz_template view
This commit is contained in:
parent
c7329b1d59
commit
db3ab180a4
4
main.py
4
main.py
@ -1676,10 +1676,12 @@ def process_normal_quiz(quiz, lecture, chapter_dir):
|
|||||||
lecture_path = os.path.join(chapter_dir, lecture_file_name)
|
lecture_path = os.path.join(chapter_dir, lecture_file_name)
|
||||||
|
|
||||||
logger.info(f" > Processing quiz {lecture_index}")
|
logger.info(f" > Processing quiz {lecture_index}")
|
||||||
|
|
||||||
with open("quiz_template.html", "r") as f:
|
with open("quiz_template.html", "r") as f:
|
||||||
html = f.read()
|
html = f.read()
|
||||||
quiz_data = {
|
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"),
|
"pass_percent": lecture.get("data").get("pass_percent"),
|
||||||
"questions": quiz["contents"],
|
"questions": quiz["contents"],
|
||||||
}
|
}
|
||||||
|
@ -1,263 +1,515 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Quiz</title>
|
<title> Quiz </title>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
body {
|
* {
|
||||||
font-family: sf pro text, -apple-system, BlinkMacSystemFont, Roboto,
|
font-family: system-ui, -apple-system, BlinkMacSystemFont,
|
||||||
segoe ui, Helvetica, Arial, sans-serif, apple color emoji,
|
"Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans",
|
||||||
segoe ui emoji, segoe ui symbol;
|
"Helvetica Neue", sans-serif;
|
||||||
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;
|
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 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;
|
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;
|
position: relative;
|
||||||
cursor: pointer;
|
background-color: #020617;
|
||||||
display: flex;
|
color: var(--primary-text-color);
|
||||||
min-width: 18rem;
|
|
||||||
border: solid #1c1d1f 2px;
|
|
||||||
}
|
}
|
||||||
.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;
|
display: flex;
|
||||||
align-items: center;
|
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%;
|
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;
|
#quiz-meta-container {
|
||||||
height: 1rem;
|
border-radius: 8px;
|
||||||
margin-right: 1.6rem;
|
background-color: var(--primary-color);
|
||||||
top: 0;
|
margin-bottom: 12px;
|
||||||
border-radius: 50%;
|
padding: 8px;
|
||||||
display: inline-block;
|
|
||||||
flex-shrink: 0;
|
|
||||||
border: 0.2rem solid #1c1d1f;
|
|
||||||
position: relative;
|
|
||||||
}
|
}
|
||||||
.selected {
|
|
||||||
background: #1c1d1f;
|
#quiz-title {
|
||||||
box-shadow: 0 0 0 0.4rem #fff inset;
|
text-align: center;
|
||||||
|
font-size: 24px;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#quiz-description {
|
||||||
|
line-height: 1.5;
|
||||||
|
padding: 2px 6px;
|
||||||
}
|
}
|
||||||
.score {
|
|
||||||
position: fixed;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body onload="main()">
|
<body onload="main()">
|
||||||
<div id="score" class="score">
|
<main>
|
||||||
<span>Score: N/A of N/A</span>
|
<section id="quiz-meta-container">
|
||||||
</div>
|
<h1 id="quiz-title"></h1>
|
||||||
<div id="quiz-container" class="quiz-content"></div>
|
<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>
|
<script>
|
||||||
const quizData = __data_placeholder__;
|
const quizData = __data_placeholder__;
|
||||||
var correct = 0;
|
let correct = new Set();
|
||||||
var total = 0;
|
let incorrect = new Set();
|
||||||
const questionData = quizData.questions
|
let totalNumberOfQuestions = 0;
|
||||||
const passPercent = quizData.pass_percent
|
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() {
|
function main() {
|
||||||
total = questionData.length;
|
// update quiz meta data
|
||||||
|
document.title = quizTitle;
|
||||||
|
quizTitleElement.innerHTML = quizTitle;
|
||||||
|
quizDescriptionElement.innerHTML = quizDescription;
|
||||||
|
|
||||||
var questions = [];
|
const passPercentElement =
|
||||||
for (var i = 0; i < questionData.length; i++) {
|
document.getElementById("pass-percent");
|
||||||
var question = questionData[i];
|
passPercentElement.innerHTML = passPercent + "%";
|
||||||
var questionText = question.prompt.question;
|
totalNumberOfQuestions = questionData.length;
|
||||||
var answers = question.prompt.answers;
|
// shuffle the questionData to randomize the order of the questions
|
||||||
var correctAnswer = question.correct_response[0];
|
for (let i = questionData.length - 1; i > 0; i--) {
|
||||||
var correctAnswerText = answers[correctAnswer.charCodeAt(0) - 97];
|
const j = Math.floor(Math.random() * (i + 1));
|
||||||
var questionObj = {
|
[questionData[i], questionData[j]] = [
|
||||||
question: questionText,
|
questionData[j],
|
||||||
correctAnswer: correctAnswerText,
|
questionData[i],
|
||||||
answers: answers,
|
];
|
||||||
id: question.id,
|
|
||||||
};
|
|
||||||
questions.push(questionObj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
updateScore();
|
||||||
|
|
||||||
// display the questions
|
const resultClass = isCorrect
|
||||||
var questionsContainer = document.getElementById("quiz-container");
|
? "correct-answer"
|
||||||
for (var i = 0; i < questions.length; i++) {
|
: "incorrect-answer";
|
||||||
var question = questions[i];
|
|
||||||
var questionElement = document.createElement("form");
|
form.querySelectorAll(".question-lable").forEach((label) => {
|
||||||
questionElement.className = "question";
|
label.classList.remove(
|
||||||
questionElement.innerHTML =
|
"correct-answer",
|
||||||
"<span>Question " +
|
"incorrect-answer"
|
||||||
(i + 1) +
|
);
|
||||||
":</span>" +
|
});
|
||||||
'<div class="question-prompt">' +
|
selectedOption
|
||||||
question.question +
|
.closest(".question-lable")
|
||||||
"</div>";
|
.classList.add(resultClass);
|
||||||
questionElement.id = question.id;
|
};
|
||||||
questionElement.classList.add("quiz-container");
|
|
||||||
var answersElement = document.createElement("ul");
|
function renderExplanation(ev) {
|
||||||
answersElement.className = "answers";
|
modalTextElement.innerHTML =
|
||||||
for (var j = 0; j < question.answers.length; j++) {
|
ev.target.dataset?.explanation || "no explanation found";
|
||||||
var answer = question.answers[j];
|
dialog.showModal();
|
||||||
var answerElement = document.createElement("li");
|
dialog.addEventListener("click", (event) => {
|
||||||
answerElement.className = "answer";
|
if (event.target === dialog) {
|
||||||
answerElement.innerHTML =
|
dialog.close();
|
||||||
'<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);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
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>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user