print_error('invalidblog', 'oublog');
}
if (!oublog_can_view_post($post, $USER, $context, $oublog->global)) {
    print_error('accessdenied', 'oublog');
}
// Get strings.
$stroublogs = get_string('modulenameplural', 'oublog');
$stroublog = get_string('modulename', 'oublog');
$strdelete = get_string('delete', 'oublog');
$strtags = get_string('tags', 'oublog');
$strcomments = get_string('comments', 'oublog');
$strlinks = get_string('links', 'oublog');
$strfeeds = get_string('feeds', 'oublog');
// Set-up groups.
$groupmode = oublog_get_activity_groupmode($cm, $course);
$currentgroup = oublog_get_activity_group($cm, true);
// Check permissions for group (of post).
if ($groupmode == VISIBLEGROUPS && !groups_is_member($post->groupid) && !has_capability('moodle/site:accessallgroups', $context)) {
    $canpost = false;
    $canmanageposts = false;
    $canaudit = false;
}
// Print the header.
if ($oublog->global) {
    $blogtype = 'personal';
    $returnurl = 'view.php?user='******'user', array('id' => $oubloginstance->userid)))) {
        print_error('invaliduserid');
    }
    $PAGE->navbar->add(fullname($oubloguser), new moodle_url("/user/view.php", array('id' => $oubloguser->id)));
