2016-07-19 08:47:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2015, Frappe Technologies and contributors
|
|
|
|
# For lice
|
|
|
|
|
2018-10-12 09:48:26 +00:00
|
|
|
from __future__ import unicode_literals, division
|
2016-07-19 08:47:33 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
|
|
|
|
|
|
|
class OverlapError(frappe.ValidationError): pass
|
|
|
|
|
|
|
|
def validate_overlap_for(doc, doctype, fieldname, value=None):
|
2017-04-12 13:54:12 +00:00
|
|
|
"""Checks overlap for specified field.
|
2018-10-17 07:11:50 +00:00
|
|
|
|
|
|
|
:param fieldname: Checks Overlap for this field
|
2016-07-19 08:47:33 +00:00
|
|
|
"""
|
2018-10-17 07:11:50 +00:00
|
|
|
|
2016-07-19 08:47:33 +00:00
|
|
|
existing = get_overlap_for(doc, doctype, fieldname, value)
|
|
|
|
if existing:
|
|
|
|
frappe.throw(_("This {0} conflicts with {1} for {2} {3}").format(doc.doctype, existing.name,
|
|
|
|
doc.meta.get_label(fieldname) if not value else fieldname , value or doc.get(fieldname)), OverlapError)
|
2018-10-17 07:11:50 +00:00
|
|
|
|
2016-07-19 08:47:33 +00:00
|
|
|
def get_overlap_for(doc, doctype, fieldname, value=None):
|
2017-04-12 13:54:12 +00:00
|
|
|
"""Returns overlaping document for specified field.
|
2018-10-17 07:11:50 +00:00
|
|
|
|
|
|
|
:param fieldname: Checks Overlap for this field
|
2016-07-19 08:47:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
existing = frappe.db.sql("""select name, from_time, to_time from `tab{0}`
|
|
|
|
where `{1}`=%(val)s and schedule_date = %(schedule_date)s and
|
|
|
|
(
|
|
|
|
(from_time > %(from_time)s and from_time < %(to_time)s) or
|
|
|
|
(to_time > %(from_time)s and to_time < %(to_time)s) or
|
|
|
|
(%(from_time)s > from_time and %(from_time)s < to_time) or
|
|
|
|
(%(from_time)s = from_time and %(to_time)s = to_time))
|
2017-11-28 12:34:08 +00:00
|
|
|
and name!=%(name)s and docstatus!=2""".format(doctype, fieldname),
|
2016-07-19 08:47:33 +00:00
|
|
|
{
|
|
|
|
"schedule_date": doc.schedule_date,
|
|
|
|
"val": value or doc.get(fieldname),
|
|
|
|
"from_time": doc.from_time,
|
|
|
|
"to_time": doc.to_time,
|
|
|
|
"name": doc.name or "No Name"
|
|
|
|
}, as_dict=True)
|
|
|
|
|
|
|
|
return existing[0] if existing else None
|
2018-10-17 07:11:50 +00:00
|
|
|
|
2018-10-22 13:07:03 +00:00
|
|
|
|
2016-07-21 19:58:41 +00:00
|
|
|
def validate_duplicate_student(students):
|
|
|
|
unique_students= []
|
|
|
|
for stud in students:
|
|
|
|
if stud.student in unique_students:
|
|
|
|
frappe.throw(_("Student {0} - {1} appears Multiple times in row {2} & {3}")
|
|
|
|
.format(stud.student, stud.student_name, unique_students.index(stud.student)+1, stud.idx))
|
|
|
|
else:
|
|
|
|
unique_students.append(stud.student)
|
2018-10-12 09:48:26 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def evaluate_quiz(quiz_response, **kwargs):
|
|
|
|
"""LMS Function: Evaluates a simple multiple choice quiz. It recieves arguments from `www/lms/course.js` as dictionary using FormData[1].
|
2018-10-17 07:11:50 +00:00
|
|
|
|
2018-10-12 09:48:26 +00:00
|
|
|
|
|
|
|
: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.
|
|
|
|
[1]: https://developer.mozilla.org/en-US/docs/Web/API/FormData
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
quiz_response = json.loads(quiz_response)
|
2018-10-19 08:36:14 +00:00
|
|
|
quiz_name = kwargs.get('quiz')
|
2018-10-22 06:52:11 +00:00
|
|
|
course_name = kwargs.get('course')
|
2018-10-22 13:07:03 +00:00
|
|
|
enrollment = get_course_enrollment(course_name, frappe.session.user)
|
2018-10-12 09:48:26 +00:00
|
|
|
try:
|
2018-10-19 08:36:14 +00:00
|
|
|
quiz = frappe.get_doc("Quiz", quiz_name)
|
2018-10-22 13:07:03 +00:00
|
|
|
answers, score = quiz.evaluate(quiz_response, enrollment, quiz_name)
|
|
|
|
add_quiz_activity(enrollment, quiz_name, score, answers, quiz_response)
|
2018-10-22 06:52:11 +00:00
|
|
|
return score
|
2018-10-19 08:36:14 +00:00
|
|
|
except frappe.DoesNotExistError:
|
|
|
|
frappe.throw("Quiz {0} does not exist".format(quiz_name))
|
2018-10-22 06:52:11 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-10-22 13:07:03 +00:00
|
|
|
def add_quiz_activity(enrollment, quiz, score, answers, quiz_response):
|
|
|
|
print(quiz, answers, score)
|
2018-10-22 06:52:11 +00:00
|
|
|
if not enrollment:
|
2018-10-22 09:59:05 +00:00
|
|
|
frappe.throw("The user is not enrolled for the course {course}".format(course=course))
|
2018-10-22 06:52:11 +00:00
|
|
|
activity = frappe.get_doc({
|
|
|
|
"doctype": "Quiz Activity",
|
|
|
|
"enrollment": enrollment.name,
|
|
|
|
"quiz": quiz,
|
|
|
|
"score": score,
|
2018-10-22 13:07:03 +00:00
|
|
|
"date": frappe.utils.datetime.datetime.now()
|
2018-10-22 06:52:11 +00:00
|
|
|
})
|
2018-10-22 13:07:03 +00:00
|
|
|
for question in quiz_response.keys():
|
2018-10-22 09:59:05 +00:00
|
|
|
activity.append("result",
|
|
|
|
{
|
2018-10-22 13:07:03 +00:00
|
|
|
"question": question,
|
|
|
|
"selected_option": quiz_response[question],
|
|
|
|
"quiz_result": 'Correct' if answers[question] else 'Wrong'
|
|
|
|
})
|
2018-10-22 06:52:11 +00:00
|
|
|
activity.save()
|
|
|
|
frappe.db.commit()
|
2018-10-12 09:48:26 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2018-10-23 07:51:58 +00:00
|
|
|
def add_activity(content_type, content, course, program):
|
|
|
|
enrollment = get_course_enrollment(course, frappe.session.user)
|
|
|
|
if check_activity_exists(enrollment['name'], content_type, content):
|
|
|
|
pass
|
2018-10-12 09:48:26 +00:00
|
|
|
else:
|
2018-10-23 07:51:58 +00:00
|
|
|
course_actvity = frappe.get_doc({
|
|
|
|
"doctype": "Course Activity",
|
|
|
|
"enrollment": enrollment['name'],
|
|
|
|
"content_type": content_type,
|
|
|
|
"content": content,
|
|
|
|
"date": frappe.utils.datetime.datetime.now()
|
2018-10-12 09:48:26 +00:00
|
|
|
})
|
2018-10-23 07:51:58 +00:00
|
|
|
course_actvity.save()
|
2018-10-12 09:48:26 +00:00
|
|
|
frappe.db.commit()
|
|
|
|
|
2018-10-23 07:51:58 +00:00
|
|
|
def check_activity_exists(enrollment, content_type, content):
|
|
|
|
activity = frappe.get_list("Course Activity", filters={"enrollment": enrollment, "content_type": content_type, "content": content})
|
|
|
|
if activity:
|
|
|
|
return True
|
2018-10-12 09:48:26 +00:00
|
|
|
else:
|
2018-10-23 07:51:58 +00:00
|
|
|
return False
|
|
|
|
|
2018-10-15 06:15:17 +00:00
|
|
|
|
2018-10-16 11:16:27 +00:00
|
|
|
def get_program():
|
|
|
|
program_list = frappe.get_list("Program", filters={"is_published": is_published})
|
|
|
|
if program_list:
|
|
|
|
return program_list
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2018-10-16 12:24:18 +00:00
|
|
|
def get_featured_programs():
|
|
|
|
featured_programs_name = frappe.get_list("Program", filters={"is_published": True, "is_featured": True})
|
|
|
|
featured_list = [frappe.get_doc("Program", program["name"]) for program in featured_programs_name]
|
2018-10-16 11:16:27 +00:00
|
|
|
if featured_list:
|
|
|
|
return featured_list
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def add_course_enrollment(course, email):
|
|
|
|
student_id = get_student_id(email)
|
|
|
|
if not get_course_enrollment(course, email):
|
|
|
|
enrollment = frappe.get_doc({
|
|
|
|
"doctype": "Course Enrollment",
|
|
|
|
"student": student_id,
|
2018-10-22 06:52:11 +00:00
|
|
|
"course": course,
|
|
|
|
"enrollment_date": frappe.getdate()
|
2018-10-16 11:16:27 +00:00
|
|
|
})
|
|
|
|
enrollment.save()
|
|
|
|
frappe.db.commit()
|
|
|
|
return enrollment
|
|
|
|
|
|
|
|
def get_course_enrollment(course, email):
|
|
|
|
student_id = get_student_id(email)
|
|
|
|
try:
|
|
|
|
return frappe.get_list("Course Enrollment", filters={'course':course, 'student':student_id})[0]
|
|
|
|
except IndexError:
|
2018-10-23 07:51:58 +00:00
|
|
|
frappe.throw("The user is not enrolled for the course {course}".format(course=course))
|
2018-10-16 11:16:27 +00:00
|
|
|
return None
|
|
|
|
|
2018-10-22 06:52:11 +00:00
|
|
|
def get_student_id(email=None):
|
|
|
|
"""Returns student user name, example EDU-STU-2018-00001 (Based on the naming series).
|
2018-10-17 07:11:50 +00:00
|
|
|
|
2018-10-22 06:52:11 +00:00
|
|
|
:param user: a user email address
|
|
|
|
"""
|
2018-10-15 06:15:17 +00:00
|
|
|
try:
|
2018-10-22 06:52:11 +00:00
|
|
|
return frappe.get_all('Student', filters={'student_email_id': email}, fields=['name'])[0].name
|
2018-10-15 06:15:17 +00:00
|
|
|
except IndexError:
|
2018-10-22 06:52:11 +00:00
|
|
|
frappe.throw("Student with email {0} does not exist".format(email))
|
|
|
|
return None
|