From 884bd9f2392c972e833d38e74d9fcd913e981632 Mon Sep 17 00:00:00 2001 From: Riedler Date: Wed, 18 Dec 2024 03:21:51 +0100 Subject: [PATCH 1/7] fix: typo --- lbplanner/classes/helpers/config_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lbplanner/classes/helpers/config_helper.php b/lbplanner/classes/helpers/config_helper.php index af657968..aadfb148 100644 --- a/lbplanner/classes/helpers/config_helper.php +++ b/lbplanner/classes/helpers/config_helper.php @@ -86,7 +86,7 @@ public static function add_customfield(): void { $fieldcontroller = field_controller::create(0, $record, $categorycontroller); // Added the default attributes for the custom field. $fieldcontroller->set('name', 'LB Planner Task Type'); - $fieldcontroller->set('description', 'Tracks whether the task is GK/EK/GKandEK/TEST/SA/M'); + $fieldcontroller->set('description', 'Tracks whether the task is GK/EK/GK and EK/TEST/SA/M'); $fieldcontroller->set('type', 'select'); // Because moodle wants me to save the configdata as a json string, I have to do this. // I don't know why moodle does this, but it does. I don't like it. but I have to do it. so I do it. From cfad2b6555cd7372fb5aba62c6f88dd96a6722b0 Mon Sep 17 00:00:00 2001 From: Riedler Date: Wed, 18 Dec 2024 03:29:51 +0100 Subject: [PATCH 2/7] fix: removed unsupported values --- lbplanner/classes/helpers/config_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lbplanner/classes/helpers/config_helper.php b/lbplanner/classes/helpers/config_helper.php index aadfb148..656d9896 100644 --- a/lbplanner/classes/helpers/config_helper.php +++ b/lbplanner/classes/helpers/config_helper.php @@ -86,13 +86,13 @@ public static function add_customfield(): void { $fieldcontroller = field_controller::create(0, $record, $categorycontroller); // Added the default attributes for the custom field. $fieldcontroller->set('name', 'LB Planner Task Type'); - $fieldcontroller->set('description', 'Tracks whether the task is GK/EK/GK and EK/TEST/SA/M'); + $fieldcontroller->set('description', 'Tracks whether the task is GK/EK/TEST/M'); $fieldcontroller->set('type', 'select'); // Because moodle wants me to save the configdata as a json string, I have to do this. // I don't know why moodle does this, but it does. I don't like it. but I have to do it. so I do it. $fieldcontroller->set( 'configdata', - '{"required":"1","uniquevalues":"0","options":"GK\r\nEK\r\nGK and EK\r\nTEST\r\nSA\r\nM", + '{"required":"1","uniquevalues":"0","options":"GK\r\nEK\r\nTEST\r\nM", "defaultvalue":"GK","locked":"0","visibility":"2"}' ); $fieldcontroller->set('shortname', 'lb_planner_gk_ek'); From e8e6b11f193e1ce703030186f761d57526cdbffd Mon Sep 17 00:00:00 2001 From: Riedler Date: Wed, 18 Dec 2024 04:34:45 +0100 Subject: [PATCH 3/7] fix: still no using ValueError! :bonk: --- lbplanner/classes/polyfill/Enum.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lbplanner/classes/polyfill/Enum.php b/lbplanner/classes/polyfill/Enum.php index 3688d3b7..a0bce32e 100644 --- a/lbplanner/classes/polyfill/Enum.php +++ b/lbplanner/classes/polyfill/Enum.php @@ -57,7 +57,7 @@ private static function find($value, bool $try): ?EnumCase { * @param string $name the value to be matched * @param bool $try whether to return null (true) or throw an error (false) if not found * @return ?EnumCase the matching enum case or null if not found and $try==true - * @throws ValueError if not found and $try==false + * @throws \moodle_exception if not found and $try==false */ private static function find_from_name(string $name, bool $try): ?EnumCase { foreach (static::cases() as $case) { @@ -69,7 +69,7 @@ private static function find_from_name(string $name, bool $try): ?EnumCase { if ($try) { return null; } else { - throw new ValueError("name {$name} doesn't exist in ".static::class); + throw new \moodle_exception("name {$name} doesn't exist in ".static::class); } } /** @@ -90,7 +90,7 @@ public static function try_from($value) { * tries to match the passed value to one of the enum values * @param mixed $value the value to be matched * @return mixed the matching enum value - * @throws ValueError if not found + * @throws \moodle_exception if not found */ public static function from($value) { return static::find($value, false)->value; From 50b023c27703c759823c0fd5a9d2ed73c237add4 Mon Sep 17 00:00:00 2001 From: Riedler Date: Wed, 18 Dec 2024 04:38:27 +0100 Subject: [PATCH 4/7] feat: determine module type using custom field --- lbplanner/classes/enums/MODULE_TYPE.php | 4 -- lbplanner/classes/helpers/modules_helper.php | 44 ++++++-------------- 2 files changed, 12 insertions(+), 36 deletions(-) diff --git a/lbplanner/classes/enums/MODULE_TYPE.php b/lbplanner/classes/enums/MODULE_TYPE.php index b685973b..089c6d1a 100644 --- a/lbplanner/classes/enums/MODULE_TYPE.php +++ b/lbplanner/classes/enums/MODULE_TYPE.php @@ -49,8 +49,4 @@ class MODULE_TYPE extends Enum { * TODO: ??? */ const M = 3; - /** - * TODO: ??? - */ - const NONE = 4; } diff --git a/lbplanner/classes/helpers/modules_helper.php b/lbplanner/classes/helpers/modules_helper.php index 9709a191..c58a41c5 100644 --- a/lbplanner/classes/helpers/modules_helper.php +++ b/lbplanner/classes/helpers/modules_helper.php @@ -25,9 +25,11 @@ namespace local_lbplanner\helpers; +use core_customfield\category_controller; use core_external\{external_single_structure, external_value}; use moodle_url; use local_lbplanner\enums\{MODULE_STATUS, MODULE_GRADE, MODULE_TYPE}; +use local_modcustomfields\customfield\mod_handler; /** * Contains helper functions for working with modules. @@ -141,36 +143,17 @@ public static function map_status(bool $submitted, bool $done, bool $late): int } /** - * Maps the given name to a module type. + * Checks what type the module is. * * @param string $modulename The name of the module. - * @return integer The enum value for the module type. + * @return int The enum value for the module type. */ - public static function determin_type(string $modulename): int { - // Convert module name to uppercase. - $modulename = strtoupper($modulename); - - // Return TYPE_TEST if the name contains 'test' or 'sa'. - if (strpos($modulename, '[TEST]') !== false || strpos($modulename, '[SA]') !== false) { - return MODULE_TYPE::TEST; - } - // Return TYPE_GK if the name contains 'GK'. - - if (strpos($modulename, '[GK]') !== false) { - return MODULE_TYPE::GK; - } - - if (strpos($modulename, '[EK]') !== false) { - return MODULE_TYPE::EK; - } - - // Return TYPE_EK if the name contains 'M'. - if (strpos($modulename, '[M]') !== false) { - return MODULE_TYPE::M; - } - - // Return TYPE_NONE elswise. - return MODULE_TYPE::NONE; + public static function determine_type(int $moduleid): int { + $categorycontroller = category_controller::create(config_helper::get_category_id()); + $datacontroller = $categorycontroller->get_handler()->get_instance_data($moduleid)[0]; + $type = intval($datacontroller->get('value')); + MODULE_TYPE::name_from($type); // Basically asserting that this value exists as a module type. + return $type; } /** @@ -206,11 +189,8 @@ public static function get_module(int $moduleid, int $userid): array { $module = $DB->get_record(self::ASSIGN_TABLE, ['id' => $moduleid]); // Determine module type. - $type = self::determin_type($module->name); + $type = self::determine_type($moduleid); - if ($type == MODULE_TYPE::NONE) { - return []; - } // Check if there are any submissions or feedbacks for this module. $submitted = false; @@ -291,7 +271,7 @@ public static function get_all_course_modules(int $courseid, int $userid, bool $ $modules = []; foreach ($mdlmodules as $mdlmodule) { - if (!$ekenabled && self::determin_type($mdlmodule->name) == MODULE_TYPE::EK) { + if (!$ekenabled && self::determine_type($mdlmodule->id) == MODULE_TYPE::EK) { continue; } $module = self::get_module($mdlmodule->id, $userid); From af783457171a35bf49d11898534d2ea582fb730d Mon Sep 17 00:00:00 2001 From: Riedler Date: Wed, 18 Dec 2024 04:42:27 +0100 Subject: [PATCH 5/7] fix: forgotten docstring --- lbplanner/classes/helpers/modules_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lbplanner/classes/helpers/modules_helper.php b/lbplanner/classes/helpers/modules_helper.php index c58a41c5..f2fa0657 100644 --- a/lbplanner/classes/helpers/modules_helper.php +++ b/lbplanner/classes/helpers/modules_helper.php @@ -145,7 +145,7 @@ public static function map_status(bool $submitted, bool $done, bool $late): int /** * Checks what type the module is. * - * @param string $modulename The name of the module. + * @param int $moduleid The ID of the module. * @return int The enum value for the module type. */ public static function determine_type(int $moduleid): int { From 0e9814e5f2c92ea206804bde851925a11fb88a63 Mon Sep 17 00:00:00 2001 From: Riedler Date: Wed, 18 Dec 2024 04:44:14 +0100 Subject: [PATCH 6/7] added sanity check --- lbplanner/classes/helpers/modules_helper.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lbplanner/classes/helpers/modules_helper.php b/lbplanner/classes/helpers/modules_helper.php index f2fa0657..873e9e55 100644 --- a/lbplanner/classes/helpers/modules_helper.php +++ b/lbplanner/classes/helpers/modules_helper.php @@ -149,7 +149,11 @@ public static function map_status(bool $submitted, bool $done, bool $late): int * @return int The enum value for the module type. */ public static function determine_type(int $moduleid): int { - $categorycontroller = category_controller::create(config_helper::get_category_id()); + $catid = config_helper::get_category_id(); + if ($catid === -1) { + throw new \moodle_exception('couldn\'t find custom fields category ID'); + } + $categorycontroller = category_controller::create($catid); $datacontroller = $categorycontroller->get_handler()->get_instance_data($moduleid)[0]; $type = intval($datacontroller->get('value')); MODULE_TYPE::name_from($type); // Basically asserting that this value exists as a module type. From 46717c831f5cbcb0a34596c68b612b4672853384 Mon Sep 17 00:00:00 2001 From: Riedler Date: Thu, 19 Dec 2024 21:51:44 +0100 Subject: [PATCH 7/7] fix: made default option for GK/EK blank --- lbplanner/classes/helpers/config_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lbplanner/classes/helpers/config_helper.php b/lbplanner/classes/helpers/config_helper.php index 656d9896..f29417bd 100644 --- a/lbplanner/classes/helpers/config_helper.php +++ b/lbplanner/classes/helpers/config_helper.php @@ -93,7 +93,7 @@ public static function add_customfield(): void { $fieldcontroller->set( 'configdata', '{"required":"1","uniquevalues":"0","options":"GK\r\nEK\r\nTEST\r\nM", - "defaultvalue":"GK","locked":"0","visibility":"2"}' + "defaultvalue":"","locked":"0","visibility":"2"}' ); $fieldcontroller->set('shortname', 'lb_planner_gk_ek'); $fieldcontroller->save();