[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * mod_forum data generator 19 * 20 * @package mod_forum 21 * @category test 22 * @copyright 2012 Petr Skoda {@link http://skodak.org} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 29 /** 30 * Forum module data generator class 31 * 32 * @package mod_forum 33 * @category test 34 * @copyright 2012 Petr Skoda {@link http://skodak.org} 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class mod_forum_generator extends testing_module_generator { 38 39 /** 40 * @var int keep track of how many forum discussions have been created. 41 */ 42 protected $forumdiscussioncount = 0; 43 44 /** 45 * @var int keep track of how many forum posts have been created. 46 */ 47 protected $forumpostcount = 0; 48 49 /** 50 * @var int keep track of how many forum subscriptions have been created. 51 */ 52 protected $forumsubscriptionscount = 0; 53 54 /** 55 * To be called from data reset code only, 56 * do not use in tests. 57 * @return void 58 */ 59 public function reset() { 60 $this->forumdiscussioncount = 0; 61 $this->forumpostcount = 0; 62 $this->forumsubscriptionscount = 0; 63 64 parent::reset(); 65 } 66 67 public function create_instance($record = null, array $options = null) { 68 global $CFG; 69 require_once($CFG->dirroot.'/mod/forum/lib.php'); 70 $record = (object)(array)$record; 71 72 if (!isset($record->type)) { 73 $record->type = 'general'; 74 } 75 if (!isset($record->assessed)) { 76 $record->assessed = 0; 77 } 78 if (!isset($record->scale)) { 79 $record->scale = 0; 80 } 81 if (!isset($record->forcesubscribe)) { 82 $record->forcesubscribe = FORUM_CHOOSESUBSCRIBE; 83 } 84 85 return parent::create_instance($record, (array)$options); 86 } 87 88 /** 89 * Function to create a dummy subscription. 90 * 91 * @param array|stdClass $record 92 * @return stdClass the subscription object 93 */ 94 public function create_subscription($record = null) { 95 global $DB; 96 97 // Increment the forum subscription count. 98 $this->forumsubscriptionscount++; 99 100 $record = (array)$record; 101 102 if (!isset($record['course'])) { 103 throw new coding_exception('course must be present in phpunit_util::create_subscription() $record'); 104 } 105 106 if (!isset($record['forum'])) { 107 throw new coding_exception('forum must be present in phpunit_util::create_subscription() $record'); 108 } 109 110 if (!isset($record['userid'])) { 111 throw new coding_exception('userid must be present in phpunit_util::create_subscription() $record'); 112 } 113 114 $record = (object)$record; 115 116 // Add the subscription. 117 $record->id = $DB->insert_record('forum_subscriptions', $record); 118 119 return $record; 120 } 121 122 /** 123 * Function to create a dummy discussion. 124 * 125 * @param array|stdClass $record 126 * @return stdClass the discussion object 127 */ 128 public function create_discussion($record = null) { 129 global $DB; 130 131 // Increment the forum discussion count. 132 $this->forumdiscussioncount++; 133 134 $record = (array) $record; 135 136 if (!isset($record['course'])) { 137 throw new coding_exception('course must be present in phpunit_util::create_discussion() $record'); 138 } 139 140 if (!isset($record['forum'])) { 141 throw new coding_exception('forum must be present in phpunit_util::create_discussion() $record'); 142 } 143 144 if (!isset($record['userid'])) { 145 throw new coding_exception('userid must be present in phpunit_util::create_discussion() $record'); 146 } 147 148 if (!isset($record['name'])) { 149 $record['name'] = "Discussion " . $this->forumdiscussioncount; 150 } 151 152 if (!isset($record['subject'])) { 153 $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount; 154 } 155 156 if (!isset($record['message'])) { 157 $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount); 158 } 159 160 if (!isset($record['messageformat'])) { 161 $record['messageformat'] = editors_get_preferred_format(); 162 } 163 164 if (!isset($record['messagetrust'])) { 165 $record['messagetrust'] = ""; 166 } 167 168 if (!isset($record['assessed'])) { 169 $record['assessed'] = '1'; 170 } 171 172 if (!isset($record['groupid'])) { 173 $record['groupid'] = "-1"; 174 } 175 176 if (!isset($record['timestart'])) { 177 $record['timestart'] = "0"; 178 } 179 180 if (!isset($record['timeend'])) { 181 $record['timeend'] = "0"; 182 } 183 184 if (!isset($record['mailnow'])) { 185 $record['mailnow'] = "0"; 186 } 187 188 if (isset($record['timemodified'])) { 189 $timemodified = $record['timemodified']; 190 } 191 192 if (!isset($record['pinned'])) { 193 $record['pinned'] = FORUM_DISCUSSION_UNPINNED; 194 } 195 196 if (isset($record['mailed'])) { 197 $mailed = $record['mailed']; 198 } 199 200 $record = (object) $record; 201 202 // Add the discussion. 203 $record->id = forum_add_discussion($record, null, null, $record->userid); 204 205 if (isset($timemodified) || isset($mailed)) { 206 $post = $DB->get_record('forum_posts', array('discussion' => $record->id)); 207 208 if (isset($mailed)) { 209 $post->mailed = $mailed; 210 } 211 212 if (isset($timemodified)) { 213 // Enforce the time modified. 214 $record->timemodified = $timemodified; 215 $post->modified = $post->created = $timemodified; 216 217 $DB->update_record('forum_discussions', $record); 218 } 219 220 $DB->update_record('forum_posts', $post); 221 } 222 223 return $record; 224 } 225 226 /** 227 * Function to create a dummy post. 228 * 229 * @param array|stdClass $record 230 * @return stdClass the post object 231 */ 232 public function create_post($record = null) { 233 global $DB; 234 235 // Increment the forum post count. 236 $this->forumpostcount++; 237 238 // Variable to store time. 239 $time = time() + $this->forumpostcount; 240 241 $record = (array) $record; 242 243 if (!isset($record['discussion'])) { 244 throw new coding_exception('discussion must be present in phpunit_util::create_post() $record'); 245 } 246 247 if (!isset($record['userid'])) { 248 throw new coding_exception('userid must be present in phpunit_util::create_post() $record'); 249 } 250 251 if (!isset($record['parent'])) { 252 $record['parent'] = 0; 253 } 254 255 if (!isset($record['subject'])) { 256 $record['subject'] = 'Forum post subject ' . $this->forumpostcount; 257 } 258 259 if (!isset($record['message'])) { 260 $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount); 261 } 262 263 if (!isset($record['created'])) { 264 $record['created'] = $time; 265 } 266 267 if (!isset($record['modified'])) { 268 $record['modified'] = $time; 269 } 270 271 if (!isset($record['mailed'])) { 272 $record['mailed'] = 0; 273 } 274 275 if (!isset($record['messageformat'])) { 276 $record['messageformat'] = 0; 277 } 278 279 if (!isset($record['messagetrust'])) { 280 $record['messagetrust'] = 0; 281 } 282 283 if (!isset($record['attachment'])) { 284 $record['attachment'] = ""; 285 } 286 287 if (!isset($record['totalscore'])) { 288 $record['totalscore'] = 0; 289 } 290 291 if (!isset($record['mailnow'])) { 292 $record['mailnow'] = 0; 293 } 294 295 $record = (object) $record; 296 297 // Add the post. 298 $record->id = $DB->insert_record('forum_posts', $record); 299 300 // Update the last post. 301 forum_discussion_update_last_post($record->discussion); 302 303 return $record; 304 } 305 306 public function create_content($instance, $record = array()) { 307 global $USER, $DB; 308 $record = (array)$record + array( 309 'forum' => $instance->id, 310 'userid' => $USER->id, 311 'course' => $instance->course 312 ); 313 if (empty($record['discussion']) && empty($record['parent'])) { 314 // Create discussion. 315 $discussion = $this->create_discussion($record); 316 $post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost)); 317 } else { 318 // Create post. 319 if (empty($record['parent'])) { 320 $record['parent'] = $DB->get_field('forum_discussions', 'firstpost', array('id' => $record['discussion']), MUST_EXIST); 321 } else if (empty($record['discussion'])) { 322 $record['discussion'] = $DB->get_field('forum_posts', 'discussion', array('id' => $record['parent']), MUST_EXIST); 323 } 324 $post = $this->create_post($record); 325 } 326 return $post; 327 } 328 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |