Commit eb89f7cb authored by Andrew Nicols's avatar Andrew Nicols
Browse files

Merge branch 'MDL-43594-master' of git://github.com/junpataleta/moodle

parents 853555f9 418a0d04
......@@ -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
*
......
......@@ -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'));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment