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  
   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_choice
  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_choice_activity_task
  27   */
  28  
  29  /**
  30   * Structure step to restore one choice activity
  31   */
  32  class restore_choice_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('choice', '/activity/choice');
  40          $paths[] = new restore_path_element('choice_option', '/activity/choice/options/option');
  41          if ($userinfo) {
  42              $paths[] = new restore_path_element('choice_answer', '/activity/choice/answers/answer');
  43          }
  44  
  45          // Return the paths wrapped into standard activity structure
  46          return $this->prepare_activity_structure($paths);
  47      }
  48  
  49      protected function process_choice($data) {
  50          global $DB;
  51  
  52          $data = (object)$data;
  53          $oldid = $data->id;
  54          $data->course = $this->get_courseid();
  55  
  56          // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  57          // See MDL-9367.
  58          $data->timeopen = $this->apply_date_offset($data->timeopen);
  59          $data->timeclose = $this->apply_date_offset($data->timeclose);
  60  
  61          // insert the choice record
  62          $newitemid = $DB->insert_record('choice', $data);
  63          // immediately after inserting "activity" record, call this
  64          $this->apply_activity_instance($newitemid);
  65      }
  66  
  67      protected function process_choice_option($data) {
  68          global $DB;
  69  
  70          $data = (object)$data;
  71          $oldid = $data->id;
  72  
  73          $data->choiceid = $this->get_new_parentid('choice');
  74  
  75          $newitemid = $DB->insert_record('choice_options', $data);
  76          $this->set_mapping('choice_option', $oldid, $newitemid);
  77      }
  78  
  79      protected function process_choice_answer($data) {
  80          global $DB;
  81  
  82          $data = (object)$data;
  83  
  84          $data->choiceid = $this->get_new_parentid('choice');
  85          $data->optionid = $this->get_mappingid('choice_option', $data->optionid);
  86          $data->userid = $this->get_mappingid('user', $data->userid);
  87  
  88          $newitemid = $DB->insert_record('choice_answers', $data);
  89          // No need to save this mapping as far as nothing depend on it
  90          // (child paths, file areas nor links decoder)
  91      }
  92  
  93      protected function after_execute() {
  94          // Add choice related files, no need to match by itemname (just internally handled context)
  95          $this->add_related_files('mod_choice', 'intro', null);
  96      }
  97  }