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
                $fromclause
                $whereclause
                GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.isstandard, tg.tagcollid
                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);
    }