]> git.mjollnir.org Git - moodle.git/commitdiff
filters: MDL-7336 backup and restore of local filter settings.
authortjhunt <tjhunt>
Mon, 13 Apr 2009 07:06:02 +0000 (07:06 +0000)
committertjhunt <tjhunt>
Mon, 13 Apr 2009 07:06:02 +0000 (07:06 +0000)
backup/backuplib.php
backup/restorelib.php
lib/filterlib.php

index 4e44d1975164dfc5cc0f3f291de57df85511d307..2150076a83c3a51229de2cace586c28e8377872b 100644 (file)
     function write_per_context_data($bf, $preferences, $context, $startlevel) {
         write_role_overrides_xml($bf, $context, $startlevel);
         write_role_assignments_xml($bf, $preferences, $context, $startlevel);
+        write_local_filter_settings($bf, $preferences, $context, $startlevel);
     }
 
     /**
         fwrite ($bf, end_tag("ROLES_ASSIGNMENTS", $startlevel, true));
     }
 
+    /**
+     * Write any local filter settings for this context to the backup file.
+     * They comprise On/off filter_active.active overrides, and any filter_config
+     * records for this contextid.
+     */
+    function write_local_filter_settings($bf, $preferences, $context, $startlevel) {
+        if (!filter_context_may_have_filter_settings($context)) {
+            return;
+        }
+        list($actives, $configs) = filter_get_all_local_settings($context->id);
+
+        fwrite($bf, start_tag("FILTERACTIVES", $startlevel, true));
+        foreach ($actives as $active) {
+            fwrite($bf, start_tag("FILTERACTIVE", $startlevel + 1, true));
+            fwrite($bf, full_tag("FILTER", $startlevel + 2, false, $active->filter));
+            fwrite($bf, full_tag("ACTIVE", $startlevel + 2, false, $active->active));
+            fwrite($bf, end_tag("FILTERACTIVE", $startlevel + 1, true));
+        }
+        fwrite($bf, end_tag("FILTERACTIVES", $startlevel, true));
+
+        fwrite($bf, start_tag("FILTERCONFIGS", $startlevel, true));
+        foreach ($configs as $config) {
+            fwrite($bf, start_tag("FILTERCONFIG", $startlevel + 1, true));
+            fwrite($bf, full_tag("FILTER", $startlevel + 2, false, $config->filter));
+            fwrite($bf, full_tag("NAME", $startlevel + 2, false, $config->name));
+            fwrite($bf, full_tag("VALUE", $startlevel + 2, false, $config->value));
+            fwrite($bf, end_tag("FILTERCONFIG", $startlevel + 1, true));
+        }
+        fwrite($bf, end_tag("FILTERCONFIGS", $startlevel, true));
+    }
 
     function backup_execute(&$preferences, &$errorstr) {
         global $CFG, $DB;
index cf27b160baa63488b4d355db893491a7b906ad03..7ab4c01cfc8a0deab01c3d1846f46ac64b1197d6 100644 (file)
@@ -5678,6 +5678,39 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                         }
                     }
                 } /// ends role_overrides
+
+                if ($this->tree[4] == "FILTERACTIVES") {
+                    if ($this->level == 6) {
+                        switch ($tagName) {
+                            case "FILTER":
+                                $this->info->tempfilter = $this->getContents();
+                            break;
+                            case "ACTIVE":
+                                $this->info->filteractives[$this->info->tempfilter] = $this->getContents();
+                            break;
+                        }
+                    }
+                } /// ends FILTERACTIVES
+
+                if ($this->tree[4] == "FILTERCONFIGS") {
+                    if ($this->level == 6) {
+                        switch ($tagName) {
+                            case "FILTER":
+                                $this->info->tempfilter = $this->getContents();
+                            break;
+                            case "NAME":
+                                $this->info->tempname = $this->getContents();
+                            break;
+                            case "VALUE":
+                                $fc = new stdClass;
+                                $fc->filter = $this->info->tempfilter;
+                                $fc->name = $this->info->tempfilter;
+                                $fc->value = $this->getContents();
+                                $this->info->filterconfigs[] = $fc;
+                            break;
+                        }
+                    }
+                } /// ends FILTERCONFIGS
             }
 
             //Stop parsing if todo = COURSE_HEADER and tagName = HEADER (en of the tag, of course)
@@ -6123,6 +6156,40 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                     }
                 } /// ends role_overrides
 
