Permalink
Browse files

backup MDL-26606 Backup should allow themes to have course data; also…

… fix course theme restore
  • Loading branch information...
1 parent 48fe5c6 commit 89213d00419a600f54c560fd19733cf0389e37b3 @sammarshallou sammarshallou committed Mar 1, 2011
@@ -35,6 +35,7 @@
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_theme_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');
@@ -412,6 +412,10 @@ protected function define_structure() {
// attach format plugin structure to $course element, only one allowed
$this->add_plugin_structure('format', $course, false);
+ // attach theme plugin structure to $course element; multiple themes can
+ // save course data (in case of user theme, legacy theme, etc)
+ $this->add_plugin_structure('theme', $course, true);
+
// Build the tree
$course->add_child($category);
@@ -0,0 +1,60 @@
+<?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 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Base class for theme backup plugins.
+ *
+ * NOTE: When you back up a course, it runs backup for ALL themes - not just
+ * the currently selected one.
+ *
+ * That means that if, for example, a course was once in theme A, and theme A
+ * had some data settings, but it is then changed to theme B, the data settings
+ * will still be included in the backup and restore. With the restored course,
+ * if you ever change it back to theme A, the settings will be ready.
+ *
+ * It also means that other themes which are not the one set up for the course,
+ * but might be seen by some users (eg user themes, session themes, mnet themes)
+ * can store data.
+ *
+ * If this behaviour is not desired for a particular theme's data, the subclass
+ * can call is_current_theme('myname') to check.
+ */
+abstract class backup_theme_plugin extends backup_plugin {
+
+ // Stores current theme for course. Lazily initialised.
+ protected $theme;
+
+ /**
+ * Checks if an expected theme is actually the current theme for the course.
+ * @param string $expected Name of theme you are expecting e.g. 'standard'
+ * @return bool True if that theme is the current course theme
+ */
+ protected function is_current_theme($expected) {
+ if (!$this->theme) {
+ $this->theme = backup_plan_dbops::get_theme_from_courseid(
+ $this->task->get_courseid());
+ }
+ return $this->theme === $expected;
+ }
+}
@@ -34,9 +34,11 @@
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/restore_theme_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/backup_theme_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');
@@ -1008,6 +1008,9 @@ protected function define_structure() {
// Apply for 'format' plugins optional paths at course level
$this->add_plugin_structure('format', $course);
+ // Apply for 'theme' plugins optional paths at course level
+ $this->add_plugin_structure('theme', $course);
+
return array($course, $category, $tag, $allowed_module);
}
@@ -1052,8 +1055,9 @@ public function process_course($data) {
if (!array_key_exists($data->lang, $languages)) {
$data->lang = '';
}
+
$themes = get_list_of_themes(); // Get themes for quick search later
- if (!in_array($data->theme, $themes) || empty($CFG->allowcoursethemes)) {
+ if (!array_key_exists($data->theme, $themes) || empty($CFG->allowcoursethemes)) {
$data->theme = '';
}
@@ -0,0 +1,29 @@
+<?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/>.
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Restore for course plugin: theme.
+ *
+ * @package moodlecore
+ * @subpackage backup-moodle2
+ * @copyright 2011 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+abstract class restore_theme_plugin extends restore_plugin {
+ // Use default parent behaviour
+}
@@ -128,6 +128,45 @@ public static function get_courseformat_from_courseid($courseid) {
return $DB->get_field('course', 'format', array('id' => $courseid));
}
+ /**
+ * Given a course id, returns its theme. This can either be the course
+ * theme or (if not specified in course) the category, site theme.
+ *
+ * User, session, and inherited-from-mnet themes cannot have backed-up
+ * per course data. This is course-related data so it must be in a course
+ * theme specified as part of the course structure
+ * @param int $courseid
+ * @return string Name of course theme
+ * @see moodle_page#resolve_theme()
+ */
+ public static function get_theme_from_courseid($courseid) {
+ global $DB, $CFG;
+
+ // Course theme first
+ if (!empty($CFG->allowcoursethemes)) {
+ $theme = $DB->get_field('course', 'theme', array('id' => $courseid));
+ if ($theme) {
+ return $theme;
+ }
+ }
+
+ // Category themes in reverse order
+ if (!empty($CFG->allowcategorythemes)) {
+ $catid = $DB->get_field('course', 'category', array('id' => $courseid));
+ while($catid) {
+ $category = $DB->get_record('course_categories', array('id'=>$catid),
+ 'theme,parent', MUST_EXIST);
+ if ($category->theme) {
+ return $category->theme;
+ }
+ $catid = $category->parent;
+ }
+ }
+
+ // Finally use site theme
+ return $CFG->theme;
+ }
+
/**
* Return the wwwroot of the $CFG->mnet_localhost_id host
* caching it along the request

0 comments on commit 89213d0

Please sign in to comment.