diff --git a/Public/assets/js/flash-card/draft/create.js b/Public/assets/js/flash-card/draft/create.js index 8db2dfd..666d7eb 100644 --- a/Public/assets/js/flash-card/draft/create.js +++ b/Public/assets/js/flash-card/draft/create.js @@ -1,13 +1,13 @@ function createDraft() { - try { + jsonData().then(function (jsonData){ fetch("/api/notes", { method: "POST", headers: { "Accept": "application/json, text/plain, */*", "Content-Type" : "application/json" }, - body: jsonData() + body: jsonData }) .then(function (response) { if (response.ok) { @@ -21,12 +21,10 @@ function createDraft() { .then(function (json) { fetchNote(parseInt(json)); }) - .catch(function (error) { - presentErrorMessage(error.message); - }); - } catch(error) { + }) + .catch(function (error) { presentErrorMessage(error.message); - } + }); } function fetchNote(id) { @@ -80,7 +78,6 @@ function editJsonData() { } function resetCreateForm() { - $("#card-topic-id").val(""); $("#card-question").val(""); solution.value(""); } @@ -130,4 +127,27 @@ function subjectID() { path.indexOf(splitURI) + splitURI.length, path.lastIndexOf("/tasks/") )); +} + +function createSession() { + return fetch("/api/note-taking-sessions", { + method: "POST", + headers: { + "Accept": "application/json, text/plain, */*", + "Content-Type" : "application/json" + } + }) + .then(function (response) { + if (response.ok) { + return response.json(); + } else if (response.status == 400) { + throw new Error("Sjekk at all nødvendig info er fylt ut"); + } else { + throw new Error(response.statusText); + } + }) + .then(function (json) { + $("#note-session").val(json["id"]); + return json + }) } \ No newline at end of file diff --git a/Public/assets/js/flash-card/draft/json-data.js b/Public/assets/js/flash-card/draft/json-data.js index a6f40fd..926a3ab 100644 --- a/Public/assets/js/flash-card/draft/json-data.js +++ b/Public/assets/js/flash-card/draft/json-data.js @@ -1,20 +1,35 @@ function jsonData() { - let noteSession = $("#note-session").val(); - var subtopicId = parseInt($("#card-topic-id").val()); - var question = $("#card-question").val(); - var solutionValue = solution.value(); - if (isNaN(subtopicId) || subtopicId < 1) { - throw Error("Velg et tema"); - } - if (question.length < 1) { - throw Error("Du må skrive inn et spørsmål"); - } + return new Promise(function (resolve, reject) { + let noteSession = $("#note-session").val(); - return JSON.stringify({ - "noteSession" : noteSession, - "subtopicID" : subtopicId, - "question" : question, - "solution" : solutionValue - }); + var subtopicId = parseInt($("#card-topic-id").val()); + var question = $("#card-question").val(); + var solutionValue = solution.value(); + + if (isNaN(subtopicId) || subtopicId < 1) { + reject(Error("Velg et tema")); + } + if (question.length < 1) { + reject(Error("Du må skrive inn et spørsmål")); + } + + if (noteSession == "" || noteSession == null) { + createSession().then(function (json) { + resolve(JSON.stringify({ + "noteSession" : json["id"], + "subtopicID" : subtopicId, + "question" : question, + "solution" : solutionValue + })) + }) + } else { + resolve(JSON.stringify({ + "noteSession" : noteSession, + "subtopicID" : subtopicId, + "question" : question, + "solution" : solutionValue + })) + } + }) } \ No newline at end of file diff --git a/Public/assets/js/flash-card/submit-performance.js b/Public/assets/js/flash-card/submit-performance.js index 19f0a29..9a15ee3 100644 --- a/Public/assets/js/flash-card/submit-performance.js +++ b/Public/assets/js/flash-card/submit-performance.js @@ -3,7 +3,7 @@ var now = new Date(); var isSubmitting = false; var hasSubmittedSucessfully = false; -var knowledgeScore = 0; +var knowledgeScore = 2; var didSubmitt = false; var nextIndex=1; @@ -163,7 +163,7 @@ function estimatedScore(shouldSetScore) { "Accept": "application/json, text/plain, */*", "Content-Type" : "application/json" }, - body: answerJsonData("1") + body: answerJsonData("3") }) .then(function (response) { if (response.ok) { @@ -187,7 +187,7 @@ function estimatedScore(shouldSetScore) { } else if (roundedScore >= 2) { text += " kan noe 🙌" } else { - text += " burde lese litt mer 🤔" + text += " kanskje burde lese litt mer 🤔" } $("#estimate-spinner").addClass("d-none"); diff --git a/Public/assets/js/lecture-note-recap-session/create.js b/Public/assets/js/lecture-note-recap-session/create.js new file mode 100644 index 0000000..4047eb3 --- /dev/null +++ b/Public/assets/js/lecture-note-recap-session/create.js @@ -0,0 +1,34 @@ + +function recapSessionJsonData() { + let sessionID = $("#note-session").val(); + let numberOfNotes = Math.min($("#notes").children.length, 5); + + return JSON.stringify({ + "sessionID": sessionID, + "numberOfTasks": numberOfNotes + }) +} + +function startRecapSession() { + fetch("/api/lecture-note-recap", { + method: "POST", + headers: { + "Accept": "application/json, text/plain, */*", + "Content-Type" : "application/json" + }, + body: recapSessionJsonData() + }) + .then(function (response) { + if (response.ok) { + console.log(response); + return response.json(); + } else if (response.status == 400) { + throw new Error("Sjekk at all nødvendig info er fylt ut"); + } else { + throw new Error(response.statusText); + } + }) + .then(function (sessionID) { + window.location = "/lecture-note-recap/" + sessionID + "/tasks/0"; + }) +} \ No newline at end of file diff --git a/Public/assets/js/lecture-note-recap-session/submit.js b/Public/assets/js/lecture-note-recap-session/submit.js new file mode 100644 index 0000000..3329901 --- /dev/null +++ b/Public/assets/js/lecture-note-recap-session/submit.js @@ -0,0 +1,183 @@ +var startDate = new Date(); +var now = new Date(); + +var isSubmitting = false; +var hasSubmittedSucessfully = false; +var knowledgeScore = 2; +var didSubmitt = false; + +function revealSolution() { + didSubmitt = $("#solution").hasClass("d-none"); + presentControllsAndKnowledge(); + + if ($("#solution").hasClass("d-none")) { + now = new Date(); + $("#nextButton").removeClass("d-none"); + var goalValue = parseFloat($("#goal-value").text()); + var porgressBarValue = parseFloat($("#goal-progress-bar").attr("aria-valuenow")) + var currentCompleted = porgressBarValue / 100 * goalValue; + var progress = parseInt(Math.ceil((currentCompleted + 1) * 100 / goalValue)); + + if (!isNaN(progress)) { + $("#goal-progress-label").text(progress + "% "); + $("#goal-progress-bar").attr("aria-valuenow", progress); + $("#goal-progress-bar").attr("style", "width: " + progress + "%;"); + if (progress >= 100) { + $("#goal-progress-bar").addClass("bg-success"); + } + } + submitPerformance(knowledgeScore) + } +} + +function updateScoreButton() { + if (knowledgeScore < 2) { + $("#" + knowledgeScore).attr("class", "btn btn-danger"); + } else if (knowledgeScore < 4) { + $("#" + knowledgeScore).attr("class", "btn btn-warning"); + } else { + $("#" + knowledgeScore).attr("class", "btn btn-success"); + } +} + +function registerScore(score) { + $("#" + knowledgeScore).attr("class", "btn btn-light"); + knowledgeScore = score; + updateScoreButton() + submitPerformance(knowledgeScore) +} + +function submitAndEndSession() { + endSession() +} + +function submitPerformance(score) { + + if (isSubmitting) { + return + } + isSubmitting = true; + + var url = "/api/lecture-note-recap/" + sessionID() + "/tasks/" + taskIndex() + "/submit"; + + fetch(url, { + method: "POST", + headers: { + "Accept": "application/json, text/plain, */*", + "Content-Type" : "application/json" + }, + body: answerJsonData(score) + }) + .then(function (response) { + isSubmitting = false; + if (response.ok) { + return + } else { + throw new Error(response.statusText); + } + }) + .catch(function (error) { + isSubmitting = false; + $("#submitButton").attr("disabled", false); + $("#error-massage").text(error.message); + $("#error-div").fadeIn(); + $("#error-div").removeClass("d-none"); + }); +} + +function presentControlls() { + estimatedScore(didSubmitt); + $("#flash-card-answer").prop('readonly', true) + $("#submitButton").prop('disabled', true) + $("#nextButton").removeClass("d-none"); + $(".reveal").each(function () { + $(this).fadeIn(); + $(this).removeClass("d-none"); + }); + fetchSolutions(); + $("#knowledge-card").removeClass("d-none"); + updateScoreButton() +} + +function presentControllsAndKnowledge() { + presentControlls() + hasSubmitted = false; + hasSubmittedSucessfully = false; +} + + +Number.prototype.toMinuteString = function() { + var minutes = Math.floor((this % (1000 * 60 * 60)) / (1000 * 60)); + var seconds = Math.floor((this % (1000 * 60)) / 1000); + if (minutes < 10) { minutes = "0" + minutes; } + if (seconds < 10) { seconds = "0" + seconds; } + return minutes + ":" + seconds; +} + +function endSession() { + $("#end-session-form").submit(); +} + +function answerJsonData(score) { + var timeUsed = (now.getTime() - startDate.getTime()) / 1000; + let knowledge = parseFloat(score); + let submitedAnswer = $("#flash-card-answer").val(); + + if (knowledge == null) { + return + } + return JSON.stringify({ + "timeUsed" : timeUsed, + "knowledge": knowledge / 4, + "answer": submitedAnswer + }); +} + +function estimatedScore(shouldSetScore) { + let url = "/api/lecture-note-recap/" + sessionID() + "/tasks/" + taskIndex() + "/estimate"; + + $("#estimated-score-card").fadeIn(); + $("#estimated-score-card").removeClass("d-none"); + $("#estimate-spinner").html('