MDL-22609 fixed regression in my previous commit, sorry
[moodle.git] / mod / assignment / db / upgrade.php
CommitLineData
e7521559 1<?php
b8a342d7 2
45fa3412 3// This file keeps track of upgrades to
b8a342d7 4// the assignment module
5//
6// Sometimes, changes between versions involve
7// alterations to database structures and other
8// major things that may break installations.
9//
10// The upgrade function in this file will attempt
11// to perform all the necessary actions to upgrade
12// your older installtion to the current version.
13//
14// If there's something it cannot do itself, it
15// will tell you what you need to do.
16//
17// The commands in here will all be database-neutral,
b1f93b15 18// using the methods of database_manager class
775f811a 19//
20// Please do not forget to use upgrade_set_timeout()
21// before any action that may take longer time to finish.
b8a342d7 22
775f811a 23function xmldb_assignment_upgrade($oldversion) {
3a003ead 24 global $CFG, $DB, $OUTPUT;
b8a342d7 25
775f811a 26 $dbman = $DB->get_manager();
b8a342d7 27 $result = true;
28
f33e1ed4 29//===== 1.9.0 upgrade line ======//
42f50aec 30
ec29061d 31 if ($result && $oldversion < 2007101511) {
5048575d 32 // change grade typo to text if no grades MDL-13920
33 require_once $CFG->dirroot.'/mod/assignment/lib.php';
775f811a 34 assignment_upgrade_grades();
04264aed 35 upgrade_mod_savepoint($result, 2007101511, 'assignment');
5048575d 36 }
172dd12c 37
6b04ddc5 38 if ($result && $oldversion < 2008081900) {
172dd12c 39
40 /////////////////////////////////////
41 /// new file storage upgrade code ///
42 /////////////////////////////////////
43
44 $fs = get_file_storage();
45
591e205f 46 $sqlfrom = "FROM {assignment_submissions} s
47 JOIN {assignment} a ON a.id = s.assignment
48 JOIN {modules} m ON m.name = 'assignment'
f7f71f83 49 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = a.id)";
172dd12c 50
26a9dc72 51 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
172dd12c 52
f7f71f83 53 if ($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")) {
172dd12c 54
55 $pbar = new progress_bar('migrateassignmentfiles', 500, true);
56
172dd12c 57 $i = 0;
58 foreach ($rs as $submission) {
59 $i++;
775f811a 60 upgrade_set_timeout(180); // set up timeout, may also abort execution
591e205f 61 $pbar->update($i, $count, "Migrating assignment submissions - $i/$count.");
62
172dd12c 63 $basepath = "$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/";
64 if (!file_exists($basepath)) {
65 //no files
66 continue;
67 }
591e205f 68 $context = get_context_instance(CONTEXT_MODULE, $submission->cmid);
172dd12c 69
18e353b0 70 // migrate submitted files first
8cf8f2b7 71 $path = $basepath;
172dd12c 72 $filearea = 'assignment_submission';
8cf8f2b7 73 $items = new DirectoryIterator($path);
172dd12c 74 foreach ($items as $item) {
75 if (!$item->isFile()) {
76 continue;
77 }
78 if (!$item->isReadable()) {
8cf8f2b7 79 echo $OUTPUT->notification(" File not readable, skipping: ".$path.$item->getFilename());
172dd12c 80 continue;
81 }
82 $filename = clean_param($item->getFilename(), PARAM_FILE);
83 if ($filename === '') {
84 continue;
85 }
40047594 86 if (!$fs->file_exists($context->id, $filearea, $submission->id, '/', $filename)) {
d992f53d 87 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
8cf8f2b7
PS
88 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
89 unlink($path.$item->getFilename());
172dd12c 90 }
91 }
92 }
93 unset($items); //release file handles
94
95 // migrate teacher response files
8cf8f2b7
PS
96 $path = $basepath.'responses/';
97 if (file_exists($path)) {
172dd12c 98 $filearea = 'assignment_response';
8cf8f2b7 99 $items = new DirectoryIterator($path);
172dd12c 100 foreach ($items as $item) {
101 if (!$item->isFile()) {
102 continue;
103 }
104 $filename = clean_param($item->getFilename(), PARAM_FILE);
105 if ($filename === '') {
106 continue;
107 }
40047594 108 if (!$fs->file_exists($context->id, $filearea, $submission->id, '/', $filename)) {
d992f53d 109 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename,
172dd12c 110 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
111 if ($submission->teacher) {
112 $file_record['userid'] = $submission->teacher;
113 }
8cf8f2b7
PS
114 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
115 unlink($path.$item->getFilename());
172dd12c 116 }
117 }
118 }
119 unset($items); //release file handles
591e205f 120 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/responses");
172dd12c 121 }
122
930f2962 123 // remove dirs if empty
591e205f 124 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid");
930f2962 125 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment");
126 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment");
172dd12c 127 }
172dd12c 128 $rs->close();
129
172dd12c 130 }
131
6b04ddc5 132 upgrade_mod_savepoint($result, 2008081900, 'assignment');
172dd12c 133 }
134
c119057a 135 if ($result && $oldversion < 2009042000) {
136
137 /// Rename field description on table assignment to intro
138 $table = new xmldb_table('assignment');
2a88f626 139 $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'name');
c119057a 140
141 /// Launch rename field description
142 $dbman->rename_field($table, $field, 'intro');
143
144 /// assignment savepoint reached
145 upgrade_mod_savepoint($result, 2009042000, 'assignment');
146 }
147
148 if ($result && $oldversion < 2009042001) {
149
150 /// Rename field format on table assignment to introformat
151 $table = new xmldb_table('assignment');
2a88f626 152 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
c119057a 153
154 /// Launch rename field format
155 $dbman->rename_field($table, $field, 'introformat');
156
157 /// assignment savepoint reached
158 upgrade_mod_savepoint($result, 2009042001, 'assignment');
159 }
160
b8a342d7 161 return $result;
162}
163
e7521559 164