- debugging('Function coursetag_get_tags() is deprecated. Userid is no longer used for tagging courses.', DEBUG_DEVELOPER);
-
- global $CFG, $DB;
-
- // get visible course ids
- $courselist = array();
- if ($courseid === 0) {
- if ($courses = $DB->get_records_select('course', 'visible=1 AND category>0', null, '', 'id')) {
- foreach ($courses as $key => $value) {
- $courselist[] = $key;
- }
- }
- }
-
- // get tags from the db ordered by highest count first
- $params = array();
- $sql = "SELECT id as tkey, name, id, isstandard, rawname, f.timemodified, flag, count
- FROM {tag} t,
- (SELECT tagid, MAX(timemodified) as timemodified, COUNT(id) as count
- FROM {tag_instance}
- WHERE itemtype = 'course' ";
-
- if ($courseid > 0) {
- $sql .= " AND itemid = :courseid ";
- $params['courseid'] = $courseid;
- } else {
- if (!empty($courselist)) {
- list($usql, $uparams) = $DB->get_in_or_equal($courselist, SQL_PARAMS_NAMED);
- $sql .= "AND itemid $usql ";
- $params = $params + $uparams;
- }
- }
-
- if ($userid > 0) {
- $sql .= " AND tiuserid = :userid ";
- $params['userid'] = $userid;
- }
-
- $sql .= " GROUP BY tagid) f
- WHERE t.id = f.tagid ";
- if ($tagtype != '') {
- $sql .= "AND isstandard = :isstandard ";
- $params['isstandard'] = ($tagtype === 'official') ? 1 : 0;
- }
- $sql .= "ORDER BY count DESC, name ASC";
-
- // limit the number of tags for output
- if ($numtags == 0) {
- $tags = $DB->get_records_sql($sql, $params);
- } else {
- $tags = $DB->get_records_sql($sql, $params, 0, $numtags);
- }
-
- // prepare the return
- $return = array();
- if ($tags) {
- // avoid print_tag_cloud()'s ksort upsetting ordering by setting the key here
- foreach ($tags as $value) {
- $return[] = $value;
- }
- }
-
- return $return;
-