MDL-27448 Improved resource module converter

This commit is contained in:
David Mudrak 2011-06-04 20:03:28 +02:00
parent aa97e0dd52
commit 351bf4826c

View file

@ -31,9 +31,6 @@ defined('MOODLE_INTERNAL') || die();
*/ */
class moodle1_mod_resource_handler extends moodle1_mod_handler { class moodle1_mod_resource_handler extends moodle1_mod_handler {
/** @var array in-memory cache for the course module information */
protected $currentcminfo = null;
/** @var moodle1_file_manager instance */ /** @var moodle1_file_manager instance */
protected $fileman = null; protected $fileman = null;
@ -88,47 +85,15 @@ class moodle1_mod_resource_handler extends moodle1_mod_handler {
$data['introformat'] = FORMAT_HTML; $data['introformat'] = FORMAT_HTML;
} }
// if this is a file or URL resource we need to deal with the options // fix invalid null popup and options data
// before possibly branching out to the URL successor if (!array_key_exists('popup', $data) or is_null($data['popup'])) {
if ($data['type'] == 'file') {
$options = array('printheading' => 0, 'printintro' => 1);
if ($data['options'] == 'frame') {
$data['display'] = RESOURCELIB_DISPLAY_FRAME;
} else if ($data['options'] == 'objectframe') {
$data['display'] = RESOURCELIB_DISPLAY_EMBED;
} else if ($data['options'] == 'forcedownload') {
$data['display'] = RESOURCELIB_DISPLAY_DOWNLOAD;
} else if ($data['popup']) {
$data['display'] = RESOURCELIB_DISPLAY_POPUP;
if ($data['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 {
$data['display'] = RESOURCELIB_DISPLAY_AUTO;
}
$data['displayoptions'] = serialize($options);
unset($data['popup']);
}
// fix invalid NULL popup and options data in old mysql databases
if (!array_key_exists('popup', $data) || $data['popup'] === null) {
$data['popup'] = ''; $data['popup'] = '';
} }
if (!array_key_exists ('options', $data) || $data['options'] === null) { if (!array_key_exists ('options', $data) or is_null($data['options'])) {
$data['options'] = ''; $data['options'] = '';
} }
// decide if the legacy resource should be handled by a successor module
if ($successor = $this->get_successor($data['type'], $data['reference'])) { if ($successor = $this->get_successor($data['type'], $data['reference'])) {
// the instance id will be kept // the instance id will be kept
$instanceid = $data['id']; $instanceid = $data['id'];
@ -159,55 +124,82 @@ class moodle1_mod_resource_handler extends moodle1_mod_handler {
return $successor->process_legacy_resource($data, $raw); return $successor->process_legacy_resource($data, $raw);
} }
// only $data['type'] == "file" should get to here // no successor is interested in this record, convert it to the new mod_resource (aka File module)
$resource = array();
$resource['id'] = $data['id'];
$resource['name'] = $data['name'];
$resource['intro'] = $data['intro'];
$resource['introformat'] = $data['introformat'];
$resource['tobemigrated'] = 0;
$resource['legacyfiles'] = RESOURCELIB_LEGACYFILES_ACTIVE;
$resource['legacyfileslast'] = null;
$resource['filterfiles'] = 0;
$resource['revision'] = 1;
$resource['timemodified'] = $data['timemodified'];
// populate display and displayoptions fields
$options = array('printheading' => 0, 'printintro' => 1);
if ($data['options'] == 'frame') {
$resource['display'] = RESOURCELIB_DISPLAY_FRAME;
} else if ($data['options'] == 'objectframe') {
$resource['display'] = RESOURCELIB_DISPLAY_EMBED;
} else if ($data['options'] == 'forcedownload') {
$resource['display'] = RESOURCELIB_DISPLAY_DOWNLOAD;
} else if ($data['popup']) {
$resource['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 {
$resource['display'] = RESOURCELIB_DISPLAY_AUTO;
}
$resource['displayoptions'] = serialize($options);
// get the course module id and context id // get the course module id and context id
$instanceid = $data['id']; $instanceid = $resource['id'];
$this->currentcminfo = $this->get_cminfo($instanceid); $currentcminfo = $this->get_cminfo($instanceid);
$moduleid = $this->currentcminfo['id']; $moduleid = $currentcminfo['id'];
$contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid); $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
unset($data['type']); // get a fresh new file manager for this instance
unset($data['alltext']); $this->fileman = $this->converter->get_file_manager($contextid, 'mod_resource');
unset($data['popup']);
unset($data['options']);
$data['tobemigrated'] = 0; // convert course files embedded into the intro
$data['mainfile'] = null; $files = moodle1_converter::find_referenced_files($resource['intro']);
$data['legacyfiles'] = 0; if (!empty($files)) {
$data['legacyfileslast'] = null; $this->fileman->filearea = 'intro';
$data['display'] = 0; $this->fileman->itemid = 0;
$data['displayoptions'] = null; foreach ($files as $file) {
$data['filterfiles'] = 0; $this->fileman->migrate_file('course_files'.$file, dirname($file));
$data['revision'] = 0; }
unset($data['mainfile']); $resource['intro'] = moodle1_converter::rewrite_filephp_usage($resource['intro'], $files);
}
// we now have all information needed to start writing into the file // convert the referenced file itself as a main file in the content area
$this->fileman->filearea = 'content';
$this->fileman->itemid = 0;
$this->fileman->migrate_file('course_files/'.$data['reference'], '/', null, 1);
// write resource.xml
$this->open_xml_writer("activities/resource_{$moduleid}/resource.xml"); $this->open_xml_writer("activities/resource_{$moduleid}/resource.xml");
$this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid, $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
'modulename' => 'resource', 'contextid' => $contextid)); 'modulename' => 'resource', 'contextid' => $contextid));
$this->xmlwriter->begin_tag('resource', array('id' => $instanceid)); $this->write_xml('resource', $resource, array('/resource/id'));
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);
}
// prepare file manager for migrating the resource file
$this->fileman = $this->converter->get_file_manager($contextid, 'mod_resource', 'content');
$this->fileman->migrate_file('course_files/'.$data['reference']);
}
public function on_resource_end(array $data) {
if ($successor = $this->get_successor($data['type'], $data['reference'])) {
$successor->on_legacy_resource_end($data);
} else {
$this->xmlwriter->end_tag('resource');
$this->xmlwriter->end_tag('activity'); $this->xmlwriter->end_tag('activity');
$this->close_xml_writer(); $this->close_xml_writer();
// write inforef.xml for migrated resource file. // write inforef.xml
$this->open_xml_writer("activities/resource_{$this->currentcminfo['id']}/inforef.xml"); $this->open_xml_writer("activities/resource_{$currentcminfo['id']}/inforef.xml");
$this->xmlwriter->begin_tag('inforef'); $this->xmlwriter->begin_tag('inforef');
$this->xmlwriter->begin_tag('fileref'); $this->xmlwriter->begin_tag('fileref');
foreach ($this->fileman->get_fileids() as $fileid) { foreach ($this->fileman->get_fileids() as $fileid) {
@ -217,6 +209,14 @@ class moodle1_mod_resource_handler extends moodle1_mod_handler {
$this->xmlwriter->end_tag('inforef'); $this->xmlwriter->end_tag('inforef');
$this->close_xml_writer(); $this->close_xml_writer();
} }
/**
* Give succesors a chance to finish their job
*/
public function on_resource_end(array $data) {
if ($successor = $this->get_successor($data['type'], $data['reference'])) {
$successor->on_legacy_resource_end($data);
}
} }
/// internal implementation details follow ///////////////////////////////// /// internal implementation details follow /////////////////////////////////