Skip to content

Commit

Permalink
OUBlog: Allow tag cloud to be sorted #10876
Browse files Browse the repository at this point in the history
  • Loading branch information
athompson697 authored and sammarshallou committed Jul 29, 2014
1 parent 8b2b4f7 commit 5711cc6
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 19 deletions.
76 changes: 72 additions & 4 deletions internaldoc/testcase.basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CRE01 / admin.
Enter test course and turn editing on.
Create a blog called CRE01 which is a whole-course blog (no groups) and
visible to anyone in the world.
Set 'Allow comments' to Comments not allowed.

CRE02 / admin.
Enter test course and turn editing on.
Expand Down Expand Up @@ -99,18 +100,19 @@ TAG01 / admin
Enter blog 'CRE01'.
Select the 'New blog post' button.
Enter 'TAG1' into the title field.
Enter some text into message field and "tag1, tag2" into the tags field.
Enter some text into message field and "tag1, tag2, btag2" into the tags field.
Select 'Add post' button to save the new post and return to main blog view.
-- Verify 'tag1 (1)' and 'tag2 (1)' are now shown in the 'tags' tag cloud.
-- Verify 'btag2 (1)', 'tag1 (1)' and 'tag2 (1)' are now shown in the 'tags' tag cloud.

TAG02 / admin
Select 'edit' link for post TAG1 to edit the post just created.
Select the tags field.
-- Verify no tag choices are shown on field focus.
Delete all text in the tags field.
-- Verify tag choice 'tag1 [1 posts]' and 'tag2 [1 posts]' is shown.
-- Verify tag choice 'btag2 [1 posts]', 'tag1 [1 posts]' and 'tag2 [1 posts]' is shown.
Select 'tag1 [1posts]'
-- Verify tags field now contains 'tag1, ' and tag choice is now showing as 'tag2 [1 posts]'.
-- Verify tags field now contains 'tag1,' and tag choice is now showing as
'btag2 [1 posts]', 'tag2 [1 posts]'.

TAG03 / admin
Enter blog 'CRE02'.
Expand All @@ -137,6 +139,72 @@ TAG04 / student [change]
Select the tags field.
-- Verify no tag choices are shown on field focus.

TAG Testing post tag order
==========================

TAG05 / admin
Enter blog 'CRE01'.
-- Cofirm that the 'tags' tag cloud contains the label 'Order:' followed by a help icon and
that beneath this is a label 'Alphabetical', the default order, followed by a
verticle divider and link 'Use' above the existing tags 'btag2 (1)', 'tag1 (1)' and 'tag2 (1)'.

TAG06 / admin
Click the help icon to display the tag cloud, order help text.
-- Confirm it matches the following,
"You can choose to display the list of tags used ordered either in alphabetical order
or by number of posts used in. Select the two links to switch between ordering methods,
this choice is remembered and will be used on subsequent views."

TAG07 / admin
Edit the first 'untitled post' and add the tags 'atag1, btag2'.
Select 'Save changes'.
-- Verify 'atag1(1) btag2(2) tag1 (1) tag2 (1)' are now shown
in the 'tags' tag cloud, possibly with btags in larger emboldend text.

TAG08 / admin
Edit the COM1 post and add the tags 'atag1, btag2'.
Select 'Save changes'.
-- Verify 'atag1 (2) btag2 (3) tag1 (1) tag2 (1) ' are now shown in the 'tags' tag cloud,
with btag2 and atag1 in larger emboldend text.

TAG09 / admin
Edit the TAG1 post and add the tags 'atag1, ctag3'.
Select 'Save changes'.
-- Verify 'atag1 (3) btag2 (3) ctag3 (1) tag1 (1) tag2 (1)' are now shown in the 'tags' tag cloud,
with btag2 and atag1 in larger emboldend text.