function print_section_newsroom($course, $section, $mods, $modnamesused, $absolute = false, $width = "100%")
{
    /// Prints a section full of activity modules
    global $CFG, $USER;
    static $initialised;
    static $groupbuttons;
    static $groupbuttonslink;
    static $isediting;
    static $ismoving;
    static $strmovehere;
    static $strmovefull;
    static $strunreadpostsone;
    static $usetracking;
    static $groupings;
    if (!isset($initialised)) {
        $groupbuttons = ($course->groupmode or !$course->groupmodeforce);
        $groupbuttonslink = !$course->groupmodeforce;
        $isediting = isediting($course->id);
        $ismoving = $isediting && ismoving($course->id);
        if ($ismoving) {
            $strmovehere = get_string("movehere");
            $strmovefull = strip_tags(get_string("movefull", "", "'{$USER->activitycopyname}'"));
        }
        include_once $CFG->dirroot . '/mod/forum/lib.php';
        if ($usetracking = forum_tp_can_track_forums()) {
            $strunreadpostsone = get_string('unreadpostsone', 'forum');
        }
        $initialised = true;
    }
    $labelformatoptions = new object();
    $labelformatoptions->noclean = true;
    /// Casting $course->modinfo to string prevents one notice when the field is null
    $modinfo = get_fast_modinfo($course);
    //Acccessibility: replace table with list <ul>, but don't output empty list.
    if (!empty($section->sequence)) {
        // Fix bug #5027, don't want style=\"width:$width\".
        echo "<ul class=\"section img-text\">\n";
        $sectionmods = explode(",", $section->sequence);
        foreach ($sectionmods as $modnumber) {
            if (empty($mods[$modnumber])) {
                continue;
            }
            $mod = $mods[$modnumber];
            if ($ismoving and $mod->id == $USER->activitycopy) {
                // do not display moving mod
                continue;
            }
            if (isset($modinfo->cms[$modnumber])) {
                if (!$modinfo->cms[$modnumber]->uservisible) {
                    // visibility shortcut
                    continue;
                }
            } else {
                if (!file_exists("{$CFG->dirroot}/mod/{$mod->modname}/lib.php")) {
                    // module not installed
                    continue;
                }
                if (!coursemodule_visible_for_user($mod)) {
                    // full visibility check
                    continue;
                }
            }
            // The magic! ... if indent == 1 then ... hide module
            if ($mod->indent == 1) {
                $hiddemodule = 'hidden';
            } else {
                $hiddemodule = '';
            }
            echo '<li class="activity ' . $mod->modname . ' ' . $hiddemodule . '" id="module-' . $modnumber . '">';
            // Unique ID
            if ($ismoving) {
                echo '<a title="' . $strmovefull . '"' . ' href="' . $CFG->wwwroot . '/course/mod.php?moveto=' . $mod->id . '&amp;sesskey=' . $USER->sesskey . '">' . '<img class="movetarget" src="' . $CFG->pixpath . '/movehere.gif" ' . ' alt="' . $strmovehere . '" /></a><br />
                     ';
            }
            if ($mod->indent) {
                print_spacer(12, 20 * $mod->indent, false);
            }
            $extra = '';
            if (!empty($modinfo->cms[$modnumber]->extra)) {
                $extra = $modinfo->cms[$modnumber]->extra;
            }
            if ($mod->modname == "label") {
                echo "<span class=\"";
                if (!$mod->visible) {
                    echo 'dimmed_text';
                } else {
                    echo 'label';
                }
                echo '">';
                echo format_text($extra, FORMAT_HTML, $labelformatoptions);
                echo "</span>";
                if (!empty($CFG->enablegroupings) && !empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
                    if (!isset($groupings)) {
                        $groupings = groups_get_all_groupings($course->id);
                    }
                    echo " <span class=\"groupinglabel\">(" . format_string($groupings[$mod->groupingid]->name) . ')</span>';
                }
            } else {
                // Normal activity
                $instancename = format_string($modinfo->cms[$modnumber]->name, true, $course->id);
                if (!empty($modinfo->cms[$modnumber]->icon)) {
                    $icon = "{$CFG->pixpath}/" . $modinfo->cms[$modnumber]->icon;
                } else {
                    $icon = "{$CFG->modpixpath}/{$mod->modname}/icon.gif";
                }
                //Accessibility: for files get description via icon.
                $altname = '';
                if ('resource' == $mod->modname) {
                    if (!empty($modinfo->cms[$modnumber]->icon)) {
                        $possaltname = $modinfo->cms[$modnumber]->icon;
                        $mimetype = mimeinfo_from_icon('type', $possaltname);
                        $altname = get_mimetype_description($mimetype);
                    } else {
                        $altname = $mod->modfullname;
                    }
                } else {
                    $altname = $mod->modfullname;
                }
                // Avoid unnecessary duplication.
                if (false !== stripos($instancename, $altname)) {
                    $altname = '';
                }
                // File type after name, for alphabetic lists (screen reader).
                if ($altname) {
                    $altname = get_accesshide(' ' . $altname);
                }
                $linkcss = $mod->visible ? "" : " class=\"dimmed\" ";
                echo '<a ' . $linkcss . ' ' . $extra . ' href="' . $CFG->wwwroot . '/mod/' . $mod->modname . '/view.php?id=' . $mod->id . '">' . '<img src="' . $icon . '" class="activityicon" alt="" /> <span>' . $instancename . $altname . '</span></a>';
                //echo " $mod->modname ";
                // Special NEWSPAPER magic ... show summry from resource and blog's 200 chars from each post OUBlog (nadavkav)
                if ($mod->modname == 'resource') {
                    $article = get_record('resource', 'id', $mod->instance);
                    //print_r($article );
                    echo "{$article->summary}";
                }
                if ($mod->modname == 'oublog') {
                    require_once $CFG->dirroot . '/mod/oublog/locallib.php';
                    $oublog = get_record('oublog', 'id', $mod->instance);
                    if (!($oublogcm = get_coursemodule_from_instance('oublog', $oublog->id))) {
                        //error("Course module ID was incorrect");
                    }
                    $oublogcontext = get_context_instance(CONTEXT_MODULE, $oublogcm->id);
                    //oublog_check_view_permissions($oublog, $oublogcontext, $oublogcm);
                    $currentgroup = oublog_get_activity_group($oublogcm, true);
                    echo '<br/>';
                    /// Get Posts
                    list($posts, $recordcount) = oublog_get_posts($oublog, $oublogcontext, 0, $oublogcm, $currentgroup);
                    //, -1, $oubloguser->id, $tag, $canaudit);
                    $i = 0;
                    foreach ($posts as $post) {
                        if ($i < 3) {
                            echo '<a href="' . $CFG->wwwroot . '/mod/oublog/viewpost.php?blog=' . $oublog->id . '&post=' . $post->id . '">' . $post->title . '</a><br/>';
                            $summaryformatoptions->noclean = false;
                            echo mb_substr(format_text($post->message, FORMAT_HTML, $summaryformatoptions), 1, 600) . "<hr/>";
                        }
                        if ($i > 2 and $i < 6) {
                            echo '<a href="' . $CFG->wwwroot . '/mod/oublog/viewpost.php?blog=' . $oublog->id . '&post=' . $post->id . '">' . $post->title . '</a><br/>';
                        }
                        $i++;
                    }
                    //print_r($article );
                    //echo "$article->summary";
                    echo '<br/><a ' . $linkcss . ' ' . $extra . ' href="' . $CFG->wwwroot . '/mod/' . $mod->modname . '/view.php?id=' . $mod->id . '">' . '<img src="' . $icon . '" class="activityicon" alt="" /> <span>' . get_string('more', 'format_newsroom') . '</span></a>';
                }
                if (!empty($CFG->enablegroupings) && !empty($mod->groupingid) && has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
                    if (!isset($groupings)) {
                        $groupings = groups_get_all_groupings($course->id);
                    }
                    echo " <span class=\"groupinglabel\">(" . format_string($groupings[$mod->groupingid]->name) . ')</span>';
                }
            }
            if ($usetracking && $mod->modname == 'forum') {
                if ($unread = forum_tp_count_forum_unread_posts($mod, $course)) {
                    echo '<span class="unread"> <a href="' . $CFG->wwwroot . '/mod/forum/view.php?id=' . $mod->id . '">';
                    if ($unread == 1) {
                        echo $strunreadpostsone;
                    } else {
                        print_string('unreadpostsnumber', 'forum', $unread);
                    }
                    echo '</a></span>';
                }
            }
            if ($isediting) {
                // TODO: we must define this as mod property!
                if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource' and $mod->modname != 'glossary') {
                    if (!($mod->groupmodelink = $groupbuttonslink)) {
                        $mod->groupmode = $course->groupmode;
                    }
                } else {
                    $mod->groupmode = false;
                }
                echo '&nbsp;&nbsp;';
                echo make_editing_buttons($mod, $absolute, true, $mod->indent, $section->section);
            }
            echo "</li>\n";
        }
    } elseif ($ismoving) {
        echo "<ul class=\"section\">\n";
    }
    if ($ismoving) {
        echo '<li><a title="' . $strmovefull . '"' . ' href="' . $CFG->wwwroot . '/course/mod.php?movetosection=' . $section->id . '&amp;sesskey=' . $USER->sesskey . '">' . '<img class="movetarget" src="' . $CFG->pixpath . '/movehere.gif" ' . ' alt="' . $strmovehere . '" /></a></li>
             ';
    }
    if (!empty($section->sequence) || $ismoving) {
        echo "</ul><!--class='section'-->\n\n";
    }
}
 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);
         }
     }
 }
