MDL-60758 tool_analytics: Skip guess start if higher than guess end
authorDavid Monllao <davidm@moodle.com>
Thu, 9 Nov 2017 12:21:11 +0000 (13:21 +0100)
committerDavid Monllao <davidm@moodle.com>
Thu, 9 Nov 2017 16:11:34 +0000 (17:11 +0100)
admin/tool/analytics/cli/guess_course_start_and_end.php

index 4e5ea9b..e0c8974 100644 (file)
@@ -117,12 +117,21 @@ function tool_analytics_calculate_course_dates($course, $options) {
 
     $notification = $course->shortname . ' (id = ' . $course->id . '): ';
 
+    $originalenddate = null;
+    $guessedstartdate = null;
+    $guessedenddate = null;
+    $samestartdate = null;
+    $lowerenddate = null;
+
     if ($options['guessstart'] || $options['guessall']) {
 
         $originalstartdate = $course->startdate;
 
         $guessedstartdate = $courseman->guess_start();
-        if ($guessedstartdate == $originalstartdate) {
+        $samestartdate = ($guessedstartdate == $originalstartdate);
+        $lowerenddate = ($course->enddate && ($course->enddate < $guessedstartdate));
+
+        if ($samestartdate) {
             if (!$guessedstartdate) {
                 $notification .= PHP_EOL . '  ' . get_string('cantguessstartdate', 'tool_analytics');
             } else {
@@ -131,6 +140,9 @@ function tool_analytics_calculate_course_dates($course, $options) {
             }
         } else if (!$guessedstartdate) {
             $notification .= PHP_EOL . '  ' . get_string('cantguessstartdate', 'tool_analytics');
+        } else if ($lowerenddate) {
+            $notification .= PHP_EOL . '  ' . get_string('cantguessstartdate', 'tool_analytics') . ': ' .
+                get_string('enddatebeforestartdate', 'error') . ' - ' . userdate($guessedstartdate);
         } else {
             // Update it to something we guess.
 
@@ -151,6 +163,10 @@ function tool_analytics_calculate_course_dates($course, $options) {
 
     if ($options['guessend'] || $options['guessall']) {
 
+        if (!empty($lowerenddate) && !empty($guessedstartdate)) {
+            $course->startdate = $guessedstartdate;
+        }
+
         $originalenddate = $course->enddate;
 
         $format = course_get_format($course);
@@ -196,10 +212,8 @@ function tool_analytics_calculate_course_dates($course, $options) {
                     $updateit = true;
                 }
 
-                if ($options['update']) {
-                    if ($course->enddate > $course->startdate) {
-                        update_course($course);
-                    }
+                if ($options['update'] && $updateit) {
+                    update_course($course);
                 }
             }
         }