Merge branch 'MDL-54864-master' of https://github.com/snake/moodle
[moodle.git] / lib / db / upgrade.php
index f9c4194..c413aa6 100644 (file)
@@ -2084,6 +2084,47 @@ function xmldb_main_upgrade($oldversion) {
     }
 
     if ($oldversion < 2016081700.02) {
     }
 
     if ($oldversion < 2016081700.02) {
+        // Default schedule values.
+        $hour = 0;
+        $minute = 0;
+
+        // Get the old settings.
+        if (isset($CFG->statsruntimestarthour)) {
+            $hour = $CFG->statsruntimestarthour;
+        }
+        if (isset($CFG->statsruntimestartminute)) {
+            $minute = $CFG->statsruntimestartminute;
+        }
+
+        // Retrieve the scheduled task record first.
+        $stattask = $DB->get_record('task_scheduled', array('component' => 'moodle', 'classname' => '\core\task\stats_cron_task'));
+
+        // Don't touch customised scheduling.
+        if ($stattask && !$stattask->customised) {
+
+            $nextruntime = mktime($hour, $minute, 0, date('m'), date('d'), date('Y'));
+            if ($nextruntime < $stattask->lastruntime) {
+                // Add 24 hours to the next run time.
+                $newtime = new DateTime();
+                $newtime->setTimestamp($nextruntime);
+                $newtime->add(new DateInterval('P1D'));
+                $nextruntime = $newtime->getTimestamp();
+            }
+            $stattask->nextruntime = $nextruntime;
+            $stattask->minute = $minute;
+            $stattask->hour = $hour;
+            $stattask->customised = 1;
+            $DB->update_record('task_scheduled', $stattask);
+        }
+        // These settings are no longer used.
+        unset_config('statsruntimestarthour');
+        unset_config('statsruntimestartminute');
+        unset_config('statslastexecution');
+
+        upgrade_main_savepoint(true, 2016081700.02);
+    }
+
+    if ($oldversion < 2016082200.00) {
         // An upgrade step to remove any duplicate stamps, within the same context, in the question_categories table, and to
         // add a unique index to (contextid, stamp) to avoid future stamp duplication. See MDL-54864.
 
         // An upgrade step to remove any duplicate stamps, within the same context, in the question_categories table, and to
         // add a unique index to (contextid, stamp) to avoid future stamp duplication. See MDL-54864.
 
@@ -2133,7 +2174,7 @@ function xmldb_main_upgrade($oldversion) {
         }
 
         // Savepoint reached.
         }
 
         // Savepoint reached.
-        upgrade_main_savepoint(true, 2016081700.02);
+        upgrade_main_savepoint(true, 2016082200.00);
     }
 
     return true;
     }
 
     return true;