From b7b97f1d72ceaab8bd296193c314d5cde083301c Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Sun, 7 May 2017 16:10:04 +0200 Subject: [PATCH 1/2] MDL-56251 format_weeks: Cannot group by TEXT in Oracle Perform the grouping in subquery and later simply join with the table containing the TEXT col. --- course/format/weeks/lib.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index 9031c13645c..8e8a08dc295 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -516,18 +516,20 @@ class format_weeks extends format_base { // Use one DB query to retrieve necessary fields in course, value for automaticenddate and number of the last // section. This query will also validate that the course is indeed in 'weeks' format. - $sql = "SELECT c.id, c.format, c.startdate, c.enddate, fo.value AS automaticenddate, MAX(s.section) AS lastsection - FROM {course} c + $insql = "SELECT c.id, c.format, c.startdate, c.enddate, MAX(s.section) AS lastsection + FROM {course} c + JOIN {course_sections} s + ON s.course = c.id + WHERE c.format = :format + AND c.id = :courseid + GROUP BY c.id, c.format, c.startdate, c.enddate"; + $sql = "SELECT co.id, co.format, co.startdate, co.enddate, co.lastsection, fo.value AS automaticenddate + FROM ($insql) co LEFT JOIN {course_format_options} fo - ON fo.courseid = c.id - AND fo.format = c.format + ON fo.courseid = co.id + AND fo.format = co.format AND fo.name = :optionname - AND fo.sectionid = 0 - LEFT JOIN {course_sections} s - ON s.course = c.id - WHERE c.format = :format - AND c.id = :courseid - GROUP BY c.id, c.format, c.startdate, c.enddate, fo.value"; + AND fo.sectionid = 0"; $course = $DB->get_record_sql($sql, ['optionname' => 'automaticenddate', 'format' => 'weeks', 'courseid' => $courseid]); From cee8c18b42cdbb2e1f7e6b6642dec970a52b4037 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Mon, 8 May 2017 09:20:30 +0800 Subject: [PATCH 2/2] MDL-56251 format_weeks: ensure we use 'automaticenddate' default --- course/format/weeks/lib.php | 2 +- course/format/weeks/tests/observer_test.php | 32 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/course/format/weeks/lib.php b/course/format/weeks/lib.php index 8e8a08dc295..d0f0c9fa37a 100644 --- a/course/format/weeks/lib.php +++ b/course/format/weeks/lib.php @@ -545,7 +545,7 @@ class format_weeks extends format_base { // If automaticenddate is not specified take the default value. if (!isset($course->automaticenddate)) { $defaults = $format->course_format_options(); - $course->automaticenddate = $defaults['automaticenddate']; + $course->automaticenddate = $defaults['automaticenddate']['default']; } // Check that the course format for setting an automatic date is set. diff --git a/course/format/weeks/tests/observer_test.php b/course/format/weeks/tests/observer_test.php index d0ebf9ceafa..6e737d6ba15 100644 --- a/course/format/weeks/tests/observer_test.php +++ b/course/format/weeks/tests/observer_test.php @@ -148,6 +148,38 @@ class format_weeks_observer_testcase extends advanced_testcase { $this->assertEquals($enddate, $dates->end); } + /** + * Tests when we update a course without automatic end date set. + */ + public function test_create_section_without_automatic_end_date() { + global $DB; + + // Generate a course with some sections. + $startdate = time(); + $enddate = $startdate + WEEKSECS; + $course = $this->getDataGenerator()->create_course(array( + 'numsections' => 6, + 'format' => 'weeks', + 'startdate' => $startdate, + 'enddate' => $enddate, + 'automaticenddate' => 0)); + + // Delete automatic end date from the database. + $DB->delete_records('course_format_options', ['courseid' => $course->id, 'name' => 'automaticenddate']); + + // Create a new section. + course_create_section($course->id, 0); + + // Get the updated course end date. + $updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id)); + + // Confirm enddate is automatic now - since automatic end date is not set it is assumed default (which is '1'). + $format = course_get_format($course->id); + $this->assertEquals(7, $format->get_last_section_number()); + $dates = $format->get_section_dates(7); + $this->assertEquals($dates->end, $updateenddate); + } + /** * Tests when we deleting a course section with automatic end date set. */