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.
This commit is contained in:
Jun Pataleta 2015-08-24 13:49:27 +08:00
parent fd57d685c7
commit 418a0d0465
2 changed files with 88 additions and 0 deletions

View File

@ -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
*

View File

@ -327,4 +327,43 @@ class mod_assign_lib_testcase extends mod_assign_base_testcase {
$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'));
}
}