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   * @package    mod_data
  20   * @subpackage backup-moodle2
  21   * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Define all the restore steps that will be used by the restore_data_activity_task
  27   */
  28  
  29  /**
  30   * Structure step to restore one data activity
  31   */
  32  class restore_data_activity_structure_step extends restore_activity_structure_step {
  33  
  34      protected function define_structure() {
  35  
  36          $paths = array();
  37          $userinfo = $this->get_setting_value('userinfo');
  38  
  39          $paths[] = new restore_path_element('data', '/activity/data');
  40          $paths[] = new restore_path_element('data_field', '/activity/data/fields/field');
  41          if ($userinfo) {
  42              $paths[] = new restore_path_element('data_record', '/activity/data/records/record');
  43              $paths[] = new restore_path_element('data_content', '/activity/data/records/record/contents/content');
  44              $paths[] = new restore_path_element('data_rating', '/activity/data/records/record/ratings/rating');
  45              $paths[] = new restore_path_element('data_record_tag', '/activity/data/recordstags/tag');
  46          }
  47  
  48          // Return the paths wrapped into standard activity structure
  49          return $this->prepare_activity_structure($paths);
  50      }
  51  
  52      protected function process_data($data) {
  53          global $DB;
  54  
  55          $data = (object)$data;
  56          $oldid = $data->id;
  57          $data->course = $this->get_courseid();
  58  
  59          // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  60          // See MDL-9367.
  61          $data->timeavailablefrom = $this->apply_date_offset($data->timeavailablefrom);
  62          $data->timeavailableto = $this->apply_date_offset($data->timeavailableto);
  63          $data->timeviewfrom = $this->apply_date_offset($data->timeviewfrom);
  64          $data->timeviewto = $this->apply_date_offset($data->timeviewto);
  65          $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
  66          $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
  67  
  68          if ($data->scale < 0) { // scale found, get mapping
  69              $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
  70          }
  71  
  72          // Some old backups can arrive with data->notification = null (MDL-24470)
  73          // convert them to proper column default (zero)
  74          if (is_null($data->notification)) {
  75              $data->notification = 0;
  76          }
  77  
  78          // insert the data record
  79          $newitemid = $DB->insert_record('data', $data);
  80          $this->apply_activity_instance($newitemid);
  81      }
  82  
  83      protected function process_data_field($data) {
  84          global $DB;
  85  
  86          $data = (object)$data;
  87          $oldid = $data->id;
  88  
  89          $data->dataid = $this->get_new_parentid('data');
  90  
  91          // insert the data_fields record
  92          $newitemid = $DB->insert_record('data_fields', $data);
  93          $this->set_mapping('data_field', $oldid, $newitemid, false); // no files associated
  94      }
  95  
  96      protected function process_data_record($data) {
  97          global $DB;
  98  
  99          $data = (object)$data;
 100          $oldid = $data->id;
 101  
 102          $data->userid = $this->get_mappingid('user', $data->userid);
 103          $data->groupid = $this->get_mappingid('group', $data->groupid);
 104          $data->dataid = $this->get_new_parentid('data');
 105  
 106          // insert the data_records record
 107          $newitemid = $DB->insert_record('data_records', $data);
 108          $this->set_mapping('data_record', $oldid, $newitemid, false); // no files associated
 109      }
 110  
 111      protected function process_data_content($data) {
 112          global $DB;
 113  
 114          $data = (object)$data;
 115          $oldid = $data->id;
 116  
 117          $data->fieldid = $this->get_mappingid('data_field', $data->fieldid);
 118          $data->recordid = $this->get_new_parentid('data_record');
 119  
 120          // insert the data_content record
 121          $newitemid = $DB->insert_record('data_content', $data);
 122          $this->set_mapping('data_content', $oldid, $newitemid, true); // files by this itemname
 123      }
 124  
 125      /**
 126       * Add tags to restored records.
 127       *
 128       * @param stdClass $data Tag
 129       */
 130      protected function process_data_record_tag($data) {
 131          $data = (object)$data;
 132  
 133          if (!core_tag_tag::is_enabled('mod_data', 'data_records')) { // Tags disabled in server, nothing to process.
 134              return;
 135          }
 136  
 137          if (!$itemid = $this->get_mappingid('data_record', $data->itemid)) {
 138              // Some orphaned tag, we could not find the data record for it - ignore.
 139              return;
 140          }
 141  
 142          $tag = $data->rawname;
 143          $context = context_module::instance($this->task->get_moduleid());
 144          core_tag_tag::add_item_tag('mod_data', 'data_records', $itemid, $context, $tag);
 145      }
 146  
 147      protected function process_data_rating($data) {
 148          global $DB;
 149  
 150          $data = (object)$data;
 151  
 152          // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
 153          $data->contextid = $this->task->get_contextid();
 154          $data->itemid    = $this->get_new_parentid('data_record');
 155          if ($data->scaleid < 0) { // scale found, get mapping
 156              $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
 157          }
 158          $data->rating = $data->value;
 159          $data->userid = $this->get_mappingid('user', $data->userid);
 160  
 161          // We need to check that component and ratingarea are both set here.
 162          if (empty($data->component)) {
 163              $data->component = 'mod_data';
 164          }
 165          if (empty($data->ratingarea)) {
 166              $data->ratingarea = 'entry';
 167          }
 168  
 169          $newitemid = $DB->insert_record('rating', $data);
 170      }
 171  
 172      protected function after_execute() {
 173          global $DB;
 174          // Add data related files, no need to match by itemname (just internally handled context)
 175          $this->add_related_files('mod_data', 'intro', null);
 176          // Add content related files, matching by itemname (data_content)
 177          $this->add_related_files('mod_data', 'content', 'data_content');
 178          // Adjust the data->defaultsort field
 179          if ($defaultsort = $DB->get_field('data', 'defaultsort', array('id' => $this->get_new_parentid('data')))) {
 180              if ($defaultsort = $this->get_mappingid('data_field', $defaultsort)) {
 181                  $DB->set_field('data', 'defaultsort', $defaultsort, array('id' => $this->get_new_parentid('data')));
 182              }
 183          }
 184      }
 185  }