From 91e568534eefa342daf5baa41d7eb258dd306062 Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Thu, 16 Nov 2006 03:37:04 +0000 Subject: [PATCH] fix for MDL-7099, do not share user tags (better interface, now using a new text field with comma separated value). Prevented the same text for both official and personal tag (tag text is unique). Fixed capability check for removing personal tags. Changed GUI so that new personal tags will be picked up from the text field. --- blog/edit.html | 37 ++++++++++++----- blog/edit.php | 46 ++++++++++++++++----- blog/tags.html | 10 +---- blog/tags.php | 94 ++++++++++++++++++------------------------- lang/en_utf8/blog.php | 2 +- 5 files changed, 105 insertions(+), 84 deletions(-) diff --git a/blog/edit.html b/blog/edit.html index f008419491..9890eab424 100755 --- a/blog/edit.html +++ b/blog/edit.html @@ -109,19 +109,38 @@    - + "; + link_to_popup_window("/blog/tags.php",'popup',get_string('tagmanagement')); } ?> - - diff --git a/blog/edit.php b/blog/edit.php index ec67d0d62c..66a4d4285e 100755 --- a/blog/edit.php +++ b/blog/edit.php @@ -52,7 +52,7 @@ $post = new object(); // editing form data $usehtmleditor = can_use_richtext_editor(); $strblogs = get_string('blogs','blog'); - +/// Main switch for processing blog entry switch ($action) { case 'add': @@ -143,7 +143,6 @@ print_footer(); die; - /***************************** edit.php functions ***************************/ /* * Delete blog post from database @@ -192,7 +191,6 @@ function do_add(&$post, &$errors) { $post->lastmodified = time(); $post->created = time(); - // Insert the new blog entry. if ($id = insert_record('post', $post)) { $post->id = $id; @@ -239,7 +237,9 @@ function do_edit(&$post, &$errors) { // update record if (update_record('post', $post)) { + // delete all tags associated with this entry delete_records('blog_tag_instance', 'entryid', $post->id); + // add them back add_tags_info($post->id); add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject); @@ -249,26 +249,50 @@ function do_edit(&$post, &$errors) { } +/** + * function to attach tags into a post + * @param int postid - id of the blog + */ function add_tags_info($postid) { + + global $USER; + $post = get_record('post', 'id', $postid); $tag = new object(); $tag->entryid = $post->id; $tag->userid = $post->userid; - $tag->timemodified = time(); + $tag->timemodified = time(); - /// Add tags information - if ($otags = optional_param('otags','', PARAM_INT)) { - foreach ($otags as $otag) { - $tag->tagid = $otag; + /// Attach official tags + if ($otags = optional_param('otags','', PARAM_INT)) { + foreach ($otags as $otag) { + $tag->tagid = $otag; insert_record('blog_tag_instance', $tag); } } - if ($ptags = optional_param('ptags','', PARAM_INT)) { + /// Attach Personal Tags + if ($ptags = optional_param('ptags','', PARAM_NOTAGS)) { + $ptags = explode(',',$ptags); foreach ($ptags as $ptag) { - $tag->tagid = $ptag; - insert_record('blog_tag_instance', $tag); + $ptag = trim($ptag); + // check for existance + // it does not matter whether it is an offical tag or personal tag + // we do not want to have 1 copy of offical tag and 1 copy of personal tag (for the same tag) + if ($ctag = get_record('tags', 'text', $ptag)) { + $tag->tagid = $ctag->id; + insert_record('blog_tag_instance', $tag); + } else { // create a personal tag + $ctag = new object; + $ctag->userid = $USER->id; + $ctag->text = $ptag; + $ctag->type = 'personal'; + if ($tagid = insert_record('tags', $ctag)) { + $tag->tagid = $tagid; + insert_record('blog_tag_instance', $tag); + } + } } } } diff --git a/blog/tags.html b/blog/tags.html index 478ade1c3e..ec54294367 100755 --- a/blog/tags.html +++ b/blog/tags.html @@ -48,7 +48,8 @@ print_heading(get_string('tagmanagement')); -
- -
- - - -
diff --git a/blog/tags.php b/blog/tags.php index 0032fda31e..5169173e90 100755 --- a/blog/tags.php +++ b/blog/tags.php @@ -1,10 +1,12 @@ bloglevel)) { error('Blogging is disabled!'); } @@ -13,29 +15,44 @@ if (isguest()) { error(get_string('noguestpost', 'blog')); } +/// blogs are site level $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); $error = ''; switch ($action) { + /// Adding an official tag from submitted value case 'addofficial': - // only approved uses can add official tags + // Double check to make sure user has capability if (!has_capability('moodle/blog:manageofficialtags', $sitecontext)) { error('Can not add official tags tags'); } if (data_submitted() and confirm_sesskey()) { + $otag = trim(required_param('otag', PARAM_NOTAGS)); - - if (get_record('tags', 'text', $otag)) { - $error = get_string('tagalready'); - break; - } - $tag = new object(); - $tag->userid = $USER->id; - $tag->text = $otag; - $tag->type = 'official'; - if (!$tagid = insert_record('tags', $tag)) { - error('Can not create tag!'); + // When adding ofical tag, we see if there's already a personal tag + // With the same Name, if there is, we just change the type + if ($tag = get_record('tags', 'text', $otag)) { + if ($tag->type == 'official') { + // official tag already exist + $error = get_string('tagalready'); + break; + } else { + $tag->type = 'official'; + update_record('tags', $tag); + $tagid = $tag->id; + } + + } else { // Brand new offical tag + + $tag = new object(); + $tag->userid = $USER->id; + $tag->text = $otag; + $tag->type = 'official'; + + if (!$tagid = insert_record('tags', $tag)) { + error('Can not create tag!'); + } } /// Write newly added tags back into window opener. @@ -48,41 +65,10 @@ switch ($action) { } break; - - case 'addpersonal': - /// Everyone can add personal tags as long as they can write blog entries. - if (!has_capability('moodle/blog:manageofficialtags', $sitecontext) - and !has_capability('moodle/blog:create', $sitecontext)) { - error('Can not add personal tags'); - } - if (data_submitted() and confirm_sesskey()) { - $ptag = trim(required_param('ptag', PARAM_NOTAGS)); - - if (get_record('tags', 'text', $ptag)) { - $error = get_string('tagalready'); - break; - } - $tag = new object(); - $tag->userid = $USER->id; - $tag->text = $ptag; - $tag->type = 'personal'; - if (!$tagid = insert_record('tags', $tag)) { - error('Can not create tag!'); - } - - /// Write newly added tags back into window opener. - echo ''; - } - - break; - + + /// Deletes a tag. case 'delete': - /// Delete a tag. + if (data_submitted() and confirm_sesskey()) { $tagids = optional_param('tags', array(), PARAM_INT); @@ -103,19 +89,17 @@ switch ($action) { continue; } - if ($tag->type == 'personal') { - if (has_capability('moodle/blog:managepersonaltags', $sitecontext)) { - //ok - can delete any personal tag - } else if (!has_capability('moodle/blog:create', $sitecontext) or $USER->id != $tag->userid) { - // no delete - you must own the tag and be able to create blog entries - continue; - } + if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) { + //can not delete + continue; } - - + + // Delete the tag itself if (!delete_records('tags', 'id', $tagid)) { error('Can not delete tag'); } + + // Deleteing all references to this tag if (!delete_records('blog_tag_instance', 'tagid', $tagid)) { error('Can not delete blog tag instances'); } diff --git a/lang/en_utf8/blog.php b/lang/en_utf8/blog.php index bcaf3262b1..3bbe80128e 100755 --- a/lang/en_utf8/blog.php +++ b/lang/en_utf8/blog.php @@ -36,7 +36,7 @@ $string['numberoftags'] = 'Number of tags to display'; $string['otags'] = 'Official tags'; $string['pagesize'] = 'Number of blog entries per Page'; $string['personalblogs'] = 'Users can only see their own blog'; -$string['ptags'] = 'User defined tags'; +$string['ptags'] = 'User defined tags (Comma separated)'; $string['publishto'] = 'Publish to'; $string['publishtonoone'] = 'Yourself (draft)'; $string['publishtosite'] = 'Anyone on this site'; -- 2.39.5