From e890ae2e0a753dc46c08f6b6ae5ce731bc0b0a63 Mon Sep 17 00:00:00 2001 From: watson8 <14983002+watson8@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:21:30 +0100 Subject: [PATCH] CTP-3704: avoid theme init on create (#4) Clean up handling of calendar event creation, update & deletion --- classes/calendar.php | 107 +++++++++++++++++++++++++++++++++++++++++++ lib.php | 74 ++++-------------------------- version.php | 2 +- 3 files changed, 117 insertions(+), 66 deletions(-) create mode 100644 classes/calendar.php diff --git a/classes/calendar.php b/classes/calendar.php new file mode 100644 index 00000000..6d52af5c --- /dev/null +++ b/classes/calendar.php @@ -0,0 +1,107 @@ +. + +/** + * Class to handle interaction with core calendar. + * @package mod_coursework + * @copyright 2024 UCL + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_coursework; + +/** + * Class to handle interaction with core calendar. + * + * @package mod_coursework + */ +class calendar { + /** + * Get an object to create a calendar event for coursework. + * @param object $coursework + * @param string $eventtype + * @param int|null $deadline + * @return \stdClass + */ + public static function coursework_event(object $coursework, string $eventtype, ?int $deadline): \stdClass { + + $event = new \stdClass(); + $event->type = CALENDAR_EVENT_TYPE_ACTION; + + // Description field needs some formatting in same way a mod_assign does it. + // Convert the links to pluginfile. It is a bit hacky but at this stage the files + // might not have been saved in the module area yet. + if ($draftid = file_get_submitted_draft_itemid('introeditor')) { + $intro = file_rewrite_urls_to_pluginfile($coursework->intro, $draftid); + } else { + $intro = $coursework->intro; + } + + $cm = get_coursemodule_from_instance('coursework', $coursework->id); + + // We need to remove the links to files as the calendar is not ready + // to support module events with file areas. + $intro = strip_pluginfile_content($intro); + if ($cm->showdescription) { + $event->description = array( + 'text' => $intro, + 'format' => $coursework->introformat + ); + } else { + $event->description = array( + 'text' => '', + 'format' => $coursework->introformat + ); + } + + $event->courseid = $coursework->course; + $event->name = $coursework->name; + $event->groupid = 0; + $event->userid = 0; + $event->modulename = 'coursework'; + $event->instance = $coursework->id; + $event->eventtype = $eventtype; + $event->timestart = $deadline; + $event->timeduration = 0; + $event->timesort = $deadline; + $event->visible = instance_is_visible('coursework', $coursework); + + return $event; + } + + + /** + * @param object $coursework + * @param string $eventtype if null then will remove all + * @return void + * @throws \dml_exception + */ + public static function remove_event($coursework, $eventtype = '') { + global $DB; + + $params = array('modulename' => 'coursework', 'instance' => $coursework->id); + + if ($eventtype) { + $params['eventtype'] = $eventtype; + } + + $events = $DB->get_records('event', $params); + foreach ($events as $eventid) { + $event = \calendar_event::load($eventid->id); + $event->delete(); // delete events from mdl_event table + } + } +} diff --git a/lib.php b/lib.php index c6bb773a..8784f020 100644 --- a/lib.php +++ b/lib.php @@ -196,24 +196,19 @@ function coursework_add_instance($formdata) { // Create event for coursework deadline [due] if ($coursework && $coursework->deadline) { - $event = coursework_event($coursework, format_module_intro('coursework', $coursework, - $coursemodule->id), $returnid, 'due', $coursework->deadline); - + $event = \mod_coursework\calendar::coursework_event($coursework, 'due', $coursework->deadline); calendar_event::create($event); } // Create event for coursework initialmarking deadline [initialgradingdue] if ($coursework && $coursework->marking_deadline_enabled() && $coursework->initialmarkingdeadline) { - $event = coursework_event($coursework, format_module_intro('coursework', $coursework, - $coursemodule->id), $returnid, 'initialgradingdue', $coursework->initialmarkingdeadline); - + $event = \mod_coursework\calendar::coursework_event($coursework, 'initialgradingdue', $coursework->initialmarkingdeadline); calendar_event::create($event); } // Create event for coursework agreedgrademarking deadline [agreedgradingdue] if ($coursework && $coursework->marking_deadline_enabled() && $coursework->agreedgrademarkingdeadline && $coursework->has_multiple_markers()) { - $event = coursework_event($coursework, format_module_intro('coursework', $coursework, - $coursemodule->id), $returnid, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline); + $event = \mod_coursework\calendar::coursework_event($coursework, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline); calendar_event::create($event); } @@ -500,18 +495,18 @@ function coursework_update_instance($coursework) { coursework_update_events($coursework, 'initialgradingdue'); // Cw initial grading deadine } else { // Remove it - remove_event($coursework, 'initialgradingdue'); + \mod_coursework\calendar::remove_event($coursework, 'initialgradingdue'); } if ($coursework->agreedgrademarkingdeadline && $coursework->numberofmarkers > 1) { // Update coursework_update_events($coursework, 'agreedgradingdue'); // Cw agreed grade deadine } else { // Remove it - remove_event($coursework, 'agreedgradingdue' ); + \mod_coursework\calendar::remove_event($coursework, 'agreedgradingdue' ); } } else { // Remove all deadline events for this coursework regardless the type - remove_event($coursework); + \mod_coursework\calendar::remove_event($coursework); } return $DB->update_record('coursework', $coursework); @@ -534,7 +529,7 @@ function coursework_update_events($coursework, $eventtype) { // Update/create event for coursework deadline [due] if ($eventtype == 'due') { - $data = coursework_event($coursework, $coursework->intro, $coursework->id, $eventtype, $coursework->deadline); + $data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->deadline); if ($event) { $event->update($data); //update if event exists } else { @@ -544,7 +539,7 @@ function coursework_update_events($coursework, $eventtype) { // Update/create event for coursework initialmarking deadline [initialgradingdue] if ($eventtype == 'initialgradingdue') { - $data = coursework_event($coursework, $coursework->intro, $coursework->id, $eventtype, $coursework->initialmarkingdeadline); + $data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->initialmarkingdeadline); if ($event) { $event->update($data); //update if event exists } else { @@ -554,7 +549,7 @@ function coursework_update_events($coursework, $eventtype) { // Update/create event for coursework agreedgrademarking deadline [agreedgradingdue] if ($eventtype == 'agreedgradingdue') { - $data = coursework_event($coursework, $coursework->intro, $coursework->id, $eventtype, $coursework->agreedgrademarkingdeadline); + $data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->agreedgrademarkingdeadline); if ($event) { $event->update($data); //update if event exists } else { @@ -563,22 +558,6 @@ function coursework_update_events($coursework, $eventtype) { } } -function remove_event($coursework, $eventtype = false) { - global $DB; - - $params = array('modulename' => 'coursework', 'instance' => $coursework->id); - - if ($eventtype) { - $params['eventtype'] = $eventtype; - } - - $events = $DB->get_records('event', $params); - foreach ($events as $eventid) { - $event = calendar_event::load($eventid->id); - $event->delete(); // delete events from mdl_event table - } -} - /** * Given an ID of an instance of this module, * this function will permanently delete the instance @@ -1476,41 +1455,6 @@ function coursework_personal_deadline_passed($courseworkid) { } -/** - * @param coursework $coursework - * @param $description - * @param $instance - * @param $eventtype - * @param $deadline - * @return stdClass - */ -function coursework_event($coursework, $description, $instance, $eventtype, $deadline) { - - $event = new stdClass(); - $event->type = CALENDAR_EVENT_TYPE_ACTION; - - $event->description = $description; - $event->courseid = $coursework->course; - $event->name = $coursework->name; - $event->groupid = 0; - $event->userid = 0; - $event->modulename = 'coursework'; - $event->instance = $instance; - $event->eventtype = $eventtype; - $event->timestart = $deadline; - $event->timeduration = 0; - $event->timesort = $deadline; - $event->visible = instance_is_visible('coursework', $coursework); - - /* if ($eventtype == 'initialgradingdue') { - $event->name .= " (Initial stage)"; - } else if ($eventtype == 'agreedgradingdue') { - $event->name .= " (Agreed Grade stage)"; - }*/ - - return $event; -} - /** * Purge coursework cache if a role with specific capability passed * diff --git a/version.php b/version.php index 2d63d101..c671f302 100644 --- a/version.php +++ b/version.php @@ -24,7 +24,7 @@ $plugin->component = 'mod_coursework'; -$plugin->version = 2024081900; // If version == 0 then module will not be installed +$plugin->version = 2024082700; // If version == 0 then module will not be installed $plugin->requires = 2023100400; // Requires this Moodle version $plugin->cron = 300; // Period for cron to check this module (secs).