From 5fffe51c4e17236a44739ec74009fefa850c1bee Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Thu, 15 Jun 2023 11:18:17 +0530 Subject: [PATCH 1/3] fix: quiz in lessons --- lms/lms/doctype/lms_quiz/lms_quiz.py | 8 +-- lms/www/batch/edit.js | 94 +++++++++++++++++++++++----- 2 files changed, 80 insertions(+), 22 deletions(-) diff --git a/lms/lms/doctype/lms_quiz/lms_quiz.py b/lms/lms/doctype/lms_quiz/lms_quiz.py index 41da49a6..66ea1a3e 100644 --- a/lms/lms/doctype/lms_quiz/lms_quiz.py +++ b/lms/lms/doctype/lms_quiz/lms_quiz.py @@ -7,8 +7,7 @@ import frappe from frappe import _ from frappe.model.document import Document from frappe.utils import cstr - -from lms.lms.utils import generate_slug +from lms.lms.utils import generate_slug, has_course_moderator_role class LMSQuiz(Document): @@ -263,6 +262,5 @@ def check_input_answers(question, answer): @frappe.whitelist() def get_user_quizzes(): - return frappe.get_all( - "LMS Quiz", filters={"owner": frappe.session.user}, fields=["name", "title"] - ) + filters = {} if has_course_moderator_role() else {"owner": frappe.session.user} + return frappe.get_all("LMS Quiz", filters=filters, fields=["name", "title"]) diff --git a/lms/www/batch/edit.js b/lms/www/batch/edit.js index 2e9145a1..c8f3c067 100644 --- a/lms/www/batch/edit.js +++ b/lms/www/batch/edit.js @@ -6,7 +6,7 @@ frappe.ready(() => { } setup_editor(); - //fetch_quiz_list(); + fetch_quiz_list(); $("#save-lesson").click((e) => { save_lesson(e); @@ -48,6 +48,7 @@ const setup_editor = () => { const parse_string_to_lesson = () => { let lesson_content = $("#current-lesson-content").html(); let lesson_blocks = []; + this.quiz_in_lesson = []; lesson_content.split("\n").forEach((block) => { if (block.includes("{{ YouTubeVideo")) { @@ -60,6 +61,7 @@ const parse_string_to_lesson = () => { }); } else if (block.includes("{{ Quiz")) { let quiz = block.match(/'([^']+)'/)[1]; + this.quiz_in_lesson.push(quiz); lesson_blocks.push({ type: "quiz", data: { @@ -218,7 +220,7 @@ class YouTubeVideo { } render_youtube_dialog() { - let self = this; + let me = this; let youtubedialog = new frappe.ui.Dialog({ title: __("YouTube Video"), fields: [ @@ -245,8 +247,8 @@ class YouTubeVideo { primary_action_label: __("Insert"), primary_action(values) { youtubedialog.hide(); - self.youtube = values.youtube; - $(self.wrapper).html(self.render_youtube(values.youtube)); + me.youtube = values.youtube; + $(me.wrapper).html(me.render_youtube(values.youtube)); }, }); youtubedialog.show(); @@ -286,6 +288,54 @@ class Quiz { this.data = data; } + get_fields() { + let fields = [ + { + fieldname: "quiz_information", + fieldtype: "HTML", + label: __("Create New"), + options: __( + "Click to create a new quiz to add to this lesson." + ), + }, + { + fieldname: "create_quiz", + fieldtype: "Button", + label: __("Create Quiz"), + click: () => { + window.location.href = "/quizzes"; + }, + }, + { + fieldname: "quiz_list_section", + fieldtype: "Section Break", + }, + { + fieldname: "quiz_list_information", + fieldtype: "HTML", + label: __("Quiz List"), + options: __("Select a quiz to add to this lesson."), + }, + ]; + let break_index = Math.ceil(self.quiz_list.length / 2); + + self.quiz_list.forEach((quiz) => { + fields.push({ + fieldname: quiz.name, + fieldtype: "Check", + label: quiz.title, + default: self.quiz_in_lesson.includes(quiz.name) ? 1 : 0, + read_only: self.quiz_in_lesson.includes(quiz.name) ? 1 : 0, + }); + }); + + fields.splice(break_index, 0, { + fieldname: "column_break", + fieldtype: "Column Break", + }); + return fields; + } + render() { this.wrapper = document.createElement("div"); if (this.data && this.data.quiz) { @@ -297,23 +347,17 @@ class Quiz { } render_quiz_dialog() { - let self = this; + let me = this; + let fields = this.get_fields(); let quizdialog = new frappe.ui.Dialog({ - title: __("Select a Quiz"), - fields: [ - { - fieldname: "quiz", - fieldtype: "Link", - label: __("Quiz"), - reqd: 1, - options: "LMS Quiz", - }, - ], + title: __("Manage Quiz"), + fields: fields, primary_action_label: __("Insert"), primary_action(values) { - self.quiz = values.quiz; + me.analyze_quiz_list(values); quizdialog.hide(); - $(self.wrapper).html(self.render_quiz(self.quiz)); + /* self.quiz = values.quiz; + $(self.wrapper).html(self.render_quiz(self.quiz)); */ }, secondary_action_label: __("Create New"), secondary_action: () => { @@ -327,6 +371,22 @@ class Quiz { }, 1000); } + analyze_quiz_list(values) { + /* If quiz is selected and is not already in the lesson then render it. + If quiz is in the lesson and is unselected then unrender it */ + + Object.keys(values).forEach((key) => { + if (values[key] === 1 && !self.quiz_in_lesson.includes(key)) { + self.quiz_in_lesson.push(key); + this.quiz = key; + $(this.wrapper).html(this.render_quiz(key)); + } else if (values[key] == 0 && self.quiz_in_lesson.includes(key)) { + self.quiz_in_lesson.splice(self.quiz_in_lesson.indexOf(key), 1); + $(this.wrapper).html(this.render_quiz(key)); + } + }); + } + render_quiz(quiz) { return `
Quiz: ${quiz} From 35c080fcc206322ec4ff50589fe76d565e2e96f4 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Fri, 16 Jun 2023 13:08:49 +0530 Subject: [PATCH 2/3] fix: quiz dialog display --- lms/www/batch/edit.js | 59 +++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/lms/www/batch/edit.js b/lms/www/batch/edit.js index c8f3c067..106c631a 100644 --- a/lms/www/batch/edit.js +++ b/lms/www/batch/edit.js @@ -65,7 +65,7 @@ const parse_string_to_lesson = () => { lesson_blocks.push({ type: "quiz", data: { - quiz: quiz, + quiz: [quiz], }, }); } else if (block.includes("{{ Video")) { @@ -118,7 +118,9 @@ const parse_lesson_to_string = (data) => { if (block.type == "youtube") { lesson_content += `{{ YouTubeVideo("${block.data.youtube}") }}\n`; } else if (block.type == "quiz") { - lesson_content += `{{ Quiz("${block.data.quiz}") }}\n`; + block.data.quiz.forEach((quiz) => { + lesson_content += `{{ Quiz("${quiz}") }}\n`; + }); } else if (block.type == "upload") { let url = block.data.file_url; lesson_content += block.data.is_video @@ -291,11 +293,10 @@ class Quiz { get_fields() { let fields = [ { - fieldname: "quiz_information", - fieldtype: "HTML", - label: __("Create New"), - options: __( - "Click to create a new quiz to add to this lesson." + fieldname: "start_section", + fieldtype: "Section Break", + label: __( + "To create a new quiz, click on the button below. Once you have created the new quiz you can come back to this lesson and add it from here." ), }, { @@ -307,17 +308,17 @@ class Quiz { }, }, { - fieldname: "quiz_list_section", - fieldtype: "Section Break", + fieldname: "quiz_information", + fieldtype: "HTML", + options: __("OR"), }, { - fieldname: "quiz_list_information", - fieldtype: "HTML", - label: __("Quiz List"), - options: __("Select a quiz to add to this lesson."), + fieldname: "quiz_list_section", + fieldtype: "Section Break", + label: __("Select a exisitng quiz to add to this lesson."), }, ]; - let break_index = Math.ceil(self.quiz_list.length / 2); + let break_index = Math.ceil(self.quiz_list.length / 2) + 4; self.quiz_list.forEach((quiz) => { fields.push({ @@ -339,7 +340,7 @@ class Quiz { render() { this.wrapper = document.createElement("div"); if (this.data && this.data.quiz) { - $(this.wrapper).html(this.render_quiz(this.data.quiz)); + $(this.wrapper).html(this.render_quiz()); } else { this.render_quiz_dialog(); } @@ -359,7 +360,7 @@ class Quiz { /* self.quiz = values.quiz; $(self.wrapper).html(self.render_quiz(self.quiz)); */ }, - secondary_action_label: __("Create New"), + secondary_action_label: __("Create New Quiz"), secondary_action: () => { window.location.href = `/quizzes`; }, @@ -375,31 +376,35 @@ class Quiz { /* If quiz is selected and is not already in the lesson then render it. If quiz is in the lesson and is unselected then unrender it */ + this.quiz_to_render = []; Object.keys(values).forEach((key) => { if (values[key] === 1 && !self.quiz_in_lesson.includes(key)) { self.quiz_in_lesson.push(key); - this.quiz = key; - $(this.wrapper).html(this.render_quiz(key)); - } else if (values[key] == 0 && self.quiz_in_lesson.includes(key)) { - self.quiz_in_lesson.splice(self.quiz_in_lesson.indexOf(key), 1); - $(this.wrapper).html(this.render_quiz(key)); + this.quiz_to_render.push(key); } }); + + $(this.wrapper).html(this.render_quiz()); } - render_quiz(quiz) { - return `
- Quiz: ${quiz} -
`; + render_quiz() { + let html = ``; + let quiz_list = this.data.quiz || this.quiz_to_render; + quiz_list.forEach((quiz) => { + html += `
+ Quiz: ${quiz} +
`; + }); + return html; } validate(savedData) { - return !savedData.quiz || !savedData.quiz.trim() ? false : true; + return !savedData.quiz || !savedData.quiz.length ? false : true; } save(block_content) { return { - quiz: this.data.quiz || this.quiz, + quiz: this.data.quiz || this.quiz_to_render, }; } } From b1ab7e778328efa5b7705e2f1a8e2d9ec1414236 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Fri, 16 Jun 2023 13:44:55 +0530 Subject: [PATCH 3/3] fix: removed unnecesary comments --- lms/www/batch/edit.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lms/www/batch/edit.js b/lms/www/batch/edit.js index 106c631a..285fc2d8 100644 --- a/lms/www/batch/edit.js +++ b/lms/www/batch/edit.js @@ -357,8 +357,6 @@ class Quiz { primary_action(values) { me.analyze_quiz_list(values); quizdialog.hide(); - /* self.quiz = values.quiz; - $(self.wrapper).html(self.render_quiz(self.quiz)); */ }, secondary_action_label: __("Create New Quiz"), secondary_action: () => { @@ -373,8 +371,7 @@ class Quiz { } analyze_quiz_list(values) { - /* If quiz is selected and is not already in the lesson then render it. - If quiz is in the lesson and is unselected then unrender it */ + /* If quiz is selected and is not already in the lesson then render it.*/ this.quiz_to_render = []; Object.keys(values).forEach((key) => {