Permalink
Browse files

MDL-22146 backup & restore - course formats first cut

  • Loading branch information...
1 parent d911c72 commit 4fda584f2831fabd5e4bc87ff420f427f71eeb10 @stronk7 stronk7 committed Feb 3, 2011
@@ -0,0 +1,33 @@
+<?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 moodlecore
+ * @subpackage backup-moodle2
+ * @copyright 2011 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Class extending standard backup_plugin in order to implement some
+ * helper methods related with the course formats (format plugin)
+ *
+ * TODO: Finish phpdocs
+ */
+abstract class backup_format_plugin extends backup_plugin {
+ // Love these classes. :-) Nothing special to customize here for now
+}
@@ -34,6 +34,7 @@
require_once($CFG->dirroot . '/backup/moodle2/backup_xml_transformer.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
+require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php');
@@ -395,6 +395,9 @@ protected function define_structure() {
$module = new backup_nested_element('module', array(), array('modulename'));
+ // attach format plugin structure to $course element, only one allowed
+ $this->add_plugin_structure('format', $course, false);
+
// Build the tree
$course->add_child($category);
@@ -0,0 +1,33 @@
+<?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 moodlecore
+ * @subpackage backup-moodle2
+ * @copyright 2011 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Class extending standard restore_plugin in order to implement some
+ * helper methods related with the course formats (format plugin)
+ *
+ * TODO: Finish phpdocs
+ */
+abstract class restore_format_plugin extends restore_plugin {
+ // Love these classes. :-) Nothing special to customize here for now
+}
@@ -33,8 +33,10 @@
require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_plugin.class.php');
+require_once($CFG->dirroot . '/backup/moodle2/restore_format_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
+require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
@@ -991,12 +991,15 @@ class restore_course_structure_step extends restore_structure_step {
protected function define_structure() {
- $course = new restore_path_element('course', '/course', true); // Grouped
+ $course = new restore_path_element('course', '/course');
$category = new restore_path_element('category', '/course/category');
$tag = new restore_path_element('tag', '/course/tags/tag');
- $allowed = new restore_path_element('allowed', '/course/allowed_modules/module');
+ $allowed_module = new restore_path_element('allowed_module', '/course/allowed_modules/module');
- return array($course, $category, $tag, $allowed);
+ // Apply for 'format' plugins optional paths at course level
+ $this->add_plugin_structure('format', $course);
+
+ return array($course, $category, $tag, $allowed_module);
}
/**
@@ -1026,9 +1029,6 @@ public function process_course($data) {
$data->shortname= $shortname;
$data->idnumber = '';
- // Category is set by UI when choosing the destination
- unset($data->category);
-
$data->startdate= $this->apply_date_offset($data->startdate);
if ($data->defaultgroupingid) {
$data->defaultgroupingid = $this->get_mappingid('grouping', $data->defaultgroupingid);
@@ -1050,34 +1050,56 @@ public function process_course($data) {
// Course record ready, update it
$DB->update_record('course', $data);
- // Course tags
- if (!empty($CFG->usetags) && isset($coursetags)) { // if enabled in server and present in backup
+ // Role name aliases
+ restore_dbops::set_course_role_names($this->get_restoreid(), $this->get_courseid());
+ }
+
+ public function process_category($data) {
+ // Nothing to do with the category. UI sets it before restore starts
+ }
+
+ public function process_tag($data) {
+ global $CFG, $DB;
+
+ $data = (object)$data;
+
+ if (!empty($CFG->usetags)) { // if enabled in server
+ // TODO: This is highly inneficient. Each time we add one tag
+ // we fetch all the existing because tag_set() deletes them
+ // so everything must be reinserted on each call
$tags = array();
- foreach ($coursetags as $coursetag) {
- $coursetag = (object)$coursetag;
- $tags[] = $coursetag->rawname;
+ $existingtags = tag_get_tags('course', $this->get_courseid());
+ // Re-add all the existitng tags
+ foreach ($existingtags as $existingtag) {
+ $tags[] = $existingtag->rawname;
}
+ // Add the one being restored
+ $tags[] = $data->rawname;
+ // Send all the tags back to the course
tag_set('course', $this->get_courseid(), $tags);
}
- // Course allowed modules
- if (!empty($data->restrictmodules) && !empty($coursemodules)) {
+ }
+
+ public function process_allowed_module($data) {
+ global $CFG, $DB;
+
+ $data = (object)$data;
+
+ // only if enabled by admin setting
+ if (!empty($CFG->restrictmodulesfor) && $CFG->restrictmodulesfor == 'all') {
$available = get_plugin_list('mod');
- foreach ($coursemodules as $coursemodule) {
- $mname = $coursemodule['modulename'];
- if (array_key_exists($mname, $available)) {
- if ($module = $DB->get_record('modules', array('name' => $mname, 'visible' => 1))) {
- $rec = new stdclass();
- $rec->course = $this->get_courseid();
- $rec->module = $module->id;
- if (!$DB->record_exists('course_allowed_modules', (array)$rec)) {
- $DB->insert_record('course_allowed_modules', $rec);
- }
+ $mname = $data->modulename;
+ if (array_key_exists($mname, $available)) {
+ if ($module = $DB->get_record('modules', array('name' => $mname, 'visible' => 1))) {
+ $rec = new stdclass();
+ $rec->course = $this->get_courseid();
+ $rec->module = $module->id;
+ if (!$DB->record_exists('course_allowed_modules', (array)$rec)) {
+ $DB->insert_record('course_allowed_modules', $rec);
}
}
}
}
- // Role name aliases
- restore_dbops::set_course_role_names($this->get_restoreid(), $this->get_courseid());
}
protected function after_execute() {

0 comments on commit 4fda584

Please sign in to comment.