Skip to content

Commit

Permalink
MDL-60957 mod_assign: Add calendar event for extension due dates
Browse files Browse the repository at this point in the history
  • Loading branch information
djarran committed Jul 22, 2024
1 parent 554a790 commit d433a0e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
1 change: 1 addition & 0 deletions mod/assign/lang/en/assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
46 changes: 45 additions & 1 deletion mod/assign/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
38 changes: 32 additions & 6 deletions mod/assign/tests/event/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}

Expand Down

0 comments on commit d433a0e

Please sign in to comment.