Skip to content

Commit

Permalink
sheet js refactoring done
Browse files Browse the repository at this point in the history
  • Loading branch information
angelgardt committed May 23, 2024
1 parent 8f67cb3 commit 1ba5e9d
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 270 deletions.
120 changes: 60 additions & 60 deletions book/_book/debug_sheet.html

Large diffs are not rendered by default.

21 changes: 17 additions & 4 deletions book/_book/js/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ let STATUS = "initial";

// Set messages text
const MESSAGES = {
non_filled: "Нет ни одного ответа :'(",
partially_filled: "Некоторые вопросы остались без ответа ((",
filled: ""
non_filled:
[
"Нет ни одного ответа :'(",
"Нечего проверять, все вопросы пусты..."
],
partially_filled:
[
"Некоторые вопросы остались без ответа ((",
"Надо ответить на все вопросы..."
],
filled: [""]
};

// Extract answers from parsed JSON
Expand Down Expand Up @@ -165,9 +173,14 @@ function set_status(n_non_filled) {
}
}

function getRandomInt(max) {
return Math.floor(Math.random() * max);
}

function show_status() {
if (STATUS != "filled") {
document.getElementById("filled-message").innerHTML = MESSAGES[STATUS];
opt_message = getRandomInt(MESSAGES[STATUS].length)
document.getElementById("filled-message").innerHTML = MESSAGES[STATUS][opt_message];
document.getElementById("filled-message").classList.add("shown");
} else {
document.getElementById("filled-message").classList.remove("shown");
Expand Down
113 changes: 80 additions & 33 deletions book/_book/js/sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ for (let i = 0; i < INFO_JSON.length; i++) {
delete INFO_JSON;


// Add labels
// Add labels and hide autocheck where absent
for (let i = 1; i <= N_tasks; i++) {
document.getElementById("t"+i+"-hints-summary").innerHTML = "Показать подсказки";
if (INFO.level["t"+i] == "") {
document.getElementById("t"+i+"-level").hidden = true;
} else {
Expand All @@ -30,43 +31,89 @@ for (let i = 1; i <= N_tasks; i++) {
document.getElementById("toc-title").innerHTML = "Задания";


// Open hints
// Add events & innet HTML to hints summaries
const HINTS = document.getElementsByClassName("hints");
for (let i = 0; i < N_tasks; i++) {
HINTS[i].addEventListener("click", click_hints);
}

const AUTOCHECK_BUTTONS = document.getElementsByClassName("button-autocheck");
for (let i = 0; i < N_tasks; i++) {
AUTOCHECK_BUTTONS[i].addEventListener("click", check);
}

for (let i = 1; i <= N_tasks; i++) {
document.getElementById("t"+i+"-hints").open = true;

// Set messages text
const MESSAGES = {
empty:
[
"В поле ответа пусто :(",
"Нужно что-то ввести в поле ответа...",
"Нет никакого ответа. Нечего проверять (("
],
incorrect:
[
"Надо проверить вычисления или формат ввода...",
"Кажется, в вычислениях ошибка. Или в формате ввода..."
],
correct:
[
"Верно!",
"Точно так!",
"Исключительно точно!",
"Категорически корректно!"
]
};

function click_hints() {
if (this.open) {
document.getElementById(this.id+"-summary").innerHTML = "Показать подсказки";
} else {
document.getElementById(this.id+"-summary").innerHTML = "Скрыть подсказки";
}
}


// Check answers
function checker(id, ans)
{
let input_task = document.getElementById(id+"-input").value;
// console.log(input_task)
let feedback_task = document.getElementById(id+"-feedback");
if (input_task.trim() == "")
{
feedback_task.style.display = "block";
feedback_task.classList.remove("incorrect", "correct");
feedback_task.classList.add("empty");
feedback_task.innerHTML = "В поле ответа пусто :(";
// feedback_task.style.color = "#4142CE";
} else if (input_task.replaceAll(" ", "") == ans)
{
feedback_task.style.display = "block";
feedback_task.classList.remove("incorrect", "empty");
feedback_task.classList.add("correct");
feedback_task.innerHTML = "Верно!";
// feedback_task.style.color = "#35D250";
} else {
feedback_task.style.display = "block";
feedback_task.classList.remove("correct", "empty");
feedback_task.classList.add("incorrect");
feedback_task.innerHTML = "Надо проверить вычисления…";
// feedback_task.style.color = "#D33E36";
}
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}

function check() {
result = checker(get_answer(this.name), this.name);
show_feedback(this.name, result);
if (result == "correct") {
disable_autocheck(this.name);
}
}

function get_answer(id) {
return document.getElementById(id+"-input").value.replaceAll(" ", "");
}

function checker(answer, id) {
correct_answer = INFO["autocheck_answer"][id];
if (answer == "") {
return "empty"
} else if (answer == correct_answer) {
return "correct"
} else {
return "incorrect"
}
}

function show_feedback(id, result) {
feedback = document.getElementById(id+"-feedback");
feedback.classList.remove("empty", "correct", "incorrect");
feedback.classList.add("shown", result);
opt_feedback = getRandomInt(MESSAGES[result].length);
feedback.innerHTML = MESSAGES[result][opt_feedback];
document.getElementById("toc-"+id+"-title").classList.remove("toc-correct", "toc-incorrect");
if (result != "empty") {
document.getElementById("toc-"+id+"-title").classList.add("toc-"+result);
}
}

function check(id) {
checker(id = id, ans = info["autocheck_answer"][id]);
function disable_autocheck(id) {
document.getElementById(id+"-autocheck-button").disabled = true;
document.getElementById(id+"-input").disabled = true;
}
Loading

0 comments on commit 1ba5e9d

Please sign in to comment.