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
|
// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors
|
||||||
// For license information, please see license.txt
|
// 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", {
|
frappe.ui.form.on('Course Scheduling Tool', {
|
||||||
|
setup(frm) {
|
||||||
refresh: function(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.disable_save();
|
||||||
frm.page.set_primary_action(__("Schedule Course"), function() {
|
frm.page.set_primary_action(__('Schedule Course'), () => {
|
||||||
frappe.call({
|
frm.call('schedule_course')
|
||||||
method: "schedule_course",
|
.then(r => {
|
||||||
doc: frm.doc
|
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,
|
"bold": 0,
|
||||||
"collapsible": 0,
|
"collapsible": 0,
|
||||||
"columns": 0,
|
"columns": 0,
|
||||||
"fieldname": "rechedule",
|
"fieldname": "reschedule",
|
||||||
"fieldtype": "Check",
|
"fieldtype": "Check",
|
||||||
"hidden": 0,
|
"hidden": 0,
|
||||||
"ignore_user_permissions": 0,
|
"ignore_user_permissions": 0,
|
||||||
@ -485,7 +485,7 @@
|
|||||||
"in_global_search": 0,
|
"in_global_search": 0,
|
||||||
"in_list_view": 0,
|
"in_list_view": 0,
|
||||||
"in_standard_filter": 0,
|
"in_standard_filter": 0,
|
||||||
"label": "Rechedule",
|
"label": "Reschedule",
|
||||||
"length": 0,
|
"length": 0,
|
||||||
"no_copy": 0,
|
"no_copy": 0,
|
||||||
"permlevel": 0,
|
"permlevel": 0,
|
||||||
@ -602,7 +602,7 @@
|
|||||||
"istable": 0,
|
"istable": 0,
|
||||||
"max_attachments": 0,
|
"max_attachments": 0,
|
||||||
"menu_index": 0,
|
"menu_index": 0,
|
||||||
"modified": "2017-06-30 08:21:47.696063",
|
"modified": "2017-11-21 16:47:58.091740",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Schools",
|
"module": "Schools",
|
||||||
"name": "Course Scheduling Tool",
|
"name": "Course Scheduling Tool",
|
||||||
|
@ -10,71 +10,85 @@ from frappe.model.document import Document
|
|||||||
from frappe.utils import add_days, getdate
|
from frappe.utils import add_days, getdate
|
||||||
from erpnext.schools.utils import OverlapError
|
from erpnext.schools.utils import OverlapError
|
||||||
|
|
||||||
|
|
||||||
class CourseSchedulingTool(Document):
|
class CourseSchedulingTool(Document):
|
||||||
|
|
||||||
def schedule_course(self):
|
def schedule_course(self):
|
||||||
"""Creates course schedules as per specified parametes"""
|
"""Creates course schedules as per specified parameters"""
|
||||||
|
|
||||||
course_schedules= []
|
course_schedules = []
|
||||||
course_schedules_errors= []
|
course_schedules_errors = []
|
||||||
rescheduled= []
|
rescheduled = []
|
||||||
reschedule_errors= []
|
reschedule_errors = []
|
||||||
|
|
||||||
self.validate_mandatory()
|
self.validate_mandatory()
|
||||||
self.validate_date()
|
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":
|
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
|
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()]:
|
if self.day == calendar.day_name[getdate(date).weekday()]:
|
||||||
course_schedule = self.make_course_schedule(date)
|
course_schedule = self.make_course_schedule(date)
|
||||||
try:
|
try:
|
||||||
|
print('pass')
|
||||||
course_schedule.save()
|
course_schedule.save()
|
||||||
except OverlapError:
|
except OverlapError:
|
||||||
|
print('fail')
|
||||||
course_schedules_errors.append(date)
|
course_schedules_errors.append(date)
|
||||||
else:
|
else:
|
||||||
course_schedules.append(course_schedule.name + " on " + date)
|
course_schedules.append(course_schedule)
|
||||||
|
|
||||||
date = add_days(date, 7)
|
date = add_days(date, 7)
|
||||||
else:
|
else:
|
||||||
date = add_days(date, 1)
|
date = add_days(date, 1)
|
||||||
|
|
||||||
frappe.local.message_log = []
|
return dict(
|
||||||
if course_schedules:
|
course_schedules=course_schedules,
|
||||||
frappe.msgprint(_("Course Schedules created:") + "\n" + "\n".join(course_schedules))
|
course_schedules_errors=course_schedules_errors,
|
||||||
if course_schedules_errors:
|
rescheduled=rescheduled,
|
||||||
frappe.msgprint(_("There were errors while scheduling course on :") + "\n" + "\n".join(course_schedules_errors))
|
reschedule_errors=reschedule_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))
|
|
||||||
|
|
||||||
def validate_mandatory(self):
|
def validate_mandatory(self):
|
||||||
"""Validates all mandatory fields"""
|
"""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',
|
||||||
for d in fields:
|
'to_time', 'course_start_date', 'course_end_date', 'day']
|
||||||
|
for d in fields:
|
||||||
if not self.get(d):
|
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):
|
def validate_date(self):
|
||||||
"""Validates if Course Start Date is greater than Course End Date"""
|
"""Validates if Course Start Date is greater than Course End Date"""
|
||||||
if self.course_start_date > self.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):
|
def delete_course_schedule(self, rescheduled, reschedule_errors):
|
||||||
"""Delete all course schedule within the Date range and specified filters"""
|
"""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",
|
||||||
["course", "=", self.course],
|
fields=["name", "schedule_date"],
|
||||||
["schedule_date", ">=", self.course_start_date],
|
filters=[
|
||||||
["schedule_date", "<=", self.course_end_date]])
|
["student_group", "=", self.student_group],
|
||||||
|
["course", "=", self.course],
|
||||||
|
["schedule_date", ">=", self.course_start_date],
|
||||||
|
["schedule_date", "<=", self.course_end_date]
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
for d in schedules:
|
for d in schedules:
|
||||||
try:
|
try:
|
||||||
if self.day == calendar.day_name[getdate(d.schedule_date).weekday()]:
|
if self.day == calendar.day_name[getdate(d.schedule_date).weekday()]:
|
||||||
@ -83,19 +97,18 @@ class CourseSchedulingTool(Document):
|
|||||||
except:
|
except:
|
||||||
reschedule_errors.append(d.name)
|
reschedule_errors.append(d.name)
|
||||||
return rescheduled, reschedule_errors
|
return rescheduled, reschedule_errors
|
||||||
|
|
||||||
def make_course_schedule(self, date):
|
def make_course_schedule(self, date):
|
||||||
"""Makes a new Course Schedule.
|
"""Makes a new Course Schedule.
|
||||||
:param date: Date on which Course Schedule will be created."""
|
:param date: Date on which Course Schedule will be created."""
|
||||||
|
|
||||||
course_schedule = frappe.new_doc("Course Schedule")
|
course_schedule = frappe.new_doc("Course Schedule")
|
||||||
course_schedule.student_group = self.student_group
|
course_schedule.student_group = self.student_group
|
||||||
course_schedule.course = self.course
|
course_schedule.course = self.course
|
||||||
course_schedule.instructor = self.instructor
|
course_schedule.instructor = self.instructor
|
||||||
course_schedule.instructor_name = self.instructor_name
|
course_schedule.instructor_name = self.instructor_name
|
||||||
course_schedule.room = self.room
|
course_schedule.room = self.room
|
||||||
course_schedule.schedule_date= date
|
course_schedule.schedule_date = date
|
||||||
course_schedule.from_time= self.from_time
|
course_schedule.from_time = self.from_time
|
||||||
course_schedule.to_time= self.to_time
|
course_schedule.to_time = self.to_time
|
||||||
return course_schedule
|
return course_schedule
|
||||||
|
|
Loading…
Reference in New Issue
Block a user