NOBUG course is not a required param (we assume site level if none specified)
[moodle.git] / mod / assignment / db / upgrade.php
1 <?php
3 // This file keeps track of upgrades to
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,
18 // using the methods of database_manager class
19 //
20 // Please do not forget to use upgrade_set_timeout()
21 // before any action that may take longer time to finish.
23 function xmldb_assignment_upgrade($oldversion) {
24     global $CFG, $DB, $OUTPUT;
26     $dbman = $DB->get_manager();
27     $result = true;
29 //===== 1.9.0 upgrade line ======//
31     if ($result && $oldversion < 2007101511) {
32         // change grade typo to text if no grades MDL-13920
33         require_once $CFG->dirroot.'/mod/assignment/lib.php';
34         assignment_upgrade_grades();
35         upgrade_mod_savepoint($result, 2007101511, 'assignment');
36     }
38     if ($result && $oldversion < 2008081900) {
40         /////////////////////////////////////
41         /// new file storage upgrade code ///
42         /////////////////////////////////////
44         $fs = get_file_storage();
46         $sqlfrom = "FROM {assignment_submissions} s
47                     JOIN {assignment} a ON a.id = s.assignment
48                     JOIN {modules} m ON m.name = 'assignment'
49                     JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = a.id)";
51         $count = $DB->count_records_sql("SELECT COUNT('x') $sqlfrom");
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")) {
55             $pbar = new progress_bar('migrateassignmentfiles', 500, true);
57             $i = 0;
58             foreach ($rs as $submission) {
59                 $i++;
60                 upgrade_set_timeout(180); // set up timeout, may also abort execution
61                 $pbar->update($i, $count, "Migrating assignment submissions - $i/$count.");
63                 $basepath = "$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/";
64                 if (!file_exists($basepath)) {
65                     //no files
66                     continue;
67                 }
68                 $context = get_context_instance(CONTEXT_MODULE, $submission->cmid);
70                 // migrate submitted files first
71                 $newpath = "$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->id/";
72                 if (!file_exists($newpath)) {
73                     mkdir($newpath);
74                 }
75                 $filearea = 'assignment_submission';
76                 $items = new DirectoryIterator($basepath);
77                 foreach ($items as $item) {
78                     if (!$item->isFile()) {
79                         continue;
80                     }
81                     if (!$item->isReadable()) {
82                         echo $OUTPUT->notification(" File not readable, skipping: ".$basepath.$item->getFilename());
83                         continue;
84                     }
85                     $filename = clean_param($item->getFilename(), PARAM_FILE);
86                     if ($filename === '') {
87                         continue;
88                     }
89                     if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
90                         $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
91                         if ($fs->create_file_from_pathname($file_record, $basepath.$item->getFilename())) {
92                             unlink($basepath.$item->getFilename());
93                         }
94                     }
95                 }
96                 unset($items); //release file handles
98                 // migrate teacher response files
99                 $basepath = $basepath.'responses/';
100                 $newpath = $newpath.'responses/';
101                 if (file_exists($basepath)) {
102                     $filearea = 'assignment_response';
103                     $items = new DirectoryIterator($basepath);
104                     foreach ($items as $item) {
105                         if (!$item->isFile()) {
106                             continue;
107                         }
108                         $filename = clean_param($item->getFilename(), PARAM_FILE);
109                         if ($filename === '') {
110                             continue;
111                         }
112                         if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
113                             $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename,
114                                                  'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
115                             if ($submission->teacher) {
116                                 $file_record['userid'] = $submission->teacher;
117                             }
118                             if ($fs->create_file_from_pathname($file_record, $basepath.$item->getFilename())) {
119                                 unlink($basepath.$item->getFilename());
120                             }
121                         }
122                     }
123                     unset($items); //release file handles
124                     @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/responses");
125                 }
127                 // remove dirs if empty
128                 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid");
129                 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment");
130                 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment");
131             }
132             $rs->close();
134         }
136         upgrade_mod_savepoint($result, 2008081900, 'assignment');
137     }
139     if ($result && $oldversion < 2009042000) {
141     /// Rename field description on table assignment to intro
142         $table = new xmldb_table('assignment');
143         $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'name');
145     /// Launch rename field description
146         $dbman->rename_field($table, $field, 'intro');
148     /// assignment savepoint reached
149         upgrade_mod_savepoint($result, 2009042000, 'assignment');
150     }
152     if ($result && $oldversion < 2009042001) {
154     /// Rename field format on table assignment to introformat
155         $table = new xmldb_table('assignment');
156         $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
158     /// Launch rename field format
159         $dbman->rename_field($table, $field, 'introformat');
161     /// assignment savepoint reached
162         upgrade_mod_savepoint($result, 2009042001, 'assignment');
163     }
165     return $result;