/**
 * 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);
}
/**
 * Checks whether a group is writable GIVEN THAT YOU CAN SEE THE BLOG
 * (i.e. this does not handle the separate-groups case, only visible-groups).
 * The information is static-cached so this function can be called multiple
 * times.
 * @param object $cm Moodle course-module
 */
function oublog_is_writable_group($cm)
{
    $groupmode = oublog_get_activity_groupmode($cm);
    if ($groupmode != VISIBLEGROUPS) {
        // If no groups, then they must be allowed to access this;
        // if separate groups, then because this is defined to only work
        // for entries you can see, you must be allowed to access; so only
        // doubt is for visible groups.
        return true;
    }
    $groupid = oublog_get_activity_group($cm);
    if (isset($cm->writablegroups)) {
        $cm->writablegroups = array();
    }
    if (isset($cm->writablegroups[$groupid])) {
        return $cm->writablegroups[$groupid];
    }
    $cm->writablegroups[$groupid] = groups_is_member($groupid) || has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id));
    return $cm->writablegroups[$groupid];
}
/**
 * Generates oublog user participation statistics output.
 * @param object $oublog
 * @param object $cm
 * @param mod_oublog_renderer $renderer
 * @param bool $ajax true to return data object rather than html
 */
function oublog_stats_output_myparticipation($oublog, $cm, $renderer = null, $course, $currentindividual, $globalindividual = null)
{
    global $PAGE, $DB, $USER, $OUTPUT;
    if (!isloggedin()) {
        // My participation is only visible to actual users.
        return;
    }
    if (!$renderer) {
        $renderer = $PAGE->get_renderer('mod_oublog');
    }
    // Setup My Participation capability check.
    $curgroup = oublog_get_activity_group($cm);
    $canview = oublog_can_view_participation($course, $oublog, $cm, $curgroup);
    if ($oublog->global) {
        $currentindividual = $globalindividual;
    }
    // Dont show the 'block' if user cant participate.
    if ($oublog->global && $currentindividual != $USER->id || $oublog->individual > OUBLOG_NO_INDIVIDUAL_BLOGS && $currentindividual != $USER->id) {
        return;
    }
    if (!$oublog->global && $canview == OUBLOG_NO_PARTICIPATION) {
        return;
    }
    $context = context_module::instance($cm->id);
    // Get the participation object containing User, Posts and Comments.
    $participation = oublog_get_user_participation($oublog, $context, $USER->id, $curgroup, $cm, $course);
    // Generate content data to send to renderer.
    $maintitle = get_string('myparticipation', 'oublog');
    // The title of the block 'section'.
    $content = '';
    $postedcount = $commentedcount = $commenttotal = 0;
    $postshow = 8;
    $postscount = count($participation->posts);
    if (count($participation->comments) <= 4) {
        $commenttotal = count($participation->comments);
    } else {
        $commenttotal = 4;
    }
    if (!$participation->posts) {
        $content .= html_writer::tag('p', get_string('nouserposts', 'oublog'));
    } else {
        $percent = $stat = null;
        $content .= html_writer::tag('h3', get_string('numberposts', 'oublog', count($participation->posts)));
        foreach ($participation->posts as $post) {
            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');
            $label .= html_writer::div(oublog_date($post->timeposted), 'oublogstats_commentposts_blogname');
            $statinfo = new oublog_statsinfo($participation->user, $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) {
        $content .= html_writer::tag('p', get_string('numberpostsmore', 'oublog', $postspluscount));
    }
    if (!$participation->comments) {
        $content .= html_writer::tag('p', get_string('nousercomments', 'oublog'));
    } else {
        $percent = $stat = null;
        // Removing all stats div.
        $content .= html_writer::tag('h3', get_string('numbercomments', 'oublog', count($participation->comments)));
        foreach ($participation->comments as $comment) {
            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');
            $label .= html_writer::div(oublog_date($comment->timeposted), 'oublogstats_commentposts_blogname');
            $statinfo = new oublog_statsinfo($participation->user, $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, 'user' => $participation->user->id);
    $url = new moodle_url('/mod/oublog/userparticipation.php', $params);
    $viewmyparticipation = html_writer::link($url, get_string('viewmyparticipation', 'oublog'));
    $content .= html_writer::start_tag('div', array('class' => 'oublog-post-content'));
    $content .= html_writer::tag('h3', $viewmyparticipation, array('class' => 'oublog-post-title'));
    $content .= html_writer::end_tag('div');
    return $renderer->render_stats_view('myparticipation', $maintitle, $content, null, null, null, false);
}
// set up whether the group selector should display
$showgroupselector = true;
if ($oublog->individual) {
    // if separate individual and visible group, do not show groupselector
    // unless the current user has permission
    if ($oublog->individual == OUBLOG_SEPARATE_INDIVIDUAL_BLOGS && !has_capability('mod/oublog:viewindividual', $context)) {
        $showgroupselector = false;
    }
}
// All enrolled users for table pagination.
$coursecontext = context_course::instance($course->id);
// If data has been received from this form.
$curgroup = -1;
if ($cm->groupmode > NOGROUPS) {
    // Get currently viewed group.
    $curgroup = optional_param('curgroup', oublog_get_activity_group($cm), PARAM_INT);
}
// Create time filter options form.
$default = get_user_preferences('mod_oublog_postformfilter', OUBLOG_STATS_TIMEFILTER_ALL);
// Create time filter options form.
$customdata = array('type' => 'participation', 'cmid' => $cm->id, 'user' => $USER->id, 'group' => $groupid, 'download' => $download, 'page' => $page, 'startyear' => $course->startdate, 'params' => array('curgroup', $curgroup));
$timefilter = new oublog_participation_timefilter_form(null, $customdata);
// If data has been received from this form.
$start = $end = 0;
$info = 'The info needs to be provided';
if ($submitted = $timefilter->get_data()) {
    if ($submitted->start) {
        $start = strtotime('00:00:00', $submitted->start);
    }
    if ($submitted->end) {
        $end = strtotime('23:59:59', $submitted->end);