MDL-33952: mod_assignment Don't hide the modules on upgrade.
[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
40cb4879 26 require_once(__DIR__.'/upgradelib.php');
b8a342d7 27
40cb4879 28 $dbman = $DB->get_manager();
c5a8abfe 29
46f2a936
AB
30 // Moodle v2.2.0 release upgrade line
31 // Put any upgrade step following this
32
99c3c503
AB
33 // Moodle v2.3.0 release upgrade line
34 // Put any upgrade step following this
35
36
df70d008 37 if ($oldversion < 2012061701) {
b8ba23ca 38 // Fixed/updated numfiles field in assignment_submissions table to count the actual
74fc24a9 39 // number of files has been uploaded when sendformarking is disabled
b8ba23ca
RW
40 upgrade_set_timeout(600); // increase excution time for in large sites
41 $fs = get_file_storage();
42
74fc24a9
DP
43 // Fetch the moduleid for use in the course_modules table
44 $moduleid = $DB->get_field('modules', 'id', array('name' => 'assignment'), MUST_EXIST);
45
46 $selectcount = 'SELECT COUNT(s.id) ';
47 $select = 'SELECT s.id, cm.id AS cmid ';
48 $query = 'FROM {assignment_submissions} s
49 JOIN {assignment} a ON a.id = s.assignment
50 JOIN {course_modules} cm ON a.id = cm.instance AND cm.module = :moduleid
51 WHERE assignmenttype = :assignmenttype';
52
53 $params = array('moduleid' => $moduleid, 'assignmenttype' => 'upload');
54
55 $countsubmissions = $DB->count_records_sql($selectcount.$query, $params);
56 $submissions = $DB->get_recordset_sql($select.$query, $params);
b8ba23ca
RW
57
58 $pbar = new progress_bar('assignmentupgradenumfiles', 500, true);
59 $i = 0;
60 foreach ($submissions as $sub) {
61 $i++;
62 if ($context = context_module::instance($sub->cmid)) {
63 $sub->numfiles = count($fs->get_area_files($context->id, 'mod_assignment', 'submission', $sub->id, 'sortorder', false));
64 $DB->update_record('assignment_submissions', $sub);
65 }
66 $pbar->update($i, $countsubmissions, "Counting files of submissions ($i/$countsubmissions)");
67 }
68 $submissions->close();
69
70 // assignment savepoint reached
df70d008 71 upgrade_mod_savepoint(true, 2012061701, 'assignment');
b8ba23ca
RW
72 }
73
6b9dfe73
EL
74 // Moodle v2.4.0 release upgrade line
75 // Put any upgrade step following this
76
77
c9e54743
EL
78 // Moodle v2.5.0 release upgrade line.
79 // Put any upgrade step following this.
80
81
47578dbf
DW
82 // Moodle v2.6.0 release upgrade line.
83 // Put any upgrade step following this.
84
40cb4879
DW
85 if ($oldversion < 2013121900) {
86 // Define table assignment_upgrade to be created.
87 $table = new xmldb_table('assignment_upgrade');
88
89 // Adding fields to table assignment_upgrade.
90 $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
91 $table->add_field('oldcmid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
92 $table->add_field('oldinstance', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
93 $table->add_field('newcmid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
94 $table->add_field('newinstance', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
95 $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
96
97 // Adding keys to table assignment_upgrade.
98 $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
99
100 // Adding indexes to table assignment_upgrade.
101 $table->add_index('oldcmid', XMLDB_INDEX_NOTUNIQUE, array('oldcmid'));
102 $table->add_index('oldinstance', XMLDB_INDEX_NOTUNIQUE, array('oldinstance'));
103
104 // Conditionally launch create table for assignment_upgrade.
105 if (!$dbman->table_exists($table)) {
106 $dbman->create_table($table);
107 }
108
109 if ($module = $DB->get_record("modules", array("name" => "assignment"))) {
110 $DB->set_field("modules", "visible", "0", array("id" => $module->id)); // Hide module.
40cb4879
DW
111 }
112
113 $count = $DB->count_records('assignment');
114 if ($count) {
115 mod_assignment_pending_upgrades_notification($count);
116 }
117
118 // Assignment savepoint reached.
119 upgrade_mod_savepoint(true, 2013121900, 'assignment');
120 }
121
a4cdd6d2 122 return true;
b8a342d7 123}
124
e7521559 125