Exemplo n.º 1
0
/**
 * Gets tags available to choose from
 * @param object $oublog
 * @param int $groupid
 * @param object $cm
 * @param int $oubloginstanceid
 * @param int $individualid
 * @return array of tag objects
 */
function oublog_get_tag_list($oublog, $groupid, $cm, $oubloginstanceid = null, $individualid = -1)
{
    global $DB;
    $tags = oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid, $individualid, 'alpha');
    $blogtags = oublog_clarify_tags($oublog->tags);
    // For each tag added to the blog check if it is already in use
    // in the post, if it is then the 'Official' label is added to it.
    $existingtagnames = array();
    foreach ($tags as $idx => $tag) {
        if (in_array($tags[$idx]->tag, $blogtags)) {
            $tag->label = get_string('official', 'oublog');
            // Flat array of existing in use 'Set' tags.
            $existingtagnames[] = $tags[$idx]->tag;
        } else {
            if ($oublog->restricttags == 1 || $oublog->restricttags == 3) {
                // If we are restricting, remove this non-offical tag.
                unset($tags[$idx]);
            }
        }
    }
    // For each 'Official' tag added, if it is NOT already in use,
    // then add it to the list of tags.
    foreach ($blogtags as $blogtag) {
        if (!in_array($blogtag, $existingtagnames)) {
            $tagobject = (object) array('tag' => $blogtag);
            $tagobject->label = get_string('official', 'oublog');
            $tagobject->count = 0;
            $tags[] = $tagobject;
        }
    }
    return $tags;
}
 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.
 }
/**
 * Print a tag cloud for a given blog or blog instance
 *
 * @param string $baseurl
 * @param int $oublogid
 * @param int $groupid
 * @param object $cm
 * @param int $oubloginstanceid
 * @return string Tag cloud HTML
 */
function oublog_get_tag_cloud($baseurl, $oublog, $groupid, $cm, $oubloginstanceid = null, $individualid = -1)
{
    $cloud = '';
    $urlparts = array();
    $baseurl = oublog_replace_url_param($baseurl, 'tag');
    if (!($tags = oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid, $individualid))) {
        return $cloud;
    }
    foreach ($tags as $tag) {
        $cloud .= '<a href="' . $baseurl . '&amp;tag=' . urlencode($tag->tag) . '" class="oublog-tag-cloud-' . $tag->weight . '"><span class="oublog-tagname">' . strtr($tag->tag, array(' ' => '&nbsp;')) . '</span><span class="oublog-tagcount">(' . $tag->count . ')</span></a> ';
    }
    return $cloud;
}