Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions lbplanner/classes/helpers/config_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 4 additions & 32 deletions lbplanner/classes/helpers/course_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 {
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
42 changes: 42 additions & 0 deletions lbplanner/classes/model/course.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace local_lbplanner\model;

use core_external\{external_single_structure, external_value};
use DateTimeImmutable;
use local_lbplanner\helpers\course_helper;

/**
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion lbplanner/db/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 3 additions & 1 deletion lbplanner/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

4 changes: 2 additions & 2 deletions lbplanner/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -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,
Expand Down
Loading