Skip to content
Snippets Groups Projects
Commit 17e3d67c authored by Alexander Bias's avatar Alexander Bias
Browse files

Allow admin to define the sort order of the field values.

Before this patch, the sort ortder of the field values (as they appear within a course's settings and, if configured, in the course list filter on block_myoverview) was hardcoded to shw older terms first and add the term-independent term at the beginning of the list.
The more terms you have and the longer the list grows, the more this sorting becomes counterintuitive.

This patch adds an admin settings which lets the admin control how the list of terms is presented within the course settings and (if the field is used as a course filter) within the Dashboard course overview block.
parent 15ac28f8
No related branches found
No related tags found
No related merge requests found
...@@ -66,6 +66,15 @@ class data_controller extends \core_customfield\data_controller { ...@@ -66,6 +66,15 @@ class data_controller extends \core_customfield\data_controller {
* @param \MoodleQuickForm $mform * @param \MoodleQuickForm $mform
*/ */
public function instance_form_definition(\MoodleQuickForm $mform) { public function instance_form_definition(\MoodleQuickForm $mform) {
global $CFG;
// Require local library.
require_once($CFG->dirroot.'/customfield/field/semester/locallib.php');
// Get config from DB.
$config = get_config('customfield_semester');
// Compose the field values.
$field = $this->get_field(); $field = $this->get_field();
$formattedoptions = array( $formattedoptions = array(
1 => get_string('semesterindependent', 'customfield_semester') 1 => get_string('semesterindependent', 'customfield_semester')
...@@ -88,10 +97,18 @@ class data_controller extends \core_customfield\data_controller { ...@@ -88,10 +97,18 @@ class data_controller extends \core_customfield\data_controller {
$year . '/' . substr($year + 1, 2, 2)); $year . '/' . substr($year + 1, 2, 2));
} }
// The values were composed in CUSTOMFIELD_SEMESTER_PRESENTATION_ASC order here.
// If the admin wants to present them in CUSTOMFIELD_SEMESTER_PRESENTATION_DESC order, we need to reverse the array now.
if ($config->termpresentationorder == CUSTOMFIELD_SEMESTER_PRESENTATION_DESC) {
$formattedoptions = array_reverse($formattedoptions, true);
}
// Build the field widget.
$elementname = $this->get_form_element_name(); $elementname = $this->get_form_element_name();
$mform->addElement('select', $elementname, $this->get_field()->get_formatted_name(), $formattedoptions); $mform->addElement('select', $elementname, $this->get_field()->get_formatted_name(), $formattedoptions);
$mform->setDefault($elementname, $this->get_default_value()); $mform->setDefault($elementname, $this->get_default_value());
// Add the required flag if necessary.
if ($field->get_configdata_property('required')) { if ($field->get_configdata_property('required')) {
$mform->addRule($elementname, null, 'required', null, 'client'); $mform->addRule($elementname, null, 'required', null, 'client');
} }
......
...@@ -92,13 +92,32 @@ class field_controller extends \core_customfield\field_controller { ...@@ -92,13 +92,32 @@ class field_controller extends \core_customfield\field_controller {
* @return array * @return array
*/ */
public function course_grouping_format_values($values): array { public function course_grouping_format_values($values): array {
global $CFG;
// Require local library.
require_once($CFG->dirroot.'/customfield/field/semester/locallib.php');
// Get config from DB.
$config = get_config('customfield_semester');
// Prepare return array.
$ret = []; $ret = [];
// The values arrive in CUSTOMFIELD_SEMESTER_PRESENTATION_ASC order here.
// If the admin wants to present them in CUSTOMFIELD_SEMESTER_PRESENTATION_DESC order, we need to reverse the array now.
if ($config->termpresentationorder == CUSTOMFIELD_SEMESTER_PRESENTATION_DESC) {
$values = array_reverse($values, true);
}
// Iterate over given values.
foreach ($values as $value) { foreach ($values as $value) {
$name = data_controller::get_name_for_semester($value); $name = data_controller::get_name_for_semester($value);
if ($name) { if ($name) {
$ret[$value] = $name; $ret[$value] = $name;
} }
} }
// Return values.
return $ret; return $ret;
} }
} }
...@@ -34,6 +34,10 @@ $string['defaultmonthsintofuture'] = 'Standard ist das Semester in X Monaten.'; ...@@ -34,6 +34,10 @@ $string['defaultmonthsintofuture'] = 'Standard ist das Semester in X Monaten.';
$string['beginofsemesters'] = 'Das Jahr, in dem die Liste der Semester anfängt.'; $string['beginofsemesters'] = 'Das Jahr, in dem die Liste der Semester anfängt.';
$string['summertermstartmonth'] = 'Der Monat in dem das Sommersemester startet'; $string['summertermstartmonth'] = 'Der Monat in dem das Sommersemester startet';
$string['summertermstartmonth_desc'] = 'Mit dieser Einstellung definieren Sie in welchem Monat das Sommersemester startet.'; $string['summertermstartmonth_desc'] = 'Mit dieser Einstellung definieren Sie in welchem Monat das Sommersemester startet.';
$string['termpresentationasc'] = 'Ältere Semester zuerst, semesterunabhängiger Eintrag am Beginn der Liste';
$string['termpresentationdesc'] = 'Neuere Semester zuerst, semesterunabhängiger Eintrag am Ende der Liste';
$string['termpresentationorder'] = 'Darstellungsreihenfolge der Semester';
$string['termpresentationorder_desc'] = 'Mit dieser Einstellung definieren Sie wie die Liste der Semester innerhalb der Kurseinstellungen und (falls das Feld als Kursfilter verwendet wird) innerhalb des Kursübersichtsblocks auf dem Dashboard dargestellt wird.';
$string['wintertermstartmonth'] = 'Der Monat in dem das Wintersemester startet'; $string['wintertermstartmonth'] = 'Der Monat in dem das Wintersemester startet';
$string['wintertermstartmonth_desc'] = 'Mit dieser Einstellung definieren Sie in welchem Monat das Wintersemester startet.'; $string['wintertermstartmonth_desc'] = 'Mit dieser Einstellung definieren Sie in welchem Monat das Wintersemester startet.';
$string['startmonthnote'] = 'Bitte beachten: Gültige Einstellungen sind Werte zwischen 1 und 12. Diese Einstellung geht davon aus, dass das Sommersemester im Jahresverlauf vor dem Wintersemester kommt. Falls Sie die Semester andersrum konfigurieren, wird das Kursfeld Ihre Einstellung stillschweigend ignorieren und die Standardwerte nutzen.'; $string['startmonthnote'] = 'Bitte beachten: Gültige Einstellungen sind Werte zwischen 1 und 12. Diese Einstellung geht davon aus, dass das Sommersemester im Jahresverlauf vor dem Wintersemester kommt. Falls Sie die Semester andersrum konfigurieren, wird das Kursfeld Ihre Einstellung stillschweigend ignorieren und die Standardwerte nutzen.';
...@@ -34,6 +34,10 @@ $string['defaultmonthsintofuture'] = 'The default option is the semester in X mo ...@@ -34,6 +34,10 @@ $string['defaultmonthsintofuture'] = 'The default option is the semester in X mo
$string['beginofsemesters'] = 'The year, the list of semesters begins in.'; $string['beginofsemesters'] = 'The year, the list of semesters begins in.';
$string['summertermstartmonth'] = 'The month when summer term starts'; $string['summertermstartmonth'] = 'The month when summer term starts';
$string['summertermstartmonth_desc'] = 'With this setting, you define in which month the summer term starts.'; $string['summertermstartmonth_desc'] = 'With this setting, you define in which month the summer term starts.';
$string['termpresentationasc'] = 'Older terms first, term-independent entry at the beginning of the list';
$string['termpresentationdesc'] = 'Newer terms first, term-independent entry at the end of the list';
$string['termpresentationorder'] = 'Term presentation order';
$string['termpresentationorder_desc'] = 'With this setting, you control how the list of terms is presented within the course settings and (if the field is used as a course filter) within the Dashboard course overview block.';
$string['wintertermstartmonth'] = 'The month when winter term starts'; $string['wintertermstartmonth'] = 'The month when winter term starts';
$string['wintertermstartmonth_desc'] = 'With this setting, you define in which month the winter term starts.'; $string['wintertermstartmonth_desc'] = 'With this setting, you define in which month the winter term starts.';
$string['startmonthnote'] = 'Please note: Acceptable values are numbers between 1 and 12. This setting assumes that the summer term comes before the winter term. If you configure the terms the other way round, the custom field will silently ignore your settings and use the defaults.'; $string['startmonthnote'] = 'Please note: Acceptable values are numbers between 1 and 12. This setting assumes that the summer term comes before the winter term. If you configure the terms the other way round, the custom field will silently ignore your settings and use the defaults.';
...@@ -27,3 +27,7 @@ defined('MOODLE_INTERNAL') || die(); ...@@ -27,3 +27,7 @@ defined('MOODLE_INTERNAL') || die();
// Constants for term default start months. // Constants for term default start months.
define('CUSTOMFIELD_SEMESTER_SUMMERTERMSTART', 4); define('CUSTOMFIELD_SEMESTER_SUMMERTERMSTART', 4);
define('CUSTOMFIELD_SEMESTER_WINTERTERMSTART', 10); define('CUSTOMFIELD_SEMESTER_WINTERTERMSTART', 10);
// Constants for term presentation order.
define('CUSTOMFIELD_SEMESTER_PRESENTATION_ASC', 'asc');
define('CUSTOMFIELD_SEMESTER_PRESENTATION_DESC', 'desc');
...@@ -46,4 +46,13 @@ if ($ADMIN->fulltree) { ...@@ -46,4 +46,13 @@ if ($ADMIN->fulltree) {
get_string('startmonthnote', 'customfield_semester', null, true); get_string('startmonthnote', 'customfield_semester', null, true);
$setting = new admin_setting_configtext($name, $title, $description, CUSTOMFIELD_SEMESTER_WINTERTERMSTART, $monthregex, 2); $setting = new admin_setting_configtext($name, $title, $description, CUSTOMFIELD_SEMESTER_WINTERTERMSTART, $monthregex, 2);
$settings->add($setting); $settings->add($setting);
// Setting for the term presentation order.
$options = array (CUSTOMFIELD_SEMESTER_PRESENTATION_ASC => get_string('termpresentationasc', 'customfield_semester'),
CUSTOMFIELD_SEMESTER_PRESENTATION_DESC => get_string('termpresentationdesc', 'customfield_semester'));
$name = 'customfield_semester/termpresentationorder';
$title = get_string('termpresentationorder', 'customfield_semester', null, true);
$description = get_string('termpresentationorder_desc', 'customfield_semester', null, true);
$setting = new admin_setting_configselect($name, $title, $description, CUSTOMFIELD_SEMESTER_PRESENTATION_ASC, $options);
$settings->add($setting);
} }
...@@ -25,5 +25,5 @@ ...@@ -25,5 +25,5 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->component = 'customfield_semester'; $plugin->component = 'customfield_semester';
$plugin->version = 2020041302; $plugin->version = 2020041303;
$plugin->requires = 2019111800; $plugin->requires = 2019111800;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment