[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/forum/backup/moodle2/ -> restore_forum_stepslib.php (source)

   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 restore steps that will be used by the restore_forum_activity_task
  27   */
  28  
  29  /**
  30   * Structure step to restore one forum activity
  31   */
  32  class restore_forum_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('forum', '/activity/forum');
  40          if ($userinfo) {
  41              $paths[] = new restore_path_element('forum_discussion', '/activity/forum/discussions/discussion');
  42              $paths[] = new restore_path_element('forum_post', '/activity/forum/discussions/discussion/posts/post');
  43              $paths[] = new restore_path_element('forum_discussion_sub', '/activity/forum/discussions/discussion/discussion_subs/discussion_sub');
  44              $paths[] = new restore_path_element('forum_rating', '/activity/forum/discussions/discussion/posts/post/ratings/rating');
  45              $paths[] = new restore_path_element('forum_subscription', '/activity/forum/subscriptions/subscription');
  46              $paths[] = new restore_path_element('forum_digest', '/activity/forum/digests/digest');
  47              $paths[] = new restore_path_element('forum_read', '/activity/forum/readposts/read');
  48              $paths[] = new restore_path_element('forum_track', '/activity/forum/trackedprefs/track');
  49          }
  50  
  51          // Return the paths wrapped into standard activity structure
  52          return $this->prepare_activity_structure($paths);
  53      }
  54  
  55      protected function process_forum($data) {
  56          global $DB;
  57  
  58          $data = (object)$data;
  59          $oldid = $data->id;
  60          $data->course = $this->get_courseid();
  61  
  62          $data->assesstimestart = $this->apply_date_offset($data->assesstimestart);
  63          $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish);
  64          if ($data->scale < 0) { // scale found, get mapping
  65              $data->scale = -($this->get_mappingid('scale', abs($data->scale)));
  66          }
  67  
  68          $newitemid = $DB->insert_record('forum', $data);
  69          $this->apply_activity_instance($newitemid);
  70      }
  71  
  72      protected function process_forum_discussion($data) {
  73          global $DB;
  74  
  75          $data = (object)$data;
  76          $oldid = $data->id;
  77          $data->course = $this->get_courseid();
  78  
  79          $data->forum = $this->get_new_parentid('forum');
  80          $data->timemodified = $this->apply_date_offset($data->timemodified);
  81          $data->timestart = $this->apply_date_offset($data->timestart);
  82          $data->timeend = $this->apply_date_offset($data->timeend);
  83          $data->userid = $this->get_mappingid('user', $data->userid);
  84          $data->groupid = $this->get_mappingid('group', $data->groupid);
  85          $data->usermodified = $this->get_mappingid('user', $data->usermodified);
  86  
  87          $newitemid = $DB->insert_record('forum_discussions', $data);
  88          $this->set_mapping('forum_discussion', $oldid, $newitemid);
  89      }
  90  
  91      protected function process_forum_post($data) {
  92          global $DB;
  93  
  94          $data = (object)$data;
  95          $oldid = $data->id;
  96  
  97          $data->discussion = $this->get_new_parentid('forum_discussion');
  98          $data->created = $this->apply_date_offset($data->created);
  99          $data->modified = $this->apply_date_offset($data->modified);
 100          $data->userid = $this->get_mappingid('user', $data->userid);
 101          // If post has parent, map it (it has been already restored)
 102          if (!empty($data->parent)) {
 103              $data->parent = $this->get_mappingid('forum_post', $data->parent);
 104          }
 105  
 106          $newitemid = $DB->insert_record('forum_posts', $data);
 107          $this->set_mapping('forum_post', $oldid, $newitemid, true);
 108  
 109          // If !post->parent, it's the 1st post. Set it in discussion
 110          if (empty($data->parent)) {
 111              $DB->set_field('forum_discussions', 'firstpost', $newitemid, array('id' => $data->discussion));
 112          }
 113      }
 114  
 115      protected function process_forum_rating($data) {
 116          global $DB;
 117  
 118          $data = (object)$data;
 119  
 120          // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created)
 121          $data->contextid = $this->task->get_contextid();
 122          $data->itemid    = $this->get_new_parentid('forum_post');
 123          if ($data->scaleid < 0) { // scale found, get mapping
 124              $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
 125          }
 126          $data->rating = $data->value;
 127          $data->userid = $this->get_mappingid('user', $data->userid);
 128          $data->timecreated = $this->apply_date_offset($data->timecreated);
 129          $data->timemodified = $this->apply_date_offset($data->timemodified);
 130  
 131          // We need to check that component and ratingarea are both set here.
 132          if (empty($data->component)) {
 133              $data->component = 'mod_forum';
 134          }
 135          if (empty($data->ratingarea)) {
 136              $data->ratingarea = 'post';
 137          }
 138  
 139          $newitemid = $DB->insert_record('rating', $data);
 140      }
 141  
 142      protected function process_forum_subscription($data) {
 143          global $DB;
 144  
 145          $data = (object)$data;
 146          $oldid = $data->id;
 147  
 148          $data->forum = $this->get_new_parentid('forum');
 149          $data->userid = $this->get_mappingid('user', $data->userid);
 150  
 151          $newitemid = $DB->insert_record('forum_subscriptions', $data);
 152          $this->set_mapping('forum_subscription', $oldid, $newitemid, true);
 153  
 154      }
 155  
 156      protected function process_forum_discussion_sub($data) {
 157          global $DB;
 158  
 159          $data = (object)$data;
 160          $oldid = $data->id;
 161  
 162          $data->discussion = $this->get_new_parentid('forum_discussion');
 163          $data->forum = $this->get_new_parentid('forum');
 164          $data->userid = $this->get_mappingid('user', $data->userid);
 165  
 166          $newitemid = $DB->insert_record('forum_discussion_subs', $data);
 167          $this->set_mapping('forum_discussion_sub', $oldid, $newitemid, true);
 168      }
 169  
 170      protected function process_forum_digest($data) {
 171          global $DB;
 172  
 173          $data = (object)$data;
 174          $oldid = $data->id;
 175  
 176          $data->forum = $this->get_new_parentid('forum');
 177          $data->userid = $this->get_mappingid('user', $data->userid);
 178  
 179          $newitemid = $DB->insert_record('forum_digests', $data);
 180      }
 181  
 182      protected function process_forum_read($data) {
 183          global $DB;
 184  
 185          $data = (object)$data;
 186          $oldid = $data->id;
 187  
 188          $data->forumid = $this->get_new_parentid('forum');
 189          $data->discussionid = $this->get_mappingid('forum_discussion', $data->discussionid);
 190          $data->postid = $this->get_mappingid('forum_post', $data->postid);
 191          $data->userid = $this->get_mappingid('user', $data->userid);
 192  
 193          $newitemid = $DB->insert_record('forum_read', $data);
 194      }
 195  
 196      protected function process_forum_track($data) {
 197          global $DB;
 198  
 199          $data = (object)$data;
 200          $oldid = $data->id;
 201  
 202          $data->forumid = $this->get_new_parentid('forum');
 203          $data->userid = $this->get_mappingid('user', $data->userid);
 204  
 205          $newitemid = $DB->insert_record('forum_track_prefs', $data);
 206      }
 207  
 208      protected function after_execute() {
 209          // Add forum related files, no need to match by itemname (just internally handled context)
 210          $this->add_related_files('mod_forum', 'intro', null);
 211  
 212          // Add post related files, matching by itemname = 'forum_post'
 213          $this->add_related_files('mod_forum', 'post', 'forum_post');
 214          $this->add_related_files('mod_forum', 'attachment', 'forum_post');
 215      }
 216  
 217      protected function after_restore() {
 218          global $DB;
 219  
 220          // If the forum is of type 'single' and no discussion has been ignited
 221          // (non-userinfo backup/restore) create the discussion here, using forum
 222          // information as base for the initial post.
 223          $forumid = $this->task->get_activityid();
 224          $forumrec = $DB->get_record('forum', array('id' => $forumid));
 225          if ($forumrec->type == 'single' && !$DB->record_exists('forum_discussions', array('forum' => $forumid))) {
 226              // Create single discussion/lead post from forum data
 227              $sd = new stdClass();
 228              $sd->course   = $forumrec->course;
 229              $sd->forum    = $forumrec->id;
 230              $sd->name     = $forumrec->name;
 231              $sd->assessed = $forumrec->assessed;
 232              $sd->message  = $forumrec->intro;
 233              $sd->messageformat = $forumrec->introformat;
 234              $sd->messagetrust  = true;
 235              $sd->mailnow  = false;
 236              $sdid = forum_add_discussion($sd, null, null, $this->task->get_userid());
 237              // Mark the post as mailed
 238              $DB->set_field ('forum_posts','mailed', '1', array('discussion' => $sdid));
 239              // Copy all the files from mod_foum/intro to mod_forum/post
 240              $fs = get_file_storage();
 241              $files = $fs->get_area_files($this->task->get_contextid(), 'mod_forum', 'intro');
 242              foreach ($files as $file) {
 243                  $newfilerecord = new stdClass();
 244                  $newfilerecord->filearea = 'post';
 245                  $newfilerecord->itemid   = $DB->get_field('forum_discussions', 'firstpost', array('id' => $sdid));
 246                  $fs->create_file_from_storedfile($newfilerecord, $file);
 247              }
 248          }
 249      }
 250  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1