From 9d4617ca732b9ed788f142bd49cc9602a1a3e359 Mon Sep 17 00:00:00 2001 From: Dan Marsden Date: Tue, 25 Jul 2017 13:36:40 +1200 Subject: [PATCH] Fixes #269 Allow Calendar events to be enabled/disabled Also includes script to perform clean-up if settings change. --- classes/calendar_helpers.php | 19 +++++++- lang/en/attendance.php | 8 ++++ lib.php | 4 ++ resetcalendar.php | 91 ++++++++++++++++++++++++++++++++++++ settings.php | 4 ++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 resetcalendar.php diff --git a/classes/calendar_helpers.php b/classes/calendar_helpers.php index 723264c..9b5b8bf 100644 --- a/classes/calendar_helpers.php +++ b/classes/calendar_helpers.php @@ -32,12 +32,17 @@ require_once(dirname(__FILE__).'/../../../calendar/lib.php'); * @return bool result of calendar event creation */ function attendance_create_calendar_event(&$session) { + global $DB; + // We don't want to create multiple calendar events for 1 session. if ($session->caleventid) { return $session->caleventid; } + if (empty(get_config('attendance', 'enablecalendar'))) { + // Calendar events are not used. + return true; + } - global $DB; $attendance = $DB->get_record('attendance', array('id' => $session->attendanceid)); $caleventdata = new stdClass(); @@ -68,6 +73,12 @@ function attendance_create_calendar_event(&$session) { */ function attendance_create_calendar_events($sessionsids) { global $DB; + + if (empty(get_config('attendance', 'enablecalendar'))) { + // Calendar events are not used. + return true; + } + $sessions = $DB->get_recordset_list('attendance_sessions', 'id', $sessionsids); foreach ($sessions as $session) { @@ -87,6 +98,12 @@ function attendance_create_calendar_events($sessionsids) { * @return bool result of updating */ function attendance_update_calendar_event($caleventid, $timeduration, $timestart) { + + if (empty(get_config('attendance', 'enablecalendar'))) { + // Calendar events are not used. + return true; + } + $caleventdata = new stdClass(); $caleventdata->timeduration = $timeduration; $caleventdata->timestart = $timestart; diff --git a/lang/en/attendance.php b/lang/en/attendance.php index 8ecaeb4..1eb0354 100644 --- a/lang/en/attendance.php +++ b/lang/en/attendance.php @@ -168,6 +168,8 @@ $string['emailuser'] = 'Email user'; $string['emailuser_help'] = 'If checked, a warning will be sent to the student.'; $string['emptyacronym'] = 'Empty acronyms are not allowed. Status record not updated.'; $string['emptydescription'] = 'Empty descriptions are not allowed. Status record not updated.'; +$string['enablecalendar'] = 'Create calendar events'; +$string['enablecalendar_desc'] = 'If enabled, a calendar event will be created for each attendance session. After changing this setting you should run the reset calendar report.'; $string['enablewarnings'] = 'Enable warnings'; $string['enablewarnings_desc'] = 'This allows a warning set to be defined for an attendance and email notifications to users when attendance drops below the configured threshold.
WARNING: This is a new feature and has not been tested extensively. Please use at your own-risk and provide feeback in the moodle forums if you find it works well.'; $string['endofperiod'] = 'End of period'; @@ -180,6 +182,8 @@ $string['errorinaddingsession'] = 'Error in adding session'; $string['erroringeneratingsessions'] = 'Error in generating sessions '; $string['eventdurationupdated'] = 'Session duration updated'; $string['eventreportviewed'] = 'Attendance report viewed'; +$string['eventscreated'] = 'Calendar events created'; +$string['eventsdeleted'] = 'Calendar events deleted'; $string['eventsessionadded'] = 'Session added'; $string['eventsessiondeleted'] = 'Session deleted'; $string['eventsessionupdated'] = 'Session updated'; @@ -243,6 +247,7 @@ $string['noattendanceusers'] = 'It is not possible to export any data as there a $string['noautomark'] = 'Disabled'; $string['noattforuser'] = 'No attendance records exist for the user'; $string['nodescription'] = 'Regular class session'; +$string['noeventstoreset'] = 'There are no calendar events that require an update.'; $string['nogroups'] = 'You can\'t add group sessions. No groups exists in course.'; $string['noguest'] = 'Guest can\'t see attendance'; $string['noofdaysabsent'] = 'No of days absent'; @@ -337,6 +342,9 @@ $string['requiredentry_help'] = '

Attendance

Temporay user will be deleted in all cases after merge action

