3 // This file keeps track of upgrades to
4 // the assignment module
6 // Sometimes, changes between versions involve
7 // alterations to database structures and other
8 // major things that may break installations.
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.
14 // If there's something it cannot do itself, it
15 // will tell you what you need to do.
17 // The commands in here will all be database-neutral,
18 // using the methods of database_manager class
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();
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');
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);
58 foreach ($rs as $submission) {
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)) {
68 $context = get_context_instance(CONTEXT_MODULE, $submission->cmid);
70 // migrate submitted files first
72 $filearea = 'assignment_submission';
73 $items = new DirectoryIterator($path);
74 foreach ($items as $item) {
75 if (!$item->isFile()) {
78 if (!$item->isReadable()) {
79 echo $OUTPUT->notification(" File not readable, skipping: ".$path.$item->getFilename());
82 $filename = clean_param($item->getFilename(), PARAM_FILE);
83 if ($filename === '') {
86 if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
87 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename, 'userid'=>$submission->userid);
88 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
89 unlink($path.$item->getFilename());
93 unset($items); //release file handles
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()) {
104 $filename = clean_param($item->getFilename(), PARAM_FILE);
105 if ($filename === '') {
108 if (!$fs->file_exists($context->id, $filearea, $submission->userid, '/', $filename)) {
109 $file_record = array('contextid'=>$context->id, 'filearea'=>$filearea, 'itemid'=>$submission->id, 'filepath'=>'/', 'filename'=>$filename,
110 'timecreated'=>$item->getCTime(), 'timemodified'=>$item->getMTime());
111 if ($submission->teacher) {
112 $file_record['userid'] = $submission->teacher;
114 if ($fs->create_file_from_pathname($file_record, $path.$item->getFilename())) {
115 unlink($path.$item->getFilename());
119 unset($items); //release file handles
120 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid/responses");
123 // remove dirs if empty
124 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment/$submission->userid");
125 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment/$submission->assignment");
126 @rmdir("$CFG->dataroot/$submission->course/$CFG->moddata/assignment");
132 upgrade_mod_savepoint($result, 2008081900, 'assignment');
135 if ($result && $oldversion < 2009042000) {
137 /// Rename field description on table assignment to intro
138 $table = new xmldb_table('assignment');
139 $field = new xmldb_field('description', XMLDB_TYPE_TEXT, 'small', null, XMLDB_NOTNULL, null, null, 'name');
141 /// Launch rename field description
142 $dbman->rename_field($table, $field, 'intro');
144 /// assignment savepoint reached
145 upgrade_mod_savepoint($result, 2009042000, 'assignment');
148 if ($result && $oldversion < 2009042001) {
150 /// Rename field format on table assignment to introformat
151 $table = new xmldb_table('assignment');
152 $field = new xmldb_field('format', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'intro');
154 /// Launch rename field format
155 $dbman->rename_field($table, $field, 'introformat');
157 /// assignment savepoint reached
158 upgrade_mod_savepoint($result, 2009042001, 'assignment');