2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Upgrade helper functions
20 * This file is used for special upgrade functions - for example groups and gradebook.
21 * These functions must use SQL and database related functions only- no other Moodle API,
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!)
25 * @package core_install
27 * @copyright 2007 Petr Skoda (http://skodak.org)
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 defined('MOODLE_INTERNAL') || die();
34 * Returns all non-view and non-temp tables with sane names.
35 * Prints list of non-supported tables using $OUTPUT->notification()
39 function upgrade_mysql_get_supported_tables() {
43 $patprefix = str_replace('_', '\\_', $DB->get_prefix());
44 $pregprefix = preg_quote($DB->get_prefix(), '/');
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);
54 if ($pregprefix === '') {
58 $name = preg_replace("/^$pregprefix/", '', $fullname, -1, $count);
64 if (!preg_match("/^[a-z][a-z0-9_]*$/", $name)) {
65 echo $OUTPUT->notification("Database table with invalid name '$fullname' detected, skipping.", 'notifyproblem');
68 if ($type === 'VIEW') {
69 echo $OUTPUT->notification("Unsupported database table view '$fullname' detected, skipping.", 'notifyproblem');
72 $tables[$name] = $name;
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.
84 * Included as a function so that it can be shared between upgrade and restore,
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
91 function upgrade_group_members_only($groupingid, $availability) {
92 // Work out the new JSON object representing this option.
94 // Require specific grouping.
95 $condition = (object)array('type' => 'grouping', 'id' => (int)$groupingid);
97 // No grouping specified, so require membership of any group.
98 $condition = (object)array('type' => 'group');
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));
105 // There are existing conditions.
106 $tree = json_decode($availability);
109 // For & conditions we can just add this one.
110 $tree->c[] = $condition;
111 $tree->showc[] = false;
114 // For 'not or' conditions we can add this one
116 $tree->c[] = (object)array('op' => '!&', 'c' => array($condition));
117 $tree->showc[] = false;
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);
130 return json_encode($tree);
134 * Updates the mime-types for files that exist in the database, based on their
137 * @param array $filetypes Array with file extension as the key, and mimetype as the value
139 function upgrade_mimetypes($filetypes) {
141 $select = $DB->sql_like('filename', '?', false);
142 foreach ($filetypes as $extension=>$mimetype) {
143 $DB->set_field_select(
154 * Marks all courses with changes in extra credit weight calculation
156 * Used during upgrade and in course restore process
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.
161 * @param int $onlycourseid
163 function upgrade_extra_credit_weightoverride($onlycourseid = 0) {
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,
177 'onlycourseid' => $onlycourseid,
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);
189 * Marks all courses that require calculated grade items be updated.
191 * Used during upgrade and in course restore process.
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.
196 * @param int $courseid Specify a course ID to run this script on just one course.
198 function upgrade_calculated_grade_items($courseid = null) {
201 $affectedcourses = array();
202 $possiblecourseids = array();
204 $singlecoursesql = '';
205 if (isset($courseid)) {
206 $singlecoursesql = "AND ns.id = :courseid";
207 $params['courseid'] = $courseid;
209 $siteminmaxtouse = 1;
210 if (isset($CFG->grade_minmaxtouse)) {
211 $siteminmaxtouse = $CFG->grade_minmaxtouse;
213 $courseidsql = "SELECT ns.id
215 SELECT c.id, coalesce(" . $DB->sql_compare_text('gs.value') . ", :siteminmax) AS gradevalue
217 LEFT JOIN {grade_settings} gs
218 ON c.id = gs.courseid
219 AND ((gs.name = 'minmaxtouse' AND " . $DB->sql_compare_text('gs.value') . " = '2'))
221 WHERE " . $DB->sql_compare_text('ns.gradevalue') . " = '2' $singlecoursesql";
222 $params['siteminmax'] = $siteminmaxtouse;
223 $courses = $DB->get_records_sql($courseidsql, $params);
224 foreach ($courses as $course) {
225 $possiblecourseids[$course->id] = $course->id;
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
234 WHERE calculation IS NOT NULL
235 AND itemtype = 'manual'
236 AND (grademax <> 100 OR grademin <> 0)
238 $affectedcourses = $DB->get_records_sql($coursesql, $params);
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 = '';
246 if (isset($courseid)) {
247 $singlecoursesql = "AND courseid = :courseid";
248 $params['courseid'] = $courseid;
250 $nestedsql = "SELECT id
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;
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
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)
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;
278 foreach ($affectedcourses as $affectedcourseid) {
279 if (isset($CFG->upgrade_calculatedgradeitemsonlyregrade) && !($courseid)) {
280 $DB->set_field('grade_items', 'needsupdate', 1, array('courseid' => $affectedcourseid->courseid));
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);
292 * This upgrade script merges all tag instances pointing to the same course tag
294 * User id is no longer used for those tag instances
296 function upgrade_course_tags() {
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'));
305 list($idsql, $idparams) = $DB->get_in_or_equal($ids);
306 $DB->execute('UPDATE {tag_instance} SET tiuserid = 0 WHERE id ' . $idsql, $idparams);
308 $DB->execute("DELETE FROM {tag_instance} WHERE itemtype = ? AND tiuserid <> 0", array('course'));
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.
321 function make_default_scale() {
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();
334 $defaultscale->id = $DB->insert_record('scale', $defaultscale);
335 return $defaultscale;
340 * Create another default scale.
342 * @param int $oldversion
343 * @return bool always true
345 function make_competence_scale() {
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();
357 $defaultscale->id = $DB->insert_record('scale', $defaultscale);
358 return $defaultscale;
362 * Marks all courses that require rounded grade items be updated.
364 * Used during upgrade and in course restore process.
366 * This upgrade script is needed because it has been decided that if a grade is rounded up, and it will changed a letter
367 * grade or satisfy a course completion grade criteria, then it should be set as so, and the letter will be awarded and or
368 * the course completion grade will be awarded.
370 * @param int $courseid Specify a course ID to run this script on just one course.
372 function upgrade_course_letter_boundary($courseid = null) {
376 $params = array('contextlevel' => CONTEXT_COURSE);
377 if (!empty($courseid)) {
378 $coursesql = 'AND c.id = :courseid';
379 $params['courseid'] = $courseid;
382 $contextselect = context_helper::get_preload_record_columns_sql('ctx');
384 // 3, 13, 23, 31, and 32 are the grade display types that incorporate showing letters. See lib/grade/constants/php.
385 if (isset($CFG->grade_displaytype) && in_array($CFG->grade_displaytype, array(3, 13, 23, 31, 32))) {
386 // Check to see if the system letter boundaries are borked.
387 $systemcontext = context_system::instance();
388 if (upgrade_letter_boundary_needs_freeze($systemcontext)) {
389 // Select courses with no grade setting for display and a grade item that is using the default display,
390 // but have not altered the course letter boundary configuration. These courses are definitely affected.
391 $sql = "SELECT DISTINCT c.id AS courseid
392 FROM {grade_items} gi
393 JOIN {course} c ON c.id = gi.courseid
394 LEFT JOIN {grade_settings} gs ON gs.courseid = c.id AND name = 'displaytype'
395 LEFT JOIN (SELECT DISTINCT c.id
396 FROM {grade_letters} gl
397 JOIN {context} ctx ON gl.contextid = ctx.id
398 JOIN {course} c ON ctx.instanceid = c.id
399 WHERE ctx.contextlevel = :contextlevel) gl ON gl.id = c.id
400 WHERE (gi.display = 0 AND (gs.value is NULL))
401 AND gl.id is NULL $coursesql";
402 $affectedcourseids = $DB->get_recordset_sql($sql, $params);
403 foreach ($affectedcourseids as $courseid) {
404 set_config('gradebook_calculations_freeze_' . $courseid->courseid, 20160511);
406 $affectedcourseids->close();
408 // If the system letter boundary is okay proceed to check grade item and course grade display settings.
409 $params['contextlevel2'] = CONTEXT_COURSE;
410 $sql = "SELECT DISTINCT c.id AS courseid, $contextselect
412 JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
413 JOIN {grade_items} gi ON c.id = gi.courseid
414 LEFT JOIN {grade_settings} gs ON c.id = gs.courseid AND name = 'displaytype'
415 LEFT JOIN (SELECT DISTINCT c.id
416 FROM {grade_letters} gl
417 JOIN {context} ctx ON gl.contextid = ctx.id
418 JOIN {course} c ON ctx.instanceid = c.id
419 WHERE ctx.contextlevel = :contextlevel2) gl ON gl.id = c.id
420 WHERE (gi.display IN (3, 13, 23, 31, 32)
421 OR (" . $DB->sql_compare_text('gs.value') . " IN ('3', '13', '23', '31', '32'))
422 OR gl.id is NOT NULL)
425 // There is no site setting for letter grades. Just check the modified letter boundaries.
426 $sql = "SELECT DISTINCT c.id AS courseid, $contextselect
427 FROM {grade_letters} l, {course} c
428 JOIN {context} ctx ON ctx.instanceid = c.id AND ctx.contextlevel = :contextlevel
429 WHERE l.contextid = ctx.id
430 AND ctx.instanceid = c.id
434 $potentialcourses = $DB->get_recordset_sql($sql, $params);
436 foreach ($potentialcourses as $value) {
437 $gradebookfreeze = 'gradebook_calculations_freeze_' . $value->courseid;
439 // Check also if this course id has already been frozen.
440 // If we already have this course ID then move on to the next record.
441 if (!property_exists($CFG, $gradebookfreeze)) {
442 // Check for 57 letter grade issue.
443 context_helper::preload_from_record($value);
444 $coursecontext = context_course::instance($value->courseid);
445 if (upgrade_letter_boundary_needs_freeze($coursecontext)) {
446 // We have a course with a possible score standardisation problem. Flag for freeze.
447 // Flag this course as being frozen.
448 set_config('gradebook_calculations_freeze_' . $value->courseid, 20160511);
452 $potentialcourses->close();
456 * Checks the letter boundary of the provided context to see if it needs freezing.
457 * Each letter boundary is tested to see if receiving that boundary number will
458 * result in achieving the cosponsoring letter.
460 * @param object $context Context object
461 * @return bool if the letter boundary for this context should be frozen.
463 function upgrade_letter_boundary_needs_freeze($context) {
466 $contexts = $context->get_parent_context_ids();
467 array_unshift($contexts, $context->id);
469 foreach ($contexts as $ctxid) {
471 $letters = $DB->get_records_menu('grade_letters', array('contextid' => $ctxid), 'lowerboundary DESC',
472 'lowerboundary, letter');
474 if (!empty($letters)) {
475 foreach ($letters as $boundary => $notused) {
476 $standardisedboundary = upgrade_standardise_score($boundary, 0, 100, 0, 100);
477 if ($boundary != $standardisedboundary) {
481 // We found letters but we have no boundary problem.
489 * Given a float value situated between a source minimum and a source maximum, converts it to the
490 * corresponding value situated between a target minimum and a target maximum. Thanks to Darlene
491 * for the formula :-)
493 * @param float $rawgrade
494 * @param float $sourcemin
495 * @param float $sourcemax
496 * @param float $targetmin
497 * @param float $targetmax
498 * @return float Converted value
500 function upgrade_standardise_score($rawgrade, $sourcemin, $sourcemax, $targetmin, $targetmax) {
501 if (is_null($rawgrade)) {
505 if ($sourcemax == $sourcemin or $targetmin == $targetmax) {
506 // Prevent division by 0.
510 $factor = ($rawgrade - $sourcemin) / ($sourcemax - $sourcemin);
511 $diff = $targetmax - $targetmin;
512 $standardisedvalue = $factor * $diff + $targetmin;
513 return $standardisedvalue;