function smarty_function_mttagname($args, &$ctx) { $tag = $ctx->stash('Tag'); if (!$tag) { return ''; } if (is_array($tag)) { $tag_name = $tag['tag_name']; } else { $tag_name = $tag; } if ($args['quote'] && preg_match('/ /', $tag_name)) { $tag_name = '"' . $tag_name . '"'; } elseif ($args['normalize']) { require_once "MTUtil.php"; $tag_name = tag_normalize($tag_name); } return $tag_name; }
/** * Prints the tag search results * * @param string $query text that tag names will be matched against * @param int $page current page * @param int $perpage nr of users displayed per page * @param $return if true return html string */ function tag_print_search_results($query, $page, $perpage, $return = false) { global $CFG, $USER; $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL)); $count = sizeof(tag_find_tags($query, false)); $tags = array(); if ($found_tags = tag_find_tags($query, true, $page * $perpage, $perpage)) { $tags = array_values($found_tags); } $baseurl = $CFG->wwwroot . '/tag/search.php?query=' . rawurlencode($query); $output = ''; // link "Add $query to my interests" $addtaglink = ''; if (!tag_record_tagged_with('user', $USER->id, $query)) { $addtaglink = '<a href="' . $CFG->wwwroot . '/tag/user.php?action=addinterest&sesskey=' . sesskey() . '&tag=' . rawurlencode($query) . '">'; $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) . '</a>'; } if (!empty($tags)) { // there are results to display!! $output .= print_heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) . " : {$count}", '', 3, 'main', true); //print a link "Add $query to my interests" if (!empty($addtaglink)) { $output .= print_box($addtaglink, 'box', 'tag-management-box', true); } $nr_of_lis_per_ul = 6; $nr_of_uls = ceil(sizeof($tags) / $nr_of_lis_per_ul); $output .= '<ul id="tag-search-results">'; for ($i = 0; $i < $nr_of_uls; $i++) { $output .= '<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_display_name($tag) . '</a>'; $output .= '•' . $tag_link . '<br/>'; } $output .= '</li>'; } $output .= '</ul>'; $output .= '<div> </div>'; // <-- small layout hack in order to look good in Firefox $output .= print_paging_bar($count, $page, $perpage, $baseurl . '&', 'page', false, true); } else { //no results were found!! $output .= print_heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), '', 3, 'main', true); //print a link "Add $query to my interests" if (!empty($addtaglink)) { $output .= print_box($addtaglink, 'box', 'tag-management-box', true); } } if ($return) { return $output; } else { echo $output; } }
/** * Search for tags with names that match some text * * @param string $text escaped string that the tag names will be matched against * @param boolean $ordered If true, tags are ordered by their popularity. If false, no ordering. * @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, or false if no records were found or an error occured. */ function tag_find_tags($text, $ordered = true, $limitfrom = '', $limitnum = '') { global $CFG; $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"; } else { $query = "SELECT tg.id, tg.name, tg.rawname " . "FROM {$CFG->prefix}tag tg " . "WHERE tg.name LIKE '%{$text}%'"; } return get_records_sql($query, $limitfrom, $limitnum); }
$tagform->set_data($tag); // If new data has been sent, update the tag record if ($tagnew = $tagform->get_data()) { if (has_capability('moodle/tag:manage', $systemcontext)) { if ($tag->tagtype != 'default' && (!isset($tagnew->tagtype) || $tagnew->tagtype != '1')) { tag_type_set($tag->id, 'default'); } elseif ($tag->tagtype != 'official' && $tagnew->tagtype == '1') { tag_type_set($tag->id, 'official'); } } if (!has_capability('moodle/tag:manage', $systemcontext)) { unset($tagnew->name); unset($tagnew->rawname); } else { // They might be trying to change the rawname, make sure it's a change that doesn't affect name $norm = tag_normalize($tagnew->rawname, TAG_CASE_LOWER); $tagnew->name = array_shift($norm); 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 = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat); $tagnew->timemodified = time(); if (has_capability('moodle/tag:manage', $systemcontext)) {
public function test_normalize() { $tagset = array('Cat', ' Dog ', '<Mouse', '<>', 'mouse', 'Dog'); // Test function tag_normalize() that was deprecated in 3.1. $this->assertEquals(array('Cat' => 'Cat', 'Dog' => 'Dog', '<Mouse' => 'Mouse', '<>' => '', 'mouse' => 'mouse'), tag_normalize($tagset, TAG_CASE_ORIGINAL)); $this->assertDebuggingCalled(); $this->assertEquals(array('Cat' => 'cat', 'Dog' => 'dog', '<Mouse' => 'mouse', '<>' => '', 'mouse' => 'mouse'), tag_normalize($tagset, TAG_CASE_LOWER)); $this->assertDebuggingCalled(); // Test replacement function core_tag_tag::normalize(). $this->assertEquals(array('Cat' => 'Cat', 'Dog' => 'Dog', '<Mouse' => 'Mouse', '<>' => '', 'mouse' => 'mouse'), core_tag_tag::normalize($tagset, false)); $this->assertEquals(array('Cat' => 'cat', 'Dog' => 'dog', '<Mouse' => 'mouse', '<>' => '', 'mouse' => 'mouse'), core_tag_tag::normalize($tagset, true)); }
$tagform->set_data($tag); // If new data has been sent, update the tag record if ($tagnew = $tagform->get_data()) { if (has_capability('moodle/tag:manage', $systemcontext)) { if ($tag->tagtype != 'default' && (!isset($tagnew->tagtype) || $tagnew->tagtype != '1')) { tag_type_set($tag->id, 'default'); } elseif ($tag->tagtype != 'official' && $tagnew->tagtype == '1') { tag_type_set($tag->id, 'official'); } } if (!has_capability('moodle/tag:manage', $systemcontext) && !has_capability('moodle/tag:edit', $systemcontext)) { unset($tagnew->name); unset($tagnew->rawname); } 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 ($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 = file_postupdate_standard_editor($tagnew, 'description', $editoroptions, $systemcontext, 'tag', 'description', $tag->id); tag_description_set($tag_id, $tagnew->description, $tagnew->descriptionformat); $tagnew->timemodified = time(); if (has_capability('moodle/tag:manage', $systemcontext)) { // rename tag
/** * Search for tags with names that match some text * * @param string $text escaped string that the tag names will be matched against * @param boolean $ordered If true, tags are ordered by their popularity. If false, no ordering. * @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, or false if no records were found or an error occured. */ function tag_find_tags($text, $ordered = true, $limitfrom = '', $limitnum = '') { 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\n FROM {tag} tg LEFT JOIN {tag_instance} ti ON tg.id = ti.tagid\n WHERE tg.name LIKE ?\n GROUP BY tg.id, tg.name, tg.rawname\n ORDER BY count DESC"; } else { $query = "SELECT tg.id, tg.name, tg.rawname\n FROM {tag} tg\n WHERE tg.name LIKE ?"; } $params = array("%{$text}%"); return $DB->get_records_sql($query, $params, $limitfrom, $limitnum); }
require_once '../tag/lib.php'; $action = optional_param('action', '', PARAM_ALPHA); require_login(); if (empty($CFG->usetags)) { error('Tags are disabled!'); } if (isguestuser()) { print_error('noguest'); } if (!confirm_sesskey()) { print_error('sesskey'); } switch ($action) { case 'addinterest': $id = optional_param('id', 0, PARAM_INT); $name = optional_param('name', '', PARAM_TEXT); if (empty($name) && $id) { $name = tag_name($id); } tag_an_item('user', $USER->id, $name); if (!empty($name) && !$id) { $id = tag_id(tag_normalize($name)); } redirect($CFG->wwwroot . '/tag/index.php?id=' . $id); break; case 'flaginappropriate': $id = required_param('id', PARAM_INT); tag_flag_inappropriate($id); redirect($CFG->wwwroot . '/tag/index.php?id=' . $id, get_string('responsiblewillbenotified', 'tag')); break; }
function tag_split_delim($delim, $str) { $delim = quotemeta($delim); $tags = array(); $str = trim($str); while (strlen($str) && preg_match("/^(((['\"])(.*?)[^{$delim}]*?|.*?)({$delim}\\s*|\$))/s", $str, $match)) { $str = substr($str, strlen($match[1])); $tag = isset($match[4]) && $match[4] != '' ? $match[4] : $match[2]; $tag = trim($tag); $tag = preg_replace('/\\s+/', ' ', $tag); $n8d_tag = tag_normalize($tag); if ($n8d_tag != '') { if ($tag != '') { $tags[] = $tag; } } } return $tags; }
/** * Sets 'Set' tags for groups for the forum. * Necessary to use this rather than core tag lib as that does not deal with context * and as group item ids can be the same that is an issue * Also can only have 1 unique group/tag/user record * @param int $forumid forum table id * @param int $groupid groups table id * @param array $tags array of tag rawnames e.g. Fish, frog */ public static function set_group_tags($forumid, $groupid, $tags) { global $DB, $CFG, $USER; require_once $CFG->dirroot . '/tag/lib.php'; $forum = self::get_from_id($forumid, self::CLONE_DIRECT); $context = $forum->get_context(true); $transaction = $DB->start_delegated_transaction(); // Get existing tags used. $settags = array(); $taginstances = $DB->get_records_sql("\n SELECT DISTINCT t.*, ti.id as instanceid\n FROM {tag} t\n INNER JOIN {tag_instance} ti\n ON t.id = ti.tagid\n WHERE ti.component = ? AND ti.itemtype = ? AND ti.contextid = ? AND ti.itemid = ?", array('mod_forumng', 'groups', $context->id, $groupid)); // Delete instances any not in new tags (note tag records not deleted as cleaned in cron). $tistodelete = array(); foreach ($taginstances as $tinstance) { if (!in_array($tinstance->rawname, $tags)) { $tistodelete[] = $tinstance->instanceid; } else { // Store existing tag instance used. $settags[$tinstance->instanceid] = $tinstance->rawname; } } if ($tistodelete) { list($delsql, $delparams) = $DB->get_in_or_equal($tistodelete); $DB->delete_records_select('tag_instance', "id {$delsql}", $delparams); } // Add/get new tag records. $existingtags = tag_get_id($tags, TAG_RETURN_ARRAY); // Normalize tags passed so can match to existing tags array. $normaltags = tag_normalize($tags); // Add tag instances (where needed). $ordering = 0; foreach ($normaltags as $rawname => $name) { if (in_array($rawname, $settags)) { // Pre-existing instance, skip. $ordering++; continue; } $tagid = 0; if (!array_key_exists($name, $existingtags) || empty($existingtags[$name])) { // Need to add tag. $newtag = tag_add($rawname); $tagid = array_pop($newtag); } else { $tagid = $existingtags[$name]; } // Create instance (like tag_assign()). $tag_instance_object = new stdClass(); $tag_instance_object->tagid = $tagid; $tag_instance_object->component = 'mod_forumng'; $tag_instance_object->itemid = $groupid; $tag_instance_object->itemtype = 'groups'; $tag_instance_object->contextid = $context->id; $tag_instance_object->ordering = $ordering; $tag_instance_object->timecreated = time(); $tag_instance_object->timemodified = $tag_instance_object->timecreated; $tag_instance_object->tiuserid = self::get_group_taginstance_userid($groupid, $tagid); $DB->insert_record('tag_instance', $tag_instance_object); $ordering++; } $DB->commit_delegated_transaction($transaction); }
redirect(new moodle_url($PAGE->url, array('notice' => 'resetflag'))); break; case 'changetype': require_sesskey(); if ($tagtype === 'official' || $tagtype === 'default') { if (tag_type_set($tagid, $tagtype)) { redirect(new moodle_url($PAGE->url, array('notice' => 'typechanged'))); } } redirect($PAGE->url); break; case 'addofficialtag': require_sesskey(); $otagsadd = optional_param('otagsadd', '', PARAM_RAW); $newtags = preg_split('/\\s*,\\s*/', trim($otagsadd), -1, PREG_SPLIT_NO_EMPTY); $newtags = array_filter(tag_normalize($newtags, TAG_CASE_ORIGINAL)); if (!$newtags) { redirect($PAGE->url); } foreach ($newtags as $newotag) { if ($newotagid = tag_get_id($newotag)) { // Tag exists, change the type. tag_type_set($newotagid, 'official'); } else { tag_add($newotag, 'official'); } } redirect(new moodle_url($PAGE->url, array('notice' => 'added'))); break; } echo $OUTPUT->header();
/** * Search for tags with names that match some text * * @param string $text escaped string that the tag names will be matched against * @param boolean $ordered If true, tags are ordered by their popularity. If false, no ordering. * @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, or false if no records were found or an error occured. */ function tag_find_tags($text, $ordered = true, $limitfrom = '', $limitnum = '') { global $CFG; $text = addslashes(array_shift(tag_normalize($text, TAG_CASE_LOWER))); if ($ordered) { $query = "SELECT tg.id, tg.name, tg.rawname, ti.itemtype, ti.itemid, 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"; } else { $query = "SELECT tg.id, tg.name, tg.rawname " . "FROM {$CFG->prefix}tag tg " . "WHERE tg.name LIKE '%{$text}%'"; } //funky TAO hack to prevent no published LPS from disp(laying. $tags = get_records_sql($query, $limitfrom, $limitnum); $tags = tao_filter_tags($tags); return $tags; }
/** * Function that updates tags names. * Updates only if the new name suggested for a tag doesn´t exist already. * * @param Array $tags_names_changed array of new tag names indexed by tag ids. * @return Array array of tags names that were effectively updated, indexed by tag ids. */ function tag_update_name($tags_names_changed) { $tags_names_updated = array(); foreach ($tags_names_changed as $id => $newname) { $norm_newname = tag_normalize($newname); if (!tag_exists($norm_newname) && is_tag_name_valid($norm_newname)) { $tag = tag_by_id($id); $tags_names_updated[$id] = $tag->name; // rawname keeps the original casing of the string $tag->rawname = tag_normalize($newname, false); // name lowercases the string $tag->name = $norm_newname; $tag->timemodified = time(); update_record('tag', $tag); } } return $tags_names_updated; }