blob: 69424e1f7d2f573784857de9fbe4d1f736c5d995 [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
scmmishra2b7e1582019-03-29 12:45:08 +053093 enrollment = utils.get_course_enrollment(course_name).name
scmmishra66d23922019-04-22 12:54:43 +053094 quiz_progress = {}
95 quiz_progress['is_complete'], quiz_progress['score'], quiz_progress['result'] = utils.check_quiz_completion(quiz, enrollment)
scmmishra2b7e1582019-03-29 12:45:08 +053096 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 +053097 return { 'quizData': quiz_output, 'status': quiz_progress}
scmmishra2b7e1582019-03-29 12:45:08 +053098
scmmishra35bf5612018-11-13 16:36:22 +053099@frappe.whitelist()
scmmishraab8fc8c2019-02-28 16:42:25 +0530100def evaluate_quiz(course, quiz_response, quiz_name):
scmmishra35bf5612018-11-13 16:36:22 +0530101 """LMS Function: Evaluates a simple multiple choice quiz.
scmmishra66d23922019-04-22 12:54:43 +0530102 :param course: name of the course
scmmishra35bf5612018-11-13 16:36:22 +0530103 :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 +0530104 :param quiz_name: Name of the quiz attempted
scmmishra35bf5612018-11-13 16:36:22 +0530105 """
106 import json
107 quiz_response = json.loads(quiz_response)
108 quiz = frappe.get_doc("Quiz", quiz_name)
109 answers, score, status = quiz.evaluate(quiz_response, quiz_name)
scmmishraac8d3472019-04-22 14:30:55 +0530110 print(answers)
scmmishra35bf5612018-11-13 16:36:22 +0530111
scmmishraac8d3472019-04-22 14:30:55 +0530112 course_enrollment = utils.get_course_enrollment(course)
113 if course_enrollment:
114 course_enrollment.add_quiz_activity(quiz_name, quiz_response, answers, score, status)
scmmishra35bf5612018-11-13 16:36:22 +0530115
scmmishraac8d3472019-04-22 14:30:55 +0530116 return score
scmmishra35bf5612018-11-13 16:36:22 +0530117
118@frappe.whitelist()
scmmishra35bf5612018-11-13 16:36:22 +0530119def enroll_in_program(program_name):
scmmishra327334a2019-04-22 12:03:17 +0530120 student = utils.get_current_student()
121 if not student:
scmmishrabf9a10f2019-03-06 15:45:35 +0530122 utils.create_student_from_current_user()
scmmishra35bf5612018-11-13 16:36:22 +0530123 program_enrollment = student.enroll_in_program(program_name)
scmmishra0a4902f2018-11-15 11:16:53 +0530124 return program_name
scmmishra35bf5612018-11-13 16:36:22 +0530125
scmmishraac8d3472019-04-22 14:30:55 +0530126# Academdy Activity
scmmishra35bf5612018-11-13 16:36:22 +0530127@frappe.whitelist()
scmmishraa592f702018-11-20 18:37:01 +0530128def add_activity(course, content_type, content):
scmmishra000e7062019-03-19 12:30:43 +0530129 if not utils.get_current_student():
130 return
scmmishraa592f702018-11-20 18:37:01 +0530131 enrollment = utils.get_course_enrollment(course)
scmmishraac8d3472019-04-22 14:30:55 +0530132 enrollment.add_activity(content_type, content)
scmmishra35bf5612018-11-13 16:36:22 +0530133
scmmishrafdbabde2018-11-22 15:33:30 +0530134@frappe.whitelist()
scmmishrad5973fe2019-04-22 12:05:22 +0530135def get_student_course_details(course_name, program_name):
scmmishrada39da62018-12-13 11:51:31 +0530136 """
137 Return the porgress of a course in a program as well as the content to continue from.
scmmishra95bb3c02019-02-26 16:49:58 +0530138 :param course_name:
139 :param program_name:
scmmishrada39da62018-12-13 11:51:31 +0530140 """
scmmishra327334a2019-04-22 12:03:17 +0530141 student = utils.get_current_student()
142 if not student:
scmmishra000e7062019-03-19 12:30:43 +0530143 return {'flag':'Start Course' }
scmmishrafdbabde2018-11-22 15:33:30 +0530144 course_enrollment = utils.get_course_enrollment(course_name)
scmmishrab3154ef2018-12-06 20:13:20 +0530145 program_enrollment = utils.get_program_enrollment(program_name)
146 if not program_enrollment:
147 return None
scmmishra97c994f2018-11-26 14:41:15 +0530148 if not course_enrollment:
scmmishra5c646e62019-03-18 18:37:26 +0530149 course_enrollment = utils.enroll_in_course(course_name, program_name)
scmmishrada39da62018-12-13 11:51:31 +0530150 progress = course_enrollment.get_progress(student)
151 count = sum([activity['is_complete'] for activity in progress])
scmmishraa592f702018-11-20 18:37:01 +0530152 if count == 0:
scmmishra000e7062019-03-19 12:30:43 +0530153 return {'flag':'Start Course'}
scmmishraa592f702018-11-20 18:37:01 +0530154 elif count == len(progress):
scmmishra000e7062019-03-19 12:30:43 +0530155 return {'flag':'Completed'}
scmmishraa592f702018-11-20 18:37:01 +0530156 elif count < len(progress):
157 next_item = next(item for item in progress if item['is_complete']==False)
scmmishra000e7062019-03-19 12:30:43 +0530158 return {'flag':'Continue'}
scmmishra95bb3c02019-02-26 16:49:58 +0530159
scmmishraaffbfe72018-11-26 11:59:25 +0530160@frappe.whitelist()
scmmishrad78c3262019-04-22 12:06:28 +0530161def get_student_topic_details(topic_name, course_name):
scmmishra7e1678e2019-02-26 17:11:01 +0530162 """
163 Return the porgress of a course in a program as well as the content to continue from.
164 :param topic_name:
165 :param course_name:
scmmishra7e1678e2019-02-26 17:11:01 +0530166 """
scmmishra000e7062019-03-19 12:30:43 +0530167 topic = frappe.get_doc("Topic", topic_name)
scmmishra327334a2019-04-22 12:03:17 +0530168 student = utils.get_current_student()
169 if not student:
scmmishra000e7062019-03-19 12:30:43 +0530170 topic_content = topic.get_all_children()
171 if topic_content:
172 return {'flag':'Start Course', 'content_type': topic_content[0].content_type, 'content': topic_content[0].content}
173 else:
174 return None
scmmishra7e1678e2019-02-26 17:11:01 +0530175 course_enrollment = utils.get_course_enrollment(course_name)
scmmishrade5f71a2019-02-28 15:40:49 +0530176 progress = student.get_topic_progress(course_enrollment.name, topic)
scmmishra3726f8a2019-02-28 16:33:53 +0530177 if not progress:
178 return { 'flag':'Start Topic', 'content_type': None, 'content': None }
scmmishra7e1678e2019-02-26 17:11:01 +0530179 count = sum([activity['is_complete'] for activity in progress])
180 if count == 0:
scmmishrade5f71a2019-02-28 15:40:49 +0530181 return {'flag':'Start Topic', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']}
scmmishra7e1678e2019-02-26 17:11:01 +0530182 elif count == len(progress):
183 return {'flag':'Completed', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']}
184 elif count < len(progress):
185 next_item = next(item for item in progress if item['is_complete']==False)
186 return {'flag':'Continue', 'content_type': next_item['content_type'], 'content': next_item['content']}
187
188@frappe.whitelist()
scmmishra201fec32018-11-26 16:52:45 +0530189def get_program_progress(program_name):
scmmishra29558512018-11-26 19:16:54 +0530190 import math
scmmishraaffbfe72018-11-26 11:59:25 +0530191 program = frappe.get_doc("Program", program_name)
scmmishrada39da62018-12-13 11:51:31 +0530192 program_enrollment = utils.get_program_enrollment(program_name)
scmmishra9967d272019-04-22 12:08:41 +0530193 program_progress = {}
scmmishra97c994f2018-11-26 14:41:15 +0530194 if not program_enrollment:
195 return None
196 else:
scmmishra201fec32018-11-26 16:52:45 +0530197 progress = []
scmmishra97c994f2018-11-26 14:41:15 +0530198 for course in program.get_all_children():
scmmishra9967d272019-04-22 12:08:41 +0530199 course_progress = get_student_course_details(course.course, program_name)
scmmishra201fec32018-11-26 16:52:45 +0530200 is_complete = False
scmmishra9967d272019-04-22 12:08:41 +0530201 if course_progress['flag'] == "Completed":
scmmishra201fec32018-11-26 16:52:45 +0530202 is_complete = True
203 progress.append({'course_name': course.course_name, 'name': course.course, 'is_complete': is_complete})
scmmishra209250c2019-03-28 14:27:51 +0530204
scmmishra9967d272019-04-22 12:08:41 +0530205 program_progress['progress'] = progress
206 program_progress['name'] = program_name
207 program_progress['program'] = program.program_name
scmmishra209250c2019-03-28 14:27:51 +0530208
209 try:
scmmishra9967d272019-04-22 12:08:41 +0530210 program_progress['percentage'] = math.ceil((sum([item['is_complete'] for item in progress] * 100)/len(progress)))
scmmishra209250c2019-03-28 14:27:51 +0530211 except ZeroDivisionError:
scmmishra9967d272019-04-22 12:08:41 +0530212 program_progress['percentage'] = 0
scmmishra209250c2019-03-28 14:27:51 +0530213
scmmishra9967d272019-04-22 12:08:41 +0530214 return program_progress
scmmishra29558512018-11-26 19:16:54 +0530215
216@frappe.whitelist()
217def get_joining_date():
scmmishra9b7ac3e2019-03-28 14:47:22 +0530218 current_student = utils.get_current_student()
scmmishra327334a2019-04-22 12:03:17 +0530219 if current_student:
scmmishra9b7ac3e2019-03-28 14:47:22 +0530220 return student.joining_date
221 else:
222 return None
scmmishra4d102292018-12-07 17:41:40 +0530223
224@frappe.whitelist()
225def get_quiz_progress(program_name):
226 program = frappe.get_doc("Program", program_name)
scmmishrada39da62018-12-13 11:51:31 +0530227 program_enrollment = utils.get_program_enrollment(program_name)
scmmishra9967d272019-04-22 12:08:41 +0530228 quiz_progress = frappe._dict()
scmmishra327334a2019-04-22 12:03:17 +0530229 student = utils.get_current_student()
scmmishra4d102292018-12-07 17:41:40 +0530230 if not program_enrollment:
231 return None
232 else:
233 progress_list = []
234 for course in program.get_all_children():
235 course_enrollment = utils.get_course_enrollment(course.course)
scmmishra9967d272019-04-22 12:08:41 +0530236 course_progress = course_enrollment.get_progress(student)
237 for progress_item in course_progress:
scmmishra4d102292018-12-07 17:41:40 +0530238 if progress_item['content_type'] == "Quiz":
scmmishra87df23b2018-12-09 19:57:12 +0530239 progress_item['course'] = course.course_name
scmmishra4d102292018-12-07 17:41:40 +0530240 progress_list.append(progress_item)
scmmishra209250c2019-03-28 14:27:51 +0530241 if not progress_list:
242 return None
scmmishra9967d272019-04-22 12:08:41 +0530243 quiz_progress.quiz_attempt = progress_list
244 quiz_progress.name = program_name
245 quiz_progress.program = program.program_name
246 return quiz_progress
scmmishra95bb3c02019-02-26 16:49:58 +0530247
248
249@frappe.whitelist(allow_guest=True)
250def get_course_details(course_name):
251 try:
scmmishra64d2fe02019-04-22 12:30:08 +0530252 course = frappe.get_doc('Course', course_name)
scmmishra95bb3c02019-02-26 16:49:58 +0530253 return course
254 except:
255 return None
256
257# Functions to get program & course details
258@frappe.whitelist(allow_guest=True)
259def get_topics(course_name):
scmmishra4add8262019-04-22 12:28:13 +0530260 try:
261 course = frappe.get_doc('Course', course_name)
262 return course.get_topics()
263 except frappe.DoesNotExistError:
264 frappe.throw(_("Course {0} does not exist.".format(course_name)))
scmmishra23880ae2019-02-27 12:09:57 +0530265
266@frappe.whitelist()
scmmishra4add8262019-04-22 12:28:13 +0530267def get_content(content_type, content):
scmmishra23880ae2019-02-27 12:09:57 +0530268 try:
scmmishra4add8262019-04-22 12:28:13 +0530269 return frappe.get_doc(content_type, content)
270 except frappe.DoesNotExistError:
271 frappe.throw(_("{0} {1} does not exist.".format(content_type, content)))