Course Scheduling Tool (#11676)
* [education] Fix Course Scheduling Tool * minor
This commit is contained in:
parent
476c66d699
commit
6ca6a69070
@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
@ -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",
|
||||
|
@ -10,71 +10,85 @@ 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"""
|
||||
|
||||
course_schedules= []
|
||||
course_schedules_errors= []
|
||||
rescheduled= []
|
||||
reschedule_errors= []
|
||||
|
||||
"""Creates course schedules as per specified parameters"""
|
||||
|
||||
course_schedules = []
|
||||
course_schedules_errors = []
|
||||
rescheduled = []
|
||||
reschedule_errors = []
|
||||
|
||||
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
|
||||
self.course = course
|
||||
|
||||
if self.reschedule:
|
||||
rescheduled, reschedule_errors = self.delete_course_schedule(
|
||||
rescheduled, reschedule_errors)
|
||||
|
||||
if self.rechedule:
|
||||
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']
|
||||
for d in fields:
|
||||
|
||||
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],
|
||||
["course", "=", self.course],
|
||||
["schedule_date", ">=", self.course_start_date],
|
||||
["schedule_date", "<=", self.course_end_date]])
|
||||
|
||||
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]
|
||||
]
|
||||
)
|
||||
|
||||
for d in schedules:
|
||||
try:
|
||||
if self.day == calendar.day_name[getdate(d.schedule_date).weekday()]:
|
||||
@ -83,19 +97,18 @@ class CourseSchedulingTool(Document):
|
||||
except:
|
||||
reschedule_errors.append(d.name)
|
||||
return rescheduled, reschedule_errors
|
||||
|
||||
|
||||
def make_course_schedule(self, date):
|
||||
"""Makes a new Course Schedule.
|
||||
:param date: Date on which Course Schedule will be created."""
|
||||
|
||||
|
||||
course_schedule = frappe.new_doc("Course Schedule")
|
||||
course_schedule.student_group = self.student_group
|
||||
course_schedule.course = self.course
|
||||
course_schedule.instructor = self.instructor
|
||||
course_schedule.instructor_name = self.instructor_name
|
||||
course_schedule.room = self.room
|
||||
course_schedule.schedule_date= date
|
||||
course_schedule.from_time= self.from_time
|
||||
course_schedule.to_time= self.to_time
|
||||
course_schedule.schedule_date = date
|
||||
course_schedule.from_time = self.from_time
|
||||
course_schedule.to_time = self.to_time
|
||||
return course_schedule
|
||||
|
Loading…
Reference in New Issue
Block a user