Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Provides support for the conversion of moodle1 backup to the moodle2 format
  19   *
  20   * @package    mod_book
  21   * @copyright  2011 Tõnis Tartes <t6nis20@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Book conversion handler
  29   */
  30  class moodle1_mod_book_handler extends moodle1_mod_handler {
  31  
  32      /** @var moodle1_file_manager */
  33      protected $fileman = null;
  34  
  35      /** @var int cmid */
  36      protected $moduleid = null;
  37  
  38      /**
  39       * Declare the paths in moodle.xml we are able to convert
  40       *
  41       * The method returns list of {@link convert_path} instances. For each path returned,
  42       * at least one of on_xxx_start(), process_xxx() and on_xxx_end() methods must be
  43       * defined. The method process_xxx() is not executed if the associated path element is
  44       * empty (i.e. it contains none elements or sub-paths only).
  45       *
  46       * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK 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('book', '/MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK',
  55                      array(
  56                          'renamefields' => array(
  57                              'summary' => 'intro',
  58                          ),
  59                          'newfields' => array(
  60                              'introformat' => FORMAT_MOODLE,
  61                          ),
  62                          'dropfields' => array(
  63                              'disableprinting'
  64                          ),
  65                      )
  66                  ),
  67              new convert_path('book_chapters', '/MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK/CHAPTERS/CHAPTER',
  68                      array(
  69                          'newfields' => array(
  70                              'contentformat' => FORMAT_HTML,
  71                          ),
  72                      )
  73                  ),
  74          );
  75      }
  76  
  77      /**
  78       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK
  79       * data available
  80       * @param array $data
  81       */
  82      public function process_book($data) {
  83          global $CFG;
  84  
  85          // get the course module id and context id
  86          $instanceid     = $data['id'];
  87          $cminfo         = $this->get_cminfo($instanceid);
  88          $this->moduleid = $cminfo['id'];
  89          $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
  90  
  91          // replay the upgrade step 2009042006
  92          if ($CFG->texteditors !== 'textarea') {
  93              $data['intro']       = text_to_html($data['intro'], false, false, true);
  94              $data['introformat'] = FORMAT_HTML;
  95          }
  96  
  97          // get a fresh new file manager for this instance
  98          $this->fileman = $this->converter->get_file_manager($contextid, 'mod_book');
  99  
 100          // convert course files embedded into the intro
 101          $this->fileman->filearea = 'intro';
 102          $this->fileman->itemid   = 0;
 103          $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
 104  
 105          // start writing book.xml
 106          $this->open_xml_writer("activities/book_{$this->moduleid}/book.xml");
 107          $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
 108              'modulename' => 'book', 'contextid' => $contextid));
 109          $this->xmlwriter->begin_tag('book', array('id' => $instanceid));
 110  
 111          foreach ($data as $field => $value) {
 112              if ($field <> 'id') {
 113                  $this->xmlwriter->full_tag($field, $value);
 114              }
 115          }
 116      }
 117  
 118      /**
 119       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/BOOK/CHAPTERS/CHAPTER
 120       * data available
 121       * @param array $data
 122       */
 123      public function process_book_chapters($data) {
 124          // Convert chapter files.
 125          $this->fileman->filearea = 'chapter';
 126          $this->fileman->itemid   = $data['id'];
 127          $data['content'] = moodle1_converter::migrate_referenced_files($data['content'], $this->fileman);
 128  
 129          $this->write_xml('chapter', $data, array('/chapter/id'));
 130      }
 131  
 132      /**
 133       * This is executed when the parser reaches the <CHAPTERS> opening element
 134       */
 135      public function on_book_chapters_start() {
 136          $this->xmlwriter->begin_tag('chapters');
 137      }
 138  
 139      /**
 140       * This is executed when the parser reaches the closing </CHAPTERS> element
 141       */
 142      public function on_book_chapters_end() {
 143          $this->xmlwriter->end_tag('chapters');
 144      }
 145  
 146      /**
 147       * This is executed when we reach the closing </MOD> tag of our 'book' path
 148       */
 149      public function on_book_end() {
 150          // finalize book.xml
 151          $this->xmlwriter->end_tag('book');
 152          $this->xmlwriter->end_tag('activity');
 153          $this->close_xml_writer();
 154  
 155          // write inforef.xml
 156          $this->open_xml_writer("activities/book_{$this->moduleid}/inforef.xml");
 157          $this->xmlwriter->begin_tag('inforef');
 158          $this->xmlwriter->begin_tag('fileref');
 159          foreach ($this->fileman->get_fileids() as $fileid) {
 160              $this->write_xml('file', array('id' => $fileid));
 161          }
 162          $this->xmlwriter->end_tag('fileref');
 163          $this->xmlwriter->end_tag('inforef');
 164          $this->close_xml_writer();
 165      }
 166  }