From 418a0d0465bb3dfe1cec5f4fa4d4fd6d09086187 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Mon, 24 Aug 2015 13:49:27 +0800 Subject: [PATCH] MDL-43594 mod_assign: Fix calendar update on course reset This patch fixes the updating of calendar events when the course is reset and its start date is changed. --- mod/assign/lib.php | 49 +++++++++++++++++++++++++++++++++++ mod/assign/tests/lib_test.php | 39 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/mod/assign/lib.php b/mod/assign/lib.php index 61d1e9859a406..3a1f85aa23426 100644 --- a/mod/assign/lib.php +++ b/mod/assign/lib.php @@ -87,6 +87,55 @@ function assign_reset_userdata($data) { return $status; } +/** + * This standard function will check all instances of this module + * and make sure there are up-to-date events created for each of them. + * If courseid = 0, then every assignment event in the site is checked, else + * only assignment events belonging to the course specified are checked. + * + * @param int $courseid + * @return bool + */ +function assign_refresh_events($courseid = 0) { + global $CFG, $DB; + require_once($CFG->dirroot . '/mod/assign/locallib.php'); + + if ($courseid) { + // Make sure that the course id is numeric. + if (!is_numeric($courseid)) { + return false; + } + if (!$assigns = $DB->get_records('assign', array('course' => $courseid))) { + return false; + } + // Get course from courseid parameter. + if (!$course = $DB->get_record('course', array('id' => $courseid), '*')) { + return false; + } + } else { + if (!$assigns = $DB->get_records('assign')) { + return false; + } + } + foreach ($assigns as $assign) { + // Use assignment's course column if courseid parameter is not given. + if (!$courseid) { + $courseid = $assign->course; + if (!$course = $DB->get_record('course', array('id' => $courseid), '*')) { + continue; + } + } + if (!$cm = get_coursemodule_from_instance('assign', $assign->id, $courseid, false)) { + continue; + } + $context = context_module::instance($cm->id); + $assignment = new assign($context, $cm, $course); + $assignment->update_calendar($cm->id); + } + + return true; +} + /** * Removes all grades from gradebook * diff --git a/mod/assign/tests/lib_test.php b/mod/assign/tests/lib_test.php index 73336ae9057ed..66cabaf38be74 100644 --- a/mod/assign/tests/lib_test.php +++ b/mod/assign/tests/lib_test.php @@ -327,4 +327,43 @@ public function test_assign_get_completion_state() { $this->assertTrue($result); } + /** + * Tests for mod_assign_refresh_events. + */ + public function test_assign_refresh_events() { + global $DB; + $duedate = time(); + $this->setAdminUser(); + + $assign = $this->create_instance(array('duedate' => $duedate)); + + // Normal case, with existing course. + $this->assertTrue(assign_refresh_events($this->course->id)); + + $instance = $assign->get_instance(); + $eventparams = array('modulename' => 'assign', 'instance' => $instance->id); + $event = $DB->get_record('event', $eventparams, '*', MUST_EXIST); + $this->assertEquals($event->timestart, $duedate); + + // In case the course ID is passed as a numeric string. + $this->assertTrue(assign_refresh_events('' . $this->course->id)); + + // Course ID not provided. + $this->assertTrue(assign_refresh_events()); + + $eventparams = array('modulename' => 'assign'); + $events = $DB->get_records('event', $eventparams); + foreach ($events as $event) { + if ($event->modulename === 'assign' && $event->instance === $instance->id) { + $this->assertEquals($event->timestart, $duedate); + } + } + + // Non-existing course ID. + $this->assertFalse(assign_refresh_events(-1)); + + // Invalid course ID. + $this->assertFalse(assign_refresh_events('aaa')); + } + }