Skip to content

Commit

Permalink
MDL-56251 format_weeks: add new 'automaticenddate' setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese authored and mdjnelson committed May 4, 2017
1 parent b151723 commit 039ff55
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions course/format/weeks/lang/en/format_weeks.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
$string['page-course-view-weeks-x'] = 'Any course page in weeks format';
$string['hidefromothers'] = 'Hide week';
$string['showfromothers'] = 'Show week';
$string['automaticenddate'] = 'Calculate the end date from the number of sections';
$string['automaticenddate_help'] = 'If enabled, the end date for the course will be automatically calculated from the number of sections and the course start date.';
37 changes: 37 additions & 0 deletions course/format/weeks/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public function get_default_blocks() {
* Weeks format uses the following options:
* - coursedisplay
* - hiddensections
* - automaticenddate
*
* @param bool $foreditform
* @return array of options
Expand All @@ -237,6 +238,10 @@ public function course_format_options($foreditform = false) {
'default' => $courseconfig->coursedisplay,
'type' => PARAM_INT,
),
'automaticenddate' => array(
'default' => 1,
'type' => PARAM_BOOL,
),
);
}
if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
Expand Down Expand Up @@ -264,6 +269,12 @@ public function course_format_options($foreditform = false) {
),
'help' => 'coursedisplay',
'help_component' => 'moodle',
),
'automaticenddate' => array(
'label' => new lang_string('automaticenddate', 'format_weeks'),
'help' => 'automaticenddate',
'help_component' => 'format_weeks',
'element_type' => 'advcheckbox',
)
);
$courseformatoptions = array_merge_recursive($courseformatoptions, $courseformatoptionsedit);
Expand Down Expand Up @@ -299,6 +310,15 @@ public function create_edit_form_elements(&$mform, $forsection = false) {
array_unshift($elements, $element);
}

// Re-order things.
$mform->insertElementBefore($mform->removeElement('automaticenddate', false), 'idnumber');
$mform->disabledIf('enddate', 'automaticenddate', 'checked');
foreach ($elements as $key => $element) {
if ($element->getName() == 'automaticenddate') {
unset($elements[$key]);
}
}

return $elements;
}

Expand Down Expand Up @@ -329,6 +349,23 @@ public function update_course_format_options($data, $oldcourse = null) {
}
}
}

if ($data['automaticenddate']) {
$startdate = $data['startdate'];
if (!empty($data['numsections'])) {
$numsections = $data['numsections'];
} else if ($this->get_courseid()) {
// For existing courses get the number of sections.
$numsections = $this->get_last_section_number();
} else {
// Fallback to the default value for new courses.
$numsections = get_config('moodlecourse', 'numsections');
}

// Final week's last day.
$dates = $this->get_section_dates(intval($numsections), $startdate);
$data['enddate'] = $dates->end;
}
return $this->update_format_options($data);
}

Expand Down

0 comments on commit 039ff55

Please sign in to comment.