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.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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->displayactivityname)) {
  70              $data->displayactivityname = true;
  71          }
  72          if (!isset($data->completionstatusallscos)) {
  73              $data->completionstatusallscos = false;
  74          }
  75          // insert the scorm record
  76          $newitemid = $DB->insert_record('scorm', $data);
  77          // immediately after inserting "activity" record, call this
  78          $this->apply_activity_instance($newitemid);
  79      }
  80  
  81      protected function process_scorm_sco($data) {
  82          global $DB;
  83  
  84          $data = (object)$data;
  85  
  86          $oldid = $data->id;
  87          $data->scorm = $this->get_new_parentid('scorm');
  88  
  89          $newitemid = $DB->insert_record('scorm_scoes', $data);
  90          $this->set_mapping('scorm_sco', $oldid, $newitemid);
  91      }
  92  
  93      protected function process_scorm_sco_data($data) {
  94          global $DB;
  95  
  96          $data = (object)$data;
  97          $oldid = $data->id;
  98          $data->scoid = $this->get_new_parentid('scorm_sco');
  99  
 100          $newitemid = $DB->insert_record('scorm_scoes_data', $data);
 101          // No need to save this mapping as far as nothing depend on it
 102          // (child paths, file areas nor links decoder)
 103      }
 104  
 105      protected function process_scorm_seq_objective($data) {
 106          global $DB;
 107  
 108          $data = (object)$data;
 109          $oldid = $data->id;
 110          $data->scoid = $this->get_new_parentid('scorm_sco');
 111  
 112          $newitemid = $DB->insert_record('scorm_seq_objective', $data);
 113          $this->set_mapping('scorm_seq_objective', $oldid, $newitemid);
 114      }
 115  
 116      protected function process_scorm_seq_rolluprule($data) {
 117          global $DB;
 118  
 119          $data = (object)$data;
 120          $oldid = $data->id;
 121          $data->scoid = $this->get_new_parentid('scorm_sco');
 122  
 123          $newitemid = $DB->insert_record('scorm_seq_rolluprule', $data);
 124          $this->set_mapping('scorm_seq_rolluprule', $oldid, $newitemid);
 125      }
 126  
 127      protected function process_scorm_seq_rolluprulecond($data) {
 128          global $DB;
 129  
 130          $data = (object)$data;
 131          $oldid = $data->id;
 132          $data->scoid = $this->get_new_parentid('scorm_sco');
 133          $data->ruleconditions = $this->get_new_parentid('scorm_seq_rolluprule');
 134  
 135          $newitemid = $DB->insert_record('scorm_seq_rolluprulecond', $data);
 136          // No need to save this mapping as far as nothing depend on it
 137          // (child paths, file areas nor links decoder)
 138      }
 139  
 140      protected function process_scorm_seq_rulecond($data) {
 141          global $DB;
 142  
 143          $data = (object)$data;
 144          $oldid = $data->id;
 145          $data->scoid = $this->get_new_parentid('scorm_sco');
 146  
 147          $newitemid = $DB->insert_record('scorm_seq_ruleconds', $data);
 148          $this->set_mapping('scorm_seq_ruleconds', $oldid, $newitemid);
 149      }
 150  
 151      protected function process_scorm_seq_rulecond_data($data) {
 152          global $DB;
 153  
 154          $data = (object)$data;
 155          $oldid = $data->id;
 156          $data->scoid = $this->get_new_parentid('scorm_sco');
 157          $data->ruleconditions = $this->get_new_parentid('scorm_seq_ruleconds');
 158  
 159          $newitemid = $DB->insert_record('scorm_seq_rulecond', $data);
 160          // No need to save this mapping as far as nothing depend on it
 161          // (child paths, file areas nor links decoder)
 162      }
 163  
 164  
 165  
 166      protected function process_scorm_seq_mapinfo($data) {
 167          global $DB;
 168  
 169          $data = (object)$data;
 170          $oldid = $data->id;
 171          $data->scoid = $this->get_new_parentid('scorm_sco');
 172          $data->objectiveid = $this->get_new_parentid('scorm_seq_objective');
 173          $newitemid = $DB->insert_record('scorm_scoes_data', $data);
 174          // No need to save this mapping as far as nothing depend on it
 175          // (child paths, file areas nor links decoder)
 176      }
 177  
 178      protected function process_scorm_sco_track($data) {
 179          global $DB;
 180  
 181          $data = (object)$data;
 182          $oldid = $data->id;
 183          $data->scormid = $this->get_new_parentid('scorm');
 184          $data->scoid = $this->get_new_parentid('scorm_sco');
 185          $data->userid = $this->get_mappingid('user', $data->userid);
 186  
 187          $newitemid = $DB->insert_record('scorm_scoes_track', $data);
 188          // No need to save this mapping as far as nothing depend on it
 189          // (child paths, file areas nor links decoder)
 190      }
 191  
 192      protected function after_execute() {
 193          global $DB;
 194  
 195          // Add scorm related files, no need to match by itemname (just internally handled context)
 196          $this->add_related_files('mod_scorm', 'intro', null);
 197          $this->add_related_files('mod_scorm', 'content', null);
 198          $this->add_related_files('mod_scorm', 'package', null);
 199  
 200          // Fix launch param in scorm table to use new sco id.
 201          $scormid = $this->get_new_parentid('scorm');
 202          $scorm = $DB->get_record('scorm', array('id' => $scormid));
 203          $scorm->launch = $this->get_mappingid('scorm_sco', $scorm->launch, '');
 204  
 205          if (!empty($scorm->launch)) {
 206              // Check that this sco has a valid launch value.
 207              $scolaunch = $DB->get_field('scorm_scoes', 'launch', array('id' => $scorm->launch));
 208              if (empty($scolaunch)) {
 209                  // This is not a valid sco - set to empty so we can find a valid launch sco.
 210                  $scorm->launch = '';
 211              }
 212          }
 213  
 214          if (empty($scorm->launch)) {
 215              // This scorm has an invalid launch param - we need to calculate it and get the first launchable sco.
 216              $sqlselect = 'scorm = ? AND '.$DB->sql_isnotempty('scorm_scoes', 'launch', false, true);
 217              // We use get_records here as we need to pass a limit in the query that works cross db.
 218              $scoes = $DB->get_records_select('scorm_scoes', $sqlselect, array($scormid), 'sortorder', 'id', 0, 1);
 219              if (!empty($scoes)) {
 220                  $sco = reset($scoes); // We only care about the first record - the above query only returns one.
 221                  $scorm->launch = $sco->id;
 222              }
 223          }
 224          if (!empty($scorm->launch)) {
 225              $DB->update_record('scorm', $scorm);
 226          }
 227      }
 228  }