From 91a7a7884b1c1e7c6a67dc33f018857be0591659 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Tue, 30 Nov 2021 11:20:49 +0100 Subject: [PATCH] Backups --- .../backup_expcontent_activity_task.class.php | 70 ++++++++++++ backup/moodle2/backup_expcontent_stepslib.php | 53 +++++++++ ...restore_expcontent_activity_task.class.php | 101 ++++++++++++++++++ .../moodle2/restore_expcontent_stepslib.php | 73 +++++++++++++ lib.php | 1 + version.php | 2 +- 6 files changed, 299 insertions(+), 1 deletion(-) create mode 100644 backup/moodle2/backup_expcontent_activity_task.class.php create mode 100644 backup/moodle2/backup_expcontent_stepslib.php create mode 100644 backup/moodle2/restore_expcontent_activity_task.class.php create mode 100644 backup/moodle2/restore_expcontent_stepslib.php diff --git a/backup/moodle2/backup_expcontent_activity_task.class.php b/backup/moodle2/backup_expcontent_activity_task.class.php new file mode 100644 index 0000000..10732b3 --- /dev/null +++ b/backup/moodle2/backup_expcontent_activity_task.class.php @@ -0,0 +1,70 @@ +. + +/** + * This file contains the backup tasks that provides all the settings and steps to perform a backup of the activity. + * + * @package mod_expcontent + * @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); + +require_once($CFG->dirroot . '/mod/expcontent/backup/moodle2/backup_expcontent_stepslib.php'); + +/** + * Handles creating tasks to peform in order to create the backup. + * + * @package mod_expcontent + * @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class backup_expcontent_activity_task extends backup_activity_task { + + /** + * Define particular settings this activity can have. + */ + protected function define_my_settings() { + // No particular settings for this activity. + } + + /** + * Define particular steps this activity can have. + */ + protected function define_my_steps() { + // The expcontent only has one structure step. + $this->add_step(new backup_expcontent_activity_structure_step('expcontent_structure', 'expcontent.xml')); + } + + /** + * Code the transformations to perform in the activity in order to get transportable (encoded) links. + * + * @param string $content + * @return mixed|string + */ + public static function encode_content_links($content) { + global $CFG; + + $base = preg_quote($CFG->wwwroot, "/"); + + // Link to expcontent view by moduleid. + $search = "/(".$base."\/mod\/expcontent\/view.php\?id\=)([0-9]+)/"; + $content = preg_replace($search, '$@EXPCONTENTVIEWBYID*$2@$', $content); + + return $content; + } +} diff --git a/backup/moodle2/backup_expcontent_stepslib.php b/backup/moodle2/backup_expcontent_stepslib.php new file mode 100644 index 0000000..af55474 --- /dev/null +++ b/backup/moodle2/backup_expcontent_stepslib.php @@ -0,0 +1,53 @@ +. + +/** + * Define all the backup steps that will be used by the backup_expcontent_activity_task. + * + * @package mod_expcontent + * @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); + +/** + * Define the complete expcontent structure for backup, with file and id annotations. + * + * @package mod_expcontent + * @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class backup_expcontent_activity_structure_step extends backup_activity_structure_step { + + /** + * Define the structure of the backup file. + * + * @return backup_nested_element + */ + protected function define_structure() { + + // The instance. + $expcontent = new backup_nested_element('expcontent', array('id'), array( + 'name', 'contentid', 'intro', 'introformat')); + + // Define sources. + $expcontent->set_source_table('expcontent', array('id' => backup::VAR_ACTIVITYID)); + + // Return the root element (expcontent), wrapped into standard activity structure. + return $this->prepare_activity_structure($expcontent); + } +} diff --git a/backup/moodle2/restore_expcontent_activity_task.class.php b/backup/moodle2/restore_expcontent_activity_task.class.php new file mode 100644 index 0000000..a866cdc --- /dev/null +++ b/backup/moodle2/restore_expcontent_activity_task.class.php @@ -0,0 +1,101 @@ +. + +/** + * Define all the restore steps that will be used by the restore_expcontent_activity_task. + * + * @package mod_expcontent + * @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); + +require_once($CFG->dirroot . '/mod/expcontent/backup/moodle2/restore_expcontent_stepslib.php'); + +/** + * The class definition for assigning tasks that provide the settings and steps to perform a restore of the activity. + * + * @package mod_expcontent + * @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class restore_expcontent_activity_task extends restore_activity_task { + + /** + * Define particular settings this activity can have. + */ + protected function define_my_settings() { + // No particular settings for this activity. + } + + /** + * Define particular steps this activity can have. + */ + protected function define_my_steps() { + // The expcontent only has one structure step. + $this->add_step(new restore_expcontent_activity_structure_step('expcontent_structure', 'expcontent.xml')); + } + + /** + * Define the contents in the activity that must be processed by the link decoder. + */ + public static function define_decode_contents() { + $contents = array(); + + $contents[] = new restore_decode_content('expcontent', array('intro'), 'expcontent'); + + return $contents; + } + + /** + * Define the decoding rules for links belonging to the activity to be executed by the link decoder. + */ + public static function define_decode_rules() { + $rules = array(); + + $rules[] = new restore_decode_rule('EXPCONTENTVIEWBYID', '/mod/expcontent/view.php?id=$1', 'course_module'); + + return $rules; + + } + + /** + * Define the restore log rules that will be applied by the {@see restore_logs_processor} when restoring + * expcontent logs. It must return one array of {@see restore_log_rule} objects. + * + * @return array the restore log rules + */ + public static function define_restore_log_rules() { + $rules = array(); + + $rules[] = new restore_log_rule('expcontent', 'add', 'view.php?id={course_module}', '{expcontent}'); + $rules[] = new restore_log_rule('expcontent', 'update', 'view.php?id={course_module}', '{expcontent}'); + $rules[] = new restore_log_rule('expcontent', 'view', 'view.php?id={course_module}', '{expcontent}'); + $rules[] = new restore_log_rule('expcontent', 'view report', 'view.php?id={course_module}', '{expcontent}'); + + return $rules; + } + + /** + * This function is called after all the activities in the backup have been restored. This allows us to get + * the new course module ids, as they may have been restored after the expcontent module, meaning no id + * was available at the time. + */ + public function after_restore() { + global $DB; + } +} diff --git a/backup/moodle2/restore_expcontent_stepslib.php b/backup/moodle2/restore_expcontent_stepslib.php new file mode 100644 index 0000000..7c844cd --- /dev/null +++ b/backup/moodle2/restore_expcontent_stepslib.php @@ -0,0 +1,73 @@ +. + +/** + * Define all the restore steps that will be used by the restore_expcontent_activity_task. + * + * @package mod_expcontent + * @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.'); + +/** + * Define the complete expcontent structure for restore, with file and id annotations. + * + * @package mod_expcontent + * @copyright 2013 Mark Nelson , 2021 Klaus-Uwe Mitterer + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class restore_expcontent_activity_structure_step extends restore_activity_structure_step { + + /** + * Define the different items to restore. + * + * @return array the restore paths + */ + protected function define_structure() { + // The array used to store the path to the items we want to restore. + $paths = array(); + + // The htmlcert instance. + $paths[] = new restore_path_element('expcontent', '/activity/expcontent'); + + // Return the paths wrapped into standard activity structure. + return $this->prepare_activity_structure($paths); + } + + /** + * Handles restoring the htmlcert activity. + * + * @param stdClass $data the htmlcert data + */ + protected function process_expcontent($data) { + global $DB; + + $data = (object) $data; + $data->course = $this->get_courseid(); + $data->timecreated = $this->apply_date_offset($data->timecreated); + $data->timemodified = $this->apply_date_offset($data->timemodified); + $data->usermodified = $USER->id; + + // Insert the htmlcert record. + $newitemid = $DB->insert_record('expcontent', $data); + + // Immediately after inserting record call this. + $this->apply_activity_instance($newitemid); + } + +} diff --git a/lib.php b/lib.php index c078e8b..2ca2ba4 100644 --- a/lib.php +++ b/lib.php @@ -42,6 +42,7 @@ function expcontent_supports($feature) { switch($feature) { case FEATURE_COMPLETION_TRACKS_VIEWS: return true; case FEATRUE_SHOW_COMPLETION: return true; + case FEATURE_BACKUP_MOODLE2: return true; default: return null; } } diff --git a/version.php b/version.php index 65831a8..446baab 100644 --- a/version.php +++ b/version.php @@ -2,7 +2,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = "2021113000"; +$plugin->version = "2021113001"; $plugin->component = 'mod_expcontent'; $plugin->maturity = MATURITY_ALPHA; $plugin->release = 'v0.0.1';