From a3efd16f6cb402e6f307f467e043aea84a353826 Mon Sep 17 00:00:00 2001 From: Markus Date: Fri, 6 Sep 2024 15:55:07 +0200 Subject: [PATCH] add version number field to mbz, some minor code cleanups --- .../backup_local_adler_plugin.class.php | 75 +++++++++---------- .../restore_local_adler_plugin.class.php | 62 +++++++++------ classes/external/trigger_event_cm_viewed.php | 1 - .../backport_cm_completion_details.php | 9 +-- 4 files changed, 80 insertions(+), 67 deletions(-) diff --git a/backup/moodle2/backup_local_adler_plugin.class.php b/backup/moodle2/backup_local_adler_plugin.class.php index d05a231..4406c68 100644 --- a/backup/moodle2/backup_local_adler_plugin.class.php +++ b/backup/moodle2/backup_local_adler_plugin.class.php @@ -1,44 +1,37 @@ get_plugin_element(); +protected function define_course_plugin_structure(): backup_plugin_element { + $plugin = $this->get_plugin_element(); - // To know if we are including userinfo -// $userinfo = $this->get_setting_value('userinfo'); - - // Define each element separated - $pluginwrapper = new backup_nested_element($this->get_recommended_name()); + // Define each element separated + $pluginwrapper = new backup_nested_element($this->get_recommended_name(), null, [ + 'plugin_set_version', + ]); - // Moodle does not allow names in nested elements that are used in the root element, therefore "score" is not allowed - $score_item = new backup_nested_element("adler_module", null, array( - 'score_max', - 'uuid', - 'timecreated', - 'timemodified', - )); + // Moodle does not allow names in nested elements that are used in the root element, therefore "score" is not allowed + $adler_course = new backup_nested_element("adler_course", null, [ + 'uuid', + ]); - // Build the tree - $plugin->add_child($pluginwrapper); + // Build the tree + $plugin->add_child($pluginwrapper); + $pluginwrapper->add_child($adler_course); - $pluginwrapper->add_child($score_item); + // Define sources + // version number according to https://github.com/ProjektAdLer/moodle-docker/blob/main/plugin-releases.json + $pluginwrapper->set_source_array([['plugin_set_version' => '4.0.0']]); + $adler_course->set_source_table('local_adler_course', array('course_id' => backup::VAR_COURSEID)); + // Define id annotations - // Define sources - $score_item->set_source_table('local_adler_course_modules', array('cmid' => backup::VAR_MODID)); - - // Define id annotations - - // Define file annotations - - // Return the root element, wrapped into standard activity structure - return $plugin; - } + // Define file annotations + // Return the root element, wrapped into standard activity structure + return $plugin; +} protected function define_section_plugin_structure(): backup_plugin_element { $plugin = $this->get_plugin_element(); @@ -67,31 +60,35 @@ protected function define_section_plugin_structure(): backup_plugin_element { return $plugin; } - /** + /** Defines the structure of the backup file when backing up an instance of the local Adler plugin. + * + * @return backup_plugin_element * @throws base_element_struct_exception */ - protected function define_course_plugin_structure(): backup_plugin_element { + protected function define_module_plugin_structure(): backup_plugin_element { $plugin = $this->get_plugin_element(); // To know if we are including userinfo // $userinfo = $this->get_setting_value('userinfo'); // Define each element separated - $pluginwrapper = new backup_nested_element($this->get_recommended_name()); + $pluginwrapper = new backup_nested_element($this->get_recommended_name(), ); // Moodle does not allow names in nested elements that are used in the root element, therefore "score" is not allowed - // For now there is no data to back up. The only relevant information is that the entry exists. - // Because of Moodle logics, empty elements are ignored during restore, so there has to be a dummy field. - $adler_course = new backup_nested_element("adler_course", null, [ + $score_item = new backup_nested_element("adler_module", null, array( + 'score_max', 'uuid', - ]); + 'timecreated', + 'timemodified', + )); // Build the tree $plugin->add_child($pluginwrapper); - $pluginwrapper->add_child($adler_course); + + $pluginwrapper->add_child($score_item); // Define sources - $adler_course->set_source_table('local_adler_course', array('course_id' => backup::VAR_COURSEID)); + $score_item->set_source_table('local_adler_course_modules', array('cmid' => backup::VAR_MODID)); // Define id annotations diff --git a/backup/moodle2/restore_local_adler_plugin.class.php b/backup/moodle2/restore_local_adler_plugin.class.php index afced02..4962f17 100644 --- a/backup/moodle2/restore_local_adler_plugin.class.php +++ b/backup/moodle2/restore_local_adler_plugin.class.php @@ -6,40 +6,42 @@ * Restoring logic for the local Adler plugin. */ class restore_local_adler_plugin extends restore_local_plugin { + // all possible methods: define_course_plugin_structure, define_section_plugin_structure, + // define_module_plugin_structure, define_grade_item_plugin_structure, define_question_plugin_structure protected $db; + private $plugin_set_version; + public function __construct($name, $plugin, $restore) { global $DB; parent::__construct($name, $plugin, $restore); $this->db = $DB; } - /** Defines the structure of the backup file when backing up an instance of the local Adler plugin. - * - * @return restore_path_element[] - */ - protected function define_module_plugin_structure(): array { + protected function define_course_plugin_structure(): array { return [ - new restore_path_element('adler_module', $this->get_pathfor('/adler_module')) + new restore_path_element('plugin_set_version', $this->get_pathfor('/')), + new restore_path_element('adler_course', $this->get_pathfor('/adler_course')) ]; } - /** Processes a score item record during the restore process. - * - * @param object|array $data The data for the score item. Should be of type object, but is sometimes array (moodle logic). - * @return void + public function process_plugin_set_version($data) { + if (isset($data['plugin_set_version']) && version_compare($data['plugin_set_version'], '4.0.0', '<')) { + throw new moodle_exception('invalid_plugin_set_version', 'local_adler', '', null, 'plugin_set_version is below 3.2.0'); + } + $this->plugin_set_version = $data['plugin_set_version']; + } + + /** * @throws dml_exception */ - public function process_adler_module($data) { + public function process_adler_course($data) { global $DB; // Cast $data to object if it is an array // This is required because the object can sometimes randomly be an array $data = (object)$data; - $cmid = $this->task->get_moduleid(); - $data->cmid = $cmid; - // default values for timecreated and timemodified, if they are not set if (!isset($data->timecreated)) { $data->timecreated = time(); @@ -48,10 +50,10 @@ public function process_adler_module($data) { $data->timemodified = time(); } - // The information whether availability is enabled or not is not (easily) available here -> not checking for it + $data->course_id = $this->task->get_courseid(); // Insert the record into the database - $DB->insert_record('local_adler_course_modules', $data); + $DB->insert_record('local_adler_course', $data); } protected function define_section_plugin_structure(): array { @@ -82,22 +84,32 @@ public function process_adler_section($data) { $this->db->insert_record('local_adler_sections', $data); } - protected function define_course_plugin_structure(): array { + /** Defines the structure of the backup file when backing up an instance of the local Adler plugin. + * + * @return restore_path_element[] + */ + protected function define_module_plugin_structure(): array { return [ - new restore_path_element('adler_course', $this->get_pathfor('/adler_course')) + new restore_path_element('adler_module', $this->get_pathfor('/adler_module')) ]; } - /** + /** Processes a score item record during the restore process. + * + * @param object|array $data The data for the score item. Should be of type object, but is sometimes array (moodle logic). + * @return void * @throws dml_exception */ - public function process_adler_course($data) { + public function process_adler_module($data) { global $DB; // Cast $data to object if it is an array // This is required because the object can sometimes randomly be an array $data = (object)$data; + $cmid = $this->task->get_moduleid(); + $data->cmid = $cmid; + // default values for timecreated and timemodified, if they are not set if (!isset($data->timecreated)) { $data->timecreated = time(); @@ -106,10 +118,16 @@ public function process_adler_course($data) { $data->timemodified = time(); } - $data->course_id = $this->task->get_courseid(); + // The information whether availability is enabled or not is not (easily) available here -> not checking for it // Insert the record into the database - $DB->insert_record('local_adler_course', $data); + $DB->insert_record('local_adler_course_modules', $data); + } + + public function after_restore_course() { + if (empty($this->plugin_set_version)) { + // TODO: run migration code + } } } diff --git a/classes/external/trigger_event_cm_viewed.php b/classes/external/trigger_event_cm_viewed.php index e4487c7..2f909a8 100644 --- a/classes/external/trigger_event_cm_viewed.php +++ b/classes/external/trigger_event_cm_viewed.php @@ -19,7 +19,6 @@ use local_adler\local\db\adler_course_module_repository; use local_adler\local\exceptions\not_an_adler_cm_exception; use local_adler\local\exceptions\not_an_adler_course_exception; -use local_logging\logger; use moodle_exception; use restricted_context_exception; diff --git a/classes/local/backport/backport_cm_completion_details.php b/classes/local/backport/backport_cm_completion_details.php index 61c2b4f..901afca 100644 --- a/classes/local/backport/backport_cm_completion_details.php +++ b/classes/local/backport/backport_cm_completion_details.php @@ -6,7 +6,7 @@ use cm_info; use core_completion\cm_completion_details as core_cm_completion_details; -if (get_config('moodle', 'version') < 2023100900) { +if (get_config('moodle', 'version') < 2023100900) { // 2023100900 = Moodle 4.3.0 class backport_cm_completion_details extends core_cm_completion_details { /** * Backport from Moodle 4.4 for Moodle versions below 4.3 @@ -52,12 +52,12 @@ public function is_overall_complete(): bool { /** * From Moodle 4.4 to make backports work, as otherwise the baseclass is returned by this method. - * + * * Generates an instance of this class. * * @param cm_info $cminfo The course module info instance. * @param int $userid The user ID that we're fetching completion details for. - * @param bool $returndetails Whether to return completion details or not. + * @param bool $returndetails Whether to return completion details or not. * @return core_cm_completion_details */ public static function get_instance(cm_info $cminfo, int $userid, bool $returndetails = true): core_cm_completion_details { @@ -67,6 +67,5 @@ public static function get_instance(cm_info $cminfo, int $userid, bool $returnde } } } else { - class backport_cm_completion_details extends core_cm_completion_details { - } + class backport_cm_completion_details extends core_cm_completion_details {} }