Merge branch 'w50_MDL-30625_m23_adminorder' of git://github.com/skodak/moodle
[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
2e0406a5 12// your older installation to the current version.
b8a342d7 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
f33e1ed4 28//===== 1.9.0 upgrade line ======//
42f50aec 29
a4cdd6d2 30 if ($oldversion < 2007101511) {
5048575d 31 // change grade typo to text if no grades MDL-13920
32 require_once $CFG->dirroot.'/mod/assignment/lib.php';
775f811a 33 assignment_upgrade_grades();
a4cdd6d2 34 upgrade_mod_savepoint(true, 2007101511, 'assignment');
5048575d 35 }
172dd12c 36
a4cdd6d2 37 if ($oldversion < 2008081900) {
172dd12c 38
39 /////////////////////////////////////
40 /// new file storage upgrade code ///
41 /////////////////////////////////////
42
43 $fs = get_file_storage();
44
591e205f 45 $sqlfrom = "FROM {assignment_submissions} s
46 JOIN {assignment} a ON a.id = s.assignment
47 JOIN {modules} m ON m.name = 'assignment'
f7f71f83 48 JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = a.id)";
172dd12c 49
26a9dc72 50 $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
172dd12c 51
6b1b1d03 52 $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 53
6b1b1d03 54 if ($rs->valid()) {
172dd12c 55 $pbar = new progress_bar('migrateassignmentfiles', 500, true);
172dd12c 56 $i = 0;
57 foreach ($rs as $submission) {
58 $i++;
775f811a 59 upgrade_set_timeout(180); // set up timeout, may also abort execution
591e205f 60 $pbar->update($i, $count, "Migrating assignment submissions - $i/$count.");
61
172dd12c 62 $basepath = "$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/";
63 if (!file_exists($basepath)) {
64 //no files
65 continue;
66 }
591e205f 67 $context = get_context_instance(CONTEXT_MODULE, $submission->cmid);
172dd12c 68
18e353b0 69 // migrate submitted files first
8cf8f2b7 70 $path = $basepath;
8cf8f2b7 71 $items = new DirectoryIterator($path);
172dd12c 72 foreach ($items as $item) {
73 if (!$item->isFile()) {
74 continue;
75 }
76 if (!$item->isReadable()) {
8cf8f2b7 77 echo $OUTPUT->notification(" File not readable, skipping: ".$path.$item->getFilename());
172dd12c 78 continue;
79 }
80 $filename = clean_param($item->getFilename(), PARAM_FILE);
81 if ($filename === '') {
82 continue;
83 }
64f93798
PS
84 if (!$fs->file_exists($context->id, 'mod_assignment', 'submission', $submission->id, '/', $filename)) {
85 $file_record = array('contextid'=>$context->id, 'component'=>'mod_assignment', 'filearea'=>'submission', 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
8cf8f2b7
PS
86 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
87 unlink($path.$item->getFilename());
172dd12c 88 }
89 }
90 }
91 unset($items); //release file handles
92
64f93798 93 // migrate teacher response files for "upload" subtype, unfortunately we do not
8cf8f2b7
PS
94 $path = $basepath.'responses/';
95 if (file_exists($path)) {
8cf8f2b7 96 $items = new DirectoryIterator($path);
172dd12c 97 foreach ($items as $item) {
98 if (!$item->isFile()) {
99 continue;
100 }
101 $filename = clean_param($item->getFilename(), PARAM_FILE);
102 if ($filename === '') {
103 continue;
104 }
64f93798
PS
105 if (!$fs->file_exists($context->id, 'mod_assignment', 'response', $submission->id, '/', $filename)) {
106 $file_record = array('contextid'=>$context->id, 'component'=>'mod_assignment', 'filearea'=>'response', 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename,
172dd12c 107 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
108 if ($submission->teacher) {
109 $file_record['userid'] = $submission->teacher;
110 }
8cf8f2b7
PS
111 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
112 unlink($path.$item->getFilename());
172dd12c 113 }
114 }
115 }
116 unset($items); //release file handles
591e205f 117 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/responses");
172dd12c 118 }
119
930f2962 120 // remove dirs if empty
591e205f 121 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid");
930f2962 122 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment");
123 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment");
172dd12c 124 }
172dd12c 125 }
6b1b1d03 126 $rs->close();
172dd12c 127
a4cdd6d2 128 upgrade_mod_savepoint(true, 2008081900, 'assignment');
172dd12c 129 }
130
a4cdd6d2 131 if ($oldversion < 2009042000) {
c119057a 132
133 /// Rename field description on table assignment to intro
134 $table = new xmldb_table('assignment');
2a88f626 135 $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'name');
c119057a 136
137 /// Launch rename field description
138 $dbman->rename_field($table, $field, 'intro');
139
140 /// assignment savepoint reached
a4cdd6d2 141 upgrade_mod_savepoint(true, 2009042000, 'assignment');
c119057a 142 }
143
a4cdd6d2 144 if ($oldversion < 2009042001) {
c119057a 145
146 /// Rename field format on table assignment to introformat
147 $table = new xmldb_table('assignment');
2a88f626 148 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
c119057a 149
150 /// Launch rename field format
151 $dbman->rename_field($table, $field, 'introformat');
152
153 /// assignment savepoint reached
a4cdd6d2 154 upgrade_mod_savepoint(true, 2009042001, 'assignment');
c119057a 155 }
156
c5a8abfe
AB
157 // Moodle v2.1.0 release upgrade line
158 // Put any upgrade step following this
159
a4cdd6d2 160 return true;
b8a342d7 161}
162
e7521559 163