Permalink
Browse files

Essential patch for MDL-11992 course tagging. This prevents a bug tha…

…t removes offical tags for a course when a user adds a personal tag to that course.
  • Loading branch information...
1 parent 3a3931d commit b91de8a59968213080b77584007690950f18aad5 jbeedell committed Sep 5, 2008
Showing with 46 additions and 39 deletions.
  1. +3 −2 blocks/tags/coursetags.js
  2. +2 −2 blocks/tags/styles.php
  3. +2 −0 lang/en_utf8/tag.php
  4. +6 −2 lib/db/upgrade.php
  5. +1 −1 tag/coursetags_add.php
  6. +1 −6 tag/coursetags_edit.php
  7. +20 −13 tag/coursetagslib.php
  8. +11 −13 tag/lib.php
View
5 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() {
View
4 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;
View
2 lang/en_utf8/tag.php
@@ -49,6 +49,8 @@
$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';
View
8 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);
View
2 tag/coursetags_add.php
@@ -19,7 +19,7 @@
$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');
View
7 tag/coursetags_edit.php
@@ -43,12 +43,7 @@
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) {
View
33 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);
- }
}
}
}
View
24 tag/lib.php
@@ -63,10 +63,9 @@
* @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

0 comments on commit b91de8a

Please sign in to comment.