Skip to content

Commit

Permalink
add version number field to mbz, some minor code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Sep 6, 2024
1 parent 53f745f commit a3efd16
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 67 deletions.
75 changes: 36 additions & 39 deletions backup/moodle2/backup_local_adler_plugin.class.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
<?php

class backup_local_adler_plugin extends backup_local_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_module_plugin_structure(): backup_plugin_element {
$plugin = $this->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();

Expand Down Expand Up @@ -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

Expand Down
62 changes: 40 additions & 22 deletions backup/moodle2/restore_local_adler_plugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -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
}
}
}

1 change: 0 additions & 1 deletion classes/external/trigger_event_cm_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 4 additions & 5 deletions classes/local/backport/backport_cm_completion_details.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {}
}

0 comments on commit a3efd16

Please sign in to comment.