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
|
2018-11-20 12:08:01 +00:00
|
|
|
try:
|
2018-12-06 14:43:20 +00:00
|
|
|
student = frappe.get_doc("Student", student)
|
2018-11-20 12:08:01 +00:00
|
|
|
return student.get_program_enrollments()
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_all_course_enrollments():
|
|
|
|
student = utils.get_current_student()
|
|
|
|
if student == None:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
student = frappe.get_doc("Student", student)
|
|
|
|
return student.get_all_course_enrollments()
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
"""
|
2018-11-13 11:06:22 +00:00
|
|
|
settings = frappe.get_doc("Education Settings")
|
|
|
|
title = settings.portal_title
|
|
|
|
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:
|
2018-11-19 10:43:21 +00:00
|
|
|
featured_list = [utils.get_program(program['name']) for program in featured_program_names]
|
2018-11-13 11:06:22 +00:00
|
|
|
return featured_list
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
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:
|
2018-11-19 10:43:21 +00:00
|
|
|
featured_list = [utils.get_program(program['name']) for program in program_names]
|
2018-11-14 08:53:06 +00:00
|
|
|
return featured_list
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-11-13 11:06:22 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_program_details(program_name):
|
|
|
|
try:
|
|
|
|
program = frappe.get_doc('Program', program_name)
|
|
|
|
return program
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
def get_next_content(content, content_type, course):
|
|
|
|
if frappe.session.user == "Guest":
|
|
|
|
return None
|
|
|
|
course_doc = frappe.get_doc("Course", course)
|
|
|
|
content_list = [{'content_type':item.content_type, 'content':item.content} for item in course_doc.get_all_children()]
|
|
|
|
current_index = content_list.index({'content': content, 'content_type': content_type})
|
|
|
|
try:
|
2018-11-20 12:08:01 +00:00
|
|
|
# print(content_list[current_index + 1])
|
2018-11-13 11:06:22 +00:00
|
|
|
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()
|
|
|
|
def get_quiz_without_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} 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()
|
|
|
|
def evaluate_quiz(enrollment, quiz_response, quiz_name):
|
|
|
|
"""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.
|
|
|
|
"""
|
2018-11-19 10:43:21 +00:00
|
|
|
|
2018-11-13 11:06:22 +00:00
|
|
|
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:
|
|
|
|
item['selected_option'] = frappe.get_value('Options', quiz_response[key], 'option')
|
|
|
|
except:
|
|
|
|
item['selected_option'] = "Unattempted"
|
|
|
|
result_data.append(item)
|
|
|
|
# result_data = [{'question': key, 'selected_option': frappe.get_value('Options', quiz_response[key], 'option'), 'quiz_result': result[key]} for key in answers]
|
|
|
|
|
|
|
|
add_quiz_activity(enrollment, quiz_name, result_data, score, status)
|
|
|
|
return(score)
|
|
|
|
|
2018-11-19 10:43:21 +00:00
|
|
|
def add_quiz_activity(enrollment, quiz_name, result_data, score, status):
|
|
|
|
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
|
|
|
|
})
|
|
|
|
quiz_activity.save()
|
|
|
|
frappe.db.commit()
|
2018-11-13 11:06:22 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def enroll_in_program(program_name):
|
2018-11-19 10:43:21 +00:00
|
|
|
if(not utils.get_current_student()):
|
2018-11-23 11:46:22 +00:00
|
|
|
utils.create_student()
|
2018-11-19 10:43:21 +00:00
|
|
|
student = frappe.get_doc("Student", utils.get_current_student())
|
2018-11-13 11:06:22 +00:00
|
|
|
program_enrollment = student.enroll_in_program(program_name)
|
2018-11-19 10:43:21 +00:00
|
|
|
utils.enroll_all_courses_in_program(program_enrollment, student)
|
2018-11-15 05:46:53 +00:00
|
|
|
return program_name
|
2018-11-13 11:06:22 +00:00
|
|
|
|
|
|
|
# Academty Activity
|
|
|
|
@frappe.whitelist()
|
2018-11-20 13:07:01 +00:00
|
|
|
def add_activity(course, content_type, content):
|
|
|
|
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-19 10:43:21 +00:00
|
|
|
def get_course_progress(course_enrollment):
|
|
|
|
course = frappe.get_doc('Course', course_enrollment.course)
|
2018-11-20 12:08:01 +00:00
|
|
|
contents = course.get_contents()
|
|
|
|
progress = []
|
2018-11-22 10:03:30 +00:00
|
|
|
for content in contents:
|
2018-11-20 12:08:01 +00:00
|
|
|
if content.doctype in ('Article', 'Video'):
|
|
|
|
status = check_content_completion(content.name, content.doctype, course_enrollment.name)
|
|
|
|
progress.append({'content': content.name, 'content_type': content.doctype, 'is_complete': status})
|
|
|
|
elif content.doctype == 'Quiz':
|
2018-11-20 13:07:01 +00:00
|
|
|
status, score, result = check_quiz_completion(content, course_enrollment.name)
|
|
|
|
progress.append({'content': content.name, 'content_type': content.doctype, 'is_complete': status, 'score': score, 'result': result})
|
2018-11-20 12:08:01 +00:00
|
|
|
return progress
|
|
|
|
|
|
|
|
def check_content_completion(content_name, content_type, enrollment_name):
|
|
|
|
activity = frappe.get_list("Course Activity", filters={'enrollment': enrollment_name, 'content_type': content_type, 'content': content_name})
|
|
|
|
if activity:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def check_quiz_completion(quiz, enrollment_name):
|
|
|
|
attempts = frappe.get_list("Quiz Activity", filters={'enrollment': enrollment_name, 'quiz': quiz.name}, fields=["name", "activity_date", "score", "status"])
|
2018-11-20 13:07:01 +00:00
|
|
|
status = bool(len(attempts) == quiz.max_attempts)
|
|
|
|
score = None
|
|
|
|
result = None
|
2018-11-22 10:03:30 +00:00
|
|
|
if attempts:
|
|
|
|
if quiz.grading_basis == 'Last Highest Score':
|
|
|
|
attempts = sorted(attempts, key = lambda i: int(i.score), reverse=True)
|
|
|
|
score = attempts[0]['score']
|
|
|
|
result = attempts[0]['status']
|
|
|
|
if result == 'Pass':
|
|
|
|
status = True
|
2018-11-20 13:07:01 +00:00
|
|
|
return status, score, result
|
2018-11-22 10:03:30 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2018-11-26 09:11:15 +00:00
|
|
|
def get_course_meta(course_name, program_name):
|
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:
|
|
|
|
utils.enroll_in_course(course_name, program_name)
|
2018-11-20 13:07:01 +00:00
|
|
|
progress = get_course_progress(course_enrollment)
|
|
|
|
count = sum([act['is_complete'] for act in progress])
|
|
|
|
if count == 0:
|
|
|
|
return {'flag':'Start Course', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']}
|
|
|
|
elif count == len(progress):
|
2018-12-05 05:05:48 +00:00
|
|
|
return {'flag':'Completed', 'content_type': progress[0]['content_type'], 'content': progress[0]['content']}
|
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)
|
|
|
|
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-11-26 09:11:15 +00:00
|
|
|
program_enrollment = frappe.get_list("Program Enrollment", filters={'student': utils.get_current_student(), 'program': program_name })[0].name
|
2018-11-26 11:22:45 +00:00
|
|
|
program_meta = {}
|
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():
|
2018-11-26 11:22:45 +00:00
|
|
|
meta = get_course_meta(course.course, program_name)
|
|
|
|
is_complete = False
|
|
|
|
if meta['flag'] == "Complete":
|
|
|
|
is_complete = True
|
|
|
|
progress.append({'course_name': course.course_name, 'name': course.course, 'is_complete': is_complete})
|
|
|
|
program_meta['progress'] = progress
|
|
|
|
program_meta['name'] = program_name
|
|
|
|
program_meta['program'] = program.program_name
|
2018-11-26 13:46:54 +00:00
|
|
|
program_meta['percentage'] = math.ceil((sum([item['is_complete'] for item in progress] * 100)/len(progress)))
|
|
|
|
return program_meta
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_joining_date():
|
|
|
|
student = frappe.get_doc("Student", utils.get_current_student())
|
|
|
|
return student.joining_date
|