scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
| 2 | import frappe |
| 3 | import erpnext.education.utils as utils |
| 4 | |
scmmishra | 9c642ac | 2018-11-01 17:17:49 +0530 | [diff] [blame] | 5 | # Functions to get homepage details |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 6 | @frappe.whitelist() |
| 7 | def get_portal_details(): |
| 8 | settings = frappe.get_doc("Education Settings") |
| 9 | title = settings.portal_title |
| 10 | description = settings.description |
| 11 | return dict(title=title, description=description) |
| 12 | |
| 13 | @frappe.whitelist() |
| 14 | def get_featured_programs(): |
| 15 | featured_program_names = frappe.get_list("Program", filters={"is_published": True, "is_featured": True}) |
| 16 | featured_list = [program["name"] for program in featured_program_names] |
| 17 | if featured_list: |
| 18 | return featured_list |
| 19 | else: |
| 20 | return None |
| 21 | |
scmmishra | 9c642ac | 2018-11-01 17:17:49 +0530 | [diff] [blame] | 22 | # Functions to get program & course details |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 23 | @frappe.whitelist() |
| 24 | def get_program_details(program_name): |
| 25 | try: |
| 26 | program = frappe.get_doc('Program', program_name) |
| 27 | return program |
| 28 | except: |
| 29 | return None |
scmmishra | 9c642ac | 2018-11-01 17:17:49 +0530 | [diff] [blame] | 30 | |
| 31 | @frappe.whitelist() |
| 32 | def get_courses(program_name): |
| 33 | program = frappe.get_doc('Program', program_name) |
| 34 | courses = program.get_course_list() |
| 35 | return courses |
| 36 | |
| 37 | @frappe.whitelist() |
| 38 | def get_starting_content(course_name): |
| 39 | course = frappe.get_doc('Course', course_name) |
| 40 | content = course.course_content[0].content |
| 41 | content_type = course.course_content[0].content_type |
| 42 | return dict(content=content, content_type=content_type) |
| 43 | |
| 44 | |
| 45 | # Functions to get content details |
| 46 | @frappe.whitelist() |
| 47 | def get_content(content_name, content_type): |
| 48 | try: |
| 49 | content = frappe.get_doc(content_type, content_name) |
| 50 | return content |
| 51 | except: |
| 52 | frappe.throw("{0} with name {1} does not exist".format(content_type, content_name)) |
| 53 | return None |
| 54 | |
| 55 | @frappe.whitelist() |
| 56 | def get_next_content(content, content_type, course): |
| 57 | course_doc = frappe.get_doc("Course", course) |
| 58 | content_list = [{'content_type':item.content_type, 'content':item.content} for item in course_doc.get_all_children()] |
| 59 | current_index = content_list.index({'content': content, 'content_type': content_type}) |
| 60 | try: |
| 61 | return content_list[current_index + 1] |
| 62 | except IndexError: |
scmmishra | 3741844 | 2018-11-01 19:50:47 +0530 | [diff] [blame] | 63 | return None |
| 64 | |
| 65 | def get_quiz_with_answers(quiz_name): |
| 66 | try: |
| 67 | quiz = frappe.get_doc("Quiz", quiz_name).get_questions() |
| 68 | quiz_output = [{'name':question.name, 'question':question.question, 'options':[{'name': option.name, 'option':option.option, 'is_correct':option.is_correct} for option in question.options]} for question in quiz] |
| 69 | return quiz_output |
| 70 | except: |
| 71 | frappe.throw("Quiz {0} does not exist".format(quiz_name)) |
| 72 | return None |
| 73 | |
| 74 | @frappe.whitelist() |
| 75 | def get_quiz_without_answers(quiz_name): |
| 76 | try: |
| 77 | quiz = frappe.get_doc("Quiz", quiz_name).get_questions() |
| 78 | quiz_output = [{'name':question.name, 'question':question.question, 'options':[{'name': option.name, 'option':option.option} for option in question.options]} for question in quiz] |
| 79 | return quiz_output |
| 80 | except: |
| 81 | frappe.throw("Quiz {0} does not exist".format(quiz_name)) |
scmmishra | f7029f0 | 2018-11-02 13:15:49 +0530 | [diff] [blame] | 82 | return None |
| 83 | |
| 84 | @frappe.whitelist() |
| 85 | def evaluate_quiz(quiz_response): |
| 86 | """LMS Function: Evaluates a simple multiple choice quiz. It recieves arguments from `www/lms/course.js` as dictionary using FormData[1]. |
| 87 | |
| 88 | |
| 89 | :param quiz_response: contains user selected choices for a quiz in the form of a string formatted as a dictionary. The function uses `json.loads()` to convert it to a python dictionary. |
| 90 | [1]: https://developer.mozilla.org/en-US/docs/Web/API/FormData |
| 91 | """ |
| 92 | import json |
| 93 | print("---------------------------") |
| 94 | print(quiz_response) |
| 95 | quiz_response = json.loads(quiz_response) |
| 96 | print(type(quiz_response)) |
| 97 | # quiz_name = kwargs.get('quiz') |
| 98 | # course_name = kwargs.get('course') |
| 99 | # enrollment = get_course_enrollment(course_name, frappe.session.user) |
| 100 | # try: |
| 101 | # quiz = frappe.get_doc("Quiz", quiz_name) |
| 102 | # answers, score = quiz.evaluate(quiz_response, enrollment, quiz_name) |
| 103 | # add_quiz_activity(enrollment, quiz_name, score, answers, quiz_response) |
| 104 | # return score |
| 105 | # except frappe.DoesNotExistError: |
| 106 | # frappe.throw("Quiz {0} does not exist".format(quiz_name)) |
| 107 | # return None |