public function test_oublog_get_participation_details()
 {
     global $SITE, $USER, $DB;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $now = time();
     // Whole course.
     // Single course for all subsequent tests?
     $course = $this->get_new_course();
     $oublog = $this->get_new_oublog($course->id);
     $oublog->allowcomments = OUBLOG_COMMENTS_ALLOW;
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $student1 = $this->get_new_user('student', $course->id);
     $student2 = $this->get_new_user('student', $course->id);
     // Number of posts and comments to create for whole course tests.
     $postcountwc = 3;
     $titlecheck = 'test_oublog_get_parts_wc';
     // Prepare to make some posts using the posts stub.
     $posthashes = array();
     for ($i = 1; $i <= $postcountwc; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title = 'Test Post ' . $titlecheck;
         // Add the posting student.
         $posthashes[$i]->userid = $student1->id;
     }
     // Create the posts - assumes oublog_add_post is working,
     // also add student comments to those posts.
     $postids = $commentids = array();
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         // Add the commenting student.
         $comment = $this->get_comment_stub($posthash->id, $student2->id);
         $comment->title .= " " . $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment);
     }
     // Get the participation object with counts of posts and comments.
     $curgroup = oublog_get_activity_group($cm);
     $curindividual = 0;
     $studentnamed = fullname($student1);
     $limitnum = 4;
     $start = $end = $page = $limitfrom = 0;
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test postscount && commentscount.
     // The same number of records should be 'discovered' that were added.
     $this->assertEquals($postcountwc, $participation->postscount);
     $this->assertEquals($postcountwc, $participation->commentscount);
     // This blog activity  allows comments.
     $this->assertEquals(OUBLOG_COMMENTS_ALLOW, $oublog->allowcomments);
     // The number of records should be 'returned' that were added.
     $this->assertCount($postcountwc, $participation->posts);
     $this->assertCount($postcountwc, $participation->comments);
     // The posts returned should match the ones added.
     foreach ($participation->posts as $post) {
         // Do some basic checks - does it match our test post created above.
         $this->assertInstanceOf('stdClass', $post);
         $this->assertEquals('Test Post ' . $titlecheck, $post->title);
         $this->assertEquals($student1->id, $post->userid);
         $this->assertEquals(0, $post->groupid);
         $postername = fullname($post);
         $this->assertEquals($studentnamed, $postername);
         $this->assertEquals($oublog->allowcomments, $post->allowcomments);
     }
     // Test comments object.
     foreach ($participation->comments as $comment) {
         // Same basic checks - does it match our test comment created above.
         $this->assertInstanceOf('stdClass', $comment);
         $this->assertEquals('Test Comment ' . $titlecheck, $comment->title);
         $this->assertEquals($oublog->id, $comment->oublogid);
         $this->assertEquals($post->title, $comment->posttitle);
         $this->assertEquals($post->userid, $comment->posterid);
         $this->assertEquals($post->userid, $comment->postauthorid);
         $this->assertEquals($post->groupid, $comment->groupid);
         $this->assertEquals($post->visibility, $comment->visibility);
         $this->assertEquals($studentnamed, $comment->posterfirstname . " " . $comment->posterlastname);
     }
     // Test for comments turned ON the blog.
     // But turned OFF on ONE post.
     $oublog->allowcomments = OUBLOG_COMMENTS_ALLOW;
     $getposts = false;
     // Dont need to see posts.
     $getcomments = true;
     // Do want to see comments.
     // Create additional post.
     $posttest = $this->get_post_stub($oublog->id);
     $posttest->title = 'Test Post ' . $titlecheck;
     // Add the posting student.
     $posttest->userid = $student1->id;
     // Add one post which doesnt allow comments
     $posttest->allowcomments = OUBLOG_COMMENTS_PREVENT;
     $postids[] = oublog_add_post($posttest, $cm, $oublog, $course);
     // Attemt to add a student comment, though not allowed.
     $comment3 = $this->get_comment_stub($posttest->id, $student2->id);
     $comment3->title .= " NOT ALLOWED " . $titlecheck;
     $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment3);
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // The number of posts that should be 'discovered' plus the new one.
     $this->assertEquals($postcountwc + 1, $participation->postscount);
     // The number of posts that should be 'returned'.
     $this->assertCount(0, $participation->posts);
     // This blog activity DOES allow comments.
     $this->assertEquals(OUBLOG_COMMENTS_ALLOW, $oublog->allowcomments);
     // The number of comments that should be 'discovered', does not not include the new one.
     $this->assertEquals($postcountwc, $participation->commentscount);
     // The number of comments to be 'returned' does not not include the new one.
     $this->assertCount($postcountwc, $participation->comments);
     // Separate groups.
     $oublog = $this->get_new_oublog($course->id, array('groupmode' => SEPARATEGROUPS));
     $group1 = $this->get_new_group($course->id);
     $group2 = $this->get_new_group($course->id);
     $this->get_new_group_member($group1->id, $student1->id);
     $this->get_new_group_member($group2->id, $student2->id);
     $postcountsg = 3;
     $titlecheck = ' test_oublog_get_sg';
     // Make some posts to use from post stub.
     $posthashes = array();
     for ($i = 1; $i <= $postcountsg; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title = $titlecheck;
         // Add the posting student.
         $posthashes[$i]->userid = $student1->id;
         $posthashes[$i]->groupid = $group1->id;
     }
     // Create the posts assuming oublog_add_post is working.
     $postids = array();
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         // Not adding comments for this test.
     }
     $curgroup = 0;
     // All groups.
     $curindividual = 0;
     $getposts = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // The number of posts that should be 'discovered' plus the new one.
     $this->assertEquals($postcountsg, $participation->postscount);
     // The number of posts that should be 'returned'.
     $this->assertCount($postcountsg, $participation->posts);
     // The number of comments should be 'discovered'.
     $this->assertEquals(0, $participation->commentscount);
     // The number of comments should be 'returned'.
     $this->assertCount(0, $participation->comments);
     // Test posts object. Only Group ONE
     foreach ($participation->posts as $post) {
         // Basic checks.
         $this->assertInstanceOf('stdClass', $post);
         $this->assertEquals($group1->id, $post->groupid);
         $this->assertEquals($student1->id, $post->userid);
         $postername = fullname($post);
         $this->assertEquals($postername, $post->firstname . " " . $post->lastname);
     }
     // Prepare some more post stubs to use.
     $posthashes = array();
     for ($i = 1; $i <= $postcountsg; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title = $titlecheck;
         // Add the posting student.
         $posthashes[$i]->userid = $student2->id;
         $posthashes[$i]->groupid = $group2->id;
     }
     // Creating the posts, and add comments.
     $postids = array();
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         // Add the commenting student.
         $comment = $this->get_comment_stub($posthash->id, $student2->id);
         $comment->title .= $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment);
     }
     // Test posts object. ONLY Group TWO.
     $curgroup = $group2->id;
     $curindividual = 0;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     $this->assertEquals($postcountsg, $participation->postscount);
     // No: of comments made same as posts.
     $this->assertEquals($postcountsg, $participation->commentscount);
     // Test posts object. ONLY Group TWO.
     foreach ($participation->posts as $post) {
         // Do some basic checks.
         $this->assertInstanceOf('stdClass', $post);
         $this->assertEquals($group2->id, $post->groupid);
         $this->assertEquals($student2->id, $post->userid);
     }
     // Test comments object.
     foreach ($participation->comments as $comment) {
         // Do some basic checks - does it match our test post created above?.
         $this->assertInstanceOf('stdClass', $comment);
         $this->assertEquals('Test Comment' . $titlecheck, $comment->title);
         $this->assertEquals($oublog->id, $comment->oublogid);
         $this->assertEquals($post->title, $comment->posttitle);
         $this->assertEquals($post->userid, $comment->posterid);
     }
     // Separate groups, separate individuals.
     $oublog = $this->get_new_oublog($course->id, array('groupmode' => SEPARATEGROUPS, 'individual' => OUBLOG_SEPARATE_INDIVIDUAL_BLOGS));
     $group5 = $this->get_new_group($course->id);
     $group6 = $this->get_new_group($course->id);
     $this->get_new_group_member($group5->id, $student1->id);
     $this->get_new_group_member($group6->id, $student2->id);
     // Number of posts/comments for these tests.
     $postcountsgsi = 2;
     $titlecheck = ' testing_oublog_sgsi_get_posts';
     // Some post stubs to use in group 5.
     $posthashes = array();
     for ($i = 1; $i <= $postcountsgsi; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title = $titlecheck;
         // Add the posting student.
         $posthashes[$i]->userid = $student1->id;
     }
     // Create group 5's posts and comments.
     $postids = array();
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         $comment = $this->get_comment_stub($posthash->id, $student1->id);
         $comment->title .= $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment);
     }
     // Make sure we have some post stubs to use in group 6.
     $posthashes = array();
     for ($i = 1; $i <= $postcountsgsi; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title = $titlecheck;
         $posthashes[$i]->userid = $student2->id;
     }
     // Create group 6s posts and comments.
     $postids = array();
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         $comment = $this->get_comment_stub($posthash->id, $student2->id);
         $comment->title .= $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment);
     }
     // Test no groups.
     $curgroup = 0;
     $curindividual = 0;
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // The number of posts that should be 'discovered', from both groups.
     $this->assertEquals($postcountsgsi * 2, $participation->postscount);
     // The number of posts that should be 'returned'.
     $this->assertCount($postcountsgsi * 2, $participation->posts);
     // The number of comments should be 'discovered', from both groups.
     $this->assertEquals($postcountsgsi * 2, $participation->commentscount);
     // The number of comments should be 'returned'.
     $this->assertCount($postcountsgsi * 2, $participation->comments);
     // Test of student in group 6.
     $curgroup = $group6->id;
     $curindividual = $student2->id;
     $getposts = false;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // The number of one students posts that should be 'discovered' from group.
     $this->assertEquals($postcountsgsi, $participation->postscount);
     // There are posts but should not be 'returned'.
     $this->assertCount(0, $participation->posts);
     // The number of comments that should be 'discovered', from both groups.
     $this->assertEquals($postcountsgsi, $participation->commentscount);
     // The number of comments that should be 'returned'.
     $this->assertCount($postcountsgsi, $participation->comments);
     // Test for student not in group 6.
     $curgroup = $group6->id;
     $curindividual = $student1->id;
     $getposts = false;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Student not in the group could make no posts or comments
     $this->assertEquals(0, $participation->postscount);
     $this->assertCount(0, $participation->posts);
     $this->assertEquals(0, $participation->commentscount);
     $this->assertCount(0, $participation->comments);
     // Separate individuals.
     $oublog = $this->get_new_oublog($course->id, array('individual' => OUBLOG_SEPARATE_INDIVIDUAL_BLOGS));
     // Number of posts/comments for these tests.
     $postcountsi = 2;
     $titlecheck = 'oublog_si_tests';
     // Creating some post stubs for student 1 to use.
     $posthashes = array();
     for ($i = 1; $i <= $postcountsi; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title .= $titlecheck;
         $posthashes[$i]->userid = $student1->id;
     }
     // Create student 1s posts and comments.
     $postids = array();
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         $comment1 = $this->get_comment_stub($posthash->id, $student1->id);
         $comment1->title .= $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment1);
         $comment2 = $this->get_comment_stub($posthash->id, $student2->id);
         $comment2->title .= $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment2);
     }
     // Create student 2s posts and comments.
     for ($i = 1; $i <= $postcountsi; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title = $titlecheck;
         $posthashes[$i]->userid = $student2->id;
     }
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         $comment1 = $this->get_comment_stub($posthash->id, $student2->id);
         $comment1->title .= $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment1);
         $comment2 = $this->get_comment_stub($posthash->id, $student2->id);
         $comment2->title .= $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment2);
     }
     // Test All individuals.
     $curgroup = 0;
     $curindividual = 0;
     $oublog->individual = OUBLOG_SEPARATE_INDIVIDUAL_BLOGS;
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test count of posts by both students.
     $this->assertEquals($postcountsi * 2, $participation->postscount);
     // The number of student posts should be 'returned' that were added.
     $this->assertCount($postcountsi * 2, $participation->posts);
     // Test count of all students comments.
     $this->assertEquals($postcountsi * 4, $participation->commentscount);
     // The number of comments which should be 'returned'.
     $this->assertCount($postcountsi * 2, $participation->comments);
     // Test an individual.
     $curgroup = 0;
     $curindividual = $student2->id;
     $studentnamed = fullname($student2);
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test count of posts by both students.
     $this->assertEquals($postcountsi, $participation->postscount);
     // The number of student posts should be 'returned' that were added.
     $this->assertCount($postcountsi, $participation->posts);
     // Test count of all students comments.
     $this->assertEquals($postcountsi * 2, $participation->commentscount);
     // The number of comments which should be 'returned'.
     $this->assertCount($postcountsi * 2, $participation->comments);
     // The posts returned should match the ones added.
     foreach ($participation->posts as $post) {
         // Do some basic checks - does it match our test post created above.
         $this->assertInstanceOf('stdClass', $post);
         $this->assertEquals($titlecheck, $post->title);
         $this->assertEquals($student2->id, $post->userid);
         $this->assertEquals(0, $post->groupid);
         $postername = fullname($post);
         $this->assertEquals($studentnamed, $postername);
     }
     // Global blog = Personal blog.
     if (!($oublog = $DB->get_record('oublog', array('course' => $SITE->id, 'global' => 1)))) {
         $oublog = $this->get_new_oublog($SITE->id, array('global' => 1, 'individual' => OUBLOG_NO_INDIVIDUAL_BLOGS, 'allowcomments' => OUBLOG_COMMENTS_ALLOWPUBLIC, 'maxvisibility' => OUBLOG_VISIBILITY_PUBLIC));
     }
     $cm = get_coursemodule_from_instance('oublog', $oublog->id);
     // Number of posts and comments to create for Global course tests.
     $postcountpb = 2;
     $titlecheck = 'PERSBLOG_parts';
     // Prepare to make some posts to use for test 1.
     $posthashes1 = array();
     for ($ai = 1; $ai <= $postcountpb; $ai++) {
         $posthashes1[$ai] = $this->get_post_stub($oublog->id);
         $posthashes1[$ai]->title = "TP " . $titlecheck;
         $posthashes1[$ai]->userid = $student1->id;
         $posthashes1[$ai]->visibility = OUBLOG_VISIBILITY_COURSEUSER;
     }
     // Create the posts - assumes oublog_add_post is working,
     // also add student comments to those posts.
     $postids = $commentids = array();
     foreach ($posthashes1 as $posthasha) {
         $postids[] = oublog_add_post($posthasha, $cm, $oublog, $SITE);
         $comment1 = $this->get_comment_stub($posthasha->id, $student1->id);
         $comment1->title = "TC " . $titlecheck;
         $commentids[] = oublog_add_comment($SITE, $cm, $oublog, $comment1);
         $comment2 = $this->get_comment_stub($posthasha->id, $student2->id);
         $comment2->title = "TC " . $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment2);
     }
     // Get the participation object with counts of posts and comments.
     $curgroup = 0;
     $curindividual = 0;
     $limitnum = 8;
     $start = $end = $page = 0;
     $limitfrom = 0;
     $getposts = false;
     // Dont need posts on global blog.
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test postscount && commentscount;
     // When visibility is private course user only return none.
     $this->assertEquals(0, $participation->postscount);
     $this->assertEquals(0, $participation->commentscount);
     // Prepare to make some posts to use for test two.
     $posthashes1 = array();
     for ($ai = 1; $ai <= $postcountpb; $ai++) {
         $posthashes1[$ai] = $this->get_post_stub($oublog->id);
         $posthashes1[$ai]->title = "TP " . $titlecheck;
         $posthashes1[$ai]->userid = $student1->id;
         $posthashes1[$ai]->visibility = OUBLOG_VISIBILITY_LOGGEDINUSER;
     }
     // Create the posts - assumes oublog_add_post is working,
     // also add student comments to those posts.
     $postids = $commentids = array();
     foreach ($posthashes1 as $posthasha) {
         $postids[] = oublog_add_post($posthasha, $cm, $oublog, $SITE);
         $comment1 = $this->get_comment_stub($posthasha->id, $student1->id);
         $comment1->title = "TC " . $titlecheck;
         $commentids[] = oublog_add_comment($SITE, $cm, $oublog, $comment1);
         $comment2 = $this->get_comment_stub($posthasha->id, $student2->id);
         $comment2->title = "TC " . $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment2);
     }
     // Get the participation object with counts of posts and comments.
     $curgroup = 0;
     $curindividual = 0;
     $limitnum = 8;
     $start = $end = $page = $limitfrom = 0;
     $getposts = false;
     // Dont need posts on global blog.
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // For this test each post commented on twice.
     $this->assertEquals($postcountpb, $participation->postscount);
     $this->assertEquals($postcountpb * 2, $participation->commentscount);
     // Test comments object.
     foreach ($participation->comments as $comment) {
         // Same basic checks - does it match our test comment created above.
         $this->assertInstanceOf('stdClass', $comment);
         $this->assertEquals("TC " . $titlecheck, $comment->title);
         $this->assertEquals(OUBLOG_VISIBILITY_LOGGEDINUSER, $comment->visibility);
     }
     // Prepare to make some posts to use for test 3.
     $posthashes1 = array();
     for ($ai = 1; $ai <= $postcountpb; $ai++) {
         $posthashes1[$ai] = $this->get_post_stub($oublog->id);
         $posthashes1[$ai]->title = "TP " . $titlecheck;
         $posthashes1[$ai]->userid = $student1->id;
         $posthashes1[$ai]->visibility = OUBLOG_VISIBILITY_PUBLIC;
     }
     // Create the posts - assumes oublog_add_post is working,
     // also add student comments to those posts.
     $postids = $commentids = array();
     foreach ($posthashes1 as $posthasha) {
         $postids[] = oublog_add_post($posthasha, $cm, $oublog, $SITE);
         $comment1 = $this->get_comment_stub($posthasha->id, $student1->id);
         $comment1->title = "TC " . $titlecheck;
         $commentids[] = oublog_add_comment($SITE, $cm, $oublog, $comment1);
         $comment2 = $this->get_comment_stub($posthasha->id, $student2->id);
         $comment2->title = "TC " . $titlecheck;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment2);
     }
     // Get the participation object with counts of posts and comments.
     $curgroup = 0;
     $curindividual = 0;
     $limitnum = 8;
     $start = $end = $page = $limitfrom = 0;
     $getposts = false;
     // Dont need posts on global blog.
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test postscount && commentscount; the same number of records
     // or multiples of $postcountpb, should be discovered.
     $this->assertEquals($postcountpb * 2, $participation->postscount);
     $this->assertEquals($postcountpb * 4, $participation->commentscount);
     // But we should only return the limited amount of 8 comments.
     $this->assertCount(0, $participation->posts);
     $this->assertCount($limitnum, $participation->comments);
     // Test comments object.
     foreach ($participation->comments as $comment) {
         // Same basic checks - does it match our test comment created above.
         $this->assertInstanceOf('stdClass', $comment);
         $this->assertEquals("TC " . $titlecheck, $comment->title);
     }
     // Set as guest user not logged in.
     // Get the participation object with counts of posts and comments.
     $curgroup = 0;
     $this->setUser(0);
     $curindividual = 0;
     $limitnum = 8;
     $start = $end = $page = $limitfrom = 0;
     $getposts = false;
     // Dont need posts on global blog.
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test postscount && commentscount; the same number of records
     // or multiples of $postcountpb, should be returned that were added.
     $this->assertEquals($postcountpb, $participation->postscount);
     $this->assertEquals($postcountpb * 2, $participation->commentscount);
     // Test that we only return the limited amount of 8 commentss.
     $this->assertCount(0, $participation->posts);
     $this->assertLessThan($limitnum, count($participation->comments));
     // Test comments object.
     foreach ($participation->comments as $comment) {
         // Same basic checks - does it match our test comment created above.
         $this->assertInstanceOf('stdClass', $comment);
         $this->assertEquals("TC " . $titlecheck, $comment->title);
     }
     // Separate individuals time checking.
     // Reset the User previously set as guest user not logged in.
     $this->setAdminUser();
     $yesterday = time() - 24 * 60 * 60 - 60;
     $beforeyesterday = time() - 2 * 24 * 60 * 60;
     $sometimetoday = time();
     $oublog = $this->get_new_oublog($course->id, array('individual' => OUBLOG_SEPARATE_INDIVIDUAL_BLOGS));
     // Number of posts/comments for these tests.
     $postcountsit = 4;
     $titlecheck = ' oublog_sit_test_times';
     // Creating some post stubs for student 1 to use
     $posthashes = array();
     for ($i = 1; $i <= $postcountsit; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title .= $titlecheck;
         $posthashes[$i]->userid = $student1->id;
         $posthashes[$i]->timeposted = $sometimetoday;
     }
     // Create student 1s posts with comments by S1 and S2.
     $postids = array();
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         $comment1 = $this->get_comment_stub($posthash->id, $student1->id);
         $comment1->title .= $titlecheck;
         $comment1->timeposted = $sometimetoday;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment1);
         $comment2 = $this->get_comment_stub($posthash->id, $student2->id);
         $comment2->title .= $titlecheck;
         $comment2->timeposted = $sometimetoday;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment2);
     }
     // Create student 2s yesterday posts and comments.
     for ($i = 1; $i <= $postcountsit; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title .= $titlecheck;
         $posthashes[$i]->userid = $student2->id;
         $posthashes[$i]->timeposted = $yesterday;
     }
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         $comment1 = $this->get_comment_stub($posthash->id, $student2->id);
         $comment1->title .= $titlecheck;
         $comment1->timeposted = $yesterday;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment1);
         $comment2 = $this->get_comment_stub($posthash->id, $student1->id);
         $comment2->title .= $titlecheck;
         $comment2->timeposted = $yesterday;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment2);
     }
     // Create more student 2 posts before yesterdays posts and comments.
     // A limited set of these will be returned in a later test.
     for ($i = 1; $i <= $postcountsit; $i++) {
         $posthashes[$i] = $this->get_post_stub($oublog->id);
         $posthashes[$i]->title = $titlecheck . $student2->id . $beforeyesterday . $i;
         $posthashes[$i]->userid = $student2->id;
         $posthashes[$i]->timeposted = $beforeyesterday;
     }
     foreach ($posthashes as $posthash) {
         $postids[] = oublog_add_post($posthash, $cm, $oublog, $course);
         $comment1 = $this->get_comment_stub($posthash->id, $student1->id);
         $comment1->title = $titlecheck . $student1->id . $beforeyesterday . $posthash->id;
         $comment1->timeposted = $beforeyesterday;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment1);
         $comment2 = $this->get_comment_stub($posthash->id, $student2->id);
         $comment2->title = $titlecheck . $student2->id . $beforeyesterday . $posthash->id;
         $comment2->timeposted = $beforeyesterday;
         $commentids[] = oublog_add_comment($course, $cm, $oublog, $comment2);
         // Saving last entries for limited beforeyesterday assertions later.
         $posttitlebeforeyesterday = $posthash->title;
         $comment1titlebeforeyesterday = $comment1->title;
         $comment2titlebeforeyesterday = $comment2->title;
     }
     // Test All time entries.
     $curgroup = 0;
     $curindividual = 0;
     $oublog->individual = OUBLOG_SEPARATE_INDIVIDUAL_BLOGS;
     $limitnum = 8;
     $start = 0;
     $end = 0;
     $page = 0;
     $limitfrom = 0;
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test count of posts.
     $this->assertEquals($postcountsit * 3, $participation->postscount);
     // The number of student posts should be 'returned' that were added.
     $this->assertCount($postcountsit * 2, $participation->posts);
     // Test count of all students comments.
     $this->assertEquals($postcountsit * 6, $participation->commentscount);
     // The number of comments should be 'returned'.
     $this->assertCount($postcountsit * 2, $participation->comments);
     // Test that the posts returned match the ones added.
     foreach ($participation->posts as $post) {
         // Do some basic checks - does it match our test post created above.
         $this->assertInstanceOf('stdClass', $post);
         $this->assertEquals("testpost" . $titlecheck, $post->title);
         $postername = fullname($post);
         $this->assertEquals($postername, $post->firstname . " " . $post->lastname);
     }
     // Test comments object.
     foreach ($participation->comments as $comment) {
         // Same basic checks - does it match our test comment created above.
         $this->assertInstanceOf('stdClass', $comment);
     }
     // Test previous day timed entries.
     $limitnum = 8;
     $start = $yesterday - 60;
     $end = $sometimetoday;
     $page = $limitfrom = 0;
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test count of posts for period.
     $this->assertEquals($postcountsit, $participation->postscount);
     // The number of student posts for period which should be 'returned'
     $this->assertCount($postcountsit, $participation->posts);
     // Test count of all students comments for test period.
     $this->assertEquals($postcountsit * 2, $participation->commentscount);
     // The number of comments should be 'returned'.
     $this->assertCount($postcountsit * 2, $participation->comments);
     // Test a limited time period during previous day.
     $curgroup = 0;
     $curindividual = 0;
     $limitnum = 8;
     $start = $yesterday - 60 * 60;
     $end = $yesterday + 60 * 60;
     $page = $limitfrom = 0;
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test count of posts for period.
     $this->assertEquals($postcountsit, $participation->postscount);
     // The number of student posts for period which should be 'returned'
     $this->assertCount($postcountsit, $participation->posts);
     // Test count of all students comments for test period.
     $this->assertEquals($postcountsit * 2, $participation->commentscount);
     // The number of comments should be 'returned'.
     $this->assertCount($postcountsit * 2, $participation->comments);
     // Test extended daily time period
     $curgroup = 0;
     $curindividual = 0;
     $limitnum = 8;
     $start = $beforeyesterday - 60;
     $end = $sometimetoday + 60;
     $page = $limitfrom = 0;
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test count of posts to be 'discovered' for period.
     $this->assertEquals($postcountsit * 3, $participation->postscount);
     // The number of student posts for period which should be 'returned'
     $this->assertCount($postcountsit * 2, $participation->posts);
     // Test count of all students comments 'discovered' for test period.
     $this->assertEquals($postcountsit * 6, $participation->commentscount);
     // The number of comments which should be 'returned'.
     $this->assertCount($postcountsit * 2, $participation->comments);
     foreach ($participation->posts as $post) {
         // Do some basic checks - does it match our test post created above.
         $this->assertInstanceOf('stdClass', $post);
         $this->assertEquals("testpost" . $titlecheck, $post->title);
         $this->assertEquals(fullname($post), $post->firstname . " " . $post->lastname);
     }
     // Test comments object.
     foreach ($participation->comments as $comment) {
         // Same basic checks - does it match our test comment created above.
         $this->assertInstanceOf('stdClass', $comment);
         $this->assertEquals(OUBLOG_VISIBILITY_COURSEUSER, $comment->visibility);
     }
     // Test using extended daily time period with limited return of posts and comments,
     // using saved last entries for beforeyesterday assertions.
     $curgroup = 0;
     $curindividual = 0;
     $limitnum = $postcountsit * 2;
     // Any Number, above the $postcountsit
     $start = $beforeyesterday - 1;
     $end = $beforeyesterday + 1;
     $page = 0;
     $limitfrom = $postcountsit - 2;
     // Any Number, less that $postcountsit.
     $getposts = true;
     $getcomments = true;
     $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
     // Test count of posts to be 'discovered' for period.
     $this->assertEquals($postcountsit, $participation->postscount);
     // The number of student posts for period which should be 'returned'
     $this->assertCount($postcountsit - 2, $participation->posts);
     // Test count of all students comments for test period.
     $this->assertEquals($limitnum, $participation->commentscount);
     // The number of comments should be 'returned'.
     $this->assertCount($limitnum - $limitfrom, $participation->comments);
     // Test posts object.
     foreach ($participation->posts as $post) {
         // Do some basic checks - does it match our test post created above.
         $this->assertInstanceOf('stdClass', $post);
         $this->assertEquals($student2->id, $post->userid);
         $postername = fullname($post);
         $this->assertEquals($postername, $post->firstname . " " . $post->lastname);
         // Recognise last returned post correctly, but without id in title.
         if ($post->title == $posttitlebeforeyesterday) {
             $this->assertTrue(true);
         }
     }
     foreach ($participation->comments as $comment) {
         // Same basic checks - does it match our test comment created above.
         $this->assertInstanceOf('stdClass', $comment);
         $this->assertLessThanOrEqual(OUBLOG_VISIBILITY_COURSEUSER, $comment->visibility);
         // Recognise last returned comments correctly, but with out ids in titles.
         if ($comment->title == $comment1titlebeforeyesterday || $comment->title == $comment2titlebeforeyesterday) {
             $this->assertTrue(true);
         }
     }
 }
