Skip to content

Commit

Permalink
v2021062500
Browse files Browse the repository at this point in the history
Internal name "V3"

Main change: Enable Backup/Restore
  • Loading branch information
Thomas Chapeaux committed Jun 25, 2021
1 parent 9aaca83 commit 1b31bbe
Show file tree
Hide file tree
Showing 15 changed files with 635 additions and 98 deletions.
67 changes: 67 additions & 0 deletions backup/moodle2/backup_wooclap_activity_task.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package mod_wooclap
* @copyright 2018 CBlue sprl
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once __DIR__ . '/../../../../config.php';

require_once $CFG->dirroot . '/mod/wooclap/backup/moodle2/backup_wooclap_stepslib.php'; // Because it exists (must)

/**
* wooclap backup task that provides all the settings and steps to perform one
* complete backup of the activity
*/
class backup_wooclap_activity_task extends backup_activity_task {

/**
* Define (add) particular settings this activity can have
*/
protected function define_my_settings() {
// No particular settings for this activity.
}

/**
* Define (add) particular steps this activity can have
*/
protected function define_my_steps() {
// Wooclap only has one structure step.
$this->add_step(new backup_wooclap_activity_structure_step('wooclap_structure', 'wooclap.xml'));
}

/**
* Code the transformations to perform in the activity in
* order to get transportable (encoded) links
*/
static public function encode_content_links($content) {
global $CFG;

$base = preg_quote($CFG->wwwroot,"/");

// Link to the list of wooclap activities
$search="/(".$base."\/mod\/wooclap\/index.php\?id\=)([0-9]+)/";
$content= preg_replace($search, '$@WOOCLAPINDEX*$2@$', $content);

// Link to wooclap view by moduleid
$search="/(".$base."\/mod\/wooclap\/view.php\?id\=)([0-9]+)/";
$content= preg_replace($search, '$@WOOCLAPVIEWBYID*$2@$', $content);

return $content;
}
}
76 changes: 76 additions & 0 deletions backup/moodle2/backup_wooclap_stepslib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package mod_wooclap
* @copyright 2018 CBlue sprl
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once __DIR__ . '/../../../../config.php';

/**
* Define all the backup steps that will be used by the backup_wooclap_activity_task
*/

/**
* Define the complete wooclap structure for backup, with file and id annotations
*/
class backup_wooclap_activity_structure_step extends backup_activity_structure_step {

protected function define_structure() {

// To know if we are including userinfo.
$userinfo = $this->get_setting_value('userinfo');

// Define each element separated.
$wooclap = new backup_nested_element('wooclap', array('id'), array(
/*"course",*/"name", "intro",
"introformat", "editurl", "quiz",
"authorid", "customcompletion", "timecreated",
"timemodified", "wooclapeventid",
"linkedwooclapeventslug",
));

$completions = new backup_nested_element('completions');

$completion = new backup_nested_element('completion', array('id'), array(
"wooclapid", "userid", "completionstatus", "grade", "timecreated", "timemodified"));

// Build the tree.
$wooclap->add_child($completions);
$completions->add_child($completion);

// Define sources.
$wooclap->set_source_table('wooclap', array('id' => backup::VAR_ACTIVITYID));

// All the rest of elements only happen if we are including user info.
if ($userinfo) {
$completion->set_source_table('wooclap_completion', array('wooclapid' => backup::VAR_PARENTID));
}

// Define id annotations.
$wooclap->annotate_ids('user', 'authorid');
$completion->annotate_ids('user', 'userid');

// Define file annotations.
$wooclap->annotate_files('mod_wooclap', 'intro', null, $contextid = null); // This file area does not have an itemid.

// Return the root element (wooclap), wrapped into standard activity structure.
return $this->prepare_activity_structure($wooclap);

}
}
109 changes: 109 additions & 0 deletions backup/moodle2/restore_wooclap_activity_task.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package mod_wooclap
* @copyright 2018 CBlue sprl
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once __DIR__ . '/../../../../config.php';

/**
* wooclap restore task that provides all the settings and steps to perform one
* complete restore of the activity
*/

