MDL-56251 format_weeks: add new 'automaticenddate' setting

This commit is contained in:
Damyon Wiese 2017-04-11 16:10:45 +08:00 committed by Mark Nelson
parent b1517237ad
commit 039ff55129
2 changed files with 39 additions and 0 deletions

View File

@ -36,3 +36,5 @@ $string['page-course-view-weeks'] = 'Any course main page in weeks format';
$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.';

View File

@ -220,6 +220,7 @@ class format_weeks extends format_base {
* Weeks format uses the following options:
* - coursedisplay
* - hiddensections
* - automaticenddate
*
* @param bool $foreditform
* @return array of options
@ -237,6 +238,10 @@ class format_weeks extends format_base {
'default' => $courseconfig->coursedisplay,
'type' => PARAM_INT,
),
'automaticenddate' => array(
'default' => 1,
'type' => PARAM_BOOL,
),
);
}
if ($foreditform && !isset($courseformatoptions['coursedisplay']['label'])) {
@ -264,6 +269,12 @@ class format_weeks extends format_base {
),
'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);
@ -299,6 +310,15 @@ class format_weeks extends format_base {
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;
}
@ -329,6 +349,23 @@ class format_weeks extends format_base {
}
}
}
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);
}