PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/resource/backup/moodle1/lib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 305 lines | 179 code | 40 blank | 86 comment | 30 complexity | bb368a63e2ceebdda7c8e8c10eccc05f MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Provides support for the conversion of moodle1 backup to the moodle2 format
  18. *
  19. * @package mod
  20. * @subpackage resource
  21. * @copyright 2011 Andrew Davis <andrew@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /**
  26. * Resource conversion handler
  27. */
  28. class moodle1_mod_resource_handler extends moodle1_mod_handler {
  29. /** @var moodle1_file_manager instance */
  30. protected $fileman = null;
  31. /** @var array of resource successors handlers */
  32. private $successors = array();
  33. /**
  34. * Declare the paths in moodle.xml we are able to convert
  35. *
  36. * The method returns list of {@link convert_path} instances.
  37. * For each path returned, the corresponding conversion method must be
  38. * defined.
  39. *
  40. * Note that the paths /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE do not
  41. * actually exist in the file. The last element with the module name was
  42. * appended by the moodle1_converter class.
  43. *
  44. * @return array of {@link convert_path} instances
  45. */
  46. public function get_paths() {
  47. return array(
  48. new convert_path(
  49. 'resource', '/MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE',
  50. array(
  51. 'renamefields' => array(
  52. 'summary' => 'intro',
  53. ),
  54. 'newfields' => array(
  55. 'introformat' => 0,
  56. ),
  57. 'dropfields' => array(
  58. 'modtype',
  59. ),
  60. )
  61. )
  62. );
  63. }
  64. /**
  65. * Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data
  66. *
  67. * This methods detects the resource type and eventually re-dispatches it to the
  68. * corresponding resource successor (url, forum, page, imscp).
  69. */
  70. public function process_resource(array $data, array $raw) {
  71. global $CFG;
  72. require_once("$CFG->libdir/resourcelib.php");
  73. // replay the upgrade step 2009042001
  74. if ($CFG->texteditors !== 'textarea') {
  75. $data['intro'] = text_to_html($data['intro'], false, false, true);
  76. $data['introformat'] = FORMAT_HTML;
  77. }
  78. // fix invalid null popup and options data
  79. if (!array_key_exists('popup', $data) or is_null($data['popup'])) {
  80. $data['popup'] = '';
  81. }
  82. if (!array_key_exists ('options', $data) or is_null($data['options'])) {
  83. $data['options'] = '';
  84. }
  85. // decide if the legacy resource should be handled by a successor module
  86. if ($successor = $this->get_successor($data['type'], $data['reference'])) {
  87. // the instance id will be kept
  88. $instanceid = $data['id'];
  89. // move the instance from the resource's modinfo stash to the successor's
  90. // modinfo stash
  91. $resourcemodinfo = $this->converter->get_stash('modinfo_resource');
  92. $successormodinfo = $this->converter->get_stash('modinfo_'.$successor->get_modname());
  93. $successormodinfo['instances'][$instanceid] = $resourcemodinfo['instances'][$instanceid];
  94. unset($resourcemodinfo['instances'][$instanceid]);
  95. $this->converter->set_stash('modinfo_resource', $resourcemodinfo);
  96. $this->converter->set_stash('modinfo_'.$successor->get_modname(), $successormodinfo);
  97. // get the course module information for the legacy resource module
  98. $cminfo = $this->get_cminfo($instanceid);
  99. // use the version of the successor instead of the current mod/resource
  100. // beware - the version.php declares info via $module object, do not use
  101. // a variable of such name here
  102. $module = new stdClass();
  103. include $CFG->dirroot.'/mod/'.$successor->get_modname().'/version.php';
  104. $cminfo['version'] = $module->version;
  105. // stash the new course module information for this successor
  106. $cminfo['modulename'] = $successor->get_modname();
  107. $this->converter->set_stash('cminfo_'.$cminfo['modulename'], $cminfo, $instanceid);
  108. // rewrite the coursecontents stash
  109. $coursecontents = $this->converter->get_stash('coursecontents');
  110. $coursecontents[$cminfo['id']]['modulename'] = $successor->get_modname();
  111. $this->converter->set_stash('coursecontents', $coursecontents);
  112. // delegate the processing to the successor handler
  113. return $successor->process_legacy_resource($data, $raw);
  114. }
  115. // no successor is interested in this record, convert it to the new mod_resource (aka File module)
  116. $resource = array();
  117. $resource['id'] = $data['id'];
  118. $resource['name'] = $data['name'];
  119. $resource['intro'] = $data['intro'];
  120. $resource['introformat'] = $data['introformat'];
  121. $resource['tobemigrated'] = 0;
  122. $resource['legacyfiles'] = RESOURCELIB_LEGACYFILES_ACTIVE;
  123. $resource['legacyfileslast'] = null;
  124. $resource['filterfiles'] = 0;
  125. $resource['revision'] = 1;
  126. $resource['timemodified'] = $data['timemodified'];
  127. // populate display and displayoptions fields
  128. $options = array('printheading' => 0, 'printintro' => 1);
  129. if ($data['options'] == 'frame') {
  130. $resource['display'] = RESOURCELIB_DISPLAY_FRAME;
  131. } else if ($data['options'] == 'objectframe') {
  132. $resource['display'] = RESOURCELIB_DISPLAY_EMBED;
  133. } else if ($data['options'] == 'forcedownload') {
  134. $resource['display'] = RESOURCELIB_DISPLAY_DOWNLOAD;
  135. } else if ($data['popup']) {
  136. $resource['display'] = RESOURCELIB_DISPLAY_POPUP;
  137. $rawoptions = explode(',', $data['popup']);
  138. foreach ($rawoptions as $rawoption) {
  139. list($name, $value) = explode('=', trim($rawoption), 2);
  140. if ($value > 0 and ($name == 'width' or $name == 'height')) {
  141. $options['popup'.$name] = $value;
  142. continue;
  143. }
  144. }
  145. } else {
  146. $resource['display'] = RESOURCELIB_DISPLAY_AUTO;
  147. }
  148. $resource['displayoptions'] = serialize($options);
  149. // get the course module id and context id
  150. $instanceid = $resource['id'];
  151. $currentcminfo = $this->get_cminfo($instanceid);
  152. $moduleid = $currentcminfo['id'];
  153. $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $moduleid);
  154. // get a fresh new file manager for this instance
  155. $this->fileman = $this->converter->get_file_manager($contextid, 'mod_resource');
  156. // convert course files embedded into the intro
  157. $this->fileman->filearea = 'intro';
  158. $this->fileman->itemid = 0;
  159. $resource['intro'] = moodle1_converter::migrate_referenced_files($resource['intro'], $this->fileman);
  160. // convert the referenced file itself as a main file in the content area
  161. $reference = $data['reference'];
  162. if (strpos($reference, '$@FILEPHP@$') === 0) {
  163. $reference = str_replace(array('$@FILEPHP@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$'), array('', '/', ''), $reference);
  164. }
  165. $this->fileman->filearea = 'content';
  166. $this->fileman->itemid = 0;
  167. // Rebuild the file path.
  168. $curfilepath = '/';
  169. if ($reference) {
  170. $curfilepath = pathinfo('/'.$reference, PATHINFO_DIRNAME);
  171. if ($curfilepath != '/') {
  172. $curfilepath .= '/';
  173. }
  174. }
  175. try {
  176. $this->fileman->migrate_file('course_files/'.$reference, $curfilepath, null, 1);
  177. } catch (moodle1_convert_exception $e) {
  178. // the file probably does not exist
  179. $this->log('error migrating the resource main file', backup::LOG_WARNING, 'course_files/'.$reference);
  180. }
  181. // write resource.xml
  182. $this->open_xml_writer("activities/resource_{$moduleid}/resource.xml");
  183. $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $moduleid,
  184. 'modulename' => 'resource', 'contextid' => $contextid));
  185. $this->write_xml('resource', $resource, array('/resource/id'));
  186. $this->xmlwriter->end_tag('activity');
  187. $this->close_xml_writer();
  188. // write inforef.xml
  189. $this->open_xml_writer("activities/resource_{$currentcminfo['id']}/inforef.xml");
  190. $this->xmlwriter->begin_tag('inforef');
  191. $this->xmlwriter->begin_tag('fileref');
  192. foreach ($this->fileman->get_fileids() as $fileid) {
  193. $this->write_xml('file', array('id' => $fileid));
  194. }
  195. $this->xmlwriter->end_tag('fileref');
  196. $this->xmlwriter->end_tag('inforef');
  197. $this->close_xml_writer();
  198. }
  199. /**
  200. * Give succesors a chance to finish their job
  201. */
  202. public function on_resource_end(array $data) {
  203. if ($successor = $this->get_successor($data['type'], $data['reference'])) {
  204. $successor->on_legacy_resource_end($data);
  205. }
  206. }
  207. /// internal implementation details follow /////////////////////////////////
  208. /**
  209. * Returns the handler of the new 2.0 mod type according the given type of the legacy 1.9 resource
  210. *
  211. * @param string $type the value of the 'type' field in 1.9 resource
  212. * @param string $reference a file path. Necessary to differentiate files from web URLs
  213. * @throws moodle1_convert_exception for the unknown types
  214. * @return null|moodle1_mod_handler the instance of the handler, or null if the type does not have a successor
  215. */
  216. protected function get_successor($type, $reference) {
  217. switch ($type) {
  218. case 'text':
  219. case 'html':
  220. $name = 'page';
  221. break;
  222. case 'directory':
  223. $name = 'folder';
  224. break;
  225. case 'ims':
  226. $name = 'imscp';
  227. break;
  228. case 'file':
  229. // if starts with $@FILEPHP@$ then it is URL link to a local course file
  230. // to be migrated to the new resource module
  231. if (strpos($reference, '$@FILEPHP@$') === 0) {
  232. $name = null;
  233. break;
  234. }
  235. // if http:// https:// ftp:// OR starts with slash need to be converted to URL
  236. if (strpos($reference, '://') or strpos($reference, '/') === 0) {
  237. $name = 'url';
  238. } else {
  239. $name = null;
  240. }
  241. break;
  242. default:
  243. throw new moodle1_convert_exception('unknown_resource_successor', $type);
  244. }
  245. if (is_null($name)) {
  246. return null;
  247. }
  248. if (!isset($this->successors[$name])) {
  249. $this->log('preparing resource successor handler', backup::LOG_DEBUG, $name);
  250. $class = 'moodle1_mod_'.$name.'_handler';
  251. $this->successors[$name] = new $class($this->converter, 'mod', $name);
  252. // add the successor into the modlist stash
  253. $modnames = $this->converter->get_stash('modnameslist');
  254. $modnames[] = $name;
  255. $modnames = array_unique($modnames); // should not be needed but just in case
  256. $this->converter->set_stash('modnameslist', $modnames);
  257. // add the successor's modinfo stash
  258. $modinfo = $this->converter->get_stash('modinfo_resource');
  259. $modinfo['name'] = $name;
  260. $modinfo['instances'] = array();
  261. $this->converter->set_stash('modinfo_'.$name, $modinfo);
  262. }
  263. return $this->successors[$name];
  264. }
  265. }