scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 1 | from __future__ import unicode_literals |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 2 | import erpnext.education.utils as utils |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 3 | import frappe |
scmmishra | 936f36c | 2019-04-22 18:55:19 +0530 | [diff] [blame] | 4 | from frappe import _ |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 5 | |
scmmishra | 1192529 | 2018-11-20 17:38:01 +0530 | [diff] [blame] | 6 | # LMS Utils to Update State for Vue Store |
| 7 | @frappe.whitelist() |
| 8 | def get_program_enrollments(): |
scmmishra | b3154ef | 2018-12-06 20:13:20 +0530 | [diff] [blame] | 9 | student = utils.get_current_student() |
| 10 | if student == None: |
| 11 | return None |
scmmishra | 327334a | 2019-04-22 12:03:17 +0530 | [diff] [blame] | 12 | return student.get_program_enrollments() |
scmmishra | 1192529 | 2018-11-20 17:38:01 +0530 | [diff] [blame] | 13 | |
| 14 | @frappe.whitelist() |
| 15 | def get_all_course_enrollments(): |
| 16 | student = utils.get_current_student() |
| 17 | if student == None: |
| 18 | return None |
scmmishra | 327334a | 2019-04-22 12:03:17 +0530 | [diff] [blame] | 19 | return student.get_all_course_enrollments() |
scmmishra | 1192529 | 2018-11-20 17:38:01 +0530 | [diff] [blame] | 20 | |
| 21 | # Vue Client Functions |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 22 | @frappe.whitelist(allow_guest=True) |
| 23 | def get_portal_details(): |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 24 | """ |
| 25 | Returns portal details from Education Settings Doctype. This contains the Title and Description for LMS amoung other things. |
| 26 | """ |
scmmishra | c22eef2 | 2019-03-18 15:36:49 +0530 | [diff] [blame] | 27 | from erpnext import get_default_company |
| 28 | |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 29 | settings = frappe.get_doc("Education Settings") |
scmmishra | c22eef2 | 2019-03-18 15:36:49 +0530 | [diff] [blame] | 30 | title = settings.portal_title or get_default_company() |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 31 | description = settings.description |
| 32 | return dict(title=title, description=description) |
| 33 | |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 34 | @frappe.whitelist(allow_guest=True) |
| 35 | def get_featured_programs(): |
| 36 | featured_program_names = frappe.get_all("Program", filters={"is_published": True, "is_featured": True}) |
| 37 | if featured_program_names: |
scmmishra | da2c90c | 2019-04-22 12:19:39 +0530 | [diff] [blame] | 38 | featured_list = [utils.get_program_and_enrollment_status(program['name']) for program in featured_program_names] |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 39 | return featured_list |
| 40 | else: |
scmmishra | 621cad8 | 2019-03-18 15:40:47 +0530 | [diff] [blame] | 41 | return get_all_programs()[:2] |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 42 | |
scmmishra | 85c2fee | 2018-11-14 14:23:06 +0530 | [diff] [blame] | 43 | @frappe.whitelist(allow_guest=True) |
| 44 | def get_all_programs(): |
| 45 | program_names = frappe.get_all("Program", filters={"is_published": True}) |
| 46 | if program_names: |
scmmishra | da2c90c | 2019-04-22 12:19:39 +0530 | [diff] [blame] | 47 | program_list = [utils.get_program_and_enrollment_status(program['name']) for program in program_names] |
scmmishra | 621cad8 | 2019-03-18 15:40:47 +0530 | [diff] [blame] | 48 | return program_list |
scmmishra | 85c2fee | 2018-11-14 14:23:06 +0530 | [diff] [blame] | 49 | |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 50 | @frappe.whitelist(allow_guest=True) |
scmmishra | da2c90c | 2019-04-22 12:19:39 +0530 | [diff] [blame] | 51 | def get_program(program_name): |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 52 | try: |
scmmishra | da2c90c | 2019-04-22 12:19:39 +0530 | [diff] [blame] | 53 | return frappe.get_doc('Program', program_name) |
| 54 | except frappe.DoesNotExistError: |
| 55 | frappe.throw(_("Program {0} does not exist.".format(program_name))) |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 56 | |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 57 | # Functions to get program & course details |
| 58 | @frappe.whitelist(allow_guest=True) |
| 59 | def get_courses(program_name): |
| 60 | program = frappe.get_doc('Program', program_name) |
| 61 | courses = program.get_course_list() |
scmmishra | fdbabde | 2018-11-22 15:33:30 +0530 | [diff] [blame] | 62 | return courses |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 63 | |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 64 | @frappe.whitelist() |
scmmishra | da39da6 | 2018-12-13 11:51:31 +0530 | [diff] [blame] | 65 | def get_next_content(current_content, current_content_type, topic): |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 66 | if frappe.session.user == "Guest": |
| 67 | return None |
scmmishra | da39da6 | 2018-12-13 11:51:31 +0530 | [diff] [blame] | 68 | topic = frappe.get_doc("Topic", topic) |
| 69 | content_list = [{'content_type':item.doctype, 'content':item.name} for item in topic.get_contents()] |
| 70 | current_index = content_list.index({'content': current_content, 'content_type': current_content_type}) |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 71 | try: |
| 72 | return content_list[current_index + 1] |
| 73 | except IndexError: |
| 74 | return None |
| 75 | |
| 76 | def get_quiz_with_answers(quiz_name): |
| 77 | try: |
| 78 | quiz = frappe.get_doc("Quiz", quiz_name).get_questions() |
| 79 | 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] |
| 80 | return quiz_output |
| 81 | except: |
| 82 | frappe.throw("Quiz {0} does not exist".format(quiz_name)) |
| 83 | return None |
| 84 | |
| 85 | @frappe.whitelist() |
scmmishra | 2b7e158 | 2019-03-29 12:45:08 +0530 | [diff] [blame] | 86 | def get_quiz_without_answers(quiz_name, course_name): |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 87 | try: |
scmmishra | 2b7e158 | 2019-03-29 12:45:08 +0530 | [diff] [blame] | 88 | quiz = frappe.get_doc("Quiz", quiz_name) |
| 89 | questions = quiz.get_questions() |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 90 | except: |
| 91 | frappe.throw("Quiz {0} does not exist".format(quiz_name)) |
| 92 | return None |
| 93 | |
scmmishra | 3673b25 | 2019-04-22 18:10:19 +0530 | [diff] [blame] | 94 | if utils.check_super_access(): |
| 95 | quiz_output = [{'name':question.name, 'question':question.question, 'type': question.type, 'options':[{'name': option.name, 'option':option.option} for option in question.options]} for question in questions] |
| 96 | return { 'quizData': quiz_output, 'status': None} |
| 97 | |
scmmishra | 2b7e158 | 2019-03-29 12:45:08 +0530 | [diff] [blame] | 98 | enrollment = utils.get_course_enrollment(course_name).name |
scmmishra | 66d2392 | 2019-04-22 12:54:43 +0530 | [diff] [blame] | 99 | quiz_progress = {} |
| 100 | quiz_progress['is_complete'], quiz_progress['score'], quiz_progress['result'] = utils.check_quiz_completion(quiz, enrollment) |
scmmishra | 2b7e158 | 2019-03-29 12:45:08 +0530 | [diff] [blame] | 101 | quiz_output = [{'name':question.name, 'question':question.question, 'type': question.type, 'options':[{'name': option.name, 'option':option.option} for option in question.options]} for question in questions] |
scmmishra | 66d2392 | 2019-04-22 12:54:43 +0530 | [diff] [blame] | 102 | return { 'quizData': quiz_output, 'status': quiz_progress} |
scmmishra | 2b7e158 | 2019-03-29 12:45:08 +0530 | [diff] [blame] | 103 | |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 104 | @frappe.whitelist() |
scmmishra | ab8fc8c | 2019-02-28 16:42:25 +0530 | [diff] [blame] | 105 | def evaluate_quiz(course, quiz_response, quiz_name): |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 106 | """LMS Function: Evaluates a simple multiple choice quiz. |
scmmishra | 66d2392 | 2019-04-22 12:54:43 +0530 | [diff] [blame] | 107 | :param course: name of the course |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 108 | :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. |
scmmishra | 66d2392 | 2019-04-22 12:54:43 +0530 | [diff] [blame] | 109 | :param quiz_name: Name of the quiz attempted |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 110 | """ |
| 111 | import json |
| 112 | quiz_response = json.loads(quiz_response) |
| 113 | quiz = frappe.get_doc("Quiz", quiz_name) |
| 114 | answers, score, status = quiz.evaluate(quiz_response, quiz_name) |
scmmishra | ac8d347 | 2019-04-22 14:30:55 +0530 | [diff] [blame] | 115 | print(answers) |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 116 | |
scmmishra | ac8d347 | 2019-04-22 14:30:55 +0530 | [diff] [blame] | 117 | course_enrollment = utils.get_course_enrollment(course) |
| 118 | if course_enrollment: |
| 119 | course_enrollment.add_quiz_activity(quiz_name, quiz_response, answers, score, status) |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 120 | |
scmmishra | ac8d347 | 2019-04-22 14:30:55 +0530 | [diff] [blame] | 121 | return score |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 122 | |
| 123 | @frappe.whitelist() |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 124 | def enroll_in_program(program_name): |
scmmishra | 327334a | 2019-04-22 12:03:17 +0530 | [diff] [blame] | 125 | student = utils.get_current_student() |
| 126 | if not student: |
scmmishra | 5079e6c | 2019-04-22 14:46:26 +0530 | [diff] [blame] | 127 | student = utils.create_student_from_current_user() |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 128 | program_enrollment = student.enroll_in_program(program_name) |
scmmishra | 0a4902f | 2018-11-15 11:16:53 +0530 | [diff] [blame] | 129 | return program_name |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 130 | |
scmmishra | ac8d347 | 2019-04-22 14:30:55 +0530 | [diff] [blame] | 131 | # Academdy Activity |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 132 | @frappe.whitelist() |
scmmishra | a592f70 | 2018-11-20 18:37:01 +0530 | [diff] [blame] | 133 | def add_activity(course, content_type, content): |
scmmishra | 000e706 | 2019-03-19 12:30:43 +0530 | [diff] [blame] | 134 | if not utils.get_current_student(): |
| 135 | return |
scmmishra | a592f70 | 2018-11-20 18:37:01 +0530 | [diff] [blame] | 136 | enrollment = utils.get_course_enrollment(course) |
scmmishra | ac8d347 | 2019-04-22 14:30:55 +0530 | [diff] [blame] | 137 | enrollment.add_activity(content_type, content) |
scmmishra | 35bf561 | 2018-11-13 16:36:22 +0530 | [diff] [blame] | 138 | |
scmmishra | fdbabde | 2018-11-22 15:33:30 +0530 | [diff] [blame] | 139 | @frappe.whitelist() |
scmmishra | d5973fe | 2019-04-22 12:05:22 +0530 | [diff] [blame] | 140 | def get_student_course_details(course_name, program_name): |
scmmishra | da39da6 | 2018-12-13 11:51:31 +0530 | [diff] [blame] | 141 | """ |
| 142 | Return the porgress of a course in a program as well as the content to continue from. |
scmmishra | 95bb3c0 | 2019-02-26 16:49:58 +0530 | [diff] [blame] | 143 | :param course_name: |
| 144 | :param program_name: |
scmmishra | da39da6 | 2018-12-13 11:51:31 +0530 | [diff] [blame] | 145 | """ |
scmmishra | 327334a | 2019-04-22 12:03:17 +0530 | [diff] [blame] | 146 | student = utils.get_current_student() |
| 147 | if not student: |
scmmishra | 000e706 | 2019-03-19 12:30:43 +0530 | [diff] [blame] | 148 | return {'flag':'Start Course' } |
scmmishra | 3673b25 | 2019-04-22 18:10:19 +0530 | [diff] [blame] | 149 | |
scmmishra | fdbabde | 2018-11-22 15:33:30 +0530 | [diff] [blame] | 150 | course_enrollment = utils.get_course_enrollment(course_name) |
scmmishra | b3154ef | 2018-12-06 20:13:20 +0530 | [diff] [blame] | 151 | program_enrollment = utils.get_program_enrollment(program_name) |
scmmishra | 3673b25 | 2019-04-22 18:10:19 +0530 | [diff] [blame] | 152 | |
scmmishra | b3154ef | 2018-12-06 20:13:20 +0530 | [diff] [blame] | 153 | if not program_enrollment: |
| 154 | return None |
scmmishra | 3673b25 | 2019-04-22 18:10:19 +0530 | [diff] [blame] | 155 | |
scmmishra | 97c994f | 2018-11-26 14:41:15 +0530 | [diff] [blame] | 156 | if not course_enrollment: |
scmmishra | 5c646e6 | 2019-03-18 18:37:26 +0530 | [diff] [blame] | 157 | course_enrollment = utils.enroll_in_course(course_name, program_name) |
scmmishra | 3673b25 | 2019-04-22 18:10:19 +0530 | [diff] [blame] | 158 | |
scmmishra | da39da6 | 2018-12-13 11:51:31 +0530 | [diff] [blame] | 159 | progress = course_enrollment.get_progress(student) |
| 160 | count = sum([activity['is_complete'] for activity in progress]) |
scmmishra | a592f70 | 2018-11-20 18:37:01 +0530 | [diff] [blame] | 161 | if count == 0: |
scmmishra | 000e706 | 2019-03-19 12:30:43 +0530 | [diff] [blame] | 162 | return {'flag':'Start Course'} |
scmmishra | a592f70 | 2018-11-20 18:37:01 +0530 | [diff] [blame] | 163 | elif count == len(progress): |
scmmishra | 000e706 | 2019-03-19 12:30:43 +0530 | [diff] [blame] | 164 | return {'flag':'Completed'} |
scmmishra | a592f70 | 2018-11-20 18:37:01 +0530 | [diff] [blame] | 165 | elif count < len(progress): |
| 166 | next_item = next(item for item in progress if item['is_complete']==False) |
scmmishra | 000e706 | 2019-03-19 12:30:43 +0530 | [diff] [blame] | 167 | return {'flag':'Continue'} |
scmmishra | 95bb3c0 | 2019-02-26 16:49:58 +0530 | [diff] [blame] | 168 | |
scmmishra | affbfe7 | 2018-11-26 11:59:25 +0530 | [diff] [blame] | 169 | @frappe.whitelist() |
scmmishra | d78c326 | 2019-04-22 12:06:28 +0530 | [diff] [blame] | 170 | def get_student_topic_details(topic_name, course_name): |
scmmishra | 7e1678e | 2019-02-26 17:11:01 +0530 | [diff] [blame] | 171 | """ |
| 172 | Return the porgress of a course in a program as well as the content to continue from. |
| 173 | :param topic_name: |
| 174 | :param course_name: |
scmmishra | 7e1678e | 2019-02-26 17:11:01 +0530 | [diff] [blame] | 175 | """ |
scmmishra | 000e706 | 2019-03-19 12:30:43 +0530 | [diff] [blame] | 176 | topic = frappe.get_doc("Topic", topic_name) |
scmmishra | 327334a | 2019-04-22 12:03:17 +0530 | [diff] [blame] | 177 | student = utils.get_current_student() |
| 178 | if not student: |
scmmishra | 000e706 | 2019-03-19 12:30:43 +0530 | [diff] [blame] | 179 | topic_content = topic.get_all_children() |
| 180 | if topic_content: |
| 181 | return {'flag':'Start Course', 'content_type': topic_content[0].content_type, 'content': topic_content[0].content} |
| 182 | else: |
| 183 | return None |
scmmishra | 7e1678e | 2019-02-26 17:11:01 +0530 | [diff] [blame] | 184 | course_enrollment = utils.get_course_enrollment(course_name) |
scmmishra | de5f71a | 2019-02-28 15:40:49 +0530 | [diff] [blame] | 185 | progress = student.get_topic_progress(course_enrollment.name, topic) |
scmmishra | 3726f8a | 2019-02-28 16:33:53 +0530 | [diff] [blame] | 186 | if not progress: |
| 187 | return { 'flag':'Start Topic', 'content_type': None, 'content': None } |
scmmishra | 7e1678e | 2019-02-26 17:11:01 +0530 | [diff] [blame] | 188 | count = sum([activity['is_complete'] for activity in progress]) |
| 189 | if count == 0: |
scmmishra | de5f71a | 2019-02-28 15:40:49 +0530 | [diff] [blame] | 190 | return {'flag':'Start Topic', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']} |
scmmishra | 7e1678e | 2019-02-26 17:11:01 +0530 | [diff] [blame] | 191 | elif count == len(progress): |
| 192 | return {'flag':'Completed', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']} |
| 193 | elif count < len(progress): |
| 194 | next_item = next(item for item in progress if item['is_complete']==False) |
| 195 | return {'flag':'Continue', 'content_type': next_item['content_type'], 'content': next_item['content']} |
| 196 | |
| 197 | @frappe.whitelist() |
scmmishra | 201fec3 | 2018-11-26 16:52:45 +0530 | [diff] [blame] | 198 | def get_program_progress(program_name): |
scmmishra | 68b3203 | 2019-04-22 18:40:46 +0530 | [diff] [blame] | 199 | program_enrollment = frappe.get_doc("Program Enrollment", utils.get_program_enrollment(program_name)) |
scmmishra | 97c994f | 2018-11-26 14:41:15 +0530 | [diff] [blame] | 200 | if not program_enrollment: |
| 201 | return None |
| 202 | else: |
scmmishra | 68b3203 | 2019-04-22 18:40:46 +0530 | [diff] [blame] | 203 | return program_enrollment.get_program_progress() |
scmmishra | 2955851 | 2018-11-26 19:16:54 +0530 | [diff] [blame] | 204 | |
| 205 | @frappe.whitelist() |
| 206 | def get_joining_date(): |
scmmishra | 5079e6c | 2019-04-22 14:46:26 +0530 | [diff] [blame] | 207 | student = utils.get_current_student() |
| 208 | if student: |
scmmishra | 9b7ac3e | 2019-03-28 14:47:22 +0530 | [diff] [blame] | 209 | return student.joining_date |
scmmishra | 4d10229 | 2018-12-07 17:41:40 +0530 | [diff] [blame] | 210 | |
| 211 | @frappe.whitelist() |
scmmishra | 68b3203 | 2019-04-22 18:40:46 +0530 | [diff] [blame] | 212 | def get_quiz_progress_of_program(program_name): |
| 213 | program_enrollment = frappe.get_doc("Program Enrollment", utils.get_program_enrollment(program_name)) |
scmmishra | 4d10229 | 2018-12-07 17:41:40 +0530 | [diff] [blame] | 214 | if not program_enrollment: |
| 215 | return None |
| 216 | else: |
scmmishra | 68b3203 | 2019-04-22 18:40:46 +0530 | [diff] [blame] | 217 | return program_enrollment.get_quiz_progress() |
scmmishra | 95bb3c0 | 2019-02-26 16:49:58 +0530 | [diff] [blame] | 218 | |
| 219 | |
| 220 | @frappe.whitelist(allow_guest=True) |
| 221 | def get_course_details(course_name): |
| 222 | try: |
scmmishra | 64d2fe0 | 2019-04-22 12:30:08 +0530 | [diff] [blame] | 223 | course = frappe.get_doc('Course', course_name) |
scmmishra | 95bb3c0 | 2019-02-26 16:49:58 +0530 | [diff] [blame] | 224 | return course |
| 225 | except: |
| 226 | return None |
| 227 | |
| 228 | # Functions to get program & course details |
| 229 | @frappe.whitelist(allow_guest=True) |
| 230 | def get_topics(course_name): |
scmmishra | 4add826 | 2019-04-22 12:28:13 +0530 | [diff] [blame] | 231 | try: |
| 232 | course = frappe.get_doc('Course', course_name) |
| 233 | return course.get_topics() |
| 234 | except frappe.DoesNotExistError: |
| 235 | frappe.throw(_("Course {0} does not exist.".format(course_name))) |
scmmishra | 23880ae | 2019-02-27 12:09:57 +0530 | [diff] [blame] | 236 | |
| 237 | @frappe.whitelist() |
scmmishra | 4add826 | 2019-04-22 12:28:13 +0530 | [diff] [blame] | 238 | def get_content(content_type, content): |
scmmishra | 23880ae | 2019-02-27 12:09:57 +0530 | [diff] [blame] | 239 | try: |
scmmishra | 4add826 | 2019-04-22 12:28:13 +0530 | [diff] [blame] | 240 | return frappe.get_doc(content_type, content) |
| 241 | except frappe.DoesNotExistError: |
| 242 | frappe.throw(_("{0} {1} does not exist.".format(content_type, content))) |