/**
* Moodle tag library
*
- * Tag strings : you can use any character in tags, except the comma (which is
- * the separator) and the '\' (backslash). Note that many spaces (or other
- * blank characters) will get "compressed" into one. A tag string is always a
+ * Tag strings : you can use any character in tags, except the comma (which is
+ * the separator) and the '\' (backslash). Note that many spaces (or other
+ * blank characters) will get "compressed" into one. A tag string is always a
* rawurlencode'd string. This is the same behavior as http://del.icio.us.
*
- * A "record" is a php array (note that an object will work too) that contains
- * the following variables :
+ * A "record" is a php array (note that an object will work too) that contains
+ * the following variables :
* - type: the table containing the record that we are tagging (eg: for a
* blog, this is table 'post', and for a user it is 'user')
- * - id: the id of the record
+ * - id: the id of the record
*
- * TODO: turn this into a full-fledged categorization system. This could start
- * by modifying (removing, probably) the 'tag type' to use another table
- * describing the relationship between tags (parents, sibling, etc.), which
+ * TODO: turn this into a full-fledged categorization system. This could start
+ * by modifying (removing, probably) the 'tag type' to use another table
+ * describing the relationship between tags (parents, sibling, etc.), which
* could then be merged with the 'course categorization' system...
*
- * BASIC INSTRUCTIONS :
- * - to "tag a blog post" (for example):
+ * BASIC INSTRUCTIONS :
+ * - to "tag a blog post" (for example):
* tag_set('post', $blog_post->id, $array_of_tags);
*
* - to "remove all the tags on a blog post":
/**
* Set the tags assigned to a record. This overwrites the current tags.
- *
- * This function is meant to be fed the string coming up from the user
+ *
+ * This function is meant to be fed the string coming up from the user
* interface, which contains all tags assigned to a record.
*
- * @param string $record_type the type of record to tag ('post' for blogs,
+ * @param string $record_type the type of record to tag ('post' for blogs,
* 'user' for users, 'tag' for tags, etc.
* @param int $record_id the id of the record to tag
- * @param array $tags the array of tags to set on the record. If
+ * @param array $tags the array of tags to set on the record. If
* given an empty array, all tags will be removed.
- * @return void
+ * @return void
*/
function tag_set($record_type, $record_id, $tags) {
//echo 'tags-in-tag_set'; var_dump($tags); var_dump($tags_ids); var_dump($cleaned_tags);
$current_ids = tag_get_tags_ids($record_type, $record_id);
- //var_dump($current_ids);
+ //var_dump($current_ids);
// for data coherence reasons, it's better to remove deleted tags
// before adding new data: ordering could be duplicated.
if (!in_array($current_id, $tags_ids)) {
tag_delete_instance($record_type, $record_id, $current_id);
if ( $record_type == 'tag' && !$in_recursion_semaphore) {
- // if we are removing a tag-on-a-tag (manually related tag),
+ // if we are removing a tag-on-a-tag (manually related tag),
// we need to remove the opposite relationship as well.
tag_delete_instance('tag', $current_id, $record_id);
}
$clean_tag = $cleaned_tags[$tag];
$tag_current_id = $tags_ids[$clean_tag];
-
+
if ( is_null($tag_current_id) ) {
// create new tags
//echo "call to add tag $tag\n";
/**
* Adds a tag to a record, without overwriting the current tags.
- *
- * @param string $record_type the type of record to tag ('post' for blogs,
+ *
+ * @param string $record_type the type of record to tag ('post' for blogs,
* 'user' for users, etc.
* @param int $record_id the id of the record to tag
* @param string $tag the tag to add
$new_tags[] = $current_tag->rawname;
}
$new_tags[] = $tag;
-
+
return tag_set($record_type, $record_id, $new_tags);
}
/**
* Removes a tag from a record, without overwriting other current tags.
- *
- * @param string $record_type the type of record to tag ('post' for blogs,
+ *
+ * @param string $record_type the type of record to tag ('post' for blogs,
* 'user' for users, etc.
* @param int $record_id the id of the record to tag
* @param string $tag the tag to delete
}
-/**
+/**
* Set the description of a tag
- *
+ *
* @param int $tagid the id of the tag
* @param string $description the description
* @param int $descriptionformat the moodle text format of the description
- * @return true on success, false otherwise
+ * @return true on success, false otherwise
*/
function tag_description_set($tagid, $description, $descriptionformat) {
global $DB;
/**
- * Get the array of db record of tags associated to a record (instances). Use
+ * Get the array of db record of tags associated to a record (instances). Use
* tag_get_tags_csv to get the same information in a comma-separated string.
*
- * @param string $record_type the record type for which we want to get the tags
- * @param int $record_id the record id for which we want to get the tags
+ * @param string $record_type the record type for which we want to get the tags
+ * @param int $record_id the record id for which we want to get the tags
* @param string $type the tag type (either 'default' or 'official'). By default,
* all tags are returned.
* @return array the array of tags
// if the fields in this query are changed, you need to do the same changes in tag_get_correlated_tags
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 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 = $DB->get_records_sql("SELECT ti.tagid, tg.tagtype, tg.name, tg.rawname, tg.flag, ti.ordering ".
// "FROM {tag_instance} ti LEFT JOIN {tag} tg ON ti.tagid = tg.id ".
/**
* Get the array of tags display names, indexed by id.
- *
+ *
* @param string $record_type the record type for which we want to get the tags
* @param int $record_id the record id for which we want to get the tags
* @param string $type the tag type (either 'default' or 'official'). By default,
* @return array of tag ids, indexed and sorted by 'ordering'
*/
function tag_get_tags_ids($record_type, $record_id) {
-
+
$tag_ids = array();
foreach (tag_get_tags($record_type, $record_id) as $tag) {
if ( array_key_exists($tag->ordering, $tag_ids) ) {
return $tag_ids;
}
-/**
+/**
* Returns the database ID of a set of tags.
- *
+ *
* @param mixed $tags one tag, or array of tags, to look for.
- * @param bool $return_value specify the type of the returned value. Either
- * TAG_RETURN_OBJECT, or TAG_RETURN_ARRAY (default). If TAG_RETURN_ARRAY
- * is specified, an array will be returned even if only one tag was
+ * @param bool $return_value specify the type of the returned value. Either
+ * TAG_RETURN_OBJECT, or TAG_RETURN_ARRAY (default). If TAG_RETURN_ARRAY
+ * is specified, an array will be returned even if only one tag was
* passed in $tags.
- * @return mixed tag-indexed array of ids (or objects, if second parameter is
- * TAG_RETURN_OBJECT), or only an int, if only one tag is given *and* the
+ * @return mixed tag-indexed array of ids (or objects, if second parameter is
+ * TAG_RETURN_OBJECT), or only an int, if only one tag is given *and* the
* second parameter is null. No value for a key means the tag wasn't found.
*/
function tag_get_id($tags, $return_value=null) {
$return_an_int = false;
if (!is_array($tags)) {
if(is_null($return_value) || $return_value == TAG_RETURN_OBJECT) {
- $return_an_int = true;
+ $return_an_int = true;
}
$tags = array($tags);
}
-
+
$result = array();
-
+
//TODO: test this and see if it helps performance without breaking anything
//foreach($tags as $key => $tag) {
// $clean_tag = moodle_strtolower($tag);
$tags = array_values(tag_normalize($tags));
foreach($tags as $key => $tag) {
- $tags[$key] = 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.
}
* - manually added related tags, which are tag_instance entries for that tag
* - correlated tags, which are a calculated
*
- * @param string $tag_name_or_id is a single **normalized** tag name or the id
+ * @param string $tag_name_or_id is a single **normalized** tag name or the id
* of a tag
- * @param int $type the function will return either manually
- * (TAG_RELATED_MANUAL) related tags or correlated (TAG_RELATED_CORRELATED)
+ * @param int $type the function will return either manually
+ * (TAG_RELATED_MANUAL) related tags or correlated (TAG_RELATED_CORRELATED)
* tags. Default is TAG_RELATED_ALL, which returns everything.
- * @param int $limitnum return a subset comprising this many records (optional,
+ * @param int $limitnum return a subset comprising this many records (optional,
* default is 10)
* @return array an array of tag objects
*/
return array_slice(object_array_unique($related_tags), 0 , $limitnum);
}
-/**
+/**
* Get a comma-separated list of tags related to another tag.
*
* @param array $related_tags the array returned by tag_get_related_tags
// Prevent the rename if a tag with that name already exists
if ($existing = tag_get('name', $newname_clean, 'id, name, rawname')) {
if ($existing->id != $tagid) { // Another tag already exists with this name
- return false;
+ return false;
}
}
if ($tag = tag_get('id', $tagid, 'id, name, rawname')) {
- $tag->rawname = $newrawname_clean;
- $tag->name = $newname_clean;
+ $tag->rawname = $newrawname_clean;
+ $tag->name = $newname_clean;
$tag->timemodified = time();
return $DB->update_record('tag', $tag);
}
/**
* Delete one or more tag, and all their instances if there are any left.
- *
+ *
* @param mixed $tagids one tagid (int), or one array of tagids to delete
- * @return bool true on success, false otherwise
+ * @return bool true on success, false otherwise
*/
function tag_delete($tagids) {
global $DB;
if (is_null($tagid)) { // can happen if tag doesn't exists
continue;
}
- // 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)
+ // 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 $DB->delete_records_select()
if ($DB->delete_records('tag_instance', array('tagid'=>$tagid)) ) {
$success &= (bool) $DB->delete_records('tag', array('id'=>$tagid));
FROM {".$type."} it INNER JOIN {tag_instance} tt ON it.id = tt.itemid
WHERE tt.itemtype = ? AND tt.tagid = ?";
$params = array($type, $tagid);
-
- return $DB->get_records_sql($query, $params, $limitfrom, $limitnum);
+
+ return $DB->get_records_sql($query, $params, $limitfrom, $limitnum);
}
/////////////////// PRIVATE TAG API ///////////////////
/**
- * Adds one or more tag in the database. This function should not be called
+ * Adds one or more tag in the database. This function should not be called
* directly : you should use tag_set.
*
* @param mixed $tags one tag, or an array of tags, to be created
- * @param string $type type of tag to be created ("default" is the default
+ * @param string $type type of tag to be created ("default" is the default
* value and "official" is the only other supported value at this time). An
* official tag is kept even if there are no records tagged with it.
- * @return an array of tags ids, indexed by their lowercase normalized names.
+ * @return an array of tags ids, indexed by their lowercase normalized names.
* Any boolean false in the array indicates an error while adding the tag.
*/
function tag_add($tags, $type="default") {
global $USER, $DB;
- require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM));
+ require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM));
if (!is_array($tags)) {
$tags = array($tags);
if (!$tag) {
$tags_ids[$tag] = false;
} else {
- // note that the difference between rawname and name is only
- // capitalization : the rawname is NOT the same at the rawtag.
- $tag_object->rawname = $tag;
+ // note that the difference between rawname and name is only
+ // capitalization : the rawname is NOT the same at the rawtag.
+ $tag_object->rawname = $tag;
$tag_name_lc = moodle_strtolower($tag);
$tag_object->name = $tag_name_lc;
//var_dump($tag_object);
/**
* Assigns a tag to a record: if the record already exists, the time and
* ordering will be updated.
- *
+ *
* @param string $record_type the type of the record that will be tagged
* @param int $record_id the id of the record that will be tagged
- * @param string $tagid the tag id to set on the record.
+ * @param string $tagid the tag id to set on the record.
* @param int $ordering the order of the instance for this record
* @return bool true on success, false otherwise
*/
$tag_instance_object->ordering = $ordering;
$tag_instance_object->timemodified = time();
return $DB->update_record('tag_instance', $tag_instance_object);
- } else {
+ } else {
$tag_instance_object = new StdClass;
$tag_instance_object->tagid = $tagid;
$tag_instance_object->itemid = $record_id;
WHERE tg.name LIKE ?", array(moodle_strtolower($text)."%"));
}
-/**
+/**
* Clean up the tag tables, making sure all tagged object still exists.
*
* This should normally not be necessary, but in case related tags are not deleted
* when the tagged record is removed, this should be done once in a while, perhaps on
- * an occasional cron run. On a site with lots of tags, this could become an expensive
+ * an occasional cron run. On a site with lots of tags, this could become an expensive
* function to call: don't run at peak time.
*/
function tag_cleanup() {
// cleanup tag instances
foreach ($instances as $instance) {
$delete = false;
-
+
if (!$DB->record_exists('tag', array('id'=>$instance->tagid))) {
// if the tag has been removed, instance should be deleted.
$delete = true;
function tag_compute_correlations($min_correlation=2) {
global $DB;
- if (!$all_tags = $DB->get_records_list('tag')) {
+ if (!$all_tags = $DB->get_records('tag')) {
return;
}
WHERE ta.tagid = ? AND tb.tagid <> ?
GROUP BY tb.tagid
HAVING nr > ?
- ORDER BY nr DESC";
+ ORDER BY nr DESC";
$params = array($tag->id, $tag->id, $min_correlation);
$correlated = array();
- // Correlated tags happen when they appear together in more occasions
+ // Correlated tags happen when they appear together in more occasions
// than $min_correlation.
if ($tag_correlations = $DB->get_records_sql($query, $params)) {
foreach($tag_correlations as $correlation) {
return $DB->get_records_sql($query, $params, $limitfrom , $limitnum);
}
-/**
+/**
* Get the name of a tag
- *
+ *
* @param mixed $tagids the id of the tag, or an array of ids
* @return mixed string name of one tag, or id-indexed array of strings
*/
}
$tag_names = array();
- foreach($DB->get_records_list('tag', 'id', $tagids) as $tag) {
+ foreach($DB->get_records_list('tag', 'id', $tagids) as $tag) {
$tag_names[$tag->id] = $tag->name;
}
/**
* Returns the correlated tags of a tag, retrieved from the tag_correlation
- * table. Make sure cron runs, otherwise the table will be empty and this
+ * table. Make sure cron runs, otherwise the table will be empty and this
* function won't return anything.
*
* @param int $tag_id is a single 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 = $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 $result;
}
* Function that normalizes a list of tag names.
*
* @param mixed $tags array of tags, or a single tag.
- * @param int $case case to use for returned value (default: lower case).
+ * @param int $case case to use for returned value (default: lower case).
* Either TAG_CASE_LOWER (default) or TAG_CASE_ORIGINAL
- * @return array of lowercased normalized tags, indexed by the normalized tag,
+ * @return array of lowercased normalized tags, indexed by the normalized tag,
* in the same order as the original array. (Eg: 'Banana' => 'banana').
*/
function tag_normalize($rawtags, $case = TAG_CASE_LOWER) {
$cleaned_tags_lc[$rawtag] = moodle_strtolower( clean_param($rawtag, PARAM_TAG) );
$cleaned_tags_mc[$rawtag] = clean_param($rawtag, PARAM_TAG);
}
- if ( $case == TAG_CASE_LOWER ) {
+ if ( $case == TAG_CASE_LOWER ) {
$result[$rawtag] = $cleaned_tags_lc[$rawtag];
} else { // TAG_CASE_ORIGINAL
$result[$rawtag] = $cleaned_tags_mc[$rawtag];
}
}
-
+
return $result;
}
}
/**
- * Determine if a record is tagged with a specific tag
+ * Determine if a record is tagged with a specific tag
*
* @param string $record_type the record type to look for
* @param int $record_id the record id to look for
/**
* Flag a tag as inapropriate
- *
+ *
* @param mixed $tagids one (int) tagid, or an array of tagids
* @return void
*/
}
}
-/**
+/**
* Remove the inapropriate flag on a tag
- *
+ *
* @param mixed $tagids one (int) tagid, or an array of tagids
* @return bool true if function succeeds, false otherwise
*/