'; $string['requiresubnet'] = 'Require network address'; $string['requiresubnet_help'] = 'Attendance recording may be restricted to particular subnets by specifying a comma-separated list of partial or full IP addresses.'; +$string['resetcalendar'] = 'Reset calendar'; +$string['resetcaledarcreate'] = 'Calendar events have been enabled but a number of existing sessions do not have events. Do you want to create calendar events for all existing sessions?'; +$string['resetcaledardelete'] = 'Calendar events have been disabled but a number of existing sessions have events that should be deleted. Do you want to delete all existing events?'; $string['resetdescription'] = 'Remember that deleting attendance data will erase information from database. You can just hide older sessions having changed start date of course!'; $string['resetstatuses'] = 'Reset statuses to default'; $string['restoredefaults'] = 'Restore defaults'; diff --git a/lib.php b/lib.php index 429ef0d..9189e50 100644 --- a/lib.php +++ b/lib.php @@ -486,6 +486,10 @@ function attendance_print_settings_tabs($selected = 'settings') { $tabs[] = new tabobject('atrisk', $CFG->wwwroot . '/mod/attendance/atrisk.php', get_string('atriskreport', 'attendance'), get_string('atriskreport', 'attendance'), false); } + + $tabs[] = new tabobject('resetcalendar', $CFG->wwwroot.'/mod/attendance/resetcalendar.php', + get_string('resetcalendar', 'attendance'), get_string('resetcalendar', 'attendance'), false); + ob_start(); print_tabs(array($tabs), $selected); $tabmenu = ob_get_contents(); diff --git a/resetcalendar.php b/resetcalendar.php new file mode 100644 index 0000000..be8b598 --- /dev/null +++ b/resetcalendar.php @@ -0,0 +1,91 @@ +. + +/** + * Reset Calendar events. + * + * @package mod_attendance + * @copyright 2017 onwards Dan Marsden http://danmarsden.com + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once('../../config.php'); +require_once($CFG->libdir.'/adminlib.php'); +require_once($CFG->dirroot.'/mod/attendance/lib.php'); +require_once($CFG->dirroot.'/mod/attendance/locallib.php'); + +$action = optional_param('action', '', PARAM_ALPHA); + +admin_externalpage_setup('managemodules'); +$context = context_system::instance(); + +// Check permissions. +require_capability('mod/attendance:viewreports', $context); + +$exportfilename = 'attendanceatrisk.csv'; + +$PAGE->set_url('/mod/attendance/resetcalendar.php'); + +$PAGE->set_heading($SITE->fullname); + +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('resetcalendar', 'mod_attendance')); +$tabmenu = attendance_print_settings_tabs('resetcalendar'); +echo $tabmenu; + +if (get_config('attendance', 'enablecalendar')) { + // Check to see if all sessions have calendar events. + if ($action == 'create' && confirm_sesskey()) { + $sessions = $DB->get_recordset('attendance_sessions', array('caleventid' => 0)); + foreach ($sessions as $session) { + attendance_create_calendar_event($session); + if ($session->caleventid) { + $DB->update_record('attendance_sessions', $session); + } + } + $sessions->close(); + echo $OUTPUT->notification(get_string('eventscreated', 'mod_attendance'), 'notifysuccess'); + } else { + if ($DB->record_exists('attendance_sessions', array('caleventid' => 0))) { + $createurl = new moodle_url('/mod/attendance/resetcalendar.php', array('action' => 'create')); + $returnurl = new moodle_url('/admin/settings.php', array('section' => 'modsettingattendance')); + + echo $OUTPUT->confirm(get_string('resetcaledarcreate', 'mod_attendance'), $createurl, $returnurl); + } else { + echo $OUTPUT->box(get_string("noeventstoreset", "mod_attendance")); + } + } +} else { + if ($action == 'delete' && confirm_sesskey()) { + $caleventids = $DB->get_records_select_menu('attendance_sessions', 'caleventid > 0', array(), '', 'caleventid, caleventid as id2'); + $DB->delete_records_list('event', 'id', $caleventids); + $DB->execute("UPDATE {attendance_sessions} set caleventid = 0"); + echo $OUTPUT->notification(get_string('eventsdeleted', 'mod_attendance'), 'notifysuccess'); + } else { + // Check to see if there are any events that need to be deleted. + if ($DB->record_exists_select('attendance_sessions', 'caleventid > 0')) { + $deleteurl = new moodle_url('/mod/attendance/resetcalendar.php', array('action' => 'delete')); + $returnurl = new moodle_url('/admin/settings.php', array('section' => 'modsettingattendance')); + + echo $OUTPUT->confirm(get_string('resetcaledardelete', 'mod_attendance'), $deleteurl, $returnurl); + } else { + echo $OUTPUT->box(get_string("noeventstoreset", "mod_attendance")); + } + } + +} + +echo $OUTPUT->footer(); \ No newline at end of file diff --git a/settings.php b/settings.php index e99e3d2..fc99010 100644 --- a/settings.php +++ b/settings.php @@ -74,6 +74,10 @@ if ($ADMIN->fulltree) { get_string('defaultview', 'attendance'), get_string('defaultview_desc', 'attendance'), ATT_VIEW_WEEKS, $options)); + $settings->add(new admin_setting_configcheckbox('attendance/enablecalendar', + get_string('enablecalendar', 'attendance'), + get_string('enablecalendar_desc', 'attendance'), 1)); + $settings->add(new admin_setting_configcheckbox('attendance/enablewarnings', get_string('enablewarnings', 'attendance'), get_string('enablewarnings_desc', 'attendance'), 0));