MDL-18909 Fixed a typo.
[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
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
172dd12c 71 $path = $basepath;
72 $filearea = 'assignment_submission';
73 $items = new DirectoryIterator($path);
74 foreach ($items as $item) {
75 if (!$item->isFile()) {
76 continue;
77 }
78 if (!$item->isReadable()) {
79 notify(" File not readable, skipping: ".$path.$item->getFilename());
80 continue;
81 }
82 $filename = clean_param($item->getFilename(), PARAM_FILE);
83 if ($filename === '') {
84 continue;
85 }
b91807e3 86 if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
172dd12c 87 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
88 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
89 unlink($path.$item->getFilename());
90 }
91 }
92 }
93 unset($items); //release file handles
94
95 // migrate teacher response files
96 $path = $basepath.'responses/';
97 if (file_exists($path)) {
98 $filearea = 'assignment_response';
99 $items = new DirectoryIterator($path);
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 }
b91807e3 108 if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
172dd12c 109 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->userid, 'filepath'=>'/', 'filename'=>$filename,
110 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
111 if ($submission->teacher) {
112 $file_record['userid'] = $submission->teacher;
113 }
114 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
115 unlink($path.$item->getFilename());
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
b8a342d7 135 return $result;
136}
137
138?>