Skip to content

Commit

Permalink
filters: MDL-7336 backup and restore of local filter settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhunt committed Apr 13, 2009
1 parent c9a2ecf commit 34f0786
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 0 deletions.
31 changes: 31 additions & 0 deletions backup/backuplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2821,6 +2821,7 @@ function backup_fetch_roles($preferences) {
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);
}

/**
Expand Down Expand Up @@ -2900,6 +2901,36 @@ function write_role_assignments_xml($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;
Expand Down
105 changes: 105 additions & 0 deletions backup/restorelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5678,6 +5678,39 @@ function endElementCourseHeader($parser, $tagName) {
}
}
} /// 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)
Expand Down Expand Up @@ -6123,6 +6156,40 @@ function endElementSections($parser, $tagName) {
}
} /// 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) {
Expand Down Expand Up @@ -9032,6 +9099,9 @@ function restore_roles_and_filter_settings($restore, $xmlfile) {
}
}

// Per-context filter settings.
restore_write_local_filter_settings($restore, $course, $newcoursecontext);

/*******************************************************
* Restoring role assignments/overrdies *
* from module level assignments *
Expand All @@ -9058,6 +9128,8 @@ function restore_roles_and_filter_settings($restore, $xmlfile) {
restore_write_roleoverrides($restore, $modoverride->overrides, $newmodcontext, $oldroleid);
}
}
// Per-context filter settings.
restore_write_local_filter_settings($restore, $mod, $newmodcontext);
}
}
}
Expand Down Expand Up @@ -9185,6 +9257,39 @@ function restore_write_roleoverrides($restore, $overrides, $newcontext, $oldrole
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') {

Expand Down
27 changes: 27 additions & 0 deletions lib/filterlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
*
Expand Down

0 comments on commit 34f0786

Please sign in to comment.