diff --git a/lbplanner/classes/helpers/config_helper.php b/lbplanner/classes/helpers/config_helper.php index 57c65be4..868ca930 100644 --- a/lbplanner/classes/helpers/config_helper.php +++ b/lbplanner/classes/helpers/config_helper.php @@ -67,9 +67,10 @@ public static function set_default_active_year() { */ public static function add_customfield(): void { // Check if the category is already created and only create it if it doesn't exist. - // Check if plugin "modcustomfields" is installed and create the category and the custom field. + // TODO: When the plugin gets uninstalled, the category should be deleted as well. + // TODO: Or check if it is already there when reinstalling. if (!get_config('local_lbplanner', 'categoryid')) { - + // Check if plugin "modcustomfields" is installed and create the category and the custom field. if (array_key_exists('modcustomfields', core_component::get_plugin_list('local'))) { $handler = mod_handler::create(); diff --git a/lbplanner/classes/helpers/modules_helper.php b/lbplanner/classes/helpers/modules_helper.php index 51b06eed..3fd53cea 100644 --- a/lbplanner/classes/helpers/modules_helper.php +++ b/lbplanner/classes/helpers/modules_helper.php @@ -24,7 +24,12 @@ namespace local_lbplanner\helpers; -use block_accessreview\external\get_module_data; + +use core_customfield\category; +use local_lbplanner\helpers\config_helper; +use core_customfield\category_controller; +use customfield_select\data_controller; +use customfield_select\field_controller; use external_function_parameters; use external_single_structure; use external_value; @@ -35,42 +40,6 @@ * Contains helper functions for working with modules. */ class modules_helper { - - /** - * Table where modules are stored. - */ - const ASSIGN_TABLE = 'assign'; - - /** - * Table where max. and min. grades of the modules are stored. - */ - const GRADE_ITEMS_TABLE = 'grade_items'; - - /** - * Table where course modules are stored. - */ - const COURSE_MODULES_TABLE = 'course_modules'; - - /** - * Table where grades of the modules are stored. - */ - const GRADES_TABLE = 'assign_grades'; - - /** - * Table where grading scales are stored. - */ - const SCALE_TABLE = 'scale'; - - /** - * Table where submissions of the modules are stored. - */ - const SUBMISSIONS_TABLE = 'assign_submission'; - - /** - * Submitted status name of a submission. - */ - const SUBMISSION_STATUS_SUBMITTED = 'submitted'; - /** * The return structure of a module. * @@ -146,9 +115,10 @@ public static function map_status(bool $submitted, bool $done, bool $late): int * Maps the given name to a module type. * * @param string $modulename The name of the module. + * @param int $cmid The coursemodule id of the module. * @return integer The enum value for the module type. */ - public static function determin_type(string $modulename): int { + public static function determin_type(string $modulename, int $cmid): int { // Convert module name to uppercase. $modulename = strtoupper($modulename); @@ -170,6 +140,22 @@ public static function determin_type(string $modulename): int { if (strpos($modulename, '[M]') !== false) { return MODULE_TYPE::M; } + $categorycontroller = category_controller::create(config_helper::get_category_id()); + $datacontroller = $categorycontroller->get_handler()->get_instance_data($cmid)[0]; + $type = intval($datacontroller->get('value')); + if ($type == 1) { + return MODULE_TYPE::GK; + } else if ($type == 2) { + return MODULE_TYPE::EK; + } else if ($type == 3) { + return MODULE_TYPE::GK; + } else if ($type == 4) { + return MODULE_TYPE::TEST; + } else if ($type == 5) { + return MODULE_TYPE::TEST; + } else if ($type == 6) { + return MODULE_TYPE::M; + } // Return TYPE_NONE elswise. return MODULE_TYPE::NONE; @@ -182,15 +168,9 @@ public static function determin_type(string $modulename): int { * @param int $courseid The id of the course. * @return string The url of the module. */ - public static function get_module_url(int $moduleid, int $courseid): string { - global $DB; - - $view = $DB->get_record( - self::COURSE_MODULES_TABLE, - ['course' => $courseid, 'instance' => $moduleid, 'module' => 1] - ); - - return strval(new moodle_url('/mod/assign/view.php?id='.$view->id)); + public static function get_module_url(string $type, int $cmid): string { + $url = new moodle_url('/mod/'.$type.'/view.php', ['id' => $cmid]); + return $url->out(false); } /** diff --git a/lbplanner/classes/model/activity.php b/lbplanner/classes/model/activity.php new file mode 100644 index 00000000..c1cc9cd7 --- /dev/null +++ b/lbplanner/classes/model/activity.php @@ -0,0 +1,49 @@ +. +/** + * Collection of helper classes for handling modules + * + * @package local_lbplanner + * @subpackage helpers + * @copyright 2024 NecodeIT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace local_lbplanner\model; + +class activity { + public $moduleid; + public $name; + public $courseid; + public $status; + public $type; + public $url; + public $grade; + public $deadline; + public $modtype; + public function __construct($moduleid, $name, $courseid, $status, $modtype, $type, $url, $grade, $deadline) { + $this->modtype = $modtype; + $this->moduleid = $moduleid; + $this->name = $name; + $this->courseid = $courseid; + $this->status = $status; + $this->type = $type; + $this->url = $url; + $this->grade = $grade; + $this->deadline = $deadline; + } +} + diff --git a/lbplanner/db/upgrade.php b/lbplanner/db/upgrade.php index ee29a436..927f4783 100644 --- a/lbplanner/db/upgrade.php +++ b/lbplanner/db/upgrade.php @@ -26,15 +26,19 @@ /** * Upgrades the DB version - * right now it only sets the default active year + * This function does anything necessary to upgrade the plugin from an old version to the current version. * * @param mixed $oldversion (unused) the previous version to upgrade from * @return bool true */ function xmldb_local_lbplanner_upgrade($oldversion): bool { - if ($oldversion < 2024022700) { + if ($oldversion < 2024022703) { config_helper::set_default_active_year(); - config_helper::add_customfield(); + try { + config_helper::add_customfield(); + } catch (coding_exception $e) { + new moodle_exception('error_adding_customfield', 'local_lbplanner', '', $e->getMessage()); + } } return true; } diff --git a/lbplanner/services/modules/get_all_course_modules.php b/lbplanner/services/modules/get_all_course_modules.php index b53ec82d..ce4b5aec 100644 --- a/lbplanner/services/modules/get_all_course_modules.php +++ b/lbplanner/services/modules/get_all_course_modules.php @@ -16,12 +16,20 @@ namespace local_lbplanner_services; + +use core_completion\cm_completion_details; +use core_customfield\category_controller; +use core_external\external_single_structure; +use core_h5p\core; +use course_modinfo; +use customfield_select\data_controller; use external_api; use external_function_parameters; use external_multiple_structure; use external_value; +use local_lbplanner\model\activity; use local_lbplanner\helpers\modules_helper; -use local_lbplanner\helpers\user_helper; +use stdClass; /** * Get all the modules of the given course. @@ -38,45 +46,64 @@ class modules_get_all_course_modules extends external_api { */ public static function get_all_course_modules_parameters(): external_function_parameters { return new external_function_parameters([ - 'courseid' => new external_value(PARAM_INT, 'The id of the course', VALUE_REQUIRED, null, NULL_NOT_ALLOWED), - 'userid' => new external_value(PARAM_INT, 'The id of the user', VALUE_REQUIRED, null, NULL_NOT_ALLOWED), - 'ekenabled' => new external_value( - PARAM_BOOL, - 'Whether or not to include ek modules', - VALUE_REQUIRED, - false, - NULL_NOT_ALLOWED), - ]); + 'courseid' => new external_value(PARAM_INT, 'The id of the course', VALUE_REQUIRED, null, NULL_NOT_ALLOWED)]); } /** * Returns all the modules inside a course. * * @param int $courseid The ID of the course - * @param int $userid The ID of the user * @param bool $ekenabled whether or not to include ek modules - * @return array the modules + * @return array|null the modules */ - public static function get_all_course_modules(int $courseid, int $userid, bool $ekenabled): array { - global $DB; - + public static function get_all_course_modules(int $courseid): ?array { + global $DB, $USER, $CFG; + require_once($CFG->dirroot . '/course/lib.php'); + // path to the module class which is in this plugin self::validate_parameters( self::get_all_course_modules_parameters(), - ['courseid' => $courseid, 'userid' => $userid, 'ekenabled' => $ekenabled] + ['courseid' => $courseid] ); - - user_helper::assert_access($userid); - - return modules_helper::get_all_course_modules($courseid, $userid, $ekenabled); + $activities = course_modinfo::get_array_of_activities(get_course($courseid)); + $modules = []; + foreach ($activities as $activity) { + $modtype = $activity->mod; + if ($modtype !== 'assign' || $modtype !== 'quiz') { + continue; + } + $id = $activity->id; + $deadline = $activity->duedate; + $name = $activity->name; + $cmid = $activity->cm; + $url = modules_helper::get_module_url($modtype, $cmid); + $modules[] = new activity( + $id, + $name, + $courseid, + 1, + $modtype, + modules_helper::determin_type($name, $cmid), + $url, + 1, + $deadline + ); + } + die(var_dump()); + $test = new stdClass(); + $test->id = 392; + die(var_dump(assign_get_user_grades($test, $USER->id))); + $completion = new \completion_info(get_course($courseid)); + $courseandcm = get_course_and_cm_from_cmid(392, null, $courseid, $USER->id); + die(var_dump(get_course_and_cm_from_cmid(392, null, $courseid, $USER->id))); + die(var_dump(cm_completion_details::get_instance(get_fast_modinfo($courseid, $USER->id))->get_details())); + return $type; } /** * Returns the structure of the module array. * @return external_multiple_structure */ - public static function get_all_course_modules_returns(): external_multiple_structure { - return new external_multiple_structure( - modules_helper::structure(), - ); + public static function get_all_course_modules_returns() { + new external_value(PARAM_INT, 'Module ID'); } } diff --git a/lbplanner/services/modules/get_module.php b/lbplanner/services/modules/get_module.php index a57c33d4..842d6de3 100644 --- a/lbplanner/services/modules/get_module.php +++ b/lbplanner/services/modules/get_module.php @@ -20,6 +20,7 @@ use external_function_parameters; use external_single_structure; use external_value; +use local_lbplanner\helpers\course_helper; use local_lbplanner\helpers\modules_helper; use local_lbplanner\helpers\plan_helper; use local_lbplanner\helpers\user_helper; @@ -40,7 +41,6 @@ class modules_get_module extends external_api { public static function get_module_parameters(): external_function_parameters { return new external_function_parameters([ 'moduleid' => new external_value(PARAM_INT, 'The id of the module', VALUE_REQUIRED, null, NULL_NOT_ALLOWED), - 'userid' => new external_value(PARAM_INT, 'The id of the user', VALUE_REQUIRED, null, NULL_NOT_ALLOWED), ]); } @@ -48,21 +48,14 @@ public static function get_module_parameters(): external_function_parameters { * Returns the data for a module * * @param int $moduleid The ID of the course - * @param int $userid The ID of the user * @return array the module */ - public static function get_module(int $moduleid, int $userid): array { + public static function get_module(int $moduleid): array { global $DB; - self::validate_parameters(self::get_module_parameters(), ['moduleid' => $moduleid, 'userid' => $userid]); + self::validate_parameters(self::get_module_parameters(), ['moduleid' => $moduleid]); - user_helper::assert_access($userid); - - if (!$DB->record_exists(modules_helper::MDL_ASSIGN_TABLE, ['id' => $moduleid])) { - throw new \moodle_exception('Module not found'); - } - - return modules_helper::get_module($moduleid, $userid); + return modules_helper::get_module($moduleid); } /** diff --git a/lbplanner/version.php b/lbplanner/version.php index caab6006..8acc347a 100644 --- a/lbplanner/version.php +++ b/lbplanner/version.php @@ -27,7 +27,7 @@ $plugin->component = 'local_lbplanner'; $plugin->release = 'Alpha v.'.$release; -$plugin->version = 2024031200; +$plugin->version = 2024031301; $plugin->dependencies = [ // Depend upon version 2023110600 of local_modcustomfields. 'local_modcustomfields' => 2023110600,