Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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.
  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,
  17  // using the methods of database_manager class
  18  //
  19  // Please do not forget to use upgrade_set_timeout()
  20  // before any action that may take longer time to finish.
  21  
  22  function xmldb_glossary_upgrade($oldversion) {
  23      global $DB;
  24  
  25      // Automatically generated Moodle v3.5.0 release upgrade line.
  26      // Put any upgrade step following this.
  27  
  28      if ($oldversion < 2018051401) {
  29  
  30          // Fetch the module ID for the glossary module.
  31          $glossarymoduleid = $DB->get_field('modules', 'id', ['name' => 'glossary']);
  32  
  33          // Get id of section 1 on the frontpage course.
  34          $fpsection1 = $DB->get_field('course_sections', 'id', ['course' => SITEID, 'section' => 1]);
  35  
  36          // Fetch sections for the frontpage not matching 1.
  37          $sectionselect = 'course = :course AND section <> 1';
  38          $sitesections = $DB->get_recordset_select('course_sections', $sectionselect, ['course' => SITEID]);
  39          $newsection1cmids = [];
  40          foreach ($sitesections as $section) {
  41              // Check if we have anything to process for this section.
  42              if (empty($section->sequence)) {
  43                  // If there's none, ignore.
  44                  continue;
  45              }
  46              // Fetch the course module IDs of the course modules in the section.
  47              $cmids = explode(',', $section->sequence);
  48              $nonglossarycmids = [];
  49              // Update the section in the course_modules table for glossary instances if necessary.
  50              foreach ($cmids as $cmid) {
  51                  $params = [
  52                      'id' => $cmid,
  53                      'module' => $glossarymoduleid
  54                  ];
  55                  // Check if the record in the course_modules tables is that of a glossary activity.
  56                  if ($DB->record_exists('course_modules', $params)) {
  57                      // If so, move it to front page's section 1.
  58                      $DB->set_field('course_modules', 'section', $fpsection1, $params);
  59                      $newsection1cmids[] = $cmid;
  60                  } else {
  61                      // Otherwise, ignore this course module as we only want to update glossary items.
  62                      $nonglossarycmids[] = $cmid;
  63                  }
  64              }
  65              // Check if we need to update the section record or we can delete it.
  66              if (!empty($nonglossarycmids)) {
  67                  // Update the section record with a sequence that now excludes the glossary instance(s) (if it changed).
  68                  $sequence = implode(',', $nonglossarycmids);
  69                  if ($sequence != $section->sequence) {
  70                      $DB->set_field('course_sections', 'sequence', $sequence, ['id' => $section->id]);
  71                  }
  72              } else {
  73                  // This section doesn't contain any items anymore, we can remove this.
  74                  $DB->delete_records('course_sections', ['id' => $section->id]);
  75              }
  76          }
  77          $sitesections->close();
  78  
  79          // Update the sequence field for the site's section 1 if necessary.
  80          if (!empty($newsection1cmids)) {
  81              $section1params = [
  82                  'course' => SITEID,
  83                  'section' => 1
  84              ];
  85              $section1sequence = $DB->get_field('course_sections', 'sequence', $section1params);
  86              $newsection1sequence = implode(',', array_merge([$section1sequence], $newsection1cmids));
  87              // Update the sequence field of the first section for the front page.
  88              $DB->set_field('course_sections', 'sequence', $newsection1sequence, $section1params);
  89          }
  90  
  91          upgrade_mod_savepoint(true, 2018051401, 'glossary');
  92      }
  93  
  94      // Automatically generated Moodle v3.6.0 release upgrade line.
  95      // Put any upgrade step following this.
  96  
  97      // Automatically generated Moodle v3.7.0 release upgrade line.
  98      // Put any upgrade step following this.
  99  
 100      // Automatically generated Moodle v3.8.0 release upgrade line.
 101      // Put any upgrade step following this.
 102  
 103      // Automatically generated Moodle v3.9.0 release upgrade line.
 104      // Put any upgrade step following this.
 105  
 106      // Automatically generated Moodle v3.10.0 release upgrade line.
 107      // Put any upgrade step following this.
 108  
 109      return true;
 110  }