diff --git a/.github/helper/install.sh b/.github/helper/install.sh index b5661726..21bb9d9a 100644 --- a/.github/helper/install.sh +++ b/.github/helper/install.sh @@ -43,4 +43,4 @@ build_pid=$! bench --site lms.test reinstall --yes bench --site lms.test install-app lms -wait $build_pid +wait $build_pid \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aafbfa01..01ee036a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -77,5 +77,4 @@ jobs: run: bench --site frappe.local build - name: run tests working-directory: /home/runner/frappe-bench - run: bench --site frappe.local run-tests --app lms - + run: bench --site frappe.local run-tests --app lms \ No newline at end of file diff --git a/lms/lms/doctype/lms_assignment/lms_assignment.py b/lms/lms/doctype/lms_assignment/lms_assignment.py index 647833de..b4aaedbf 100644 --- a/lms/lms/doctype/lms_assignment/lms_assignment.py +++ b/lms/lms/doctype/lms_assignment/lms_assignment.py @@ -3,7 +3,7 @@ import frappe from frappe.model.document import Document -from lms.lms.utils import can_create_courses +from lms.lms.utils import has_course_moderator_role, has_course_instructor_role class LMSAssignment(Document): @@ -12,7 +12,7 @@ class LMSAssignment(Document): @frappe.whitelist() def save_assignment(assignment, title, type, question): - if not can_create_courses(): + if not has_course_moderator_role() or not has_course_instructor_role(): return if assignment: diff --git a/lms/lms/doctype/lms_batch/lms_batch.js b/lms/lms/doctype/lms_batch/lms_batch.js index f7273fc1..1a95c493 100644 --- a/lms/lms/doctype/lms_batch/lms_batch.js +++ b/lms/lms/doctype/lms_batch/lms_batch.js @@ -12,12 +12,16 @@ frappe.ui.form.on("LMS Batch", { }); frm.set_query("reference_doctype", "timetable", function () { - let doctypes = [ - "Course Lesson", - "LMS Quiz", - "LMS Assignment", - "LMS Live Class", - ]; + let doctypes = ["Course Lesson", "LMS Quiz", "LMS Assignment"]; + return { + filters: { + name: ["in", doctypes], + }, + }; + }); + + frm.set_query("reference_doctype", "timetable_legends", function () { + let doctypes = ["Course Lesson", "LMS Quiz", "LMS Assignment"]; return { filters: { name: ["in", doctypes], @@ -27,36 +31,41 @@ frappe.ui.form.on("LMS Batch", { }, timetable_template: function (frm) { - if (frm.doc.timetable_template) { - frm.clear_table("timetable"); - frm.refresh_fields(); - - frappe.call({ - method: "frappe.client.get_list", - args: { - doctype: "LMS Batch Timetable", - parent: "LMS Timetable Template", - fields: [ - "reference_doctype", - "reference_docname", - "day", - "start_time", - "end_time", - "duration", - ], - filters: { - parent: frm.doc.timetable_template, - }, - order_by: "idx", - }, - callback: (data) => { - add_timetable_rows(frm, data.message); - }, - }); - } + set_timetable(frm); }, }); +const set_timetable = (frm) => { + if (frm.doc.timetable_template) { + frm.clear_table("timetable"); + frm.refresh_fields(); + + frappe.call({ + method: "frappe.client.get_list", + args: { + doctype: "LMS Batch Timetable", + parent: "LMS Timetable Template", + fields: [ + "reference_doctype", + "reference_docname", + "day", + "start_time", + "end_time", + "duration", + ], + filters: { + parent: frm.doc.timetable_template, + parenttype: "LMS Timetable Template", + }, + order_by: "idx", + }, + callback: (data) => { + add_timetable_rows(frm, data.message); + }, + }); + } +}; + const add_timetable_rows = (frm, timetable) => { timetable.forEach((row) => { let child = frm.add_child("timetable"); @@ -75,5 +84,40 @@ const add_timetable_rows = (frm, timetable) => { child.duration = row.duration; }); frm.refresh_field("timetable"); + + set_legends(frm); +}; + +const set_legends = (frm) => { + if (frm.doc.timetable_template) { + frm.clear_table("timetable_legends"); + frm.refresh_fields(); + frappe.call({ + method: "frappe.client.get_list", + args: { + doctype: "LMS Timetable Legend", + parent: "LMS Timetable Template", + fields: ["reference_doctype", "label", "color"], + filters: { + parent: frm.doc.timetable_template, + parenttype: "LMS Timetable Template", + }, + order_by: "idx", + }, + callback: (data) => { + add_legend_rows(frm, data.message); + }, + }); + } +}; + +const add_legend_rows = (frm, legends) => { + legends.forEach((row) => { + let child = frm.add_child("timetable_legends"); + child.reference_doctype = row.reference_doctype; + child.label = row.label; + child.color = row.color; + }); + frm.refresh_field("timetable_legends"); frm.save(); }; diff --git a/lms/lms/doctype/lms_batch/lms_batch.json b/lms/lms/doctype/lms_batch/lms_batch.json index 4cdb1e0a..f0838a4b 100644 --- a/lms/lms/doctype/lms_batch/lms_batch.json +++ b/lms/lms/doctype/lms_batch/lms_batch.json @@ -35,8 +35,10 @@ "timetable_template", "column_break_anya", "show_live_class", + "allow_future", "section_break_ontp", "timetable", + "timetable_legends", "pricing_tab", "section_break_gsac", "paid_batch", @@ -220,7 +222,7 @@ "default": "0", "fieldname": "show_live_class", "fieldtype": "Check", - "label": "Show Live Class" + "label": "Show live class" }, { "fieldname": "section_break_ontp", @@ -263,11 +265,23 @@ "fieldtype": "Code", "label": "Custom Script (JavaScript)", "options": "Javascript" + }, + { + "fieldname": "timetable_legends", + "fieldtype": "Table", + "label": "Timetable Legends", + "options": "LMS Timetable Legend" + }, + { + "default": "1", + "fieldname": "allow_future", + "fieldtype": "Check", + "label": "Allow accessing future dates" } ], "index_web_pages_for_search": 1, "links": [], - "modified": "2023-09-28 12:18:34.418812", + "modified": "2023-10-12 12:53:37.351989", "modified_by": "Administrator", "module": "LMS", "name": "LMS Batch", diff --git a/lms/lms/doctype/lms_course/lms_course.py b/lms/lms/doctype/lms_course/lms_course.py index 74ccc51a..9065b639 100644 --- a/lms/lms/doctype/lms_course/lms_course.py +++ b/lms/lms/doctype/lms_course/lms_course.py @@ -216,7 +216,7 @@ def save_course( course_price=None, currency=None, ): - if not can_create_courses(): + if not can_create_courses(course): return if course: diff --git a/lms/lms/doctype/lms_question/lms_question.py b/lms/lms/doctype/lms_question/lms_question.py index f2076284..ede8a070 100644 --- a/lms/lms/doctype/lms_question/lms_question.py +++ b/lms/lms/doctype/lms_question/lms_question.py @@ -8,7 +8,7 @@ from frappe.model.document import Document class LMSQuestion(Document): def validate(self): - self.validate_correct_answers() + validate_correct_answers(self) def validate_correct_answers(question): diff --git a/lms/lms/doctype/lms_quiz/lms_quiz.py b/lms/lms/doctype/lms_quiz/lms_quiz.py index facbffca..d91490f8 100644 --- a/lms/lms/doctype/lms_quiz/lms_quiz.py +++ b/lms/lms/doctype/lms_quiz/lms_quiz.py @@ -6,8 +6,12 @@ import frappe from frappe import _ from frappe.model.document import Document from frappe.utils import cstr -from lms.lms.utils import generate_slug, has_course_moderator_role, can_create_courses from lms.lms.doctype.lms_question.lms_question import validate_correct_answers +from lms.lms.utils import ( + generate_slug, + has_course_moderator_role, + has_course_instructor_role, +) class LMSQuiz(Document): @@ -74,7 +78,7 @@ def quiz_summary(quiz, results): def save_quiz( quiz_title, max_attempts=1, quiz=None, show_answers=1, show_submission_history=0 ): - if not can_create_courses(): + if not has_course_moderator_role() or not has_course_instructor_role(): return values = { diff --git a/lms/lms/doctype/lms_timetable_legend/__init__.py b/lms/lms/doctype/lms_timetable_legend/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.js b/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.js new file mode 100644 index 00000000..7a8a3684 --- /dev/null +++ b/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.js @@ -0,0 +1,8 @@ +// Copyright (c) 2023, Frappe and contributors +// For license information, please see license.txt + +// frappe.ui.form.on("LMS Timetable Legend", { +// refresh(frm) { + +// }, +// }); diff --git a/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.json b/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.json new file mode 100644 index 00000000..6ae0ffe4 --- /dev/null +++ b/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.json @@ -0,0 +1,52 @@ +{ + "actions": [], + "allow_rename": 1, + "autoname": "hash", + "creation": "2023-10-11 16:36:45.079267", + "default_view": "List", + "doctype": "DocType", + "editable_grid": 1, + "engine": "InnoDB", + "field_order": [ + "reference_doctype", + "label", + "color" + ], + "fields": [ + { + "fieldname": "reference_doctype", + "fieldtype": "Link", + "in_list_view": 1, + "label": "Reference DocType", + "options": "DocType", + "reqd": 1 + }, + { + "fieldname": "color", + "fieldtype": "Color", + "in_list_view": 1, + "label": "Color", + "reqd": 1 + }, + { + "fieldname": "label", + "fieldtype": "Data", + "in_list_view": 1, + "label": "Label", + "reqd": 1 + } + ], + "index_web_pages_for_search": 1, + "istable": 1, + "links": [], + "modified": "2023-10-11 17:15:37.039139", + "modified_by": "Administrator", + "module": "LMS", + "name": "LMS Timetable Legend", + "naming_rule": "Random", + "owner": "Administrator", + "permissions": [], + "sort_field": "modified", + "sort_order": "DESC", + "states": [] +} \ No newline at end of file diff --git a/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.py b/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.py new file mode 100644 index 00000000..c842e3a2 --- /dev/null +++ b/lms/lms/doctype/lms_timetable_legend/lms_timetable_legend.py @@ -0,0 +1,9 @@ +# Copyright (c) 2023, Frappe and contributors +# For license information, please see license.txt + +# import frappe +from frappe.model.document import Document + + +class LMSTimetableLegend(Document): + pass diff --git a/lms/lms/doctype/lms_timetable_legend/test_lms_timetable_legend.py b/lms/lms/doctype/lms_timetable_legend/test_lms_timetable_legend.py new file mode 100644 index 00000000..816b1793 --- /dev/null +++ b/lms/lms/doctype/lms_timetable_legend/test_lms_timetable_legend.py @@ -0,0 +1,9 @@ +# Copyright (c) 2023, Frappe and Contributors +# See license.txt + +# import frappe +from frappe.tests.utils import FrappeTestCase + + +class TestLMSTimetableLegend(FrappeTestCase): + pass diff --git a/lms/lms/doctype/lms_timetable_template/lms_timetable_template.js b/lms/lms/doctype/lms_timetable_template/lms_timetable_template.js index a9bff04e..b5ec2d9b 100644 --- a/lms/lms/doctype/lms_timetable_template/lms_timetable_template.js +++ b/lms/lms/doctype/lms_timetable_template/lms_timetable_template.js @@ -11,5 +11,19 @@ frappe.ui.form.on("LMS Timetable Template", { }, }; }); + + frm.set_query("reference_doctype", "timetable_legends", function () { + let doctypes = [ + "Course Lesson", + "LMS Quiz", + "LMS Assignment", + "LMS Live Class", + ]; + return { + filters: { + name: ["in", doctypes], + }, + }; + }); }, }); diff --git a/lms/lms/doctype/lms_timetable_template/lms_timetable_template.json b/lms/lms/doctype/lms_timetable_template/lms_timetable_template.json index 99c88628..3b016b7a 100644 --- a/lms/lms/doctype/lms_timetable_template/lms_timetable_template.json +++ b/lms/lms/doctype/lms_timetable_template/lms_timetable_template.json @@ -8,7 +8,8 @@ "engine": "InnoDB", "field_order": [ "title", - "timetable" + "timetable", + "timetable_legends" ], "fields": [ { @@ -21,11 +22,17 @@ "fieldtype": "Table", "label": "Timetable", "options": "LMS Batch Timetable" + }, + { + "fieldname": "timetable_legends", + "fieldtype": "Table", + "label": "Timetable Legends", + "options": "LMS Timetable Legend" } ], "index_web_pages_for_search": 1, "links": [], - "modified": "2023-09-18 17:57:15.819072", + "modified": "2023-10-11 17:09:05.096243", "modified_by": "Administrator", "module": "LMS", "name": "LMS Timetable Template", diff --git a/lms/lms/utils.py b/lms/lms/utils.py index b3d4e1b9..f7dbe489 100644 --- a/lms/lms/utils.py +++ b/lms/lms/utils.py @@ -521,21 +521,35 @@ def has_course_instructor_role(member=None): ) -def can_create_courses(member=None): +def can_create_courses(course, member=None): if not member: member = frappe.session.user + instructors = frappe.get_all( + "Course Instructor", + { + "parent": course, + }, + pluck="instructor", + ) + if frappe.session.user == "Guest": return False - if has_course_instructor_role(member) or has_course_moderator_role(member): + if has_course_moderator_role(member): + return True + + if has_course_instructor_role(member) and member in instructors: return True portal_course_creation = frappe.db.get_single_value( "LMS Settings", "portal_course_creation" ) - return portal_course_creation == "Anyone" + if portal_course_creation == "Anyone" and member in instructors: + return True + + return False def has_course_moderator_role(member=None): @@ -727,7 +741,7 @@ def get_chart_data(chart_name, timespan, timegrain, from_date, to_date): } -@frappe.whitelist() +@frappe.whitelist(allow_guest=True) def get_course_completion_data(): all_membership = frappe.db.count("LMS Enrollment") completed = frappe.db.count("LMS Enrollment", {"progress": ["like", "%100%"]}) diff --git a/lms/www/assignments/assignment.py b/lms/www/assignments/assignment.py index 9b1c299d..ec8cc543 100644 --- a/lms/www/assignments/assignment.py +++ b/lms/www/assignments/assignment.py @@ -1,12 +1,12 @@ import frappe from frappe import _ -from lms.lms.utils import can_create_courses +from lms.lms.utils import has_course_moderator_role, has_course_instructor_role def get_context(context): context.no_cache = 1 - if not can_create_courses(): + if not has_course_moderator_role() or not has_course_instructor_role(): message = "You do not have permission to access this page." if frappe.session.user == "Guest": message = "Please login to access this page." diff --git a/lms/www/batch/quiz.py b/lms/www/batch/quiz.py index f88aaef5..52d0634f 100644 --- a/lms/www/batch/quiz.py +++ b/lms/www/batch/quiz.py @@ -1,13 +1,13 @@ import frappe from frappe.utils import cstr from frappe import _ -from lms.lms.utils import can_create_courses +from lms.lms.utils import has_course_instructor_role, has_course_moderator_role def get_context(context): context.no_cache = 1 - if not can_create_courses(): + if not has_course_moderator_role() or not has_course_instructor_role(): message = "You do not have permission to access this page." if frappe.session.user == "Guest": message = "Please login to access this page." diff --git a/lms/www/batch/quiz_list.py b/lms/www/batch/quiz_list.py index ae8df7fc..ee4321a7 100644 --- a/lms/www/batch/quiz_list.py +++ b/lms/www/batch/quiz_list.py @@ -1,12 +1,12 @@ import frappe -from lms.lms.utils import can_create_courses, has_course_moderator_role +from lms.lms.utils import has_course_instructor_role, has_course_moderator_role from frappe import _ def get_context(context): context.no_cache = 1 - if not can_create_courses(): + if not has_course_moderator_role() or not has_course_instructor_role(): message = "You do not have permission to access this page." if frappe.session.user == "Guest": message = "Please login to access this page." diff --git a/lms/www/batches/batch.html b/lms/www/batches/batch.html index 2f78e0c6..86cb1202 100644 --- a/lms/www/batches/batch.html +++ b/lms/www/batches/batch.html @@ -552,7 +552,7 @@ {% for legend in legends %}
-
{{ legend.title }}
+
{{ legend.label }}
{% endfor %} @@ -574,6 +574,8 @@ diff --git a/lms/www/batches/batch.js b/lms/www/batches/batch.js index e67e74c6..f4fbb885 100644 --- a/lms/www/batches/batch.js +++ b/lms/www/batches/batch.js @@ -698,8 +698,8 @@ const get_calendar_options = (element, calendar_id) => { const create_events = (calendar, events, calendar_id) => { let calendar_events = []; - events.forEach((event, idx) => { + let clr = get_background_color(event.reference_doctype); calendar_events.push({ id: `event${idx}`, calendarId: calendar_id, @@ -707,7 +707,7 @@ const create_events = (calendar, events, calendar_id) => { start: `${event.date}T${event.start_time}`, end: `${event.date}T${event.end_time}`, isAllday: event.start_time ? false : true, - borderColor: get_background_color(event.reference_doctype), + borderColor: clr, backgroundColor: "var(--fg-color)", customStyle: { borderRadius: "var(--border-radius-md)", @@ -724,10 +724,15 @@ const create_events = (calendar, events, calendar_id) => { calendar.createEvents(calendar_events); }; -const add_links_to_events = (calendar, events) => { +const add_links_to_events = (calendar) => { calendar.on("clickEvent", ({ event }) => { - const el = document.getElementById("clicked-event"); - window.open(event.raw.url, "_blank"); + let event_date = event.start.d.d; + event_date = moment(event_date).format("YYYY-MM-DD"); + + let current_date = moment().format("YYYY-MM-DD"); + if (allow_future || moment(event_date).isSameOrBefore(current_date)) { + window.open(event.raw.url, "_blank"); + } }); }; @@ -764,10 +769,10 @@ const set_calendar_range = (calendar, events) => { }; const get_background_color = (doctype) => { - if (doctype == "Course Lesson") return "var(--blue-400)"; - if (doctype == "LMS Quiz") return "var(--green-400)"; - if (doctype == "LMS Assignment") return "var(--orange-400)"; - if (doctype == "LMS Live Class") return "var(--purple-400)"; + const match = legends.filter((legend) => { + return legend.reference_doctype == doctype; + }); + if (match.length) return match[0].color; }; const email_to_students = () => { diff --git a/lms/www/batches/batch.py b/lms/www/batches/batch.py index 5e408c84..bb3ad307 100644 --- a/lms/www/batches/batch.py +++ b/lms/www/batches/batch.py @@ -42,6 +42,7 @@ def get_context(context): "currency", "batch_details", "published", + "allow_future", ], as_dict=True, ) @@ -96,7 +97,7 @@ def get_context(context): "parent": batch_name, }, ) - context.legends = get_legends() + context.legends = get_legends(batch_name) custom_tabs = frappe.get_hooks("lms_batch_tabs") @@ -261,22 +262,9 @@ def get_course_progress(batch_courses, student_details): student_details.courses[course.course] = 0 -def get_legends(): - return [ - { - "title": "Lesson", - "color": "var(--blue-400)", - }, - { - "title": "Quiz", - "color": "var(--green-400)", - }, - { - "title": "Assignment", - "color": "var(--orange-400)", - }, - { - "title": "Live Class", - "color": "var(--purple-400)", - }, - ] +def get_legends(batch): + return frappe.get_all( + "LMS Timetable Legend", + filters={"parenttype": "LMS Batch", "parent": batch}, + fields=["reference_doctype", "color", "label"], + ) diff --git a/lms/www/courses/course.py b/lms/www/courses/course.py index b4aa476c..b64395eb 100644 --- a/lms/www/courses/course.py +++ b/lms/www/courses/course.py @@ -23,7 +23,7 @@ def get_context(context): redirect_to_courses_list() if course_name == "new-course": - if not can_create_courses(): + if not can_create_courses(course_name): message = "You do not have permission to access this page." if frappe.session.user == "Guest": message = "Please login to access this page." diff --git a/lms/www/courses/create.py b/lms/www/courses/create.py index 901ce1f2..7b83f3f5 100644 --- a/lms/www/courses/create.py +++ b/lms/www/courses/create.py @@ -15,7 +15,7 @@ def get_context(context): except KeyError: redirect_to_courses_list() - if not can_create_courses(): + if not can_create_courses(course_name): message = "You do not have permission to access this page." if frappe.session.user == "Guest": message = "Please login to access this page." diff --git a/lms/www/courses/index.py b/lms/www/courses/index.py index d887e1ca..7df2a960 100644 --- a/lms/www/courses/index.py +++ b/lms/www/courses/index.py @@ -1,13 +1,13 @@ import frappe from frappe import _ from lms.lms.utils import ( - can_create_courses, check_profile_restriction, get_restriction_details, has_course_moderator_role, get_courses_under_review, get_average_rating, check_multicurrency, + has_course_instructor_role, ) from lms.overrides.user import get_enrolled_courses, get_authored_courses @@ -21,7 +21,17 @@ def get_context(context): context.created_courses = get_authored_courses(None, False) context.review_courses = get_courses_under_review() context.restriction = check_profile_restriction() - context.show_creators_section = can_create_courses() + + portal_course_creation = frappe.db.get_single_value( + "LMS Settings", "portal_course_creation" + ) + context.show_creators_section = ( + True + if portal_course_creation == "Anyone" + or has_course_moderator_role() + or has_course_instructor_role() + else False + ) context.show_review_section = ( has_course_moderator_role() and frappe.session.user != "Guest" ) diff --git a/lms/www/courses/outline.py b/lms/www/courses/outline.py index ad8f18d9..f01a71af 100644 --- a/lms/www/courses/outline.py +++ b/lms/www/courses/outline.py @@ -10,7 +10,7 @@ def get_context(context): if not frappe.db.exists("LMS Course", course_name): redirect_to_courses_list() - if not can_create_courses(): + if not can_create_courses(course_name): message = "You do not have permission to access this page." if frappe.session.user == "Guest": message = "Please login to access this page."