Merge branch 'MDL-30102_master' of git://github.com/markn86/moodle
authorDamyon Wiese <damyon@moodle.com>
Tue, 5 Feb 2013 03:23:01 +0000 (11:23 +0800)
committerDamyon Wiese <damyon@moodle.com>
Tue, 5 Feb 2013 03:23:01 +0000 (11:23 +0800)
lib/testing/generator/data_generator.php
lib/testing/tests/generator_test.php
mod/forum/db/services.php
mod/forum/externallib.php
mod/forum/lib.php
mod/forum/tests/externallib_test.php
mod/forum/version.php

index b6927ab..7ab11b1 100644 (file)
@@ -41,6 +41,16 @@ class testing_data_generator {
     protected $groupcount = 0;
     protected $groupingcount = 0;
 
+    /**
+     * @var int keep track of how many forum discussions have been created.
+     */
+    protected $forumdiscussioncount = 0;
+
+    /**
+     * @var int keep track of how many forum posts have been created.
+     */
+    protected $forumpostcount = 0;
+
     /** @var array list of plugin generators */
     protected $generators = array();
 
@@ -82,6 +92,8 @@ EOD;
         $this->categorycount = 0;
         $this->coursecount = 0;
         $this->scalecount = 0;
+        $this->forumdiscussioncount = 0;
+        $this->forumpostcount = 0;
 
         foreach ($this->generators as $generator) {
             $generator->reset();
@@ -655,6 +667,136 @@ EOD;
 
         return true;
     }
+
+    /**
+     * Function to create a dummy discussion.
+     *
+     * @param array|stdClass $record
+     * @return stdClass the discussion object
+     */
+    public function create_forum_discussion($record = null) {
+        global $DB;
+
+        // Increment the forum discussion count.
+        $this->forumdiscussioncount++;
+
+        $record = (array) $record;
+
+        if (!isset($record['course'])) {
+            throw new coding_exception('course must be present in phpunit_util::create_forum_discussion() $record');
+        }
+
+        if (!isset($record['forum'])) {
+            throw new coding_exception('forum must be present in phpunit_util::create_forum_discussion() $record');
+        }
+
+        if (!isset($record['userid'])) {
+            throw new coding_exception('userid must be present in phpunit_util::create_forum_discussion() $record');
+        }
+
+        if (!isset($record['name'])) {
+            $record['name'] = "Discussion " . $this->forumdiscussioncount;
+        }
+
+        if (!isset($record['subject'])) {
+            $record['subject'] = "Subject for discussion " . $this->forumdiscussioncount;
+        }
+
+        if (!isset($record['message'])) {
+            $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->forumdiscussioncount);
+        }
+
+        if (!isset($record['messageformat'])) {
+            $record['messageformat'] = editors_get_preferred_format();
+        }
+
+        if (!isset($record['messagetrust'])) {
+            $record['messagetrust'] = "";
+        }
+
+        if (!isset($record['assessed'])) {
+            $record['assessed'] = '1';
+        }
+
+        if (!isset($record['groupid'])) {
+            $record['groupid'] = "-1";
+        }
+
+        if (!isset($record['timestart'])) {
+            $record['timestart'] = "0";
+        }
+
+        if (!isset($record['timeend'])) {
+            $record['timeend'] = "0";
+        }
+
+        if (!isset($record['mailnow'])) {
+            $record['mailnow'] = "0";
+        }
+
+        $record = (object) $record;
+
+        // Add the discussion.
+        $record->id = forum_add_discussion($record, null, null, $record->userid);
+
+        return $record;
+    }
+
+    /**
+     * Function to create a dummy post.
+     *
+     * @param array|stdClass $record
+     * @return stdClass the post object
+     */
+    public function create_forum_post($record = null) {
+        global $DB;
+
+        // Increment the forum post count.
+        $this->forumpostcount++;
+
+        // Variable to store time.
+        $time = time() + $this->forumpostcount;
+
+        $record = (array) $record;
+
+        if (!isset($record['discussion'])) {
+            throw new coding_exception('discussion must be present in phpunit_util::create_forum_post() $record');
+        }
+
+        if (!isset($record['userid'])) {
+            throw new coding_exception('userid must be present in phpunit_util::create_forum_post() $record');
+        }
+
+        if (!isset($record['parent'])) {
+            $record['parent'] = 0;
+        }
+
+        if (!isset($record['subject'])) {
+            $record['subject'] = 'Forum post subject ' . $this->forumpostcount;
+        }
+
+        if (!isset($record['message'])) {
+            $record['message'] = html_writer::tag('p', 'Forum message post ' . $this->forumpostcount);
+        }
+
+        if (!isset($record['created'])) {
+            $record['created'] = $time;
+        }
+
+        if (!isset($record['modified'])) {
+            $record['modified'] = $time;
+        }
+
+        $record = (object) $record;
+
+        // Add the post.
+        $record->id = $DB->insert_record('forum_posts', $record);
+
+        // Update the last post.
+        forum_discussion_update_last_post($record->discussion);
+
+        return $record;
+    }
 }
 
 /**
index 19acca6..a5c451a 100644 (file)
@@ -203,4 +203,80 @@ class core_test_generator_testcase extends advanced_testcase {
         $this->assertFalse($result);
 
     }
+
+    /**
+     * Test create_forum_discussion.
+     */
+    public function test_create_forum_discussion() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        // User that will create the forum.
+        $user = self::getDataGenerator()->create_user();
+
+        // Create course to add the forum to.
+        $course = self::getDataGenerator()->create_course();
+
+        // The forum.
+        $record = new stdClass();
+        $record->course = $course->id;
+        $forum = self::getDataGenerator()->create_module('forum', $record);
+
+        // Add a few discussions.
+        $record = array();
+        $record['course'] = $course->id;
+        $record['forum'] = $forum->id;
+        $record['userid'] = $user->id;
+        self::getDataGenerator()->create_forum_discussion($record);
+        self::getDataGenerator()->create_forum_discussion($record);
+        self::getDataGenerator()->create_forum_discussion($record);
+
+        // Check the discussions were correctly created.
+        $this->assertEquals(3, $DB->count_records_select('forum_discussions', 'forum = :forum',
+                array('forum' => $forum->id)));
+    }
+
+    /**
+     * Test create_forum_post.
+     */
+    public function test_create_forum_post() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        // Create a bunch of users
+        $user1 = self::getDataGenerator()->create_user();
+        $user2 = self::getDataGenerator()->create_user();
+        $user3 = self::getDataGenerator()->create_user();
+        $user4 = self::getDataGenerator()->create_user();
+
+        // Create course to add the forum.
+        $course = self::getDataGenerator()->create_course();
+
+        // The forum.
+        $record = new stdClass();
+        $record->course = $course->id;
+        $forum = self::getDataGenerator()->create_module('forum', $record);
+
+        // Add a discussion.
+        $record->forum = $forum->id;
+        $record->userid = $user1->id;
+        $discussion = self::getDataGenerator()->create_forum_discussion($record);
+
+        // Add a bunch of replies, changing the userid.
+        $record = new stdClass();
+        $record->discussion = $discussion->id;
+        $record->userid = $user2->id;
+        self::getDataGenerator()->create_forum_post($record);
+        $record->userid = $user3->id;
+        self::getDataGenerator()->create_forum_post($record);
+        $record->userid = $user4->id;
+        self::getDataGenerator()->create_forum_post($record);
+
+        // Check the posts were correctly created, remember, when creating a discussion a post
+        // is generated as well, so we should have 4 posts, not 3.
+        $this->assertEquals(4, $DB->count_records_select('forum_posts', 'discussion = :discussion',
+                array('discussion' => $discussion->id)));
+    }
 }