TAG10 / admin
Select the 'New blog post' button.
Enter 'TAG2' into the title field.
Enter some text into message field and "tag1, btag2" into the tags field.
Select 'Add post' button to save the new post and return to main blog view.
-- Verify that the tag order is alphabetical 'atag1 (3) btag2 (4) ctag3 (1) tag1 (2)
tag2 (1)' shown in the 'tags' tag cloud.

TAG11 / admin
Click the link 'Use' to change the order to popular use order.
-- Confirm that the 'Use' link becomes a label.
-- Confirm that the 'Alphabetical' label becomes a link.
-- Verify that 'btag2 (4) atag1 (3) tag1 (2) tag2 (1) ctag3 (1)' are now shown
in the 'tags' tag cloud, possibly with some tags in larger emboldend text.
Retrun to the course main page.
Re enter blog CRE01
-- Confirm that the link is currently 'Alphabetical'.
-- Confirm that the tag order is still popular use order 'btag2 (4) atag1 (3) tag1 (1)
tag2 (1) ctag3 (1)' as before.

TAG12 / admin
Click the link 'Alphabetical' to change the current order to alphabetical order.
-- Confirm that 'Use' becomes the link.
-- Verify that tag cloud now shows 'atag1 (3) btag2 (4) ctag3 (1) tag1 (2) tag2 (1)'
possibly with some tags in larger emboldend text.
Return to the course main page.
Re enter blog CRE01
-- Confirm that the link is currently 'USE'.
-- Confirm that the tag order is alphabetical 'atag1 (3) btag2 (4) ctag3 (1) tag1 (2)
tag2 (1)' as before.


PAG Testing blog display pagination.
===================================

Expand Down
7 changes: 7 additions & 0 deletions lang/en/oublog.php
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,13 @@
$string['viewmyparticipation'] = 'View my participation';
$string['viewallparticipation'] = 'View all participation';
$string['timestartenderror'] = 'Selection end date cannot be earlier than the start date';
$string['order'] = 'Order:';
$string['alpha'] = 'Alphabetical';
$string['use'] = 'Use';
$string['order_help'] = 'You can choose to display the list of tags used ordered
either in alphabetical order or by number of posts used in.
Select the two links to switch between ordering methods,
this choice is remembered and will be used on subsequent views.';
$string['nouserpostpartsfound'] = 'No posts made in this period.';
$string['nousercommentpartsfound'] = 'No comments added in this period.';
$string['participation_all'] = 'Participation - All time';
Expand Down
15 changes: 8 additions & 7 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ function oublog_get_tags_csv($postid) {
* @param int $oubloginstanceid
* @return array Tag data
*/
function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $individualid=-1) {
function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $individualid=-1, $tagorder = 'alpha') {
global $CFG, $DB, $USER;
$tags = array();
$params = array();
Expand Down Expand Up @@ -969,9 +969,11 @@ function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $indivi
foreach ($tags as $idx => $tag) {
$tags[$idx]->weight = round(($tag->count-$min)/$delta*4);
}
uasort($tags, function($a, $b) {
return strcmp ($a->tag, $b->tag);
});
if ($tagorder == 'alpha') {
uasort($tags, function($a, $b) {
return strcmp ($a->tag, $b->tag);
});
}
}
return($tags);
}
Expand All @@ -988,13 +990,12 @@ function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $indivi
* @param int $oubloginstanceid
* @return string Tag cloud HTML
*/
function oublog_get_tag_cloud($baseurl, $oublog, $groupid, $cm, $oubloginstanceid=null, $individualid=-1) {
function oublog_get_tag_cloud($baseurl, $oublog, $groupid, $cm, $oubloginstanceid=null, $individualid=-1, $tagorder) {
$cloud = '';
$urlparts= array();

$baseurl = oublog_replace_url_param($baseurl, 'tag');

if (!$tags = oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid, $individualid)) {
if (!$tags = oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid, $individualid, $tagorder)) {
return($cloud);
}

Expand Down
25 changes: 25 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,31 @@ public function render_oublog_print_delete_dialog($cmid, $postid) {
$PAGE->requires->js_init_call('M.mod_oublog.init_deleteandemail', array($cmid, $postid), true, $jsmodule);
}

/**
* Renders the ordering label, help and links in tags block
* @param string $selected
* @return html
*/
public function render_tag_order($selected) {
global $PAGE, $OUTPUT;

$output = '';
$strorder = get_string('order', 'oublog');
$stralpha = get_string('alpha', 'oublog');
$struse = get_string('use', 'oublog');
$output .= html_writer::start_tag('div', array('class' => 'oublog-tag-order'));
$output .= $strorder . $OUTPUT->help_icon('order', 'oublog');
if ($selected == 'use') {
$burl = new moodle_url($PAGE->url, array('tagorder' => 'alpha'));
$output .= " " . html_writer::link($burl, $stralpha) . " | " . $struse;
} else {
$burl = new moodle_url($PAGE->url, array('tagorder' => 'use'));
$output .= " " . $stralpha . " | " . html_writer::link($burl, $struse);
}
$output .= html_writer::end_tag('div');
return $output;
}

}

