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/config_helper.php b/lbplanner/classes/helpers/config_helper.php index af657968..f29417bd 100644 --- a/lbplanner/classes/helpers/config_helper.php +++ b/lbplanner/classes/helpers/config_helper.php @@ -86,14 +86,14 @@ 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/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", - "defaultvalue":"GK","locked":"0","visibility":"2"}' + '{"required":"1","uniquevalues":"0","options":"GK\r\nEK\r\nTEST\r\nM", + "defaultvalue":"","locked":"0","visibility":"2"}' ); $fieldcontroller->set('shortname', 'lb_planner_gk_ek'); $fieldcontroller->save(); diff --git a/lbplanner/classes/helpers/modules_helper.php b/lbplanner/classes/helpers/modules_helper.php index 9709a191..873e9e55 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,21 @@ 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. + * @param int $moduleid The ID of the module. + * @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; + public static function determine_type(int $moduleid): int { + $catid = config_helper::get_category_id(); + if ($catid === -1) { + throw new \moodle_exception('couldn\'t find custom fields category ID'); } - - // 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; + $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. + return $type; } /** @@ -206,11 +193,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 +275,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); 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;