]> git.mjollnir.org Git - moodle.git/commitdiff
Merge from 1.9
authorscyrma <scyrma>
Tue, 18 Mar 2008 08:32:56 +0000 (08:32 +0000)
committerscyrma <scyrma>
Tue, 18 Mar 2008 08:32:56 +0000 (08:32 +0000)
MDL-13594 - Fixes a display problem with "flagged" tags, code cleanup, other small fixes.
MDL-13434 - Cleaned up a bit more the api
MDL-13687 - If a manual relation is added to a tag, the opposition relation will be added at the same time.

tag/edit.php
tag/index.php
tag/lib.php
tag/user.php

index e567e77da5b27e3c9e17ceb0c5ea4d62008b3449..8fee2b462eaa0788e0ed592418948b9b54f6296c 100644 (file)
@@ -61,7 +61,7 @@ if ($tagnew = $tagform->get_data()) {
     tag_description_set($tag_id, stripslashes($tagnew->description), $tagnew->descriptionformat);
 
     if (has_capability('moodle/tag:manage', $systemcontext)) {
-        if (($tag->tagtype != 'default') && ($tagnew->tagtype != '1')) {
+        if (($tag->tagtype != 'default') && (!isset($tagnew->tagtype) || ($tagnew->tagtype != '1'))) {
             tag_type_set($tag->id, 'default');
 
         } elseif (($tag->tagtype != 'official') && ($tagnew->tagtype == '1')) {
index dfdeb6022f26c60431d69237db884de0fa9dca3d..082378072cc8fa641d1db19a8e836fc1b361f47f 100644 (file)
@@ -70,7 +70,7 @@ echo '<td valign="top" id="middle-column">';
 $tagname  = tag_display_name($tag);
 
 if ($tag->flag > 0 && has_capability('moodle/tag:manage', $systemcontext)) {
-    $tagname =  '<span class="flagged-tag">' . rawurlencode($tagname) . '</span>';
+    $tagname =  '<span class="flagged-tag">' . $tagname . '</span>';
 }
 
 print_heading($tagname, '', 2, 'headingblock header tag-heading');
index 945de06da1801c6b92b31957f8b0c7d3588f392e..064a58b02013320e5bd16587498da87650fe5799 100644 (file)
@@ -70,23 +70,29 @@ require_once($CFG->dirroot .'/tag/locallib.php');
  * @return void 
  */
 function tag_set($record_type, $record_id, $tags) {
-    global $db;
 
-    $record = array('type' => $record_type, 'id' => $record_id);
+    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);
-    $tags_to_assign = array();
+    $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, $current_id);
+            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);
+            }
         }
     }
 
@@ -103,17 +109,17 @@ function tag_set($record_type, $record_id, $tags) {
             // create new tags
             //echo "call to add tag $tag\n";
             $new_tag = tag_add($tag);
-            tag_assign($record, $new_tag[$clean_tag], $ordering);
-        } 
-        elseif ( empty($current_ids) || !in_array($tag_current_id, $current_ids) ) {
-            // assign existing tags
-            tag_assign($record, $tag_current_id, $ordering);
-        } 
-        elseif ( isset($current_ids[$ordering]) && $current_ids[$ordering] != $tag_current_id ) { 
-            // this actually checks if the ordering number points to the same tag
-            //recompute ordering, if necessary
-            //echo 'ordering changed for ', $tag, ':', $ordering, "\n";
-            tag_assign($record, $tag_current_id, $ordering);
+            $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;
         }
     }
 }
@@ -155,7 +161,7 @@ function tag_set_delete($record_type, $record_id, $tag) {
             $new_tags[] = $current_tag->name;
         }
     }
-    
+
     return tag_set($record_type, $record_id, $new_tags);
 }
 
