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.
   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_scorm
  21   * @copyright  2011 Aparup Banerjee <nebgor@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   * Scorm conversion handler
  29   */
  30  class moodle1_mod_scorm_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.
  42       * For each path returned, the corresponding conversion method must be
  43       * defined.
  44       *
  45       * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM does not
  46       * actually exist in the file. The last element with the module name was
  47       * appended by the moodle1_converter class.
  48       *
  49       * @return array of {@link convert_path} instances
  50       */
  51      public function get_paths() {
  52          return array(
  53              new convert_path('scorm', '/MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM',
  54                  array(
  55                      'newfields' => array(
  56                          'whatgrade' => 0,
  57                          'scormtype' => 'local',
  58                          'sha1hash' => null,
  59                          'revision' => '0',
  60                          'forcecompleted' => 0,
  61                          'forcenewattempt' => 0,
  62                          'lastattemptlock' => 0,
  63                          'masteryoverride' => 1,
  64                          'displayattemptstatus' => 1,
  65                          'displaycoursestructure' => 0,
  66                          'timeopen' => '0',
  67                          'timeclose' => '0',
  68                          'introformat' => '0',
  69                          'completionstatusallscos' => 0,
  70                      ),
  71                      'renamefields' => array(
  72                          'summary' => 'intro'
  73                      )
  74                  )
  75              ),
  76              new convert_path('scorm_sco', '/MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM/SCOES/SCO')
  77          );
  78      }
  79  
  80      /**
  81       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM
  82       * data available
  83       */
  84      public function process_scorm($data) {
  85          global $CFG;
  86  
  87          // get the course module id and context id
  88          $instanceid     = $data['id'];
  89          $currentcminfo  = $this->get_cminfo($instanceid);
  90          $this->moduleid = $currentcminfo['id'];
  91          $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
  92  
  93          // conditionally migrate to html format in intro
  94          if ($CFG->texteditors !== 'textarea') {
  95              $data['intro']       = text_to_html($data['intro'], false, false, true);
  96              $data['introformat'] = FORMAT_HTML;
  97          }
  98  
  99          // get a fresh new file manager for this instance
 100          $this->fileman = $this->converter->get_file_manager($contextid, 'mod_scorm');
 101  
 102          // convert course files embedded into the intro
 103          $this->fileman->filearea = 'intro';
 104          $this->fileman->itemid   = 0;
 105          $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman);
 106  
 107          // check 1.9 version where backup was created
 108          $backupinfo = $this->converter->get_stash('backup_info');
 109          if ($backupinfo['moodle_version'] < 2007110503) {
 110              // as we have no module version data, assume $currmodule->version <= $module->version
 111              // - fix data as the source 1.9 build hadn't yet at time of backing up.
 112              $data['grademethod'] = $data['grademethod']%10;
 113          }
 114  
 115          // update scormtype (logic is consistent as done in scorm/db/upgrade.php)
 116          $ismanifest = preg_match('/imsmanifest\.xml$/', $data['reference']);
 117          $iszippif = preg_match('/.(zip|pif)$/', $data['reference']);
 118          $isurl = preg_match('/^((http|https):\/\/|www\.)/', $data['reference']);
 119          if ($isurl) {
 120              if ($ismanifest) {
 121                  $data['scormtype'] = 'external';
 122              } else if ($iszippif) {
 123                  $data['scormtype'] = 'localtype';
 124              }
 125          }
 126  
 127          // migrate scorm package file
 128          $this->fileman->filearea = 'package';
 129          $this->fileman->itemid   = 0;
 130          $this->fileman->migrate_file('course_files/'.$data['reference']);
 131  
 132          // start writing scorm.xml
 133          $this->open_xml_writer("activities/scorm_{$this->moduleid}/scorm.xml");
 134          $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
 135              'modulename' => 'scorm', 'contextid' => $contextid));
 136          $this->xmlwriter->begin_tag('scorm', array('id' => $instanceid));
 137  
 138          foreach ($data as $field => $value) {
 139              if ($field <> 'id') {
 140                  $this->xmlwriter->full_tag($field, $value);
 141              }
 142          }
 143  
 144          $this->xmlwriter->begin_tag('scoes');
 145  
 146          return $data;
 147      }
 148  
 149      /**
 150       * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/SCORM/SCOES/SCO
 151       * data available
 152       */
 153      public function process_scorm_sco($data) {
 154          $this->write_xml('sco', $data, array('/sco/id'));
 155      }
 156  
 157      /**
 158       * This is executed when we reach the closing </MOD> tag of our 'scorm' path
 159       */
 160      public function on_scorm_end() {
 161          // close scorm.xml
 162          $this->xmlwriter->end_tag('scoes');
 163          $this->xmlwriter->end_tag('scorm');
 164          $this->xmlwriter->end_tag('activity');
 165          $this->close_xml_writer();
 166  
 167          // write inforef.xml
 168          $this->open_xml_writer("activities/scorm_{$this->moduleid}/inforef.xml");
 169          $this->xmlwriter->begin_tag('inforef');
 170          $this->xmlwriter->begin_tag('fileref');
 171          foreach ($this->fileman->get_fileids() as $fileid) {
 172              $this->write_xml('file', array('id' => $fileid));
 173          }
 174          $this->xmlwriter->end_tag('fileref');
 175          $this->xmlwriter->end_tag('inforef');
 176          $this->close_xml_writer();
 177      }
 178  }