Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright (c) 2015, Frappe Technologies and contributors |
| 3 | # For lice |
| 4 | |
scmmishra | 4ae11f4 | 2018-10-12 15:18:26 +0530 | [diff] [blame] | 5 | from __future__ import unicode_literals, division |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 6 | import frappe |
| 7 | from frappe import _ |
| 8 | |
| 9 | class OverlapError(frappe.ValidationError): pass |
| 10 | |
| 11 | def validate_overlap_for(doc, doctype, fieldname, value=None): |
Manas Solanki | 54c4240 | 2017-04-12 19:24:12 +0530 | [diff] [blame] | 12 | """Checks overlap for specified field. |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 13 | |
| 14 | :param fieldname: Checks Overlap for this field |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 15 | """ |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 16 | |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 17 | existing = get_overlap_for(doc, doctype, fieldname, value) |
| 18 | if existing: |
| 19 | frappe.throw(_("This {0} conflicts with {1} for {2} {3}").format(doc.doctype, existing.name, |
| 20 | doc.meta.get_label(fieldname) if not value else fieldname , value or doc.get(fieldname)), OverlapError) |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 21 | |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 22 | def get_overlap_for(doc, doctype, fieldname, value=None): |
Manas Solanki | 54c4240 | 2017-04-12 19:24:12 +0530 | [diff] [blame] | 23 | """Returns overlaping document for specified field. |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 24 | |
| 25 | :param fieldname: Checks Overlap for this field |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 26 | """ |
| 27 | |
| 28 | existing = frappe.db.sql("""select name, from_time, to_time from `tab{0}` |
| 29 | where `{1}`=%(val)s and schedule_date = %(schedule_date)s and |
| 30 | ( |
| 31 | (from_time > %(from_time)s and from_time < %(to_time)s) or |
| 32 | (to_time > %(from_time)s and to_time < %(to_time)s) or |
| 33 | (%(from_time)s > from_time and %(from_time)s < to_time) or |
| 34 | (%(from_time)s = from_time and %(to_time)s = to_time)) |
Manas Solanki | 84e9d45 | 2017-11-28 18:04:08 +0530 | [diff] [blame] | 35 | and name!=%(name)s and docstatus!=2""".format(doctype, fieldname), |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 36 | { |
| 37 | "schedule_date": doc.schedule_date, |
| 38 | "val": value or doc.get(fieldname), |
| 39 | "from_time": doc.from_time, |
| 40 | "to_time": doc.to_time, |
| 41 | "name": doc.name or "No Name" |
| 42 | }, as_dict=True) |
| 43 | |
| 44 | return existing[0] if existing else None |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 45 | |
Neil Trini Lasrado | f521a9c | 2016-07-22 01:28:41 +0530 | [diff] [blame] | 46 | def validate_duplicate_student(students): |
| 47 | unique_students= [] |
| 48 | for stud in students: |
| 49 | if stud.student in unique_students: |
| 50 | frappe.throw(_("Student {0} - {1} appears Multiple times in row {2} & {3}") |
| 51 | .format(stud.student, stud.student_name, unique_students.index(stud.student)+1, stud.idx)) |
| 52 | else: |
| 53 | unique_students.append(stud.student) |
scmmishra | 4ae11f4 | 2018-10-12 15:18:26 +0530 | [diff] [blame] | 54 | |
| 55 | def get_student_name(email=None): |
| 56 | """Returns student user name, example EDU-STU-2018-00001 (Based on the naming series). |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 57 | |
scmmishra | 4ae11f4 | 2018-10-12 15:18:26 +0530 | [diff] [blame] | 58 | :param user: a user email address |
| 59 | """ |
| 60 | try: |
| 61 | return frappe.get_all('Student', filters={'student_email_id': email}, fields=['name'])[0].name |
| 62 | except IndexError: |
| 63 | return None |
| 64 | |
| 65 | @frappe.whitelist() |
| 66 | def evaluate_quiz(quiz_response, **kwargs): |
| 67 | """LMS Function: Evaluates a simple multiple choice quiz. It recieves arguments from `www/lms/course.js` as dictionary using FormData[1]. |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 68 | |
scmmishra | 4ae11f4 | 2018-10-12 15:18:26 +0530 | [diff] [blame] | 69 | |
| 70 | :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. |
| 71 | [1]: https://developer.mozilla.org/en-US/docs/Web/API/FormData |
| 72 | """ |
| 73 | import json |
| 74 | quiz_response = json.loads(quiz_response) |
| 75 | correct_answers = [frappe.get_value('Question', name, 'correct_options') for name in quiz_response.keys()] |
| 76 | selected_options = quiz_response.values() |
| 77 | result = [selected == correct for selected, correct in zip(selected_options, correct_answers)] |
| 78 | try: |
| 79 | score = int((result.count(True)/len(selected_options))*100) |
| 80 | except ZeroDivisionError: |
| 81 | score = 0 |
| 82 | |
| 83 | kwargs['selected_options'] = selected_options |
| 84 | kwargs['result'] = result |
| 85 | kwargs['score'] = score |
| 86 | add_activity('Quiz', **kwargs) |
| 87 | return score |
| 88 | |
| 89 | @frappe.whitelist() |
| 90 | def add_activity(content_type, **kwargs): |
| 91 | activity_does_not_exists, activity = check_entry_exists(kwargs.get('program')) |
| 92 | if activity_does_not_exists: |
| 93 | current_activity = frappe.get_doc({ |
scmmishra | 31cbe3c | 2018-10-15 16:04:14 +0530 | [diff] [blame] | 94 | "doctype": "Course Activity", |
scmmishra | 17294e7 | 2018-10-15 11:45:17 +0530 | [diff] [blame] | 95 | "student_id": get_student_id(frappe.session.user), |
scmmishra | 4ae11f4 | 2018-10-12 15:18:26 +0530 | [diff] [blame] | 96 | "program_name": kwargs.get('program'), |
| 97 | "lms_activity": [{ |
| 98 | "course_name": kwargs.get('course'), |
| 99 | "content_name": kwargs.get('content'), |
| 100 | "status": "Completed" |
| 101 | }] |
| 102 | }) |
| 103 | if content_type == "Quiz": |
| 104 | activity = current_activity.lms_activity[-1] |
| 105 | activity.quiz_score = kwargs.get('score') |
| 106 | activity.selected_options = ", ".join(kwargs.get('selected_options')) |
| 107 | activity.result = ", ".join([str(item) for item in kwargs.get('result')]), |
| 108 | activity.status = "Passed" |
| 109 | current_activity.save() |
| 110 | frappe.db.commit() |
| 111 | else: |
| 112 | if content_type in ("Article", "Video"): |
| 113 | lms_activity_list = [[data.course_name, data.content_name] for data in activity.lms_activity] |
| 114 | if not [kwargs.get('course'), kwargs.get('content')] in lms_activity_list: |
| 115 | activity.append("lms_activity", { |
| 116 | "course_name": kwargs.get('course'), |
| 117 | "content_name": kwargs.get('content'), |
| 118 | "status": "Completed" |
| 119 | }) |
| 120 | else: |
| 121 | activity.append("lms_activity", { |
| 122 | "course_name": kwargs.get('course'), |
| 123 | "content_name": kwargs.get('content'), |
| 124 | "status": "Passed", |
| 125 | "quiz_score": kwargs.get('score'), |
| 126 | "selected_options": ", ".join(kwargs.get('selected_options')), |
| 127 | "result": ", ".join([str(item) for item in kwargs.get('result')]) |
| 128 | }) |
| 129 | activity.save() |
| 130 | frappe.db.commit() |
| 131 | |
| 132 | def check_entry_exists(program): |
| 133 | try: |
scmmishra | 31cbe3c | 2018-10-15 16:04:14 +0530 | [diff] [blame] | 134 | activity_name = frappe.get_all("Course Activity", filters={"student_id": get_student_id(frappe.session.user), "program_name": program})[0] |
scmmishra | 4ae11f4 | 2018-10-12 15:18:26 +0530 | [diff] [blame] | 135 | except IndexError: |
| 136 | return True, None |
| 137 | else: |
scmmishra | 31cbe3c | 2018-10-15 16:04:14 +0530 | [diff] [blame] | 138 | return None, frappe.get_doc("Course Activity", activity_name) |
scmmishra | 17294e7 | 2018-10-15 11:45:17 +0530 | [diff] [blame] | 139 | |
scmmishra | fa2db3c | 2018-10-16 16:46:27 +0530 | [diff] [blame] | 140 | def get_contents_in_course(course_name): |
| 141 | try: |
| 142 | course_doc = frappe.get_doc("Course", {"name":course_name, "is_published": True}) |
| 143 | return [frappe.get_doc("Content", content.content) for content in course_doc.get_all_children()] |
| 144 | except frappe.DoesNotExistError: |
| 145 | return None |
| 146 | |
| 147 | def get_courses_in_program(program): |
| 148 | try: |
| 149 | program_doc = frappe.get_doc("Program", program) |
| 150 | if program_doc.is_published: |
| 151 | course_list = [frappe.get_doc("Course", course.course_name) for course in program_doc.get_all_children()] |
| 152 | return [course for course in course_list if course.is_published == True] |
| 153 | else: |
| 154 | return None |
| 155 | except frappe.DoesNotExistError: |
| 156 | return None |
| 157 | |
| 158 | def get_program(): |
| 159 | program_list = frappe.get_list("Program", filters={"is_published": is_published}) |
| 160 | if program_list: |
| 161 | return program_list |
| 162 | else: |
| 163 | return None |
| 164 | |
scmmishra | 3757a4e | 2018-10-16 17:54:18 +0530 | [diff] [blame] | 165 | def get_featured_programs(): |
| 166 | featured_programs_name = frappe.get_list("Program", filters={"is_published": True, "is_featured": True}) |
| 167 | featured_list = [frappe.get_doc("Program", program["name"]) for program in featured_programs_name] |
scmmishra | fa2db3c | 2018-10-16 16:46:27 +0530 | [diff] [blame] | 168 | if featured_list: |
| 169 | return featured_list |
| 170 | else: |
| 171 | return None |
| 172 | |
| 173 | @frappe.whitelist() |
| 174 | def add_course_enrollment(course, email): |
| 175 | student_id = get_student_id(email) |
| 176 | if not get_course_enrollment(course, email): |
| 177 | enrollment = frappe.get_doc({ |
| 178 | "doctype": "Course Enrollment", |
| 179 | "student": student_id, |
| 180 | "course": course |
| 181 | }) |
| 182 | enrollment.save() |
| 183 | frappe.db.commit() |
| 184 | return enrollment |
| 185 | |
| 186 | def get_course_enrollment(course, email): |
| 187 | student_id = get_student_id(email) |
| 188 | try: |
| 189 | return frappe.get_list("Course Enrollment", filters={'course':course, 'student':student_id})[0] |
| 190 | except IndexError: |
| 191 | return None |
| 192 | |
scmmishra | 17294e7 | 2018-10-15 11:45:17 +0530 | [diff] [blame] | 193 | def get_student_id(email): |
| 194 | """Returns Student ID, example EDU-STU-2018-00001 from email address |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 195 | |
scmmishra | 17294e7 | 2018-10-15 11:45:17 +0530 | [diff] [blame] | 196 | :params email: email address of the student""" |
| 197 | try: |
scmmishra | fa2db3c | 2018-10-16 16:46:27 +0530 | [diff] [blame] | 198 | return frappe.get_list('Student', filters={'student_email_id': email})[0].name |
scmmishra | 17294e7 | 2018-10-15 11:45:17 +0530 | [diff] [blame] | 199 | except IndexError: |
scmmishra | 31cbe3c | 2018-10-15 16:04:14 +0530 | [diff] [blame] | 200 | frappe.throw("Student Account with email:{0} does not exist".format(email)) |
| 201 | |
| 202 | def get_quiz(content): |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 203 | try: |
| 204 | quiz_doc = frappe.get_doc("Content", content) |
| 205 | if quiz_doc.content_type != "Quiz": |
| 206 | frappe.throw("<b>{0}</b> is not a Quiz".format(content)) |
| 207 | quiz = [frappe.get_doc("Question", item.question_link) for item in quiz_doc.questions] |
| 208 | return quiz |
| 209 | except frappe.DoesNotExistError: |
| 210 | frappe.throw("The quiz \"{0}\" does not exist".format(content)) |
| 211 | |
| 212 | def get_quiz_as_dict(content): |
scmmishra | 31cbe3c | 2018-10-15 16:04:14 +0530 | [diff] [blame] | 213 | """Helper Function to get questions for a quiz |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 214 | |
scmmishra | 31cbe3c | 2018-10-15 16:04:14 +0530 | [diff] [blame] | 215 | :params content: name of a Content doctype with content_type quiz""" |
| 216 | try: |
| 217 | quiz_doc = frappe.get_doc("Content", content) |
scmmishra | 82df437 | 2018-10-16 13:04:40 +0530 | [diff] [blame] | 218 | if quiz_doc.content_type != "Quiz": |
scmmishra | 31cbe3c | 2018-10-15 16:04:14 +0530 | [diff] [blame] | 219 | frappe.throw("<b>{0}</b> is not a Quiz".format(content)) |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 220 | |
scmmishra | 82df437 | 2018-10-16 13:04:40 +0530 | [diff] [blame] | 221 | import json |
| 222 | quiz = [frappe.get_doc("Question", item.question_link) for item in quiz_doc.questions] |
| 223 | data = [] |
| 224 | for question in quiz: |
| 225 | d = {} |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 226 | d['id'] = question.name |
| 227 | d['question'] = question.question |
| 228 | d['options'] = [item.option for item in quiz[0].options] |
scmmishra | 82df437 | 2018-10-16 13:04:40 +0530 | [diff] [blame] | 229 | data.append(d) |
| 230 | return data |
scmmishra | 31cbe3c | 2018-10-15 16:04:14 +0530 | [diff] [blame] | 231 | except frappe.DoesNotExistError: |
scmmishra | 82df437 | 2018-10-16 13:04:40 +0530 | [diff] [blame] | 232 | frappe.throw("The quiz \"{0}\" does not exist".format(content)) |