@@ -214,7 +220,7 @@ function tag_description_set($tagid, $description, $descriptionformat) {
  **/
 function tag_get($field, $value, $returnfields='id, name, rawname') {
     if ($field == 'name') {
-        $value = moodle_strtolower($value);   // To cope with input that might just be wrong case
+        $value = addslashes(moodle_strtolower($value));   // To cope with input that might just be wrong case
     }
     return get_record('tag', $field, $value, '', '', '', '', $returnfields);
 }
@@ -305,7 +311,7 @@ function tag_get_tags_csv($record_type, $record_id, $html=TAG_RETURN_HTML, $type
 /**
  * Get an array of tag ids associated to a record.
  *
- * @param string $record the record type for which we want to get the tags
+ * @param string $record_type the record type for which we want to get the tags
  * @param int $record_id the record id for which we want to get the tags
  * @return array of tag ids, indexed and sorted by 'ordering'
  */
@@ -313,6 +319,12 @@ function tag_get_tags_ids($record_type, $record_id) {
     
     $tag_ids = array();
     foreach (tag_get_tags($record_type, $record_id) as $tag) {
+        if ( array_key_exists($tag->ordering, $tag_ids) ) {
+            // until we can add a unique constraint, in table tag_instance,
+            // on (itemtype, itemid, ordering), this is needed to prevent a bug
+            // TODO : modify database in 2.0
+            $tag->ordering++;
+        }
         $tag_ids[$tag->ordering] = $tag->id;
     }
     ksort($tag_ids);
@@ -504,14 +516,15 @@ function tag_delete($tagids) {
  * Delete one instance of a tag.  If the last instance was deleted, it will
  * also delete the tag, unless it's type is 'official'.
  *
- * @param array $record the record for which to remove the instance
+ * @param string $record_type the type of the record for which to remove the instance
+ * @param int $record_id the id of the record for which to remove the instance
  * @param int $tagid the tagid that needs to be removed
  * @return bool true on success, false otherwise
  */
-function tag_delete_instance($record, $tagid) {
+function tag_delete_instance($record_type, $record_id, $tagid) {
     global $CFG;
 
-    if ( delete_records('tag_instance', 'tagid', $tagid, 'itemtype', $record['type'], 'itemid', $record['id']) ) {
+    if ( delete_records('tag_instance', 'tagid', $tagid, 'itemtype', $record_type, 'itemid', $record_id) ) {
         if ( !record_exists_sql("SELECT * FROM {$CFG->prefix}tag tg, {$CFG->prefix}tag_instance ti ".
                 "WHERE (tg.id = ti.tagid AND ti.tagid = {$tagid} ) OR ".
                 "(tg.id = {$tagid} AND tg.tagtype = 'official')") ) {
@@ -633,24 +646,25 @@ function tag_add($tags, $type="default") {
  * Assigns a tag to a record: if the record already exists, the time and
  * ordering will be updated.
  * 
- * @param array $record the record that will be tagged
+ * @param string $record_type the type of the record that will be tagged
+ * @param int $record_id the id of the record that will be tagged
  * @param string $tagid the tag id to set on the record. 
  * @param int $ordering the order of the instance for this record
  * @return bool true on success, false otherwise
  */
-function tag_assign($record, $tagid, $ordering) {
+function tag_assign($record_type, $record_id, $tagid, $ordering) {
 
     require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM));
 
-    if ( $tag_instance_object = get_record('tag_instance', 'tagid', $tagid, 'itemtype', $record['type'], 'itemid', $record['id'], 'tagid') ) {
+    if ( $tag_instance_object = get_record('tag_instance', 'tagid', $tagid, 'itemtype', $record_type, 'itemid', $record_id, 'id') ) {
         $tag_instance_object->ordering = $ordering;
         $tag_instance_object->timemodified = time();
         return update_record('tag_instance', $tag_instance_object);
     } else { 
         $tag_instance_object = new StdClass;
         $tag_instance_object->tagid = $tagid;
-        $tag_instance_object->itemid = $record['id'];
-        $tag_instance_object->itemtype = $record['type'];
+        $tag_instance_object->itemid = $record_id;
+        $tag_instance_object->itemtype = $record_type;
         $tag_instance_object->ordering = $ordering;
         $tag_instance_object->timemodified = time();
         return insert_record('tag_instance', $tag_instance_object);
index ec8273de8d735a8e5bb40be139d69e8b39ff51d1..b683b889d70c8cbda5e5bde92a7f42e36a62b92d 100644 (file)
@@ -37,7 +37,7 @@ switch ($action) {
         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));