Skip to content

Commit

Permalink
MDL-22146 backup & restore - course formats first cut
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Feb 10, 2011
1 parent d911c72 commit 4fda584
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 25 deletions.
33 changes: 33 additions & 0 deletions backup/moodle2/backup_format_plugin.class.php
@@ -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
}
1 change: 1 addition & 0 deletions backup/moodle2/backup_plan_builder.class.php
Expand Up @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions backup/moodle2/backup_stepslib.php
Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions backup/moodle2/restore_format_plugin.class.php
@@ -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
}
2 changes: 2 additions & 0 deletions backup/moodle2/restore_plan_builder.class.php
Expand Up @@ -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');
Expand Down
72 changes: 47 additions & 25 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand All @@ -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() {
Expand Down

0 comments on commit 4fda584

Please sign in to comment.