MDL-45390 gradebook: Behat tests for grade boundary issues.
[moodle.git] / lib / db / upgradelib.php
CommitLineData
117bd748 1<?php
5b4a78e2
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Upgrade helper functions
19 *
42ff9ce6 20 * This file is used for special upgrade functions - for example groups and gradebook.
56a1a882 21 * These functions must use SQL and database related functions only- no other Moodle API,
42ff9ce6 22 * because it might depend on db structures that are not yet present during upgrade.
23 * (Do not use functions from accesslib.php, grades classes or group functions at all!)
5b4a78e2 24 *
39b90b51
EL
25 * @package core_install
26 * @category upgrade
27 * @copyright 2007 Petr Skoda (http://skodak.org)
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42ff9ce6 29 */
30
5b4a78e2
PS
31defined('MOODLE_INTERNAL') || die();
32
3182d885
PS
33/**
34 * Returns all non-view and non-temp tables with sane names.
35 * Prints list of non-supported tables using $OUTPUT->notification()
36 *
37 * @return array
38 */
39function upgrade_mysql_get_supported_tables() {
40 global $OUTPUT, $DB;
41
42 $tables = array();
43 $patprefix = str_replace('_', '\\_', $DB->get_prefix());
44 $pregprefix = preg_quote($DB->get_prefix(), '/');
45
46 $sql = "SHOW FULL TABLES LIKE '$patprefix%'";
47 $rs = $DB->get_recordset_sql($sql);
48 foreach ($rs as $record) {
49 $record = array_change_key_case((array)$record, CASE_LOWER);
50 $type = $record['table_type'];
51 unset($record['table_type']);
52 $fullname = array_shift($record);
53
54 if ($pregprefix === '') {
55 $name = $fullname;
56 } else {
57 $count = null;
58 $name = preg_replace("/^$pregprefix/", '', $fullname, -1, $count);
59 if ($count !== 1) {
60 continue;
61 }
62 }
63
64 if (!preg_match("/^[a-z][a-z0-9_]*$/", $name)) {
65 echo $OUTPUT->notification("Database table with invalid name '$fullname' detected, skipping.", 'notifyproblem');
66 continue;
67 }
68 if ($type === 'VIEW') {
69 echo $OUTPUT->notification("Unsupported database table view '$fullname' detected, skipping.", 'notifyproblem');
70 continue;
71 }
72 $tables[$name] = $name;
73 }
74 $rs->close();
75
76 return $tables;
77}
7385cc00 78
061e6b28 79/**
80 * Using data for a single course-module that has groupmembersonly enabled,
81 * returns the new availability value that incorporates the correct
82 * groupmembersonly option.
83 *
84 * Included as a function so that it can be shared between upgrade and restore,
85 * and unit-tested.
86 *
87 * @param int $groupingid Grouping id for the course-module (0 if none)
88 * @param string $availability Availability JSON data for the module (null if none)
89 * @return string New value for availability for the module
90 */
91function upgrade_group_members_only($groupingid, $availability) {
92 // Work out the new JSON object representing this option.
93 if ($groupingid) {
94 // Require specific grouping.
95 $condition = (object)array('type' => 'grouping', 'id' => (int)$groupingid);
96 } else {
97 // No grouping specified, so require membership of any group.
98 $condition = (object)array('type' => 'group');
99 }
100
101 if (is_null($availability)) {
102 // If there are no conditions using the new API then just set it.
103 $tree = (object)array('op' => '&', 'c' => array($condition), 'showc' => array(false));
104 } else {
105 // There are existing conditions.
106 $tree = json_decode($availability);
107 switch ($tree->op) {
108 case '&' :
109 // For & conditions we can just add this one.
110 $tree->c[] = $condition;
111 $tree->showc[] = false;
112 break;
113 case '!|' :
114 // For 'not or' conditions we can add this one
115 // but negated.
116 $tree->c[] = (object)array('op' => '!&', 'c' => array($condition));
117 $tree->showc[] = false;
118 break;
119 default:
120 // For the other two (OR and NOT AND) we have to add
121 // an extra level to the tree.
122 $tree = (object)array('op' => '&', 'c' => array($tree, $condition),
123 'showc' => array($tree->show, false));
124 // Inner trees do not have a show option, so remove it.
125 unset($tree->c[0]->show);
126 break;
127 }
128 }
129
130 return json_encode($tree);
131}
132
b5725a97
JF
133/**
134 * Updates the mime-types for files that exist in the database, based on their
135 * file extension.
136 *
137 * @param array $filetypes Array with file extension as the key, and mimetype as the value
138 */
139function upgrade_mimetypes($filetypes) {
140 global $DB;
141 $select = $DB->sql_like('filename', '?', false);
142 foreach ($filetypes as $extension=>$mimetype) {
143 $DB->set_field_select(
144 'files',
145 'mimetype',
146 $mimetype,
147 $select,
148 array($extension)
149 );
150 }
156d0486
MG
151}
152
153/**
154 * Marks all courses with changes in extra credit weight calculation
155 *
156 * Used during upgrade and in course restore process
157 *
158 * This upgrade script is needed because we changed the algorithm for calculating the automatic weights of extra
159 * credit items and want to prevent changes in the existing student grades.
160 *
161 * @param int $onlycourseid
162 */
163function upgrade_extra_credit_weightoverride($onlycourseid = 0) {
164 global $DB;
165
166 // Find all courses that have categories in Natural aggregation method where there is at least one extra credit
167 // item and at least one item with overridden weight.
168 $courses = $DB->get_fieldset_sql(
169 "SELECT DISTINCT gc.courseid
170 FROM {grade_categories} gc
171 INNER JOIN {grade_items} gi ON gc.id = gi.categoryid AND gi.weightoverride = :weightoverriden
172 INNER JOIN {grade_items} gie ON gc.id = gie.categoryid AND gie.aggregationcoef = :extracredit
173 WHERE gc.aggregation = :naturalaggmethod" . ($onlycourseid ? " AND gc.courseid = :onlycourseid" : ''),
174 array('naturalaggmethod' => 13,
175 'weightoverriden' => 1,
176 'extracredit' => 1,
177 'onlycourseid' => $onlycourseid,
178 )
179 );
180 foreach ($courses as $courseid) {
181 $gradebookfreeze = get_config('core', 'gradebook_calculations_freeze_' . $courseid);
182 if (!$gradebookfreeze) {
183 set_config('gradebook_calculations_freeze_' . $courseid, 20150619);
184 }
185 }
4d4dcc27
AG
186}
187
188/**
189 * Marks all courses that require calculated grade items be updated.
190 *
191 * Used during upgrade and in course restore process.
192 *
193 * This upgrade script is needed because the calculated grade items were stuck with a maximum of 100 and could be changed.
194 * This flags the courses that are affected and the grade book is frozen to retain grade integrity.
195 *
196 * @param int $courseid Specify a course ID to run this script on just one course.
197 */
198function upgrade_calculated_grade_items($courseid = null) {
199 global $DB, $CFG;
200
201 $affectedcourses = array();
202 $possiblecourseids = array();
203 $params = array();
204 $singlecoursesql = '';
205 if (isset($courseid)) {
206 $singlecoursesql = "AND ns.id = :courseid";
207 $params['courseid'] = $courseid;
208 }
316c560c
AG
209 $siteminmaxtouse = 1;
210 if (isset($CFG->grade_minmaxtouse)) {
211 $siteminmaxtouse = $CFG->grade_minmaxtouse;
212 }
4d4dcc27
AG
213 $courseidsql = "SELECT ns.id
214 FROM (
3f8a714f 215 SELECT c.id, coalesce(" . $DB->sql_compare_text('gs.value') . ", :siteminmax) AS gradevalue
4d4dcc27
AG
216 FROM {course} c
217 LEFT JOIN {grade_settings} gs
218 ON c.id = gs.courseid
3f8a714f 219 AND ((gs.name = 'minmaxtouse' AND " . $DB->sql_compare_text('gs.value') . " = '2'))
4d4dcc27 220 ) ns
3f8a714f 221 WHERE " . $DB->sql_compare_text('ns.gradevalue') . " = '2' $singlecoursesql";
4d4dcc27
AG
222 $params['siteminmax'] = $siteminmaxtouse;
223 $courses = $DB->get_records_sql($courseidsql, $params);
224 foreach ($courses as $course) {
225 $possiblecourseids[$course->id] = $course->id;
226 }
227
228 if (!empty($possiblecourseids)) {
229 list($sql, $params) = $DB->get_in_or_equal($possiblecourseids);
230 // A calculated grade item grade min != 0 and grade max != 100 and the course setting is set to
231 // "Initial min and max grades".
232 $coursesql = "SELECT DISTINCT courseid
233 FROM {grade_items}
234 WHERE calculation IS NOT NULL
235 AND itemtype = 'manual'
236 AND (grademax <> 100 OR grademin <> 0)
237 AND courseid $sql";
238 $affectedcourses = $DB->get_records_sql($coursesql, $params);
239 }
240
241 // Check for second type of affected courses.
242 // If we already have the courseid parameter set in the affectedcourses then there is no need to run through this section.
243 if (!isset($courseid) || !in_array($courseid, $affectedcourses)) {
244 $singlecoursesql = '';
245 $params = array();
246 if (isset($courseid)) {
247 $singlecoursesql = "AND courseid = :courseid";
248 $params['courseid'] = $courseid;
249 }
250 $nestedsql = "SELECT id
251 FROM {grade_items}
252 WHERE itemtype = 'category'
253 AND calculation IS NOT NULL $singlecoursesql";
254 $calculatedgradecategories = $DB->get_records_sql($nestedsql, $params);
255 $categoryids = array();
256 foreach ($calculatedgradecategories as $key => $gradecategory) {
257 $categoryids[$key] = $gradecategory->id;
258 }
259
260 if (!empty($categoryids)) {
261 list($sql, $params) = $DB->get_in_or_equal($categoryids);
262 // A category with a calculation where the raw grade min and the raw grade max don't match the grade min and grade max
263 // for the category.
264 $coursesql = "SELECT DISTINCT gi.courseid
265 FROM {grade_grades} gg, {grade_items} gi
266 WHERE gi.id = gg.itemid
267 AND (gg.rawgrademax <> gi.grademax OR gg.rawgrademin <> gi.grademin)
268 AND gi.id $sql";
269 $additionalcourses = $DB->get_records_sql($coursesql, $params);
270 foreach ($additionalcourses as $key => $additionalcourse) {
271 if (!array_key_exists($key, $affectedcourses)) {
272 $affectedcourses[$key] = $additionalcourse;
273 }
274 }
275 }
276 }
277
316c560c
AG
278 foreach ($affectedcourses as $affectedcourseid) {
279 if (isset($CFG->upgrade_calculatedgradeitemsonlyregrade) && !($courseid)) {
280 $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $affectedcourseid->courseid));
281 } else {
282 // Check to see if the gradebook freeze is already in affect.
283 $gradebookfreeze = get_config('core', 'gradebook_calculations_freeze_' . $affectedcourseid->courseid);
284 if (!$gradebookfreeze) {
285 set_config('gradebook_calculations_freeze_' . $affectedcourseid->courseid, 20150627);
286 }
4d4dcc27
AG
287 }
288 }
0d1e5456
MG
289}
290
291/**
292 * This upgrade script merges all tag instances pointing to the same course tag
293 *
294 * User id is no longer used for those tag instances
295 */
296function upgrade_course_tags() {
297 global $DB;
298 $sql = "SELECT min(ti.id)
299 FROM {tag_instance} ti
300 LEFT JOIN {tag_instance} tii on tii.itemtype = ? and tii.itemid = ti.itemid and tii.tiuserid = 0 and tii.tagid = ti.tagid
301 where ti.itemtype = ? and ti.tiuserid <> 0 AND tii.id is null
302 group by ti.tagid, ti.itemid";
303 $ids = $DB->get_fieldset_sql($sql, array('course', 'course'));
304 if ($ids) {
305 list($idsql, $idparams) = $DB->get_in_or_equal($ids);
306 $DB->execute('UPDATE {tag_instance} SET tiuserid = 0 WHERE id ' . $idsql, $idparams);
307 }
308 $DB->execute("DELETE FROM {tag_instance} WHERE itemtype = ? AND tiuserid <> 0", array('course'));
309}
8de0b1ab
DW
310
311/**
8de0b1ab
DW
312 * This function creates a default separated/connected scale
313 * so there's something in the database. The locations of
314 * strings and files is a bit odd, but this is because we
315 * need to maintain backward compatibility with many different
316 * existing language translations and older sites.
317 *
318 * @global object
319 * @return void
320 */
321function make_default_scale() {
322 global $DB;
323
324 $defaultscale = new stdClass();
325 $defaultscale->courseid = 0;
326 $defaultscale->userid = 0;
327 $defaultscale->name = get_string('separateandconnected');
328 $defaultscale->description = get_string('separateandconnectedinfo');
329 $defaultscale->scale = get_string('postrating1', 'forum').','.
330 get_string('postrating2', 'forum').','.
331 get_string('postrating3', 'forum');
332 $defaultscale->timemodified = time();
333
334 $defaultscale->id = $DB->insert_record('scale', $defaultscale);
335 return $defaultscale;
336}
337
338
339/**
340 * Create another default scale.
341 *
342 * @param int $oldversion
343 * @return bool always true
344 */
345function make_competence_scale() {
346 global $DB;
347
348 $defaultscale = new stdClass();
349 $defaultscale->courseid = 0;
350 $defaultscale->userid = 0;
351 $defaultscale->name = get_string('defaultcompetencescale');
352 $defaultscale->description = get_string('defaultcompetencescaledesc');
353 $defaultscale->scale = get_string('defaultcompetencescalenotproficient').','.
354 get_string('defaultcompetencescaleproficient');
355 $defaultscale->timemodified = time();
356
357 $defaultscale->id = $DB->insert_record('scale', $defaultscale);
358 return $defaultscale;
359}