/** * 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) { static $in_recursion_semaphore = false; // this is to prevent loops when tagging a tag if ($record_type == 'tag' && !$in_recursion_semaphore) { $current_tagged_tag_name = tag_get_name($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); // 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_type, $record_id, $current_id); if ($record_type == 'tag' && !$in_recursion_semaphore) { // 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); } } } foreach ($tags as $ordering => $tag) { $tag = trim($tag); if (!$tag) { continue; } $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_current_id = $new_tag[$clean_tag]; } tag_assign($record_type, $record_id, $tag_current_id, $ordering); // if we are tagging a tag (adding a manually-assigned related tag), we // need to create the opposite relationship as well. if ($record_type == 'tag' && !$in_recursion_semaphore) { $in_recursion_semaphore = true; tag_set_add('tag', $tag_current_id, $current_tagged_tag_name); $in_recursion_semaphore = false; } } }
/** * Stores a tag for a course for a user * * @uses $CFG * @param array $tags simple array of keywords to be stored * @param integer $courseid * @param integer $userid * @param string $tagtype official or default only * @param string $myurl optional for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid=0, $tagtype='official', $myurl='') { global $CFG; if (is_array($tags) and !empty($tags)) { foreach($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!$tagid = tag_get_id($tag)) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[moodle_strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid); //logging - note only for user added tags if ($tagtype == 'default' and $myurl != '') { $url = $myurl.'?query='.urlencode($tag); add_to_log($courseid, 'coursetags', 'add', $url, 'Course tagged'); } } } } }
/** * Stores a tag for a course for a user * * @package core_tag * @category tag * @param array $tags simple array of keywords to be stored * @param int $courseid the id of the course we wish to store a tag for * @param int $userid the id of the user we wish to store a tag for * @param string $tagtype official or default only * @param string $myurl (optional) for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid = 0, $tagtype = 'official', $myurl = '') { global $CFG; if (is_array($tags) and !empty($tags)) { foreach ($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!($tagid = tag_get_id($tag))) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[core_text::strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid, 'core', context_course::instance($courseid)->id); } } } }
/** * Stores a tag for a course for a user * * @deprecated since 3.0 * @package core_tag * @category tag * @param array $tags simple array of keywords to be stored * @param int $courseid the id of the course we wish to store a tag for * @param int $userid the id of the user we wish to store a tag for * @param string $tagtype official or default only * @param string $myurl (optional) for logging creation of course tags */ function coursetag_store_keywords($tags, $courseid, $userid = 0, $tagtype = 'official', $myurl = '') { debugging('Function coursetag_store_keywords() is deprecated. Userid is no longer used for tagging courses.', DEBUG_DEVELOPER); global $CFG; require_once $CFG->dirroot . '/tag/lib.php'; if (is_array($tags) and !empty($tags)) { foreach ($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags //add tag if does not exist if (!($tagid = tag_get_id($tag))) { $tag_id_array = tag_add(array($tag), $tagtype); $tagid = $tag_id_array[core_text::strtolower($tag)]; } //ordering $ordering = 0; if ($current_ids = tag_get_tags_ids('course', $courseid)) { end($current_ids); $ordering = key($current_ids) + 1; } //set type tag_type_set($tagid, $tagtype); //tag_instance entry tag_assign('course', $courseid, $tagid, $ordering, $userid, 'core', context_course::instance($courseid)->id); } } } }