From 9f8a15b461fc616acfbf0c1ae826753d0cba123f Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 3 May 2017 11:26:12 +0800 Subject: [PATCH] MDL-56251 format_weeks: add upgrade for new 'automaticenddate' setting --- course/format/weeks/db/upgrade.php | 35 ++++++++++++++++++++++++++++++ course/format/weeks/version.php | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/course/format/weeks/db/upgrade.php b/course/format/weeks/db/upgrade.php index 97516e77cf2..b3d1d4a2c3e 100644 --- a/course/format/weeks/db/upgrade.php +++ b/course/format/weeks/db/upgrade.php @@ -43,5 +43,40 @@ function xmldb_format_weeks_upgrade($oldversion) { upgrade_plugin_savepoint(true, 2017020200, 'format', 'weeks'); } + if ($oldversion < 2017050300) { + // Go through the existing courses using the weeks format with no value set for the 'automaticenddate'. + $sql = "SELECT c.id, c.enddate, cfo.id as cfoid + FROM {course} c + LEFT JOIN {course_format_options} cfo + ON cfo.courseid = c.id + AND cfo.format = c.format + AND cfo.name = :optionname + AND cfo.sectionid = 0 + WHERE c.format = :format + AND cfo.id IS NULL"; + $params = ['optionname' => 'automaticenddate', 'format' => 'weeks']; + $courses = $DB->get_recordset_sql($sql, $params); + foreach ($courses as $course) { + $option = new stdClass(); + $option->courseid = $course->id; + $option->format = 'weeks'; + $option->sectionid = 0; + $option->name = 'automaticenddate'; + if (empty($course->enddate)) { + $option->value = 1; + $DB->insert_record('course_format_options', $option); + + // Now, let's update the course end date. + format_weeks::update_end_date($course->id); + } else { + $option->value = 0; + $DB->insert_record('course_format_options', $option); + } + } + $courses->close(); + + upgrade_plugin_savepoint(true, 2017050300, 'format', 'weeks'); + } + return true; } diff --git a/course/format/weeks/version.php b/course/format/weeks/version.php index befeee07dc4..c5955662f25 100644 --- a/course/format/weeks/version.php +++ b/course/format/weeks/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017050200; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2017050300; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2016112900; // Requires this Moodle version. $plugin->component = 'format_weeks'; // Full name of the plugin (used for diagnostics).