MDL-14679 towards tags conversion

This commit is contained in:
skodak 2008-05-31 15:09:25 +00:00
parent 7f008c0502
commit f7ed268752
3 changed files with 167 additions and 138 deletions

View File

@ -174,10 +174,12 @@ function tag_set_delete($record_type, $record_id, $tag) {
* @return true on success, false otherwise
*/
function tag_type_set($tagid, $type) {
if ($tag = get_record('tag', 'id', $tagid, '', '', '', '', 'id')) {
$tag->tagtype = addslashes($type);
global $DB;
if ($tag = $DB->get_record('tag', array('id'=>$tagid), 'id')) {
$tag->tagtype = $type;
$tag->timemodified = time();
return update_record('tag', $tag);
return $DB->update_record('tag', $tag);
}
return false;
}
@ -192,11 +194,13 @@ function tag_type_set($tagid, $type) {
* @return true on success, false otherwise
*/
function tag_description_set($tagid, $description, $descriptionformat) {
if ($tag = get_record('tag', 'id', $tagid, '', '', '', '', 'id')) {
$tag->description = addslashes($description);
$tag->descriptionformat = addslashes($descriptionformat);
global $DB;
if ($tag = $DB->get_record('tag', array('id'=>$tagid),'id')) {
$tag->description = $description;
$tag->descriptionformat = $descriptionformat;
$tag->timemodified = time();
return update_record('tag', $tag);
return $DB->update_record('tag', $tag);
}
return false;
}
@ -218,10 +222,12 @@ function tag_description_set($tagid, $description, $descriptionformat) {
*
**/
function tag_get($field, $value, $returnfields='id, name, rawname') {
global $DB;
if ($field == 'name') {
$value = addslashes(moodle_strtolower($value)); // To cope with input that might just be wrong case
$value = moodle_strtolower($value); // To cope with input that might just be wrong case
}
return get_record('tag', $field, $value, '', '', '', '', $returnfields);
return $DB->get_record('tag', array($field=>$value), $returnfields);
}
@ -236,32 +242,34 @@ function tag_get($field, $value, $returnfields='id, name, rawname') {
* @return array the array of tags
*/
function tag_get_tags($record_type, $record_id, $type=null) {
global $CFG;
global $CFG, $DB;
$params = array();
if ($type) {
$type = "AND tg.tagtype = '$type'";
$type = "AND tg.tagtype = :type";
$params['type'] = $type;
} else {
$type = '';
}
$sql = "SELECT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering
FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid
WHERE ti.itemtype = :recordtype AND ti.itemid = :recordid $type
ORDER BY ti.ordering ASC";
$params['recordtype'] = $record_type;
$params['recordid'] = $record_id;
// if the fields in this query are changed, you need to do the same changes in tag_get_correlated_tags
$tags = get_records_sql("SELECT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering ".
"FROM {$CFG->prefix}tag_instance ti INNER JOIN {$CFG->prefix}tag tg ON tg.id = ti.tagid ".
"WHERE ti.itemtype = '{$record_type}' AND ti.itemid = '{$record_id}' {$type} ".
"ORDER BY ti.ordering ASC");
return $DB->get_records_sql($sql, $params);
// This version of the query, reversing the ON clause, "correctly" returns
// a row with NULL values for instances that are still in the DB even though
// the tag has been deleted. This shouldn't happen, but if it did, using
// this query could help "clean it up". This causes bugs at this time.
//$tags = get_records_sql("SELECT ti.tagid, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering ".
//$tags = $DB->get_records_sql("SELECT ti.tagid, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering ".
// "FROM {$CFG->prefix}tag_instance ti LEFT JOIN {$CFG->prefix}tag tg ON ti.tagid = tg.id ".
// "WHERE ti.itemtype = '{$record_type}' AND ti.itemid = '{$record_id}' {$type} ".
// "ORDER BY ti.ordering ASC");
if (!$tags) {
return array();
} else {
return $tags;
}
}
/**
@ -343,7 +351,8 @@ function tag_get_tags_ids($record_type, $record_id) {
* second parameter is null. No value for a key means the tag wasn't found.
*/
function tag_get_id($tags, $return_value=null) {
global $CFG;
global $CFG, $DB;
static $tag_id_cache = array();
$return_an_int = false;
@ -367,19 +376,25 @@ function tag_get_id($tags, $return_value=null) {
$tags = array_values(tag_normalize($tags));
foreach($tags as $key => $tag) {
$tags[$key] = addslashes(moodle_strtolower($tag));
$tags[$key] = moodle_strtolower($tag);
$result[moodle_strtolower($tag)] = null; // key must exists : no value for a key means the tag wasn't found.
}
$tag_string = "'". implode("', '", $tags) ."'";
if ($rs = get_recordset_sql("SELECT * FROM {$CFG->prefix}tag WHERE name in ({$tag_string}) order by name")) {
while ($record = rs_fetch_next_record($rs)) {
if (empty($tags)) {
return array();
}
list($tag_string, $params) = $DB->get_in_or_equal($tags);
if ($rs = $DB->get_recordset_sql("SELECT * FROM {tag} WHERE name $tag_string ORDER BY name", $params)) {
foreach ($rs as $record) {
if ($return_value == TAG_RETURN_OBJECT) {
$result[$record->name] = $record;
} else { // TAG_RETURN_ARRAY
$result[$record->name] = $record->id;
}
}
$rs->close();
}
if ($return_an_int) {
@ -457,6 +472,7 @@ function tag_get_related_tags_csv($related_tags, $html=TAG_RETURN_HTML) {
* @return bool true on success, false otherwise
*/
function tag_rename($tagid, $newrawname) {
global $DB;
if (! $newrawname_clean = array_shift(tag_normalize($newrawname, TAG_CASE_ORIGINAL)) ) {
return false;
@ -474,10 +490,10 @@ function tag_rename($tagid, $newrawname) {
}
if ($tag = tag_get('id', $tagid, 'id, name, rawname')) {
$tag->rawname = addslashes($newrawname_clean);
$tag->name = addslashes($newname_clean);
$tag->rawname = $newrawname_clean;
$tag->name = $newname_clean;
$tag->timemodified = time();
return update_record('tag', $tag);
return $DB->update_record('tag', $tag);
}
return false;
}
@ -490,6 +506,7 @@ function tag_rename($tagid, $newrawname) {
* @return bool true on success, false otherwise
*/
function tag_delete($tagids) {
global $DB;
if (!is_array($tagids)) {
$tagids = array($tagids);
@ -503,8 +520,8 @@ function tag_delete($tagids) {
// only delete the main entry if there were no problems deleting all the
// instances - that (and the fact we won't often delete lots of tags)
// is the reason for not using delete_records_select()
if ( delete_records('tag_instance', 'tagid', $tagid) ) {
$success &= (bool) delete_records('tag', 'id', $tagid);
if ($DB->delete_records('tag_instance', array('tagid'=>$tagid)) ) {
$success &= (bool) $DB->delete_records('tag', array('id'=>$tagid));
}
}
@ -521,12 +538,14 @@ function tag_delete($tagids) {
* @return bool true on success, false otherwise
*/
function tag_delete_instance($record_type, $record_id, $tagid) {
global $CFG;
global $CFG, $DB;
if ( delete_records('tag_instance', 'tagid', $tagid, 'itemtype', $record_type, 'itemid', $record_id) ) {
if ( !record_exists_sql("SELECT * FROM {$CFG->prefix}tag tg, {$CFG->prefix}tag_instance ti ".
"WHERE (tg.id = ti.tagid AND ti.tagid = {$tagid} ) OR ".
"(tg.id = {$tagid} AND tg.tagtype = 'official')") ) {
if ($DB->delete_records('tag_instance', array('tagid'=>$tagid, 'itemtype'=>$record_type, 'itemid'=>$record_id))) {
if (!$DB->record_exists_sql("SELECT *
FROM {tag} tg, {tag_instance} ti
WHERE (tg.id = ti.tagid AND ti.tagid = ? )
OR (tg.id = ? AND tg.tagtype = 'official')",
array($tagid, $tagid)) ) {
return tag_delete($tagid);
}
} else {
@ -572,8 +591,7 @@ function tag_display_name($tag_object) {
* @return array of matching objects, indexed by record id, from the table containing the type requested
*/
function tag_find_records($tag, $type, $limitfrom='', $limitnum='') {
global $CFG;
global $CFG, $DB;
if (!$tag || !$type) {
return array();
@ -581,11 +599,12 @@ function tag_find_records($tag, $type, $limitfrom='', $limitnum='') {
$tagid = tag_get_id($tag);
$query = "SELECT it.* ".
"FROM {$CFG->prefix}{$type} it INNER JOIN {$CFG->prefix}tag_instance tt ON it.id = tt.itemid ".
"WHERE tt.itemtype = '{$type}' AND tt.tagid = '{$tagid}'";
$query = "SELECT it.*
FROM {".$type."} it INNER JOIN {tag_instance} tt ON it.id = tt.itemid
WHERE tt.itemtype = ? AND tt.tagid = ?";
$params = array($type, $tagid);
return get_records_sql($query, $limitfrom, $limitnum);
return $DB->get_records_sql($query, $params, $limitfrom, $limitnum);
}
@ -606,7 +625,7 @@ function tag_find_records($tag, $type, $limitfrom='', $limitnum='') {
* Any boolean false in the array indicates an error while adding the tag.
*/
function tag_add($tags, $type="default") {
global $USER;
global $USER, $DB;
require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM));
@ -615,9 +634,9 @@ function tag_add($tags, $type="default") {
}
$tag_object = new StdClass;
$tag_object->tagtype = $type;
$tag_object->userid = $USER->id;
$tag_object->timemodified = time();
$tag_object->tagtype = $type;
$tag_object->userid = $USER->id;
$tag_object->timemodified = time();
$clean_tags = tag_normalize($tags, TAG_CASE_ORIGINAL);
@ -629,11 +648,11 @@ function tag_add($tags, $type="default") {
} else {
// note that the difference between rawname and name is only
// capitalization : the rawname is NOT the same at the rawtag.
$tag_object->rawname = addslashes($tag);
$tag_name_lc = moodle_strtolower($tag);
$tag_object->name = addslashes($tag_name_lc);
$tag_object->rawname = $tag;
$tag_name_lc = moodle_strtolower($tag);
$tag_object->name = $tag_name_lc;
//var_dump($tag_object);
$tags_ids[$tag_name_lc] = insert_record('tag', $tag_object);
$tags_ids[$tag_name_lc] = $DB->insert_record('tag', $tag_object);
}
}
@ -651,21 +670,22 @@ function tag_add($tags, $type="default") {
* @return bool true on success, false otherwise
*/
function tag_assign($record_type, $record_id, $tagid, $ordering) {
global $DB;
require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM));
if ( $tag_instance_object = get_record('tag_instance', 'tagid', $tagid, 'itemtype', $record_type, 'itemid', $record_id, 'id') ) {
$tag_instance_object->ordering = $ordering;
if ( $tag_instance_object = $DB->get_record('tag_instance', array('tagid'=>$tagid, 'itemtype'=>$record_type, 'itemid'=>$record_id), 'id')) {
$tag_instance_object->ordering = $ordering;
$tag_instance_object->timemodified = time();
return update_record('tag_instance', $tag_instance_object);
return $DB->update_record('tag_instance', $tag_instance_object);
} else {
$tag_instance_object = new StdClass;
$tag_instance_object->tagid = $tagid;
$tag_instance_object->itemid = $record_id;
$tag_instance_object->itemtype = $record_type;
$tag_instance_object->ordering = $ordering;
$tag_instance_object->tagid = $tagid;
$tag_instance_object->itemid = $record_id;
$tag_instance_object->itemtype = $record_type;
$tag_instance_object->ordering = $ordering;
$tag_instance_object->timemodified = time();
return insert_record('tag_instance', $tag_instance_object);
return $DB->insert_record('tag_instance', $tag_instance_object);
}
}
@ -676,8 +696,10 @@ function tag_assign($record_type, $record_id, $tagid, $ordering) {
* @return mixed an array of objects, or false if no records were found or an error occured.
*/
function tag_autocomplete($text) {
global $CFG;
return get_records_sql("SELECT tg.id, tg.name, tg.rawname FROM {$CFG->prefix}tag tg WHERE tg.name LIKE '". moodle_strtolower($text) ."%'");
global $DB;
return $DB->get_records_sql("SELECT tg.id, tg.name, tg.rawname
FROM {tag} tg
WHERE tg.name LIKE ?", array(moodle_strtolower($text)."%"));
}
/**
@ -689,26 +711,26 @@ function tag_autocomplete($text) {
* function to call: don't run at peak time.
*/
function tag_cleanup() {
global $CFG;
global $DB;
$instances = get_recordset('tag_instance');
$instances = $DB->get_recordset('tag_instance');
// cleanup tag instances
while ($instance = rs_fetch_next_record($instances)) {
foreach ($instances as $instance) {
$delete = false;
if (!record_exists('tag', 'id', $instance->tagid)) {
if (!$DB->record_exists('tag', array('id'=>$instance->tagid))) {
// if the tag has been removed, instance should be deleted.
$delete = true;
} else {
switch ($instance->itemtype) {
case 'user': // users are marked as deleted, but not actually deleted
if (record_exists('user', 'id', $instance->itemid, 'deleted', 1)) {
if ($DB->record_exists('user', array('id'=>$instance->itemid, 'deleted'=>1))) {
$delete = true;
}
break;
default: // anything else, if the instance is not there, delete.
if (!record_exists($instance->itemtype, 'id', $instance->itemid)) {
if (!$DB->record_exists($instance->itemtype, array('id'=>$instance->itemid))) {
$delete = true;
}
break;
@ -719,20 +741,26 @@ function tag_cleanup() {
//debugging('deleting tag_instance #'. $instance->id .', linked to tag id #'. $instance->tagid, DEBUG_DEVELOPER);
}
}
rs_close($instances);
$instances->close();
// TODO: this will only clean tags of type 'default'. This is good as
// it won't delete 'official' tags, but the day we get more than two
// types, we need to fix this.
$unused_tags = get_recordset_sql("SELECT tg.id FROM {$CFG->prefix}tag tg WHERE tg.tagtype = 'default' AND NOT EXISTS (".
"SELECT 'x' FROM {$CFG->prefix}tag_instance ti WHERE ti.tagid = tg.id)");
$unused_tags = $DB->get_recordset_sql("SELECT tg.id
FROM {tag} tg
WHERE tg.tagtype = 'default'
AND NOT EXISTS (
SELECT 'x'
FROM {tag_instance} ti
WHERE ti.tagid = tg.id
)");
// cleanup tags
while ($unused_tag = rs_fetch_next_record($unused_tags)) {
foreach ($unused_tags as $unused_tag) {
tag_delete($unused_tag->id);
//debugging('deleting unused tag #'. $unused_tag->id, DEBUG_DEVELOPER);
}
rs_close($unused_tags);
$unused_tags->close();
}
/**
@ -749,10 +777,9 @@ function tag_cleanup() {
* @param number $min_correlation cutoff percentage (optional, default is 2)
*/
function tag_compute_correlations($min_correlation=2) {
global $DB;
global $CFG;
if (!$all_tags = get_records_list('tag')) {
if (!$all_tags = $DB->get_records_list('tag')) {
return;
}
@ -761,18 +788,19 @@ function tag_compute_correlations($min_correlation=2) {
// query that counts how many times any tag appears together in items
// with the tag passed as argument ($tag_id)
$query = "SELECT tb.tagid , COUNT(*) AS nr ".
"FROM {$CFG->prefix}tag_instance ta INNER JOIN {$CFG->prefix}tag_instance tb ON ta.itemid = tb.itemid ".
"WHERE ta.tagid = {$tag->id} AND tb.tagid != {$tag->id} ".
"GROUP BY tb.tagid ".
"HAVING nr > $min_correlation ".
"ORDER BY nr DESC";
$query = "SELECT tb.tagid , COUNT(*) AS nr
FROM {tag_instance} ta JOIN {tag_instance} tb ON ta.itemid = tb.itemid
WHERE ta.tagid = ? AND tb.tagid <> ?
GROUP BY tb.tagid
HAVING nr > ?
ORDER BY nr DESC";
$params = array($tag->id, $tag->id, $min_correlation);
$correlated = array();
// Correlated tags happen when they appear together in more occasions
// than $min_correlation.
if ($tag_correlations = get_records_sql($query)) {
if ($tag_correlations = $DB->get_records_sql($query, $params)) {
foreach($tag_correlations as $correlation) {
// commented out - now done in query. kept here in case it breaks on some db
// if($correlation->nr >= $min_correlation){
@ -789,13 +817,13 @@ function tag_compute_correlations($min_correlation=2) {
//var_dump($correlated);
//saves correlation info in the caching table
if ($tag_correlation_obj = get_record('tag_correlation', 'tagid', $tag->id, '', '', '', '', 'tagid')) {
if ($tag_correlation_obj = $DB->get_record('tag_correlation', array('tagid'=>$tag->id), 'tagid')) {
$tag_correlation_obj->correlatedtags = $correlated;
update_record('tag_correlation', $tag_correlation_obj);
$DB->update_record('tag_correlation', $tag_correlation_obj);
} else {
$tag_correlation_obj->tagid = $tag->id;
$tag_correlation_obj->correlatedtags = $correlated;
insert_record('tag_correlation', $tag_correlation_obj);
$DB->insert_record('tag_correlation', $tag_correlation_obj);
}
}
}
@ -818,23 +846,23 @@ function tag_cron() {
* @return mixed an array of objects, or false if no records were found or an error occured.
*/
function tag_find_tags($text, $ordered=true, $limitfrom='', $limitnum='') {
global $CFG;
global $DB;
$text = array_shift(tag_normalize($text, TAG_CASE_LOWER));
if ($ordered) {
$query = "SELECT tg.id, tg.name, tg.rawname, COUNT(ti.id) AS count ".
"FROM {$CFG->prefix}tag tg LEFT JOIN {$CFG->prefix}tag_instance ti ON tg.id = ti.tagid ".
"WHERE tg.name LIKE '%{$text}%' ".
"GROUP BY tg.id, tg.name, tg.rawname ".
"ORDER BY count DESC";
$query = "SELECT tg.id, tg.name, tg.rawname, COUNT(ti.id) AS count
FROM {tag} tg LEFT JOIN {tag_instance} ti ON tg.id = ti.tagid
WHERE tg.name LIKE ?
GROUP BY tg.id, tg.name, tg.rawname
ORDER BY count DESC";
} else {
$query = "SELECT tg.id, tg.name, tg.rawname ".
"FROM {$CFG->prefix}tag tg ".
"WHERE tg.name LIKE '%{$text}%'";
$query = "SELECT tg.id, tg.name, tg.rawname
FROM {tag} tg
WHERE tg.name LIKE ?";
}
return get_records_sql($query, $limitfrom , $limitnum);
$params = array("%{$text}%");
return $DB->get_records_sql($query, $params, $limitfrom , $limitnum);
}
/**
@ -844,22 +872,20 @@ function tag_find_tags($text, $ordered=true, $limitfrom='', $limitnum='') {
* @return mixed string name of one tag, or id-indexed array of strings
*/
function tag_get_name($tagids) {
global $DB;
$return_a_string = false;
if ( !is_array($tagids) ) {
$return_a_string = true;
$tagids = array($tagids);
if (!is_array($tagids)) {
if ($tag = $DB->get_record('tag', array('id'=>$tagids))) {
return $tag->name;
}
return false;
}
$tag_names = array();
foreach(get_records_list('tag', 'id', implode(',', $tagids)) as $tag) {
foreach($DB->get_records_list('tag', 'id', $tagids) as $tag) {
$tag_names[$tag->id] = $tag->name;
}
if ($return_a_string) {
return array_pop($tag_names);
}
return $tag_names;
}
@ -872,18 +898,18 @@ function tag_get_name($tagids) {
* @return array an array of tag objects, empty if no correlated tags are found
*/
function tag_get_correlated($tag_id, $limitnum=null) {
global $CFG;
global $DB;
$tag_correlation = get_record('tag_correlation', 'tagid', $tag_id);
$tag_correlation = $DB->get_record('tag_correlation', array('tagid'=>$tag_id));
if (!$tag_correlation || empty($tag_correlation->correlatedtags)) {
return array();
}
// this is (and has to) return the same fields as the query in tag_get_tags
if ( !$result = get_records_sql("SELECT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering ".
"FROM {$CFG->prefix}tag tg INNER JOIN {$CFG->prefix}tag_instance ti ON tg.id = ti.tagid ".
"WHERE tg.id IN ({$tag_correlation->correlatedtags})") ) {
if ( !$result = $DB->get_records_sql("SELECT tg.id, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering
FROM {tag} tg INNER JOIN {tag_instance} ti ON tg.id = ti.tagid
WHERE tg.id IN ({$tag_correlation->correlatedtags})") ) {
return array();
}
@ -937,7 +963,8 @@ function tag_normalize($rawtags, $case = TAG_CASE_LOWER) {
* @return int number of mathing tags.
*/
function tag_record_count($record_type, $tagid) {
return count_records('tag_instance', 'itemtype', $record_type, 'tagid', $tagid);
global $DB;
return $DB->count_records('tag_instance', array('itemtype'=>$record_type, 'tagid'=>$tagid));
}
/**
@ -949,8 +976,9 @@ function tag_record_count($record_type, $tagid) {
* @return bool true if it is tagged, false otherwise
*/
function tag_record_tagged_with($record_type, $record_id, $tag) {
global $DB;
if ($tagid = tag_get_id($tag)) {
return count_records('tag_instance', 'itemtype', $record_type, 'itemid', $record_id, 'tagid', $tagid);
return $DB->count_records('tag_instance', array('itemtype'=>$record_type, 'itemid'=>$record_id, 'tagid'=>$tagid));
} else {
return 0; // tag doesn't exist
}
@ -963,14 +991,14 @@ function tag_record_tagged_with($record_type, $record_id, $tag) {
* @return void
*/
function tag_set_flag($tagids) {
if ( !is_array($tagids) ) {
$tagids = array($tagids);
}
global $DB;
$tagids = (array)$tagids;
foreach ($tagids as $tagid) {
$tag = get_record('tag', 'id', $tagid, '', '', '', '', 'id, flag');
$tag = $DB->get_record('tag', array('id'=>$tagid), 'id, flag');
$tag->flag++;
$tag->timemodified = time();
update_record('tag', $tag);
$DB->update_record('tag', $tag);
}
}
@ -981,7 +1009,7 @@ function tag_set_flag($tagids) {
* @return bool true if function succeeds, false otherwise
*/
function tag_unset_flag($tagids) {
global $CFG;
global $DB;
require_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
@ -989,7 +1017,7 @@ function tag_unset_flag($tagids) {
$tagids = implode(',', $tagids);
}
$timemodified = time();
return execute_sql("UPDATE {$CFG->prefix}tag tg SET tg.flag = 0, tg.timemodified = $timemodified WHERE tg.id IN ($tagids)", false);
return $DB->execute_sql("UPDATE {tag} tg SET tg.flag = 0, tg.timemodified = ? WHERE tg.id IN ($tagids)", array($timemodified));
}
?>

View File

@ -16,16 +16,15 @@
* @param $return if true return html string
*/
function tag_print_cloud($nr_of_tags=150, $return=false) {
global $CFG;
global $CFG, $DB;
$can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
if ( !$tagcloud = get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag '.
'FROM '. $CFG->prefix .'tag_instance ti INNER JOIN '. $CFG->prefix .'tag tg ON tg.id = ti.tagid '.
'WHERE ti.itemtype != \'tag\' '.
'GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype '.
'ORDER BY count DESC, tg.name ASC', 0, $nr_of_tags) ) {
if ( !$tagcloud = $DB->get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag
FROM {tag_instance} ti JOIN {tag} tg ON tg.id = ti.tagid
WHERE ti.itemtype <> \'tag\'
GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype
ORDER BY count DESC, tg.name ASC', null, 0, $nr_of_tags) ) {
$tagcloud = array();
}

View File

@ -16,7 +16,7 @@ $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
require_login();
if (empty($CFG->usetags)) {
error(get_string('tagsaredisabled', 'tag'));
print_error('tagsaredisabled', 'tag');
}
//managing tags requires moodle/tag:manage capability
@ -35,7 +35,7 @@ $notice = '';
// get all the possible tag types from db
$existing_tagtypes = array();
if ($ptypes = get_records_sql("SELECT DISTINCT(tagtype) FROM {$CFG->prefix}tag")) {
if ($ptypes = $DB->get_records_sql("SELECT DISTINCT(tagtype) FROM {tag}")) {
foreach ($ptypes as $ptype) {
$existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
}
@ -182,6 +182,8 @@ TABLE_VAR_PAGE => 'spage'
$table->setup();
$params = array();
if ($table->get_sql_sort()) {
$sort = 'ORDER BY '. $table->get_sql_sort();
} else {
@ -194,13 +196,13 @@ if ($table->get_sql_where()) {
$where = '';
}
$query = 'SELECT tg.id, tg.name, tg.rawname, tg.tagtype, COUNT(ti.id) AS count, u.id AS owner, tg.flag, tg.timemodified, u.firstname, u.lastname '.
'FROM '. $CFG->prefix .'tag_instance ti RIGHT JOIN '. $CFG->prefix .'tag tg ON tg.id = ti.tagid LEFT JOIN '. $CFG->prefix .'user u ON tg.userid = u.id '.
$where .' '.
'GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, u.id, tg.flag, tg.timemodified, u.firstname, u.lastname '.
$sort;
$totalcount = count_records_sql('SELECT COUNT(DISTINCT(tg.id)) FROM '. $CFG->prefix .'tag tg LEFT JOIN '. $CFG->prefix .'user u ON u.id = tg.userid '. $where);
$query = 'SELECT tg.id, tg.name, tg.rawname, tg.tagtype, COUNT(ti.id) AS count, u.id AS owner, tg.flag, tg.timemodified, u.firstname, u.lastname
FROM {tag_instance} ti RIGHT JOIN {tag} tg ON tg.id = ti.tagid LEFT JOIN {user} u ON tg.userid = u.id
'.$where.'
GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, u.id, tg.flag, tg.timemodified, u.firstname, u.lastname
'.$sort;
$totalcount = $DB->count_records_sql('SELECT COUNT(DISTINCT(tg.id))
FROM {tag} tg LEFT JOIN {user} u ON u.id = tg.userid '. $where, $params);
$table->initialbars(true); // always initial bars
$table->pagesize($perpage, $totalcount);
@ -208,11 +210,11 @@ $table->pagesize($perpage, $totalcount);
echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
//retrieve tags from DB
if ($tagrecords = get_records_sql($query, $table->get_page_start(), $table->get_page_size())) {
if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) {
$taglist = array_values($tagrecords);
//print_tag_cloud(array_values(get_records_sql($query)), false);
//print_tag_cloud(array_values($DB->get_records_sql($query)), false);
//populate table with data
foreach ($taglist as $tag ){
$id = $tag->id;