Merged MDL-13725 Remove tag_get_tag_by_id completely. I also re-ordered tag/lib.php a bit

This commit is contained in:
moodler 2008-02-29 06:26:21 +00:00
parent 15234a92a1
commit 8479c2e0dd
6 changed files with 445 additions and 429 deletions

View file

@ -43,13 +43,13 @@ class block_tag_flickr extends block_base {
$tag = optional_param('tag', '', PARAM_TAG); // tag $tag = optional_param('tag', '', PARAM_TAG); // tag
if ($tag) { if ($tag) {
$tag_object = tag_get_id($tag, TAG_RETURN_OBJECT); $tag_object = tag_get('name', $tag);
} elseif (!$tag && $tagid) { } else if ($tagid) {
$tag_object = tag_get_tag_by_id($tagid); $tag_object = tag_get('id', $tagid);
} else { }
// can't display the block if no tag was submitted!
// todo: something useful here. if (empty($tag_object)) {
error('Missing tag parameter'); errorcode('tagnotfound');
} }
//include related tags in the photo query ? //include related tags in the photo query ?

View file

@ -82,13 +82,13 @@ class block_tag_youtube extends block_base {
$tag = optional_param('tag', '', PARAM_TAG); // tag $tag = optional_param('tag', '', PARAM_TAG); // tag
if ($tag) { if ($tag) {
$tag_object = tag_get_id($tag, TAG_RETURN_OBJECT); $tag_object = tag_get('name', $tag);
} elseif (!$tag && $tagid) { } else if ($tagid) {
$tag_object = tag_get_tag_by_id($tagid); $tag_object = tag_get('id', $tagid);
} else { }
// can't display the block if no tag was submitted!
// todo: something useful here. if (empty($tag_object)) {
error('Missing tag parameter'); errorcode('tagnotfound');
} }
$query_tag = html_entity_decode(tag_display_name($tag_object)); $query_tag = html_entity_decode(tag_display_name($tag_object));
@ -114,13 +114,13 @@ class block_tag_youtube extends block_base {
$tag = optional_param('tag', '', PARAM_TAG); // tag $tag = optional_param('tag', '', PARAM_TAG); // tag
if ($tag) { if ($tag) {
$tag_object = tag_get_id($tag, TAG_RETURN_OBJECT); $tag_object = tag_get('name', $tag);
} elseif (!$tag && $tagid) { } else if ($tagid) {
$tag_object = tag_get_tag_by_id($tagid); $tag_object = tag_get('id', $tagid);
} else { }
// can't display the block if no tag was submitted!
// todo: something useful here. if (empty($tag_object)) {
error('Missing tag parameter'); errorcode('tagnotfound');
} }
$query_tag = html_entity_decode(tag_display_name($tag_object)); $query_tag = html_entity_decode(tag_display_name($tag_object));

View file

