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_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 backup steps that will be used by the backup_data_activity_task
  27   */
  28  
  29  /**
  30   * Define the complete data structure for backup, with file and id annotations
  31   */
  32  class backup_data_activity_structure_step extends backup_activity_structure_step {
  33  
  34      protected function define_structure() {
  35  
  36          // To know if we are including userinfo
  37          $userinfo = $this->get_setting_value('userinfo');
  38  
  39          // Define each element separated
  40          $data = new backup_nested_element('data', array('id'), array(
  41              'name', 'intro', 'introformat', 'comments',
  42              'timeavailablefrom', 'timeavailableto', 'timeviewfrom', 'timeviewto',
  43              'requiredentries', 'requiredentriestoview', 'maxentries', 'rssarticles',
  44              'singletemplate', 'listtemplate', 'listtemplateheader', 'listtemplatefooter',
  45              'addtemplate', 'rsstemplate', 'rsstitletemplate', 'csstemplate',
  46              'jstemplate', 'asearchtemplate', 'approval', 'manageapproved', 'scale',
  47              'assessed', 'assesstimestart', 'assesstimefinish', 'defaultsort',
  48              'defaultsortdir', 'editany', 'notification', 'timemodified', 'config', 'completionentries'));
  49  
  50          $tags = new backup_nested_element('recordstags');
  51          $tag = new backup_nested_element('tag', array('id'), array('itemid', 'rawname'));
  52  
  53          $fields = new backup_nested_element('fields');
  54  
  55          $field = new backup_nested_element('field', array('id'), array(
  56              'type', 'name', 'description', 'required', 'param1', 'param2',
  57              'param3', 'param4', 'param5', 'param6',
  58              'param7', 'param8', 'param9', 'param10'));
  59  
  60          $records = new backup_nested_element('records');
  61  
  62          $record = new backup_nested_element('record', array('id'), array(
  63              'userid', 'groupid', 'timecreated', 'timemodified',
  64              'approved'));
  65  
  66          $contents = new backup_nested_element('contents');
  67  
  68          $content = new backup_nested_element('content', array('id'), array(
  69              'fieldid', 'content', 'content1', 'content2',
  70              'content3', 'content4'));
  71  
  72          $ratings = new backup_nested_element('ratings');
  73  
  74          $rating = new backup_nested_element('rating', array('id'), array(
  75              'component', 'ratingarea', 'scaleid', 'value', 'userid', 'timecreated', 'timemodified'));
  76  
  77          // Build the tree
  78          $data->add_child($fields);
  79          $fields->add_child($field);
  80  
  81          $data->add_child($records);
  82          $records->add_child($record);
  83  
  84          $record->add_child($contents);
  85          $contents->add_child($content);
  86  
  87          $record->add_child($ratings);
  88          $ratings->add_child($rating);
  89  
  90          $data->add_child($tags);
  91          $tags->add_child($tag);
  92  
  93          // Define sources
  94          $data->set_source_table('data', array('id' => backup::VAR_ACTIVITYID));
  95  
  96          $field->set_source_sql('
  97              SELECT *
  98                FROM {data_fields}
  99               WHERE dataid = ?',
 100              array(backup::VAR_PARENTID));
 101  
 102          // All the rest of elements only happen if we are including user info
 103          if ($userinfo) {
 104              $record->set_source_table('data_records', array('dataid' => backup::VAR_PARENTID));
 105  
 106              $content->set_source_table('data_content', array('recordid' => backup::VAR_PARENTID));
 107  
 108              $rating->set_source_table('rating', array('contextid'  => backup::VAR_CONTEXTID,
 109                                                        'itemid'     => backup::VAR_PARENTID,
 110                                                        'component'  => backup_helper::is_sqlparam('mod_data'),
 111                                                        'ratingarea' => backup_helper::is_sqlparam('entry')));
 112              $rating->set_source_alias('rating', 'value');
 113              if (core_tag_tag::is_enabled('mod_data', 'data_records')) {
 114                  $tag->set_source_sql('SELECT t.id, ti.itemid, t.rawname
 115                                          FROM {tag} t
 116                                          JOIN {tag_instance} ti
 117                                            ON ti.tagid = t.id
 118                                         WHERE ti.itemtype = ?
 119                                           AND ti.component = ?
 120                                           AND ti.contextid = ?', array(
 121                      backup_helper::is_sqlparam('data_records'),
 122                      backup_helper::is_sqlparam('mod_data'),
 123                      backup::VAR_CONTEXTID));
 124              }
 125          }
 126  
 127          // Define id annotations
 128          $data->annotate_ids('scale', 'scale');
 129  
 130          $record->annotate_ids('user', 'userid');
 131          $record->annotate_ids('group', 'groupid');
 132  
 133          $rating->annotate_ids('scale', 'scaleid');
 134          $rating->annotate_ids('user', 'userid');
 135  
 136          // Define file annotations
 137          $data->annotate_files('mod_data', 'intro', null); // This file area hasn't itemid
 138          $content->annotate_files('mod_data', 'content', 'id'); // By content->id
 139  
 140          // Return the root element (data), wrapped into standard activity structure
 141          return $this->prepare_activity_structure($data);
 142      }
 143  }