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  // 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_feedback
  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_feedback_activity_task
  26   */
  27  
  28  /**
  29   * Structure step to restore one feedback activity
  30   */
  31  class restore_feedback_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('feedback', '/activity/feedback');
  39          $paths[] = new restore_path_element('feedback_item', '/activity/feedback/items/item');
  40          if ($userinfo) {
  41              $paths[] = new restore_path_element('feedback_completed', '/activity/feedback/completeds/completed');
  42              $paths[] = new restore_path_element('feedback_value', '/activity/feedback/completeds/completed/values/value');
  43          }
  44  
  45          // Return the paths wrapped into standard activity structure
  46          return $this->prepare_activity_structure($paths);
  47      }
  48  
  49      protected function process_feedback($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 feedback record
  62          $newitemid = $DB->insert_record('feedback', $data);
  63          // immediately after inserting "activity" record, call this
  64          $this->apply_activity_instance($newitemid);
  65      }
  66  
  67      protected function process_feedback_item($data) {
  68          global $DB;
  69  
  70          $data = (object)$data;
  71          $oldid = $data->id;
  72          $data->feedback = $this->get_new_parentid('feedback');
  73  
  74          $newitemid = $DB->insert_record('feedback_item', $data);
  75          $this->set_mapping('feedback_item', $oldid, $newitemid, true); // Can have files
  76      }
  77  
  78      protected function process_feedback_completed($data) {
  79          global $DB;
  80  
  81          $data = (object)$data;
  82          $oldid = $data->id;
  83          $data->feedback = $this->get_new_parentid('feedback');
  84          $data->userid = $this->get_mappingid('user', $data->userid);
  85          if ($this->task->is_samesite() && !empty($data->courseid)) {
  86              $data->courseid = $data->courseid;
  87          } else if ($this->get_courseid() == SITEID) {
  88              $data->courseid = SITEID;
  89          } else {
  90              $data->courseid = 0;
  91          }
  92  
  93          $newitemid = $DB->insert_record('feedback_completed', $data);
  94          $this->set_mapping('feedback_completed', $oldid, $newitemid);
  95      }
  96  
  97      protected function process_feedback_value($data) {
  98          global $DB;
  99  
 100          $data = (object)$data;
 101          $oldid = $data->id;
 102          $data->completed = $this->get_new_parentid('feedback_completed');
 103          $data->item = $this->get_mappingid('feedback_item', $data->item);
 104          if ($this->task->is_samesite() && !empty($data->course_id)) {
 105              $data->course_id = $data->course_id;
 106          } else if ($this->get_courseid() == SITEID) {
 107              $data->course_id = SITEID;
 108          } else {
 109              $data->course_id = 0;
 110          }
 111  
 112          $newitemid = $DB->insert_record('feedback_value', $data);
 113          $this->set_mapping('feedback_value', $oldid, $newitemid);
 114      }
 115  
 116      protected function after_execute() {
 117          global $DB;
 118          // Add feedback related files, no need to match by itemname (just internally handled context)
 119          $this->add_related_files('mod_feedback', 'intro', null);
 120          $this->add_related_files('mod_feedback', 'page_after_submit', null);
 121          $this->add_related_files('mod_feedback', 'item', 'feedback_item');
 122  
 123          // Once all items are restored we can set their dependency.
 124          if ($records = $DB->get_records('feedback_item', array('feedback' => $this->task->get_activityid()))) {
 125              foreach ($records as $record) {
 126                  // Get new id for dependitem if present. This will also reset dependitem if not found.
 127                  $record->dependitem = $this->get_mappingid('feedback_item', $record->dependitem);
 128                  $DB->update_record('feedback_item', $record);
 129              }
 130          }
 131      }
 132  }