[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/data/backup/moodle2/ -> restore_data_stepslib.php (source)

   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          }
  46  
  47          // Return the paths wrapped into standard activity structure
  48          return $this->prepare_activity_structure($paths);
  49      }
  50  
  51      protected function process_data($data) {
  52          global $DB;
  53  
  54          $data = (object)$data;
  55          $oldid = $data->id;
  56          $data->course = $this->get_courseid();
  57  
  58          $data->timeavailablefrom = $this->apply_date_offset($data->timeavailablefrom);
  59          $data->timeavailableto = $this->apply_date_offset($data->timeavailableto);
  60          $data->timeviewfrom = $this->apply_date_offset($data->timeviewfrom);
  61          $data->timeviewto = $this->apply_date_offset($data->timeviewto);
  62          $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
  63          $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
  64          // Added in 3.1, hence conditional.
  65          $data->timemodified = isset($data->timemodified) ? $this->apply_date_offset($data->timemodified) : time();
  66  
  67          if ($data->scale < 0) { // scale found, get mapping
  68              $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
  69          }
  70  
  71          // Some old backups can arrive with data->notification = null (MDL-24470)
  72          // convert them to proper column default (zero)
  73          if (is_null($data->notification)) {
  74              $data->notification = 0;
  75          }
  76  
  77          // insert the data record
  78          $newitemid = $DB->insert_record('data', $data);
  79          $this->apply_activity_instance($newitemid);
  80      }
  81  
  82      protected function process_data_field($data) {
  83          global $DB;
  84  
  85          $data = (object)$data;
  86          $oldid = $data->id;
  87  
  88          $data->dataid = $this->get_new_parentid('data');
  89  
  90          // insert the data_fields record
  91          $newitemid = $DB->insert_record('data_fields', $data);
  92          $this->set_mapping('data_field', $oldid, $newitemid, false); // no files associated
  93      }
  94  
  95      protected function process_data_record($data) {
  96          global $DB;
  97  
  98          $data = (object)$data;
  99          $oldid = $data->id;
 100  
 101          $data->timecreated = $this->apply_date_offset($data->timecreated);
 102          $data->timemodified = $this->apply_date_offset($data->timemodified);
 103  
 104          $data->userid = $this->get_mappingid('user', $data->userid);
 105          $data->groupid = $this->get_mappingid('group', $data->groupid);
 106          $data->dataid = $this->get_new_parentid('data');
 107  
 108          // insert the data_records record
 109          $newitemid = $DB->insert_record('data_records', $data);
 110          $this->set_mapping('data_record', $oldid, $newitemid, false); // no files associated
 111      }
 112  
 113      protected function process_data_content($data) {
 114          global $DB;
 115  
 116          $data = (object)$data;
 117          $oldid = $data->id;
 118  
 119          $data->fieldid = $this->get_mappingid('data_field', $data->fieldid);
 120          $data->recordid = $this->get_new_parentid('data_record');
 121  
 122          // insert the data_content record
 123          $newitemid = $DB->insert_record('data_content', $data);
 124          $this->set_mapping('data_content', $oldid, $newitemid, true); // files by this itemname
 125      }
 126  
 127      protected function process_data_rating($data) {
 128          global $DB;
 129  
 130          $data = (object)$data;
 131  
 132          // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
 133          $data->contextid = $this->task->get_contextid();
 134          $data->itemid    = $this->get_new_parentid('data_record');
 135          if ($data->scaleid < 0) { // scale found, get mapping
 136              $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
 137          }
 138          $data->rating = $data->value;
 139          $data->userid = $this->get_mappingid('user', $data->userid);
 140          $data->timecreated = $this->apply_date_offset($data->timecreated);
 141          $data->timemodified = $this->apply_date_offset($data->timemodified);
 142  
 143          // We need to check that component and ratingarea are both set here.
 144          if (empty($data->component)) {
 145              $data->component = 'mod_data';
 146          }
 147          if (empty($data->ratingarea)) {
 148              $data->ratingarea = 'entry';
 149          }
 150  
 151          $newitemid = $DB->insert_record('rating', $data);
 152      }
 153  
 154      protected function after_execute() {
 155          global $DB;
 156          // Add data related files, no need to match by itemname (just internally handled context)
 157          $this->add_related_files('mod_data', 'intro', null);
 158          // Add content related files, matching by itemname (data_content)
 159          $this->add_related_files('mod_data', 'content', 'data_content');
 160          // Adjust the data->defaultsort field
 161          if ($defaultsort = $DB->get_field('data', 'defaultsort', array('id' => $this->get_new_parentid('data')))) {
 162              if ($defaultsort = $this->get_mappingid('data_field', $defaultsort)) {
 163                  $DB->set_field('data', 'defaultsort', $defaultsort, array('id' => $this->get_new_parentid('data')));
 164              }
 165          }
 166      }
 167  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1