Skip to content
Snippets Groups Projects
Commit 2b9f59be authored by Juho Jaakkola's avatar Juho Jaakkola Committed by Frode Petterson
Browse files

Implements preliminary version of Moodle's backup feature

parent 32eced50
No related branches found
No related tags found
No related merge requests found
...@@ -25,11 +25,11 @@ ...@@ -25,11 +25,11 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php');
require_once($CFG->dirroot . '/mod/hvp/backup/moodle2/backup_hvp_stepslib.php'); require_once($CFG->dirroot . '/mod/hvp/backup/moodle2/backup_hvp_stepslib.php');
require_once($CFG->dirroot . '/mod/hvp/backup/moodle2/backup_hvp_settingslib.php');
/** /**
* Provides the steps to perform one complete backup of the Choice instance * Provides the steps to perform one complete backup of a H5P instance
*/ */
class backup_hvp_activity_task extends backup_activity_task { class backup_hvp_activity_task extends backup_activity_task {
...@@ -40,10 +40,10 @@ class backup_hvp_activity_task extends backup_activity_task { ...@@ -40,10 +40,10 @@ class backup_hvp_activity_task extends backup_activity_task {
} }
/** /**
* Defines a backup step to store the instance data in the choice.xml file * Defines a backup step to store the instance data in the hvp.xml file
*/ */
protected function define_my_steps() { protected function define_my_steps() {
// $this->add_step(new backup_hvp_activity_structure_step('hvp_structure', 'hvp.xml')); $this->add_step(new backup_hvp_activity_structure_step('hvp_structure', 'hvp.xml'));
} }
/** /**
......
...@@ -15,68 +15,107 @@ ...@@ -15,68 +15,107 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** /**
* @package moodlecore * @package mod_hvp
* @subpackage backup-moodle2 * @copyright 2016 Mediamaisteri Oy {@link http://www.mediamaisteri.com}
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
require_once($CFG->dirroot . '/backup/moodle2/backup_activity_task.class.php');
/** /**
* Define all the backup steps that will be used by the backup_choice_activity_task * Define all the backup steps that will be used by the backup_hvp_activity_task
*/ */
/** /**
* Define the complete choice structure for backup, with file and id annotations * Define the complete hvp structure for backup, with file and id annotations
*/ */
class backup_hvp_activity_structure_step extends backup_activity_structure_step { class backup_hvp_activity_structure_step extends backup_activity_structure_step {
protected function define_structure() { protected function define_structure() {
/*
// To know if we are including userinfo // To know if we are including userinfo
$userinfo = $this->get_setting_value('userinfo'); $userinfo = $this->get_setting_value('userinfo');
// Define each element separated // Define each element separated
$choice = new backup_nested_element('choice', array('id'), array( $hvp = new backup_nested_element('hvp', array('id'), array(
'name', 'intro', 'introformat', 'publish', 'course',
'showresults', 'display', 'allowupdate', 'showunanswered', 'name',
'limitanswers', 'timeopen', 'timeclose', 'timemodified', 'json_content',
'completionsubmit')); 'embed_type',
'disable',
$options = new backup_nested_element('options'); 'main_library_id',
'content_type',
$option = new backup_nested_element('option', array('id'), array( 'author',
'text', 'maxanswers', 'timemodified')); 'license',
'meta_keywords',
$answers = new backup_nested_element('answers'); 'meta_description',
'filtered',
$answer = new backup_nested_element('answer', array('id'), array( 'slug',
'userid', 'optionid', 'timemodified')); 'timecreated',
'timemodified',
));
$hvp_libraries = new backup_nested_element('libraries');
$hvp_library = new backup_nested_element('library', null, array(
'machine_name',
'major_version',
'minor_version',
'patch_version',
'dependency_type',
'drop_css',
'weight',
));
$content_user_data_entries = new backup_nested_element('content_user_data');
$content_user_data = new backup_nested_element('entry', array('id'), array(
'user_id',
'hvp_id',
'sub_content_id',
'data_id',
'data',
'preloaded',
'delete_on_content_change',
));
// Build the tree // Build the tree
$choice->add_child($options); $hvp->add_child($content_user_data_entries);
$options->add_child($option); $content_user_data_entries->add_child($content_user_data);
$choice->add_child($answers); $hvp->add_child($hvp_libraries);
$answers->add_child($answer); $hvp_libraries->add_child($hvp_library);
// Define sources // Define sources
$choice->set_source_table('choice', array('id' => backup::VAR_ACTIVITYID)); $hvp->set_source_table('hvp', array('id' => backup::VAR_ACTIVITYID));
$option->set_source_table('choice_options', array('choiceid' => backup::VAR_PARENTID), 'id ASC'); $lib_sql = "SELECT
machine_name,
major_version,
minor_version,
patch_version,
dependency_type,
drop_css,
weight
FROM {hvp_libraries} hl
JOIN {hvp_contents_libraries} hcl
ON hl.id = hcl.library_id
JOIN {hvp} h
ON h.id = hcl.hvp_id
WHERE h.id = ?";
$hvp_library->set_source_sql($lib_sql, array(backup::VAR_ACTIVITYID));
// All the rest of elements only happen if we are including user info // All the rest of elements only happen if we are including user info
if ($userinfo) { if ($userinfo) {
$answer->set_source_table('choice_answers', array('choiceid' => '../../id')); $content_user_data->set_source_table('hvp_content_user_data', array('hvp_id' => backup::VAR_PARENTID), 'id ASC');
} }
// Define id annotations // Define id annotations
$answer->annotate_ids('user', 'userid'); $content_user_data->annotate_ids('user', 'user_id');
// Define file annotations // Define file annotations
$choice->annotate_files('mod_choice', 'intro', null); // This file area hasn't itemid $hvp->annotate_files('mod_hvp', 'content', null);
// Return the root element (choice), wrapped into standard activity structure // Return the root element (hvp), wrapped into standard activity structure
return $this->prepare_activity_structure($choice); return $this->prepare_activity_structure($hvp);
*/
} }
} }
...@@ -15,9 +15,8 @@ ...@@ -15,9 +15,8 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** /**
* @package moodlecore * @package mod_hvp
* @subpackage backup-moodle2 * @copyright 2016 Mediamaisteri Oy {@link http://www.mediamaisteri.com}
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
...@@ -26,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); ...@@ -26,7 +25,7 @@ defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/hvp/backup/moodle2/restore_hvp_stepslib.php'); // Because it exists (must). require_once($CFG->dirroot . '/mod/hvp/backup/moodle2/restore_hvp_stepslib.php'); // Because it exists (must).
/** /**
* choice restore task that provides all the settings and steps to perform one * hvp restore task that provides all the settings and steps to perform one
* complete restore of the activity * complete restore of the activity
*/ */
class restore_hvp_activity_task extends restore_activity_task { class restore_hvp_activity_task extends restore_activity_task {
...@@ -42,8 +41,7 @@ class restore_hvp_activity_task extends restore_activity_task { ...@@ -42,8 +41,7 @@ class restore_hvp_activity_task extends restore_activity_task {
* Define (add) particular steps this activity can have * Define (add) particular steps this activity can have
*/ */
protected function define_my_steps() { protected function define_my_steps() {
// Choice only has one structure step. $this->add_step(new restore_hvp_activity_structure_step('hvp_structure', 'hvp.xml'));
// $this->add_step(new restore_choice_activity_structure_step('choice_structure', 'choice.xml'));
} }
/** /**
...@@ -53,7 +51,7 @@ class restore_hvp_activity_task extends restore_activity_task { ...@@ -53,7 +51,7 @@ class restore_hvp_activity_task extends restore_activity_task {
static public function define_decode_contents() { static public function define_decode_contents() {
$contents = array(); $contents = array();
// $contents[] = new restore_decode_content('choice', array('intro'), 'choice'); //$contents[] = new restore_decode_content('hvp', array('intro'), 'hvp');
return $contents; return $contents;
} }
...@@ -65,29 +63,30 @@ class restore_hvp_activity_task extends restore_activity_task { ...@@ -65,29 +63,30 @@ class restore_hvp_activity_task extends restore_activity_task {
static public function define_decode_rules() { static public function define_decode_rules() {
$rules = array(); $rules = array();
// $rules[] = new restore_decode_rule('CHOICEVIEWBYID', '/mod/choice/view.php?id=$1', 'course_module'); //$rules[] = new restore_decode_rule('CHOICEVIEWBYID', '/mod/hvp/view.php?id=$1', 'course_module');
// $rules[] = new restore_decode_rule('CHOICEINDEX', '/mod/choice/index.php?id=$1', 'course'); //$rules[] = new restore_decode_rule('CHOICEINDEX', '/mod/hvp/index.php?id=$1', 'course');
return $rules; return $rules;
} }
/** /**
* Define the restore log rules that will be applied * Define the restore log rules that will be applied
* by the {@link restore_logs_processor} when restoring * by the {@link restore_logs_processor} when restoring
* choice logs. It must return one array * hvp logs. It must return one array
* of {@link restore_log_rule} objects * of {@link restore_log_rule} objects
*/ */
static public function define_restore_log_rules() { static public function define_restore_log_rules() {
$rules = array(); $rules = array();
/* /*
$rules[] = new restore_log_rule('choice', 'add', 'view.php?id={course_module}', '{choice}'); $rules[] = new restore_log_rule('hvp', 'add', 'view.php?id={course_module}', '{hvp}');
$rules[] = new restore_log_rule('choice', 'update', 'view.php?id={course_module}', '{choice}'); $rules[] = new restore_log_rule('hvp', 'update', 'view.php?id={course_module}', '{hvp}');
$rules[] = new restore_log_rule('choice', 'view', 'view.php?id={course_module}', '{choice}'); $rules[] = new restore_log_rule('hvp', 'view', 'view.php?id={course_module}', '{hvp}');
$rules[] = new restore_log_rule('choice', 'choose', 'view.php?id={course_module}', '{choice}'); $rules[] = new restore_log_rule('hvp', 'choose', 'view.php?id={course_module}', '{hvp}');
$rules[] = new restore_log_rule('choice', 'choose again', 'view.php?id={course_module}', '{choice}'); $rules[] = new restore_log_rule('hvp', 'choose again', 'view.php?id={course_module}', '{hvp}');
$rules[] = new restore_log_rule('choice', 'report', 'report.php?id={course_module}', '{choice}'); $rules[] = new restore_log_rule('hvp', 'report', 'report.php?id={course_module}', '{hvp}');
*/ */
return $rules; return $rules;
} }
...@@ -105,10 +104,11 @@ class restore_hvp_activity_task extends restore_activity_task { ...@@ -105,10 +104,11 @@ class restore_hvp_activity_task extends restore_activity_task {
$rules = array(); $rules = array();
/* /*
// Fix old wrong uses (missing extension) // Fix old wrong uses (missing extension)
$rules[] = new restore_log_rule('choice', 'view all', 'index?id={course}', null, $rules[] = new restore_log_rule('hvp', 'view all', 'index?id={course}', null,
null, null, 'index.php?id={course}'); null, null, 'index.php?id={course}');
$rules[] = new restore_log_rule('choice', 'view all', 'index.php?id={course}', null); $rules[] = new restore_log_rule('hvp', 'view all', 'index.php?id={course}', null);
*/ */
return $rules; return $rules;
} }
} }
...@@ -15,84 +15,107 @@ ...@@ -15,84 +15,107 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/** /**
* @package moodlecore * @package mod_hvp
* @subpackage backup-moodle2 * @copyright 2016 Mediamaisteri Oy {@link http://www.mediamaisteri.com}
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
/** /**
* Define all the restore steps that will be used by the restore_choice_activity_task * Define all the restore steps that will be used by the restore_hvp_activity_task
*/ */
/** /**
* Structure step to restore one choice activity * Structure step to restore one H5P activity
*/ */
class restore_hvp_activity_structure_step extends restore_activity_structure_step { class restore_hvp_activity_structure_step extends restore_activity_structure_step {
/*
protected function define_structure() { protected function define_structure() {
$paths = array(); $paths = array();
$userinfo = $this->get_setting_value('userinfo'); $userinfo = $this->get_setting_value('userinfo');
$paths[] = new restore_path_element('choice', '/activity/choice'); $paths[] = new restore_path_element('hvp', '/activity/hvp');
$paths[] = new restore_path_element('choice_option', '/activity/choice/options/option'); $paths[] = new restore_path_element('library', '/activity/hvp/libraries/library');
if ($userinfo) { if ($userinfo) {
$paths[] = new restore_path_element('choice_answer', '/activity/choice/answers/answer'); $paths[] = new restore_path_element('content_user_data', '/activity/hvp/content_user_data/entry');
} }
// Return the paths wrapped into standard activity structure // Return the paths wrapped into standard activity structure
return $this->prepare_activity_structure($paths); return $this->prepare_activity_structure($paths);
} }
protected function process_choice($data) { protected function process_hvp($data) {
global $DB; global $DB;
$data = (object)$data; $data = (object)$data;
$oldid = $data->id; $oldid = $data->id;
$data->course = $this->get_courseid(); $data->course = $this->get_courseid();
$data->timeopen = $this->apply_date_offset($data->timeopen); $data->timecreated = $this->apply_date_offset($data->timecreated);
$data->timeclose = $this->apply_date_offset($data->timeclose);
$data->timemodified = $this->apply_date_offset($data->timemodified); $data->timemodified = $this->apply_date_offset($data->timemodified);
// insert the choice record // insert the hvp record
$newitemid = $DB->insert_record('choice', $data); $newitemid = $DB->insert_record('hvp', $data);
// immediately after inserting "activity" record, call this // immediately after inserting "activity" record, call this
$this->apply_activity_instance($newitemid); $this->apply_activity_instance($newitemid);
} }
protected function process_choice_option($data) { /**
* Connects the restored H5P content to content libraries
*
* This assumes that the libraries have already been installed
* into the system.
*
* @param array $data Library dependency defitinions of the content
*/
protected function process_library($data) {
global $DB; global $DB;
$data = (object)$data; $data = (object)$data;
$oldid = $data->id; //$oldid = $data->id;
$data->choiceid = $this->get_new_parentid('choice'); // TODO Cache a list of existing libraries somewhere, so we won't
$data->timemodified = $this->apply_date_offset($data->timemodified); // have to check them multiple times in case restoring more than
// one H5P activity.
$newitemid = $DB->insert_record('choice_options', $data); $library = $DB->get_record('hvp_libraries', array(
$this->set_mapping('choice_option', $oldid, $newitemid); 'machine_name' => $data->machine_name,
'major_version' => $data->major_version,
'minor_version' => $data->minor_version,
'patch_version' => $data->patch_version,
));
if (empty($library)) {
// TODO What to do now? It isn't possible to continue restoring
// the activity without the needed libraries.
print_error("Your system is missing the library {$data->machine_name} version {$data->major_version}.{$data->minor_version}.{$data->patch_version}");
} }
protected function process_choice_answer($data) { $content_library = (object) array(
global $DB; 'id' => null,
'hvp_id' => $this->get_new_parentid('hvp'), // From hvp table
$data = (object)$data; 'library_id' => $library->id, // From hvp_libraries table
'dependency_type' => $data->dependency_type,
'drop_css' => $data->drop_css,
'weight' => $data->weight,
);
// Note that we're not in fact adding a new library but a connection
// between the restored H5P activity and an existing library.
$newitemid = $DB->insert_record('hvp_contents_libraries', $content_library);
// TODO
// $this->set_mapping('hvp_todo', $oldid, $newitemid);
}
$data->choiceid = $this->get_new_parentid('choice'); protected function process_content_user_data($data) {
$data->optionid = $this->get_mappingid('choice_option', $data->optionid);
$data->userid = $this->get_mappingid('user', $data->userid);
$data->timemodified = $this->apply_date_offset($data->timemodified);
$newitemid = $DB->insert_record('choice_answers', $data);
// No need to save this mapping as far as nothing depend on it
// (child paths, file areas nor links decoder)
} }
protected function after_execute() { protected function after_execute() {
// Add choice related files, no need to match by itemname (just internally handled context) // TODO
$this->add_related_files('mod_choice', 'intro', null); // Add hvp related files, no need to match by itemname (just internally handled context)
$this->add_related_files('mod_hvp', 'content', 'id');
} }
*/
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment