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
|
|
|
|
2018-11-19 10:43:21 +00:00
|
|
|
return None
|
|
|
|
|
2018-11-20 12:08:01 +00:00
|
|
|
# LMS Utils
|
2018-11-19 10:43:21 +00:00
|
|
|
def get_current_student():
|
|
|
|
"""
|
|
|
|
Returns student user name, example EDU-STU-2018-00001 (Based on the naming series).
|
|
|
|
Takes email from from frappe.session.user
|
|
|
|
"""
|
|
|
|
email = frappe.session.user
|
|
|
|
if email in ('Administrator', 'Guest'):
|
|
|
|
return None
|
|
|
|
try:
|
2018-12-13 06:21:31 +00:00
|
|
|
student_id = frappe.get_all("Student", {"student_email_id": email}, ["name"])[0].name
|
2018-11-19 10:43:21 +00:00
|
|
|
return student_id
|
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
2019-03-19 07:00:43 +00:00
|
|
|
def check_super_access():
|
|
|
|
current_user = frappe.get_doc('User', frappe.session.user)
|
|
|
|
roles = set([role.role for role in current_user.roles])
|
|
|
|
return bool(roles & {'Administrator', 'Instructor', 'Education Manager', 'System Manager', 'Academic User'})
|
|
|
|
|
2018-11-19 10:43:21 +00:00
|
|
|
def get_program_enrollment(program_name):
|
|
|
|
"""
|
|
|
|
Function to get program enrollments for a particular student for a program
|
|
|
|
"""
|
|
|
|
student = get_current_student()
|
|
|
|
if not student:
|
|
|
|
return None
|
|
|
|
else:
|
2018-12-13 06:21:31 +00:00
|
|
|
enrollment = frappe.get_all("Program Enrollment", filters={'student':student, 'program': program_name})
|
2018-11-19 10:43:21 +00:00
|
|
|
if enrollment:
|
|
|
|
return enrollment[0].name
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_program(program_name):
|
|
|
|
program = frappe.get_doc('Program', program_name)
|
2019-03-19 07:00:43 +00:00
|
|
|
is_enrolled = bool(get_program_enrollment(program_name)) or check_super_access()
|
2018-11-19 10:43:21 +00:00
|
|
|
return {'program': program, 'is_enrolled': is_enrolled}
|
|
|
|
|
|
|
|
def get_course_enrollment(course_name):
|
2018-11-20 12:08:01 +00:00
|
|
|
student = get_current_student()
|
2018-11-19 10:43:21 +00:00
|
|
|
enrollment_name = frappe.get_all("Course Enrollment", filters={'student': student, 'course':course_name})
|
|
|
|
try:
|
|
|
|
name = enrollment_name[0].name
|
|
|
|
enrollment = frappe.get_doc("Course Enrollment", name)
|
|
|
|
return enrollment
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2019-03-06 10:15:35 +00:00
|
|
|
def create_student_from_current_user():
|
2018-11-23 11:46:22 +00:00
|
|
|
user = frappe.get_doc("User", frappe.session.user)
|
2018-11-19 10:43:21 +00:00
|
|
|
student = frappe.get_doc({
|
|
|
|
"doctype": "Student",
|
2018-11-23 11:46:22 +00:00
|
|
|
"first_name": user.first_name,
|
|
|
|
"last_name": user.last_name,
|
|
|
|
"student_email_id": user.email,
|
2018-12-06 14:47:39 +00:00
|
|
|
"user": frappe.session.user
|
2018-11-19 10:43:21 +00:00
|
|
|
})
|
|
|
|
student.save(ignore_permissions=True)
|
|
|
|
frappe.db.commit()
|
2018-11-26 09:11:15 +00:00
|
|
|
return student
|
|
|
|
|
|
|
|
def enroll_in_course(course_name, program_name):
|
|
|
|
student_id = get_current_student()
|
|
|
|
student = frappe.get_doc("Student", student_id)
|
2019-03-18 13:07:26 +00:00
|
|
|
return student.enroll_in_course(course_name=course_name, program_enrollment=get_program_enrollment(program_name))
|
2018-11-19 10:43:21 +00:00
|
|
|
|
|
|
|
def check_activity_exists(enrollment, content_type, content):
|
|
|
|
activity = frappe.get_all("Course Activity", filters={'enrollment': enrollment, 'content_type': content_type, 'content': content})
|
2018-12-05 05:05:48 +00:00
|
|
|
return bool(activity)
|
|
|
|
|
2018-12-12 10:44:36 +00:00
|
|
|
def check_content_completion(content_name, content_type, enrollment_name):
|
2018-12-13 06:21:31 +00:00
|
|
|
activity = frappe.get_all("Course Activity", filters={'enrollment': enrollment_name, 'content_type': content_type, 'content': content_name})
|
2018-12-12 10:44:36 +00:00
|
|
|
if activity:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def check_quiz_completion(quiz, enrollment_name):
|
2018-12-13 06:21:31 +00:00
|
|
|
attempts = frappe.get_all("Quiz Activity", filters={'enrollment': enrollment_name, 'quiz': quiz.name}, fields=["name", "activity_date", "score", "status"])
|
2018-12-12 10:44:36 +00:00
|
|
|
status = bool(len(attempts) == quiz.max_attempts)
|
|
|
|
score = None
|
|
|
|
result = None
|
|
|
|
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
|
2019-03-06 10:15:35 +00:00
|
|
|
return status, score, result
|