From b91de8a59968213080b77584007690950f18aad5 Mon Sep 17 00:00:00 2001 From: jbeedell Date: Fri, 5 Sep 2008 14:49:06 +0000 Subject: [PATCH] Essential patch for MDL-11992 course tagging. This prevents a bug that removes offical tags for a course when a user adds a personal tag to that course. --- blocks/tags/coursetags.js | 5 +++-- blocks/tags/styles.php | 4 ++-- lang/en_utf8/tag.php | 2 ++ lib/db/upgrade.php | 8 ++++++-- tag/coursetags_add.php | 2 +- tag/coursetags_edit.php | 7 +------ tag/coursetagslib.php | 33 ++++++++++++++++++++------------- tag/lib.php | 24 +++++++++++------------- 8 files changed, 46 insertions(+), 39 deletions(-) diff --git a/blocks/tags/coursetags.js b/blocks/tags/coursetags.js index de4f1780c2..783146fc05 100644 --- a/blocks/tags/coursetags.js +++ b/blocks/tags/coursetags.js @@ -45,10 +45,11 @@ function ctags_getKeywords() { } } document.forms['coursetag'].coursetag_sug_keyword.value = sug_disp; - if (!sug.length || input == sug_disp) + if (!sug.length || input == sug_disp) { document.getElementById('coursetag_sug_btn').style.display = "none"; - else + } else { document.getElementById('coursetag_sug_btn').style.display = "block"; + } } function ctags_setKeywords() { diff --git a/blocks/tags/styles.php b/blocks/tags/styles.php index ef74336922..e7344b1844 100644 --- a/blocks/tags/styles.php +++ b/blocks/tags/styles.php @@ -62,7 +62,7 @@ padding-top: 4px; } -/* Styles for edit_tags.php */ +/* Styles for coursetag_edit.php */ .coursetag_edit_centered { position: relative; @@ -93,7 +93,7 @@ display: none; } -/* Styles for more_tags.php */ +/* Styles for coursetag_more.php */ .coursetag_more_title { margin: 30px 30px -25px 30px; diff --git a/lang/en_utf8/tag.php b/lang/en_utf8/tag.php index 256291ebcb..8fdd76ba09 100644 --- a/lang/en_utf8/tag.php +++ b/lang/en_utf8/tag.php @@ -49,6 +49,8 @@ $string['tagtype_default'] = 'Default'; $string['tagindex_coursetitle'] = 'Courses tagged with \"$a\"'; $string['tags'] = 'Tags'; $string['tagsaredisabled'] = 'Tags are disabled'; +$string['thingtaggedwith'] = '\"$a->name\" is used once'; +$string['thingstaggedwith'] = '\"$a->name\" is used $a->count times'; $string['thistaghasnodesc'] = 'This tag currently has no description.'; $string['timemodified'] = 'Modified'; $string['typechanged'] = 'Tag type changed'; diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index a98a1845fc..e9c1901c53 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -154,10 +154,14 @@ function xmldb_main_upgrade($oldversion) { // modify index $index = new xmldb_index('itemtype-itemid-tagid'); $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid')); - $dbman->drop_index($table, $index); + if ($dbman->index_exists($table, $index)) { + $dbman->drop_index($table, $index); + } $index = new xmldb_index('itemtype-itemid-tagid-tiuserid'); $index->set_attributes(XMLDB_INDEX_UNIQUE, array('itemtype', 'itemid', 'tagid', 'tiuserid')); - $dbman->add_index($table, $index); + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } /// Main savepoint reached upgrade_main_savepoint($result, 2008063001); diff --git a/tag/coursetags_add.php b/tag/coursetags_add.php index 268f6e7468..acdaf1d1bf 100644 --- a/tag/coursetags_add.php +++ b/tag/coursetags_add.php @@ -19,7 +19,7 @@ $keyword = optional_param('coursetag_new_tag', '', PARAM_TEXT); $courseid = optional_param('entryid', 0, PARAM_INT); $userid = optional_param('userid', 0, PARAM_INT); -$keyword = trim(strip_tags($keyword)); //better cleanup of user input is done later +$keyword = trim(strip_tags($keyword)); if ($keyword and confirm_sesskey()) { require_once($CFG->dirroot.'/tag/coursetagslib.php'); diff --git a/tag/coursetags_edit.php b/tag/coursetags_edit.php index 36c8efbff7..124a1ab0de 100644 --- a/tag/coursetags_edit.php +++ b/tag/coursetags_edit.php @@ -43,12 +43,7 @@ if ($data = data_submitted()) { if (trim(strip_tags($keyword))) { $myurl = 'tag/search.php'; $keywords = explode(',', $keyword); - try { - coursetag_store_keywords($keywords, $courseid, $USER->id, 'default', $myurl); - } - catch (Exception $e) { - notice($e->getmessage()); - } + coursetag_store_keywords($keywords, $courseid, $USER->id, 'default', $myurl); } // delete personal tag if ($deltag > 0) { diff --git a/tag/coursetagslib.php b/tag/coursetagslib.php index cc898ba94b..685e96077f 100644 --- a/tag/coursetagslib.php +++ b/tag/coursetagslib.php @@ -357,26 +357,33 @@ function coursetag_store_keywords($tags, $courseid, $userid=0, $tagtype='officia global $CFG; if (is_array($tags) and !empty($tags)) { - - //tag_set_add('course', $courseid, $tags, $userid); - //if ($tagtype == 'official') { - // $tags_ids = tag_get_id($tags); - //} - foreach($tags as $tag) { $tag = trim($tag); if (strlen($tag) > 0) { - tag_set_add('course', $courseid, $tag, $userid); - if ($myurl) { - $url = $myurl.'?query='.urlencode($tag); + //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 != '') { - // log the tagging request - note only for user added tags + $url = $myurl.'?query='.urlencode($tag); add_to_log($courseid, 'coursetags', 'add', $url, 'Course tagged'); } - if ($tagtype == 'official') { - tag_type_set(tag_get_id($tag), $tagtype); - } } } } diff --git a/tag/lib.php b/tag/lib.php index 8c576b45d9..5c6229d3a9 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -63,10 +63,9 @@ define('TAG_RELATED_CORRELATED', 2); * @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. - * @param int $userid optional only required for course tagging * @return void */ -function tag_set($record_type, $record_id, $tags, $userid = 0) { +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) { @@ -109,7 +108,7 @@ function tag_set($record_type, $record_id, $tags, $userid = 0) { $tag_current_id = $new_tag[$clean_tag]; } - tag_assign($record_type, $record_id, $tag_current_id, $ordering, $userid); + 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. @@ -128,18 +127,17 @@ function tag_set($record_type, $record_id, $tags, $userid = 0) { * 'user' for users, etc. * @param int $record_id the id of the record to tag * @param string $tag the tag to add - * @param int $userid optional only required for course tagging * @return void */ -function tag_set_add($record_type, $record_id, $tag, $userid = 0) { +function tag_set_add($record_type, $record_id, $tag) { $new_tags = array(); - foreach( tag_get_tags($record_type, $record_id, NULL, $userid) as $current_tag ) { + foreach( tag_get_tags($record_type, $record_id) as $current_tag ) { $new_tags[] = $current_tag->rawname; } $new_tags[] = $tag; - return tag_set($record_type, $record_id, $new_tags, $userid); + return tag_set($record_type, $record_id, $new_tags); } /** @@ -550,10 +548,10 @@ function tag_delete_instance($record_type, $record_id, $tagid) { if (!$DB->record_exists_sql("SELECT * ". "FROM {tag} tg ". "WHERE tg.id = ? AND ( tg.tagtype = 'official' OR ". - "EXISTS (SELECT 1 - FROM {tag_instance} ti - WHERE ti.tagid = ?) )", - array($tagid, $tagid))) { + "EXISTS (SELECT 1 + FROM {tag_instance} ti + WHERE ti.tagid = ?) )", + array($tagid, $tagid))) { return tag_delete($tagid); } } else { @@ -592,7 +590,7 @@ function tag_display_name($tagobject, $html=TAG_RETURN_HTML) { return $tagname; } else { // TAG_RETURN_HTML return htmlspecialchars($tagname); - } + } } /** @@ -801,7 +799,7 @@ function tag_compute_correlations($min_correlation=2) { // query that counts how many times any tag appears together in items // with the tag passed as argument ($tag_id) - $query = "SELECT tb.tagid + $query = "SELECT tb.tagid FROM {tag_instance} ta JOIN {tag_instance} tb ON ta.itemid = tb.itemid WHERE ta.tagid = ? AND tb.tagid <> ? GROUP BY tb.tagid -- 2.39.5