3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * URL module upgrade related helper functions
21 * @package url-resource
22 * @copyright 2009 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 * Migrate url module data from 1.9 resource_old table to new url table
30 function url_20_migrate() {
33 require_once("$CFG->libdir/filelib.php");
34 require_once("$CFG->libdir/resourcelib.php");
35 require_once("$CFG->dirroot/course/lib.php");
37 if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) {
38 // bad luck, somebody deleted resource module
42 require_once("$CFG->dirroot/mod/resource/db/upgradelib.php");
44 // create resource_old table and copy resource table there if needed
45 if (!resource_20_prepare_migration()) {
46 // no modules or fresh install
50 if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'file', 'migrated'=>0))) {
54 foreach ($candidates as $candidate) {
55 $path = $candidate->reference;
56 $siteid = get_site()->id;
58 if (strpos($path, 'LOCALPATH') === 0) {
59 // ignore not maintained local files - sorry
61 } else if (!strpos($path, '://')) {
64 } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$siteid(/[^\s'\"&\?#]+)|", $path, $matches)) {
65 // handled by resource module
67 } else if (preg_match("|$CFG->wwwroot/file.php(\?file=)?/$candidate->course(/[^\s'\"&\?#]+)|", $path, $matches)) {
68 // handled by resource module
72 upgrade_set_timeout();
75 $url->course = $candidate->course;
76 $url->name = $candidate->name;
77 $url->intro = $candidate->intro;
78 $url->introformat = $candidate->introformat;
79 $url->externalurl = $path;
80 $url->timemodified = time();
82 $options = array('printheading'=>0, 'printintro'=>1);
83 $parameters = array();
84 if ($candidate->options == 'frame') {
85 $url->display = RESOURCELIB_DISPLAY_FRAME;
87 } else if ($candidate->options == 'objectframe') {
88 $url->display = RESOURCELIB_DISPLAY_EMBED;
90 } else if ($candidate->popup) {
91 $url->display = RESOURCELIB_DISPLAY_POPUP;
92 if ($candidate->popup) {
93 $rawoptions = explode(',', $candidate->popup);
94 foreach ($rawoptions as $rawoption) {
95 list($name, $value) = explode('=', trim($rawoption), 2);
96 if ($value > 0 and ($name == 'width' or $name == 'height')) {
97 $options['popup'.$name] = $value;
104 $url->display = RESOURCELIB_DISPLAY_AUTO;
106 $url->displayoptions = serialize($options);
108 if ($candidate->alltext) {
109 $rawoptions = explode(',', $candidate->alltext);
110 foreach ($rawoptions as $rawoption) {
111 list($variable, $parameter) = explode('=', trim($rawoption), 2);
112 $parameters[$parameter] = $variable;
116 $url->parameters = serialize($parameters);
118 if (!$url = resource_migrate_to_module('url', $candidate, $url)) {
123 $candidates->close();
125 // clear all course modinfo caches
126 rebuild_course_cache(0, true);