require_once $CFG->dirroot . '/mod/wooclap/backup/moodle2/restore_wooclap_stepslib.php'; // Because it exists (must)

class restore_wooclap_activity_task extends restore_activity_task {

/**
* Define (add) particular settings this activity can have
*/
protected function define_my_settings() {
// No particular settings for this activity
}

/**
* Define (add) particular steps this activity can have
*/
protected function define_my_steps() {
// Wooclap only has one structure step
$this->add_step(new restore_wooclap_activity_structure_step('wooclap_structure', 'wooclap.xml'));
}

/**
* Define the contents in the activity that must be
* processed by the link decoder
*/
static public function define_decode_contents() {
$contents = array();

$contents[] = new restore_decode_content('wooclap', array('intro'), 'wooclap');

return $contents;
}

/**
* Define the decoding rules for links belonging
* to the activity to be executed by the link decoder
*/
static public function define_decode_rules() {
$rules = array();

$rules[] = new restore_decode_rule('WOOCLAPVIEWBYID', '/mod/wooclap/view.php?id=$1', 'course_module');
$rules[] = new restore_decode_rule('WOOCLAPINDEX', '/mod/wooclap/index.php?id=$1', 'course');

return $rules;

}

/**
* Define the restore log rules that will be applied
* by the {@link restore_logs_processor} when restoring
* wooclap logs. It must return one array
* of {@link restore_log_rule} objects
*/
static public function define_restore_log_rules() {
$rules = array();

$rules[] = new restore_log_rule('wooclap', 'add', 'view.php?id={course_module}', '{wooclap}');
$rules[] = new restore_log_rule('wooclap', 'update', 'view.php?id={course_module}', '{wooclap}');
$rules[] = new restore_log_rule('wooclap', 'view', 'view.php?id={course_module}', '{wooclap}');
$rules[] = new restore_log_rule('wooclap', 'choose', 'view.php?id={course_module}', '{wooclap}');
$rules[] = new restore_log_rule('wooclap', 'choose again', 'view.php?id={course_module}', '{wooclap}');
$rules[] = new restore_log_rule('wooclap', 'report', 'report.php?id={course_module}', '{wooclap}');

return $rules;
}

/**
* Define the restore log rules that will be applied
* by the {@link restore_logs_processor} when restoring
* course logs. It must return one array
* of {@link restore_log_rule} objects
*
* Note this rules are applied when restoring course logs
* by the restore final task, but are defined here at
* activity level. All them are rules not linked to any module instance (cmid = 0)
*/
static public function define_restore_log_rules_for_course() {
$rules = array();
return $rules;
}

}
72 changes: 72 additions & 0 deletions backup/moodle2/restore_wooclap_stepslib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package mod_wooclap
* @copyright 2018 CBlue sprl
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require_once __DIR__ . '/../../../../config.php';

/**
* Structure step to restore one wooclap activity
*/
class restore_wooclap_activity_structure_step extends restore_activity_structure_step {

protected function define_structure() {

$paths = array();
$userinfo = $this->get_setting_value('userinfo');

$paths[] = new restore_path_element('wooclap', '/activity/wooclap');
if ($userinfo) {
$paths[] = new restore_path_element('wooclap_completion', '/activity/wooclap/completions/completion');
}

// Return the paths wrapped into standard activity structure.
return $this->prepare_activity_structure($paths);
}

protected function process_wooclap($data) {
global $DB;

$data = (object) $data;
$data->course = $this->get_courseid();

// Insert the wooclap record.
$newitemid = $DB->insert_record('wooclap', $data);
// Immediately after inserting "activity" record, call this.
$this->apply_activity_instance($newitemid);
}

protected function process_wooclap_completion($data) {
global $DB;

$data = (object) $data;
$oldid = $data->id;

$data->wooclapid = $this->get_new_parentid('wooclap');

$newitemid = $DB->insert_record('wooclap_completion', $data);
$this->set_mapping('wooclap_completion', $oldid, $newitemid);
}

protected function after_execute() {
// Add choice related files, no need to match by itemname (just internally handled context).
$this->add_related_files('mod_wooclap', 'intro', null);
}
}
Loading

0 comments on commit 1b31bbe

Please sign in to comment.