diff --git a/add_form.php b/add_form.php index a1c6b69..439408f 100644 --- a/add_form.php +++ b/add_form.php @@ -125,6 +125,11 @@ class mod_attendance_add_form extends moodleform { array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean' => true, 'context' => $modcontext)); $mform->setType('sdescription', PARAM_RAW); + $mform->addElement('checkbox', 'calendarevent', '', get_string('calendarevent', 'attendance')); + $mform->addHelpButton('calendarevent', 'calendarevent', 'attendance'); + // XXX - this should be modified to use a different config setting if we keep enablecalendar's current meaning + $mform->setDefault('calendarevent', get_config('attendance', 'enablecalendar')); + // If warnings allow selector for reporting. if (!empty(get_config('attendance', 'enablewarnings'))) { $mform->addElement('checkbox', 'absenteereport', '', get_string('includeabsentee', 'attendance')); diff --git a/classes/calendar_helpers.php b/classes/calendar_helpers.php index 1e3c5f0..ce9472e 100644 --- a/classes/calendar_helpers.php +++ b/classes/calendar_helpers.php @@ -38,8 +38,8 @@ function attendance_create_calendar_event(&$session) { if ($session->caleventid) { return $session->caleventid; } - if (empty(get_config('attendance', 'enablecalendar'))) { - // Calendar events are not used. + if (empty(get_config('attendance', 'enablecalendar')) || $session->calendarevent === 0) { + // Calendar events are not used, or event not required for this session. return true; } @@ -98,18 +98,42 @@ function attendance_create_calendar_events($sessionsids) { /** * Update calendar event duration and date * - * @param int $caleventid calendar event id - * @param int $timeduration duration of the event - * @param int $timestart start time of the event + * @param stdClass $session Session data * @return bool result of updating */ -function attendance_update_calendar_event($caleventid, $timeduration, $timestart) { +function attendance_update_calendar_event($session) { + global $DB; + + $caleventid = $session->caleventid; + $timeduration = $session->duration; + $timestart = $session->sessdate; if (empty(get_config('attendance', 'enablecalendar'))) { // Calendar events are not used. return true; } + // Should there even be an event? + if ($session->calendarevent == 0) { + if ($session->caleventid != 0) { + // There is an existing event we should delete + // (calendarevent option just got turned off) + $DB->delete_records_list('event', 'id', array($caleventid)); + $session->caleventid = 0; + $DB->update_record('attendance_sessions', $session); + return true; + } else { + // This should be the common case when session does not want event + return true; + } + } + + // Do we need new event (calendarevent option has just been turned on)? + if ($session->caleventid == 0) { + return attendance_create_calendar_event($session); + } + + // Boring update $caleventdata = new stdClass(); $caleventdata->timeduration = $timeduration; $caleventdata->timestart = $timestart; diff --git a/classes/import/sessions.php b/classes/import/sessions.php index 74b9dcf..1e99a7a 100644 --- a/classes/import/sessions.php +++ b/classes/import/sessions.php @@ -109,7 +109,8 @@ class sessions { get_string('autoassignstatus', 'attendance'), get_string('absenteereport', 'attendance'), get_string('preventsharedip', 'attendance'), - get_string('preventsharediptime', 'attendance') + get_string('preventsharediptime', 'attendance'), + get_string('calendarevent', 'attendance') ); } @@ -148,6 +149,7 @@ class sessions { 'absenteereport' => $data->header15, 'preventsharedip' => $data->header16, 'preventsharediptime' => $data->header17, + 'calendarevent' => $data->header18 ); } else { return array( @@ -168,7 +170,8 @@ class sessions { 'autoassignstatus' => 14, 'absenteereport' => 15, 'preventsharedip' => 16, - 'preventsharediptime' => 17 + 'preventsharediptime' => 17, + 'calendarevent' => 18 ); } } diff --git a/classes/structure.php b/classes/structure.php index 2863bf6..1df808a 100644 --- a/classes/structure.php +++ b/classes/structure.php @@ -534,6 +534,7 @@ class mod_attendance_structure { array('subdirs' => false, 'maxfiles' => -1, 'maxbytes' => 0), $formdata->sdescription['text']); $sess->description = $description; $sess->descriptionformat = $formdata->sdescription['format']; + $sess->calendarevent = empty($formdata->calendarevent) ? 0 : $formdata->calendarevent; $sess->studentscanmark = 0; $sess->autoassignstatus = 0; @@ -579,7 +580,7 @@ class mod_attendance_structure { // This shouldn't really happen, but just in case to prevent fatal error. attendance_create_calendar_event($sess); } else { - attendance_update_calendar_event($sess->caleventid, $sess->duration, $sess->sessdate); + attendance_update_calendar_event($sess); } $info = construct_session_full_date_time($sess->sessdate, $sess->duration); @@ -1200,7 +1201,7 @@ class mod_attendance_structure { $sess->timemodified = $now; $DB->update_record('attendance_sessions', $sess); if ($sess->caleventid) { - attendance_update_calendar_event($sess->caleventid, $duration, $sess->sessdate); + attendance_update_calendar_event($sess); } $event = \mod_attendance\event\session_duration_updated::create(array( 'objectid' => $this->id, diff --git a/db/install.xml b/db/install.xml index e83a161..89c5ab0 100644 --- a/db/install.xml +++ b/db/install.xml @@ -48,6 +48,7 @@ + diff --git a/db/upgrade.php b/db/upgrade.php index e306234..be18683 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -511,5 +511,15 @@ function xmldb_attendance_upgrade($oldversion=0) { upgrade_mod_savepoint(true, 2018050100, 'attendance'); } + if ($oldversion < 2018051402) { + $table = new xmldb_table('attendance_sessions'); + $field = new xmldb_field('calendarevent', XMLDB_TYPE_INTEGER, '1', XMLDB_UNSIGNED, + XMLDB_NOTNULL, null, '1', 'caleventid'); + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + upgrade_mod_savepoint(true, 2018051402, 'attendance'); + } + return $result; } diff --git a/lang/en/attendance.php b/lang/en/attendance.php index a1243f0..88b5b78 100644 --- a/lang/en/attendance.php +++ b/lang/en/attendance.php @@ -78,6 +78,9 @@ $string['autorecorded'] = 'system auto recorded'; $string['averageattendance'] = 'Average attendance'; $string['averageattendancegraded'] = 'Average attendance'; $string['calclose'] = 'Close'; +$string['calendarevent'] = 'Create calendar event for session'; +$string['calendarevent_help'] = 'If enabled, a calendar event will be created for this session. +If disabled, any existing calendar event for this session will be deleted.'; $string['caleventcreated'] = 'Calendar event for session successfully created'; $string['caleventdeleted'] = 'Calendar event for session successfully deleted'; $string['calmonths'] = 'January,February,March,April,May,June,July,August,September,October,November,December'; diff --git a/locallib.php b/locallib.php index 519513a..d7acd12 100644 --- a/locallib.php +++ b/locallib.php @@ -575,6 +575,11 @@ function attendance_construct_sessions_data_for_add($formdata, mod_attendance_st $formdata->studentscanmark = 0; } + $calendarevent = 0; + if (isset($formdata->calendarevent)) { // Calendar event should be created + $calendarevent = 1; + } + $sessions = array(); if (isset($formdata->addmultiply)) { $startdate = $sessiondate; @@ -607,6 +612,7 @@ function attendance_construct_sessions_data_for_add($formdata, mod_attendance_st $sess->descriptionitemid = $formdata->sdescription['itemid']; $sess->description = $formdata->sdescription['text']; $sess->descriptionformat = $formdata->sdescription['format']; + $sess->calendarevent = $calendarevent; $sess->timemodified = $now; $sess->absenteereport = $absenteereport; $sess->studentpassword = ''; @@ -657,6 +663,7 @@ function attendance_construct_sessions_data_for_add($formdata, mod_attendance_st $sess->descriptionitemid = $formdata->sdescription['itemid']; $sess->description = $formdata->sdescription['text']; $sess->descriptionformat = $formdata->sdescription['format']; + $sess->calendarevent = $calendarevent; $sess->timemodified = $now; $sess->studentscanmark = 0; $sess->autoassignstatus = 0; diff --git a/resetcalendar.php b/resetcalendar.php index 63ac20c..2095325 100644 --- a/resetcalendar.php +++ b/resetcalendar.php @@ -47,9 +47,9 @@ $tabmenu = attendance_print_settings_tabs('resetcalendar'); echo $tabmenu; if (get_config('attendance', 'enablecalendar')) { - // Check to see if all sessions have calendar events. + // Check to see if all sessions that need them have calendar events. if ($action == 'create' && confirm_sesskey()) { - $sessions = $DB->get_recordset('attendance_sessions', array('caleventid' => 0)); + $sessions = $DB->get_recordset('attendance_sessions', array('caleventid' => 0, 'calendarevent' => 1)); foreach ($sessions as $session) { attendance_create_calendar_event($session); if ($session->caleventid) { @@ -59,7 +59,7 @@ if (get_config('attendance', 'enablecalendar')) { $sessions->close(); echo $OUTPUT->notification(get_string('eventscreated', 'mod_attendance'), 'notifysuccess'); } else { - if ($DB->record_exists('attendance_sessions', array('caleventid' => 0))) { + if ($DB->record_exists('attendance_sessions', array('caleventid' => 0, 'calendarevent' => 1))) { $createurl = new moodle_url('/mod/attendance/resetcalendar.php', array('action' => 'create')); $returnurl = new moodle_url('/admin/settings.php', array('section' => 'modsettingattendance')); diff --git a/tests/attendance_webservices_test.php b/tests/attendance_webservices_test.php index 4712c15..73fadc3 100644 --- a/tests/attendance_webservices_test.php +++ b/tests/attendance_webservices_test.php @@ -86,6 +86,7 @@ class attendance_webservices_tests extends advanced_testcase { $session->statusset = 0; $session->groupid = 0; $session->absenteereport = 1; + $session->calendarevent = 0; // Creating two sessions. $this->sessions[] = $session; diff --git a/update_form.php b/update_form.php index a9c0623..3dcbc64 100644 --- a/update_form.php +++ b/update_form.php @@ -62,19 +62,22 @@ class mod_attendance_update_form extends moodleform { $endhour = floor($endtime / HOURSECS); $endminute = floor(($endtime - $endhour * HOURSECS) / MINSECS); - $data = array('sessiondate' => $sess->sessdate, - 'sestime' => array('starthour' => $starthour, 'startminute' => $startminute, - 'endhour' => $endhour, 'endminute' => $endminute), - 'sdescription' => $sess->description_editor, - 'studentscanmark' => $sess->studentscanmark, - 'studentpassword' => $sess->studentpassword, - 'autoassignstatus' => $sess->autoassignstatus, - 'subnet' => $sess->subnet, - 'automark' => $sess->automark, - 'absenteereport' => $sess->absenteereport, - 'automarkcompleted' => 0, - 'preventsharedip' => $sess->preventsharedip, - 'preventsharediptime' => $sess->preventsharediptime); + $data = array( + 'sessiondate' => $sess->sessdate, + 'sestime' => array('starthour' => $starthour, 'startminute' => $startminute, + 'endhour' => $endhour, 'endminute' => $endminute), + 'sdescription' => $sess->description_editor, + 'calendarevent' => $sess->calendarevent, + 'studentscanmark' => $sess->studentscanmark, + 'studentpassword' => $sess->studentpassword, + 'autoassignstatus' => $sess->autoassignstatus, + 'subnet' => $sess->subnet, + 'automark' => $sess->automark, + 'absenteereport' => $sess->absenteereport, + 'automarkcompleted' => 0, + 'preventsharedip' => $sess->preventsharedip, + 'preventsharediptime' => $sess->preventsharediptime + ); if ($sess->subnet == $attendancesubnet) { $data['usedefaultsubnet'] = 1; } else { @@ -110,6 +113,11 @@ class mod_attendance_update_form extends moodleform { array('rows' => 1, 'columns' => 80), $defopts); $mform->setType('sdescription', PARAM_RAW); + $mform->addElement('checkbox', 'calendarevent', '', get_string('calendarevent', 'attendance')); + $mform->addHelpButton('calendarevent', 'calendarevent', 'attendance'); + // XXX - this should be modified to use a different config setting if we keep enablecalendar's current meaning + // $mform->setDefault('calendarevent', get_config('attendance', 'enablecalendar')); + // If warnings allow selector for reporting. if (!empty(get_config('attendance', 'enablewarnings'))) { $mform->addElement('checkbox', 'absenteereport', '', get_string('includeabsentee', 'attendance')); @@ -185,7 +193,6 @@ class mod_attendance_update_form extends moodleform { } $mform->setDefaults($data); - $this->add_action_buttons(true); } diff --git a/version.php b/version.php index efcb227..6a90541 100644 --- a/version.php +++ b/version.php @@ -23,7 +23,7 @@ */ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2018051401; +$plugin->version = 2018051402; $plugin->requires = 2018050800; // Requires 3.5. $plugin->release = '3.5.1'; $plugin->maturity = MATURITY_STABLE;