diff --git a/community/lms/api.py b/community/lms/api.py index ab69ede4..879c2a05 100644 --- a/community/lms/api.py +++ b/community/lms/api.py @@ -21,3 +21,13 @@ def get_section(name): """ doc = frappe.get_doc("LMS Section", name) return doc and doc.as_dict() + +@frappe.whitelist() +def submit_solution(exercise, code): + """Submits a solution. + """ + ex = frappe.get_doc("Exercise", exercise) + if not ex: + return + doc = ex.submit(code) + return {"name": doc.name, "creation": doc.creation} diff --git a/community/lms/doctype/lesson/lesson.py b/community/lms/doctype/lesson/lesson.py index b1105240..87960951 100644 --- a/community/lms/doctype/lesson/lesson.py +++ b/community/lms/doctype/lesson/lesson.py @@ -18,6 +18,7 @@ class Lesson(Document): def make_lms_section(self, index, section): s = frappe.new_doc('LMS Section', parent_doc=self, parentfield='sections') s.type = section.type + s.id = section.id s.label = section.label s.contents = section.contents s.index = index diff --git a/community/lms/doctype/lms_section/lms_section.json b/community/lms/doctype/lms_section/lms_section.json index 4a275d32..3b056485 100644 --- a/community/lms/doctype/lms_section/lms_section.json +++ b/community/lms/doctype/lms_section/lms_section.json @@ -9,7 +9,8 @@ "contents", "code", "attrs", - "index" + "index", + "id" ], "fields": [ { @@ -43,12 +44,17 @@ "fieldname": "index", "fieldtype": "Int", "label": "Index" + }, + { + "fieldname": "id", + "fieldtype": "Data", + "label": "id" } ], "index_web_pages_for_search": 1, "istable": 1, "links": [], - "modified": "2021-03-12 17:56:23.118854", + "modified": "2021-05-19 18:55:26.019625", "modified_by": "Administrator", "module": "LMS", "name": "LMS Section", diff --git a/community/lms/doctype/lms_section/lms_section.py b/community/lms/doctype/lms_section/lms_section.py index f51fec69..65c33f46 100644 --- a/community/lms/doctype/lms_section/lms_section.py +++ b/community/lms/doctype/lms_section/lms_section.py @@ -10,6 +10,10 @@ class LMSSection(Document): def __repr__(self): return f"" + def get_exercise(self): + if self.type == "exercise": + return frappe.get_doc("Exercise", self.id) + def get_latest_code_for_user(self): """Returns the latest code for the logged in user. """ diff --git a/community/lms/widgets/Exercise.html b/community/lms/widgets/Exercise.html new file mode 100644 index 00000000..af3ba383 --- /dev/null +++ b/community/lms/widgets/Exercise.html @@ -0,0 +1,14 @@ +{% from "www/macros/livecode.html" import LiveCodeEditorJS, LiveCodeEditor with context %} + +
+

{{ exercise.title }}

+
{{frappe.utils.md_to_html(exercise.description)}}
+ + {% set submission = exercise.get_user_submission() %} + + {{ LiveCodeEditor(exercise.name, + code=exercise.code, + reset_code=exercise.code, + is_exercise=True, + last_submitted=submission and submission.creation) }} +
diff --git a/community/www/courses/learn/index.html b/community/www/courses/learn/index.html index 4804f508..3dbd5adf 100644 --- a/community/www/courses/learn/index.html +++ b/community/www/courses/learn/index.html @@ -55,8 +55,14 @@ {% macro render_section(s) %} {% if s.type == "text" %} {{ render_section_text(s) }} - {% elif s.type == "example" or s.type == "code" or s.type == "exercise" %} - {{ LiveCodeEditor(s.name, s.get_latest_code_for_user(), s.type=="exercise", "2 hours ago") }} + {% elif s.type == "example" or s.type == "code" %} + {{ LiveCodeEditor(s.name, + code=s.get_latest_code_for_user(), + reset_code=s.contents, + is_exercise=False) + }} + {% elif s.type == "exercise" %} + {{ widgets.Exercise(exercise=s.get_exercise())}} {% else %}
Unknown section type: {{s.type}}
{% endif %} diff --git a/community/www/macros/livecode.html b/community/www/macros/livecode.html index 94502e53..a4ec142f 100644 --- a/community/www/macros/livecode.html +++ b/community/www/macros/livecode.html @@ -24,7 +24,7 @@ {% endmacro %} -{% macro LiveCodeEditor(name, code, is_exercise, last_submitted) %} +{% macro LiveCodeEditor(name, code, reset_code, is_exercise=False, last_submitted=None) %}
@@ -34,10 +34,13 @@ {% if is_exercise %} {% if last_submitted %} - Submitted on {{last_submitted}} + {% endif %} {% endif %}
+
+
{{reset_code}}
+
@@ -59,20 +62,67 @@ {% macro LiveCodeEditorJS(name, code) %} + + + + + + + + {% endmacro %}