* @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;
}
* @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;
}
*
**/
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);
}
* @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;
- }
}
/**
* 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;
$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) {
* @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;
}
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;
}
* @return bool true on success, false otherwise
*/
function tag_delete($tagids) {
+ global $DB;
if (!is_array($tagids)) {
$tagids = array($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));
}
}
* @return bool true on success, false otherwise
*/
function tag_delete_instance($record_type, $record_id, $tagid) {
- global $CFG;
-
- 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')") ) {
+ global $CFG, $DB;
+
+ 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 {
* @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();
$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);
}
* 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));
}
$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);
} 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);
}
}
* @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);
}
}
* @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)."%"));
}
/**
* 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;
//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();
}
/**
* @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;
}
// 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){
//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);
}
}
}
* @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);
}
/**
* @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;
}
* @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();
}
* @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));
}
/**
* @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
}
* @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);
}
}
* @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));
$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));
}
?>