|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00032 class restore_forum_activity_structure_step extends restore_activity_structure_step { 00033 00034 protected function define_structure() { 00035 00036 $paths = array(); 00037 $userinfo = $this->get_setting_value('userinfo'); 00038 00039 $paths[] = new restore_path_element('forum', '/activity/forum'); 00040 if ($userinfo) { 00041 $paths[] = new restore_path_element('forum_discussion', '/activity/forum/discussions/discussion'); 00042 $paths[] = new restore_path_element('forum_post', '/activity/forum/discussions/discussion/posts/post'); 00043 $paths[] = new restore_path_element('forum_rating', '/activity/forum/discussions/discussion/posts/post/ratings/rating'); 00044 $paths[] = new restore_path_element('forum_subscription', '/activity/forum/subscriptions/subscription'); 00045 $paths[] = new restore_path_element('forum_read', '/activity/forum/readposts/read'); 00046 $paths[] = new restore_path_element('forum_track', '/activity/forum/trackedprefs/track'); 00047 } 00048 00049 // Return the paths wrapped into standard activity structure 00050 return $this->prepare_activity_structure($paths); 00051 } 00052 00053 protected function process_forum($data) { 00054 global $DB; 00055 00056 $data = (object)$data; 00057 $oldid = $data->id; 00058 $data->course = $this->get_courseid(); 00059 00060 $data->assesstimestart = $this->apply_date_offset($data->assesstimestart); 00061 $data->assesstimefinish = $this->apply_date_offset($data->assesstimefinish); 00062 if ($data->scale < 0) { // scale found, get mapping 00063 $data->scale = -($this->get_mappingid('scale', abs($data->scale))); 00064 } 00065 00066 $newitemid = $DB->insert_record('forum', $data); 00067 $this->apply_activity_instance($newitemid); 00068 } 00069 00070 protected function process_forum_discussion($data) { 00071 global $DB; 00072 00073 $data = (object)$data; 00074 $oldid = $data->id; 00075 $data->course = $this->get_courseid(); 00076 00077 $data->forum = $this->get_new_parentid('forum'); 00078 $data->timemodified = $this->apply_date_offset($data->timemodified); 00079 $data->timestart = $this->apply_date_offset($data->timestart); 00080 $data->timeend = $this->apply_date_offset($data->timeend); 00081 $data->userid = $this->get_mappingid('user', $data->userid); 00082 $data->groupid = $this->get_mappingid('group', $data->groupid); 00083 $data->usermodified = $this->get_mappingid('user', $data->usermodified); 00084 00085 $newitemid = $DB->insert_record('forum_discussions', $data); 00086 $this->set_mapping('forum_discussion', $oldid, $newitemid); 00087 } 00088 00089 protected function process_forum_post($data) { 00090 global $DB; 00091 00092 $data = (object)$data; 00093 $oldid = $data->id; 00094 00095 $data->discussion = $this->get_new_parentid('forum_discussion'); 00096 $data->created = $this->apply_date_offset($data->created); 00097 $data->modified = $this->apply_date_offset($data->modified); 00098 $data->userid = $this->get_mappingid('user', $data->userid); 00099 // If post has parent, map it (it has been already restored) 00100 if (!empty($data->parent)) { 00101 $data->parent = $this->get_mappingid('forum_post', $data->parent); 00102 } 00103 00104 $newitemid = $DB->insert_record('forum_posts', $data); 00105 $this->set_mapping('forum_post', $oldid, $newitemid, true); 00106 00107 // If !post->parent, it's the 1st post. Set it in discussion 00108 if (empty($data->parent)) { 00109 $DB->set_field('forum_discussions', 'firstpost', $newitemid, array('id' => $data->discussion)); 00110 } 00111 } 00112 00113 protected function process_forum_rating($data) { 00114 global $DB; 00115 00116 $data = (object)$data; 00117 00118 // Cannot use ratings API, cause, it's missing the ability to specify times (modified/created) 00119 $data->contextid = $this->task->get_contextid(); 00120 $data->itemid = $this->get_new_parentid('forum_post'); 00121 if ($data->scaleid < 0) { // scale found, get mapping 00122 $data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid))); 00123 } 00124 $data->rating = $data->value; 00125 $data->userid = $this->get_mappingid('user', $data->userid); 00126 $data->timecreated = $this->apply_date_offset($data->timecreated); 00127 $data->timemodified = $this->apply_date_offset($data->timemodified); 00128 00129 // We need to check that component and ratingarea are both set here. 00130 if (empty($data->component)) { 00131 $data->component = 'mod_forum'; 00132 } 00133 if (empty($data->ratingarea)) { 00134 $data->ratingarea = 'post'; 00135 } 00136 00137 $newitemid = $DB->insert_record('rating', $data); 00138 } 00139 00140 protected function process_forum_subscription($data) { 00141 global $DB; 00142 00143 $data = (object)$data; 00144 $oldid = $data->id; 00145 00146 $data->forum = $this->get_new_parentid('forum'); 00147 $data->userid = $this->get_mappingid('user', $data->userid); 00148 00149 $newitemid = $DB->insert_record('forum_subscriptions', $data); 00150 } 00151 00152 protected function process_forum_read($data) { 00153 global $DB; 00154 00155 $data = (object)$data; 00156 $oldid = $data->id; 00157 00158 $data->forumid = $this->get_new_parentid('forum'); 00159 $data->discussionid = $this->get_mappingid('forum_discussion', $data->discussionid); 00160 $data->postid = $this->get_mappingid('forum_post', $data->postid); 00161 $data->userid = $this->get_mappingid('user', $data->userid); 00162 00163 $newitemid = $DB->insert_record('forum_read', $data); 00164 } 00165 00166 protected function process_forum_track($data) { 00167 global $DB; 00168 00169 $data = (object)$data; 00170 $oldid = $data->id; 00171 00172 $data->forumid = $this->get_new_parentid('forum'); 00173 $data->userid = $this->get_mappingid('user', $data->userid); 00174 00175 $newitemid = $DB->insert_record('forum_track_prefs', $data); 00176 } 00177 00178 protected function after_execute() { 00179 global $DB; 00180 00181 // Add forum related files, no need to match by itemname (just internally handled context) 00182 $this->add_related_files('mod_forum', 'intro', null); 00183 00184 // If the forum is of type 'single' and no discussion has been ignited 00185 // (non-userinfo backup/restore) create the discussion here, using forum 00186 // information as base for the initial post. 00187 $forumid = $this->task->get_activityid(); 00188 $forumrec = $DB->get_record('forum', array('id' => $forumid)); 00189 if ($forumrec->type == 'single' && !$DB->record_exists('forum_discussions', array('forum' => $forumid))) { 00190 // Create single discussion/lead post from forum data 00191 $sd = new stdclass(); 00192 $sd->course = $forumrec->course; 00193 $sd->forum = $forumrec->id; 00194 $sd->name = $forumrec->name; 00195 $sd->assessed = $forumrec->assessed; 00196 $sd->message = $forumrec->intro; 00197 $sd->messageformat = $forumrec->introformat; 00198 $sd->messagetrust = true; 00199 $sd->mailnow = false; 00200 $sdid = forum_add_discussion($sd, null, $sillybyrefvar, $this->task->get_userid()); 00201 // Mark the post as mailed 00202 $DB->set_field ('forum_posts','mailed', '1', array('discussion' => $sdid)); 00203 // Copy all the files from mod_foum/intro to mod_forum/post 00204 $fs = get_file_storage(); 00205 $files = $fs->get_area_files($this->task->get_contextid(), 'mod_forum', 'intro'); 00206 foreach ($files as $file) { 00207 $newfilerecord = new stdclass(); 00208 $newfilerecord->filearea = 'post'; 00209 $newfilerecord->itemid = $DB->get_field('forum_discussions', 'firstpost', array('id' => $sdid)); 00210 $fs->create_file_from_storedfile($newfilerecord, $file); 00211 } 00212 } 00213 00214 // Add post related files, matching by itemname = 'forum_post' 00215 $this->add_related_files('mod_forum', 'post', 'forum_post'); 00216 $this->add_related_files('mod_forum', 'attachment', 'forum_post'); 00217 } 00218 }