2018-11-13 11:06:22 +00:00
|
|
|
from __future__ import unicode_literals
|
2018-11-19 10:43:21 +00:00
|
|
|
import erpnext.education.utils as utils
|
2018-11-13 11:06:22 +00:00
|
|
|
import frappe
|
|
|
|
|
2018-11-20 12:08:01 +00:00
|
|
|
# LMS Utils to Update State for Vue Store
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_program_enrollments():
|
2018-12-06 14:43:20 +00:00
|
|
|
student = utils.get_current_student()
|
|
|
|
if student == None:
|
|
|
|
return None
|
2019-04-22 06:33:17 +00:00
|
|
|
return student.get_program_enrollments()
|
2018-11-20 12:08:01 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_all_course_enrollments():
|
|
|
|
student = utils.get_current_student()
|
|
|
|
if student == None:
|
|
|
|
return None
|
2019-04-22 06:33:17 +00:00
|
|
|
return student.get_all_course_enrollments()
|
2018-11-20 12:08:01 +00:00
|
|
|
|
|
|
|
# Vue Client Functions
|
2018-11-13 11:06:22 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_portal_details():
|
2018-11-19 10:43:21 +00:00
|
|
|
"""
|
|
|
|
Returns portal details from Education Settings Doctype. This contains the Title and Description for LMS amoung other things.
|
|
|
|
"""
|
2019-03-18 10:06:49 +00:00
|
|
|
from erpnext import get_default_company
|
|
|
|
|
2018-11-13 11:06:22 +00:00
|
|
|
settings = frappe.get_doc("Education Settings")
|
2019-03-18 10:06:49 +00:00
|
|
|
title = settings.portal_title or get_default_company()
|
2018-11-13 11:06:22 +00:00
|
|
|
description = settings.description
|
|
|
|
return dict(title=title, description=description)
|
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_featured_programs():
|
|
|
|
featured_program_names = frappe.get_all("Program", filters={"is_published": True, "is_featured": True})
|
|
|
|
if featured_program_names:
|
2019-04-22 06:49:39 +00:00
|
|
|
featured_list = [utils.get_program_and_enrollment_status(program['name']) for program in featured_program_names]
|
2018-11-13 11:06:22 +00:00
|
|
|
return featured_list
|
|
|
|
else:
|
2019-03-18 10:10:47 +00:00
|
|
|
return get_all_programs()[:2]
|
2018-11-13 11:06:22 +00:00
|
|
|
|
2018-11-14 08:53:06 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_all_programs():
|
|
|
|
program_names = frappe.get_all("Program", filters={"is_published": True})
|
|
|
|
if program_names:
|
2019-04-22 06:49:39 +00:00
|
|
|
program_list = [utils.get_program_and_enrollment_status(program['name']) for program in program_names]
|
2019-03-18 10:10:47 +00:00
|
|
|
return program_list
|
2018-11-14 08:53:06 +00:00
|
|
|
|
2018-11-13 11:06:22 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-04-22 06:49:39 +00:00
|
|
|
def get_program(program_name):
|
2018-11-13 11:06:22 +00:00
|
|
|
try:
|
2019-04-22 06:49:39 +00:00
|
|
|
return frappe.get_doc('Program', program_name)
|
|
|
|
except frappe.DoesNotExistError:
|
|
|
|
frappe.throw(_("Program {0} does not exist.".format(program_name)))
|
2018-11-13 11:06:22 +00:00
|
|
|
|
|
|
|
# Functions to get program & course details
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_courses(program_name):
|
|
|
|
program = frappe.get_doc('Program', program_name)
|
|
|
|
courses = program.get_course_list()
|
2018-11-22 10:03:30 +00:00
|
|
|
return courses
|
2018-11-13 11:06:22 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2018-12-13 06:21:31 +00:00
|
|
|
def get_next_content(current_content, current_content_type, topic):
|
2018-11-13 11:06:22 +00:00
|
|
|
if frappe.session.user == "Guest":
|
|
|
|
return None
|
2018-12-13 06:21:31 +00:00
|
|
|
topic = frappe.get_doc("Topic", topic)
|
|
|
|
content_list = [{'content_type':item.doctype, 'content':item.name} for item in topic.get_contents()]
|
|
|
|
current_index = content_list.index({'content': current_content, 'content_type': current_content_type})
|
2018-11-13 11:06:22 +00:00
|
|
|
try:
|
|
|
|
return content_list[current_index + 1]
|
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_quiz_with_answers(quiz_name):
|
|
|
|
try:
|
|
|
|
quiz = frappe.get_doc("Quiz", quiz_name).get_questions()
|
|
|
|
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]
|
|
|
|
return quiz_output
|
|
|
|
except:
|
|
|
|
frappe.throw("Quiz {0} does not exist".format(quiz_name))
|
|
|
|
return None
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
2019-03-29 07:15:08 +00:00
|
|
|
def get_quiz_without_answers(quiz_name, course_name):
|
2018-11-13 11:06:22 +00:00
|
|
|
try:
|
2019-03-29 07:15:08 +00:00
|
|
|
quiz = frappe.get_doc("Quiz", quiz_name)
|
|
|
|
questions = quiz.get_questions()
|
2018-11-13 11:06:22 +00:00
|
|
|
except:
|
|
|
|
frappe.throw("Quiz {0} does not exist".format(quiz_name))
|
|
|
|
return None
|
|
|
|
|
2019-03-29 07:15:08 +00:00
|
|
|
enrollment = utils.get_course_enrollment(course_name).name
|
|
|
|
quiz_status = {}
|
|
|
|
quiz_status['is_complete'], quiz_status['score'], quiz_status['result'] = utils.check_quiz_completion(quiz, enrollment)
|
|
|
|
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]
|
|
|
|
return { 'quizData': quiz_output, 'status': quiz_status}
|
|
|
|
|
2018-11-13 11:06:22 +00:00
|
|
|
@frappe.whitelist()
|
2019-02-28 11:12:25 +00:00
|
|
|
def evaluate_quiz(course, quiz_response, quiz_name):
|
2018-11-13 11:06:22 +00:00
|
|
|
"""LMS Function: Evaluates a simple multiple choice quiz.
|
|
|
|
: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.
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
quiz_response = json.loads(quiz_response)
|
|
|
|
quiz = frappe.get_doc("Quiz", quiz_name)
|
|
|
|
answers, score, status = quiz.evaluate(quiz_response, quiz_name)
|
|
|
|
|
|
|
|
result = {k: ('Correct' if v else 'Wrong') for k,v in answers.items()}
|
|
|
|
result_data = []
|
|
|
|
for key in answers:
|
|
|
|
item = {}
|
|
|
|
item['question'] = key
|
|
|
|
item['quiz_result'] = result[key]
|
|
|
|
try:
|
2019-03-27 12:31:14 +00:00
|
|
|
if isinstance(quiz_response[key], list):
|
|
|
|
item['selected_option'] = ', '.join(frappe.get_value('Options', res, 'option') for res in quiz_response[key])
|
|
|
|
else:
|
|
|
|
item['selected_option'] = frappe.get_value('Options', quiz_response[key], 'option')
|
2018-11-13 11:06:22 +00:00
|
|
|
except:
|
|
|
|
item['selected_option'] = "Unattempted"
|
|
|
|
result_data.append(item)
|
|
|
|
|
2019-03-19 07:00:43 +00:00
|
|
|
add_quiz_activity(course, quiz_name, result_data, score, status)
|
2018-11-13 11:06:22 +00:00
|
|
|
return(score)
|
|
|
|
|
2019-03-19 07:00:43 +00:00
|
|
|
def add_quiz_activity(course, quiz_name, result_data, score, status):
|
|
|
|
if not utils.get_current_student():
|
|
|
|
return None
|
|
|
|
enrollment = utils.get_course_enrollment(course).name
|
2018-11-19 10:43:21 +00:00
|
|
|
quiz_activity = frappe.get_doc({
|
|
|
|
"doctype": "Quiz Activity",
|
|
|
|
"enrollment": enrollment,
|
|
|
|
"quiz": quiz_name,
|
|
|
|
"activity_date": frappe.utils.datetime.datetime.now(),
|
|
|
|
"result": result_data,
|
|
|
|
"score": score,
|
|
|
|
"status": status
|
2019-03-27 12:31:14 +00:00
|
|
|
}).insert()
|
2018-11-13 11:06:22 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def enroll_in_program(program_name):
|
2019-04-22 06:33:17 +00:00
|
|
|
student = utils.get_current_student()
|
|
|
|
if not student:
|
2019-03-06 10:15:35 +00:00
|
|
|
utils.create_student_from_current_user()
|
2018-11-13 11:06:22 +00:00
|
|
|
program_enrollment = student.enroll_in_program(program_name)
|
2018-11-15 05:46:53 +00:00
|
|
|
return program_name
|
2018-11-13 11:06:22 +00:00
|
|
|
|
2019-02-26 11:19:58 +00:00
|
|
|
# Academty Activity
|
2018-11-13 11:06:22 +00:00
|
|
|
@frappe.whitelist()
|
2018-11-20 13:07:01 +00:00
|
|
|
def add_activity(course, content_type, content):
|
2019-03-19 07:00:43 +00:00
|
|
|
if not utils.get_current_student():
|
|
|
|
return
|
2018-11-20 13:07:01 +00:00
|
|
|
enrollment = utils.get_course_enrollment(course)
|
|
|
|
if(utils.check_activity_exists(enrollment.name, content_type, content)):
|
2018-11-13 11:06:22 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
activity = frappe.get_doc({
|
|
|
|
"doctype": "Course Activity",
|
2018-11-20 13:07:01 +00:00
|
|
|
"enrollment": enrollment.name,
|
2018-11-13 11:06:22 +00:00
|
|
|
"content_type": content_type,
|
|
|
|
"content": content,
|
|
|
|
"activity_date": frappe.utils.datetime.datetime.now()
|
|
|
|
})
|
|
|
|
activity.save()
|
|
|
|
frappe.db.commit()
|
|
|
|
|
2018-11-22 10:03:30 +00:00
|
|
|
@frappe.whitelist()
|
2019-04-22 06:35:22 +00:00
|
|
|
def get_student_course_details(course_name, program_name):
|
2018-12-13 06:21:31 +00:00
|
|
|
"""
|
|
|
|
Return the porgress of a course in a program as well as the content to continue from.
|
2019-02-26 11:19:58 +00:00
|
|
|
:param course_name:
|
|
|
|
:param program_name:
|
2018-12-13 06:21:31 +00:00
|
|
|
"""
|
2019-04-22 06:33:17 +00:00
|
|
|
student = utils.get_current_student()
|
|
|
|
if not student:
|
2019-03-19 07:00:43 +00:00
|
|
|
return {'flag':'Start Course' }
|
2018-11-22 10:03:30 +00:00
|
|
|
course_enrollment = utils.get_course_enrollment(course_name)
|
2018-12-06 14:43:20 +00:00
|
|
|
program_enrollment = utils.get_program_enrollment(program_name)
|
|
|
|
if not program_enrollment:
|
|
|
|
return None
|
2018-11-26 09:11:15 +00:00
|
|
|
if not course_enrollment:
|
2019-03-18 13:07:26 +00:00
|
|
|
course_enrollment = utils.enroll_in_course(course_name, program_name)
|
2018-12-13 06:21:31 +00:00
|
|
|
progress = course_enrollment.get_progress(student)
|
|
|
|
count = sum([activity['is_complete'] for activity in progress])
|
2018-11-20 13:07:01 +00:00
|
|
|
if count == 0:
|
2019-03-19 07:00:43 +00:00
|
|
|
return {'flag':'Start Course'}
|
2018-11-20 13:07:01 +00:00
|
|
|
elif count == len(progress):
|
2019-03-19 07:00:43 +00:00
|
|
|
return {'flag':'Completed'}
|
2018-11-20 13:07:01 +00:00
|
|
|
elif count < len(progress):
|
|
|
|
next_item = next(item for item in progress if item['is_complete']==False)
|
2019-03-19 07:00:43 +00:00
|
|
|
return {'flag':'Continue'}
|
2019-02-26 11:19:58 +00:00
|
|
|
|
2019-02-26 11:41:01 +00:00
|
|
|
@frappe.whitelist()
|
2019-04-22 06:36:28 +00:00
|
|
|
def get_student_topic_details(topic_name, course_name):
|
2019-02-26 11:41:01 +00:00
|
|
|
"""
|
|
|
|
Return the porgress of a course in a program as well as the content to continue from.
|
|
|
|
:param topic_name:
|
|
|
|
:param course_name:
|
|
|
|
"""
|
2019-03-19 07:00:43 +00:00
|
|
|
topic = frappe.get_doc("Topic", topic_name)
|
2019-04-22 06:33:17 +00:00
|
|
|
student = utils.get_current_student()
|
|
|
|
if not student:
|
2019-03-19 07:00:43 +00:00
|
|
|
topic_content = topic.get_all_children()
|
|
|
|
if topic_content:
|
|
|
|
return {'flag':'Start Course', 'content_type': topic_content[0].content_type, 'content': topic_content[0].content}
|
|
|
|
else:
|
|
|
|
return None
|
2019-02-26 11:41:01 +00:00
|
|
|
course_enrollment = utils.get_course_enrollment(course_name)
|
2019-02-28 10:10:49 +00:00
|
|
|
progress = student.get_topic_progress(course_enrollment.name, topic)
|
2019-02-28 11:03:53 +00:00
|
|
|
if not progress:
|
|
|
|
return { 'flag':'Start Topic', 'content_type': None, 'content': None }
|
2019-02-26 11:41:01 +00:00
|
|
|
count = sum([activity['is_complete'] for activity in progress])
|
|
|
|
if count == 0:
|
2019-02-28 10:10:49 +00:00
|
|
|
return {'flag':'Start Topic', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']}
|
2019-02-26 11:41:01 +00:00
|
|
|
elif count == len(progress):
|
|
|
|
return {'flag':'Completed', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']}
|
|
|
|
elif count < len(progress):
|
|
|
|
next_item = next(item for item in progress if item['is_complete']==False)
|
|
|
|
return {'flag':'Continue', 'content_type': next_item['content_type'], 'content': next_item['content']}
|
|
|
|
|
2018-11-26 06:29:25 +00:00
|
|
|
@frappe.whitelist()
|
2018-11-26 11:22:45 +00:00
|
|
|
def get_program_progress(program_name):
|
2018-11-26 13:46:54 +00:00
|
|
|
import math
|
2018-11-26 06:29:25 +00:00
|
|
|
program = frappe.get_doc("Program", program_name)
|
2018-12-13 06:21:31 +00:00
|
|
|
program_enrollment = utils.get_program_enrollment(program_name)
|
2019-04-22 06:38:41 +00:00
|
|
|
program_progress = {}
|
2018-11-26 09:11:15 +00:00
|
|
|
if not program_enrollment:
|
|
|
|
return None
|
|
|
|
else:
|
2018-11-26 11:22:45 +00:00
|
|
|
progress = []
|
2018-11-26 09:11:15 +00:00
|
|
|
for course in program.get_all_children():
|
2019-04-22 06:38:41 +00:00
|
|
|
course_progress = get_student_course_details(course.course, program_name)
|
2018-11-26 11:22:45 +00:00
|
|
|
is_complete = False
|
2019-04-22 06:38:41 +00:00
|
|
|
if course_progress['flag'] == "Completed":
|
2018-11-26 11:22:45 +00:00
|
|
|
is_complete = True
|
|
|
|
progress.append({'course_name': course.course_name, 'name': course.course, 'is_complete': is_complete})
|
2019-03-28 08:57:51 +00:00
|
|
|
|
2019-04-22 06:38:41 +00:00
|
|
|
program_progress['progress'] = progress
|
|
|
|
program_progress['name'] = program_name
|
|
|
|
program_progress['program'] = program.program_name
|
2019-03-28 08:57:51 +00:00
|
|
|
|
|
|
|
try:
|
2019-04-22 06:38:41 +00:00
|
|
|
program_progress['percentage'] = math.ceil((sum([item['is_complete'] for item in progress] * 100)/len(progress)))
|
2019-03-28 08:57:51 +00:00
|
|
|
except ZeroDivisionError:
|
2019-04-22 06:38:41 +00:00
|
|
|
program_progress['percentage'] = 0
|
2019-03-28 08:57:51 +00:00
|
|
|
|
2019-04-22 06:38:41 +00:00
|
|
|
return program_progress
|
2018-11-26 13:46:54 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_joining_date():
|
2019-03-28 09:17:22 +00:00
|
|
|
current_student = utils.get_current_student()
|
2019-04-22 06:33:17 +00:00
|
|
|
if current_student:
|
2019-03-28 09:17:22 +00:00
|
|
|
return student.joining_date
|
|
|
|
else:
|
|
|
|
return None
|
2018-12-07 12:11:40 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_quiz_progress(program_name):
|
|
|
|
program = frappe.get_doc("Program", program_name)
|
2018-12-13 06:21:31 +00:00
|
|
|
program_enrollment = utils.get_program_enrollment(program_name)
|
2019-04-22 06:38:41 +00:00
|
|
|
quiz_progress = frappe._dict()
|
2019-04-22 06:33:17 +00:00
|
|
|
student = utils.get_current_student()
|
2018-12-07 12:11:40 +00:00
|
|
|
if not program_enrollment:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
progress_list = []
|
|
|
|
for course in program.get_all_children():
|
|
|
|
course_enrollment = utils.get_course_enrollment(course.course)
|
2019-04-22 06:38:41 +00:00
|
|
|
course_progress = course_enrollment.get_progress(student)
|
|
|
|
for progress_item in course_progress:
|
2018-12-07 12:11:40 +00:00
|
|
|
if progress_item['content_type'] == "Quiz":
|
2018-12-09 14:27:12 +00:00
|
|
|
progress_item['course'] = course.course_name
|
2018-12-07 12:11:40 +00:00
|
|
|
progress_list.append(progress_item)
|
2019-03-28 08:57:51 +00:00
|
|
|
if not progress_list:
|
|
|
|
return None
|
2019-04-22 06:38:41 +00:00
|
|
|
quiz_progress.quiz_attempt = progress_list
|
|
|
|
quiz_progress.name = program_name
|
|
|
|
quiz_progress.program = program.program_name
|
|
|
|
return quiz_progress
|
2019-02-26 11:19:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_course_details(course_name):
|
|
|
|
try:
|
2019-04-22 06:38:41 +00:00
|
|
|
course = sfrappe.get_doc('Course', course_name)
|
2019-02-26 11:19:58 +00:00
|
|
|
return course
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Functions to get program & course details
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_topics(course_name):
|
|
|
|
course = frappe.get_doc('Course', course_name)
|
|
|
|
topics = course.get_topics()
|
2019-02-27 06:39:57 +00:00
|
|
|
return topics
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_content(type, content):
|
|
|
|
try:
|
|
|
|
return frappe.get_doc(type, content)
|
|
|
|
except:
|
|
|
|
return None
|