MDL-61932 mod_glossary: Fetch the correct front page section 1 ID
[moodle.git] / mod / glossary / db / upgrade.php
CommitLineData
86342d63 1<?php
cbc2b5df 2// This file keeps track of upgrades to
ecfeb03a 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
2e0406a5 11// your older installation to the current version.
ecfeb03a 12//
13// If there's something it cannot do itself, it
14// will tell you what you need to do.
15//
16// The commands in here will all be database-neutral,
b1f93b15 17// using the methods of database_manager class
775f811a 18//
19// Please do not forget to use upgrade_set_timeout()
20// before any action that may take longer time to finish.
ecfeb03a 21
775f811a 22function xmldb_glossary_upgrade($oldversion) {
266eccfd 23 global $CFG, $DB;
6d29c4ac 24
75c57a08
EL
25 // Automatically generated Moodle v3.2.0 release upgrade line.
26 // Put any upgrade step following this.
27
5e272283
EL
28 // Automatically generated Moodle v3.3.0 release upgrade line.
29 // Put any upgrade step following this.
30
6499085f
EL
31 // Automatically generated Moodle v3.4.0 release upgrade line.
32 // Put any upgrade step following this.
33
266eccfd
JP
34 if ($oldversion < 2018051401) {
35
36 // Fetch the module ID for the glossary module.
37 $glossarymoduleid = $DB->get_field('modules', 'id', ['name' => 'glossary']);
38
d9805288
JP
39 // Get id of section 1 on the frontpage course.
40 $fpsection1 = $DB->get_field('course_sections', 'id', ['course' => SITEID, 'section' => 1]);
41
266eccfd
JP
42 // Fetch sections for the frontpage not matching 1.
43 $sectionselect = 'course = :course AND section <> 1';
44 $sitesections = $DB->get_recordset_select('course_sections', $sectionselect, ['course' => SITEID]);
45 $newsection1cmids = [];
46 foreach ($sitesections as $section) {
d9805288
JP
47 // Check if we have anything to process for this section.
48 if (empty($section->sequence)) {
49 // If there's none, ignore.
50 continue;
51 }
266eccfd
JP
52 // Fetch the course module IDs of the course modules in the section.
53 $cmids = explode(',', $section->sequence);
d9805288 54 $nonglossarycmids = [];
266eccfd
JP
55 // Update the section in the course_modules table for glossary instances if necessary.
56 foreach ($cmids as $cmid) {
57 $params = [
58 'id' => $cmid,
59 'module' => $glossarymoduleid
60 ];
61 // Check if the record in the course_modules tables is that of a glossary activity.
62 if ($DB->record_exists('course_modules', $params)) {
d9805288
JP
63 // If so, move it to front page's section 1.
64 $DB->set_field('course_modules', 'section', $fpsection1, $params);
266eccfd
JP
65 $newsection1cmids[] = $cmid;
66 } else {
67 // Otherwise, ignore this course module as we only want to update glossary items.
d9805288 68 $nonglossarycmids[] = $cmid;
266eccfd
JP
69 }
70 }
71 // Check if we need to update the section record or we can delete it.
d9805288
JP
72 if (!empty($nonglossarycmids)) {
73 // Update the section record with a sequence that now excludes the glossary instance(s) (if it changed).
74 $sequence = implode(',', $nonglossarycmids);
75 if ($sequence != $section->sequence) {
76 $DB->set_field('course_sections', 'sequence', $sequence, ['id' => $section->id]);
77 }
266eccfd
JP
78 } else {
79 // This section doesn't contain any items anymore, we can remove this.
80 $DB->delete_records('course_sections', ['id' => $section->id]);
81 }
82 }
83 $sitesections->close();
84
85 // Update the sequence field for the site's section 1 if necessary.
86 if (!empty($newsection1cmids)) {
87 $section1params = [
d9805288 88 'course' => SITEID,
266eccfd
JP
89 'section' => 1
90 ];
91 $section1sequence = $DB->get_field('course_sections', 'sequence', $section1params);
92 $newsection1sequence = implode(',', array_merge([$section1sequence], $newsection1cmids));
93 // Update the sequence field of the first section for the front page.
94 $DB->set_field('course_sections', 'sequence', $newsection1sequence, $section1params);
95 }
96
97 upgrade_mod_savepoint(true, 2018051401, 'glossary');
98 }
99
a4cdd6d2 100 return true;
ecfeb03a 101}