From d433a0e77d1f8ab8a78b5731631095d22074668c Mon Sep 17 00:00:00 2001 From: djarrancotleanu Date: Wed, 3 Jul 2024 11:26:14 +1000 Subject: [PATCH] MDL-60957 mod_assign: Add calendar event for extension due dates --- mod/assign/lang/en/assign.php | 1 + mod/assign/locallib.php | 46 +++++++++++++++++++++++++- mod/assign/tests/event/events_test.php | 38 +++++++++++++++++---- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/mod/assign/lang/en/assign.php b/mod/assign/lang/en/assign.php index ded62596673e6..1263a7df4000e 100644 --- a/mod/assign/lang/en/assign.php +++ b/mod/assign/lang/en/assign.php @@ -130,6 +130,7 @@ $string['blindmarking_help'] = 'Anonymous submissions hide the identity of students from markers. Anonymous submission settings will be locked once a submission or grade has been made in relation to this assignment.'; $string['cachedef_overrides'] = 'User and group override information'; $string['calendardue'] = '{$a} is due'; +$string['calendarextension'] = '{$a} is due (extension)'; $string['calendargradingdue'] = '{$a} is due to be graded'; $string['caneditsubmission'] = 'You can edit your submission and submit it after the time limit has expired, but it will be marked as late.'; $string['changeuser'] = 'Change user'; diff --git a/mod/assign/locallib.php b/mod/assign/locallib.php index 11a34a060cf06..8de2f9c0a96be 100644 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -88,6 +88,7 @@ define('ASSIGN_EVENT_TYPE_GRADINGDUE', 'gradingdue'); define('ASSIGN_EVENT_TYPE_OPEN', 'open'); define('ASSIGN_EVENT_TYPE_CLOSE', 'close'); +define('ASSIGN_EVENT_TYPE_EXTENSION', 'extension'); require_once($CFG->libdir . '/accesslib.php'); require_once($CFG->libdir . '/formslib.php'); @@ -6940,7 +6941,8 @@ protected function process_submit_for_grading($mform, $notices) { * @return boolean */ public function save_user_extension($userid, $extensionduedate) { - global $DB; + global $DB, $CFG; + require_once($CFG->dirroot.'/calendar/lib.php'); // Need submit permission to submit an assignment. require_capability('mod/assign:grantextension', $this->context); @@ -6970,6 +6972,48 @@ public function save_user_extension($userid, $extensionduedate) { if ($result) { \mod_assign\event\extension_granted::create_from_assign($this, $userid)->trigger(); + + $cm = $this->get_course_module(); + $instance = $this->get_instance(); + + if ($extensionduedate) { + $event = $DB->get_record('event', [ + 'userid' => $userid, + 'eventtype' => ASSIGN_EVENT_TYPE_EXTENSION, + 'modulename' => 'assign', + 'instance' => $instance->id, + ]); + + if ($event) { + $event->timestart = $extensionduedate; + $DB->update_record('event', $event); + } else { + $event = new stdClass(); + $event->type = CALENDAR_EVENT_TYPE_ACTION; + $event->name = get_string('calendarextension', 'assign', $instance->name); + $event->description = format_module_intro('assign', $instance, $cm->id); + $event->format = FORMAT_HTML; + $event->courseid = 0; + $event->groupid = 0; + $event->userid = $userid; + $event->modulename = 'assign'; + $event->instance = $instance->id; + $event->timestart = $extensionduedate; + $event->timeduration = 0; + $event->visible = instance_is_visible('assign', $instance); + $event->eventtype = ASSIGN_EVENT_TYPE_EXTENSION; + $event->priority = null; + + calendar_event::create($event, false); + } + } else { + $DB->delete_records('event', [ + 'userid' => $userid, + 'eventtype' => ASSIGN_EVENT_TYPE_EXTENSION, + 'modulename' => 'assign', + 'instance' => $instance->id, + ]); + } } return $result; } diff --git a/mod/assign/tests/event/events_test.php b/mod/assign/tests/event/events_test.php index b7d0c78cb9617..7de84b0d63f88 100644 --- a/mod/assign/tests/event/events_test.php +++ b/mod/assign/tests/event/events_test.php @@ -265,7 +265,15 @@ public function test_team_submission_removed(): void { $sink->close(); } + /** + * Test event creation for save_user_extension(). + * + * @covers \assign::save_user_extension + */ public function test_extension_granted(): void { + global $DB, $CFG; + require_once($CFG->dirroot.'/calendar/lib.php'); + $this->resetAfterTest(); $course = $this->getDataGenerator()->create_course(); @@ -287,12 +295,30 @@ public function test_extension_granted(): void { $assign->testable_save_user_extension($student->id, $tomorrow); $events = $sink->get_events(); - $this->assertCount(1, $events); - $event = reset($events); - $this->assertInstanceOf('\mod_assign\event\extension_granted', $event); - $this->assertEquals($assign->get_context(), $event->get_context()); - $this->assertEquals($assign->get_instance()->id, $event->objectid); - $this->assertEquals($student->id, $event->relateduserid); + + // Event for extension granted and extension due date. + $this->assertCount(2, $events); + + $grantedevent = $events[0]; + $this->assertInstanceOf('\mod_assign\event\extension_granted', $grantedevent); + $this->assertEquals($assign->get_context(), $grantedevent->get_context()); + $this->assertEquals($assign->get_instance()->id, $grantedevent->objectid); + $this->assertEquals($student->id, $grantedevent->relateduserid); + + $calendarevent = $events[1]; + $this->assertInstanceOf('\core\event\calendar_event_created', $calendarevent); + + // Check that the calendar event is deleted if extension is revoked. + $assign->testable_save_user_extension($student->id, ''); + + $isexist = $DB->record_exists('event', [ + 'userid' => $student->id, + 'eventtype' => ASSIGN_EVENT_TYPE_EXTENSION, + 'modulename' => 'assign', + 'instance' => $assign->get_course_module()->id, + ]); + $this->assertFalse($isexist); + $sink->close(); }