[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/forum/tests/generator/ -> lib.php (source)

   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          $record = (object) $record;
 189  
 190          // Add the discussion.
 191          $record->id = forum_add_discussion($record, null, null, $record->userid);
 192  
 193          return $record;
 194      }
 195  
 196      /**
 197       * Function to create a dummy post.
 198       *
 199       * @param array|stdClass $record
 200       * @return stdClass the post object
 201       */
 202      public function create_post($record = null) {
 203          global $DB;
 204  
 205          // Increment the forum post count.
 206          $this->forumpostcount++;
 207  
 208          // Variable to store time.
 209          $time = time() + $this->forumpostcount;
 210  
 211          $record = (array) $record;
 212  
 213          if (!isset($record['discussion'])) {
 214              throw new coding_exception('discussion must be present in phpunit_util::create_post() $record');
 215          }
 216  
 217          if (!isset($record['userid'])) {
 218              throw new coding_exception('userid must be present in phpunit_util::create_post() $record');
 219          }
 220  
 221          if (!isset($record['parent'])) {
 222              $record['parent'] = 0;
 223          }
 224  
 225          if (!isset($record['subject'])) {
 226              $record['subject'] = 'Forum post subject ' . $this->forumpostcount;
 227          }
 228  
 229          if (!isset($record['message'])) {
 230              $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount);
 231          }
 232  
 233          if (!isset($record['created'])) {
 234              $record['created'] = $time;
 235          }
 236  
 237          if (!isset($record['modified'])) {
 238              $record['modified'] = $time;
 239          }
 240  
 241          if (!isset($record['mailed'])) {
 242              $record['mailed'] = 0;
 243          }
 244  
 245          if (!isset($record['messageformat'])) {
 246              $record['messageformat'] = 0;
 247          }
 248  
 249          if (!isset($record['messagetrust'])) {
 250              $record['messagetrust'] = 0;
 251          }
 252  
 253          if (!isset($record['attachment'])) {
 254              $record['attachment'] = "";
 255          }
 256  
 257          if (!isset($record['totalscore'])) {
 258              $record['totalscore'] = 0;
 259          }
 260  
 261          if (!isset($record['mailnow'])) {
 262              $record['mailnow'] = 0;
 263          }
 264  
 265          $record = (object) $record;
 266  
 267          // Add the post.
 268          $record->id = $DB->insert_record('forum_posts', $record);
 269  
 270          // Update the last post.
 271          forum_discussion_update_last_post($record->discussion);
 272  
 273          return $record;
 274      }
 275  
 276      public function create_content($instance, $record = array()) {
 277          global $USER, $DB;
 278          $record = (array)$record + array(
 279              'forum' => $instance->id,
 280              'userid' => $USER->id,
 281              'course' => $instance->course
 282          );
 283          if (empty($record['discussion']) && empty($record['parent'])) {
 284              // Create discussion.
 285              $discussion = $this->create_discussion($record);
 286              $post = $DB->get_record('forum_posts', array('id' => $discussion->firstpost));
 287          } else {
 288              // Create post.
 289              if (empty($record['parent'])) {
 290                  $record['parent'] = $DB->get_field('forum_discussions', 'firstpost', array('id' => $record['discussion']), MUST_EXIST);
 291              } else if (empty($record['discussion'])) {
 292                  $record['discussion'] = $DB->get_field('forum_posts', 'discussion', array('id' => $record['parent']), MUST_EXIST);
 293              }
 294              $post = $this->create_post($record);
 295          }
 296          return $post;
 297      }
 298  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1