Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Provides support for the conversion of moodle1 backup to the moodle2 format
  20   *
  21   * @package mod_glossary
  22   * @copyright  2011 David Mudrak <david@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Glossary conversion handler
  30   */
  31  class moodle1_mod_glossary_handler extends moodle1_mod_handler {
  32  
  33      /** @var moodle1_file_manager */
  34      protected $fileman = null;
  35  
  36      /** @var int cmid */
  37      protected $moduleid = null;
  38  
  39      /**
  40       * Declare the paths in moodle.xml we are able to convert
  41       *
  42       * The method returns list of {@link convert_path} instances.
  43       * For each path returned, the corresponding conversion method must be
  44       * defined.
  45       *
  46       * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/GLOSSARY does not
  47       * actually exist in the file. The last element with the module name was
  48       * appended by the moodle1_converter class.
  49       *
  50       * @return array of {@link convert_path} instances
  51       */
  52      public function get_paths() {
  53          return array(
  54              new convert_path(
  55                  'glossary', '/MOODLE_BACKUP/COURSE/MODULES/MOD/GLOSSARY',
  56                  array(
  57                      'newfields' => array(
  58                          'introformat'       => FORMAT_MOODLE,
  59                          'completionentries' => 0,
  60                      ),
  61                  )
  62              ),
  63              new convert_path('glossary_categories', '/MOODLE_BACKUP/COURSE/MODULES/MOD/GLOSSARY/CATEGORIES'),
  64              new convert_path(
  65                  'glossary_category', '/MOODLE_BACKUP/COURSE/MODULES/MOD/GLOSSARY/CATEGORIES/CATEGORY',
  66                  array(
  67                      'dropfields' => array(
  68                          'glossaryid'
  69                      )
  70                  )
  71              )
  72          );
  73      }
  74  
  75      /**
  76       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/GLOSSARY
  77       * data available
  78       */
  79      public function process_glossary($data) {
  80          global $CFG;
  81  
  82          // get the course module id and context id
  83          $instanceid     = $data['id'];
  84          $cminfo         = $this->get_cminfo($instanceid);
  85          $this->moduleid = $cminfo['id'];
  86          $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
  87  
  88          // replay the upgrade step 2009042006
  89          if ($CFG->texteditors !== 'textarea') {
  90              $data['intro']       = text_to_html($data['intro'], false, false, true);
  91              $data['introformat'] = FORMAT_HTML;
  92          }
  93  
  94          // get a fresh new file manager for this instance
  95          $this->fileman = $this->converter->get_file_manager($contextid, 'mod_glossary');
  96  
  97          // convert course files embedded into the intro
  98          $this->fileman->filearea = 'intro';
  99          $this->fileman->itemid   = 0;
 100          $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
 101  
 102          // start writing glossary.xml
 103          $this->open_xml_writer("activities/glossary_{$this->moduleid}/glossary.xml");
 104          $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
 105              'modulename' => 'glossary', 'contextid' => $contextid));
 106          $this->xmlwriter->begin_tag('glossary', array('id' => $instanceid));
 107  
 108          foreach ($data as $field => $value) {
 109              if ($field <> 'id') {
 110                  $this->xmlwriter->full_tag($field, $value);
 111              }
 112          }
 113  
 114          return $data;
 115      }
 116  
 117      /**
 118       * This is executed when the parser reaches the <CATEGORIES> opening element
 119       */
 120      public function on_glossary_categories_start() {
 121          $this->xmlwriter->begin_tag('categories');
 122      }
 123  
 124      /**
 125       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/GLOSSARY/CATEGORIES/CATEGORY
 126       * data available
 127       */
 128      public function process_glossary_category($data) {
 129          $this->write_xml('category', $data, array('/category/id'));
 130      }
 131  
 132      /**
 133       * This is executed when the parser reaches the closing </CATEGORIES> element
 134       */
 135      public function on_glossary_categories_end() {
 136          $this->xmlwriter->end_tag('categories');
 137      }
 138  
 139      /**
 140       * This is executed when we reach the closing </MOD> tag of our 'glossary' path
 141       */
 142      public function on_glossary_end() {
 143          // finalize glossary.xml
 144          $this->xmlwriter->end_tag('glossary');
 145          $this->xmlwriter->end_tag('activity');
 146          $this->close_xml_writer();
 147  
 148          // write inforef.xml
 149          $this->open_xml_writer("activities/glossary_{$this->moduleid}/inforef.xml");
 150          $this->xmlwriter->begin_tag('inforef');
 151          $this->xmlwriter->begin_tag('fileref');
 152          foreach ($this->fileman->get_fileids() as $fileid) {
 153              $this->write_xml('file', array('id' => $fileid));
 154          }
 155          $this->xmlwriter->end_tag('fileref');
 156          $this->xmlwriter->end_tag('inforef');
 157          $this->close_xml_writer();
 158      }
 159  }