| 1 | 
           efrain | 
           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 | 
                   }
  | 
        
        
           | 11 | 
           efrain | 
           72 | 
                   if (!empty($data->globalglossary) && !has_capability('mod/glossary:manageentries', context_system::instance())) {
  | 
        
        
            | 
            | 
           73 | 
                       $data->globalglossary = 0;
  | 
        
        
            | 
            | 
           74 | 
                   }
  | 
        
        
           | 1 | 
           efrain | 
           75 | 
                   if (!empty($data->mainglossary) and $data->mainglossary == 1 and
  | 
        
        
            | 
            | 
           76 | 
                       $DB->record_exists('glossary', array('mainglossary' => 1, 'course' => $this->get_courseid()))) {
  | 
        
        
            | 
            | 
           77 | 
                       // Only allow one main glossary in the course
  | 
        
        
            | 
            | 
           78 | 
                       $data->mainglossary = 0;
  | 
        
        
            | 
            | 
           79 | 
                   }
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
                   // insert the glossary record
  | 
        
        
            | 
            | 
           82 | 
                   $newitemid = $DB->insert_record('glossary', $data);
  | 
        
        
            | 
            | 
           83 | 
                   $this->apply_activity_instance($newitemid);
  | 
        
        
            | 
            | 
           84 | 
               }
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
            | 
            | 
           86 | 
               protected function process_glossary_entry($data) {
  | 
        
        
            | 
            | 
           87 | 
                   global $DB;
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
                   $data = (object)$data;
  | 
        
        
            | 
            | 
           90 | 
                   $oldid = $data->id;
  | 
        
        
            | 
            | 
           91 | 
              | 
        
        
            | 
            | 
           92 | 
                   $data->glossaryid = $this->get_new_parentid('glossary');
  | 
        
        
            | 
            | 
           93 | 
                   $data->userid = $this->get_mappingid('user', $data->userid);
  | 
        
        
            | 
            | 
           94 | 
                   $data->sourceglossaryid = $this->get_mappingid('glossary', $data->sourceglossaryid);
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           96 | 
                   $context = context_module::instance($this->task->get_moduleid());
  | 
        
        
            | 
            | 
           97 | 
                   if (!empty($data->definitiontrust) && !trusttext_trusted($context)) {
  | 
        
        
            | 
            | 
           98 | 
                       $data->definitiontrust = 0;
  | 
        
        
            | 
            | 
           99 | 
                   }
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
           | 1 | 
           efrain | 
           101 | 
                   // insert the entry record
  | 
        
        
            | 
            | 
           102 | 
                   $newitemid = $DB->insert_record('glossary_entries', $data);
  | 
        
        
            | 
            | 
           103 | 
                   $this->set_mapping('glossary_entry', $oldid, $newitemid, true); // childs and files by itemname
  | 
        
        
            | 
            | 
           104 | 
               }
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
               protected function process_glossary_alias($data) {
  | 
        
        
            | 
            | 
           107 | 
                   global $DB;
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
                   $data = (object)$data;
  | 
        
        
            | 
            | 
           110 | 
                   $oldid = $data->id;
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                   $data->entryid = $this->get_new_parentid('glossary_entry');
  | 
        
        
            | 
            | 
           113 | 
                   $data->alias =  $data->alias_text;
  | 
        
        
            | 
            | 
           114 | 
                   $newitemid = $DB->insert_record('glossary_alias', $data);
  | 
        
        
            | 
            | 
           115 | 
               }
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
               protected function process_glossary_rating($data) {
  | 
        
        
            | 
            | 
           118 | 
                   global $DB;
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                   $data = (object)$data;
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
                   // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
  | 
        
        
            | 
            | 
           123 | 
                   $data->contextid = $this->task->get_contextid();
  | 
        
        
            | 
            | 
           124 | 
                   $data->itemid    = $this->get_new_parentid('glossary_entry');
  | 
        
        
            | 
            | 
           125 | 
                   if ($data->scaleid < 0) { // scale found, get mapping
  | 
        
        
            | 
            | 
           126 | 
                       $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
  | 
        
        
            | 
            | 
           127 | 
                   }
  | 
        
        
            | 
            | 
           128 | 
                   $data->rating = $data->value;
  | 
        
        
            | 
            | 
           129 | 
                   $data->userid = $this->get_mappingid('user', $data->userid);
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
                   // Make sure that we have both component and ratingarea set. These were added in 2.1.
  | 
        
        
            | 
            | 
           132 | 
                   // Prior to that all ratings were for entries so we know what to set them too.
  | 
        
        
            | 
            | 
           133 | 
                   if (empty($data->component)) {
  | 
        
        
            | 
            | 
           134 | 
                       $data->component = 'mod_glossary';
  | 
        
        
            | 
            | 
           135 | 
                   }
  | 
        
        
            | 
            | 
           136 | 
                   if (empty($data->ratingarea)) {
  | 
        
        
            | 
            | 
           137 | 
                       $data->ratingarea = 'entry';
  | 
        
        
            | 
            | 
           138 | 
                   }
  | 
        
        
            | 
            | 
           139 | 
              | 
        
        
            | 
            | 
           140 | 
                   $newitemid = $DB->insert_record('rating', $data);
  | 
        
        
            | 
            | 
           141 | 
               }
  | 
        
        
            | 
            | 
           142 | 
              | 
        
        
            | 
            | 
           143 | 
               protected function process_glossary_entry_tag($data) {
  | 
        
        
            | 
            | 
           144 | 
                   $data = (object)$data;
  | 
        
        
            | 
            | 
           145 | 
              | 
        
        
            | 
            | 
           146 | 
                   if (!core_tag_tag::is_enabled('mod_glossary', 'glossary_entries')) { // Tags disabled in server, nothing to process.
  | 
        
        
            | 
            | 
           147 | 
                       return;
  | 
        
        
            | 
            | 
           148 | 
                   }
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
                   $tag = $data->rawname;
  | 
        
        
            | 
            | 
           151 | 
                   if (!$itemid = $this->get_mappingid('glossary_entry', $data->itemid)) {
  | 
        
        
            | 
            | 
           152 | 
                       // Some orphaned tag, we could not find the glossary entry for it - ignore.
  | 
        
        
            | 
            | 
           153 | 
                       return;
  | 
        
        
            | 
            | 
           154 | 
                   }
  | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
                   $context = context_module::instance($this->task->get_moduleid());
  | 
        
        
            | 
            | 
           157 | 
                   core_tag_tag::add_item_tag('mod_glossary', 'glossary_entries', $itemid, $context, $tag);
  | 
        
        
            | 
            | 
           158 | 
               }
  | 
        
        
            | 
            | 
           159 | 
              | 
        
        
            | 
            | 
           160 | 
               protected function process_glossary_category($data) {
  | 
        
        
            | 
            | 
           161 | 
                   global $DB;
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
            | 
            | 
           163 | 
                   $data = (object)$data;
  | 
        
        
            | 
            | 
           164 | 
                   $oldid = $data->id;
  | 
        
        
            | 
            | 
           165 | 
              | 
        
        
            | 
            | 
           166 | 
                   $data->glossaryid = $this->get_new_parentid('glossary');
  | 
        
        
            | 
            | 
           167 | 
                   $newitemid = $DB->insert_record('glossary_categories', $data);
  | 
        
        
            | 
            | 
           168 | 
                   $this->set_mapping('glossary_category', $oldid, $newitemid);
  | 
        
        
            | 
            | 
           169 | 
               }
  | 
        
        
            | 
            | 
           170 | 
              | 
        
        
            | 
            | 
           171 | 
               protected function process_glossary_category_entry($data) {
  | 
        
        
            | 
            | 
           172 | 
                   global $DB;
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
                   $data = (object)$data;
  | 
        
        
            | 
            | 
           175 | 
                   $oldid = $data->id;
  | 
        
        
            | 
            | 
           176 | 
              | 
        
        
            | 
            | 
           177 | 
                   $data->categoryid = $this->get_new_parentid('glossary_category');
  | 
        
        
            | 
            | 
           178 | 
                   $data->entryid    = $this->get_mappingid('glossary_entry', $data->entryid);
  | 
        
        
            | 
            | 
           179 | 
                   $newitemid = $DB->insert_record('glossary_entries_categories', $data);
  | 
        
        
            | 
            | 
           180 | 
               }
  | 
        
        
            | 
            | 
           181 | 
              | 
        
        
            | 
            | 
           182 | 
               protected function after_execute() {
  | 
        
        
            | 
            | 
           183 | 
                   // Add glossary related files, no need to match by itemname (just internally handled context)
  | 
        
        
            | 
            | 
           184 | 
                   $this->add_related_files('mod_glossary', 'intro', null);
  | 
        
        
            | 
            | 
           185 | 
                   // Add entries related files, matching by itemname (glossary_entry)
  | 
        
        
            | 
            | 
           186 | 
                   $this->add_related_files('mod_glossary', 'entry', 'glossary_entry');
  | 
        
        
            | 
            | 
           187 | 
                   $this->add_related_files('mod_glossary', 'attachment', 'glossary_entry');
  | 
        
        
            | 
            | 
           188 | 
               }
  | 
        
        
            | 
            | 
           189 | 
           }
  |