MDL-57080 completion: Update completion expected date on reset

This commit is contained in:
Michael Aherne 2016-11-24 09:05:05 +00:00
parent 0fbe41f4cd
commit 1afea82da0
3 changed files with 54 additions and 2 deletions

View File

@ -213,4 +213,18 @@ class completion_criteria_date extends completion_criteria {
public function get_icon($alt, array $attributes = null) {
return new pix_icon('i/calendar', $alt, 'moodle', $attributes);
}
/**
* Shift the date when resetting course.
*
* @param int $courseid the course id
* @param int $timeshift number of seconds to shift date
* @return boolean was the operation successful?
*/
public static function update_date($courseid, $timeshift) {
if ($criteria = self::fetch(array('course' => $courseid))) {
$criteria->timeend = $criteria->timeend + $timeshift;
$criteria->update();
}
}
}

View File

@ -3205,14 +3205,24 @@ class core_course_courselib_testcase extends advanced_testcase {
* @param int $resultingenddate
*/
public function test_course_dates_reset($startdate, $enddate, $resetstartdate, $resetenddate, $resultingstartdate, $resultingenddate) {
global $DB;
global $CFG, $DB;
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
$this->resetAfterTest(true);
$CFG->enablecompletion = true;
$this->setTimezone('UTC');
$record = array('startdate' => $startdate, 'enddate' => $enddate);
$record = array('startdate' => $startdate, 'enddate' => $enddate, 'enablecompletion' => 1);
$originalcourse = $this->getDataGenerator()->create_course($record);
$coursecriteria = new completion_criteria_date(array('course' => $originalcourse->id, 'timeend' => $startdate + DAYSECS));
$coursecriteria->insert();
$activitycompletiondate = $startdate + DAYSECS;
$data = $this->getDataGenerator()->create_module('data', array('course' => $originalcourse->id),
array('completion' => 1, 'completionexpected' => $activitycompletiondate));
$resetdata = new stdClass();
$resetdata->id = $originalcourse->id;
@ -3226,6 +3236,12 @@ class core_course_courselib_testcase extends advanced_testcase {
$this->assertEquals($resultingstartdate, $course->startdate);
$this->assertEquals($resultingenddate, $course->enddate);
$coursecompletioncriteria = completion_criteria_date::fetch(array('course' => $originalcourse->id));
$this->assertEquals($resultingstartdate + DAYSECS, $coursecompletioncriteria->timeend);
$this->assertEquals($resultingstartdate + DAYSECS, $DB->get_field('course_modules', 'completionexpected',
array('id' => $data->cmid)));
}
/**

View File

@ -5180,6 +5180,7 @@ function reset_course_userdata($data) {
global $CFG, $DB;
require_once($CFG->libdir.'/gradelib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
require_once($CFG->dirroot.'/group/lib.php');
$data->courseid = $data->id;
@ -5224,6 +5225,27 @@ function reset_course_userdata($data) {
\availability_date\condition::update_all_dates($data->courseid, $data->timeshift);
}
// Update completion expected dates.
if ($CFG->enablecompletion) {
$modinfo = get_fast_modinfo($data->courseid);
$changed = false;
foreach ($modinfo->get_cms() as $cm) {
if ($cm->completion && !empty($cm->completionexpected)) {
$DB->set_field('course_modules', 'completionexpected', $cm->completionexpected + $data->timeshift,
array('id' => $cm->id));
$changed = true;
}
}
// Clear course cache if changes made.
if ($changed) {
rebuild_course_cache($data->courseid, true);
}
// Update course date completion criteria.
\completion_criteria_date::update_date($data->courseid, $data->timeshift);
}
$status[] = array('component' => $componentstr, 'item' => get_string('datechanged'), 'error' => false);
}