blob: 8feb0c027952b16c926052d5030622be413467d4 [file] [log] [blame]
scmmishra35bf5612018-11-13 16:36:22 +05301from __future__ import unicode_literals
scmmishrababb68d2018-11-19 16:13:21 +05302import erpnext.education.utils as utils
scmmishra35bf5612018-11-13 16:36:22 +05303import frappe
4
scmmishra11925292018-11-20 17:38:01 +05305# LMS Utils to Update State for Vue Store
6@frappe.whitelist()
7def get_program_enrollments():
scmmishrab3154ef2018-12-06 20:13:20 +05308 student = utils.get_current_student()
9 if student == None:
10 return None
scmmishra327334a2019-04-22 12:03:17 +053011 return student.get_program_enrollments()
scmmishra11925292018-11-20 17:38:01 +053012
13@frappe.whitelist()
14def get_all_course_enrollments():
15 student = utils.get_current_student()
16 if student == None:
17 return None
scmmishra327334a2019-04-22 12:03:17 +053018 return student.get_all_course_enrollments()
scmmishra11925292018-11-20 17:38:01 +053019
20# Vue Client Functions
scmmishra35bf5612018-11-13 16:36:22 +053021@frappe.whitelist(allow_guest=True)
22def get_portal_details():
scmmishrababb68d2018-11-19 16:13:21 +053023 """
24 Returns portal details from Education Settings Doctype. This contains the Title and Description for LMS amoung other things.
25 """
scmmishrac22eef22019-03-18 15:36:49 +053026 from erpnext import get_default_company
27
scmmishra35bf5612018-11-13 16:36:22 +053028 settings = frappe.get_doc("Education Settings")
scmmishrac22eef22019-03-18 15:36:49 +053029 title = settings.portal_title or get_default_company()
scmmishra35bf5612018-11-13 16:36:22 +053030 description = settings.description
31 return dict(title=title, description=description)
32
scmmishra35bf5612018-11-13 16:36:22 +053033@frappe.whitelist(allow_guest=True)
34def get_featured_programs():
35 featured_program_names = frappe.get_all("Program", filters={"is_published": True, "is_featured": True})
36 if featured_program_names:
scmmishrada2c90c2019-04-22 12:19:39 +053037 featured_list = [utils.get_program_and_enrollment_status(program['name']) for program in featured_program_names]
scmmishra35bf5612018-11-13 16:36:22 +053038 return featured_list
39 else:
scmmishra621cad82019-03-18 15:40:47 +053040 return get_all_programs()[:2]
scmmishra35bf5612018-11-13 16:36:22 +053041
scmmishra85c2fee2018-11-14 14:23:06 +053042@frappe.whitelist(allow_guest=True)
43def get_all_programs():
44 program_names = frappe.get_all("Program", filters={"is_published": True})
45 if program_names:
scmmishrada2c90c2019-04-22 12:19:39 +053046 program_list = [utils.get_program_and_enrollment_status(program['name']) for program in program_names]
scmmishra621cad82019-03-18 15:40:47 +053047 return program_list
scmmishra85c2fee2018-11-14 14:23:06 +053048
scmmishra35bf5612018-11-13 16:36:22 +053049@frappe.whitelist(allow_guest=True)
scmmishrada2c90c2019-04-22 12:19:39 +053050def get_program(program_name):
scmmishra35bf5612018-11-13 16:36:22 +053051 try:
scmmishrada2c90c2019-04-22 12:19:39 +053052 return frappe.get_doc('Program', program_name)
53 except frappe.DoesNotExistError:
54 frappe.throw(_("Program {0} does not exist.".format(program_name)))
scmmishra35bf5612018-11-13 16:36:22 +053055
scmmishra35bf5612018-11-13 16:36:22 +053056# Functions to get program & course details
57@frappe.whitelist(allow_guest=True)
58def get_courses(program_name):
59 program = frappe.get_doc('Program', program_name)
60 courses = program.get_course_list()
scmmishrafdbabde2018-11-22 15:33:30 +053061 return courses
scmmishra35bf5612018-11-13 16:36:22 +053062
scmmishra35bf5612018-11-13 16:36:22 +053063@frappe.whitelist()
scmmishrada39da62018-12-13 11:51:31 +053064def get_next_content(current_content, current_content_type, topic):
scmmishra35bf5612018-11-13 16:36:22 +053065 if frappe.session.user == "Guest":
66 return None
scmmishrada39da62018-12-13 11:51:31 +053067 topic = frappe.get_doc("Topic", topic)
68 content_list = [{'content_type':item.doctype, 'content':item.name} for item in topic.get_contents()]
69 current_index = content_list.index({'content': current_content, 'content_type': current_content_type})
scmmishra35bf5612018-11-13 16:36:22 +053070 try:
71 return content_list[current_index + 1]
72 except IndexError:
73 return None
74
75def get_quiz_with_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, 'is_correct':option.is_correct} 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))
82 return None
83
84@frappe.whitelist()
scmmishra2b7e1582019-03-29 12:45:08 +053085def get_quiz_without_answers(quiz_name, course_name):
scmmishra35bf5612018-11-13 16:36:22 +053086 try:
scmmishra2b7e1582019-03-29 12:45:08 +053087 quiz = frappe.get_doc("Quiz", quiz_name)
88 questions = quiz.get_questions()
scmmishra35bf5612018-11-13 16:36:22 +053089 except:
90 frappe.throw("Quiz {0} does not exist".format(quiz_name))
91 return None
92
scmmishra3673b252019-04-22 18:10:19 +053093 if utils.check_super_access():
94 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]
95 return { 'quizData': quiz_output, 'status': None}
96
scmmishra2b7e1582019-03-29 12:45:08 +053097 enrollment = utils.get_course_enrollment(course_name).name
scmmishra66d23922019-04-22 12:54:43 +053098 quiz_progress = {}
99 quiz_progress['is_complete'], quiz_progress['score'], quiz_progress['result'] = utils.check_quiz_completion(quiz, enrollment)
scmmishra2b7e1582019-03-29 12:45:08 +0530100 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]
scmmishra66d23922019-04-22 12:54:43 +0530101 return { 'quizData': quiz_output, 'status': quiz_progress}
scmmishra2b7e1582019-03-29 12:45:08 +0530102
scmmishra35bf5612018-11-13 16:36:22 +0530103@frappe.whitelist()
scmmishraab8fc8c2019-02-28 16:42:25 +0530104def evaluate_quiz(course, quiz_response, quiz_name):
scmmishra35bf5612018-11-13 16:36:22 +0530105 """LMS Function: Evaluates a simple multiple choice quiz.
scmmishra66d23922019-04-22 12:54:43 +0530106 :param course: name of the course
scmmishra35bf5612018-11-13 16:36:22 +0530107 :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.
scmmishra66d23922019-04-22 12:54:43 +0530108 :param quiz_name: Name of the quiz attempted
scmmishra35bf5612018-11-13 16:36:22 +0530109 """
110 import json
111 quiz_response = json.loads(quiz_response)
112 quiz = frappe.get_doc("Quiz", quiz_name)
113 answers, score, status = quiz.evaluate(quiz_response, quiz_name)
scmmishraac8d3472019-04-22 14:30:55 +0530114 print(answers)
scmmishra35bf5612018-11-13 16:36:22 +0530115
scmmishraac8d3472019-04-22 14:30:55 +0530116 course_enrollment = utils.get_course_enrollment(course)
117 if course_enrollment:
118 course_enrollment.add_quiz_activity(quiz_name, quiz_response, answers, score, status)
scmmishra35bf5612018-11-13 16:36:22 +0530119
scmmishraac8d3472019-04-22 14:30:55 +0530120 return score
scmmishra35bf5612018-11-13 16:36:22 +0530121
122@frappe.whitelist()
scmmishra35bf5612018-11-13 16:36:22 +0530123def enroll_in_program(program_name):
scmmishra327334a2019-04-22 12:03:17 +0530124 student = utils.get_current_student()
125 if not student:
scmmishra5079e6c2019-04-22 14:46:26 +0530126 student = utils.create_student_from_current_user()
scmmishra35bf5612018-11-13 16:36:22 +0530127 program_enrollment = student.enroll_in_program(program_name)
scmmishra0a4902f2018-11-15 11:16:53 +0530128 return program_name
scmmishra35bf5612018-11-13 16:36:22 +0530129
scmmishraac8d3472019-04-22 14:30:55 +0530130# Academdy Activity
scmmishra35bf5612018-11-13 16:36:22 +0530131@frappe.whitelist()
scmmishraa592f702018-11-20 18:37:01 +0530132def add_activity(course, content_type, content):
scmmishra000e7062019-03-19 12:30:43 +0530133 if not utils.get_current_student():
134 return
scmmishraa592f702018-11-20 18:37:01 +0530135 enrollment = utils.get_course_enrollment(course)
scmmishraac8d3472019-04-22 14:30:55 +0530136 enrollment.add_activity(content_type, content)
scmmishra35bf5612018-11-13 16:36:22 +0530137
scmmishrafdbabde2018-11-22 15:33:30 +0530138@frappe.whitelist()
scmmishrad5973fe2019-04-22 12:05:22 +0530139def get_student_course_details(course_name, program_name):
scmmishrada39da62018-12-13 11:51:31 +0530140 """
141 Return the porgress of a course in a program as well as the content to continue from.
scmmishra95bb3c02019-02-26 16:49:58 +0530142 :param course_name:
143 :param program_name:
scmmishrada39da62018-12-13 11:51:31 +0530144 """
scmmishra327334a2019-04-22 12:03:17 +0530145 student = utils.get_current_student()
146 if not student:
scmmishra000e7062019-03-19 12:30:43 +0530147 return {'flag':'Start Course' }
scmmishra3673b252019-04-22 18:10:19 +0530148
scmmishrafdbabde2018-11-22 15:33:30 +0530149 course_enrollment = utils.get_course_enrollment(course_name)
scmmishrab3154ef2018-12-06 20:13:20 +0530150 program_enrollment = utils.get_program_enrollment(program_name)
scmmishra3673b252019-04-22 18:10:19 +0530151
scmmishrab3154ef2018-12-06 20:13:20 +0530152 if not program_enrollment:
153 return None
scmmishra3673b252019-04-22 18:10:19 +0530154
scmmishra97c994f2018-11-26 14:41:15 +0530155 if not course_enrollment:
scmmishra5c646e62019-03-18 18:37:26 +0530156 course_enrollment = utils.enroll_in_course(course_name, program_name)
scmmishra3673b252019-04-22 18:10:19 +0530157
scmmishrada39da62018-12-13 11:51:31 +0530158 progress = course_enrollment.get_progress(student)
159 count = sum([activity['is_complete'] for activity in progress])
scmmishraa592f702018-11-20 18:37:01 +0530160 if count == 0:
scmmishra000e7062019-03-19 12:30:43 +0530161 return {'flag':'Start Course'}
scmmishraa592f702018-11-20 18:37:01 +0530162 elif count == len(progress):
scmmishra000e7062019-03-19 12:30:43 +0530163 return {'flag':'Completed'}
scmmishraa592f702018-11-20 18:37:01 +0530164 elif count < len(progress):
165 next_item = next(item for item in progress if item['is_complete']==False)
scmmishra000e7062019-03-19 12:30:43 +0530166 return {'flag':'Continue'}
scmmishra95bb3c02019-02-26 16:49:58 +0530167
scmmishraaffbfe72018-11-26 11:59:25 +0530168@frappe.whitelist()
scmmishrad78c3262019-04-22 12:06:28 +0530169def get_student_topic_details(topic_name, course_name):
scmmishra7e1678e2019-02-26 17:11:01 +0530170 """
171 Return the porgress of a course in a program as well as the content to continue from.
172 :param topic_name:
173 :param course_name:
scmmishra7e1678e2019-02-26 17:11:01 +0530174 """
scmmishra000e7062019-03-19 12:30:43 +0530175 topic = frappe.get_doc("Topic", topic_name)
scmmishra327334a2019-04-22 12:03:17 +0530176 student = utils.get_current_student()
177 if not student:
scmmishra000e7062019-03-19 12:30:43 +0530178 topic_content = topic.get_all_children()
179 if topic_content:
180 return {'flag':'Start Course', 'content_type': topic_content[0].content_type, 'content': topic_content[0].content}
181 else:
182 return None
scmmishra7e1678e2019-02-26 17:11:01 +0530183 course_enrollment = utils.get_course_enrollment(course_name)
scmmishrade5f71a2019-02-28 15:40:49 +0530184 progress = student.get_topic_progress(course_enrollment.name, topic)
scmmishra3726f8a2019-02-28 16:33:53 +0530185 if not progress:
186 return { 'flag':'Start Topic', 'content_type': None, 'content': None }
scmmishra7e1678e2019-02-26 17:11:01 +0530187 count = sum([activity['is_complete'] for activity in progress])
188 if count == 0:
scmmishrade5f71a2019-02-28 15:40:49 +0530189 return {'flag':'Start Topic', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']}
scmmishra7e1678e2019-02-26 17:11:01 +0530190 elif count == len(progress):
191 return {'flag':'Completed', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']}
192 elif count < len(progress):
193 next_item = next(item for item in progress if item['is_complete']==False)
194 return {'flag':'Continue', 'content_type': next_item['content_type'], 'content': next_item['content']}
195
196@frappe.whitelist()
scmmishra201fec32018-11-26 16:52:45 +0530197def get_program_progress(program_name):
scmmishra29558512018-11-26 19:16:54 +0530198 import math
scmmishraaffbfe72018-11-26 11:59:25 +0530199 program = frappe.get_doc("Program", program_name)
scmmishrada39da62018-12-13 11:51:31 +0530200 program_enrollment = utils.get_program_enrollment(program_name)
scmmishra9967d272019-04-22 12:08:41 +0530201 program_progress = {}
scmmishra97c994f2018-11-26 14:41:15 +0530202 if not program_enrollment:
203 return None
204 else:
scmmishra201fec32018-11-26 16:52:45 +0530205 progress = []
scmmishra97c994f2018-11-26 14:41:15 +0530206 for course in program.get_all_children():
scmmishra9967d272019-04-22 12:08:41 +0530207 course_progress = get_student_course_details(course.course, program_name)
scmmishra201fec32018-11-26 16:52:45 +0530208 is_complete = False
scmmishra9967d272019-04-22 12:08:41 +0530209 if course_progress['flag'] == "Completed":
scmmishra201fec32018-11-26 16:52:45 +0530210 is_complete = True
211 progress.append({'course_name': course.course_name, 'name': course.course, 'is_complete': is_complete})
scmmishra209250c2019-03-28 14:27:51 +0530212
scmmishra9967d272019-04-22 12:08:41 +0530213 program_progress['progress'] = progress
214 program_progress['name'] = program_name
215 program_progress['program'] = program.program_name
scmmishra209250c2019-03-28 14:27:51 +0530216
217 try:
scmmishra9967d272019-04-22 12:08:41 +0530218 program_progress['percentage'] = math.ceil((sum([item['is_complete'] for item in progress] * 100)/len(progress)))
scmmishra209250c2019-03-28 14:27:51 +0530219 except ZeroDivisionError:
scmmishra9967d272019-04-22 12:08:41 +0530220 program_progress['percentage'] = 0
scmmishra209250c2019-03-28 14:27:51 +0530221
scmmishra9967d272019-04-22 12:08:41 +0530222 return program_progress
scmmishra29558512018-11-26 19:16:54 +0530223
224@frappe.whitelist()
225def get_joining_date():
scmmishra5079e6c2019-04-22 14:46:26 +0530226 student = utils.get_current_student()
227 if student:
scmmishra9b7ac3e2019-03-28 14:47:22 +0530228 return student.joining_date
scmmishra4d102292018-12-07 17:41:40 +0530229
230@frappe.whitelist()
231def get_quiz_progress(program_name):
232 program = frappe.get_doc("Program", program_name)
scmmishrada39da62018-12-13 11:51:31 +0530233 program_enrollment = utils.get_program_enrollment(program_name)
scmmishra9967d272019-04-22 12:08:41 +0530234 quiz_progress = frappe._dict()
scmmishra327334a2019-04-22 12:03:17 +0530235 student = utils.get_current_student()
scmmishra4d102292018-12-07 17:41:40 +0530236 if not program_enrollment:
237 return None
238 else:
239 progress_list = []
240 for course in program.get_all_children():
241 course_enrollment = utils.get_course_enrollment(course.course)
scmmishra9967d272019-04-22 12:08:41 +0530242 course_progress = course_enrollment.get_progress(student)
243 for progress_item in course_progress:
scmmishra4d102292018-12-07 17:41:40 +0530244 if progress_item['content_type'] == "Quiz":
scmmishra87df23b2018-12-09 19:57:12 +0530245 progress_item['course'] = course.course_name
scmmishra4d102292018-12-07 17:41:40 +0530246 progress_list.append(progress_item)
scmmishra209250c2019-03-28 14:27:51 +0530247 if not progress_list:
248 return None
scmmishra9967d272019-04-22 12:08:41 +0530249 quiz_progress.quiz_attempt = progress_list
250 quiz_progress.name = program_name
251 quiz_progress.program = program.program_name
252 return quiz_progress
scmmishra95bb3c02019-02-26 16:49:58 +0530253
254
255@frappe.whitelist(allow_guest=True)
256def get_course_details(course_name):
257 try:
scmmishra64d2fe02019-04-22 12:30:08 +0530258 course = frappe.get_doc('Course', course_name)
scmmishra95bb3c02019-02-26 16:49:58 +0530259 return course
260 except:
261 return None
262
263# Functions to get program & course details
264@frappe.whitelist(allow_guest=True)
265def get_topics(course_name):
scmmishra4add8262019-04-22 12:28:13 +0530266 try:
267 course = frappe.get_doc('Course', course_name)
268 return course.get_topics()
269 except frappe.DoesNotExistError:
270 frappe.throw(_("Course {0} does not exist.".format(course_name)))
scmmishra23880ae2019-02-27 12:09:57 +0530271
272@frappe.whitelist()
scmmishra4add8262019-04-22 12:28:13 +0530273def get_content(content_type, content):
scmmishra23880ae2019-02-27 12:09:57 +0530274 try:
scmmishra4add8262019-04-22 12:28:13 +0530275 return frappe.get_doc(content_type, content)
276 except frappe.DoesNotExistError:
277 frappe.throw(_("{0} {1} does not exist.".format(content_type, content)))