Skip to content

Commit

Permalink
MDL-39800 tags: moved cloud sorting to print function
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenny Gray authored and Damyon Wiese committed Aug 20, 2013
1 parent 5ea6ac9 commit 740c4ad
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 81 deletions.
6 changes: 3 additions & 3 deletions blocks/tags/block_tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ public function get_content() {
$content = '';
$moretags = new moodle_url('/tag/coursetags_more.php', array('show'=>$tagtype));
if ($tagtype == 'all') {
$tags = coursetag_get_tags(0, 0, $this->config->tagtype, $this->config->numberoftags, 'popularity');
$tags = coursetag_get_tags(0, 0, $this->config->tagtype, $this->config->numberoftags);
} else if ($tagtype == 'course') {
$tags = coursetag_get_tags($this->page->course->id, 0, $this->config->tagtype, $this->config->numberoftags, 'popularity');
$tags = coursetag_get_tags($this->page->course->id, 0, $this->config->tagtype, $this->config->numberoftags);
$moretags->param('courseid', $this->page->course->id);
} else if ($tagtype == 'my') {
$tags = coursetag_get_tags(0, $USER->id, $this->config->tagtype, $this->config->numberoftags, 'popularity');
$tags = coursetag_get_tags(0, $USER->id, $this->config->tagtype, $this->config->numberoftags);
}
$tagcloud = tag_print_cloud($tags, 150, true);
if (!$tagcloud) {
Expand Down
37 changes: 10 additions & 27 deletions tag/coursetags_more.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,37 +103,20 @@
$myurl = $CFG->wwwroot.'/tag/coursetags_more.php';
$myurl2 = $CFG->wwwroot.'/tag/coursetags_more.php?show='.$show;

// Set up sort order global
$oldsort = $CFG->tagsort;
if ($sort == 'popularity') {
$CFG->tagsort = 'count';
} else if ($sort == 'date') {
$CFG->tagsort = 'timemodified';
} else {
$CFG->tagsort = 'name';
}

// Course tags
if ($show == 'course' and $courseid) {
$tags = tag_print_cloud(coursetag_get_tags($courseid, 0, '', 0, 'popularity'), 150, true);
// My tags
} else if ($show == 'my' and $loggedin) {
$tags = tag_print_cloud(coursetag_get_tags(0, $USER->id, 'default', 0, 'popularity'), 150, true);
// Official course tags
} else if ($show == 'official') {
$tags = tag_print_cloud(coursetag_get_tags(0, 0, 'official', 0, 'popularity'), 150, true);
// Community (official and personal together) also called user tags
} else if ($show == 'community') {
$tags = tag_print_cloud(coursetag_get_tags(0, 0, 'default', 0, 'popularity'), 150, true);
// All tags for courses and blogs and any thing else tagged - the fallback default ($show == all)
if ($show == 'course' and $courseid) { // Course tags.
$tags = tag_print_cloud(coursetag_get_tags($courseid, 0, ''), 150, true, $sort);
} else if ($show == 'my' and $loggedin) { // My tags.
$tags = tag_print_cloud(coursetag_get_tags(0, $USER->id, 'default'), 150, true, $sort);
} else if ($show == 'official') { // Official course tags.
$tags = tag_print_cloud(coursetag_get_tags(0, 0, 'official'), 150, true, $sort);
} else if ($show == 'community') { // Community (official and personal together) also called user tags.
$tags = tag_print_cloud(coursetag_get_tags(0, 0, 'default'), 150, true, $sort);
} else {
// All tags for courses and blogs and any thing else tagged - the fallback default ($show == all).
$subtitle = $showalltags;
$tags = tag_print_cloud(coursetag_get_all_tags('popularity'), 150, true);
$tags = tag_print_cloud(coursetag_get_all_tags(), 150, true, $sort);
}

// Reinstate original sort order global
$CFG->tagsort = $oldsort;

// Prepare the links for the show and order lines
if ($show == 'all') {
$link1 .= '<b>'.$showalltags.'</b>';
Expand Down
54 changes: 4 additions & 50 deletions tag/coursetagslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
* @param string $tagtype (optional) The type of tag, empty string returns all types. Currently (Moodle 2.2) there are two
* types of tags which are used within Moodle, they are 'official' and 'default'.
* @param int $numtags (optional) number of tags to display, default of 80 is set in the block, 0 returns all
* @param string $sort (optional) selected sorting, default is alpha sort (name) also timemodified or popularity
* @param string $unused (optional) was selected sorting, moved to tag_print_cloud()
* @return array
*/
function coursetag_get_tags($courseid, $userid=0, $tagtype='', $numtags=0, $sort='name') {
function coursetag_get_tags($courseid, $userid=0, $tagtype='', $numtags=0, $unused = '') {

global $CFG, $DB;

Expand Down Expand Up @@ -96,11 +96,6 @@ function coursetag_get_tags($courseid, $userid=0, $tagtype='', $numtags=0, $sort
// prepare the return
$return = array();
if ($tags) {
// sort the tag display order
if ($sort != 'popularity') {
$CFG->tagsort = $sort;
usort($tags, "coursetag_sort");
}
// avoid print_tag_cloud()'s ksort upsetting ordering by setting the key here
foreach ($tags as $value) {
$return[] = $value;
Expand All @@ -117,11 +112,11 @@ function coursetag_get_tags($courseid, $userid=0, $tagtype='', $numtags=0, $sort
*
* @package core_tag
* @category tag
* @param string $sort (optional) selected sorting, default is alpha sort (name) also timemodified or popularity
* @param string $unused (optional) was selected sorting - moved to tag_print_cloud()
* @param int $numtags (optional) number of tags to display, default of 20 is set in the block, 0 returns all
* @return array
*/
function coursetag_get_all_tags($sort='name', $numtags=0) {
function coursetag_get_all_tags($unused='', $numtags=0) {

global $CFG, $DB;

Expand All @@ -145,10 +140,6 @@ function coursetag_get_all_tags($sort='name', $numtags=0) {

$return = array();
if ($tags) {
if ($sort != 'popularity') {
$CFG->tagsort = $sort;
usort($tags, "coursetag_sort");
}
foreach ($tags as $value) {
$return[] = $value;
}
Expand All @@ -157,43 +148,6 @@ function coursetag_get_all_tags($sort='name', $numtags=0) {
return $return;
}

/**
* Sorting callback function for coursetag_get_tags() and coursetag_get_all_tags() only
*
* This function does a comparision on a field withing two variables, $a and $b. The field used is specified by
* $CFG->tagsort or we just use the 'name' field if $CFG->tagsort is empty. The comparison works as follows:
* If $a->$tagsort is greater than $b->$tagsort, 1 is returned.
* If $a->$tagsort is equal to $b->$tagsort, 0 is returned.
* If $a->$tagsort is less than $b->$tagsort, -1 is returned.
*
* Also if $a->$tagsort is not numeric or a string, 0 is returned.
*
* @package core_tag
* @access private
* @param int|string|mixed $a Variable to compare against $b
* @param int|string|mixed $b Variable to compare against $a
* @return int The result of the comparison/validation 1, 0 or -1
*/
function coursetag_sort($a, $b) {
// originally from block_blog_tags
global $CFG;

// set up the variable $tagsort as either 'name' or 'timemodified' only, 'popularity' does not need sorting
if (empty($CFG->tagsort)) {
$tagsort = 'name';
} else {
$tagsort = $CFG->tagsort;
}

if (is_numeric($a->$tagsort)) {
return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
} else if (is_string($a->$tagsort)) {
return strcmp($a->$tagsort, $b->$tagsort);
} else {
return 0;
}
}

/**
* Returns javascript for use in tags block and supporting pages
*
Expand Down
15 changes: 14 additions & 1 deletion tag/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
* @param array $tagset Array of tags to display
* @param int $nr_of_tags Limit for the number of tags to return/display, used if $tagset is null
* @param bool $return if true the function will return the generated tag cloud instead of displaying it.
* @param string $sort (optional) selected sorting, default is alpha sort (name) also timemodified or popularity
* @return string|null a HTML string or null if this function does the output
*/
function tag_print_cloud($tagset=null, $nr_of_tags=150, $return=false) {
function tag_print_cloud($tagset=null, $nr_of_tags=150, $return=false, $sort='') {
global $CFG, $DB;

$can_manage_tags = has_capability('moodle/tag:manage', context_system::instance());
Expand Down Expand Up @@ -69,7 +70,19 @@ function tag_print_cloud($tagset=null, $nr_of_tags=150, $return=false) {
$etags[] = $tag;
}

// Set up sort global - used to pass sort type into tag_cloud_sort through usort() avoiding multiple sort functions.
// TODO make calling functions pass 'count' or 'timemodified' not 'popularity' or 'date'.
$oldsort = empty($CFG->tagsort) ? null : $CFG->tagsort;
if ($sort == 'popularity') {
$CFG->tagsort = 'count';
} else if ($sort == 'date') {
$CFG->tagsort = 'timemodified';
} else {
$CFG->tagsort = 'name';
}
usort($etags, "tag_cloud_sort");
$CFG->tagsort = $oldsort;

$output = '';
$output .= "\n<ul class='tag_cloud inline-list'>\n";
foreach ($etags as $tag) {
Expand Down
8 changes: 8 additions & 0 deletions tag/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
This files describes API changes in tagging, information provided
here is intended especially for developers.

=== 2.6 ===

More cleanup was done to tag cloud sorting which involved some API changes, see MDL_39800
* tag_print_cloud() arguments were changed.
* coursetag_get_tags() arguments were changed.
* coursetag_get_all_tags() arguments were changed.
* coursetag_sort() was removed.

=== 2.4 ===

Significant cleanup was done to course tags which involved some API
Expand Down

0 comments on commit 740c4ad

Please sign in to comment.