a6e28404e9e082f6dd44006add9c78af2064852c
[moodle.git] / mod / imscp / db / upgradelib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
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.
9 //
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.
14 //
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/>.
18 /**
19  * IMS CP module upgrade related helper functions
20  *
21  * @package   mod-imscp
22  * @copyright 2009 Petr Skoda (http://skodak.org)
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 /**
27  * Migrate imscp module data from 1.9 resource_old table to new imscp table
28  * @return void
29  */
30 function imscp_20_migrate() {
31     global $CFG, $DB, $OUTPUT;
32     require_once("$CFG->libdir/filelib.php");
33     require_once("$CFG->dirroot/course/lib.php");
34     require_once("$CFG->dirroot/mod/imscp/locallib.php");
36     if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) {
37         // bad luck, somebody deleted resource module
38         return;
39     }
41     require_once("$CFG->dirroot/mod/resource/db/upgradelib.php");
43     // create resource_old table and copy resource table there if needed
44     if (!resource_20_prepare_migration()) {
45         // no modules or fresh install
46         return;
47     }
49     if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'ims', 'migrated'=>0))) {
50         return;
51     }
53     $fs = get_file_storage();
55     foreach ($candidates as $candidate) {
56         upgrade_set_timeout(60);
58         if ($CFG->texteditors !== 'textarea') {
59             $intro       = text_to_html($candidate->intro, false, false, true);
60             $introformat = FORMAT_HTML;
61         } else {
62             $intro       = $candidate->intro;
63             $introformat = FORMAT_MOODLE;
64         }
66         $imscp = new object();
67         $imscp->course       = $candidate->course;
68         $imscp->name         = $candidate->name;
69         $imscp->intro        = $intro;
70         $imscp->introformat  = $introformat;
71         $imscp->revision     = 1;
72         $imscp->keepold      = 1;
73         $imscp->timemodified = time();
75         if (!$imscp = resource_migrate_to_module('imscp', $candidate, $imscp)) {
76             continue;
77         }
79         $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
80         $root = "$CFG->dataroot/$candidate->course/$CFG->moddata/resource/$candidate->oldid";
82         // migrate package backup file
83         if ($candidate->reference) {
84             $package = basename($candidate->reference);
85             $fullpath = $root.'/'.$package;
86             if (file_exists($fullpath)) {
87                 $file_record = array('contextid' => $context->id,
88                                      'component' => 'mod_imscp',
89                                      'filearea'  => 'backup',
90                                      'itemid'    => 1,
91                                      'filepath'  => '/',
92                                      'filename'  => $package);
93                 $fs->create_file_from_pathname($file_record, $fullpath);
94             }
95         }
97         // migrate extracted package data
98         $files = imsc_migrate_get_old_files($root, '');
99         if (empty($files)) {
100             // if ims package doesn't exist, continue loop
101             echo $OUTPUT->notification("IMS package data cannot be found, failed migrating activity: \"$candidate->name\", please fix it manually");
102             continue;
103         }
105         $file_record = array('contextid'=>$context->id, 'component'=>'mod_imscp', 'filearea'=>'content', 'itemid'=>1);
106         $error = false;
107         foreach ($files as $relname=>$fullpath) {
108             $parts = explode('/', $relname);
109             $file_record['filename'] = array_pop($parts);
110             $parts[] = ''; // keep trailing slash
111             $file_record['filepath'] = implode('/', $parts);
113             try {
114                 $fs->create_file_from_pathname($file_record, $fullpath);
115             } catch (Exception $e) {
116                 //continue on error, we can not recover anyway
117                 $error = true;
118                 echo $OUTPUT->notification("IMSCP: failed migrating file: $relname");
119             }
120         }
121         unset($files);
123         // parse manifest
124         $structure = imscp_parse_structure($imscp, $context);
125         $imscp->structure = is_array($structure) ? serialize($structure) : null;
126         $DB->update_record('imscp', $imscp);
128         // remove old moddata dir only if no error and manifest ok
129         if (!$error and is_array($structure)) {
130             fulldelete($root);
131         }
132     }
134     $candidates->close();
136     // clear all course modinfo caches
137     rebuild_course_cache(0, true);
140 /**
141  * Private function returning all extracted IMS content package file
142  */
143 function imsc_migrate_get_old_files($path, $relative) {
144     global $OUTPUT;
145     $result = array();
146     if (!file_exists($path)) {
147         echo $OUTPUT->notification("File path doesn't exist: $path <br/> Please fix it manually.");
148         return array();
149     }
150     $items = new DirectoryIterator($path);
151     foreach ($items as $item) {
152         if ($item->isDot() or $item->isLink()) {
153             // symbolik links could create infinite loops or cause unintended file migration, sorry
154             continue;
155         }
156         $pathname = $item->getPathname();
157         $relname  = $relative.'/'.$item->getFilename();
158         if ($item->isFile()) {
159             $result[$relname] = $pathname;
160         } else if ($item->isDir()) {
161             $result = array_merge($result, imsc_migrate_get_old_files($pathname, $relname));
162         }
163         unset($item);
164     }
165     unset($items);
166     return $result;