class oublog_statsinfo implements renderable {
Expand Down
4 changes: 4 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,7 @@ div.tagselector_result .tagselector_result_info {
.yui3-aclist-item-active div.tagselector_result .tagselector_result_info {
color: black;
}

.oublog-tag-order {
margin-bottom: .5em;
}
93 changes: 90 additions & 3 deletions tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,14 @@ public function test_oublog_get_posts_pagination() {
}

public function test_oublog_tags() {
global $USER, $DB;
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.
Expand All @@ -559,7 +559,10 @@ public function test_oublog_tags() {
$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);
Expand All @@ -574,14 +577,98 @@ public function test_oublog_tags() {
$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, $stud1->id);
$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.
}

}
26 changes: 21 additions & 5 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
$username = optional_param('u', '', PARAM_USERNAME);// User login name.
$tag = optional_param('tag', null, PARAM_TAG); // Tag to display.
$page = optional_param('page', 0, PARAM_INT);
$tagorder = optional_param('tagorder', '', PARAM_ALPHA);// Tag display order.

// Set user value if u (username) set.
if ($username != '') {
Expand All @@ -38,10 +39,23 @@
}
$user = $oubloguser->id;
}

if (isloggedin()) {
// Determine tag order to use.
if ($tagorder != '') {
set_user_preference('oublog_tagorder', $tagorder);
} else {
$tagorder = get_user_preferences('oublog_tagorder', 'alpha');
}
} else {
// Use 'alpha'
$tagorder = 'alpha';
}

$offset = $page * OUBLOG_POSTS_PER_PAGE;
$url = new moodle_url('/mod/oublog/view.php', array('id'=>$id, 'user'=>$user,
'page' => $page,
'tag'=>$tag));
$url = new moodle_url('/mod/oublog/view.php', array('id' => $id, 'user' => $user,
'page' => $page, 'tag' => $tag, 'tagorder' => $tagorder));

$PAGE->set_url($url);

if ($id) {
Expand Down Expand Up @@ -277,12 +291,14 @@
$PAGE->blocks->add_fake_block($bc, BLOCK_POS_RIGHT);

// Tag Cloud.
if ($tags = oublog_get_tag_cloud($returnurl, $oublog, $currentgroup, $cm, $oubloginstanceid, $currentindividual)) {
if ($tags = oublog_get_tag_cloud($returnurl, $oublog, $currentgroup, $cm,
$oubloginstanceid, $currentindividual, $tagorder)) {
$bc = new block_contents();
$bc->attributes['id'] = 'oublog-tags';
$bc->attributes['class'] = 'oublog-sideblock block';
$bc->title = $strtags;
$bc->content = $tags;
$bc->content = $oublogoutput->render_tag_order($tagorder);
$bc->content .= $tags;
$PAGE->blocks->add_fake_block($bc, BLOCK_POS_RIGHT);
}

Expand Down

0 comments on commit 5711cc6

Please sign in to comment.