Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is 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_forum
  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_forum_activity_task
  27   */
  28  
  29  /**
  30   * Define the complete forum structure for backup, with file and id annotations
  31   */
  32  class backup_forum_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  
  41          $forum = new backup_nested_element('forum', array('id'), array(
  42              'type', 'name', 'intro', 'introformat', 'duedate', 'cutoffdate',
  43              'assessed', 'assesstimestart', 'assesstimefinish', 'scale',
  44              'maxbytes', 'maxattachments', 'forcesubscribe', 'trackingtype',
  45              'rsstype', 'rssarticles', 'timemodified', 'warnafter',
  46              'blockafter', 'blockperiod', 'completiondiscussions', 'completionreplies',
  47              'completionposts', 'displaywordcount', 'lockdiscussionafter', 'grade_forum'));
  48  
  49          $discussions = new backup_nested_element('discussions');
  50  
  51          $discussion = new backup_nested_element('discussion', array('id'), array(
  52              'name', 'firstpost', 'userid', 'groupid',
  53              'assessed', 'timemodified', 'usermodified', 'timestart',
  54              'timeend', 'pinned', 'timelocked'));
  55  
  56          $posts = new backup_nested_element('posts');
  57  
  58          $post = new backup_nested_element('post', array('id'), array(
  59              'parent', 'userid', 'created', 'modified',
  60              'mailed', 'subject', 'message', 'messageformat',
  61              'messagetrust', 'attachment', 'totalscore', 'mailnow', 'privatereplyto'));
  62  
  63          $tags = new backup_nested_element('poststags');
  64          $tag = new backup_nested_element('tag', array('id'), array('itemid', 'rawname'));
  65  
  66          $ratings = new backup_nested_element('ratings');
  67  
  68          $rating = new backup_nested_element('rating', array('id'), array(
  69              'component', 'ratingarea', 'scaleid', 'value', 'userid', 'timecreated', 'timemodified'));
  70  
  71          $discussionsubs = new backup_nested_element('discussion_subs');
  72  
  73          $discussionsub = new backup_nested_element('discussion_sub', array('id'), array(
  74              'userid',
  75              'preference',
  76          ));
  77  
  78          $subscriptions = new backup_nested_element('subscriptions');
  79  
  80          $subscription = new backup_nested_element('subscription', array('id'), array(
  81              'userid'));
  82  
  83          $digests = new backup_nested_element('digests');
  84  
  85          $digest = new backup_nested_element('digest', array('id'), array(
  86              'userid', 'maildigest'));
  87  
  88          $readposts = new backup_nested_element('readposts');
  89  
  90          $read = new backup_nested_element('read', array('id'), array(
  91              'userid', 'discussionid', 'postid', 'firstread',
  92              'lastread'));
  93  
  94          $trackedprefs = new backup_nested_element('trackedprefs');
  95  
  96          $track = new backup_nested_element('track', array('id'), array(
  97              'userid'));
  98  
  99          $grades = new backup_nested_element('grades');
 100  
 101          $grade = new backup_nested_element('grade', ['id'], [
 102              'forum',
 103              'itemnumber',
 104              'userid',
 105              'grade',
 106              'timecreated',
 107              'timemodified',
 108          ]);
 109  
 110          // Build the tree
 111  
 112          $forum->add_child($discussions);
 113          $discussions->add_child($discussion);
 114  
 115          $forum->add_child($subscriptions);
 116          $subscriptions->add_child($subscription);
 117  
 118          $forum->add_child($digests);
 119          $digests->add_child($digest);
 120  
 121          $forum->add_child($readposts);
 122          $readposts->add_child($read);
 123  
 124          $forum->add_child($trackedprefs);
 125          $trackedprefs->add_child($track);
 126  
 127          $forum->add_child($tags);
 128          $tags->add_child($tag);
 129  
 130          $forum->add_child($grades);
 131          $grades->add_child($grade);
 132  
 133          $discussion->add_child($posts);
 134          $posts->add_child($post);
 135  
 136          $post->add_child($ratings);
 137          $ratings->add_child($rating);
 138  
 139          $discussion->add_child($discussionsubs);
 140          $discussionsubs->add_child($discussionsub);
 141  
 142          // Define sources
 143  
 144          $forum->set_source_table('forum', array('id' => backup::VAR_ACTIVITYID));
 145  
 146          // All these source definitions only happen if we are including user info
 147          if ($userinfo) {
 148              $discussion->set_source_sql('
 149                  SELECT *
 150                    FROM {forum_discussions}
 151                   WHERE forum = ?',
 152                  array(backup::VAR_PARENTID));
 153  
 154              // Need posts ordered by id so parents are always before childs on restore
 155              $post->set_source_table('forum_posts', array('discussion' => backup::VAR_PARENTID), 'id ASC');
 156              $discussionsub->set_source_table('forum_discussion_subs', array('discussion' => backup::VAR_PARENTID));
 157  
 158              $subscription->set_source_table('forum_subscriptions', array('forum' => backup::VAR_PARENTID));
 159              $digest->set_source_table('forum_digests', array('forum' => backup::VAR_PARENTID));
 160  
 161              $read->set_source_table('forum_read', array('forumid' => backup::VAR_PARENTID));
 162  
 163              $track->set_source_table('forum_track_prefs', array('forumid' => backup::VAR_PARENTID));
 164  
 165              $rating->set_source_table('rating', array('contextid'  => backup::VAR_CONTEXTID,
 166                                                        'component'  => backup_helper::is_sqlparam('mod_forum'),
 167                                                        'ratingarea' => backup_helper::is_sqlparam('post'),
 168                                                        'itemid'     => backup::VAR_PARENTID));
 169              $rating->set_source_alias('rating', 'value');
 170  
 171              if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) {
 172                  // Backup all tags for all forum posts in this forum.
 173                  $tag->set_source_sql('SELECT t.id, ti.itemid, t.rawname
 174                                          FROM {tag} t
 175                                          JOIN {tag_instance} ti ON ti.tagid = t.id
 176                                         WHERE ti.itemtype = ?
 177                                           AND ti.component = ?
 178                                           AND ti.contextid = ?', array(
 179                      backup_helper::is_sqlparam('forum_posts'),
 180                      backup_helper::is_sqlparam('mod_forum'),
 181                      backup::VAR_CONTEXTID));
 182              }
 183  
 184              $grade->set_source_table('forum_grades', array('forum' => backup::VAR_PARENTID));
 185          }
 186  
 187          // Define id annotations
 188  
 189          $forum->annotate_ids('scale', 'scale');
 190  
 191          $discussion->annotate_ids('group', 'groupid');
 192  
 193          $post->annotate_ids('user', 'userid');
 194  
 195          $discussionsub->annotate_ids('user', 'userid');
 196  
 197          $rating->annotate_ids('scale', 'scaleid');
 198  
 199          $rating->annotate_ids('user', 'userid');
 200  
 201          $subscription->annotate_ids('user', 'userid');
 202  
 203          $digest->annotate_ids('user', 'userid');
 204  
 205          $read->annotate_ids('user', 'userid');
 206  
 207          $track->annotate_ids('user', 'userid');
 208  
 209          $grade->annotate_ids('userid', 'userid');
 210  
 211          $grade->annotate_ids('forum', 'forum');
 212          // Define file annotations
 213  
 214          $forum->annotate_files('mod_forum', 'intro', null); // This file area hasn't itemid
 215  
 216          $post->annotate_files('mod_forum', 'post', 'id');
 217          $post->annotate_files('mod_forum', 'attachment', 'id');
 218  
 219          // Return the root element (forum), wrapped into standard activity structure
 220          return $this->prepare_activity_structure($forum);
 221      }
 222  
 223  }