fixed missing global
[moodle.git] / mod / lesson / db / upgradelib.php
CommitLineData
8d1a3963
PS
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 * Resource module upgrade related helper functions
20 *
21 * @package mod
22 * @subpackage lesson
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27defined('MOODLE_INTERNAL') || die;
28
29/**
30 * Migrate lesson images incorrectly placed into moddata folder,
31 * the images were stored in random directories ignoring all coding rules.
32 *
33 * @return void
34 */
35function lesson_20_migrate_moddata_mixture($courseid, $path) {
cc83b880 36 global $CFG, $DB, $OUTPUT;
8d1a3963
PS
37
38 $fullpathname = "$CFG->dataroot/$courseid".$path;
39
40 if (!file_exists($fullpathname)) {
41 return;
42 }
43
44 $context = get_context_instance(CONTEXT_COURSE, $courseid);
45 $items = new DirectoryIterator($fullpathname);
46 $fs = get_file_storage();
47
48 foreach ($items as $item) {
49 if ($item->isDot()) {
50 // skip
51 continue;
52 }
53
54 if ($item->isFile()) {
55 if (!$item->isReadable()) {
56 echo $OUTPUT->notification(" File not readable, skipping: ".$courseid.$pathname.$item->getFilename());
57 continue;
58 }
59
60 $filepath = clean_param($path, PARAM_PATH);
61 $filename = clean_param($item->getFilename(), PARAM_FILE);
62
63 if ($filename === '') {
64 //unsupported chars, sorry
65 continue;
66 }
67
68 if (!$fs->file_exists($context->id, 'course', 'legacy', '0', $filepath, $filename)) {
69 $file_record = array('contextid'=>$context->id, 'component'=>'course', 'filearea'=>'legacy', 'itemid'=>0, 'filepath'=>$filepath, 'filename'=>$filename,
70 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
71 $fs->create_file_from_pathname($file_record, $fullpathname.$item->getFilename());
72 @unlink($fullpathname.$item->getFilename());
73 }
74
75 } else {
76 //migrate recursively all subdirectories
77 lesson_20_migrate_moddata_mixture($courseid, $path.$item->getFilename().'/');
78 }
79 }
80
81 unset($item); //release file handles
82 unset($items); //release file handles
83
84 // delete dir if empty
85 @rmdir($fullpathname);
86}