public function test_oublog_tags()
 {
     global $USER, $DB, $SITE;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $course = $this->get_new_course();
     $stud1 = $this->get_new_user('student', $course->id);
     $group1 = $this->get_new_group($course->id);
     $group2 = $this->get_new_group($course->id);
     $this->get_new_group_member($group1->id, $stud1->id);
     // Whole course blog.
     $oublog = $this->get_new_oublog($course->id);
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $post = $this->get_post_stub($oublog->id);
     $post->tags = array('1', 'new', 'new', 'new2', 'a space');
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $this->assertEquals(21, strlen(oublog_get_tags_csv($postid)));
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1);
     $this->assertCount(4, $tags);
     foreach ($tags as $tag) {
         $this->assertEquals(1, $tag->count);
         $this->assertContains($tag->tag, $post->tags);
     }
     // Individual blog.
     $oublog = $this->get_new_oublog($course->id, array('individual' => OUBLOG_VISIBLE_INDIVIDUAL_BLOGS));
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $post = $this->get_post_stub($oublog->id);
     $post->tags = array('1', 'new', 'new', 'new2', 'a space');
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $tags = oublog_get_tags($oublog, 0, $cm, null, $USER->id);
     $this->assertCount(4, $tags);
     $tags = oublog_get_tags($oublog, 0, $cm, null, $stud1->id);
     $this->assertEmpty($tags);
     // Group blog.
     $oublog = $this->get_new_oublog($course->id, array('groupmode' => VISIBLEGROUPS));
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     $post = $this->get_post_stub($oublog->id);
     $post->groupid = $group1->id;
     $post->tags = array('1', 'new', 'new', 'new2', 'a space');
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $tags = oublog_get_tags($oublog, $group1->id, $cm);
     $this->assertCount(4, $tags);
     $tags = oublog_get_tags($oublog, $group2->id, $cm);
     $this->assertEmpty($tags);
     // Personal blog.
     if (!($oublog = $DB->get_record('oublog', array('global' => 1)))) {
         $oublog = $this->get_new_oublog($SITE->id, array('global' => 1, 'maxvisibility' => OUBLOG_VISIBILITY_PUBLIC));
     }
     $cm = get_coursemodule_from_instance('oublog', $oublog->id);
     list($oublog, $bloginstance) = oublog_get_personal_blog($stud1->id);
     $post1stub = $this->get_post_stub($oublog->id);
     $post1stub->userid = $stud1->id;
     $post1stub->visibility = OUBLOG_VISIBILITY_COURSEUSER;
     // Private.
     $post1stub->tags = array('private');
     oublog_add_post($post1stub, $cm, $oublog, $SITE);
     $post2stub = $this->get_post_stub($oublog->id);
     $post2stub->userid = $stud1->id;
     $post2stub->visibility = OUBLOG_VISIBILITY_LOGGEDINUSER;
     // User must be logged in.
     $post2stub->tags = array('loggedin');
     oublog_add_post($post2stub, $cm, $oublog, $SITE);
     $post3stub = $this->get_post_stub($oublog->id);
     $post3stub->userid = $stud1->id;
     $post3stub->visibility = OUBLOG_VISIBILITY_PUBLIC;
     // Any user.
     $post3stub->tags = array('public');
     oublog_add_post($post3stub, $cm, $oublog, $SITE);
     $tags = oublog_get_tags($oublog, 0, $cm, $bloginstance->id);
     $this->assertCount(2, $tags);
     $this->setUser($stud1);
     $tags = oublog_get_tags($oublog, 0, $cm, $bloginstance->id);
     $this->assertCount(3, $tags);
     $this->setGuestUser();
     $tags = oublog_get_tags($oublog, 0, $cm, $bloginstance->id);
     $this->assertCount(1, $tags);
     $this->setUser($stud1);
     $post4stub = $this->get_post_stub($oublog->id);
     $post4stub->userid = $stud1->id;
     $post4stub->visibility = OUBLOG_VISIBILITY_PUBLIC;
     // Any user.
     // Add unordered alphabetic tags.
     $post4stub->tags = array('toad', 'newt', 'private', 'crock', 'loggedin', 'frog', 'public', 'dino');
     $postid = oublog_add_post($post4stub, $cm, $oublog, $course);
     $this->assertEquals(56, strlen(oublog_get_tags_csv($postid)));
     $post5stub = $this->get_post_stub($oublog->id);
     $post5stub->userid = $stud1->id;
     $post5stub->visibility = OUBLOG_VISIBILITY_PUBLIC;
     // Any user.
     // Add unordered alphabetic tags.
     $post5stub->tags = array('toad', 'private', 'crock', 'loggedin', 'frog', 'public', 'dino');
     $postid = oublog_add_post($post5stub, $cm, $oublog, $course);
     $this->assertEquals(50, strlen(oublog_get_tags_csv($postid)));
     // Recover tags with default ordering ie Alphabetic.
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1);
     $this->assertCount(8, $tags);
     foreach ($tags as $tag) {
         $this->assertContains($tag->tag, $post4stub->tags);
     }
     $this->assertEquals('toad', $tag->tag);
     // Last in default, Alphabetical order.
     // Recover tags in Use order.
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1, 'use');
     $this->assertCount(8, $tags);
     $lasttag = end($tags);
     $this->assertEquals('newt', $lasttag->tag);
     // Last when Use order specified.
     // Recover tags in Alphabetical order.
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1, 'alpha');
     $this->assertCount(8, $tags);
     $lasttag = end($tags);
     $this->assertEquals('toad', $lasttag->tag);
     // Last when Alphabetic order specified.
     // Testing the create update of a blog instance with predefined tags.
     // set and also testing oublog_get_tag_list().
     // Whole course blog created with predefined tag set.
     $oublog = $this->get_new_oublog($course->id, array('tags' => 'blogtag01'));
     $cm = get_coursemodule_from_id('oublog', $oublog->cmid);
     // Check that the predefined tag is inserted to the oublog_tags table.
     $blogtags = oublog_clarify_tags($oublog->tags);
     foreach ($blogtags as $tag) {
         $predefinedtags[] = $DB->get_record('oublog_tags', array('tag' => $tag));
     }
     // Confirm finding one predefined tag in the oublog_tags table.
     $this->assertCount(1, $predefinedtags);
     // Change the predefined tags on the blog.
     $oublog->tags = 'blogtag01, blogtag02';
     $oublog->instance = $oublog->id;
     oublog_update_instance($oublog);
     // Check that the changed tags are inserted to the oublog_tags table.
     $blogtags = oublog_clarify_tags($oublog->tags);
     foreach ($blogtags as $tag) {
         $changedtags[] = $DB->get_record('oublog_tags', array('tag' => $tag));
     }
     // Confirm finding predefined tags in the oublog_tags table.
     $this->assertCount(2, $changedtags);
     // Create post with 1 pre-defined and 1 user tag.
     $post = $this->get_post_stub($oublog->id);
     $post->tags = array('antelope', 'blogtag01');
     $postid = oublog_add_post($post, $cm, $oublog, $course);
     $this->assertEquals(19, strlen(oublog_get_tags_csv($postid)));
     // Recover post tags in default order.
     $tags = oublog_get_tags($oublog, 0, $cm, null, -1);
     foreach ($tags as $tag) {
         $this->assertEquals(1, $tag->count);
         $this->assertContains($tag->tag, $post->tags);
     }
     $this->assertNotEquals('antelope', $tag->tag);
     $this->assertEquals('blogtag01', $tag->tag);
     // Last in Alphabetical order.
     // Testing 'Set' tag only restrictions.
     // No restriction, recover full list of blog 'Set' and post tags.
     $oublog->restricttags = 0;
     $taglist = oublog_get_tag_list($oublog, 0, $cm, null, -1);
     $this->assertCount(3, $taglist);
     foreach ($taglist as $tag) {
         $fulltaglist[] = $tag->tag;
         if (isset($tag->label)) {
             // It should be an 'Official' ie. 'Set' predefined blog tag.
             $this->assertContains($tag->tag, $oublog->tags);
             $this->assertNotEmpty($tag->label);
             $this->assertGreaterThanOrEqual(0, $tag->count);
         }
         if (!isset($tag->label)) {
             // It should be the user post tag.
             $this->assertContains($tag->tag, $post->tags);
             $this->assertEquals('antelope', $tag->tag);
             $this->assertEquals(1, $tag->count);
         }
     }
     $this->assertContains('antelope', $fulltaglist);
     $this->assertContains('blogtag01', $fulltaglist);
     $this->assertContains('blogtag02', end($fulltaglist));
     // Last in full list.
     // Restriction applied, get restricted list of blog 'Set' tags.
     $oublog->restricttags = 1;
     $taglist = oublog_get_tag_list($oublog, 0, $cm, null, -1);
     $this->assertCount(2, $taglist);
     foreach ($taglist as $tag) {
         $restrictedtaglist[] = $tag->tag;
         if (isset($tag->label)) {
             // It should be an 'Official' ie. 'Set' predefined blog tag.
             $this->assertContains($tag->tag, $oublog->tags);
             $this->assertNotEmpty($tag->label);
             $this->assertGreaterThanOrEqual(0, $tag->count);
         }
         if (!isset($tag->label)) {
             $this->assertEmpty($tag->id);
             $this->assertEmpty($tag->weight);
             $this->assertEquals(0, $tag->count);
         }
     }
     $this->assertNotContains('antelope', $restrictedtaglist);
     $this->assertContains('blogtag01', $restrictedtaglist);
     $this->assertContains('blogtag02', end($restrictedtaglist));
     // Last in restricted list.
 }
