MDL-23204 convert text to html in folder intro
[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
d6ae4a4a
PS
60 if ($CFG->texteditors !== 'textarea') {
61 $intro = text_to_html($candidate->intro, false, false, true);
62 $introformat = FORMAT_HTML;
63 } else {
64 $intro = $candidate->intro;
65 $introformat = FORMAT_MOODLE;
66 }
67
0e16b939 68 $folder = new object();
69 $folder->course = $candidate->course;
70 $folder->name = $candidate->name;
d6ae4a4a
PS
71 $folder->intro = $intro;
72 $folder->introformat = $introformat;
0e16b939 73 $folder->revision = 1;
74 $folder->timemodified = time();
75
76 if (!$folder = resource_migrate_to_module('folder', $candidate, $folder)) {
77 continue;
78 }
79
80 // copy files in given directory, skip moddata and backups!
81 $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid);
82 $coursecontext = get_context_instance(CONTEXT_COURSE, $candidate->course);
64f93798
PS
83 $files = $fs->get_directory_files($coursecontext->id, 'course', 'legacy', 0, $directory, true, true);
84 $file_record = array('contextid'=>$context->id, 'component'=>'mod_folder', 'filearea'=>'content', 'itemid'=>0);
0e16b939 85 foreach ($files as $file) {
86 $path = $file->get_filepath();
87 if (stripos($path, '/backupdata/') === 0 or stripos($path, '/moddata/') === 0) {
88 // do not publish protected data!
89 continue;
90 }
91 $relpath = substr($path, strlen($directory) - 1); // keep only subfolder paths
92 $file_record['filepath'] = $relpath;
93 $fs->create_file_from_storedfile($file_record, $file);
94 }
95 }
96
97 $candidates->close();
98
99 // clear all course modinfo caches
100 rebuild_course_cache(0, true);
101}