Permalink
Browse files

MDL-39800 tags: moved cloud sorting to print function

  • Loading branch information...
1 parent bbec021 commit 4aafc00b1798a9fba476f870260fea1e4078f57f @jennymgray jennymgray committed Jul 29, 2013
Showing with 39 additions and 81 deletions.
  1. +3 −3 blocks/tags/block_tags.php
  2. +10 −27 tag/coursetags_more.php
  3. +4 −50 tag/coursetagslib.php
  4. +14 −1 tag/locallib.php
  5. +8 −0 tag/upgrade.txt
@@ -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) {
View
@@ -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>';
View
@@ -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;
@@ -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;
@@ -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;
@@ -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;
}
@@ -158,43 +149,6 @@ function coursetag_get_all_tags($sort='name', $numtags=0) {
}
/**
- * 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
*
* @package core_tag
View
@@ -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());
@@ -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) {
View
@@ -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

0 comments on commit 4aafc00

Please sign in to comment.