diff --git a/admin/tool/analytics/cli/guess_course_start_and_end.php b/admin/tool/analytics/cli/guess_course_start_and_end.php index 4e5ea9b6a40..e0c8974b5d5 100644 --- a/admin/tool/analytics/cli/guess_course_start_and_end.php +++ b/admin/tool/analytics/cli/guess_course_start_and_end.php @@ -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); } } }