function tag_create($tag_names_csv, $tag_type="default") {
global $USER;
- $normalized_tag_names_csv = tag_normalize($tag_names_csv) ;
-
- $tags = explode(",", $normalized_tag_names_csv );
+ $tags = explode(",", $tag_names_csv );
$tag_object = new StdClass;
$tag_object->tagtype = $tag_type;
$systemcontext = get_context_instance(CONTEXT_SYSTEM);
$can_create_tags = has_capability('moodle/tag:create',$systemcontext);
- foreach ($tags as $tag_name) {
+ $norm_tag_names_csv = '';
+ foreach ($tags as $tag) {
- $tag_object->name = $tag_name;
- $tag_object->timemodified = time();
+ // rawname keeps the original casing of the string
+ $tag_object->rawname = tag_normalize($tag, false);
+
+ // name lowecases the string
+ $tag_object->name = tag_normalize($tag);
+ $norm_tag_names_csv .= $tag_object->name . ',';
+
+ $tag_object->timemodified = time();
-// if ( !record_exists('tag', 'name', $tag_name) && !empty($tag_name) && !is_numeric($tag_name) ) {
- if ( $can_create_tags && !empty($tag_name) && !is_numeric($tag_name) ) {
+ if ( $can_create_tags && !empty($tag_object->name) && !is_numeric($tag_object->name) ) {
insert_record('tag', $tag_object);
}
}
- return tags_id($normalized_tag_names_csv);
+ $norm_tag_names_csv = substr($norm_tag_names_csv,0,-1);
+
+ return tags_id( $norm_tag_names_csv );
}
* (optional, by default 'id, tagtype, name'). The first field will be used as key for the
* array so must be a unique field such as 'id'.
*/
-function get_all_tags($tag_types_csv="default", $sort='name ASC', $fields='id, tagtype, name') {
+function get_all_tags($tag_types_csv="default", $sort='name ASC', $fields='id, tagtype, name, rawname') {
if ($tag_types_csv == '*'){
return get_records('tag', '', '', $sort, $fields);
*/
function tag_id($tag_name) {
$tag = get_record('tag', 'name', trim($tag_name), '', '', '', '', 'id');
- return $tag->id;
+
+ if ($tag){
+ return $tag->id;
+ }
+ else{
+ return false;
+ }
}
/**
*/
function tag_name($tag_id) {
$tag = get_record('tag', 'id', $tag_id, '', '', '', '', 'name');
- return $tag->name;
+
+ if ($tag){
+ return $tag->name;
+ }
+ else{
+ return '';
+ }
}
/**
function tags_name($tag_ids_csv) {
+ //remove any white spaces
+ $tag_ids_csv = str_replace(' ', '', $tag_ids_csv);
+
$tag_ids_csv_with_apos = "'" . str_replace(',', "','", $tag_ids_csv ) . "'";
$tag_objects = get_records_list('tag','id', $tag_ids_csv_with_apos, "" , "name, id" );
return $tags_names;
}
+/**
+ * Function that returns the name of a tag for display.
+ *
+ * @param mixed $tag_object
+ * @return string
+ */
+function tag_display_name($tag_object){
+
+ global $CFG;
+
+ if( !empty($CFG->keeptagnamecase) ) {
+ //this is the normalized tag name
+ return mb_convert_case($tag_object->name, MB_CASE_TITLE,"UTF-8");
+ }
+ else {
+ //original casing of the tag name
+ return $tag_object->rawname;
+ }
+
+}
+
/**
* Function that retrieves a tag object by its id
*
}
/**
- * Returns comma separated ids of tags given a string with comma separated names or ids of tags
+ * In a comma separated string of ids or names of tags, replaces all tag names with their correspoding ids
*
- * Ex: tag_id_from_string('moodle, 12, education, 33, 11')
- * might return the string '10,12,22,33,11'
+ * Ex:
+ * Suppose the DB contains only the following entries in the tags table:
+ * id name
+ * 10 moodle
+ * 12 science
+ * 22 education
*
+ * tag_id_from_string('moodle, 12, education, programming, 33, 11')
+ * will return '10,12,22,,33,11'
+ *
* This is a helper function used by functions of this API to process function arguments ($tag_name_or_id)
*
* @param string $tag_names_or_ids_csv comma separated **normalized** names or ids of tags
foreach ($tag_names_or_ids as $name_or_id) {
if (is_numeric($name_or_id)){
- $tag_ids[] = $name_or_id;
+ $tag_ids[] = trim($name_or_id);
}
elseif (is_string($name_or_id)) {
$tag_ids[] = tag_id( $name_or_id );
$tag_ids_csv = implode(',',$tag_ids);
$tag_ids_csv = str_replace(' ', '', $tag_ids_csv);
- return rtrim($tag_ids_csv, ',');
+ return $tag_ids_csv;
}
/**
- * Returns comma separated **normalized** names of tags given a string with comma separated names or ids of tags
+ * In a comma separated string of ids or names of tags, replaces all tag ids with their correspoding names
+ *
+ * Ex:
+ * Suppose the DB contains only the following entries in the tags table:
+ * id name
+ * 10 moodle
+ * 12 science
+ * 22 education
*
- * Ex: tag_name_from_string('mOOdle, 12, eduCAtIOn, 33, 11')
- * might return the string 'moodle,computers,education,algorithms,software'
+ * tag_name_from_string('mOOdle, 10, HiStOrY, 17, 22')
+ * will return the string 'mOOdle,moodle,HiStOrY,,education'
*
* This is a helper function used by functions of this API to process function arguments ($tag_name_or_id)
*
$tag_names[] = tag_name($name_or_id);
}
elseif (is_string($name_or_id)) {
- $tag_names[] = tag_normalize($name_or_id);
+ $tag_names[] = trim($name_or_id);
}
}
$tag_names_csv = implode(',',$tag_names);
- return rtrim($tag_names_csv, ',');
+ return $tag_names_csv;
}
function tag_an_item($item_type, $item_id, $tag_names_or_ids_csv, $tag_type="default") {
- $norm_tag_names_or_ids_csv = tag_normalize($tag_names_or_ids_csv);
-
//convert any tag ids passed to their corresponding tag names
- $tag_names_csv = tag_name_from_string($norm_tag_names_or_ids_csv);
+ $tag_names_csv = tag_name_from_string($tag_names_or_ids_csv);
//create the tags
$tags_created_ids = tag_create($tag_names_csv,$tag_type);
return;
}
- //normalize tags names
- $norm_tag_names_or_ids_csv = tag_normalize($tag_names_or_ids_csv);
-
//convert any tag ids passed to their corresponding tag names
- $tag_names_csv = tag_name_from_string($norm_tag_names_or_ids_csv);
+ $tag_names_csv = tag_name_from_string($tag_names_or_ids_csv);
//associate the tags passed with the item
tag_an_item($item_type, $item_id, $tag_names_csv, $tag_type );
//get the ids of the tags passed
- $existing_and_new_tags_ids = tags_id( $tag_names_csv );
+ $existing_and_new_tags_ids = tags_id( tag_normalize($tag_names_csv) );
// delete any tag instance with $item_type and $item_id
// that are not in $tag_names_csv
* @return mixed an array of objects, or false if no records were found or an error occured.
*/
-function get_item_tags($item_type, $item_id, $fields='id, name, tagtype, flag', $limitfrom='', $limitnum='') {
+function get_item_tags($item_type, $item_id, $fields='id, name, rawname, tagtype, flag', $limitfrom='', $limitnum='') {
global $CFG;
* (to avoid field name ambiguity in the query, use the identifier "it" Ex: 'it.name ASC' )
* @param string $fields a comma separated list of fields to return
* (optional, by default all fields are returned). The first field will be used as key for the
- * array so must be a unique field such as 'id'.
+ * array so must be a unique field such as 'id'. To avoid field name ambiguity in the query,
+ * use the identifier "it" Ex: 'it.name, it.id' )
* @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 mixed an array of objects indexed by their ids, or false if no records were found or an error occured.
if ($ordered) {
$query = "
SELECT
- tg.id, tg.name, COUNT(ti.id) AS count
+ tg.id, tg.name, tg.rawname, COUNT(ti.id) AS count
FROM
{$CFG->prefix}tag tg
LEFT JOIN
} else {
$query = "
SELECT
- tg.id, tg.name
+ tg.id, tg.name, tg.rawname
FROM
{$CFG->prefix}tag tg
WHERE
$query = "
SELECT
- *
+ tg.id, tg.name, tg.rawname
FROM
- {$CFG->prefix}tag t
+ {$CFG->prefix}tag tg
WHERE
- t.name
+ tg.name
LIKE
'{$text}%'
";
$tags_id_csv_with_apos = stripcslashes($tag_correlation->correlatedtags);
- return get_records_select('tag', "id IN ({$tags_id_csv_with_apos})", '', 'id, name, tagtype');
+ return get_records_select('tag', "id IN ({$tags_id_csv_with_apos})", '', 'id, name, rawname, tagtype');
}
/**
* @return string **normalized** CSV tag names
*/
-function tag_normalize($tag_names_csv) {
+function tag_normalize($tag_names_csv, $lowercase=true) {
$tags = explode(',', $tag_names_csv);
// only one tag was passed
else {
- // value is converted to lowercase and all non-alphanumeric characters are removed
- //$value = preg_replace('|[^\w ]|i', '', strtolower(trim($tag_names_csv)));
- $value = preg_replace('|[\!\@\#\$\%\^\&\*\(\)\-\+\=\~\`\.\[\]\{\}\:\;\\\/\<\>\|]|i', '', moodle_strtolower(trim($tag_names_csv)));
+
+ if ($lowercase){
+ $value = moodle_strtolower($tag_names_csv);
+ }
+ else {
+ $value = $tag_names_csv;
+ }
+
+ //$value = preg_replace('|[^\w ]|i', '', strtolower(trim($tag_names_csv)));
+ $value = preg_replace('|[\!\@\#\$\%\^\&\*\(\)\-\+\=\~\`\.\[\]\{\}\:\;\?\ยด\^\\\/\<\>\|]|i', '', trim($value));
//removes excess white spaces
$value = preg_replace('/\s\s+/', ' ', $value);
foreach ($tag_objects as $tag){
//highlight tags that have been flagged as inappropriate for those who can manage them
- $tagname = $tag->name;
+ $tagname = tag_display_name($tag);
if ($tag->flag > 0 && $can_manage_tags) {
$tagname = '<span class="flagged-tag">' . $tagname . '</span>';
}
$tags = array();
foreach ($tag_objects as $tag){
- $tags[] = $tag->name;
+ $tags[] = tag_display_name($tag);
}
return implode(',', $tags);
$query = "
SELECT
- tg.id, tg.name, COUNT(ti.id) AS count, tg.flag
+ tg.id, tg.name, tg.rawname, COUNT(ti.id) AS count, tg.flag
FROM
{$CFG->prefix}tag_instance ti
INNER JOIN
global $USER, $CFG;
- $tagname = /*ucwords*/($tag_object->name);
+ $tagname = tag_display_name($tag_object);
print_box_start('box','tag-management-box');
echo $addtaglink .' | ';
}
- // only people with moodle/tag:edittags capability may edit the tag description
+ // only people with moodle/tag:edit capability may edit the tag description
if ( has_capability('moodle/tag:edit',$systemcontext) && is_item_tagged_with('user', $USER->id, $tag_object->id ) ) {
echo ' <a href="'. $CFG->wwwroot . '/tag/edit.php?id='.$tag_object->id .'">'.get_string('edittag', 'tag').'</a> | ';
}
global $USER, $CFG;
- $tagname = /*ucwords*/($tag_object->name);
+ $tagname = tag_display_name($tag_object);
$related_tags = related_tags($tag_object->id); //get_item_tags('tags',$tag_object->id);
for($i = 0; $i < $nr_of_uls; $i++) {
echo '<li>';
foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul ) as $tag) {
- $tag_link = ' <a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'.$tag->name.'</a>';
+ $tag_link = ' <a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'.tag_display_name($tag).'</a>';
echo '•' . $tag_link . '<br/>';
}
echo '</li>';
/**
* Prints a tag cloud
*
- * @param int $nr_of_tags number of tags in the cloud
+ * @param array $tagcloud array of tag objects (fields: id, name, rawname, count and flag)
+ * @param boolean $shuffle wether or not to shuffle the array passed
* @param int $max_size maximum text size, in percentage
* @param int $min_size minimum text size, in percentage
*/
$href = 'href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'"';
//highlight tags that have been flagged as inappropriate for those who can manage them
- $tagname = $tag->name;
+ $tagname = tag_display_name($tag);
if ($tag->flag > 0 && $can_manage_tags) {
- $tagname = '<span class="flagged-tag">' . $tag->name . '</span>';
+ $tagname = '<span class="flagged-tag">' . tag_display_name($tag) . '</span>';
}
$tag_link = '<li><a '.$href.' '.$title.' '. $style .'>'.$tagname.'</a></li> ';
require_once($CFG->libdir.'/tablelib.php');
//setup table
- $tablecolumns = array('name', 'owner', 'count', 'flag', 'timemodified', '');
- $tableheaders = array( get_string('name' , 'tag'),
+
+ $tablecolumns = array('id','name', 'owner', 'count', 'flag', 'timemodified', '');
+ $tableheaders = array( get_string('id' , 'tag'),
+ get_string('name' , 'tag'),
get_string('owner','tag'),
get_string('count','tag'),
get_string('flag','tag'),
$query = "
SELECT
- tg.id, tg.name, COUNT(ti.id) AS count, u.id AS owner, tg.flag, tg.timemodified
+ tg.id, tg.name, tg.rawname, COUNT(ti.id) AS count, u.id AS owner, tg.flag, tg.timemodified
FROM
{$CFG->prefix}tag_instance ti
RIGHT JOIN
//populate table with data
foreach ($taglist as $tag ){
-
- $name = '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'.$tag->name.'</a>';
+
+ $id = $tag->id;
+ $name = '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'. tag_display_name($tag) .'</a>';
$owner = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$tag->owner.'">' . $tag->owner . '</a>';
$count = $tag->count;
$flag = $tag->flag;
//if the tag if flagged, highlight it
if ($tag->flag > 0) {
+ $id = '<span class="flagged-tag">' . $id . '</span>';
$name = '<span class="flagged-tag">' . $name . '</span>';
$owner = '<span class="flagged-tag">' . $owner . '</span>';
$count = '<span class="flagged-tag">' . $count . '</span>';
$timemodified = '<span class="flagged-tag">' . $timemodified . '</span>';
}
- $data = array($name , $owner ,$count ,$flag, $timemodified, $checkbox);
+ $data = array($id, $name , $owner ,$count ,$flag, $timemodified, $checkbox);
$table->add_data($data);
}