Skip to content

Commit

Permalink
MDL-27448 URL resource module backup conversion improved
Browse files Browse the repository at this point in the history
The patch also fixes invalid 'course' field in moodle2 backup code.
  • Loading branch information
mudrd8mz committed Jun 4, 2011
1 parent c39226d commit afb4494
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
75 changes: 63 additions & 12 deletions mod/url/backup/moodle1/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,86 @@
*/
class moodle1_mod_url_handler extends moodle1_resource_successor_handler {

/** @var moodle1_file_manager instance */
protected $fileman = null;

/**
* Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
* Called by moodle1_mod_resource_handler::process_resource()
*/
public function process_legacy_resource($data) {
$data['externalurl'] = $data['reference'];
unset($data['reference']);

// get the course module id and context id
$instanceid = $data['id'];
$cminfo = $this->get_cminfo($instanceid, 'resource');
$moduleid = $cminfo['id'];
$contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);

// we now have all information needed to start writing into the file
// prepare the new url instance record
$url = array();
$url['id'] = $data['id'];
$url['name'] = $data['name'];
$url['intro'] = $data['intro'];
$url['introformat'] = $data['introformat'];
$url['externalurl'] = $data['reference'];
$url['timemodified'] = $data['timemodified'];

// populate display and displayoptions fields
$options = array('printheading' => 0, 'printintro' => 1);
if ($data['options'] == 'frame') {
$url['display'] = RESOURCELIB_DISPLAY_FRAME;

} else if ($data['options'] == 'objectframe') {
$url['display'] = RESOURCELIB_DISPLAY_EMBED;

} else if ($data['popup']) {
$url['display'] = RESOURCELIB_DISPLAY_POPUP;
$rawoptions = explode(',', $data['popup']);
foreach ($rawoptions as $rawoption) {
list($name, $value) = explode('=', trim($rawoption), 2);
if ($value > 0 and ($name == 'width' or $name == 'height')) {
$options['popup'.$name] = $value;
continue;
}
}

} else {
$url['display'] = RESOURCELIB_DISPLAY_AUTO;
}
$url['displayoptions'] = serialize($options);

// populate the parameters field
$parameters = array();
if ($data['alltext']) {
$rawoptions = explode(',', $data['alltext']);
foreach ($rawoptions as $rawoption) {
list($variable, $parameter) = explode('=', trim($rawoption), 2);
$parameters[$parameter] = $variable;
}
}
$url['parameters'] = serialize($parameters);

// convert course files embedded into the intro
$this->fileman = $this->converter->get_file_manager($contextid, 'mod_url', 'intro');
$url['intro'] = moodle1_converter::migrate_referenced_files($url['intro'], $this->fileman);

// write url.xml
$this->open_xml_writer("activities/url_{$moduleid}/url.xml");
$this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
'modulename' => 'url', 'contextid' => $contextid));
$this->xmlwriter->begin_tag('url', array('id' => $instanceid));
$this->write_xml('url', $url, array('/url/id'));
$this->xmlwriter->end_tag('activity');
$this->close_xml_writer();

unset($data['id']); // we already write it as attribute, do not repeat it as child element
foreach ($data as $field => $value) {
$this->xmlwriter->full_tag($field, $value);
// write inforef.xml
$this->open_xml_writer("activities/url_{$moduleid}/inforef.xml");
$this->xmlwriter->begin_tag('inforef');
$this->xmlwriter->begin_tag('fileref');
foreach ($this->fileman->get_fileids() as $fileid) {
$this->write_xml('file', array('id' => $fileid));
}
}

public function on_legacy_resource_end() {
$this->xmlwriter->end_tag('url');
$this->xmlwriter->end_tag('activity');
$this->xmlwriter->end_tag('fileref');
$this->xmlwriter->end_tag('inforef');
$this->close_xml_writer();
}
}
2 changes: 1 addition & 1 deletion mod/url/backup/moodle2/backup_url_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function define_structure() {

// Define each element separated
$url = new backup_nested_element('url', array('id'), array(
'course','name', 'intro', 'introformat', 'externalurl',
'name', 'intro', 'introformat', 'externalurl',
'display', 'displayoptions', 'parameters', 'timemodified'));


Expand Down

0 comments on commit afb4494

Please sign in to comment.