Skip to content

Commit

Permalink
Merged tag_get()
Browse files Browse the repository at this point in the history
  • Loading branch information
moodler committed Feb 27, 2008
1 parent 05766b5 commit 2f4b82f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
9 changes: 3 additions & 6 deletions tag/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,19 @@
} else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name
$tagnew->name = array_shift(tag_normalize($tagnew->rawname, TAG_CASE_LOWER));

if (!$tagold = tag_get_tag_by_id($tag_id)) { // For doing checks
error('Error finding tag record');
}

if ($tagold->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
if ($tag->name != $tagnew->name) { // The name has changed, let's make sure it's not another existing tag
if (tag_get_id($tagnew->name)) { // Something exists already, so flag an error
$errorstring = s($tagnew->rawname).': '.get_string('namesalreadybeeingused', 'tag');
}
}
}

if (empty($errorstring)) { // All is OK, let's save it

$tagnew->timemodified = time();

// rename tag if needed
if (tag_rename($tag_id, $tagnew->rawname)) {
if (!tag_rename($tag->id, $tagnew->rawname)) {
error('Error updating tag record');
}

Expand Down
37 changes: 27 additions & 10 deletions tag/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ function tag_find_records($tag, $type, $limitfrom='', $limitnum='') {
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 $value the required value of the aforementioned field
* @param string $returnfields which fields do we want returned?
* @return tag object
*
**/
function tag_get($field, $value, $returnfields='id, name, rawname') {
return get_record('tag', $field, $value, '', '', '', '', $returnfields);
}


/**
* 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.
Expand Down Expand Up @@ -321,9 +336,7 @@ function tag_get_id($tags, $return_value=null) {
* @return object a line returned from get_recordset_sql, or false
*/
function tag_get_tag_by_id($tagid) {
global $CFG;
$rs = get_recordset_sql("SELECT * FROM {$CFG->prefix}tag WHERE id = $tagid");
return rs_fetch_next_record($rs);
return get_record('tag', 'id', $tagid);
}

/**
Expand Down Expand Up @@ -393,20 +406,24 @@ function tag_get_related_tags_csv($related_tags, $html=TAG_RETURN_HTML) {
*/
function tag_rename($tagid, $newrawname) {

// prevent renaming to an invalid name
if (! $newrawname_clean = array_shift(tag_normalize($newrawname, TAG_CASE_ORIGINAL)) ) {
return false;
}

$current_tag = tag_get_id($newtag_clean, TAG_RETURN_OBJECT);
if ($current_tag && ($current_tag->rawname == $newrawname_clean)) {
// 'newrawname' is already in use and merging tags is not supported.
return false;
if (! $newname_clean = moodle_strtolower($newrawname_clean)) {
return false;
}

if ($tag = get_record('tag', 'id', $tagid, '', '', '', '', 'id')) {
// 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;
}
}

if ($tag = tag_get('id', $tagid, 'id, name, rawname')) {
$tag->rawname = addslashes($newrawname_clean);
$tag->name = addslashes(moodle_strtolower($newrawname_clean));
$tag->name = addslashes($newname_clean);
$tag->timemodified = time();
return update_record('tag', $tag);
}
Expand Down

0 comments on commit 2f4b82f

Please sign in to comment.