Course Scheduling Tool (#11676)

* [education] Fix Course Scheduling Tool

* minor
This commit is contained in:
Faris Ansari 2017-11-21 17:28:51 +05:30 committed by Nabin Hait
parent 476c66d699
commit 6ca6a69070
3 changed files with 90 additions and 58 deletions

View File

@ -1,20 +1,39 @@
// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors
// For license information, please see license.txt
cur_frm.add_fetch("student_group", "program", "program");
cur_frm.add_fetch("student_group", "course", "course");
cur_frm.add_fetch("student_group", "academic_year", "academic_year");
cur_frm.add_fetch("student_group", "academic_term", "academic_term");
frappe.ui.form.on("Course Scheduling Tool", {
refresh: function(frm) {
frappe.ui.form.on('Course Scheduling Tool', {
setup(frm) {
frm.add_fetch('student_group', 'program', 'program');
frm.add_fetch('student_group', 'course', 'course');
frm.add_fetch('student_group', 'academic_year', 'academic_year');
frm.add_fetch('student_group', 'academic_term', 'academic_term');
},
refresh(frm) {
frm.disable_save();
frm.page.set_primary_action(__("Schedule Course"), function() {
frappe.call({
method: "schedule_course",
doc: frm.doc
})
frm.page.set_primary_action(__('Schedule Course'), () => {
frm.call('schedule_course')
.then(r => {
if (!r.message) {
frappe.throw(__('There were errors creating Course Schedule'));
}
const { course_schedules } = r.message;
if (course_schedules) {
const html = `
<table class="table table-bordered">
<caption>${__('Following course schedules were created')}</caption>
<thead><tr><th>${__("Course")}</th><th>${__("Date")}</th></tr></thead>
<tbody>
${course_schedules.map(
c => `<tr><td><a href="#Form/Course Schedule/${c.name}">${c.name}</a></td>
<td>${c.schedule_date}</td></tr>`
).join('')}
</tbody>
</table>`
frappe.msgprint(html);
}
});
});
}
});

View File

@ -476,7 +476,7 @@
"bold": 0,
"collapsible": 0,
"columns": 0,
"fieldname": "rechedule",
"fieldname": "reschedule",
"fieldtype": "Check",
"hidden": 0,
"ignore_user_permissions": 0,
@ -485,7 +485,7 @@
"in_global_search": 0,
"in_list_view": 0,
"in_standard_filter": 0,
"label": "Rechedule",
"label": "Reschedule",
"length": 0,
"no_copy": 0,
"permlevel": 0,
@ -602,7 +602,7 @@
"istable": 0,
"max_attachments": 0,
"menu_index": 0,
"modified": "2017-06-30 08:21:47.696063",
"modified": "2017-11-21 16:47:58.091740",
"modified_by": "Administrator",
"module": "Schools",
"name": "Course Scheduling Tool",

View File

@ -10,9 +10,11 @@ from frappe.model.document import Document
from frappe.utils import add_days, getdate
from erpnext.schools.utils import OverlapError
class CourseSchedulingTool(Document):
def schedule_course(self):
"""Creates course schedules as per specified parametes"""
"""Creates course schedules as per specified parameters"""
course_schedules = []
course_schedules_errors = []
@ -21,60 +23,72 @@ class CourseSchedulingTool(Document):
self.validate_mandatory()
self.validate_date()
self.instructor_name= frappe.db.get_value("Instructor", self.instructor, "instructor_name")
self.instructor_name = frappe.db.get_value(
"Instructor", self.instructor, "instructor_name")
group_based_on, course = frappe.db.get_value(
"Student Group", self.student_group, ["group_based_on", "course"])
group_based_on, course = frappe.db.get_value("Student Group", self.student_group, ["group_based_on", "course"])
if group_based_on == "Course":
self.course = course
if self.rechedule:
rescheduled, reschedule_errors = self.delete_course_schedule(rescheduled, reschedule_errors)
if self.reschedule:
rescheduled, reschedule_errors = self.delete_course_schedule(
rescheduled, reschedule_errors)
date = self.course_start_date
while(date < self.course_end_date):
while date < self.course_end_date:
if self.day == calendar.day_name[getdate(date).weekday()]:
course_schedule = self.make_course_schedule(date)
try:
print('pass')
course_schedule.save()
except OverlapError:
print('fail')
course_schedules_errors.append(date)
else:
course_schedules.append(course_schedule.name + " on " + date)
course_schedules.append(course_schedule)
date = add_days(date, 7)
else:
date = add_days(date, 1)
frappe.local.message_log = []
if course_schedules:
frappe.msgprint(_("Course Schedules created:") + "\n" + "\n".join(course_schedules))
if course_schedules_errors:
frappe.msgprint(_("There were errors while scheduling course on :") + "\n" + "\n".join(course_schedules_errors))
if rescheduled:
frappe.msgprint(_("Course Schedules deleted:") + "\n" + "\n".join(rescheduled))
if reschedule_errors:
frappe.msgprint(_("There were errors while deleting following schedules:") + "\n" + "\n".join(reschedule_errors))
return dict(
course_schedules=course_schedules,
course_schedules_errors=course_schedules_errors,
rescheduled=rescheduled,
reschedule_errors=reschedule_errors
)
def validate_mandatory(self):
"""Validates all mandatory fields"""
fields = ['course', 'room', 'instructor', 'from_time', 'to_time', 'course_start_date', 'course_end_date', 'day']
fields = ['course', 'room', 'instructor', 'from_time',
'to_time', 'course_start_date', 'course_end_date', 'day']
for d in fields:
if not self.get(d):
frappe.throw(_("{0} is mandatory").format(self.meta.get_label(d)))
frappe.throw(_("{0} is mandatory").format(
self.meta.get_label(d)))
def validate_date(self):
"""Validates if Course Start Date is greater than Course End Date"""
if self.course_start_date > self.course_end_date:
frappe.throw("Course Start Date cannot be greater than Course End Date.")
frappe.throw(
"Course Start Date cannot be greater than Course End Date.")
def delete_course_schedule(self, rescheduled, reschedule_errors):
"""Delete all course schedule within the Date range and specified filters"""
schedules = frappe.get_list("Course Schedule", fields=["name", "schedule_date"], filters =
[["student_group", "=", self.student_group],
schedules = frappe.get_list("Course Schedule",
fields=["name", "schedule_date"],
filters=[
["student_group", "=", self.student_group],
["course", "=", self.course],
["schedule_date", ">=", self.course_start_date],
["schedule_date", "<=", self.course_end_date]])
["schedule_date", "<=", self.course_end_date]
]
)
for d in schedules:
try:
if self.day == calendar.day_name[getdate(d.schedule_date).weekday()]:
@ -98,4 +112,3 @@ class CourseSchedulingTool(Document):
course_schedule.from_time = self.from_time
course_schedule.to_time = self.to_time
return course_schedule