// Set-up groups.
$currentgroup = oublog_get_activity_group($cm, true);
$groupmode = oublog_get_activity_groupmode($cm, $course);
if ($groupmode == VISIBLEGROUPS && !groups_is_member($currentgroup) && !$oublog->individual) {
    require_capability('moodle/site:accessallgroups', $context);
}
// Setup tag list call.
$curindividual = -1;
$curgroup = false;
if ($oublog->individual) {
    $curindividual = isset($oubloginstance->userid) ? $oubloginstance->userid : $USER->id;
} else {
    $curgroup = isset($post->groupid) ? $post->groupid : $currentgroup;
}
// Moved call to oublog_get_tag_list() here.
$tags = oublog_get_tag_list($oublog, $curgroup, $cm, $oublog->global ? $oubloginstance->id : null, $curindividual);
$mform = new mod_oublog_post_form('editpost.php', array('individual' => $oublog->individual, 'maxvisibility' => $oublog->maxvisibility, 'allowcomments' => $oublog->allowcomments, 'edit' => !empty($postid), 'personal' => $oublog->global, 'maxbytes' => $oublog->maxbytes, 'maxattachments' => $oublog->maxattachments, 'restricttags' => $oublog->restricttags, 'availtags' => $tags, 'referurl' => $referurl));
if ($mform->is_cancelled()) {
    redirect($viewurl);
    exit;
}
if (!($frmpost = $mform->get_data())) {
    if ($postid) {
        $post->post = $post->id;
        $post->general = $streditpost;
        $post->tags = oublog_get_tags_csv($post->id);
        // Add a trailing comma for autocompletion support.
        if (!empty($post->tags)) {
            $post->tags .= ', ';
        }
    } else {