<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
	<id>http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Development%3ABackup_2.0_theme_data</id>
	<title>Development:Backup 2.0 theme data - История изменений</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.mipt.ru/index.php?action=history&amp;feed=atom&amp;title=Development%3ABackup_2.0_theme_data"/>
	<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Backup_2.0_theme_data&amp;action=history"/>
	<updated>2026-05-07T15:53:42Z</updated>
	<subtitle>История изменений этой страницы в вики</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Development:Backup_2.0_theme_data&amp;diff=10846&amp;oldid=prev</id>
		<title>Олег Давидович: 1 версия импортирована</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Backup_2.0_theme_data&amp;diff=10846&amp;oldid=prev"/>
		<updated>2024-10-21T08:51:01Z</updated>

		<summary type="html">&lt;p&gt;1 версия импортирована&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Предыдущая версия&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Версия от 08:51, 21 октября 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Олег Давидович</name></author>
	</entry>
	<entry>
		<id>http://wiki.mipt.ru/index.php?title=Development:Backup_2.0_theme_data&amp;diff=10845&amp;oldid=prev</id>
		<title>1&gt;Quen в 12:09, 8 марта 2011</title>
		<link rel="alternate" type="text/html" href="http://wiki.mipt.ru/index.php?title=Development:Backup_2.0_theme_data&amp;diff=10845&amp;oldid=prev"/>
		<updated>2011-03-08T12:09:10Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Template:Development:Backup 2.0}}{{Moodle_2.0}}&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
