Skip to content

Commit

Permalink
MDL-43594 mod_assign: Fix calendar update on course reset
Browse files Browse the repository at this point in the history
This patch fixes the updating of calendar events when the course
is reset and its start date is changed.
  • Loading branch information
junpataleta committed Sep 29, 2015
1 parent fd57d68 commit 418a0d0
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
49 changes: 49 additions & 0 deletions mod/assign/lib.php
Expand Up @@ -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
*
Expand Down
39 changes: 39 additions & 0 deletions mod/assign/tests/lib_test.php
Expand Up @@ -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'));
}

}

0 comments on commit 418a0d0

Please sign in to comment.