Ejemplo n.º 1
0
/**
 * Prints or returns a HTML tag cloud with varying classes styles depending on the popularity and type of each tag.
 *
 * @deprecated since 3.1
 *
 * @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, $sort = '')
{
    global $OUTPUT;
    debugging('Function tag_print_cloud() is deprecated and replaced with function core_tag_collection::get_tag_cloud(), ' . 'templateable core_tag\\output\\tagcloud and template core_tag/tagcloud.', DEBUG_DEVELOPER);
    // Set up sort global - used to pass sort type into core_tag_collection::cloud_sort through usort() avoiding multiple sort functions.
    if ($sort == 'popularity') {
        $sort = 'count';
    } else {
        if ($sort == 'date') {
            $sort = 'timemodified';
        } else {
            $sort = 'name';
        }
    }
    if (is_null($tagset)) {
        // No tag set received, so fetch tags from database.
        // Always add query by tagcollid even when it's not known to make use of the table index.
        $tagcloud = core_tag_collection::get_tag_cloud(0, false, $nr_of_tags, $sort);
    } else {
        $tagsincloud = $tagset;
        $etags = array();
        foreach ($tagsincloud as $tag) {
            $etags[] = $tag;
        }
        core_tag_collection::$cloudsortfield = $sort;
        usort($tagsincloud, "core_tag_collection::cloud_sort");
        $tagcloud = new \core_tag\output\tagcloud($tagsincloud);
    }
    $output = $OUTPUT->render_from_template('core_tag/tagcloud', $tagcloud->export_for_template($OUTPUT));
    if ($return) {
        return $output;
    } else {
        echo $output;
    }
}
Ejemplo n.º 2
0
 /**
  * Returns the list of tags with number of items tagged
  *
  * @param int $tagcollid
  * @param null|bool $isstandard return only standard tags
  * @param int $limit maximum number of tags to retrieve, tags are sorted by the instance count
  *            descending here regardless of $sort parameter
  * @param string $sort sort order for display, default 'name' - tags will be sorted after they are retrieved
  * @param string $search search string
  * @param int $fromctx context id where this tag cloud is displayed
  * @param int $ctx only retrieve tag instances in this context
  * @param int $rec retrieve tag instances in the $ctx context and it's children (default 1)
  * @return \core_tag\output\tagcloud
  */
 public static function get_tag_cloud($tagcollid, $isstandard = false, $limit = 150, $sort = 'name', $search = '', $fromctx = 0, $ctx = 0, $rec = 1)
 {
     global $DB;
     $fromclause = 'FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid';
     $whereclause = 'WHERE ti.itemtype <> \'tag\'';
     list($sql, $params) = $DB->get_in_or_equal($tagcollid ? array($tagcollid) : array_keys(self::get_collections(true)));
     $whereclause .= ' AND tg.tagcollid ' . $sql;
     if ($isstandard) {
         $whereclause .= ' AND tg.isstandard = 1';
     }
     $context = $ctx ? context::instance_by_id($ctx) : context_system::instance();
     if ($rec && $context->contextlevel != CONTEXT_SYSTEM) {
         $fromclause .= ' JOIN {context} ctx ON ctx.id = ti.contextid ';
         $whereclause .= ' AND ctx.path LIKE ?';
         $params[] = $context->path . '%';
     } else {
         if (!$rec) {
             $whereclause .= ' AND ti.contextid = ?';
             $params[] = $context->id;
         }
     }
     if (strval($search) !== '') {
         $whereclause .= ' AND tg.name LIKE ?';
         $params[] = '%' . core_text::strtolower($search) . '%';
     }
     $tagsincloud = $DB->get_records_sql("SELECT tg.id, tg.rawname, tg.name, tg.isstandard, COUNT(ti.id) AS count, tg.flag, tg.tagcollid\n                {$fromclause}\n                {$whereclause}\n                GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.isstandard, tg.tagcollid\n                ORDER BY count DESC, tg.name ASC", $params, 0, $limit);
     $tagscount = count($tagsincloud);
     if ($tagscount == $limit) {
         $tagscount = $DB->get_field_sql("SELECT COUNT(DISTINCT tg.id) {$fromclause} {$whereclause}", $params);
     }
     self::$cloudsortfield = $sort;
     usort($tagsincloud, "self::cloud_sort");
     return new core_tag\output\tagcloud($tagsincloud, $tagscount, $fromctx, $ctx, $rec);
 }