index 7c7535f..29d666b 100644 (file)
@@ -34,5 +34,14 @@ $functions = array(
             returned.',
         'type' => 'read',
         'capabilities' => 'mod/forum:viewdiscussion'
+    ),
+
+    'mod_forum_get_forum_discussions' => array(
+        'classname' => 'mod_forum_external',
+        'methodname' => 'get_forum_discussions',
+        'classpath' => 'mod/forum/externallib.php',
+        'description' => 'Returns a list of forum discussions contained within a given set of forums.',
+        'type' => 'read',
+        'capabilities' => 'mod/forum:viewdiscussion, mod/forum:viewqandawithoutposting'
     )
 );
index 8e7f133..955bd62 100644 (file)
@@ -30,7 +30,7 @@ require_once("$CFG->libdir/externallib.php");
 class mod_forum_external extends external_api {
 
     /**
-     * Describes the parameters for get_forum
+     * Describes the parameters for get_forum.
      *
      * @return external_external_function_parameters
      * @since Moodle 2.5
@@ -39,7 +39,7 @@ class mod_forum_external extends external_api {
         return new external_function_parameters (
             array(
                 'courseids' => new external_multiple_structure(new external_value(PARAM_INT, 'course ID',
-                        '', VALUE_REQUIRED, '', NULL_NOT_ALLOWED), 'Array of course IDs', VALUE_DEFAULT, array()),
+                        '', VALUE_REQUIRED, '', NULL_NOT_ALLOWED), 'Array of Course IDs', VALUE_DEFAULT, array()),
             )
         );
     }
@@ -58,11 +58,12 @@ class mod_forum_external extends external_api {
 
         require_once($CFG->dirroot . "/mod/forum/lib.php");
 
-        if (empty($courseids)) {
+        $params = self::validate_parameters(self::get_forums_by_courses_parameters(), array('courseids' => $courseids));
+
+        if (empty($params['courseids'])) {
             // Get all the courses the user can view.
             $courseids = array_keys(enrol_get_my_courses());
         } else {
-            $params = self::validate_parameters(self::get_forums_by_courses_parameters(), array('courseids' => $courseids));
             $courseids = $params['courseids'];
         }
 
@@ -77,16 +78,14 @@ class mod_forum_external extends external_api {
                 $context = context_course::instance($cid);
                 // Check the user can function in this context.
                 self::validate_context($context);
-                // Get the forums in this course
+                // Get the forums in this course.
                 if ($forums = $DB->get_records('forum', array('course' => $cid))) {
                     // Get the modinfo for the course.
                     $modinfo = get_fast_modinfo($cid);
-                    // If modinfo returns no forum instances, set it to empty array.
-                    if (!isset($modinfo->instances['forum'])) {
-                        $modinfo->instances['forum'] = array();
-                    }
-                    // Loop through the courses returned by modinfo.
-                    foreach ($modinfo->instances['forum'] as $forumid => $cm) {
+                    // Get the forum instances.
+                    $foruminstances = $modinfo->get_instances_of('forum');
+                    // Loop through the forums returned by modinfo.
+                    foreach ($foruminstances as $forumid => $cm) {
                         // If it is not visible or present in the forums get_records call, continue.
                         if (!$cm->uservisible || !isset($forums[$forumid])) {
                             continue;
@@ -113,7 +112,7 @@ class mod_forum_external extends external_api {
     }
 
     /**
-     * Describes the get_forum return value
+     * Describes the get_forum return value.
      *
      * @return external_single_structure
      * @since Moodle 2.5
@@ -150,4 +149,195 @@ class mod_forum_external extends external_api {
             )
         );
     }
+
+    /**
+     * Describes the parameters for get_forum_discussions.
+     *
+     * @return external_external_function_parameters
+     * @since Moodle 2.5
+     */
+    public static function get_forum_discussions_parameters() {
+        return new external_function_parameters (
+            array(
+                'forumids' => new external_multiple_structure(new external_value(PARAM_INT, 'forum ID',
+                        '', VALUE_REQUIRED, '', NULL_NOT_ALLOWED), 'Array of Forum IDs', VALUE_REQUIRED),
+            )
+        );
+    }
+
+    /**
+     * Returns a list of forum discussions as well as a summary of the discussion
+     * in a provided list of forums.
+     *
+     * @param array $forumids the forum ids
+     * @return array the forum discussion details
+     * @since Moodle 2.5
+     */
+    public static function get_forum_discussions($forumids) {
+        global $CFG, $DB, $USER;
+
+        require_once($CFG->dirroot . "/mod/forum/lib.php");
+
+        // Validate the parameter.
+        $params = self::validate_parameters(self::get_forum_discussions_parameters(), array('forumids' => $forumids));
+        $forumids = $params['forumids'];
+
+        // Array to store the forum discussions to return.
+        $arrdiscussions = array();
+        // Keep track of the course ids we have performed a require_course_login check on to avoid repeating.
+        $arrcourseschecked = array();
+        // Store the modinfo for the forums in an individual courses.
+        $arrcoursesforuminfo = array();
+        // Keep track of the users we have looked up in the DB.
+        $arrusers = array();
+
+        // Loop through them.
+        foreach ($forumids as $id) {
+            // Get the forum object.
+            $forum = $DB->get_record('forum', array('id' => $id), '*', MUST_EXIST);
+            // Check that that user can view this course if check not performed yet.
+            if (!in_array($forum->course, $arrcourseschecked)) {
+                // Check the user can function in this context.
+                self::validate_context(context_course::instance($forum->course));
+                // Add to the array.
+                $arrcourseschecked[] = $forum->course;
+            }
+            // Get the modinfo for the course if we haven't already.
+            if (!isset($arrcoursesforuminfo[$forum->course])) {
+                $modinfo = get_fast_modinfo($forum->course);
+                $arrcoursesforuminfo[$forum->course] = $modinfo->get_instances_of('forum');
+            }
+            // Check if this forum does not exist in the modinfo array, should always be false unless DB is borked.
+            if (empty($arrcoursesforuminfo[$forum->course][$forum->id])) {
+                throw new moodle_exception('invalidmodule', 'error');
+            }
+            // We now have the course module.
+            $cm = $arrcoursesforuminfo[$forum->course][$forum->id];
+            // If the forum is not visible throw an exception.
+            if (!$cm->uservisible) {
+                throw new moodle_exception('nopermissiontoshow', 'error');
+            }
+            // Get the module context.
+            $modcontext = context_module::instance($cm->id);
+            // Check they have the view forum capability.
+            require_capability('mod/forum:viewdiscussion', $modcontext);
+            // Check if they can view full names.
+            $canviewfullname = has_capability('moodle/site:viewfullnames', $modcontext);
+            // Get the unreads array, this takes a forum id and returns data for all discussions.
+            $unreads = array();
+            if ($cantrack = forum_tp_can_track_forums($forum)) {
+                if ($forumtracked = forum_tp_is_tracked($forum)) {
+                    $unreads = forum_get_discussions_unread($cm);
+                }
+            }
+            // The forum function returns the replies for all the discussions in a given forum.
+            $replies = forum_count_discussion_replies($id);
+            // Get the discussions for this forum.
+            if ($discussions = $DB->get_records('forum_discussions', array('forum' => $id))) {
+                foreach ($discussions as $discussion) {
+                    // If the forum is of type qanda and the user has not posted in the discussion
+                    // we need to ensure that they have the required capability.
+                    if ($forum->type == 'qanda' && !forum_user_has_posted($discussion->forum, $discussion->id, $USER->id)) {
+                        require_capability('mod/forum:viewqandawithoutposting', $modcontext);
+                    }
+                    // If we don't have the users details then perform DB call.
+                    if (empty($arrusers[$discussion->userid])) {
+                        $arrusers[$discussion->userid] = $DB->get_record('user', array('id' => $discussion->userid),
+                            'firstname, lastname, email, picture, imagealt', MUST_EXIST);
+                    }
+                    // Get the subject.
+                    $subject = $DB->get_field('forum_posts', 'subject', array('id' => $discussion->firstpost), MUST_EXIST);
+                    // Create object to return.
+                    $return = new stdClass();
+                    $return->id = (int) $discussion->id;
+                    $return->course = $discussion->course;
+                    $return->forum = $discussion->forum;
+                    $return->name = $discussion->name;
+                    $return->userid = $discussion->userid;
+                    $return->groupid = $discussion->groupid;
+                    $return->assessed = $discussion->assessed;
+                    $return->timemodified = (int) $discussion->timemodified;
+                    $return->usermodified = $discussion->usermodified;
+                    $return->timestart = $discussion->timestart;
+                    $return->timeend = $discussion->timeend;
+                    $return->firstpost = (int) $discussion->firstpost;
+                    $return->firstuserfullname = fullname($arrusers[$discussion->userid], $canviewfullname);
+                    $return->firstuserimagealt = $arrusers[$discussion->userid]->imagealt;
+                    $return->firstuserpicture = $arrusers[$discussion->userid]->picture;
+                    $return->firstuseremail = $arrusers[$discussion->userid]->email;
+                    $return->subject = $subject;
+                    $return->numunread = '';
+                    if ($cantrack && $forumtracked) {
+                        if (isset($unreads[$discussion->id])) {
+                            $return->numunread = (int) $unreads[$discussion->id];
+                        }
+                    }
+                    // Check if there are any replies to this discussion.
+                    if (!empty($replies[$discussion->id])) {
+                         $return->numreplies = (int) $replies[$discussion->id]->replies;
+                         $return->lastpost = (int) $replies[$discussion->id]->lastpostid;
+                     } else { // No replies, so the last post will be the first post.
+                        $return->numreplies = 0;
+                        $return->lastpost = (int) $discussion->firstpost;
+                     }
+                    // Get the last post as well as the user who made it.
+                    $lastpost = $DB->get_record('forum_posts', array('id' => $return->lastpost), '*', MUST_EXIST);
+                    if (empty($arrusers[$lastpost->userid])) {
+                        $arrusers[$lastpost->userid] = $DB->get_record('user', array('id' => $lastpost->userid),
+                            'firstname, lastname, email, picture, imagealt', MUST_EXIST);
+                    }
+                    $return->lastuserid = $lastpost->userid;
+                    $return->lastuserfullname = fullname($arrusers[$lastpost->userid], $canviewfullname);
+                    $return->lastuserimagealt = $arrusers[$lastpost->userid]->imagealt;
+                    $return->lastuserpicture = $arrusers[$lastpost->userid]->picture;
+                    $return->lastuseremail = $arrusers[$lastpost->userid]->email;
+                    // Add the discussion statistics to the array to return.
+                    $arrdiscussions[$return->id] = (array) $return;
+                }
+            }
+        }
+
+        return $arrdiscussions;
+    }
+
+    /**
+     * Describes the get_forum_discussions return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 2.5
+     */
+     public static function get_forum_discussions_returns() {
+        return new external_multiple_structure(
+            new external_single_structure(
+                array(
+                    'id' => new external_value(PARAM_INT, 'Forum id'),
+                    'course' => new external_value(PARAM_INT, 'Course id'),
+                    'forum' => new external_value(PARAM_INT, 'The forum id'),
+                    'name' => new external_value(PARAM_TEXT, 'Discussion name'),
+                    'userid' => new external_value(PARAM_INT, 'User id'),
+                    'groupid' => new external_value(PARAM_INT, 'Group id'),
+                    'assessed' => new external_value(PARAM_INT, 'Is this assessed?'),
+                    'timemodified' => new external_value(PARAM_INT, 'Time modified'),
+                    'usermodified' => new external_value(PARAM_INT, 'The id of the user who last modified'),
+                    'timestart' => new external_value(PARAM_INT, 'Time discussion can start'),
+                    'timeend' => new external_value(PARAM_INT, 'Time discussion ends'),
+                    'firstpost' => new external_value(PARAM_INT, 'The first post in the discussion'),
+                    'firstuserfullname' => new external_value(PARAM_TEXT, 'The discussion creators fullname'),
+                    'firstuserimagealt' => new external_value(PARAM_TEXT, 'The discussion creators image alt'),
+                    'firstuserpicture' => new external_value(PARAM_INT, 'The discussion creators profile picture'),
+                    'firstuseremail' => new external_value(PARAM_TEXT, 'The discussion creators email'),
+                    'subject' => new external_value(PARAM_TEXT, 'The discussion subject'),
+                    'numreplies' => new external_value(PARAM_TEXT, 'The number of replies in the discussion'),
+                    'numunread' => new external_value(PARAM_TEXT, 'The number of unread posts, blank if this value is
+                        not available due to forum settings.'),
+                    'lastpost' => new external_value(PARAM_INT, 'The id of the last post in the discussion'),
+                    'lastuserid' => new external_value(PARAM_INT, 'The id of the user who made the last post'),
+                    'lastuserfullname' => new external_value(PARAM_TEXT, 'The last person to posts fullname'),
+                    'lastuserimagealt' => new external_value(PARAM_TEXT, 'The last person to posts image alt'),
+                    'lastuserpicture' => new external_value(PARAM_INT, 'The last person to posts profile picture'),
+                    'lastuseremail' => new external_value(PARAM_TEXT, 'The last person to posts email'),
+                ), 'discussion'
+            )
+        );
+    }
 }
index 5d56afb..f8f0fea 100644 (file)
@@ -4266,10 +4266,10 @@ function forum_pluginfile($course, $cm, $context, $filearea, $args, $forcedownlo
  * @param object $forum
  * @param object $cm
  * @param mixed $mform
- * @param string $message
+ * @param string $unused
  * @return bool
  */
-function forum_add_attachment($post, $forum, $cm, $mform=null, &$message=null) {
+function forum_add_attachment($post, $forum, $cm, $mform=null, $unused=null) {
     global $DB;
 
     if (empty($mform)) {
@@ -4389,16 +4389,13 @@ function forum_update_post($post, $mform, &$message) {
  * Given an object containing all the necessary data,
  * create a new discussion and return the id
  *
- * @global object
- * @global object
- * @global object
  * @param object $post
  * @param mixed $mform
- * @param string $message
+ * @param string $unused
  * @param int $userid
  * @return object
  */
-function forum_add_discussion($discussion, $mform=null, &$message=null, $userid=null) {
+function forum_add_discussion($discussion, $mform=null, $unused=null, $userid=null) {
     global $USER, $CFG, $DB;
 
     $timenow = time();
@@ -4452,7 +4449,7 @@ function forum_add_discussion($discussion, $mform=null, &$message=null, $userid=
     $DB->set_field("forum_posts", "discussion", $post->discussion, array("id"=>$post->id));
 
     if (!empty($cm->id)) {
-        forum_add_attachment($post, $forum, $cm, $mform, $message);
+        forum_add_attachment($post, $forum, $cm, $mform, $unused);
     }
 
     if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
index c2ad615..fd04396 100644 (file)
@@ -114,13 +114,13 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
 
         // Call the external function passing course ids.
         $forums = mod_forum_external::get_forums_by_courses(array($course1->id, $course2->id));
-        $this->assertEquals($forums, $expectedforums);
         external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
+        $this->assertEquals($expectedforums, $forums);
 
         // Call the external function without passing course id.
         $forums = mod_forum_external::get_forums_by_courses();
-        $this->assertEquals($forums, $expectedforums);
         external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
+        $this->assertEquals($expectedforums, $forums);
 
         // Unenrol user from second course and alter expected forums.
         $enrol->unenrol_user($instance2, $user->id);
@@ -128,16 +128,282 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
 
         // Call the external function without passing course id.
         $forums = mod_forum_external::get_forums_by_courses();
-        $this->assertEquals($forums, $expectedforums);
         external_api::clean_returnvalue(mod_forum_external::get_forums_by_courses_returns(), $forums);
+        $this->assertEquals($expectedforums, $forums);
 
-        // Call for the second course we unenrolled the user from, make sure exception thrown.
-        $this->setExpectedException('require_login_exception');
-        mod_forum_external::get_forums_by_courses(array($course2->id));
+        // Call for the second course we unenrolled the user from, ensure exception thrown.
+        try {
+            mod_forum_external::get_forums_by_courses(array($course2->id));
+            $this->fail('Exception expected due to being unenrolled from the course.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('requireloginerror', $e->errorcode);
+        }
+
+        // Call without required capability, ensure exception thrown.
+        $this->unassignUserCapability('mod/forum:viewdiscussion', $context1->id, $roleid1);
+        try {
+            mod_forum_external::get_forums_by_courses(array($course1->id));
+            $this->fail('Exception expected due to missing capability.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('nopermissions', $e->errorcode);
+        }
+    }
+
+    /**
+     * Test get forum discussions
+     */
+    public function test_mod_forum_get_forum_discussions() {
+        global $USER, $CFG, $DB;
+
+        $this->resetAfterTest(true);
+
+        // Set the CFG variable to allow track forums.
+        $CFG->forum_trackreadposts = true;
+
+        // Create a user who can track forums.
+        $record = new stdClass();
+        $record->trackforums = true;
+        $user1 = self::getDataGenerator()->create_user($record);
+        // Create a bunch of other users to post.
+        $user2 = self::getDataGenerator()->create_user();
+        $user3 = self::getDataGenerator()->create_user();
+        $user4 = self::getDataGenerator()->create_user();
+
+        // Set the first created user to the test user.
+        self::setUser($user1);
+
+        // Create courses to add the modules.
+        $course1 = self::getDataGenerator()->create_course();
+        $course2 = self::getDataGenerator()->create_course();
+
+        // First forum with tracking off.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->trackingtype = FORUM_TRACKING_OFF;
+        $forum1 = self::getDataGenerator()->create_module('forum', $record);
+
+        // Second forum of type 'qanda' with tracking enabled.
+        $record = new stdClass();
+        $record->course = $course2->id;
+        $record->type = 'qanda';
+        $record->trackingtype = FORUM_TRACKING_ON;
+        $forum2 = self::getDataGenerator()->create_module('forum', $record);
+
+        // Third forum where we will only have one discussion with no replies.
+        $record = new stdClass();
+        $record->course = $course2->id;
+        $forum3 = self::getDataGenerator()->create_module('forum', $record);
+
+        // Add discussions to the forums.
+        $record = new stdClass();
+        $record->course = $course1->id;
+        $record->userid = $user1->id;
+        $record->forum = $forum1->id;
+        $discussion1 = self::getDataGenerator()->create_forum_discussion($record);
+
+        $record = new stdClass();
+        $record->course = $course2->id;
+        $record->userid = $user2->id;
+        $record->forum = $forum2->id;
+        $discussion2 = self::getDataGenerator()->create_forum_discussion($record);
+
+        $record = new stdClass();
+        $record->course = $course2->id;
+        $record->userid = $user2->id;
+        $record->forum = $forum3->id;
+        $discussion3 = self::getDataGenerator()->create_forum_discussion($record);
+
+        // Add three replies to the discussion 1 from different users.
+        $record = new stdClass();
+        $record->discussion = $discussion1->id;
+        $record->parent = $discussion1->firstpost;
+        $record->userid = $user2->id;
+        $discussion1reply1 = self::getDataGenerator()->create_forum_post($record);
 
-        // Call without required capability.
-        $this->unassignUserCapability('mod/forum:viewdiscussion', $context->id, $roleid1);
-        $this->setExpectedException('required_capability_exception');
-        mod_forum_external::get_forums_by_courses(array($course1->id));
+        $record->parent = $discussion1reply1->id;
+        $record->userid = $user3->id;
+        $discussion1reply2 = self::getDataGenerator()->create_forum_post($record);
+
+        $record->userid = $user4->id;
+        $discussion1reply3 = self::getDataGenerator()->create_forum_post($record);
+
+        // Add two replies to discussion 2 from different users.
+        $record = new stdClass();
+        $record->discussion = $discussion2->id;
+        $record->parent = $discussion2->firstpost;
+        $record->userid = $user1->id;
+        $discussion2reply1 = self::getDataGenerator()->create_forum_post($record);
+
+        $record->parent = $discussion2reply1->id;
+        $record->userid = $user3->id;
+        $discussion2reply2 = self::getDataGenerator()->create_forum_post($record);
+
+        // Check the forums were correctly created.
+        $this->assertEquals(3, $DB->count_records_select('forum', 'id = :forum1 OR id = :forum2 OR id = :forum3',
+                array('forum1' => $forum1->id, 'forum2' => $forum2->id, 'forum3' => $forum3->id)));
+
+        // Check the discussions were correctly created.
+        $this->assertEquals(3, $DB->count_records_select('forum_discussions', 'forum = :forum1 OR forum = :forum2
+                OR id = :forum3', array('forum1' => $forum1->id, 'forum2' => $forum2->id, 'forum3' => $forum3->id)));
+
+        // Check the posts were correctly created, don't forget each discussion created also creates a post.
+        $this->assertEquals(7, $DB->count_records_select('forum_posts', 'discussion = :discussion1 OR discussion = :discussion2',
+                array('discussion1' => $discussion1->id, 'discussion2' => $discussion2->id)));
+
+        // Enrol the user in the first course.
+        $enrol = enrol_get_plugin('manual');
+        $enrolinstances = enrol_get_instances($course1->id, true);
+        foreach ($enrolinstances as $courseenrolinstance) {
+            if ($courseenrolinstance->enrol == "manual") {
+                $instance1 = $courseenrolinstance;
+                break;
+            }
+        }
+        $enrol->enrol_user($instance1, $user1->id);
+
+        // Now enrol into the second course.
+        $enrolinstances = enrol_get_instances($course2->id, true);
+        foreach ($enrolinstances as $courseenrolinstance) {
+            if ($courseenrolinstance->enrol == "manual") {
+                $instance2 = $courseenrolinstance;
+                break;
+            }
+        }
+        $enrol->enrol_user($instance2, $user1->id);
+
+        // Assign capabilities to view discussions for forum 1.
+        // Need to keep track of this context and role as we use it later in testing.
+        $cm = get_coursemodule_from_id('forum', $forum1->id, 0, false, MUST_EXIST);
+        $context1 = context_module::instance($cm->id);
+        $roleid1 = $this->assignUserCapability('mod/forum:viewdiscussion', $context1->id);
+
+        // Assign capabilities to view discussions for forum 2.
+        $cm = get_coursemodule_from_id('forum', $forum2->id, 0, false, MUST_EXIST);
+        $context = context_module::instance($cm->id);
+        $newrole = create_role('Role 2', 'role2', 'Role 2 description');
+        $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
+
+        // Assign capabilities to view discussions for forum 3.
+        $cm = get_coursemodule_from_id('forum', $forum3->id, 0, false, MUST_EXIST);
+        $context = context_module::instance($cm->id);
+        $this->assignUserCapability('mod/forum:viewdiscussion', $context->id, $newrole);
+
+        // Create what we expect to be returned when querying the forums.
+        $expecteddiscussions = array();
+        $expecteddiscussions[$discussion1->id] = array(
+                'id' => $discussion1->id,
+                'course' => $discussion1->course,
+                'forum' => $discussion1->forum,
+                'name' => $discussion1->name,
+                'firstpost' => $discussion1->firstpost,
+                'userid' => $discussion1->userid,
+                'groupid' => $discussion1->groupid,
+                'assessed' => $discussion1->assessed,
+                'timemodified' => $discussion1reply3->created,
+                'usermodified' => $discussion1reply3->userid,
+                'timestart' => $discussion1->timestart,
+                'timeend' => $discussion1->timeend,
+                'firstuserfullname' => fullname($user1),
+                'firstuserimagealt' => $user1->imagealt,
+                'firstuserpicture' => $user1->picture,
+                'firstuseremail' => $user1->email,
+                'subject' => $discussion1->name,
+                'numreplies' => 3,
+                'numunread' => '',
+                'lastpost' => $discussion1reply3->id,
+                'lastuserid' => $user4->id,
+                'lastuserfullname' => fullname($user4),
+                'lastuserimagealt' => $user4->imagealt,
+                'lastuserpicture' => $user4->picture,
+                'lastuseremail' => $user4->email
+            );
+        $expecteddiscussions[$discussion2->id] = array(
+                'id' => $discussion2->id,
+                'course' => $discussion2->course,
+                'forum' => $discussion2->forum,
+                'name' => $discussion2->name,
+                'firstpost' => $discussion2->firstpost,
+                'userid' => $discussion2->userid,
+                'groupid' => $discussion2->groupid,
+                'assessed' => $discussion2->assessed,
+                'timemodified' => $discussion2reply2->created,
+                'usermodified' => $discussion2reply2->userid,
+                'timestart' => $discussion2->timestart,
+                'timeend' => $discussion2->timeend,
+                'firstuserfullname' => fullname($user2),
+                'firstuserimagealt' => $user2->imagealt,
+                'firstuserpicture' => $user2->picture,
+                'firstuseremail' => $user2->email,
+                'subject' => $discussion2->name,
+                'numreplies' => 2,
+                'numunread' => 3,
+                'lastpost' => $discussion2reply2->id,
+                'lastuserid' => $user3->id,
+                'lastuserfullname' => fullname($user3),
+                'lastuserimagealt' => $user3->imagealt,
+                'lastuserpicture' => $user3->picture,
+                'lastuseremail' => $user3->email
+            );
+            $expecteddiscussions[$discussion3->id] = array(
+                'id' => $discussion3->id,
+                'course' => $discussion3->course,
+                'forum' => $discussion3->forum,
+                'name' => $discussion3->name,
+                'firstpost' => $discussion3->firstpost,
+                'userid' => $discussion3->userid,
+                'groupid' => $discussion3->groupid,
+                'assessed' => $discussion3->assessed,
+                'timemodified' => $discussion3->timemodified,
+                'usermodified' => $discussion3->usermodified,
+                'timestart' => $discussion3->timestart,
+                'timeend' => $discussion3->timeend,
+                'firstuserfullname' => fullname($user2),
+                'firstuserimagealt' => $user2->imagealt,
+                'firstuserpicture' => $user2->picture,
+                'firstuseremail' => $user2->email,
+                'subject' => $discussion3->name,
+                'numreplies' => 0,
+                'numunread' => 1,
+                'lastpost' => $discussion3->firstpost,
+                'lastuserid' => $user2->id,
+                'lastuserfullname' => fullname($user2),
+                'lastuserimagealt' => $user2->imagealt,
+                'lastuserpicture' => $user2->picture,
+                'lastuseremail' => $user2->email
+            );
+
+        // Call the external function passing forum ids.
+        $discussions = mod_forum_external::get_forum_discussions(array($forum1->id, $forum2->id, $forum3->id));
+        external_api::clean_returnvalue(mod_forum_external::get_forum_discussions_returns(), $discussions);
+        $this->assertEquals($expecteddiscussions, $discussions);
+
+        // Remove the users post from the qanda forum and ensure they can not return the discussion.
+        $DB->delete_records('forum_posts', array('id' => $discussion2reply1->id));
+        try {
+            mod_forum_external::get_forum_discussions(array($forum2->id));
+            $this->fail('Exception expected due to attempting to access qanda forum without posting.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('nopermissions', $e->errorcode);
+        }
+
+        // Call without required view discussion capability.
+        $this->unassignUserCapability('mod/forum:viewdiscussion', $context1->id, $roleid1);
+        try {
+            mod_forum_external::get_forum_discussions(array($forum1->id));
+            $this->fail('Exception expected due to missing capability.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('nopermissions', $e->errorcode);
+        }
+
+        // Unenrol user from second course.
+        $enrol->unenrol_user($instance2, $user1->id);
+
+        // Call for the second course we unenrolled the user from, make sure exception thrown.
+        try {
+            mod_forum_external::get_forum_discussions(array($forum2->id));
+            $this->fail('Exception expected due to being unenrolled from the course.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('requireloginerror', $e->errorcode);
+        }
     }
 }
index d43c1fa..e5da85b 100644 (file)
@@ -25,7 +25,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$module->version   = 2012112901;       // The current module version (Date: YYYYMMDDXX)
+$module->version   = 2012112902;       // The current module version (Date: YYYYMMDDXX)
 $module->requires  = 2012112900;    // Requires this Moodle version
 $module->component = 'mod_forum';      // Full name of the plugin (used for diagnostics)
 $module->cron      = 60;