MDL-14589 new file not found general function
[moodle.git] / mod / assignment / db / upgrade.php
CommitLineData
b8a342d7 1<?php //$Id$
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) {
24 global $CFG, $DB;
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
38 if ($result && $oldversion < 2008073000) {
39
40 /////////////////////////////////////
41 /// new file storage upgrade code ///
42 /////////////////////////////////////
43
44 $fs = get_file_storage();
45
46 $sql = "SELECT s.id, s.userid, s.teacher, s.assignment, a.course
47 FROM {assignment_submissions} s
48 JOIN {assignment} a ON a.id = s.assignment
49 ORDER BY a.course, s.assignment";
50
51 $count = $DB->count_records_sql($sql);
52
53 $lastcourse = 0;
54 $lastassignment = 0;
55
56 if ($rs = $DB->get_recordset_sql($sql)) {
57
58 $pbar = new progress_bar('migrateassignmentfiles', 500, true);
59
60 $olddebug = $DB->get_debug();
775f811a 61 $DB->set_debug(false); // lower debug level, there might be many files
172dd12c 62 $i = 0;
63 foreach ($rs as $submission) {
64 $i++;
775f811a 65 upgrade_set_timeout(180); // set up timeout, may also abort execution
172dd12c 66 $basepath = "$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/";
67 if (!file_exists($basepath)) {
68 //no files
69 continue;
70 }
71 $context = get_context_instance(CONTEXT_MODULE, $submission->assignment);
72
18e353b0 73 // migrate submitted files first
172dd12c 74 $path = $basepath;
75 $filearea = 'assignment_submission';
76 $items = new DirectoryIterator($path);
77 foreach ($items as $item) {
78 if (!$item->isFile()) {
79 continue;
80 }
81 if (!$item->isReadable()) {
82 notify(" File not readable, skipping: ".$path.$item->getFilename());
83 continue;
84 }
85 $filename = clean_param($item->getFilename(), PARAM_FILE);
86 if ($filename === '') {
87 continue;
88 }
b91807e3 89 if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
172dd12c 90 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
91 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
92 unlink($path.$item->getFilename());
93 }
94 }
95 }
96 unset($items); //release file handles
97
98 // migrate teacher response files
99 $path = $basepath.'responses/';
100 if (file_exists($path)) {
101 $filearea = 'assignment_response';
102 $items = new DirectoryIterator($path);
103 foreach ($items as $item) {
104 if (!$item->isFile()) {
105 continue;
106 }
107 $filename = clean_param($item->getFilename(), PARAM_FILE);
108 if ($filename === '') {
109 continue;
110 }
b91807e3 111 if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
172dd12c 112 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename,
113 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
114 if ($submission->teacher) {
115 $file_record['userid'] = $submission->teacher;
116 }
117 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
118 unlink($path.$item->getFilename());
119 }
120 }
121 }
122 unset($items); //release file handles
123 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/responses/");
124 }
125
126 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/");
127
128 if ($lastassignment and $lastassignment != $submission->assignment) {
129 @rmdir("$CFG->dataroot/$lastcourse/$CFG->moddata/assignment/$lastassignment");
130 }
131
132 if ($lastcourse and $lastcourse != $submission->course) {
133 @rmdir("$CFG->dataroot/$lastcourse/$CFG->moddata/assignment");
134 @rmdir("$CFG->dataroot/$lastcourse/$CFG->moddata");
135 @rmdir("$CFG->dataroot/$lastcourse");
136 }
137 $lastsubmission = $submission->assignment;
138 $lastcourse = $submission->course;
139
140 $pbar->update($i, $count, "Migrated assignment submissions - $i/$count.");
141 }
142 $DB->set_debug($olddebug); // reset debug level
143 $rs->close();
144
145 // cleanup after the last submission
146 if ($lastcourse) {
147 @rmdir("$CFG->dataroot/$lastcourse/$CFG->moddata/assignment/$lastassignment");
148 @rmdir("$CFG->dataroot/$lastcourse/$CFG->moddata/assignment");
149 @rmdir("$CFG->dataroot/$lastcourse/$CFG->moddata");
150 @rmdir("$CFG->dataroot/$lastcourse");
151 }
152 }
153
154 upgrade_mod_savepoint($result, 2008073000, 'assignment');
155 }
156
b8a342d7 157 return $result;
158}
159
160?>