MDL-31987 Assignment module: Assignment count submissions correctly.
[moodle.git] / mod / assignment / db / upgrade.php
CommitLineData
e7521559 1<?php
b8a342d7 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
2e0406a5 12// your older installation to the current version.
b8a342d7 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) {
3a003ead 24 global $CFG, $DB, $OUTPUT;
b8a342d7 25
775f811a 26 $dbman = $DB->get_manager();
b8a342d7 27
c5a8abfe 28
46f2a936
AB
29 // Moodle v2.2.0 release upgrade line
30 // Put any upgrade step following this
31
b574c078
AB
32 // Moodle v2.3.0 release upgrade line
33 // Put any upgrade step following this
34
45353da4
RW
35 if ($oldversion < 2012062800) {
36 // Fixed/updated numfiles field in assignment_submissions table to count the actual
37 // number of files has been uploaded.
38 upgrade_set_timeout(600); // increase excution time for in large sites
39 $fs = get_file_storage();
40
41 $selectcount = 'SELECT COUNT(s.id), cm.id AS cmid';
42 $select = 'SELECT s.id, cm.id AS cmid';
43 $query = " FROM {assignment_submissions} s
44 INNER JOIN {course_modules} cm
45 ON s.assignment = cm.instance
46 JOIN {assignment} a
47 ON a.id = s.assignment
48 WHERE a.assignmenttype in ('upload', 'uploadsingle') AND
49 cm.module = (SELECT id
50 FROM {modules}
51 WHERE name = 'assignment')";
52
53 $countsubmissions = $DB->count_records_sql($selectcount. $query);
54 $submissions = $DB->get_recordset_sql($select. $query);
55
56 $pbar = new progress_bar('assignmentupgradenumfiles', 500, true);
57 $i = 0;
58 foreach ($submissions as $sub) {
59 $i++;
60 if ($context = context_module::instance($sub->cmid)) {
61 $sub->numfiles = count($fs->get_area_files($context->id, 'mod_assignment', 'submission', $sub->id, 'sortorder', false));
62 $DB->update_record('assignment_submissions', $sub);
63 }
64 $pbar->update($i, $countsubmissions, "Counting files of submissions ($i/$countsubmissions)");
65 }
66 $submissions->close();
67
68 // assignment savepoint reached
69 upgrade_mod_savepoint(true, 2012062800, 'assignment');
70 }
b574c078 71
a4cdd6d2 72 return true;
b8a342d7 73}
74
e7521559 75