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 a907c5c0..a44f74bc 100644 --- a/lib.php +++ b/lib.php @@ -154,7 +154,6 @@ function mod_coursework_pluginfile($course, $cm, $context, $filearea, $args, $fo */ function coursework_add_instance($formdata) { global $DB, $CFG; - require_once("$CFG->dirroot/mod/coursework/locallib.php"); $formdata->timecreated = time(); @@ -197,19 +196,19 @@ function coursework_add_instance($formdata) { // Create event for coursework deadline [due] if ($coursework && $coursework->deadline) { - $event = coursework_event($coursework, '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, '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, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline); + $event = \mod_coursework\calendar::coursework_event($coursework, 'agreedgradingdue', $coursework->agreedgrademarkingdeadline); calendar_event::create($event); } @@ -496,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); @@ -520,7 +519,6 @@ function coursework_update_instance($coursework) { */ function coursework_update_events($coursework, $eventtype) { global $DB, $CFG; - require_once("$CFG->dirroot/mod/coursework/locallib.php"); $event = ""; $eventid = $DB->get_record('event', array('modulename' => 'coursework', 'instance' => $coursework->id, 'eventtype' => $eventtype)); @@ -531,7 +529,7 @@ function coursework_update_events($coursework, $eventtype) { // Update/create event for coursework deadline [due] if ($eventtype == 'due') { - $data = coursework_event($coursework, $eventtype, $coursework->deadline); + $data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->deadline); if ($event) { $event->update($data); //update if event exists } else { @@ -541,7 +539,7 @@ function coursework_update_events($coursework, $eventtype) { // Update/create event for coursework initialmarking deadline [initialgradingdue] if ($eventtype == 'initialgradingdue') { - $data = coursework_event($coursework, $eventtype, $coursework->initialmarkingdeadline); + $data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->initialmarkingdeadline); if ($event) { $event->update($data); //update if event exists } else { @@ -551,7 +549,7 @@ function coursework_update_events($coursework, $eventtype) { // Update/create event for coursework agreedgrademarking deadline [agreedgradingdue] if ($eventtype == 'agreedgradingdue') { - $data = coursework_event($coursework, $eventtype, $coursework->agreedgrademarkingdeadline); + $data = \mod_coursework\calendar::coursework_event($coursework, $eventtype, $coursework->agreedgrademarkingdeadline); if ($event) { $event->update($data); //update if event exists } else { @@ -560,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 diff --git a/locallib.php b/locallib.php deleted file mode 100644 index 455c5eac..00000000 --- a/locallib.php +++ /dev/null @@ -1,80 +0,0 @@ -. - -/** - * Local library for module coursework - * - * @package mod_coursework - * @copyright 2024 UCL - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -global $CFG; - -/** - * Get an object to create a calendar event for coursework. - * @param object $coursework - * @param string $eventtype - * @param int|null $deadline - * @return stdClass - */ -function coursework_event(object $coursework, string $eventtype, ?int $deadline) { - - $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; -}