MDL-19814 Upgraded calls to helpbutton, print_simple_box* and notify
[moodle.git] / mod / imscp / db / upgradelib.php
CommitLineData
2ff648fb 1<?php
2
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/>.
17
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 */
25
26/**
27 * Migrate imscp module data from 1.9 resource_old table to new imscp table
28 * @return void
29 */
30function imscp_20_migrate() {
a77e87bd 31 global $CFG, $DB, $OUTPUT;
2ff648fb 32 require_once("$CFG->libdir/filelib.php");
33 require_once("$CFG->dirroot/course/lib.php");
34 require_once("$CFG->dirroot/mod/imscp/locallib.php");
35
36 if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) {
37 // bad luck, somebody deleted resource module
38 return;
39 }
40
41 require_once("$CFG->dirroot/mod/resource/db/upgradelib.php");
42
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 }
48
49 if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'ims', 'migrated'=>0))) {
50 return;
51 }
52
53 $fs = get_file_storage();
54
55 foreach ($candidates as $candidate) {
56 upgrade_set_timeout(60);
57
58 $imscp = new object();
59 $imscp->course = $candidate->course;
60 $imscp->name = $candidate->name;
61 $imscp->intro = $candidate->intro;
62 $imscp->introformat = $candidate->introformat;
63 $imscp->revision = 1;
64 $imscp->keepold = 1;
65 $imscp->timemodified = time();
66
67 if (!$imscp = resource_migrate_to_module('imscp', $candidate, $imscp)) {
68 continue;
69 }
70
71 $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
72 $root = "$CFG->dataroot/$candidate->course/$CFG->moddata/resource/$candidate->oldid";
73
74 // migrate package backup file
75 if ($candidate->reference) {
76 $package = basename($candidate->reference);
77 $fullpath = $root.'/'.$package;
78 if (file_exists($fullpath)) {
79 $file_record = array('contextid' => $context->id,
80 'filearea' => 'imscp_backup',
81 'itemid' => 1,
82 'filepath' => '/',
83 'filename' => $package);
84 $fs->create_file_from_pathname($file_record, $fullpath);
85 }
86 }
87
88 // migrate extracted package data
89 $files = imsc_migrate_get_old_files($root, '');
90 $file_record = array('contextid'=>$context->id, 'filearea'=>'imscp_content', 'itemid'=>1);
91 $error = false;
92 foreach ($files as $relname=>$fullpath) {
93 $parts = explode('/', $relname);
94 $file_record['filename'] = array_pop($parts);
95 $parts[] = ''; // keep trailing slash
96 $file_record['filepath'] = implode('/', $parts);
97
98 try {
99 $fs->create_file_from_pathname($file_record, $fullpath);
100 } catch (Exception $e) {
101 //continue on error, we can not recover anyway
102 $error = true;
a77e87bd 103 echo $OUTPUT->notification("IMSCP: failed migrating file: $relname");
2ff648fb 104 }
105 }
106 unset($files);
107
108 // parse manifest
109 $structure = imscp_parse_structure($imscp, $context);
110 $imscp->structure = is_array($structure) ? serialize($structure) : null;
111 $DB->update_record('imscp', $imscp);
112
113 // remove old moddata dir only if no error and manifest ok
114 if (!$error and is_array($structure)) {
115 fulldelete($root);
116 }
117 }
118
119 $candidates->close();
120
121 // clear all course modinfo caches
122 rebuild_course_cache(0, true);
123}
124
125/**
126 * Private function returning all extracted IMS content package file
127 */
128function imsc_migrate_get_old_files($path, $relative) {
129 $result = array();
130 $items = new DirectoryIterator($path);
131 foreach ($items as $item) {
132 if ($item->isDot() or $item->isLink()) {
133 // symbolik links could create infinite loops or cause unintended file migration, sorry
134 continue;
135 }
136 $pathname = $item->getPathname();
137 $relname = $relative.'/'.$item->getFilename();
138 if ($item->isFile()) {
139 $result[$relname] = $pathname;
140 } else if ($item->isDir()) {
141 $result = array_merge($result, imsc_migrate_get_old_files($pathname, $relname));
142 }
143 unset($item);
144 }
145 unset($items);
146 return $result;
a77e87bd 147}