MDL-20066 new folder module, includes migrate from old mod/resource; remaining issues...
[moodle.git] / mod / folder / db / upgradelib.php
CommitLineData
0e16b939 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 * Folder module upgrade related helper functions
20 *
21 * @package mod-folder
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 folder module data from 1.9 resource_old table to new older table
28 * @return void
29 */
30function folder_20_migrate() {
31 global $CFG, $DB;
32 require_once("$CFG->libdir/filelib.php");
33 require_once("$CFG->dirroot/course/lib.php");
34
35 if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) {
36 // bad luck, somebody deleted resource module
37 return;
38 }
39
40 require_once("$CFG->dirroot/mod/resource/db/upgradelib.php");
41
42 // create resource_old table and copy resource table there if needed
43 if (!resource_20_prepare_migration()) {
44 // no modules or fresh install
45 return;
46 }
47
48 if (!$candidates = $DB->get_recordset('resource_old', array('type'=>'directory', 'migrated'=>0))) {
49 return;
50 }
51
52 $fs = get_file_storage();
53
54 foreach ($candidates as $candidate) {
55 upgrade_set_timeout();
56
57 $directory = '/'.trim($candidate->reference, '/').'/';
58 $directory = str_replace('//', '/', $directory);
59
60 $folder = new object();
61 $folder->course = $candidate->course;
62 $folder->name = $candidate->name;
63 $folder->intro = $candidate->intro;
64 $folder->introformat = $candidate->introformat;
65 $folder->revision = 1;
66 $folder->timemodified = time();
67
68 if (!$folder = resource_migrate_to_module('folder', $candidate, $folder)) {
69 continue;
70 }
71
72 // copy files in given directory, skip moddata and backups!
73 $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
74 $coursecontext = get_context_instance(CONTEXT_COURSE, $candidate->course);
75 $files = $fs->get_directory_files($coursecontext->id, 'course_content', 0, $directory, true, true);
76 $file_record = array('contextid'=>$context->id, 'filearea'=>'folder_content', 'itemid'=>0);
77 foreach ($files as $file) {
78 $path = $file->get_filepath();
79 if (stripos($path, '/backupdata/') === 0 or stripos($path, '/moddata/') === 0) {
80 // do not publish protected data!
81 continue;
82 }
83 $relpath = substr($path, strlen($directory) - 1); // keep only subfolder paths
84 $file_record['filepath'] = $relpath;
85 $fs->create_file_from_storedfile($file_record, $file);
86 }
87 }
88
89 $candidates->close();
90
91 // clear all course modinfo caches
92 rebuild_course_cache(0, true);
93}