if ($start && !$end) {
    $title = get_string('participation', 'oublog');
    $info = get_string('participation_from', 'oublog', $startdate);
}
if (!$start && $end) {
    $title = get_string('participation', 'oublog');
    $info = get_string('participation_to', 'oublog', $enddate);
}
if ($start && $end) {
    $a = new stdClass();
    $a->start = $startdate;
    $a->end = $enddate;
    $title = get_string('participation', 'oublog');
    $info = get_string('participation_fromto', 'oublog', $a);
}
$participation = oublog_get_participation_details($oublog, $groupid, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
$url->params(array('individual' => $curindividual, 'start' => $start, 'end' => $end));
echo html_writer::tag('h2', $info, array('class' => 'oublog-post-title'));
$timefilter->display();
$taburl = clone $url;
$taburl->remove_params(array('page', 'tab'));
$tabs = array(new tabobject('tab0', $taburl, $participation->postscount . ' ' . get_string('posts', 'oublog')), new tabobject('tab1', $taburl->out() . '&amp;tab=1', $participation->commentscount . ' ' . get_string('comments', 'oublog')));
echo $OUTPUT->tabtree($tabs, "tab{$tab}");
// Output message when no content for tab.
$warning = '';
if ($tab == 0 && (!isset($participation->postscount) || $participation->postscount < 1)) {
    $warning = get_string('nouserpostsfound', 'oublog');
    $getposts = false;
} else {
    if ($tab == 1 && (!isset($participation->commentscount) || $participation->commentscount < 1)) {
        $warning = get_string('nousercommentsfound', 'oublog');
/**
 * Generates oublog multiple users participation statistics output.
 * @param object $oublog
 * @param object $cm
 * @param mod_oublog_renderer $renderer
 * @param bool $ return data object rather than html
 */
function oublog_stats_output_participation($oublog, $cm, $renderer = null, $course, $allposts = false, $curindividual = -1, $globalindividual = null)
{
    global $PAGE, $DB, $USER, $OUTPUT;
    if (!$renderer) {
        $renderer = $PAGE->get_renderer('mod_oublog');
    }
    // Setup Participation capability checks.
    $curgroup = oublog_get_activity_group($cm);
    // Get blogtype, groupmode and individualmode
    $blogtype = $oublog->global;
    $groupmode = oublog_get_activity_groupmode($cm, $course);
    $individualmode = $oublog->individual;
    // Dont show on personal blogs if not logged in.
    if ($blogtype && !isloggedin()) {
        return;
    }
    // Dont show if current individual is not 0 and either separate individual blog or
    // visible individual with no comments.
    if (isset($curindividual) && $curindividual != 0 && $oublog->allowcomments == OUBLOG_COMMENTS_PREVENT && $individualmode != OUBLOG_NO_INDIVIDUAL_BLOGS) {
        return;
    }
    $context = context_module::instance($cm->id);
    $curindividual = $curindividual ? $curindividual : $globalindividual;
    if ($curindividual == -1) {
        $curindividual = $globalindividual;
    }
    // Get the participation object with counts of posts and comments.
    $limitnum = 8;
    $start = $end = $page = $limitfrom = $tab = 0;
    $getposts = $getcomments = true;
    if ($oublog->global) {
        // Dont want to see posts on personal blogs.
        $getposts = false;
    }
    if ($oublog->allowcomments < OUBLOG_COMMENTS_ALLOW) {
        // Dont want to see comments visible individual blogs.
        $getcomments = false;
    }
    $participation = oublog_get_participation_details($oublog, $curgroup, $curindividual, $start, $end, $page, $getposts, $getcomments, $limitfrom, $limitnum);
    // Generate content data to send to renderer.
    $maintitle = get_string('participation', 'oublog');
    // The title of the block 'section'.
    $content = '';
    $postedcount = $commentedcount = $commenttotal = 0;
    $postshow = 8;
    if (count($participation->comments) <= 4) {
        $commenttotal = count($participation->comments);
    } else {
        if (count($participation->comments) >= 4) {
            $commenttotal = 4;
        }
    }
    if (!$participation->posts) {
        if (!$blogtype && $individualmode != OUBLOG_VISIBLE_INDIVIDUAL_BLOGS) {
            $content .= html_writer::tag('p', get_string('nouserposts', 'oublog'));
        }
        // For visible individual blogs show post activity also when no individual selected.
    } else {
        $percent = $stat = null;
        $content .= html_writer::tag('p', get_string('recentposts', 'oublog'));
        foreach ($participation->posts as $post) {
            // Post user object required for oublog_statsinfo.
            $postuser = new object();
            $postuser->id = $post->userid;
            $postuser->groupid = $post->groupid;
            $fields = explode(',', user_picture::fields('', null, '', null));
            foreach ($fields as $field) {
                if ($field != 'id') {
                    $pfield = $field;
                    $postuser->{$field} = $post->{$field};
                }
            }
            $linktext = $name = $grpname = $dispname = '';
            $bparams = array();
            $a = (object) array('name' => $name, 'displayname' => $dispname);
            if ($postedcount >= $postshow - $commenttotal) {
                break;
            }
            $url = new moodle_url('/mod/oublog/viewpost.php', array('post' => $post->id));
            $postname = !empty($post->title) ? $post->title : get_string('untitledpost', 'oublog');
            $label = html_writer::div(html_writer::link($url, $postname), 'oublogstats_posts_posttitle');
            if (oublog_get_displayname($oublog) && $postuser->id != $curindividual) {
                $dispname = oublog_get_displayname($oublog);
            }
            if ($post->blogname != "") {
                $name = $post->blogname;
            } else {
                if ($groupmode > NOGROUPS && $individualmode == OUBLOG_NO_INDIVIDUAL_BLOGS) {
                    if ($post->groupid != $curgroup) {
                        $bparams['id'] = $cm->id;
                        $bparams['group'] = $post->groupid;
                        $grpname = groups_get_group_name($post->groupid);
                        $a = (object) array('name' => $grpname, 'displayname' => $dispname);
                        $linktext = get_string('defaultpersonalblogname', 'oublog', $a);
                    }
                } else {
                    if ($individualmode != OUBLOG_NO_INDIVIDUAL_BLOGS) {
                        $bparams['individual'] = $post->userid;
                        if ($postuser->id != $curindividual) {
                            $name = fullname($postuser);
                            $a = (object) array('name' => $name, 'displayname' => $dispname);
                            $linktext = get_string('defaultpersonalblogname', 'oublog', $a);
                        }
                    }
                }
            }
            if (!$groupmode && $grpname != "" || $name != "") {
                $bparams['id'] = $cm->id;
                $bparams['individual'] = $post->userid;
                $a = (object) array('name' => $name, 'displayname' => $dispname);
                $linktext = get_string('defaultpersonalblogname', 'oublog', $a);
            }
            $burl = new moodle_url('/mod/oublog/view.php', $bparams);
            if ($linktext != "") {
                // We output post time followed by a link.
                $label .= html_writer::div(oublog_date($post->timeposted) . html_writer::empty_tag('br', array()) . html_writer::link($burl, $linktext), 'oublogstats_commentposts_blogname');
            } else {
                // We output just post.
                $label .= html_writer::div(oublog_date($post->timeposted), 'oublogstats_commentposts_blogname');
            }
            $statinfo = new oublog_statsinfo($postuser, $percent, $stat, $url, $label);
            $content .= $renderer->render($statinfo);
            $postedcount++;
        }
    }
    // Pre test the numbers of posts/comments for display upto max.
    $postspluscount = count($participation->posts) - $postedcount;
    if ($postspluscount >= 1 && !$blogtype && !$individualmode) {
        $content .= html_writer::tag('p', get_string('numberpostsmore', 'oublog', $postspluscount));
    }
    unset($bparams);
    if (!$participation->comments && $getcomments) {
        $content .= html_writer::tag('p', get_string('nousercomments', 'oublog'));
    } else {
        $percent = $stat = null;
        // Removing all stats div.
        if ($blogtype || $getcomments) {
            $content .= html_writer::tag('p', get_string('recentcomments', 'oublog'));
        }
        foreach ($participation->comments as $comment) {
            // Comment user object required for oublog_statsinfo.
            $commentuser = new object();
            if (empty($comment->commenterid)) {
                $commentuser->id = -1;
            } else {
                $commentuser->id = $comment->commenterid;
            }
            $fields = explode(',', user_picture::fields('', null, '', 'commenter'));
            foreach ($fields as $field) {
                if ($field != 'id') {
                    $cfield = "commenter" . $field;
                    $commentuser->{$field} = $comment->{$cfield};
                }
            }
            // Comment poster object required.
            $commentposter = new object();
            $commentposter->id = $comment->posterid;
            $fields = explode(',', user_picture::fields('', null, '', 'poster'));
            foreach ($fields as $field) {
                if ($field != 'id') {
                    $cfield = "poster" . $field;
                    $commentposter->{$field} = $comment->{$cfield};
                }
            }
            $commentuser->groupid = $comment->groupid;
            if ($commentedcount + $postedcount >= $postshow) {
                break;
            }
            $url = new moodle_url('/mod/oublog/viewpost.php', array('post' => $comment->postid));
            $lnkurl = $url->out() . '#cid' . $comment->id;
            $commentname = !empty($comment->title) ? $comment->title : get_string('untitledcomment', 'oublog');
            $label = html_writer::div(html_writer::link($lnkurl, $commentname), 'oublogstats_commentposts_posttitle');
            $linktext = $name = $grpname = $dispname = '';
            $a = (object) array('name' => $name, 'displayname' => $dispname);
            $bparams = array();
            if (oublog_get_displayname($oublog) && $comment->posterid != $curindividual) {
                $dispname = oublog_get_displayname($oublog);
            }
            if ($comment->bloginstancename && $curindividual != $comment->posterid) {
                $bparams['user'] = $comment->posterid;
                $name = $comment->bloginstancename;
            } else {
                if ($groupmode > NOGROUPS && $individualmode == OUBLOG_NO_INDIVIDUAL_BLOGS) {
                    if ($comment->groupid != $curgroup) {
                        $bparams['id'] = $cm->id;
                        $bparams['group'] = $comment->groupid;
                        $grpname = groups_get_group_name($comment->groupid);
                        $a = (object) array('name' => $grpname, 'displayname' => $dispname);
                        $linktext = get_string('defaultpersonalblogname', 'oublog', $a);
                    }
                } else {
                    if ($individualmode != OUBLOG_NO_INDIVIDUAL_BLOGS) {
                        $bparams['id'] = $cm->id;
                        $bparams['individual'] = $comment->posterid;
                        if ($comment->posterid != $curindividual) {
                            $name = fullname($commentposter);
                            $a = (object) array('name' => $name, 'displayname' => $dispname);
                            $linktext = get_string('defaultpersonalblogname', 'oublog', $a);
                        }
                    }
                }
            }
            // Personal or Course Wide.
            if (!$groupmode && $grpname != "" || $name != "") {
                $bparams['individual'] = $comment->posterid;
                $a = (object) array('name' => $name, 'displayname' => $dispname);
                $linktext = get_string('defaultpersonalblogname', 'oublog', $a);
            }
            if (!$groupmode && $oublog->individual == 0 && $comment->posterid != $curindividual) {
                if (!$oublog->global) {
                    $bparams['id'] = $cm->id;
                }
                $bparams['individual'] = $comment->posterid;
                if ($oublog->global) {
                    $linktext = $comment->bloginstancename;
                }
            }
            $burl = new moodle_url('/mod/oublog/view.php', $bparams);
            if ($linktext != "") {
                // We output post time followed by a link.
                $label .= html_writer::div(oublog_date($comment->timeposted) . html_writer::empty_tag('br', array()) . html_writer::link($burl, $linktext), 'oublogstats_commentposts_blogname');
            } else {
                // We output just post.
                $label .= html_writer::div(oublog_date($comment->timeposted), 'oublogstats_commentposts_blogname');
            }
            $statinfo = new oublog_statsinfo($commentuser, $percent, $stat, $url, $label);
            $content .= $renderer->render($statinfo);
            $commentedcount++;
        }
    }
    // If the number of comments is more than can be shown.
    $commentspluscount = count($participation->comments) - $commentedcount;
    if ($commentspluscount >= 1) {
        $content .= html_writer::tag('p', get_string('numbercommentsmore', 'oublog', $commentspluscount));
    }
    $params = array('id' => $cm->id, 'group' => $curgroup, 'individual' => $curindividual);
    if (!$blogtype) {
        if (!$allposts) {
            $url = new moodle_url('/mod/oublog/participationlist.php', $params);
            $viewparticipation = html_writer::div(html_writer::link($url, get_string('viewallparticipation', 'oublog')));
            $content .= html_writer::start_tag('div', array('class' => 'oublog-post-content'));
            $content .= html_writer::tag('h3', $viewparticipation, array('class' => 'oublog-post-title'));
            $content .= html_writer::end_tag('div');
        }
    }
    return $renderer->render_stats_view('participation', $maintitle, $content, null, null, null);
}