Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies and contributors |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 2 | |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 3 | import frappe |
| 4 | from frappe import _ |
| 5 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 7 | class OverlapError(frappe.ValidationError): |
| 8 | pass |
| 9 | |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 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: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 19 | frappe.throw( |
| 20 | _("This {0} conflicts with {1} for {2} {3}").format( |
| 21 | doc.doctype, |
| 22 | existing.name, |
| 23 | doc.meta.get_label(fieldname) if not value else fieldname, |
| 24 | value or doc.get(fieldname), |
| 25 | ), |
| 26 | OverlapError, |
| 27 | ) |
| 28 | |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 29 | |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 30 | def get_overlap_for(doc, doctype, fieldname, value=None): |
Manas Solanki | 54c4240 | 2017-04-12 19:24:12 +0530 | [diff] [blame] | 31 | """Returns overlaping document for specified field. |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 32 | |
| 33 | :param fieldname: Checks Overlap for this field |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 34 | """ |
| 35 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 36 | existing = frappe.db.sql( |
| 37 | """select name, from_time, to_time from `tab{0}` |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 38 | where `{1}`=%(val)s and schedule_date = %(schedule_date)s and |
| 39 | ( |
| 40 | (from_time > %(from_time)s and from_time < %(to_time)s) or |
| 41 | (to_time > %(from_time)s and to_time < %(to_time)s) or |
| 42 | (%(from_time)s > from_time and %(from_time)s < to_time) or |
| 43 | (%(from_time)s = from_time and %(to_time)s = to_time)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 44 | and name!=%(name)s and docstatus!=2""".format( |
| 45 | doctype, fieldname |
| 46 | ), |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 47 | { |
| 48 | "schedule_date": doc.schedule_date, |
| 49 | "val": value or doc.get(fieldname), |
| 50 | "from_time": doc.from_time, |
| 51 | "to_time": doc.to_time, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 52 | "name": doc.name or "No Name", |
| 53 | }, |
| 54 | as_dict=True, |
| 55 | ) |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 56 | |
| 57 | return existing[0] if existing else None |
scmmishra | 685584b | 2018-10-17 12:41:50 +0530 | [diff] [blame] | 58 | |
scmmishra | 7409fe6 | 2018-10-22 18:37:03 +0530 | [diff] [blame] | 59 | |
Neil Trini Lasrado | f521a9c | 2016-07-22 01:28:41 +0530 | [diff] [blame] | 60 | def validate_duplicate_student(students): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 61 | unique_students = [] |
Neil Trini Lasrado | f521a9c | 2016-07-22 01:28:41 +0530 | [diff] [blame] | 62 | for stud in students: |
| 63 | if stud.student in unique_students: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 64 | frappe.throw( |
| 65 | _("Student {0} - {1} appears Multiple times in row {2} & {3}").format( |
| 66 | stud.student, stud.student_name, unique_students.index(stud.student) + 1, stud.idx |
| 67 | ) |
| 68 | ) |
Neil Trini Lasrado | f521a9c | 2016-07-22 01:28:41 +0530 | [diff] [blame] | 69 | else: |
| 70 | unique_students.append(stud.student) |
scmmishra | 4ae11f4 | 2018-10-12 15:18:26 +0530 | [diff] [blame] | 71 | |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 72 | return None |
| 73 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 74 | |
scmmishra | 1192529 | 2018-11-20 17:38:01 +0530 | [diff] [blame] | 75 | # LMS Utils |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 76 | def get_current_student(): |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 77 | """Returns current student from frappe.session.user |
| 78 | |
| 79 | Returns: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | object: Student Document |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 81 | """ |
| 82 | email = frappe.session.user |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 83 | if email in ("Administrator", "Guest"): |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 84 | return None |
| 85 | try: |
scmmishra | da39da6 | 2018-12-13 11:51:31 +0530 | [diff] [blame] | 86 | student_id = frappe.get_all("Student", {"student_email_id": email}, ["name"])[0].name |
scmmishra | 327334a | 2019-04-22 12:03:17 +0530 | [diff] [blame] | 87 | return frappe.get_doc("Student", student_id) |
| 88 | except (IndexError, frappe.DoesNotExistError): |
scmmishra | 66d2392 | 2019-04-22 12:54:43 +0530 | [diff] [blame] | 89 | return None |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 90 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 91 | |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 92 | def get_portal_programs(): |
| 93 | """Returns a list of all program to be displayed on the portal |
| 94 | Programs are returned based on the following logic |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 95 | is_published and (student_is_enrolled or student_can_self_enroll) |
scmmishra | 000e706 | 2019-03-19 12:30:43 +0530 | [diff] [blame] | 96 | |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 97 | Returns: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 98 | list of dictionary: List of all programs and to be displayed on the portal along with access rights |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 99 | """ |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 100 | published_programs = frappe.get_all("Program", filters={"is_published": True}) |
| 101 | if not published_programs: |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 102 | return None |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 103 | |
| 104 | program_list = [frappe.get_doc("Program", program) for program in published_programs] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 105 | portal_programs = [ |
| 106 | {"program": program, "has_access": allowed_program_access(program.name)} |
| 107 | for program in program_list |
| 108 | if allowed_program_access(program.name) or program.allow_self_enroll |
| 109 | ] |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 110 | |
| 111 | return portal_programs |
| 112 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 113 | |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 114 | def allowed_program_access(program, student=None): |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 115 | """Returns enrollment status for current student |
| 116 | |
| 117 | Args: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 118 | program (string): Name of the program |
| 119 | student (object): instance of Student document |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 120 | |
| 121 | Returns: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 122 | bool: Is current user enrolled or not |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 123 | """ |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 124 | if has_super_access(): |
| 125 | return True |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 126 | if not student: |
| 127 | student = get_current_student() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 128 | if student and get_enrollment("program", program, student.name): |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 129 | return True |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 130 | else: |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 131 | return False |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 132 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 133 | |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 134 | def get_enrollment(master, document, student): |
| 135 | """Gets enrollment for course or program |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 136 | |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 137 | Args: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 138 | master (string): can either be program or course |
| 139 | document (string): program or course name |
| 140 | student (string): Student ID |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 141 | |
| 142 | Returns: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 143 | string: Enrollment Name if exists else returns empty string |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 144 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 145 | if master == "program": |
| 146 | enrollments = frappe.get_all( |
| 147 | "Program Enrollment", filters={"student": student, "program": document, "docstatus": 1} |
| 148 | ) |
| 149 | if master == "course": |
| 150 | enrollments = frappe.get_all( |
| 151 | "Course Enrollment", filters={"student": student, "course": document} |
| 152 | ) |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 153 | |
| 154 | if enrollments: |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 155 | return enrollments[0].name |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 156 | else: |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 157 | return None |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 158 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 160 | @frappe.whitelist() |
| 161 | def enroll_in_program(program_name, student=None): |
| 162 | """Enroll student in program |
| 163 | |
| 164 | Args: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 165 | program_name (string): Name of the program to be enrolled into |
| 166 | student (string, optional): name of student who has to be enrolled, if not |
| 167 | provided, a student will be created from the current user |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 168 | |
| 169 | Returns: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 170 | string: name of the program enrollment document |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 171 | """ |
| 172 | if has_super_access(): |
| 173 | return |
| 174 | |
| 175 | if not student == None: |
| 176 | student = frappe.get_doc("Student", student) |
| 177 | else: |
| 178 | # Check if self enrollment in allowed |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 179 | program = frappe.get_doc("Program", program_name) |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 180 | if not program.allow_self_enroll: |
Anurag Mishra | 841d852 | 2019-07-03 15:15:08 +0530 | [diff] [blame] | 181 | return frappe.throw(_("You are not allowed to enroll for this course")) |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 182 | |
| 183 | student = get_current_student() |
| 184 | if not student: |
| 185 | student = create_student_from_current_user() |
| 186 | |
| 187 | # Check if student is already enrolled in program |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 188 | enrollment = get_enrollment("program", program_name, student.name) |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 189 | if enrollment: |
| 190 | return enrollment |
| 191 | |
| 192 | # Check if self enrollment in allowed |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 193 | program = frappe.get_doc("Program", program_name) |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 194 | if not program.allow_self_enroll: |
Anurag Mishra | 841d852 | 2019-07-03 15:15:08 +0530 | [diff] [blame] | 195 | return frappe.throw(_("You are not allowed to enroll for this course")) |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 196 | |
| 197 | # Enroll in program |
| 198 | program_enrollment = student.enroll_in_program(program_name) |
| 199 | return program_enrollment.name |
| 200 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 201 | |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 202 | def has_super_access(): |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 203 | """Check if user has a role that allows full access to LMS |
| 204 | |
| 205 | Returns: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 206 | bool: true if user has access to all lms content |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 207 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 208 | current_user = frappe.get_doc("User", frappe.session.user) |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 209 | roles = set([role.role for role in current_user.roles]) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 210 | return bool( |
| 211 | roles & {"Administrator", "Instructor", "Education Manager", "System Manager", "Academic User"} |
| 212 | ) |
| 213 | |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 214 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 215 | @frappe.whitelist() |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 216 | def add_activity(course, content_type, content, program): |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 217 | if has_super_access(): |
| 218 | return None |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 219 | |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 220 | student = get_current_student() |
| 221 | if not student: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 222 | return frappe.throw( |
| 223 | _("Student with email {0} does not exist").format(frappe.session.user), frappe.DoesNotExistError |
| 224 | ) |
Shivam Mishra | f927502 | 2019-05-29 18:39:52 +0530 | [diff] [blame] | 225 | |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 226 | enrollment = get_or_create_course_enrollment(course, program) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 227 | if content_type == "Quiz": |
Shivam Mishra | e94e9d2 | 2019-05-30 18:05:00 +0530 | [diff] [blame] | 228 | return |
| 229 | else: |
| 230 | return enrollment.add_activity(content_type, content) |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 231 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 232 | |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 233 | @frappe.whitelist() |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 234 | def evaluate_quiz(quiz_response, quiz_name, course, program, time_taken): |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 235 | import json |
| 236 | |
| 237 | student = get_current_student() |
| 238 | |
| 239 | quiz_response = json.loads(quiz_response) |
| 240 | quiz = frappe.get_doc("Quiz", quiz_name) |
| 241 | result, score, status = quiz.evaluate(quiz_response, quiz_name) |
| 242 | |
| 243 | if has_super_access(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 244 | return {"result": result, "score": score, "status": status} |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 245 | |
| 246 | if student: |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 247 | enrollment = get_or_create_course_enrollment(course, program) |
| 248 | if quiz.allowed_attempt(enrollment, quiz_name): |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 249 | enrollment.add_quiz_activity(quiz_name, quiz_response, result, score, status, time_taken) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 250 | return {"result": result, "score": score, "status": status} |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 251 | else: |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 252 | return None |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 253 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 254 | |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 255 | @frappe.whitelist() |
| 256 | def get_quiz(quiz_name, course): |
| 257 | try: |
| 258 | quiz = frappe.get_doc("Quiz", quiz_name) |
| 259 | questions = quiz.get_questions() |
Ankush Menat | 694ae81 | 2021-09-01 14:40:56 +0530 | [diff] [blame] | 260 | except Exception: |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 261 | frappe.throw(_("Quiz {0} does not exist").format(quiz_name), frappe.DoesNotExistError) |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 262 | return None |
| 263 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 264 | questions = [ |
| 265 | { |
| 266 | "name": question.name, |
| 267 | "question": question.question, |
| 268 | "type": question.question_type, |
| 269 | "options": [{"name": option.name, "option": option.option} for option in question.options], |
| 270 | } |
| 271 | for question in questions |
| 272 | ] |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 273 | |
| 274 | if has_super_access(): |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 275 | return { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 276 | "questions": questions, |
| 277 | "activity": None, |
| 278 | "is_time_bound": quiz.is_time_bound, |
| 279 | "duration": quiz.duration, |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 280 | } |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 281 | |
| 282 | student = get_current_student() |
| 283 | course_enrollment = get_enrollment("course", course, student.name) |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 284 | status, score, result, time_taken = check_quiz_completion(quiz, course_enrollment) |
| 285 | return { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 286 | "questions": questions, |
| 287 | "activity": {"is_complete": status, "score": score, "result": result, "time_taken": time_taken}, |
| 288 | "is_time_bound": quiz.is_time_bound, |
| 289 | "duration": quiz.duration, |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 290 | } |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 291 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 292 | |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 293 | def get_topic_progress(topic, course_name, program): |
Shivam Mishra | 6d4c666 | 2019-06-03 14:41:05 +0530 | [diff] [blame] | 294 | """ |
| 295 | Return the porgress of a course in a program as well as the content to continue from. |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 296 | :param topic_name: |
| 297 | :param course_name: |
Shivam Mishra | 6d4c666 | 2019-06-03 14:41:05 +0530 | [diff] [blame] | 298 | """ |
| 299 | student = get_current_student() |
Shivam Mishra | 6593263 | 2019-06-05 13:29:51 +0530 | [diff] [blame] | 300 | if not student: |
| 301 | return None |
Shivam Mishra | 6d4c666 | 2019-06-03 14:41:05 +0530 | [diff] [blame] | 302 | course_enrollment = get_or_create_course_enrollment(course_name, program) |
| 303 | progress = student.get_topic_progress(course_enrollment.name, topic) |
| 304 | if not progress: |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 305 | return None |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 306 | count = sum([activity["is_complete"] for activity in progress]) |
Shivam Mishra | 6d4c666 | 2019-06-03 14:41:05 +0530 | [diff] [blame] | 307 | if count == 0: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 308 | return {"completed": False, "started": False} |
Shivam Mishra | 6d4c666 | 2019-06-03 14:41:05 +0530 | [diff] [blame] | 309 | elif count == len(progress): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 310 | return {"completed": True, "started": True} |
Shivam Mishra | 6d4c666 | 2019-06-03 14:41:05 +0530 | [diff] [blame] | 311 | elif count < len(progress): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 312 | return {"completed": False, "started": True} |
| 313 | |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 314 | |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 315 | def get_course_progress(course, program): |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 316 | """ |
| 317 | Return the porgress of a course in a program as well as the content to continue from. |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 318 | :param topic_name: |
| 319 | :param course_name: |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 320 | """ |
| 321 | course_progress = [] |
| 322 | for course_topic in course.topics: |
| 323 | topic = frappe.get_doc("Topic", course_topic.topic) |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 324 | progress = get_topic_progress(topic, course.name, program) |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 325 | if progress: |
| 326 | course_progress.append(progress) |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 327 | if course_progress: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 328 | number_of_completed_topics = sum([activity["completed"] for activity in course_progress]) |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 329 | total_topics = len(course_progress) |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 330 | if total_topics == 1: |
| 331 | return course_progress[0] |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 332 | if number_of_completed_topics == 0: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 333 | return {"completed": False, "started": False} |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 334 | if number_of_completed_topics == total_topics: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 335 | return {"completed": True, "started": True} |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 336 | if number_of_completed_topics < total_topics: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 337 | return {"completed": False, "started": True} |
Shivam Mishra | 570161b | 2019-06-05 13:08:53 +0530 | [diff] [blame] | 338 | |
| 339 | return None |
Shivam Mishra | 6d4c666 | 2019-06-03 14:41:05 +0530 | [diff] [blame] | 340 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 341 | |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 342 | def get_program_progress(program): |
| 343 | program_progress = [] |
| 344 | if not program.courses: |
| 345 | return None |
| 346 | for program_course in program.courses: |
| 347 | course = frappe.get_doc("Course", program_course.course) |
| 348 | progress = get_course_progress(course, program.name) |
| 349 | if progress: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 350 | progress["name"] = course.name |
| 351 | progress["course"] = course.course_name |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 352 | program_progress.append(progress) |
| 353 | |
| 354 | if program_progress: |
| 355 | return program_progress |
| 356 | |
| 357 | return None |
| 358 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 359 | |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 360 | def get_program_completion(program): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 361 | topics = frappe.db.sql( |
| 362 | """select `tabCourse Topic`.topic, `tabCourse Topic`.parent |
Shivam Mishra | d49b5e4 | 2019-06-06 17:19:53 +0530 | [diff] [blame] | 363 | from `tabCourse Topic`, |
| 364 | `tabProgram Course` |
| 365 | where `tabCourse Topic`.parent = `tabProgram Course`.course |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 366 | and `tabProgram Course`.parent = %s""", |
| 367 | program.name, |
| 368 | ) |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 369 | |
| 370 | progress = [] |
| 371 | for topic in topics: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 372 | topic_doc = frappe.get_doc("Topic", topic[0]) |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 373 | topic_progress = get_topic_progress(topic_doc, topic[1], program.name) |
| 374 | if topic_progress: |
| 375 | progress.append(topic_progress) |
| 376 | |
| 377 | if progress: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 378 | number_of_completed_topics = sum([activity["completed"] for activity in progress if activity]) |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 379 | total_topics = len(progress) |
| 380 | try: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 381 | return int((float(number_of_completed_topics) / total_topics) * 100) |
Shivam Mishra | 16b4129 | 2019-06-05 17:29:48 +0530 | [diff] [blame] | 382 | except ZeroDivisionError: |
| 383 | return 0 |
| 384 | |
| 385 | return 0 |
| 386 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 387 | |
scmmishra | bf9a10f | 2019-03-06 15:45:35 +0530 | [diff] [blame] | 388 | def create_student_from_current_user(): |
scmmishra | 082e3c9 | 2018-11-23 17:16:22 +0530 | [diff] [blame] | 389 | user = frappe.get_doc("User", frappe.session.user) |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 390 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 391 | student = frappe.get_doc( |
| 392 | { |
| 393 | "doctype": "Student", |
| 394 | "first_name": user.first_name, |
| 395 | "last_name": user.last_name, |
| 396 | "student_email_id": user.email, |
| 397 | "user": frappe.session.user, |
| 398 | } |
| 399 | ) |
Shivam Mishra | dfdb92f | 2019-05-30 16:35:15 +0530 | [diff] [blame] | 400 | |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 401 | student.save(ignore_permissions=True) |
scmmishra | 97c994f | 2018-11-26 14:41:15 +0530 | [diff] [blame] | 402 | return student |
| 403 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 404 | |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 405 | def get_or_create_course_enrollment(course, program): |
scmmishra | 66d2392 | 2019-04-22 12:54:43 +0530 | [diff] [blame] | 406 | student = get_current_student() |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 407 | course_enrollment = get_enrollment("course", course, student.name) |
| 408 | if not course_enrollment: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 409 | program_enrollment = get_enrollment("program", program.name, student.name) |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 410 | if not program_enrollment: |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 411 | frappe.throw(_("You are not enrolled in program {0}").format(program)) |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 412 | return |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 413 | return student.enroll_in_course( |
| 414 | course_name=course, program_enrollment=get_enrollment("program", program.name, student.name) |
| 415 | ) |
Shivam Mishra | 8ddb63a | 2019-06-03 14:40:52 +0530 | [diff] [blame] | 416 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 417 | return frappe.get_doc("Course Enrollment", course_enrollment) |
| 418 | |
scmmishra | babb68d | 2018-11-19 16:13:21 +0530 | [diff] [blame] | 419 | |
scmmishra | 766f68a | 2018-12-12 16:14:36 +0530 | [diff] [blame] | 420 | def check_content_completion(content_name, content_type, enrollment_name): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 421 | activity = frappe.get_all( |
| 422 | "Course Activity", |
| 423 | filters={"enrollment": enrollment_name, "content_type": content_type, "content": content_name}, |
| 424 | ) |
scmmishra | 766f68a | 2018-12-12 16:14:36 +0530 | [diff] [blame] | 425 | if activity: |
| 426 | return True |
| 427 | else: |
| 428 | return False |
| 429 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 430 | |
scmmishra | 766f68a | 2018-12-12 16:14:36 +0530 | [diff] [blame] | 431 | def check_quiz_completion(quiz, enrollment_name): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 432 | attempts = frappe.get_all( |
| 433 | "Quiz Activity", |
| 434 | filters={"enrollment": enrollment_name, "quiz": quiz.name}, |
| 435 | fields=["name", "activity_date", "score", "status", "time_taken"], |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 436 | ) |
Shivam Mishra | d1a2521 | 2019-06-03 12:57:38 +0530 | [diff] [blame] | 437 | status = False if quiz.max_attempts == 0 else bool(len(attempts) >= quiz.max_attempts) |
scmmishra | 766f68a | 2018-12-12 16:14:36 +0530 | [diff] [blame] | 438 | score = None |
| 439 | result = None |
Jannat Patel | dcdd3be | 2021-04-19 10:36:40 +0530 | [diff] [blame] | 440 | time_taken = None |
scmmishra | 766f68a | 2018-12-12 16:14:36 +0530 | [diff] [blame] | 441 | if attempts: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 442 | if quiz.grading_basis == "Last Highest Score": |
| 443 | attempts = sorted(attempts, key=lambda i: int(i.score), reverse=True) |
| 444 | score = attempts[0]["score"] |
| 445 | result = attempts[0]["status"] |
| 446 | time_taken = attempts[0]["time_taken"] |
| 447 | if result == "Pass": |
scmmishra | 766f68a | 2018-12-12 16:14:36 +0530 | [diff] [blame] | 448 | status = True |
pateljannat | bbf07d9 | 2021-06-08 17:05:44 +0530 | [diff] [blame] | 449 | return status, score, result, time_taken |