@ -308,10 +308,11 @@ function add_tags_info($postid) {
if ($otags = optional_param('otags', '', PARAM_INT)) { if ($otags = optional_param('otags', '', PARAM_INT)) {
foreach ($otags as $tagid) { foreach ($otags as $tagid) {
// TODO : make this use the tag name in the form // TODO : make this use the tag name in the form
$tag = tag_get_tag_by_id($tagid); if ($tag = tag_get('id', $tagid)) {
$tags[] = $tag->name; $tags[] = $tag->name;
} }
} }
}
$manual_tags = optional_param('ptags', '', PARAM_NOTAGS); $manual_tags = optional_param('ptags', '', PARAM_NOTAGS);
$tags = array_merge($tags, explode(',', $manual_tags)); $tags = array_merge($tags, explode(',', $manual_tags));

View file

@ -14,13 +14,17 @@ if (empty($CFG->usetags)) {
$tag_id = optional_param('id', 0, PARAM_INT); $tag_id = optional_param('id', 0, PARAM_INT);
$tag_name = optional_param('tag', '', PARAM_TAG); $tag_name = optional_param('tag', '', PARAM_TAG);
if ($tag_name) { if ($tag_name) {
$tag = tag_get_id($tag_name, TAG_RETURN_OBJECT); $tag = tag_get('name', $tag_name, '*');
} else if ($tag_id) { } else if ($tag_id) {
$tag = tag_get_tag_by_id($tag_id); $tag = tag_get('id', $tag_id, '*');
} else {
error('A required parameter was missing');
} }
if (empty($tag)) {
redirect($CFG->wwwroot.'/tag/search.php');
}
$tagname = tag_display_name($tag); $tagname = tag_display_name($tag);
//Editing a tag requires moodle/tag:edit capability //Editing a tag requires moodle/tag:edit capability

View file

@ -13,20 +13,24 @@ if (empty($CFG->usetags)) {
} }
$tagid = optional_param('id', 0, PARAM_INT); // tag id $tagid = optional_param('id', 0, PARAM_INT); // tag id
$tag_name = optional_param('tag', '', PARAM_TAG); // tag $tagname = optional_param('tag', '', PARAM_TAG); // tag
if ($tag_name) {
$tag = tag_get_id($tag_name, TAG_RETURN_OBJECT);
} elseif ($tagid) {
$tag = tag_get_tag_by_id($tagid); // for backward compatibility
}
$edit = optional_param('edit', -1, PARAM_BOOL); $edit = optional_param('edit', -1, PARAM_BOOL);
$userpage = optional_param('userpage', 0, PARAM_INT); // which page to show $userpage = optional_param('userpage', 0, PARAM_INT); // which page to show
$perpage = optional_param('perpage', 24, PARAM_INT); $perpage = optional_param('perpage', 24, PARAM_INT);
if (!isset($tag) || !$tag->id) {
if ($tagname) {
$tag = tag_get('name', $tagname, '*');
} else if ($tagid) {
$tag = tag_get('id', $tagid, '*');
}
if (empty($tag)) {
redirect($CFG->wwwroot.'/tag/search.php'); redirect($CFG->wwwroot.'/tag/search.php');
} }
//create a new page_tag object, defined in pagelib.php //create a new page_tag object, defined in pagelib.php
$PAGE = page_create_object(PAGE_TAG_INDEX, $tag->id); $PAGE = page_create_object(PAGE_TAG_INDEX, $tag->id);
$pageblocks = blocks_setup($PAGE,BLOCKS_PINNED_BOTH); $pageblocks = blocks_setup($PAGE,BLOCKS_PINNED_BOTH);

View file

@ -54,55 +54,129 @@ require_once($CFG->dirroot .'/tag/locallib.php');
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
/////////////////// PUBLIC TAG API //////////////////// /////////////////// PUBLIC TAG API ////////////////////
/** /// Functions for settings tags //////////////////////
* 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
*/
function tag_delete($tagids) {
if (!is_array($tagids)) { /**
$tagids = array($tagids); * 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
* interface, which contains all tags assigned to a record.
*
* @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
* given an empty array, all tags will be removed.
* @return void
*/
function tag_set($record_type, $record_id, $tags) {
global $db;
$record = array('type' => $record_type, 'id' => $record_id);
$tags_ids = tag_get_id($tags, TAG_RETURN_ARRAY); // force an array, even if we only have one tag.
$cleaned_tags = tag_normalize($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);
$tags_to_assign = array();
// for data coherence reasons, it's better to remove deleted tags
// before adding new data: ordering could be duplicated.
foreach($current_ids as $current_id) {
if (!in_array($current_id, $tags_ids)) {
tag_delete_instance($record, $current_id);
}
} }
$success = true; foreach($tags as $ordering => $tag) {
foreach( $tagids as $tagid ) { $tag = trim($tag);
if (is_null($tagid)) { // can happen if tag doesn't exists if (!$tag) {
continue; 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)
// is the reason for not using delete_records_select()
if ( delete_records('tag_instance', 'tagid', $tagid) ) {
$success &= (bool) delete_records('tag', 'id', $tagid);
}
}
return $success; $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";
$new_tag = tag_add($tag);
tag_assign($record, $new_tag[$clean_tag], $ordering);
}
elseif ( empty($current_ids) || !in_array($tag_current_id, $current_ids) ) {
// assign existing tags
tag_assign($record, $tag_current_id, $ordering);
}
elseif ( isset($current_ids[$ordering]) && $current_ids[$ordering] != $tag_current_id ) {
// this actually checks if the ordering number points to the same tag
//recompute ordering, if necessary
//echo 'ordering changed for ', $tag, ':', $ordering, "\n";
tag_assign($record, $tag_current_id, $ordering);
}
}
} }
/** /**
* Delete one instance of a tag. If the last instance was deleted, it will * Adds a tag to a record, without overwriting the current tags.
* also delete the tag, unless it's type is 'official'.
* *
* @param array $record the record for which to remove the instance * @param string $record_type the type of record to tag ('post' for blogs,
* @param int $tagid the tagid that needs to be removed * 'user' for users, etc.
* @return bool true on success, false otherwise * @param int $record_id the id of the record to tag
* @param string $tag the tag to add
* @return void
*/ */
function tag_delete_instance($record, $tagid) { function tag_set_add($record_type, $record_id, $tag) {
global $CFG;
if ( delete_records('tag_instance', 'tagid', $tagid, 'itemtype', $record['type'], 'itemid', $record['id']) ) { $new_tags = array();
if ( !record_exists_sql("SELECT * FROM {$CFG->prefix}tag tg, {$CFG->prefix}tag_instance ti ". foreach( tag_get_tags($record_type, $record_id) as $current_tag ) {
"WHERE (tg.id = ti.tagid AND ti.tagid = {$tagid} ) OR ". $new_tags[] = $current_tag->rawname;
"(tg.id = {$tagid} AND tg.tagtype = 'official')") ) { }
return tag_delete($tagid); $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,
* 'user' for users, etc.
* @param int $record_id the id of the record to tag
* @param string $tag the tag to delete
* @return void
*/
function tag_set_delete($record_type, $record_id, $tag) {
$new_tags = array();
foreach( tag_get_tags($record_type, $record_id) as $current_tag ) {
if ($current_tag->name != $tag) { // Keep all tags but the one specified
$new_tags[] = $current_tag->name;
}
}
return tag_set($record_type, $record_id, $new_tags);
}
/**
* Set the type of a tag. At this time (version 1.9) the possible values
* are 'default' or 'official'. Official tags will be displayed separately "at
* tagging time" (while selecting the tags to apply to a record).
*
* @param string $tagid tagid to modify
* @param string $type either 'default' or 'official'
* @return true on success, false otherwise
*/
function tag_type_set($tagid, $type) {
if ($tag = get_record('tag', 'id', $tagid, '', '', '', '', 'id')) {
$tag->tagtype = addslashes($type);
$tag->timemodified = time();
return update_record('tag', $tag);
} }
} else {
return false; return false;
} }
}
/** /**
* Set the description of a tag * Set the description of a tag
@ -122,61 +196,15 @@ function tag_description_set($tagid, $description, $descriptionformat) {
return false; return false;
} }
/**
* Function that returns the name that should be displayed for a specific tag
*
* @param object $tag_object a line out of tag table, as returned by the adobd functions
* @return string
*/
function tag_display_name($tag_object) {
global $CFG;
if(!isset($tag_object->name)) {
return '';
}
if( empty($CFG->keeptagnamecase) ) {
//this is the normalized tag name
$textlib = textlib_get_instance(); /// Functions for getting information about tags //////
return htmlspecialchars($textlib->strtotitle($tag_object->name));
}
else {
//original casing of the tag name
return htmlspecialchars($tag_object->rawname);
}
}
/** /**
* Find all records tagged with a tag of a given type ('post', 'user', etc.) * Simple function to just return a single tag object when you know the name or something
*
* @param string $tag tag to look for
* @param string $type type to restrict search to. If null, every matching
* record will be returned
* @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
* @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
* @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;
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}'";
return get_records_sql($query, $limitfrom, $limitnum);
}
/**
* Simple function to just return a single tag object
* *
* @param string $field which field do we use to identify the tag: id, name or rawname * @param string $field which field do we use to identify the tag: id, name or rawname
* @param string $value the required value of the aforementioned field * @param string $value the required value of the aforementioned field
@ -185,6 +213,9 @@ function tag_find_records($tag, $type, $limitfrom='', $limitnum='') {
* *
**/ **/
function tag_get($field, $value, $returnfields='id, name, rawname') { function tag_get($field, $value, $returnfields='id, name, rawname') {
if ($field == 'name') {
$value = moodle_strtolower($value); // To cope with input that might just be wrong case
}
return get_record('tag', $field, $value, '', '', '', '', $returnfields); return get_record('tag', $field, $value, '', '', '', '', $returnfields);
} }
@ -347,15 +378,6 @@ function tag_get_id($tags, $return_value=null) {
return $result; return $result;
} }
/**
* Get a tag as an object (line) returned by get_recordset_sql
*
* @param int $tagid a tag id
* @return object a line returned from get_recordset_sql, or false
*/
function tag_get_tag_by_id($tagid) {
return get_record('tag', 'id', $tagid);
}
/** /**
* Returns tags related to a tag * Returns tags related to a tag
@ -449,126 +471,111 @@ function tag_rename($tagid, $newrawname) {
return false; return false;
} }
/** /**
* Set the tags assigned to a record. This overwrites the current tags. * Delete one or more tag, and all their instances if there are any left.
* *
* This function is meant to be fed the string coming up from the user * @param mixed $tagids one tagid (int), or one array of tagids to delete
* interface, which contains all tags assigned to a record. * @return bool true on success, false otherwise
*
* @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
* given an empty array, all tags will be removed.
* @return void
*/ */
function tag_set($record_type, $record_id, $tags) { function tag_delete($tagids) {
global $db;
$record = array('type' => $record_type, 'id' => $record_id); if (!is_array($tagids)) {
$tagids = array($tagids);
$tags_ids = tag_get_id($tags, TAG_RETURN_ARRAY); // force an array, even if we only have one tag.
$cleaned_tags = tag_normalize($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);
$tags_to_assign = array();
// for data coherence reasons, it's better to remove deleted tags
// before adding new data: ordering could be duplicated.
foreach($current_ids as $current_id) {
if (!in_array($current_id, $tags_ids)) {
tag_delete_instance($record, $current_id);
}
} }
foreach($tags as $ordering => $tag) { $success = true;
$tag = trim($tag); foreach( $tagids as $tagid ) {
if (!$tag) { if (is_null($tagid)) { // can happen if tag doesn't exists
continue; 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)
// is the reason for not using delete_records_select()
if ( delete_records('tag_instance', 'tagid', $tagid) ) {
$success &= (bool) delete_records('tag', 'id', $tagid);
}
}
$clean_tag = $cleaned_tags[$tag]; return $success;
$tag_current_id = $tags_ids[$clean_tag];
if ( is_null($tag_current_id) ) {
// create new tags
//echo "call to add tag $tag\n";
$new_tag = tag_add($tag);
tag_assign($record, $new_tag[$clean_tag], $ordering);
}
elseif ( empty($current_ids) || !in_array($tag_current_id, $current_ids) ) {
// assign existing tags
tag_assign($record, $tag_current_id, $ordering);
}
elseif ( isset($current_ids[$ordering]) && $current_ids[$ordering] != $tag_current_id ) {
// this actually checks if the ordering number points to the same tag
//recompute ordering, if necessary
//echo 'ordering changed for ', $tag, ':', $ordering, "\n";
tag_assign($record, $tag_current_id, $ordering);
}
}
} }
/** /**
* Adds a tag to a record, without overwriting the current tags. * Delete one instance of a tag. If the last instance was deleted, it will
* also delete the tag, unless it's type is 'official'.
* *
* @param string $record_type the type of record to tag ('post' for blogs, * @param array $record the record for which to remove the instance
* 'user' for users, etc. * @param int $tagid the tagid that needs to be removed
* @param int $record_id the id of the record to tag * @return bool true on success, false otherwise
* @param string $tag the tag to add
* @return void
*/ */
function tag_set_add($record_type, $record_id, $tag) { function tag_delete_instance($record, $tagid) {
global $CFG;
$new_tags = array(); if ( delete_records('tag_instance', 'tagid', $tagid, 'itemtype', $record['type'], 'itemid', $record['id']) ) {
foreach( tag_get_tags($record_type, $record_id) as $current_tag ) { if ( !record_exists_sql("SELECT * FROM {$CFG->prefix}tag tg, {$CFG->prefix}tag_instance ti ".
$new_tags[] = $current_tag->rawname; "WHERE (tg.id = ti.tagid AND ti.tagid = {$tagid} ) OR ".
} "(tg.id = {$tagid} AND tg.tagtype = 'official')") ) {
$new_tags[] = $tag; return tag_delete($tagid);
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,
* 'user' for users, etc.
* @param int $record_id the id of the record to tag
* @param string $tag the tag to delete
* @return void
*/
function tag_set_delete($record_type, $record_id, $tag) {
$new_tags = array();
foreach( tag_get_tags($record_type, $record_id) as $current_tag ) {
if ($current_tag->name != $tag) { // Keep all tags but the one specified
$new_tags[] = $current_tag->name;
}
}
return tag_set($record_type, $record_id, $new_tags);
}
/**
* Set the type of a tag. At this time (version 1.9) the possible values
* are 'default' or 'official'. Official tags will be displayed separately "at
* tagging time" (while selecting the tags to apply to a record).
*
* @param string $tagid tagid to modify
* @param string $type either 'default' or 'official'
* @return true on success, false otherwise
*/
function tag_type_set($tagid, $type) {
if ($tag = get_record('tag', 'id', $tagid, '', '', '', '', 'id')) {
$tag->tagtype = addslashes($type);
$tag->timemodified = time();
return update_record('tag', $tag);
} }
} else {
return false; return false;
} }
}
/**
* Function that returns the name that should be displayed for a specific tag
*
* @param object $tag_object a line out of tag table, as returned by the adobd functions
* @return string
*/
function tag_display_name($tag_object) {
global $CFG;
if(!isset($tag_object->name)) {
return '';
}
if (empty($CFG->keeptagnamecase)) {
//this is the normalized tag name
$textlib = textlib_get_instance();
return htmlspecialchars($textlib->strtotitle($tag_object->name));
} else {
//original casing of the tag name
return htmlspecialchars($tag_object->rawname);
}
}
/**
* Find all records tagged with a tag of a given type ('post', 'user', etc.)
*
* @param string $tag tag to look for
* @param string $type type to restrict search to. If null, every matching
* record will be returned
* @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
* @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
* @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;
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}'";
return get_records_sql($query, $limitfrom, $limitnum);
}
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
/////////////////// PRIVATE TAG API /////////////////// /////////////////// PRIVATE TAG API ///////////////////