Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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   * Based off of a template @ http://docs.moodle.org/dev/Backup_1.9_conversion_for_developers
  21   *
  22   * @package    mod_data
  23   * @copyright  2011 Aparup Banerjee <aparup@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Database conversion handler
  31   */
  32  class moodle1_mod_data_handler extends moodle1_mod_handler {
  33  
  34      /** @var moodle1_file_manager */
  35      protected $fileman = null;
  36  
  37      /** @var int cmid */
  38      protected $moduleid = null;
  39  
  40      /**
  41       * Declare the paths in moodle.xml we are able to convert
  42       *
  43       * The method returns list of {@link convert_path} instances.
  44       * For each path returned, the corresponding conversion method must be
  45       * defined.
  46       *
  47       * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/DATA does not
  48       * actually exist in the file. The last element with the module name was
  49       * appended by the moodle1_converter class.
  50       *
  51       * @return array of {@link convert_path} instances
  52       */
  53      public function get_paths() {
  54          return array(
  55              new convert_path('data', '/MOODLE_BACKUP/COURSE/MODULES/MOD/DATA',
  56                          array(
  57                              'newfields' => array(
  58                                  'introformat' => 0,
  59                                  'assesstimestart' => 0,
  60                                  'assesstimefinish' => 0,
  61                              )
  62                          )
  63                      ),
  64              new convert_path('data_field', '/MOODLE_BACKUP/COURSE/MODULES/MOD/DATA/FIELDS/FIELD')
  65          );
  66      }
  67  
  68      /**
  69       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/DATA
  70       * data available
  71       */
  72      public function process_data($data) {
  73          global $CFG;
  74  
  75          // get the course module id and context id
  76          $instanceid     = $data['id'];
  77          $cminfo         = $this->get_cminfo($instanceid);
  78          $this->moduleid = $cminfo['id'];
  79          $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
  80  
  81          // replay the upgrade step 2007101512
  82          if (!array_key_exists('asearchtemplate', $data)) {
  83              $data['asearchtemplate'] = null;
  84          }
  85  
  86          // replay the upgrade step 2007101513
  87          if (is_null($data['notification'])) {
  88              $data['notification'] = 0;
  89          }
  90  
  91          // conditionally migrate to html format in intro
  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_data');
  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          // @todo: user data - upgrade content to new file storage
 106  
 107          // add 'export' tag to list and single template.
 108          $pattern = '/\#\#delete\#\#(\s+)\#\#approve\#\#/';
 109          $replacement = '##delete##$1##approve##$1##export##';
 110          $data['listtemplate'] = preg_replace($pattern, $replacement, $data['listtemplate']);
 111          $data['singletemplate'] = preg_replace($pattern, $replacement, $data['singletemplate']);
 112  
 113          //@todo: user data - move data comments to comments table
 114          //@todo: user data - move data ratings to ratings table
 115  
 116          // start writing data.xml
 117          $this->open_xml_writer("activities/data_{$this->moduleid}/data.xml");
 118          $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
 119              'modulename' => 'data', 'contextid' => $contextid));
 120          $this->xmlwriter->begin_tag('data', array('id' => $instanceid));
 121  
 122          foreach ($data as $field => $value) {
 123              if ($field <> 'id') {
 124                  $this->xmlwriter->full_tag($field, $value);
 125              }
 126          }
 127  
 128          $this->xmlwriter->begin_tag('fields');
 129  
 130          return $data;
 131      }
 132  
 133      /**
 134       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/DATA/FIELDS/FIELD
 135       * data available
 136       */
 137      public function process_data_field($data) {
 138          // process database fields
 139          $this->write_xml('field', $data, array('/field/id'));
 140      }
 141  
 142      /**
 143       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/DATA/RECORDS/RECORD
 144       * data available
 145       */
 146      public function process_data_record($data) {
 147          //@todo process user data, and define the convert path in get_paths() above.
 148          //$this->write_xml('record', $data, array('/record/id'));
 149      }
 150  
 151      /**
 152       * This is executed when we reach the closing </MOD> tag of our 'data' path
 153       */
 154      public function on_data_end() {
 155          // finish writing data.xml
 156          $this->xmlwriter->end_tag('fields');
 157          $this->xmlwriter->end_tag('data');
 158          $this->xmlwriter->end_tag('activity');
 159          $this->close_xml_writer();
 160  
 161          // write inforef.xml
 162          $this->open_xml_writer("activities/data_{$this->moduleid}/inforef.xml");
 163          $this->xmlwriter->begin_tag('inforef');
 164          $this->xmlwriter->begin_tag('fileref');
 165          foreach ($this->fileman->get_fileids() as $fileid) {
 166              $this->write_xml('file', array('id' => $fileid));
 167          }
 168          $this->xmlwriter->end_tag('fileref');
 169          $this->xmlwriter->end_tag('inforef');
 170          $this->close_xml_writer();
 171      }
 172  }