/** * 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); } } } if (empty($tags)) { return true; } 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; } } }
if (!data_submitted() or !confirm_sesskey()) { break; } $changed = array(); foreach ($tagschecked as $tag_id) { if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) { //can not add new types here!! continue; } // update tag type; if (tag_type_set($tag_id, $tagtypes[$tag_id])) { $changed[] = $tag_id; } } if (!empty($changed)) { $str_changed = implode(', ', tag_get_name($changed)); $notice = $str_changed . ' -- ' . get_string('typechanged', 'tag'); } break; case 'changename': if (!data_submitted() or !confirm_sesskey()) { break; } $tags_names_changed = array(); foreach ($tagschecked as $tag_id) { if ($newnames[$tag_id] != '') { if (!($tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]))) { // if tag already exists, or is not a valid tag name, etc. $err_notice .= $newnames[$tag_id] . '-- ' . get_string('namesalreadybeeingused', 'tag') . '<br />'; } else { $tags_names_changed[$tag_id] = $newnames[$tag_id];
/** * Renders html to print list of courses tagged with particular tag * * @param int $tagid id of the tag * @return string empty string if no courses are marked with this tag or rendered list of courses */ public function tagged_courses($tagid) { global $CFG; require_once $CFG->libdir . '/coursecatlib.php'; $displayoptions = array('limit' => $CFG->coursesperpage); $displayoptions['viewmoreurl'] = new moodle_url('/course/search.php', array('tagid' => $tagid, 'page' => 1, 'perpage' => $CFG->coursesperpage)); $displayoptions['viewmoretext'] = new lang_string('findmorecourses'); $chelper = new coursecat_helper(); $searchcriteria = array('tagid' => $tagid); $chelper->set_show_courses(self::COURSECAT_SHOW_COURSES_EXPANDED_WITH_CAT)->set_search_criteria(array('tagid' => $tagid))->set_courses_display_options($displayoptions)->set_attributes(array('class' => 'course-search-result course-search-result-tagid')); // (we set the same css class as in search results by tagid) $courses = coursecat::search_courses($searchcriteria, $chelper->get_courses_display_options()); $totalcount = coursecat::search_courses_count($searchcriteria); $content = $this->coursecat_courses($chelper, $courses, $totalcount); if ($totalcount) { require_once $CFG->dirroot . '/tag/lib.php'; $heading = get_string('courses') . ' ' . get_string('taggedwith', 'tag', tag_get_name($tagid)) . ': ' . $totalcount; return $this->heading($heading, 3) . $content; } return ''; }
if (isguestuser()) { print_error('noguest'); } if (!confirm_sesskey()) { print_error('sesskey'); } switch ($action) { case 'addinterest': if (empty($tag) && $id) { // for backward-compatibility (people saving bookmarks, mostly..) $tag = tag_get_name($id); } tag_set_add('user', $USER->id, $tag); redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag)); break; case 'removeinterest': if (empty($tag) && $id) { // for backward-compatibility (people saving bookmarks, mostly..) $tag = tag_get_name($id); } tag_set_delete('user', $USER->id, $tag); redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag)); break; case 'flaginappropriate': tag_set_flag(tag_get_id($tag)); redirect($CFG->wwwroot . '/tag/index.php?tag=' . rawurlencode($tag), get_string('responsiblewillbenotified', 'tag')); break; default: print_error('unknowaction'); break; }