Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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_glossary
  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_glossary_activity_task
  27   */
  28  
  29  /**
  30   * Structure step to restore one glossary activity
  31   */
  32  class restore_glossary_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('glossary', '/activity/glossary');
  40          $paths[] = new restore_path_element('glossary_category', '/activity/glossary/categories/category');
  41          if ($userinfo) {
  42              $paths[] = new restore_path_element('glossary_entry', '/activity/glossary/entries/entry');
  43              $paths[] = new restore_path_element('glossary_entry_tag', '/activity/glossary/entriestags/tag');
  44              $paths[] = new restore_path_element('glossary_alias', '/activity/glossary/entries/entry/aliases/alias');
  45              $paths[] = new restore_path_element('glossary_rating', '/activity/glossary/entries/entry/ratings/rating');
  46              $paths[] = new restore_path_element('glossary_category_entry',
  47                                                  '/activity/glossary/categories/category/category_entries/category_entry');
  48          }
  49  
  50          // Return the paths wrapped into standard activity structure
  51          return $this->prepare_activity_structure($paths);
  52      }
  53  
  54      protected function process_glossary($data) {
  55          global $DB;
  56  
  57          $data = (object)$data;
  58          $oldid = $data->id;
  59          $data->course = $this->get_courseid();
  60  
  61          // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  62          // See MDL-9367.
  63          $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
  64          $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
  65          if ($data->scale < 0) { // scale found, get mapping
  66              $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
  67          }
  68          $formats = get_list_of_plugins('mod/glossary/formats'); // Check format
  69          if (!in_array($data->displayformat, $formats)) {
  70              $data->displayformat = 'dictionary';
  71          }
  72          if (!empty($data->mainglossary) and $data->mainglossary == 1 and
  73              $DB->record_exists('glossary', array('mainglossary' => 1, 'course' => $this->get_courseid()))) {
  74              // Only allow one main glossary in the course
  75              $data->mainglossary = 0;
  76          }
  77  
  78          // insert the glossary record
  79          $newitemid = $DB->insert_record('glossary', $data);
  80          $this->apply_activity_instance($newitemid);
  81      }
  82  
  83      protected function process_glossary_entry($data) {
  84          global $DB;
  85  
  86          $data = (object)$data;
  87          $oldid = $data->id;
  88  
  89          $data->glossaryid = $this->get_new_parentid('glossary');
  90          $data->userid = $this->get_mappingid('user', $data->userid);
  91          $data->sourceglossaryid = $this->get_mappingid('glossary', $data->sourceglossaryid);
  92  
  93          // insert the entry record
  94          $newitemid = $DB->insert_record('glossary_entries', $data);
  95          $this->set_mapping('glossary_entry', $oldid, $newitemid, true); // childs and files by itemname
  96      }
  97  
  98      protected function process_glossary_alias($data) {
  99          global $DB;
 100  
 101          $data = (object)$data;
 102          $oldid = $data->id;
 103  
 104          $data->entryid = $this->get_new_parentid('glossary_entry');
 105          $data->alias =  $data->alias_text;
 106          $newitemid = $DB->insert_record('glossary_alias', $data);
 107      }
 108  
 109      protected function process_glossary_rating($data) {
 110          global $DB;
 111  
 112          $data = (object)$data;
 113  
 114          // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
 115          $data->contextid = $this->task->get_contextid();
 116          $data->itemid    = $this->get_new_parentid('glossary_entry');
 117          if ($data->scaleid < 0) { // scale found, get mapping
 118              $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
 119          }
 120          $data->rating = $data->value;
 121          $data->userid = $this->get_mappingid('user', $data->userid);
 122  
 123          // Make sure that we have both component and ratingarea set. These were added in 2.1.
 124          // Prior to that all ratings were for entries so we know what to set them too.
 125          if (empty($data->component)) {
 126              $data->component = 'mod_glossary';
 127          }
 128          if (empty($data->ratingarea)) {
 129              $data->ratingarea = 'entry';
 130          }
 131  
 132          $newitemid = $DB->insert_record('rating', $data);
 133      }
 134  
 135      protected function process_glossary_entry_tag($data) {
 136          $data = (object)$data;
 137  
 138          if (!core_tag_tag::is_enabled('mod_glossary', 'glossary_entries')) { // Tags disabled in server, nothing to process.
 139              return;
 140          }
 141  
 142          $tag = $data->rawname;
 143          if (!$itemid = $this->get_mappingid('glossary_entry', $data->itemid)) {
 144              // Some orphaned tag, we could not find the glossary entry for it - ignore.
 145              return;
 146          }
 147  
 148          $context = context_module::instance($this->task->get_moduleid());
 149          core_tag_tag::add_item_tag('mod_glossary', 'glossary_entries', $itemid, $context, $tag);
 150      }
 151  
 152      protected function process_glossary_category($data) {
 153          global $DB;
 154  
 155          $data = (object)$data;
 156          $oldid = $data->id;
 157  
 158          $data->glossaryid = $this->get_new_parentid('glossary');
 159          $newitemid = $DB->insert_record('glossary_categories', $data);
 160          $this->set_mapping('glossary_category', $oldid, $newitemid);
 161      }
 162  
 163      protected function process_glossary_category_entry($data) {
 164          global $DB;
 165  
 166          $data = (object)$data;
 167          $oldid = $data->id;
 168  
 169          $data->categoryid = $this->get_new_parentid('glossary_category');
 170          $data->entryid    = $this->get_mappingid('glossary_entry', $data->entryid);
 171          $newitemid = $DB->insert_record('glossary_entries_categories', $data);
 172      }
 173  
 174      protected function after_execute() {
 175          // Add glossary related files, no need to match by itemname (just internally handled context)
 176          $this->add_related_files('mod_glossary', 'intro', null);
 177          // Add entries related files, matching by itemname (glossary_entry)
 178          $this->add_related_files('mod_glossary', 'entry', 'glossary_entry');
 179          $this->add_related_files('mod_glossary', 'attachment', 'glossary_entry');
 180      }
 181  }