Updated the HEAD build version to 20080816
[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
b8a342d7 19
20function xmldb_assignment_upgrade($oldversion=0) {
21
f33e1ed4 22 global $CFG, $THEME, $DB;
b8a342d7 23
24 $result = true;
25
f33e1ed4 26//===== 1.9.0 upgrade line ======//
42f50aec 27
ec29061d 28 if ($result && $oldversion < 2007101511) {
63824bbf 29 notify('Processing assignment grades, this may take a while if there are many assignments...', 'notifysuccess');
5048575d 30 // change grade typo to text if no grades MDL-13920
31 require_once $CFG->dirroot.'/mod/assignment/lib.php';
32 // too much debug output
f33e1ed4 33 $DB->set_debug(false);
5048575d 34 assignment_update_grades();
f33e1ed4 35 $DB->set_debug(true);
04264aed 36 upgrade_mod_savepoint($result, 2007101511, 'assignment');
5048575d 37 }
172dd12c 38
39 if ($result && $oldversion < 2008073000) {
40
41 /////////////////////////////////////
42 /// new file storage upgrade code ///
43 /////////////////////////////////////
44
45 $fs = get_file_storage();
46
47 $sql = "SELECT s.id, s.userid, s.teacher, s.assignment, a.course
48 FROM {assignment_submissions} s
49 JOIN {assignment} a ON a.id = s.assignment
50 ORDER BY a.course, s.assignment";
51
52 $count = $DB->count_records_sql($sql);
53
54 $lastcourse = 0;
55 $lastassignment = 0;
56
57 if ($rs = $DB->get_recordset_sql($sql)) {
58
59 $pbar = new progress_bar('migrateassignmentfiles', 500, true);
60
61 $olddebug = $DB->get_debug();
62// $DB->set_debug(false); // lower debug level, there might be many files
63 $i = 0;
64 foreach ($rs as $submission) {
65 $i++;
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
73 // migrate submitted files fisrt
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 }
89 if (!$fs->file_exists($context->id, $filearea, '0', '/', $filename)) {
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 }
111 if (!$fs->file_exists($context->id, $filearea, '0', '/', $filename)) {
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?>