MDL-62478 upgrade: add 3.5.0 separation line to all upgrade scripts
[moodle.git] / mod / glossary / db / upgrade.php
blobcc92bdae7a496b4055f1754f803b9fb941a892ba
1 <?php
2 // This file keeps track of upgrades to
3 // the glossary module
4 //
5 // Sometimes, changes between versions involve
6 // alterations to database structures and other
7 // major things that may break installations.
8 //
9 // The upgrade function in this file will attempt
10 // to perform all the necessary actions to upgrade
11 // your older installation to the current version.
13 // If there's something it cannot do itself, it
14 // will tell you what you need to do.
16 // The commands in here will all be database-neutral,
17 // using the methods of database_manager class
19 // Please do not forget to use upgrade_set_timeout()
20 // before any action that may take longer time to finish.
22 function xmldb_glossary_upgrade($oldversion) {
23 global $CFG, $DB;
25 // Automatically generated Moodle v3.2.0 release upgrade line.
26 // Put any upgrade step following this.
28 // Automatically generated Moodle v3.3.0 release upgrade line.
29 // Put any upgrade step following this.
31 // Automatically generated Moodle v3.4.0 release upgrade line.
32 // Put any upgrade step following this.
34 // Automatically generated Moodle v3.5.0 release upgrade line.
35 // Put any upgrade step following this.
37 if ($oldversion < 2018051401) {
39 // Fetch the module ID for the glossary module.
40 $glossarymoduleid = $DB->get_field('modules', 'id', ['name' => 'glossary']);
42 // Get id of section 1 on the frontpage course.
43 $fpsection1 = $DB->get_field('course_sections', 'id', ['course' => SITEID, 'section' => 1]);
45 // Fetch sections for the frontpage not matching 1.
46 $sectionselect = 'course = :course AND section <> 1';
47 $sitesections = $DB->get_recordset_select('course_sections', $sectionselect, ['course' => SITEID]);
48 $newsection1cmids = [];
49 foreach ($sitesections as $section) {
50 // Check if we have anything to process for this section.
51 if (empty($section->sequence)) {
52 // If there's none, ignore.
53 continue;
55 // Fetch the course module IDs of the course modules in the section.
56 $cmids = explode(',', $section->sequence);
57 $nonglossarycmids = [];
58 // Update the section in the course_modules table for glossary instances if necessary.
59 foreach ($cmids as $cmid) {
60 $params = [
61 'id' => $cmid,
62 'module' => $glossarymoduleid
64 // Check if the record in the course_modules tables is that of a glossary activity.
65 if ($DB->record_exists('course_modules', $params)) {
66 // If so, move it to front page's section 1.
67 $DB->set_field('course_modules', 'section', $fpsection1, $params);
68 $newsection1cmids[] = $cmid;
69 } else {
70 // Otherwise, ignore this course module as we only want to update glossary items.
71 $nonglossarycmids[] = $cmid;
74 // Check if we need to update the section record or we can delete it.
75 if (!empty($nonglossarycmids)) {
76 // Update the section record with a sequence that now excludes the glossary instance(s) (if it changed).
77 $sequence = implode(',', $nonglossarycmids);
78 if ($sequence != $section->sequence) {
79 $DB->set_field('course_sections', 'sequence', $sequence, ['id' => $section->id]);
81 } else {
82 // This section doesn't contain any items anymore, we can remove this.
83 $DB->delete_records('course_sections', ['id' => $section->id]);
86 $sitesections->close();
88 // Update the sequence field for the site's section 1 if necessary.
89 if (!empty($newsection1cmids)) {
90 $section1params = [
91 'course' => SITEID,
92 'section' => 1
94 $section1sequence = $DB->get_field('course_sections', 'sequence', $section1params);
95 $newsection1sequence = implode(',', array_merge([$section1sequence], $newsection1cmids));
96 // Update the sequence field of the first section for the front page.
97 $DB->set_field('course_sections', 'sequence', $newsection1sequence, $section1params);
100 upgrade_mod_savepoint(true, 2018051401, 'glossary');
103 return true;