MDL-57080 completion: Update completion expected date on reset
authorMichael Aherne <michael.aherne@strath.ac.uk>
Thu, 24 Nov 2016 09:05:05 +0000 (09:05 +0000)
committerMichael Aherne <michael.aherne@strath.ac.uk>
Thu, 22 Dec 2016 11:37:51 +0000 (11:37 +0000)
completion/criteria/completion_criteria_date.php
course/tests/courselib_test.php
lib/moodlelib.php

index 9327466..e3fbb6d 100644 (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();
+        }
+    }
 }
index 7ab3925..9c75b9a 100644 (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)));
     }
 
     /**
index 664cf40..311e9c8 100644 (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);
     }