Skip to content
Snippets Groups Projects
Commit 15ac28f8 authored by Alexander Bias's avatar Alexander Bias Committed by Alexander Bias
Browse files

Don't hardcode the term start months

This patch gets rid of the hardcoded term start months (april and october) and makes them configurable within two admin settings.

In addition to that change, two functions are added to the data_controller class which make the term start months gettable for callers from outside of the plugin.
To make clear from when on these functions are available for other plugins which require this plugin, the plugin version is bumped.
parent 07a678ee
Branches
No related tags found
No related merge requests found
...@@ -157,14 +157,64 @@ class data_controller extends \core_customfield\data_controller { ...@@ -157,14 +157,64 @@ class data_controller extends \core_customfield\data_controller {
public static function get_semester_for_datetime(DateTime $datetime): int { public static function get_semester_for_datetime(DateTime $datetime): int {
$year = (int) $datetime->format('Y'); $year = (int) $datetime->format('Y');
$month = (int) $datetime->format('m'); $month = (int) $datetime->format('m');
if ($month < 4) { $summertermstartmonth = self::get_summerterm_startmonth();
$wintertermstartmonth = self::get_winterterm_startmonth();
if ($month < $summertermstartmonth) {
$year--; $year--;
$semester = 1; $semester = 1;
} else if ($month < 10) { } else if ($month < $wintertermstartmonth) {
$semester = 0; $semester = 0;
} else { } else {
$semester = 1; $semester = 1;
} }
return $year * 10 + $semester; return $year * 10 + $semester;
} }
/**
* Returns the configured start month of the summer term from the plugin settings.
*
* @return int
*/
public static function get_summerterm_startmonth(): int {
global $CFG;
// Require local library.
require_once($CFG->dirroot.'/customfield/field/semester/locallib.php');
// Get config from DB.
$config = get_config('customfield_semester');
// Double-check that the value is within the acceptable range. If not, return the default value.
if (is_numeric($config->summertermstartmonth) == false ||
$config->summertermstartmonth < 1 || $config->summertermstartmonth > 12 ||
$config->summertermstartmonth > $config->wintertermstartmonth) {
return CUSTOMFIELD_SEMESTER_SUMMERTERMSTART;
}
return $config->summertermstartmonth;
}
/**
* Returns the configured start month of the winter term from the plugin settings.
*
* @return int
*/
public static function get_winterterm_startmonth(): int {
global $CFG;
// Require local library.
require_once($CFG->dirroot.'/customfield/field/semester/locallib.php');
// Get config from DB.
$config = get_config('customfield_semester');
// Double-check that the value is within the acceptable range. If not, return the default value.
if (is_numeric($config->wintertermstartmonth) == false ||
$config->wintertermstartmonth < 1 || $config->wintertermstartmonth > 12 ||
$config->wintertermstartmonth < $config->summertermstartmonth) {
return CUSTOMFIELD_SEMESTER_WINTERTERMSTART;
}
return $config->wintertermstartmonth;
}
} }
...@@ -32,3 +32,8 @@ $string['specificsettings'] = 'Einstellungen für das Semesterfeld'; ...@@ -32,3 +32,8 @@ $string['specificsettings'] = 'Einstellungen für das Semesterfeld';
$string['showmonthsintofuture'] = 'Ein Semester ist auswählbar, wenn es in weniger als X Monaten beginnt.'; $string['showmonthsintofuture'] = 'Ein Semester ist auswählbar, wenn es in weniger als X Monaten beginnt.';
$string['defaultmonthsintofuture'] = 'Standard ist das Semester in X Monaten.'; $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_desc'] = 'Mit dieser Einstellung definieren Sie in welchem Monat das Sommersemester 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['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.';
...@@ -32,3 +32,8 @@ $string['specificsettings'] = 'Semester field settings'; ...@@ -32,3 +32,8 @@ $string['specificsettings'] = 'Semester field settings';
$string['showmonthsintofuture'] = 'A semester will be selectable, if it begins in less than X months.'; $string['showmonthsintofuture'] = 'A semester will be selectable, if it begins in less than X months.';
$string['defaultmonthsintofuture'] = 'The default option is the semester in X months.'; $string['defaultmonthsintofuture'] = 'The default option is the semester in X months.';
$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_desc'] = 'With this setting, you define in which month the summer 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['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.';
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Customfield Semester Type - Local library.
*
* @package customfield_semester
* @copyright 2021 Alexander Bias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Constants for term default start months.
define('CUSTOMFIELD_SEMESTER_SUMMERTERMSTART', 4);
define('CUSTOMFIELD_SEMESTER_WINTERTERMSTART', 10);
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Customfield Semester Type - Settings file.
*
* @package customfield_semester
* @copyright 2021 Alexander Bias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if ($ADMIN->fulltree) {
// Require local library.
require_once($CFG->dirroot.'/customfield/field/semester/locallib.php');
// Prepare regex for month number. This will be used instead of the PARAM_* type within the admin settings.
$monthregex = '/^([1-9]|1[0-2])$/';
// Setting for the summer term start month.
$name = 'customfield_semester/summertermstartmonth';
$title = get_string('summertermstartmonth', 'customfield_semester', null, true);
$description = get_string('summertermstartmonth_desc', 'customfield_semester', null, true).'<br />'.
get_string('startmonthnote', 'customfield_semester', null, true);
$setting = new admin_setting_configtext($name, $title, $description, CUSTOMFIELD_SEMESTER_SUMMERTERMSTART, $monthregex, 2);
$settings->add($setting);
// Setting for the winter term start month.
$name = 'customfield_semester/wintertermstartmonth';
$title = get_string('wintertermstartmonth', 'customfield_semester', null, true);
$description = get_string('wintertermstartmonth_desc', 'customfield_semester', null, true).'<br />'.
get_string('startmonthnote', 'customfield_semester', null, true);
$setting = new admin_setting_configtext($name, $title, $description, CUSTOMFIELD_SEMESTER_WINTERTERMSTART, $monthregex, 2);
$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 = 2020041301; $plugin->version = 2020041302;
$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