From a8662028ab30b75f5ef406344062c5bdc38f1e36 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Wed, 11 Apr 2018 16:39:04 +0800 Subject: [PATCH 1/2] MDL-61932 mod_glossary: Display site-level glossaries on section 1 * Glossary activities created on the front page by importing entries are being added to section 0, but the front page only shows activities on section 1. --- mod/glossary/db/upgrade.php | 58 ++++++++++++++++++++++++++++++++++++- mod/glossary/import.php | 11 +++++-- mod/glossary/version.php | 2 +- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php index 5272f6f1124..4da3823382d 100644 --- a/mod/glossary/db/upgrade.php +++ b/mod/glossary/db/upgrade.php @@ -20,7 +20,7 @@ // before any action that may take longer time to finish. function xmldb_glossary_upgrade($oldversion) { - global $CFG; + global $CFG, $DB; // Moodle v3.1.0 release upgrade line. // Put any upgrade step following this. @@ -34,5 +34,61 @@ function xmldb_glossary_upgrade($oldversion) { // Automatically generated Moodle v3.4.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2017111301) { + + // Fetch the module ID for the glossary module. + $glossarymoduleid = $DB->get_field('modules', 'id', ['name' => 'glossary']); + + // Fetch sections for the frontpage not matching 1. + $sectionselect = 'course = :course AND section <> 1'; + $sitesections = $DB->get_recordset_select('course_sections', $sectionselect, ['course' => SITEID]); + $newsection1cmids = []; + foreach ($sitesections as $section) { + // Fetch the course module IDs of the course modules in the section. + $cmids = explode(',', $section->sequence); + $newcmids = []; + // Update the section in the course_modules table for glossary instances if necessary. + foreach ($cmids as $cmid) { + $params = [ + 'id' => $cmid, + 'module' => $glossarymoduleid + ]; + // Check if the record in the course_modules tables is that of a glossary activity. + if ($DB->record_exists('course_modules', $params)) { + // If so, move it to section 1. + $DB->set_field('course_modules', 'section', 1, $params); + $newsection1cmids[] = $cmid; + } else { + // Otherwise, ignore this course module as we only want to update glossary items. + $newcmids[] = $cmid; + } + } + // Check if we need to update the section record or we can delete it. + if (!empty($newcmids)) { + // Update the section record with a sequence that now excludes the glossary instance(s). + $sequence = implode(',', $newcmids); + $DB->set_field('course_sections', 'section', $sequence, ['id' => $section->id]); + } else { + // This section doesn't contain any items anymore, we can remove this. + $DB->delete_records('course_sections', ['id' => $section->id]); + } + } + $sitesections->close(); + + // Update the sequence field for the site's section 1 if necessary. + if (!empty($newsection1cmids)) { + $section1params = [ + 'course' => 1, + 'section' => 1 + ]; + $section1sequence = $DB->get_field('course_sections', 'sequence', $section1params); + $newsection1sequence = implode(',', array_merge([$section1sequence], $newsection1cmids)); + // Update the sequence field of the first section for the front page. + $DB->set_field('course_sections', 'sequence', $newsection1sequence, $section1params); + } + + upgrade_mod_savepoint(true, 2017111301, 'glossary'); + } + return true; } diff --git a/mod/glossary/import.php b/mod/glossary/import.php index 902455cd245..783864ef6d5 100644 --- a/mod/glossary/import.php +++ b/mod/glossary/import.php @@ -141,8 +141,15 @@ if ($xml = glossary_read_imported_file($result)) { $glossary->assessed = 0; $glossary->availability = null; - // New glossary is to be inserted in section 0, it is always visible. - $glossary->section = 0; + // Check if we're creating the new glossary on the front page or inside a course. + if ($cm->course == SITEID) { + // On the front page, activities are in section 1. + $glossary->section = 1; + } else { + // Inside a course, add to the general section (0). + $glossary->section = 0; + } + // New glossary is always visible. $glossary->visible = 1; $glossary->visibleoncoursepage = 1; diff --git a/mod/glossary/version.php b/mod/glossary/version.php index 9a358279a43..a7eade07e5a 100644 --- a/mod/glossary/version.php +++ b/mod/glossary/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017111300; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2017111301; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2017110800; // Requires this Moodle version $plugin->component = 'mod_glossary'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0; From 1fc9b813b293e33a21394ac8fb97b26e37e9627a Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Tue, 22 May 2018 12:14:41 +0800 Subject: [PATCH 2/2] MDL-61932 mod_glossary: Fetch the correct front page section 1 ID --- mod/glossary/db/upgrade.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/mod/glossary/db/upgrade.php b/mod/glossary/db/upgrade.php index 4da3823382d..b91443c411e 100644 --- a/mod/glossary/db/upgrade.php +++ b/mod/glossary/db/upgrade.php @@ -39,14 +39,22 @@ function xmldb_glossary_upgrade($oldversion) { // Fetch the module ID for the glossary module. $glossarymoduleid = $DB->get_field('modules', 'id', ['name' => 'glossary']); + // Get id of section 1 on the frontpage course. + $fpsection1 = $DB->get_field('course_sections', 'id', ['course' => SITEID, 'section' => 1]); + // Fetch sections for the frontpage not matching 1. $sectionselect = 'course = :course AND section <> 1'; $sitesections = $DB->get_recordset_select('course_sections', $sectionselect, ['course' => SITEID]); $newsection1cmids = []; foreach ($sitesections as $section) { + // Check if we have anything to process for this section. + if (empty($section->sequence)) { + // If there's none, ignore. + continue; + } // Fetch the course module IDs of the course modules in the section. $cmids = explode(',', $section->sequence); - $newcmids = []; + $nonglossarycmids = []; // Update the section in the course_modules table for glossary instances if necessary. foreach ($cmids as $cmid) { $params = [ @@ -55,19 +63,21 @@ function xmldb_glossary_upgrade($oldversion) { ]; // Check if the record in the course_modules tables is that of a glossary activity. if ($DB->record_exists('course_modules', $params)) { - // If so, move it to section 1. - $DB->set_field('course_modules', 'section', 1, $params); + // If so, move it to front page's section 1. + $DB->set_field('course_modules', 'section', $fpsection1, $params); $newsection1cmids[] = $cmid; } else { // Otherwise, ignore this course module as we only want to update glossary items. - $newcmids[] = $cmid; + $nonglossarycmids[] = $cmid; } } // Check if we need to update the section record or we can delete it. - if (!empty($newcmids)) { - // Update the section record with a sequence that now excludes the glossary instance(s). - $sequence = implode(',', $newcmids); - $DB->set_field('course_sections', 'section', $sequence, ['id' => $section->id]); + if (!empty($nonglossarycmids)) { + // Update the section record with a sequence that now excludes the glossary instance(s) (if it changed). + $sequence = implode(',', $nonglossarycmids); + if ($sequence != $section->sequence) { + $DB->set_field('course_sections', 'sequence', $sequence, ['id' => $section->id]); + } } else { // This section doesn't contain any items anymore, we can remove this. $DB->delete_records('course_sections', ['id' => $section->id]); @@ -78,7 +88,7 @@ function xmldb_glossary_upgrade($oldversion) { // Update the sequence field for the site's section 1 if necessary. if (!empty($newsection1cmids)) { $section1params = [ - 'course' => 1, + 'course' => SITEID, 'section' => 1 ]; $section1sequence = $DB->get_field('course_sections', 'sequence', $section1params);