diff --git a/lbplanner/classes/helpers/config_helper.php b/lbplanner/classes/helpers/config_helper.php index 47e7a8e0..b2919e4b 100644 --- a/lbplanner/classes/helpers/config_helper.php +++ b/lbplanner/classes/helpers/config_helper.php @@ -30,33 +30,6 @@ * @license https://creativecommons.org/licenses/by-nc-sa/4.0/ CC-BY-NC-SA 4.0 International or later */ class config_helper { - /** - * Sets the current active year - */ - public static function set_default_active_year() { - $currentmonth = idate('m'); - $currentyear = idate('Y') % 100; - $lastyear = $currentyear - 1; - $nextyear = $currentyear + 1; - // Adding the default active year, when the plugin is installed for the first time. - // If the current month is between August and December, the default active year is set to current year and the next year. - if ($currentmonth >= 8 && $currentmonth <= 12) { - set_config( - 'defaultactiveyear', - $currentyear . '/' . $nextyear, - 'local_lbplanner' - ); - // If the current month is between January and July, the default active year is set to the previous year and the - // current year. - } else { - set_config( - 'defaultactiveyear', - $lastyear . '/' . $currentyear, - 'local_lbplanner' - ); - } - } - /** * Adds a customfield to moodle for each activity where teachers can select GK EK Test or M. * Default value is empty. diff --git a/lbplanner/classes/helpers/course_helper.php b/lbplanner/classes/helpers/course_helper.php index 96c5a865..eff74118 100644 --- a/lbplanner/classes/helpers/course_helper.php +++ b/lbplanner/classes/helpers/course_helper.php @@ -58,21 +58,6 @@ class course_helper { "#CA37B9", ]; - /** - * Get the current school year from the config - * Definition of a school year: 2020/2021 - * Check in config_helper.php for more info how the date is set for defaultactiveyear - * - * @return string the current year the last 2 digits (20/20) - * @throws dml_exception - */ - public static function get_current_year(): string { - if (strpos(get_config('local_lbplanner', 'activeyear'), '/') !== false) { - return get_config('local_lbplanner', 'activeyear'); - } - return get_config('local_lbplanner', 'defaultactiveyear'); - } - /** * Get course from lbpanner DB * @@ -88,7 +73,7 @@ public static function get_lbplanner_course(int $courseid, int $userid): course } /** - * Get all the courses of the current year. + * Get all current courses. * @return course[] all courses of the current year */ public static function get_all_lbplanner_courses(): array { @@ -98,14 +83,12 @@ public static function get_all_lbplanner_courses(): array { $mdlcourses = enrol_get_my_courses(); // Remove Duplicates. $mdlcourses = array_unique($mdlcourses, SORT_REGULAR); - // Check this out: https://www.youtube.com/watch?v=WmdAk2zyQkU . $results = []; foreach ($mdlcourses as $mdlcourse) { $courseid = $mdlcourse->id; - // Check if the course is from the current year. - // TODO: pass fullname to function instead of courseid. - if (!self::check_current_year($courseid)) { + // Check if the course is outdated. + if (!course::check_year($mdlcourse)) { continue; } // Check if the course is already in the LB Planner database. @@ -128,6 +111,7 @@ public static function get_all_lbplanner_courses(): array { } // Add name to fetched Course. $fetchedcourse->set_fullname($mdlcourse->fullname); + $fetchedcourse->set_mdlcourse($mdlcourse); array_push($results, $fetchedcourse); } return $results; @@ -160,16 +144,4 @@ public static function check_access(int $courseid, int $userid): bool { public static function get_fullname(int $courseid): string { return get_course($courseid)->fullname; } - - /** - * Check if the course is from the current year - * - * @param int $courseid the course id - * - * @return bool true if the course is from the current year - * @throws dml_exception - */ - public static function check_current_year(int $courseid): bool { - return strpos(self::get_fullname($courseid), self::get_current_year()) !== false; - } } diff --git a/lbplanner/classes/model/course.php b/lbplanner/classes/model/course.php index ee2654e3..07db999b 100644 --- a/lbplanner/classes/model/course.php +++ b/lbplanner/classes/model/course.php @@ -26,6 +26,7 @@ namespace local_lbplanner\model; use core_external\{external_single_structure, external_value}; +use DateTimeImmutable; use local_lbplanner\helpers\course_helper; /** @@ -61,6 +62,10 @@ class course { * @var bool $enabled whether the user wants to see this course */ public bool $enabled; + /** + * @var ?\stdClass $mdlcourse cached moodle course object + */ + private ?\stdClass $mdlcourse; /** * Constructs a new course @@ -79,6 +84,7 @@ public function __construct(int $id, int $courseid, int $userid, string $shortna $this->set_color($color); $this->enabled = $enabled; $this->fullname = null; + $this->mdlcourse = null; } /** @@ -153,6 +159,7 @@ public function set_enabled(bool $enabled) { /** * sets the cached fullname (mainly for deduplicating DB requests) + * TODO: remove in favour of cached mdluser * @param string $fullname the cached fullname */ public function set_fullname(string $fullname) { @@ -188,6 +195,41 @@ public static function prepare_shortname(string $shortname): string { return strtoupper($shortname); } + /** + * Check if the course is outdated + * @param \stdClass $mdlcourse the moodle course object to check + * @return bool false if the course's end is one year or longer ago, true otherwise + */ + public static function check_year(\stdClass $mdlcourse): bool { + $enddate = $mdlcourse->enddate; + $now = new DateTimeImmutable(); + $dti = $now->setTimestamp($enddate); + return $now->diff($dti)->y >= 0; + } + + /** + * sets the associated moodle course (for caching) + * @param \stdClass $mdlcourse + */ + public function set_mdlcourse(\stdClass $mdlcourse): void { + if ($this->mdlcourse !== null) { + throw new \coding_exception('tried to set cached mdluser twice'); + } + $this->mdlcourse = $mdlcourse; + } + + /** + * gets the associated moodle course + * @return \stdClass mdlcourse + */ + public function get_mdlcourse(): \stdClass { + if ($this->mdlcourse === null) { + $this->mdlcourse = get_course($this->id); + } + + return $this->mdlcourse; + } + /** * Prepares data for the DB endpoint. * doesn't set ID if it's 0 diff --git a/lbplanner/db/install.php b/lbplanner/db/install.php index 5f4dbe0f..71a98d0b 100644 --- a/lbplanner/db/install.php +++ b/lbplanner/db/install.php @@ -30,6 +30,5 @@ * Runs when plugin is first installed */ function xmldb_local_lbplanner_install() { - config_helper::set_default_active_year(); config_helper::add_customfield(); } diff --git a/lbplanner/db/upgrade.php b/lbplanner/db/upgrade.php index 52c389ac..5102d297 100644 --- a/lbplanner/db/upgrade.php +++ b/lbplanner/db/upgrade.php @@ -34,9 +34,11 @@ */ function xmldb_local_lbplanner_upgrade($oldversion): bool { if ($oldversion < 2024022700) { - config_helper::set_default_active_year(); config_helper::add_customfield(); } + if ($oldversion < 20250117) { + unset_config('defaultactiveyear', 'local_lbplanner'); + } return true; } diff --git a/lbplanner/version.php b/lbplanner/version.php index 2ea23f9b..c7015d60 100644 --- a/lbplanner/version.php +++ b/lbplanner/version.php @@ -18,7 +18,7 @@ * Defines versioning * * @package local_lbplanner - * @copyright 2024 NecodeIT + * @copyright 2025 NecodeIT * @license https://creativecommons.org/licenses/by-nc-sa/4.0/ CC-BY-NC-SA 4.0 International or later */ @@ -28,7 +28,7 @@ $plugin->component = 'local_lbplanner'; $plugin->release = 'Alpha v.'.$release; -$plugin->version = 2024031200; +$plugin->version = 2025011800; $plugin->dependencies = [ // Depend upon version 2023110600 of local_modcustomfields. 'local_modcustomfields' => 2023110600,