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.

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

   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   * @package    mod_scorm
  19   * @subpackage backup-moodle2
  20   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  /**
  25   * Define all the restore steps that will be used by the restore_scorm_activity_task
  26   */
  27  
  28  /**
  29   * Structure step to restore one scorm activity
  30   */
  31  class restore_scorm_activity_structure_step extends restore_activity_structure_step {
  32  
  33      protected function define_structure() {
  34  
  35          $paths = array();
  36          $userinfo = $this->get_setting_value('userinfo');
  37  
  38          $paths[] = new restore_path_element('scorm', '/activity/scorm');
  39          $paths[] = new restore_path_element('scorm_sco', '/activity/scorm/scoes/sco');
  40          $paths[] = new restore_path_element('scorm_sco_data', '/activity/scorm/scoes/sco/sco_datas/sco_data');
  41          $paths[] = new restore_path_element('scorm_seq_objective', '/activity/scorm/scoes/sco/seq_objectives/seq_objective');
  42          $paths[] = new restore_path_element('scorm_seq_rolluprule', '/activity/scorm/scoes/sco/seq_rolluprules/seq_rolluprule');
  43          $paths[] = new restore_path_element('scorm_seq_rolluprulecond', '/activity/scorm/scoes/sco/seq_rollupruleconds/seq_rolluprulecond');
  44          $paths[] = new restore_path_element('scorm_seq_rulecond', '/activity/scorm/scoes/sco/seq_ruleconds/seq_rulecond');
  45          $paths[] = new restore_path_element('scorm_seq_rulecond_data', '/activity/scorm/scoes/sco/seq_rulecond_datas/seq_rulecond_data');
  46  
  47          $paths[] = new restore_path_element('scorm_seq_mapinfo', '/activity/scorm/scoes/sco/seq_objectives/seq_objective/seq_mapinfos/seq_mapinfo');
  48          if ($userinfo) {
  49              $paths[] = new restore_path_element('scorm_sco_track', '/activity/scorm/scoes/sco/sco_tracks/sco_track');
  50          }
  51  
  52          // Return the paths wrapped into standard activity structure
  53          return $this->prepare_activity_structure($paths);
  54      }
  55  
  56      protected function process_scorm($data) {
  57          global $DB;
  58  
  59          $data = (object)$data;
  60          $oldid = $data->id;
  61  
  62          $data->course = $this->get_courseid();
  63  
  64          // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  65          // See MDL-9367.
  66          $data->timeopen = $this->apply_date_offset($data->timeopen);
  67          $data->timeclose = $this->apply_date_offset($data->timeclose);
  68  
  69          if (!isset($data->completionstatusallscos)) {
  70              $data->completionstatusallscos = false;
  71          }
  72          // insert the scorm record
  73          $newitemid = $DB->insert_record('scorm', $data);
  74          // immediately after inserting "activity" record, call this
  75          $this->apply_activity_instance($newitemid);
  76      }
  77  
  78      protected function process_scorm_sco($data) {
  79          global $DB;
  80  
  81          $data = (object)$data;
  82  
  83          $oldid = $data->id;
  84          $data->scorm = $this->get_new_parentid('scorm');
  85  
  86          $newitemid = $DB->insert_record('scorm_scoes', $data);
  87          $this->set_mapping('scorm_sco', $oldid, $newitemid);
  88      }
  89  
  90      protected function process_scorm_sco_data($data) {
  91          global $DB;
  92  
  93          $data = (object)$data;
  94          $oldid = $data->id;
  95          $data->scoid = $this->get_new_parentid('scorm_sco');
  96  
  97          $newitemid = $DB->insert_record('scorm_scoes_data', $data);
  98          // No need to save this mapping as far as nothing depend on it
  99          // (child paths, file areas nor links decoder)
 100      }
 101  
 102      protected function process_scorm_seq_objective($data) {
 103          global $DB;
 104  
 105          $data = (object)$data;
 106          $oldid = $data->id;
 107          $data->scoid = $this->get_new_parentid('scorm_sco');
 108  
 109          $newitemid = $DB->insert_record('scorm_seq_objective', $data);
 110          $this->set_mapping('scorm_seq_objective', $oldid, $newitemid);
 111      }
 112  
 113      protected function process_scorm_seq_rolluprule($data) {
 114          global $DB;
 115  
 116          $data = (object)$data;
 117          $oldid = $data->id;
 118          $data->scoid = $this->get_new_parentid('scorm_sco');
 119  
 120          $newitemid = $DB->insert_record('scorm_seq_rolluprule', $data);
 121          $this->set_mapping('scorm_seq_rolluprule', $oldid, $newitemid);
 122      }
 123  
 124      protected function process_scorm_seq_rolluprulecond($data) {
 125          global $DB;
 126  
 127          $data = (object)$data;
 128          $oldid = $data->id;
 129          $data->scoid = $this->get_new_parentid('scorm_sco');
 130          $data->ruleconditions = $this->get_new_parentid('scorm_seq_rolluprule');
 131  
 132          $newitemid = $DB->insert_record('scorm_seq_rolluprulecond', $data);
 133          // No need to save this mapping as far as nothing depend on it
 134          // (child paths, file areas nor links decoder)
 135      }
 136  
 137      protected function process_scorm_seq_rulecond($data) {
 138          global $DB;
 139  
 140          $data = (object)$data;
 141          $oldid = $data->id;
 142          $data->scoid = $this->get_new_parentid('scorm_sco');
 143  
 144          $newitemid = $DB->insert_record('scorm_seq_ruleconds', $data);
 145          $this->set_mapping('scorm_seq_ruleconds', $oldid, $newitemid);
 146      }
 147  
 148      protected function process_scorm_seq_rulecond_data($data) {
 149          global $DB;
 150  
 151          $data = (object)$data;
 152          $oldid = $data->id;
 153          $data->scoid = $this->get_new_parentid('scorm_sco');
 154          $data->ruleconditions = $this->get_new_parentid('scorm_seq_ruleconds');
 155  
 156          $newitemid = $DB->insert_record('scorm_seq_rulecond', $data);
 157          // No need to save this mapping as far as nothing depend on it
 158          // (child paths, file areas nor links decoder)
 159      }
 160  
 161  
 162  
 163      protected function process_scorm_seq_mapinfo($data) {
 164          global $DB;
 165  
 166          $data = (object)$data;
 167          $oldid = $data->id;
 168          $data->scoid = $this->get_new_parentid('scorm_sco');
 169          $data->objectiveid = $this->get_new_parentid('scorm_seq_objective');
 170          $newitemid = $DB->insert_record('scorm_scoes_data', $data);
 171          // No need to save this mapping as far as nothing depend on it
 172          // (child paths, file areas nor links decoder)
 173      }
 174  
 175      protected function process_scorm_sco_track($data) {
 176          global $DB;
 177  
 178          $data = (object)$data;
 179          $oldid = $data->id;
 180          $data->scormid = $this->get_new_parentid('scorm');
 181          $data->scoid = $this->get_new_parentid('scorm_sco');
 182          $data->userid = $this->get_mappingid('user', $data->userid);
 183  
 184          $newitemid = $DB->insert_record('scorm_scoes_track', $data);
 185          // No need to save this mapping as far as nothing depend on it
 186          // (child paths, file areas nor links decoder)
 187      }
 188  
 189      protected function after_execute() {
 190          global $DB;
 191  
 192          // Add scorm related files, no need to match by itemname (just internally handled context)
 193          $this->add_related_files('mod_scorm', 'intro', null);
 194          $this->add_related_files('mod_scorm', 'content', null);
 195          $this->add_related_files('mod_scorm', 'package', null);
 196  
 197          // Fix launch param in scorm table to use new sco id.
 198          $scormid = $this->get_new_parentid('scorm');
 199          $scorm = $DB->get_record('scorm', array('id' => $scormid));
 200          $scorm->launch = $this->get_mappingid('scorm_sco', $scorm->launch, '');
 201  
 202          if (!empty($scorm->launch)) {
 203              // Check that this sco has a valid launch value.
 204              $scolaunch = $DB->get_field('scorm_scoes', 'launch', array('id' => $scorm->launch));
 205              if (empty($scolaunch)) {
 206                  // This is not a valid sco - set to empty so we can find a valid launch sco.
 207                  $scorm->launch = '';
 208              }
 209          }
 210  
 211          if (empty($scorm->launch)) {
 212              // This scorm has an invalid launch param - we need to calculate it and get the first launchable sco.
 213              $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
 214              // We use get_records here as we need to pass a limit in the query that works cross db.
 215              $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scormid), 'sortorder', 'id', 0, 1);
 216              if (!empty($scoes)) {
 217                  $sco = reset($scoes); // We only care about the first record - the above query only returns one.
 218                  $scorm->launch = $sco->id;
 219              }
 220          }
 221          if (!empty($scorm->launch)) {
 222              $DB->update_record('scorm', $scorm);
 223          }
 224      }
 225  }