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 license information, please see license.txt |
| 4 | |
| 5 | from __future__ import unicode_literals |
| 6 | import frappe |
| 7 | import json |
| 8 | from frappe import _ |
| 9 | from frappe.model.mapper import get_mapped_doc |
Neil Trini Lasrado | 2a9e0f6 | 2017-01-09 18:25:43 +0530 | [diff] [blame] | 10 | from frappe.utils import flt, cstr |
Neil Trini Lasrado | fa2023d | 2017-02-13 19:26:16 +0530 | [diff] [blame] | 11 | from frappe.email.doctype.email_group.email_group import add_subscribers |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 12 | |
Manas Solanki | f60bd60 | 2017-06-01 18:30:35 +0530 | [diff] [blame] | 13 | def get_course(program): |
| 14 | '''Return list of courses for a particular program |
| 15 | :param program: Program |
| 16 | ''' |
| 17 | courses = frappe.db.sql('''select course, course_name from `tabProgram Course` where parent=%s''', |
| 18 | (program), as_dict=1) |
| 19 | return courses |
| 20 | |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 21 | @frappe.whitelist() |
| 22 | def enroll_student(source_name): |
| 23 | """Creates a Student Record and returns a Program Enrollment. |
| 24 | |
| 25 | :param source_name: Student Applicant. |
| 26 | """ |
Manas Solanki | ad2dc2c | 2017-03-15 17:45:02 +0530 | [diff] [blame] | 27 | frappe.publish_realtime('enroll_student_progress', {"progress": [1, 4]}, user=frappe.session.user) |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 28 | student = get_mapped_doc("Student Applicant", source_name, |
| 29 | {"Student Applicant": { |
| 30 | "doctype": "Student", |
| 31 | "field_map": { |
| 32 | "name": "student_applicant" |
| 33 | } |
Neil Trini Lasrado | 0672459 | 2016-08-22 12:57:09 +0530 | [diff] [blame] | 34 | }}, ignore_permissions=True) |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 35 | student.save() |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 36 | program_enrollment = frappe.new_doc("Program Enrollment") |
| 37 | program_enrollment.student = student.name |
| 38 | program_enrollment.student_name = student.title |
| 39 | program_enrollment.program = frappe.db.get_value("Student Applicant", source_name, "program") |
Manas Solanki | ad2dc2c | 2017-03-15 17:45:02 +0530 | [diff] [blame] | 40 | frappe.publish_realtime('enroll_student_progress', {"progress": [4, 4]}, user=frappe.session.user) |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 41 | return program_enrollment |
| 42 | |
| 43 | @frappe.whitelist() |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 44 | def check_attendance_records_exist(course_schedule=None, student_group=None, date=None): |
| 45 | """Check if Attendance Records are made against the specified Course Schedule or Student Group for given date. |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 46 | |
| 47 | :param course_schedule: Course Schedule. |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 48 | :param student_group: Student Group. |
Neil Trini Lasrado | d9bc559 | 2016-11-17 15:35:54 +0530 | [diff] [blame] | 49 | :param date: Date. |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 50 | """ |
Neil Trini Lasrado | d9bc559 | 2016-11-17 15:35:54 +0530 | [diff] [blame] | 51 | if course_schedule: |
| 52 | return frappe.get_list("Student Attendance", filters={"course_schedule": course_schedule}) |
| 53 | else: |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 54 | return frappe.get_list("Student Attendance", filters={"student_group": student_group, "date": date}) |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 55 | |
| 56 | @frappe.whitelist() |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 57 | def mark_attendance(students_present, students_absent, course_schedule=None, student_group=None, date=None): |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 58 | """Creates Multiple Attendance Records. |
| 59 | |
| 60 | :param students_present: Students Present JSON. |
| 61 | :param students_absent: Students Absent JSON. |
| 62 | :param course_schedule: Course Schedule. |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 63 | :param student_group: Student Group. |
Neil Trini Lasrado | d9bc559 | 2016-11-17 15:35:54 +0530 | [diff] [blame] | 64 | :param date: Date. |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 65 | """ |
Neil Trini Lasrado | eccbf43 | 2016-12-09 17:38:59 +0530 | [diff] [blame] | 66 | |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 67 | present = json.loads(students_present) |
| 68 | absent = json.loads(students_absent) |
Neil Trini Lasrado | eccbf43 | 2016-12-09 17:38:59 +0530 | [diff] [blame] | 69 | |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 70 | for d in present: |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 71 | make_attendance_records(d["student"], d["student_name"], "Present", course_schedule, student_group, date) |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 72 | |
| 73 | for d in absent: |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 74 | make_attendance_records(d["student"], d["student_name"], "Absent", course_schedule, student_group, date) |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 75 | |
Neil Trini Lasrado | 6841b31 | 2016-12-01 18:40:27 +0530 | [diff] [blame] | 76 | frappe.db.commit() |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 77 | frappe.msgprint(_("Attendance has been marked successfully.")) |
| 78 | |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 79 | def make_attendance_records(student, student_name, status, course_schedule=None, student_group=None, date=None): |
Manas Solanki | f869a67 | 2017-02-21 11:34:30 +0530 | [diff] [blame] | 80 | """Creates/Update Attendance Record. |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 81 | |
| 82 | :param student: Student. |
| 83 | :param student_name: Student Name. |
| 84 | :param course_schedule: Course Schedule. |
| 85 | :param status: Status (Present/Absent) |
| 86 | """ |
Nabin Hait | 08847bd | 2017-02-22 13:05:46 +0530 | [diff] [blame] | 87 | student_attendance_list = frappe.get_list("Student Attendance", fields = ['name'], filters = { |
Manas Solanki | f869a67 | 2017-02-21 11:34:30 +0530 | [diff] [blame] | 88 | "student": student, |
| 89 | "course_schedule": course_schedule, |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 90 | "student_group": student_group, |
Manas Solanki | f869a67 | 2017-02-21 11:34:30 +0530 | [diff] [blame] | 91 | "date": date |
| 92 | }) |
| 93 | |
Nabin Hait | 08847bd | 2017-02-22 13:05:46 +0530 | [diff] [blame] | 94 | if student_attendance_list: |
| 95 | student_attendance = frappe.get_doc("Student Attendance", student_attendance_list[0]) |
Manas Solanki | f869a67 | 2017-02-21 11:34:30 +0530 | [diff] [blame] | 96 | else: |
| 97 | student_attendance = frappe.new_doc("Student Attendance") |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 98 | student_attendance.student = student |
| 99 | student_attendance.student_name = student_name |
| 100 | student_attendance.course_schedule = course_schedule |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 101 | student_attendance.student_group = student_group |
Neil Trini Lasrado | d9bc559 | 2016-11-17 15:35:54 +0530 | [diff] [blame] | 102 | student_attendance.date = date |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 103 | student_attendance.status = status |
Manas Solanki | f869a67 | 2017-02-21 11:34:30 +0530 | [diff] [blame] | 104 | student_attendance.save() |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 105 | |
| 106 | @frappe.whitelist() |
Neil Trini Lasrado | fa2023d | 2017-02-13 19:26:16 +0530 | [diff] [blame] | 107 | def get_student_guardians(student): |
| 108 | """Returns List of Guardians of a Student. |
| 109 | |
| 110 | :param student: Student. |
| 111 | """ |
| 112 | guardians = frappe.get_list("Student Guardian", fields=["guardian"] , |
| 113 | filters={"parent": student}) |
| 114 | return guardians |
| 115 | |
| 116 | @frappe.whitelist() |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 117 | def get_student_group_students(student_group): |
| 118 | """Returns List of student, student_name in Student Group. |
| 119 | |
| 120 | :param student_group: Student Group. |
| 121 | """ |
Neil Trini Lasrado | 16d40cf | 2017-01-20 19:16:39 +0530 | [diff] [blame] | 122 | students = frappe.get_list("Student Group Student", fields=["student", "student_name"] , |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 123 | filters={"parent": student_group, "active": 1}, order_by= "group_roll_number") |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 124 | return students |
| 125 | |
| 126 | @frappe.whitelist() |
| 127 | def get_fee_structure(program, academic_term=None): |
| 128 | """Returns Fee Structure. |
| 129 | |
| 130 | :param program: Program. |
| 131 | :param academic_term: Academic Term. |
| 132 | """ |
| 133 | fee_structure = frappe.db.get_values("Fee Structure", {"program": program, |
| 134 | "academic_term": academic_term}, 'name', as_dict=True) |
| 135 | return fee_structure[0].name if fee_structure else None |
| 136 | |
| 137 | @frappe.whitelist() |
Neil Trini Lasrado | f02a0d4 | 2016-08-05 15:50:52 +0530 | [diff] [blame] | 138 | def get_fee_components(fee_structure): |
| 139 | """Returns Fee Components. |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 140 | |
| 141 | :param fee_structure: Fee Structure. |
| 142 | """ |
| 143 | if fee_structure: |
Neil Trini Lasrado | f02a0d4 | 2016-08-05 15:50:52 +0530 | [diff] [blame] | 144 | fs = frappe.get_list("Fee Component", fields=["fees_category", "amount"] , filters={"parent": fee_structure}, order_by= "idx") |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 145 | return fs |
| 146 | |
| 147 | @frappe.whitelist() |
Neil Trini Lasrado | c20adb5 | 2016-09-05 16:55:54 +0530 | [diff] [blame] | 148 | def get_fee_schedule(program, student_category=None): |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 149 | """Returns Fee Schedule. |
| 150 | |
| 151 | :param program: Program. |
Neil Trini Lasrado | c20adb5 | 2016-09-05 16:55:54 +0530 | [diff] [blame] | 152 | :param student_category: Student Category |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 153 | """ |
| 154 | fs = frappe.get_list("Program Fee", fields=["academic_term", "fee_structure", "due_date", "amount"] , |
Neil Trini Lasrado | c20adb5 | 2016-09-05 16:55:54 +0530 | [diff] [blame] | 155 | filters={"parent": program, "student_category": student_category }, order_by= "idx") |
Neil Trini Lasrado | 3f0a581 | 2016-07-19 14:17:33 +0530 | [diff] [blame] | 156 | return fs |
| 157 | |
| 158 | @frappe.whitelist() |
| 159 | def collect_fees(fees, amt): |
| 160 | paid_amount = flt(amt) + flt(frappe.db.get_value("Fees", fees, "paid_amount")) |
| 161 | total_amount = flt(frappe.db.get_value("Fees", fees, "total_amount")) |
| 162 | frappe.db.set_value("Fees", fees, "paid_amount", paid_amount) |
| 163 | frappe.db.set_value("Fees", fees, "outstanding_amount", (total_amount - paid_amount)) |
| 164 | return paid_amount |
| 165 | |
| 166 | @frappe.whitelist() |
| 167 | def get_course_schedule_events(start, end, filters=None): |
| 168 | """Returns events for Course Schedule Calendar view rendering. |
| 169 | |
| 170 | :param start: Start date-time. |
| 171 | :param end: End date-time. |
| 172 | :param filters: Filters (JSON). |
| 173 | """ |
| 174 | from frappe.desk.calendar import get_event_conditions |
| 175 | conditions = get_event_conditions("Course Schedule", filters) |
| 176 | |
| 177 | data = frappe.db.sql("""select name, course, |
| 178 | timestamp(schedule_date, from_time) as from_datetime, |
| 179 | timestamp(schedule_date, to_time) as to_datetime, |
| 180 | room, student_group, 0 as 'allDay' |
| 181 | from `tabCourse Schedule` |
| 182 | where ( schedule_date between %(start)s and %(end)s ) |
| 183 | {conditions}""".format(conditions=conditions), { |
| 184 | "start": start, |
| 185 | "end": end |
| 186 | }, as_dict=True, update={"allDay": 0}) |
| 187 | |
| 188 | return data |
Neil Trini Lasrado | 06ed74d | 2016-12-14 17:49:47 +0530 | [diff] [blame] | 189 | |
| 190 | @frappe.whitelist() |
Neil Trini Lasrado | 749516c | 2017-02-01 18:40:30 +0530 | [diff] [blame] | 191 | def get_assessment_criteria(course): |
| 192 | """Returns Assessmemt Criteria and their Weightage from Course Master. |
Neil Trini Lasrado | 06ed74d | 2016-12-14 17:49:47 +0530 | [diff] [blame] | 193 | |
| 194 | :param Course: Course |
| 195 | """ |
Neil Trini Lasrado | 749516c | 2017-02-01 18:40:30 +0530 | [diff] [blame] | 196 | return frappe.get_list("Course Assessment Criteria", \ |
| 197 | fields=["assessment_criteria", "weightage"], filters={"parent": course}, order_by= "idx") |
Neil Trini Lasrado | 2a9e0f6 | 2017-01-09 18:25:43 +0530 | [diff] [blame] | 198 | |
| 199 | @frappe.whitelist() |
Manas Solanki | 426b8a1 | 2017-05-09 15:32:52 +0530 | [diff] [blame] | 200 | def get_assessment_students(assessment_plan, student_group): |
| 201 | |
| 202 | student_list = get_student_group_students(student_group) |
Neil Trini Lasrado | 2a9e0f6 | 2017-01-09 18:25:43 +0530 | [diff] [blame] | 203 | for i, student in enumerate(student_list): |
| 204 | result = get_result(student.student, assessment_plan) |
| 205 | if result: |
| 206 | student_result = {} |
| 207 | for d in result.details: |
Neil Trini Lasrado | 749516c | 2017-02-01 18:40:30 +0530 | [diff] [blame] | 208 | student_result.update({d.assessment_criteria: cstr(d.score) + " ("+ d.grade + ")"}) |
Neil Trini Lasrado | 2a9e0f6 | 2017-01-09 18:25:43 +0530 | [diff] [blame] | 209 | student_result.update({"total_score": cstr(result.total_score) + " (" + result.grade + ")"}) |
| 210 | student.update({'assessment_details': student_result}) |
| 211 | else: |
| 212 | student.update({'assessment_details': None}) |
| 213 | return student_list |
| 214 | |
Neil Trini Lasrado | 06ed74d | 2016-12-14 17:49:47 +0530 | [diff] [blame] | 215 | @frappe.whitelist() |
Neil Trini Lasrado | a56224c | 2017-01-04 17:00:43 +0530 | [diff] [blame] | 216 | def get_assessment_details(assessment_plan): |
Neil Trini Lasrado | 749516c | 2017-02-01 18:40:30 +0530 | [diff] [blame] | 217 | """Returns Assessment Criteria and Maximum Score from Assessment Plan Master. |
Neil Trini Lasrado | 06ed74d | 2016-12-14 17:49:47 +0530 | [diff] [blame] | 218 | |
Neil Trini Lasrado | a56224c | 2017-01-04 17:00:43 +0530 | [diff] [blame] | 219 | :param Assessment Plan: Assessment Plan |
Neil Trini Lasrado | 06ed74d | 2016-12-14 17:49:47 +0530 | [diff] [blame] | 220 | """ |
Neil Trini Lasrado | 749516c | 2017-02-01 18:40:30 +0530 | [diff] [blame] | 221 | return frappe.get_list("Assessment Plan Criteria", \ |
| 222 | fields=["assessment_criteria", "maximum_score"], filters={"parent": assessment_plan}, order_by= "idx") |
Neil Trini Lasrado | 06ed74d | 2016-12-14 17:49:47 +0530 | [diff] [blame] | 223 | |
Neil Trini Lasrado | 2a9e0f6 | 2017-01-09 18:25:43 +0530 | [diff] [blame] | 224 | @frappe.whitelist() |
| 225 | def get_result(student, assessment_plan): |
| 226 | """Returns Submitted Result of given student for specified Assessment Plan |
| 227 | |
| 228 | :param Student: Student |
| 229 | :param Assessment Plan: Assessment Plan |
| 230 | """ |
| 231 | results = frappe.get_all("Assessment Result", filters={"student": student, "assessment_plan": assessment_plan, "docstatus": 1}) |
| 232 | if results: |
| 233 | return frappe.get_doc("Assessment Result", results[0]) |
| 234 | else: |
| 235 | return None |
Neil Trini Lasrado | 06ed74d | 2016-12-14 17:49:47 +0530 | [diff] [blame] | 236 | |
| 237 | @frappe.whitelist() |
| 238 | def get_grade(grading_scale, percentage): |
| 239 | """Returns Grade based on the Grading Scale and Score. |
| 240 | |
| 241 | :param Grading Scale: Grading Scale |
| 242 | :param Percentage: Score Percentage Percentage |
| 243 | """ |
| 244 | grading_scale_intervals = {} |
Neil Trini Lasrado | 233ecfd | 2017-01-04 15:41:38 +0530 | [diff] [blame] | 245 | for d in frappe.get_all("Grading Scale Interval", fields=["grade_code", "threshold"], filters={"parent": grading_scale}): |
| 246 | grading_scale_intervals.update({d.threshold:d.grade_code}) |
Neil Trini Lasrado | 06ed74d | 2016-12-14 17:49:47 +0530 | [diff] [blame] | 247 | intervals = sorted(grading_scale_intervals.keys(), key=float, reverse=True) |
| 248 | for interval in intervals: |
| 249 | if flt(percentage) >= interval: |
| 250 | grade = grading_scale_intervals.get(interval) |
| 251 | break |
| 252 | else: |
Neil Trini Lasrado | 2a9e0f6 | 2017-01-09 18:25:43 +0530 | [diff] [blame] | 253 | grade = "" |
| 254 | return grade |
| 255 | |
| 256 | @frappe.whitelist() |
| 257 | def mark_assessment_result(student, assessment_plan, scores): |
| 258 | student_score = json.loads(scores) |
| 259 | details = [] |
| 260 | for s in student_score.keys(): |
| 261 | details.append({ |
Neil Trini Lasrado | 749516c | 2017-02-01 18:40:30 +0530 | [diff] [blame] | 262 | "assessment_criteria": s, |
Neil Trini Lasrado | 2a9e0f6 | 2017-01-09 18:25:43 +0530 | [diff] [blame] | 263 | "score": flt(student_score[s]) |
| 264 | }) |
| 265 | assessment_result = frappe.new_doc("Assessment Result") |
| 266 | assessment_result.update({ |
| 267 | "student": student, |
| 268 | "student_name": frappe.db.get_value("Student", student, "title"), |
| 269 | "assessment_plan": assessment_plan, |
| 270 | "details": details |
| 271 | }) |
| 272 | assessment_result.save() |
| 273 | assessment_result.submit() |
Neil Trini Lasrado | fa2023d | 2017-02-13 19:26:16 +0530 | [diff] [blame] | 274 | return assessment_result |
| 275 | |
| 276 | @frappe.whitelist() |
| 277 | def update_email_group(doctype, name): |
| 278 | if not frappe.db.exists("Email Group", name): |
| 279 | email_group = frappe.new_doc("Email Group") |
| 280 | email_group.title = name |
| 281 | email_group.save() |
| 282 | email_list = [] |
| 283 | students = [] |
Neil Trini Lasrado | fa2023d | 2017-02-13 19:26:16 +0530 | [diff] [blame] | 284 | if doctype == "Student Group": |
| 285 | students = get_student_group_students(name) |
| 286 | for stud in students: |
| 287 | for guard in get_student_guardians(stud.student): |
| 288 | email = frappe.db.get_value("Guardian", guard.guardian, "email_address") |
| 289 | if email: |
| 290 | email_list.append(email) |
Nabin Hait | 753c8de | 2017-02-22 11:53:13 +0530 | [diff] [blame] | 291 | add_subscribers(name, email_list) |