In Moodle 2, themes can have data tables and files which can be backed up with a course. (To create data tables in a theme, use the standard &amp;lt;tt&amp;gt;db&amp;lt;/tt&amp;gt; folder.)&lt;br /&gt;
&lt;br /&gt;
None of the standard themes have any course-related data so it is not obvious how to code the backup and restore for them. This page gives an example theme as a case study which you can use to implement your own backup code for a theme which contains per-course data.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note: Theme data backup requires Moodle 2.0.3 or later.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Example: the &amp;#039;ou&amp;#039; theme ==&lt;br /&gt;
&lt;br /&gt;
Our theme called the &amp;#039;ou&amp;#039; theme (i.e. in the folder theme/ou) contains the following per-course data which needs to be backed up:&lt;br /&gt;
&lt;br /&gt;
* Each course using the theme can select a different colour (referred to as &amp;#039;variant&amp;#039; of the theme). This is so we can have different courses in any of 10 different colours without having to make 10 different themes.&lt;br /&gt;
&lt;br /&gt;
* Each course can also select a custom image which is used in the header on some screens. This is so we can have cute kitten pictures on our test servers.&lt;br /&gt;
&lt;br /&gt;
The variant is stored in a table called &amp;lt;tt&amp;gt;theme_ou_courseoptions&amp;lt;/tt&amp;gt;. This table has 3 fields: &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;courseid&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;variant&amp;lt;/tt&amp;gt;. (If we ever need to add more theme data, we can add fields to it.)&lt;br /&gt;
&lt;br /&gt;
The image file is stored using the Moodle file API at the course context, in the &amp;lt;tt&amp;gt;theme_ou&amp;lt;/tt&amp;gt; component and the &amp;lt;tt&amp;gt;image&amp;lt;/tt&amp;gt; file area.&lt;br /&gt;
&lt;br /&gt;
=== Backup ===&lt;br /&gt;
&lt;br /&gt;
The backup code is in the file &amp;lt;tt&amp;gt;theme/ou/backup/moodle2/backup_theme_ou_plugin.class.php&amp;lt;/tt&amp;gt;. Leaving out the headers, this file looks like:&lt;br /&gt;
&lt;br /&gt;
 class backup_theme_ou_plugin extends backup_theme_plugin {&lt;br /&gt;
 &lt;br /&gt;
     /**&lt;br /&gt;
      * Returns the theme information to attach to course element&lt;br /&gt;
      */&lt;br /&gt;
     protected function define_course_plugin_structure() {&lt;br /&gt;
         // Define virtual plugin element&lt;br /&gt;
         $plugin = $this-&amp;gt;get_plugin_element(null, $this-&amp;gt;get_theme_condition(), &amp;#039;ou&amp;#039;);&lt;br /&gt;
 &lt;br /&gt;
         // Create plugin container element with standard name&lt;br /&gt;
         $pluginwrapper = new backup_nested_element($this-&amp;gt;get_recommended_name());&lt;br /&gt;
 &lt;br /&gt;
         // Add wrapper to plugin&lt;br /&gt;
         $plugin-&amp;gt;add_child($pluginwrapper);&lt;br /&gt;
 &lt;br /&gt;
         // Set up theme&amp;#039;s own structure and add to wrapper&lt;br /&gt;
         $studyplan = new backup_nested_element(&amp;#039;ou&amp;#039;, array(&amp;#039;id&amp;#039;), array(&lt;br /&gt;
             &amp;#039;variant&amp;#039;));&lt;br /&gt;
         $pluginwrapper-&amp;gt;add_child($studyplan);&lt;br /&gt;
 &lt;br /&gt;
         // Use database to get source&lt;br /&gt;
         $studyplan-&amp;gt;set_source_table(&amp;#039;theme_ou_courseoptions&amp;#039;,&lt;br /&gt;
                 array(&amp;#039;courseid&amp;#039; =&amp;gt; backup::VAR_COURSEID));&lt;br /&gt;
 &lt;br /&gt;
         // Include files which have theme_ou and area image and no itemid&lt;br /&gt;
         $studyplan-&amp;gt;annotate_files(&amp;#039;theme_ou&amp;#039;, &amp;#039;image&amp;#039;, null);&lt;br /&gt;
 &lt;br /&gt;
         return $plugin;&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
As you can see this works in a similar way to backup for modules and other plugins. The first line:&lt;br /&gt;
&lt;br /&gt;
 $this-&amp;gt;get_plugin_element(null, $this-&amp;gt;get_theme_condition(), &amp;#039;ou&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
means that the theme data will only be backed up if the current theme for the course is actually the &amp;#039;ou&amp;#039; theme. In other words, if you change the course theme to &amp;#039;standard&amp;#039;, this backup data will not be created. (It is possible to change this behaviour if you want to backup theme data even when a different theme is currently selected for the course; leave out the last two parameters.)&lt;br /&gt;
&lt;br /&gt;
You can see how you would change the code to use a different theme name, database table, fields, or file area. It&amp;#039;s also possible to add a more complicated data structured in the same way as for other plugins. You could even store user data - however, unless the user data is course-specific, it would probably be more appropriate to store user theme options in user preferences rather than within theme-managed tables.&lt;br /&gt;
&lt;br /&gt;
==== Testing ====&lt;br /&gt;
&lt;br /&gt;
To test the backup, carry it out and then look inside the resulting zip file. In the &amp;lt;tt&amp;gt;course&amp;lt;/tt&amp;gt; folder there should be a file called &amp;lt;tt&amp;gt;course.xml&amp;lt;/tt&amp;gt;. If theme data is backed up, it will include a section like the following:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;plugin_theme_ou_course&amp;gt;&lt;br /&gt;
  &amp;lt;ou id=&amp;quot;2&amp;quot;&amp;gt;&lt;br /&gt;
   &amp;lt;variant&amp;gt;purple&amp;lt;/variant&amp;gt;&lt;br /&gt;
  &amp;lt;/ou&amp;gt;&lt;br /&gt;
 &amp;lt;/plugin_theme_ou_course&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Additionally, if the file backup worked, you should be able to find any files from within the specified file area inside the &amp;#039;files&amp;#039; section of the backup (it will help if you know the hash code of the file; otherwise, note the size and make sure the test course doesn&amp;#039;t have many other files to look through).&lt;br /&gt;
&lt;br /&gt;
If you switch the course to a different theme, this data will not be backed up.&lt;br /&gt;
&lt;br /&gt;
=== Restore ===&lt;br /&gt;
&lt;br /&gt;
Unsurprisingly, restore code goes in the file &amp;lt;tt&amp;gt;theme/ou/backup/moodle2/restore_theme_ou_plugin.class.php&amp;lt;/tt&amp;gt;. Leaving out the headers, this file is:&lt;br /&gt;
&lt;br /&gt;
 class restore_theme_ou_plugin extends restore_theme_plugin {&lt;br /&gt;
 &lt;br /&gt;
     /**&lt;br /&gt;
      * Returns the paths to be handled by the plugin at course level&lt;br /&gt;
      */&lt;br /&gt;
     protected function define_course_plugin_structure() {&lt;br /&gt;
         $paths = array();&lt;br /&gt;
 &lt;br /&gt;
         // Because of using get_recommended_name() it is able to find the&lt;br /&gt;
         // correct path just by using the part inside the element name (which&lt;br /&gt;
         // only has a /ou element).&lt;br /&gt;
         $elepath = $this-&amp;gt;get_pathfor(&amp;#039;/ou&amp;#039;);&lt;br /&gt;
 &lt;br /&gt;
         // The &amp;#039;ou&amp;#039; here defines that it will use the process_ou function&lt;br /&gt;
         // to restore its element.&lt;br /&gt;
         $paths[] = new restore_path_element(&amp;#039;ou&amp;#039;, $elepath);&lt;br /&gt;
 &lt;br /&gt;
         return $paths;&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     /**&lt;br /&gt;
      * Called after this runs for a course.&lt;br /&gt;
      */&lt;br /&gt;
     function after_execute_course() {&lt;br /&gt;
         // Need to restore file&lt;br /&gt;
         $this-&amp;gt;add_related_files(&amp;#039;theme_ou&amp;#039;, &amp;#039;image&amp;#039;, null);&lt;br /&gt;
     }&lt;br /&gt;
 &lt;br /&gt;
     /**&lt;br /&gt;
      * Process the &amp;#039;ou&amp;#039; element&lt;br /&gt;
      */&lt;br /&gt;
     public function process_ou($data) {&lt;br /&gt;
         global $DB;&lt;br /&gt;
 &lt;br /&gt;
         // Get data record ready to insert in database&lt;br /&gt;
         $data = (object)$data;&lt;br /&gt;
         $data-&amp;gt;courseid = $this-&amp;gt;task-&amp;gt;get_courseid();&lt;br /&gt;
 &lt;br /&gt;
         // See if there is an existing record for this course&lt;br /&gt;
         $existingid = $DB-&amp;gt;get_field(&amp;#039;theme_ou_courseoptions&amp;#039;, &amp;#039;id&amp;#039;,&lt;br /&gt;
                 array(&amp;#039;courseid&amp;#039;=&amp;gt;$data-&amp;gt;courseid));&lt;br /&gt;
         if ($existingid) {&lt;br /&gt;
             $data-&amp;gt;id = $existingid;&lt;br /&gt;
             $DB-&amp;gt;update_record(&amp;#039;theme_ou_courseoptions&amp;#039;, data);&lt;br /&gt;
         } else {&lt;br /&gt;
             $DB-&amp;gt;insert_record(&amp;#039;theme_ou_courseoptions&amp;#039;, $data);&lt;br /&gt;
         }&lt;br /&gt;
 &lt;br /&gt;
         // No need to record the old/new id as nothing ever refers to&lt;br /&gt;
         // the id of this table.&lt;br /&gt;
     }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Again this is just the same as restoring a module. To change this to support your own theme, you would change the word &amp;#039;ou&amp;#039; to your own theme&amp;#039;s name, change references to the &amp;lt;tt&amp;gt;theme_ou_courseoptions&amp;lt;/tt&amp;gt; table to your own data table (or otherwise change what exactly it does when restoring), and change the file area details in the &amp;lt;tt&amp;gt;add_related_files&amp;lt;/tt&amp;gt; call.&lt;br /&gt;
&lt;br /&gt;
Unlike backup, the restore code always runs if the data was included in the backup, even if the course actually has a different theme once it is restored (e.g. the site default theme has changed). This should not cause a particular problem; at worst you&amp;#039;ll get a superfluous database row on rare occasions.&lt;br /&gt;
&lt;br /&gt;
==== Testing ====&lt;br /&gt;
&lt;br /&gt;
It is probably obvious how to test restore :) Once you&amp;#039;ve verified that backup is producing the correct XML data, just check that restore recreates the necessary data for you. &lt;br /&gt;
&lt;br /&gt;
In our case, we know restore works if the restored course is purple and has a cute kitten picture.&lt;/div&gt;</summary>
		<author><name>1&gt;Quen</name></author>
	</entry>
</feed>