Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/resource/backup/moodle1/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00032 class moodle1_mod_resource_handler extends moodle1_mod_handler {
00033 
00035     protected $fileman = null;
00036 
00038     private $successors = array();
00039 
00053     public function get_paths() {
00054         return array(
00055             new convert_path(
00056                 'resource', '/MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE',
00057                 array(
00058                     'renamefields' => array(
00059                         'summary' => 'intro',
00060                     ),
00061                     'newfields' => array(
00062                         'introformat' => 0,
00063                     ),
00064                     'dropfields' => array(
00065                         'modtype',
00066                     ),
00067                 )
00068             )
00069         );
00070     }
00071 
00078     public function process_resource(array $data, array $raw) {
00079         global $CFG;
00080         require_once("$CFG->libdir/resourcelib.php");
00081 
00082         // replay the upgrade step 2009042001
00083         if ($CFG->texteditors !== 'textarea') {
00084             $data['intro']       = text_to_html($data['intro'], false, false, true);
00085             $data['introformat'] = FORMAT_HTML;
00086         }
00087 
00088         // fix invalid null popup and options data
00089         if (!array_key_exists('popup', $data) or is_null($data['popup'])) {
00090             $data['popup'] = '';
00091         }
00092         if (!array_key_exists ('options', $data) or is_null($data['options'])) {
00093             $data['options'] = '';
00094         }
00095 
00096         // decide if the legacy resource should be handled by a successor module
00097         if ($successor = $this->get_successor($data['type'], $data['reference'])) {
00098             // the instance id will be kept
00099             $instanceid = $data['id'];
00100 
00101             // move the instance from the resource's modinfo stash to the successor's
00102             // modinfo stash
00103             $resourcemodinfo  = $this->converter->get_stash('modinfo_resource');
00104             $successormodinfo = $this->converter->get_stash('modinfo_'.$successor->get_modname());
00105             $successormodinfo['instances'][$instanceid] = $resourcemodinfo['instances'][$instanceid];
00106             unset($resourcemodinfo['instances'][$instanceid]);
00107             $this->converter->set_stash('modinfo_resource', $resourcemodinfo);
00108             $this->converter->set_stash('modinfo_'.$successor->get_modname(), $successormodinfo);
00109 
00110             // get the course module information for the legacy resource module
00111             $cminfo = $this->get_cminfo($instanceid);
00112 
00113             // use the version of the successor instead of the current mod/resource
00114             // beware - the version.php declares info via $module object, do not use
00115             // a variable of such name here
00116             include $CFG->dirroot.'/mod/'.$successor->get_modname().'/version.php';
00117             $cminfo['version'] = $module->version;
00118 
00119             // stash the new course module information for this successor
00120             $cminfo['modulename'] = $successor->get_modname();
00121             $this->converter->set_stash('cminfo_'.$cminfo['modulename'], $cminfo, $instanceid);
00122 
00123             // rewrite the coursecontents stash
00124             $coursecontents = $this->converter->get_stash('coursecontents');
00125             $coursecontents[$cminfo['id']]['modulename'] = $successor->get_modname();
00126             $this->converter->set_stash('coursecontents', $coursecontents);
00127 
00128             // delegate the processing to the successor handler
00129             return $successor->process_legacy_resource($data, $raw);
00130         }
00131 
00132         // no successor is interested in this record, convert it to the new mod_resource (aka File module)
00133 
00134         $resource = array();
00135         $resource['id']              = $data['id'];
00136         $resource['name']            = $data['name'];
00137         $resource['intro']           = $data['intro'];
00138         $resource['introformat']     = $data['introformat'];
00139         $resource['tobemigrated']    = 0;
00140         $resource['legacyfiles']     = RESOURCELIB_LEGACYFILES_ACTIVE;
00141         $resource['legacyfileslast'] = null;
00142         $resource['filterfiles']     = 0;
00143         $resource['revision']        = 1;
00144         $resource['timemodified']    = $data['timemodified'];
00145 
00146         // populate display and displayoptions fields
00147         $options = array('printheading' => 0, 'printintro' => 1);
00148         if ($data['options'] == 'frame') {
00149             $resource['display'] = RESOURCELIB_DISPLAY_FRAME;
00150 
00151         } else if ($data['options'] == 'objectframe') {
00152             $resource['display'] = RESOURCELIB_DISPLAY_EMBED;
00153 
00154         } else if ($data['options'] == 'forcedownload') {
00155             $resource['display'] = RESOURCELIB_DISPLAY_DOWNLOAD;
00156 
00157         } else if ($data['popup']) {
00158             $resource['display'] = RESOURCELIB_DISPLAY_POPUP;
00159             $rawoptions = explode(',', $data['popup']);
00160             foreach ($rawoptions as $rawoption) {
00161                 list($name, $value) = explode('=', trim($rawoption), 2);
00162                 if ($value > 0 and ($name == 'width' or $name == 'height')) {
00163                     $options['popup'.$name] = $value;
00164                     continue;
00165                 }
00166             }
00167 
00168         } else {
00169             $resource['display'] = RESOURCELIB_DISPLAY_AUTO;
00170         }
00171         $resource['displayoptions'] = serialize($options);
00172 
00173         // get the course module id and context id
00174         $instanceid     = $resource['id'];
00175         $currentcminfo  = $this->get_cminfo($instanceid);
00176         $moduleid       = $currentcminfo['id'];
00177         $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
00178 
00179         // get a fresh new file manager for this instance
00180         $this->fileman = $this->converter->get_file_manager($contextid, 'mod_resource');
00181 
00182         // convert course files embedded into the intro
00183         $this->fileman->filearea = 'intro';
00184         $this->fileman->itemid   = 0;
00185         $resource['intro'] = moodle1_converter::migrate_referenced_files($resource['intro'], $this->fileman);
00186 
00187         // convert the referenced file itself as a main file in the content area
00188         $reference = $data['reference'];
00189         if (strpos($reference, '$@FILEPHP@$') === 0) {
00190             $reference = str_replace(array('$@FILEPHP@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$'), array('', '/', ''), $reference);
00191         }
00192         $this->fileman->filearea = 'content';
00193         $this->fileman->itemid   = 0;
00194         try {
00195             $this->fileman->migrate_file('course_files/'.$reference, '/', null, 1);
00196         } catch (moodle1_convert_exception $e) {
00197             // the file probably does not exist
00198             $this->log('error migrating the resource main file', backup::LOG_WARNING, 'course_files/'.$reference);
00199         }
00200 
00201         // write resource.xml
00202         $this->open_xml_writer("activities/resource_{$moduleid}/resource.xml");
00203         $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
00204             'modulename' => 'resource', 'contextid' => $contextid));
00205         $this->write_xml('resource', $resource, array('/resource/id'));
00206         $this->xmlwriter->end_tag('activity');
00207         $this->close_xml_writer();
00208 
00209         // write inforef.xml
00210         $this->open_xml_writer("activities/resource_{$currentcminfo['id']}/inforef.xml");
00211         $this->xmlwriter->begin_tag('inforef');
00212         $this->xmlwriter->begin_tag('fileref');
00213         foreach ($this->fileman->get_fileids() as $fileid) {
00214             $this->write_xml('file', array('id' => $fileid));
00215         }
00216         $this->xmlwriter->end_tag('fileref');
00217         $this->xmlwriter->end_tag('inforef');
00218         $this->close_xml_writer();
00219     }
00220 
00224     public function on_resource_end(array $data) {
00225         if ($successor = $this->get_successor($data['type'], $data['reference'])) {
00226             $successor->on_legacy_resource_end($data);
00227         }
00228     }
00229 
00231 
00240     protected function get_successor($type, $reference) {
00241 
00242         switch ($type) {
00243             case 'text':
00244             case 'html':
00245                 $name = 'page';
00246                 break;
00247             case 'directory':
00248                 $name = 'folder';
00249                 break;
00250             case 'ims':
00251                 $name = 'imscp';
00252                 break;
00253             case 'file':
00254                 // if starts with $@FILEPHP@$ then it is URL link to a local course file
00255                 // to be migrated to the new resource module
00256                 if (strpos($reference, '$@FILEPHP@$') === 0) {
00257                     $name = null;
00258                     break;
00259                 }
00260                 // if http:// https:// ftp:// OR starts with slash need to be converted to URL
00261                 if (strpos($reference, '://') or strpos($reference, '/') === 0) {
00262                     $name = 'url';
00263                 } else {
00264                     $name = null;
00265                 }
00266                 break;
00267             default:
00268                 throw new moodle1_convert_exception('unknown_resource_successor', $type);
00269         }
00270 
00271         if (is_null($name)) {
00272             return null;
00273         }
00274 
00275         if (!isset($this->successors[$name])) {
00276             $this->log('preparing resource successor handler', backup::LOG_DEBUG, $name);
00277             $class = 'moodle1_mod_'.$name.'_handler';
00278             $this->successors[$name] = new $class($this->converter, 'mod', $name);
00279 
00280             // add the successor into the modlist stash
00281             $modnames = $this->converter->get_stash('modnameslist');
00282             $modnames[] = $name;
00283             $modnames = array_unique($modnames); // should not be needed but just in case
00284             $this->converter->set_stash('modnameslist', $modnames);
00285 
00286             // add the successor's modinfo stash
00287             $modinfo = $this->converter->get_stash('modinfo_resource');
00288             $modinfo['name'] = $name;
00289             $modinfo['instances'] = array();
00290             $this->converter->set_stash('modinfo_'.$name, $modinfo);
00291         }
00292 
00293         return $this->successors[$name];
00294      }
00295 }
 All Data Structures Namespaces Files Functions Variables Enumerations