Skip to content

Commit

Permalink
Merge branch 'MDL-56251_master-fix' of https://github.com/markn86/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
snake committed May 8, 2017
2 parents d363778 + cee8c18 commit 99e9d9e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
24 changes: 13 additions & 11 deletions course/format/weeks/lib.php
Expand Up @@ -516,18 +516,20 @@ public static function update_end_date($courseid) {

// 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]);

Expand All @@ -543,7 +545,7 @@ public static function update_end_date($courseid) {
// 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.
Expand Down
32 changes: 32 additions & 0 deletions course/format/weeks/tests/observer_test.php
Expand Up @@ -148,6 +148,38 @@ public function test_course_section_created_with_automatic_end_date() {
$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.
*/
Expand Down

0 comments on commit 99e9d9e

Please sign in to comment.