mirrored from git://git.moodle.org/moodle.git
/
upgrade.php
203 lines (168 loc) · 8.7 KB
/
upgrade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// This file keeps track of upgrades to
// the assignment module
//
// Sometimes, changes between versions involve
// alterations to database structures and other
// major things that may break installations.
//
// The upgrade function in this file will attempt
// to perform all the necessary actions to upgrade
// your older installation to the current version.
//
// If there's something it cannot do itself, it
// will tell you what you need to do.
//
// The commands in here will all be database-neutral,
// using the methods of database_manager class
//
// Please do not forget to use upgrade_set_timeout()
// before any action that may take longer time to finish.
function xmldb_assignment_upgrade($oldversion) {
global $CFG, $DB, $OUTPUT;
$dbman = $DB->get_manager();
//===== 1.9.0 upgrade line ======//
if ($oldversion < 2007101511) {
// change grade typo to text if no grades MDL-13920
require_once $CFG->dirroot.'/mod/assignment/lib.php';
assignment_upgrade_grades();
upgrade_mod_savepoint(true, 2007101511, 'assignment');
}
if ($oldversion < 2008081900) {
/////////////////////////////////////
/// new file storage upgrade code ///
/////////////////////////////////////
$fs = get_file_storage();
$sqlfrom = "FROM {assignment_submissions} s
JOIN {assignment} a ON a.id = s.assignment
JOIN {modules} m ON m.name = 'assignment'
JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = a.id)";
$count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
$rs = $DB->get_recordset_sql("SELECT s.id, s.userid, s.teacher, s.assignment, a.course, cm.id AS cmid $sqlfrom ORDER BY a.course, s.assignment");
if ($rs->valid()) {
$pbar = new progress_bar('migrateassignmentfiles', 500, true);
$i = 0;
foreach ($rs as $submission) {
$i++;
upgrade_set_timeout(180); // set up timeout, may also abort execution
$pbar->update($i, $count, "Migrating assignment submissions - $i/$count.");
$basepath = "$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/";
if (!file_exists($basepath)) {
//no files
continue;
}
$context = get_context_instance(CONTEXT_MODULE, $submission->cmid);
// migrate submitted files first
$path = $basepath;
$items = new DirectoryIterator($path);
foreach ($items as $item) {
if (!$item->isFile()) {
continue;
}
if (!$item->isReadable()) {
echo $OUTPUT->notification(" File not readable, skipping: ".$path.$item->getFilename());
continue;
}
$filename = clean_param($item->getFilename(), PARAM_FILE);
if ($filename === '') {
continue;
}
if (!$fs->file_exists($context->id, 'mod_assignment', 'submission', $submission->id, '/', $filename)) {
$file_record = array('contextid'=>$context->id, 'component'=>'mod_assignment', 'filearea'=>'submission', 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
unlink($path.$item->getFilename());
}
}
}
unset($items); //release file handles
// migrate teacher response files for "upload" subtype, unfortunately we do not
$path = $basepath.'responses/';
if (file_exists($path)) {
$items = new DirectoryIterator($path);
foreach ($items as $item) {
if (!$item->isFile()) {
continue;
}
$filename = clean_param($item->getFilename(), PARAM_FILE);
if ($filename === '') {
continue;
}
if (!$fs->file_exists($context->id, 'mod_assignment', 'response', $submission->id, '/', $filename)) {
$file_record = array('contextid'=>$context->id, 'component'=>'mod_assignment', 'filearea'=>'response', 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename,
'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
if ($submission->teacher) {
$file_record['userid'] = $submission->teacher;
}
if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
unlink($path.$item->getFilename());
}
}
}
unset($items); //release file handles
@rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/responses");
}
// remove dirs if empty
@rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid");
@rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment");
@rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment");
}
}
$rs->close();
upgrade_mod_savepoint(true, 2008081900, 'assignment');
}
if ($oldversion < 2009042000) {
/// Rename field description on table assignment to intro
$table = new xmldb_table('assignment');
$field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'name');
/// Launch rename field description
$dbman->rename_field($table, $field, 'intro');
/// assignment savepoint reached
upgrade_mod_savepoint(true, 2009042000, 'assignment');
}
if ($oldversion < 2009042001) {
/// Rename field format on table assignment to introformat
$table = new xmldb_table('assignment');
$field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
/// Launch rename field format
$dbman->rename_field($table, $field, 'introformat');
/// assignment savepoint reached
upgrade_mod_savepoint(true, 2009042001, 'assignment');
}
// Moodle v2.1.0 release upgrade line
// Put any upgrade step following this
// Moodle v2.2.0 release upgrade line
// Put any upgrade step following this
if ($oldversion < 2012062800) {
// Fixed/updated numfiles field in assignment_submissions table to count the actual
// number of files has been uploaded.
upgrade_set_timeout(600); // increase excution time for in large sites
$fs = get_file_storage();
$selectcount = 'SELECT COUNT(s.id), cm.id AS cmid';
$select = 'SELECT s.id, cm.id AS cmid';
$query = " FROM {assignment_submissions} s
INNER JOIN {course_modules} cm
ON s.assignment = cm.instance
JOIN {assignment} a
ON a.id = s.assignment
WHERE a.assignmenttype in ('upload', 'uploadsingle') AND
cm.module = (SELECT id
FROM {modules}
WHERE name = 'assignment')";
$countsubmissions = $DB->count_records_sql($selectcount. $query);
$submissions = $DB->get_recordset_sql($select. $query);
$pbar = new progress_bar('assignmentupgradenumfiles', 500, true);
$i = 0;
foreach ($submissions as $sub) {
$i++;
if ($context = context_module::instance($sub->cmid)) {
$sub->numfiles = count($fs->get_area_files($context->id, 'mod_assignment', 'submission', $sub->id, 'sortorder', false));
$DB->update_record('assignment_submissions', $sub);
}
$pbar->update($i, $countsubmissions, "Counting files of submissions ($i/$countsubmissions)");
}
$submissions->close();
// assignment savepoint reached
upgrade_mod_savepoint(true, 2012062800, 'assignment');
}
return true;
}