+
+                if (isset($this->tree[7]) && $this->tree[7] == "FILTERACTIVES") {
+                    if ($this->level == 9) {
+                        switch ($tagName) {
+                            case "FILTER":
+                                $this->info->tempfilter = $this->getContents();
+                            break;
+                            case "ACTIVE":
+                                $this->info->filteractives[$this->info->tempfilter] = $this->getContents();
+                            break;
+                        }
+                    }
+                } /// ends FILTERACTIVES
+
+                if (isset($this->tree[7]) && $this->tree[7] == "FILTERCONFIGS") {
+                    if ($this->level == 9) {
+                        switch ($tagName) {
+                            case "FILTER":
+                                $this->info->tempfilter = $this->getContents();
+                            break;
+                            case "NAME":
+                                $this->info->tempname = $this->getContents();
+                            break;
+                            case "VALUE":
+                                $fc = new stdClass;
+                                $fc->filter = $this->info->tempfilter;
+                                $fc->name = $this->info->tempfilter;
+                                $fc->value = $this->getContents();
+                                $this->info->filteractives[]->filter = $fc;
+                            break;
+                        }
+                    }
+                } /// ends FILTERCONFIGS
+
                 if (isset($this->tree[7]) && $this->tree[7] == "COMPLETIONDATA") {
                     if($this->level == 8) {
                         switch($tagName) {
@@ -9032,6 +9099,9 @@ WHERE
             }
         }
 
+        // Per-context filter settings.
+        restore_write_local_filter_settings($restore, $course, $newcoursecontext);
+
         /*******************************************************
          * Restoring role assignments/overrdies                *
          * from module level assignments                       *
@@ -9058,6 +9128,8 @@ WHERE
                             restore_write_roleoverrides($restore, $modoverride->overrides, $newmodcontext, $oldroleid);
                         }
                     }
+                    // Per-context filter settings.
+                    restore_write_local_filter_settings($restore, $mod, $newmodcontext);
                 }
             }
         }
@@ -9185,6 +9257,39 @@ WHERE
             assign_capability($override->capability, $override->permission, $override->roleid, $override->contextid);
         }
     }
+
+    /**
+     * Write any per-context filter settings from the backup XML to the DB.
+     * @param object $restore the restore we are part of.
+     * @param object $data sata loaded from the XML.
+     * @param object $newmodcontext the restored context object.
+     */
+    function restore_write_local_filter_settings($restore, $data, $newcontext) {
+        if (filter_context_may_have_filter_settings($newcontext)) {
+            return;
+        }
+
+        $installedfilters = filter_get_all_installed();
+
+        if (!isset($data->filteractives)) {
+            $data->filteractives = array();
+        }
+        foreach ($data->filteractives as $filter => $state) {
+            if (isset($installedfilters[$filter])) {
+                filter_set_local_state($filter, $newcontext->id, $state);
+            }
+        }
+
+        if (!isset($data->filterconfigs)) {
+            $data->filterconfigs = array();
+        }
+        foreach ($data->filterconfigs as $fc) {
+            if (isset($installedfilters[$fc->filter])) {
+                filter_set_local_config($fc->filter, $newcontext->id, $fc->name, $fc->value);
+            }
+        }
+    }
+
     //write activity date changes to the html log file, and update date values in the the xml array
     function restore_log_date_changes($recordtype, &$restore, &$xml, $TAGS, $NAMETAG='NAME') {
 
index 45aad2fd5edcbe6afa21d6fd55935b06ad9f47c2..46caf2e4375a7d6b6a9f1f28e08fe9b8763ca76c 100644 (file)
@@ -586,6 +586,23 @@ function filter_get_local_config($filter, $contextid) {
     return $DB->get_records_menu('filter_config', array('filter' => $filter, 'contextid' => $contextid), '', 'name,value');
 }
 
+/**
+ * This function is for use by backup. Gets all the filter information specific
+ * to one context.
+ * @return array with two elements. The first element is an array of objects with
+ *      fields filter and active. These come from the filter_active table. The
+ *      second element is an array of objects with fields filter, name and value
+ *      from the filter_config table.
+ */
+function filter_get_all_local_settings($contextid) {
+    global $DB;
+    $context = get_context_instance(CONTEXT_SYSTEM);
+    return array(
+        $DB->get_records('filter_active', array('contextid' => $contextid), 'filter', 'filter,active'),
+        $DB->get_records('filter_config', array('contextid' => $contextid), 'filter,name', 'filter,name,value'),
+    );
+}
+
 /**
  * Get the list of active filters, in the order that they should be used
  * for a particular context, along with any local configuration variables.
@@ -670,6 +687,16 @@ function filter_has_global_settings($filter) {
     return is_readable($settingspath);
 }
 
+/**
+ * Certain types of context (block and user) may not have local filter settings.
+ * the function checks a context to see whether it may have local config.
+ * @param object $context a context.
+ * @return boolean whether this context may have local filter settings.
+ */
+function filter_context_may_have_filter_settings($context) {
+    return $context->contextlevel != CONTEXT_BLOCK && $context->contextlevel != CONTEXT_USER;
+}
+
 /**
  * Process phrases intelligently found within a HTML text (such as adding links)
  *