3 // This file keeps track of upgrades to
6 // Sometimes, changes between versions involve
7 // alterations to database structures and other
8 // major things that may break installations.
10 // The upgrade function in this file will attempt
11 // to perform all the necessary actions to upgrade
12 // your older installation to the current version.
14 // If there's something it cannot do itself, it
15 // will tell you what you need to do.
17 // The commands in here will all be database-neutral,
18 // using the methods of database_manager class
20 // Please do not forget to use upgrade_set_timeout()
21 // before any action that may take longer time to finish.
23 function xmldb_data_upgrade($oldversion) {
24 global $CFG, $DB, $OUTPUT;
26 $dbman = $DB->get_manager();
29 // Moodle v2.2.0 release upgrade line
30 // Put any upgrade step following this
32 // Moodle v2.3.0 release upgrade line
33 // Put any upgrade step following this
35 if ($oldversion < 2012112901) {
36 // Check if there is a directory containing any old presets.
37 $olddatadir = $CFG->dataroot . '/data';
38 $oldpresetdir = "$olddatadir/preset";
39 if (file_exists($oldpresetdir)) {
40 // Get directory contents.
41 $userfolders = new DirectoryIterator($oldpresetdir);
42 // Store the system context, these are site wide presets.
43 $context = context_system::instance();
44 // Create file storage object.
45 $fs = get_file_storage();
46 // Create array of accepted files.
47 $arracceptedfilenames = array('singletemplate.html', 'listtemplateheader.html', 'listtemplate.html',
48 'listtemplatefooter.html', 'addtemplate.html', 'rsstemplate.html',
49 'rsstitletemplate.html', 'csstemplate.css', 'jstemplate.js',
50 'asearchtemplate.html', 'preset.xml');
51 // Loop through all the folders, they should represent userids.
52 foreach ($userfolders as $userfolder) {
53 // If it is a file, skip it.
54 if ($userfolder->isFile()) {
57 // The folder name should represent the user id.
58 $userid = $userfolder->getFilename();
59 // Skip if it is not numeric.
60 if (!is_numeric($userid)) {
63 // Skip if the number does not correspond to a user (does not matter if user was deleted).
64 if (!$DB->record_exists('user', array('id' => $userid))) {
68 $presetfolders = new DirectoryIterator("$oldpresetdir/$userid");
69 foreach ($presetfolders as $presetfolder) {
70 // If it is a file, skip it.
71 if ($presetfolder->isFile()) {
74 // Save the name of the preset.
75 $presetname = $presetfolder->getFilename();
76 // Get the files in this preset folder.
77 $presetfiles = new DirectoryIterator("$oldpresetdir/$userid/$presetname");
78 // Now we want to get the contents of the presets.
79 foreach ($presetfiles as $file) {
80 // If it is not a file, skip it.
81 if (!$file->isFile()) {
85 $filename = $file->getFilename();
86 // If it is not in the array of accepted file names skip it.
87 if (!in_array($filename, $arracceptedfilenames)) {
90 // Store the full file path.
91 $fullfilepath = "$oldpresetdir/$userid/$presetname/$filename";
92 // Create file record.
93 $filerecord = array('contextid' => $context->id,
94 'component' => 'mod_data',
95 'filearea' => 'site_presets',
97 'filename' => $filename,
99 // Check to ensure it does not already exists in the file directory.
100 if (!$fs->file_exists($context->id, 'mod_data', 'site_presets', 0, '/' . $presetfolder . '/', $filename)) {
101 $filerecord['filepath'] = '/' . $presetfolder . '/';
103 $filerecord['filepath'] = '/' . $presetfolder . '_' . $userid . '_old/';
105 $fs->create_file_from_pathname($filerecord, $fullfilepath);
107 @unlink($fullfilepath);
109 // Remove the preset directory.
110 @rmdir("$oldpresetdir/$userid/$presetname");
112 // Remove the user directory.
113 @rmdir("$oldpresetdir/$userid");
115 // Remove the final directories.
116 @rmdir("$oldpresetdir");
117 @rmdir("$olddatadir");
120 upgrade_mod_savepoint(true, 2012112901, 'data');
123 // Moodle v2.4.0 release upgrade line
124 // Put any upgrade step following this
127 // Moodle v2.5.0 release upgrade line.
128 // Put any upgrade step following this.
131 // Moodle v2.6.0 release upgrade line.
132 // Put any upgrade step following this.
134 // Moodle v2.7.0 release upgrade line.
135 // Put any upgrade step following this.
137 // Moodle v2.8.0 release upgrade line.
138 // Put any upgrade step following this.
140 if ($oldversion < 2015030900) {
141 // Define field required to be added to data_fields.
142 $table = new xmldb_table('data_fields');
143 $field = new xmldb_field('required', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'description');
145 // Conditionally launch add field required.
146 if (!$dbman->field_exists($table, $field)) {
147 $dbman->add_field($table, $field);
150 upgrade_mod_savepoint(true, 2015030900, 'data');
153 // Moodle v2.9.0 release upgrade line.
154 // Put any upgrade step following this.
156 if ($oldversion < 2015092200) {
158 // Define field manageapproved to be added to data.
159 $table = new xmldb_table('data');
160 $field = new xmldb_field('manageapproved', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1', 'approval');
162 // Conditionally launch add field manageapproved.
163 if (!$dbman->field_exists($table, $field)) {
164 $dbman->add_field($table, $field);
167 // Data savepoint reached.
168 upgrade_mod_savepoint(true, 2015092200, 'data');