]> git.mjollnir.org Git - moodle.git/commitdiff
Committing Luiz's code for MDL-10968 to add ordering for the tags
authormoodler <moodler>
Fri, 24 Aug 2007 03:20:12 +0000 (03:20 +0000)
committermoodler <moodler>
Fri, 24 Aug 2007 03:20:12 +0000 (03:20 +0000)
lib/db/install.xml
lib/db/upgrade.php
tag/lib.php
version.php

index 131985f1f6a0ab6438373d22528e542d0a35462f..d1d10a032f1d62c3d576a7c0c403af75dde64564 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-<XMLDB PATH="lib/db" VERSION="20070813" COMMENT="XMLDB file for core Moodle tables"
+<XMLDB PATH="lib/db" VERSION="20070823" COMMENT="XMLDB file for core Moodle tables"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
 >
         <FIELD NAME="id" TYPE="int" LENGTH="11" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="tagid"/>
         <FIELD NAME="tagid" TYPE="int" LENGTH="11" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="itemtype"/>
         <FIELD NAME="itemtype" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="tagid" NEXT="itemid"/>
-        <FIELD NAME="itemid" TYPE="int" LENGTH="11" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" ENUM="false" PREVIOUS="itemtype"/>
+        <FIELD NAME="itemid" TYPE="int" LENGTH="11" NOTNULL="true" UNSIGNED="false" SEQUENCE="false" ENUM="false" PREVIOUS="itemtype" NEXT="ordering"/>
+        <FIELD NAME="ordering" TYPE="int" LENGTH="10" NOTNULL="false" UNSIGNED="false" SEQUENCE="false" ENUM="false" COMMENT="Maintains the order of the tag instances of an item" PREVIOUS="itemid"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for tag_instance"/>
index e6cb1ab4bd15ff11a6cc78ef5d14c3b30ed9a961..7723e5c192b422c0a7489aa19a52f438971f8b8e 100644 (file)
@@ -1749,6 +1749,17 @@ function xmldb_main_upgrade($oldversion=0) {
 
     }
 
+    if ($result && $oldversion < 2007082300) {
+
+    /// Define field ordering to be added to tag_instance table
+        $table = new XMLDBTable('tag_instance');
+        $field = new XMLDBField('ordering');
+        
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'itemid');
+
+    /// Launch add field rawname
+        $result = $result && add_field($table, $field);
+    }    
 
 /*
     /// drop old gradebook tables
index a9cc9b4a5b46175a45811af65e55c78102b36b55..da49934a0c820bb46f4b5e3c06330963dc42efe2 100644 (file)
@@ -341,26 +341,54 @@ function tag_name_from_string($tag_names_or_ids_csv) {
 
 function tag_an_item($item_type, $item_id, $tag_names_or_ids_csv, $tag_type="default") {
 
+    global $CFG;
     //convert any tag ids passed to their corresponding tag names
     $tag_names_csv = tag_name_from_string($tag_names_or_ids_csv);
-
+        
     //create the tags
     $tags_created_ids = tag_create($tag_names_csv,$tag_type);
 
+    //tag instances of an item are ordered, get the last one
+    $query = "
+        SELECT
+            MAX(ordering) max_order
+        FROM
+            {$CFG->prefix}tag_instance ti
+        WHERE
+            ti.itemtype = '{$item_type}'
+        AND
+            ti.itemid = '{$item_id}'
+        ";
+    
+    $max_order = get_field_sql($query);    
+    $tag_names = explode(',', tag_normalize($tag_names_csv));
+    
+    $ordering = array();
+    foreach($tag_names as $tag_name){
+        $ordering[$tag_name] = ++$max_order;
+    }
+    
+    //setup tag_instance object
     $tag_instance = new StdClass;
     $tag_instance->itemtype = $item_type;
     $tag_instance->itemid = $item_id;
-
+    
+    
     //create tag instances
-    foreach ($tags_created_ids as $tag_id) {
+    foreach ($tags_created_ids as $tag_normalized_name => $tag_id) {
 
         $tag_instance->tagid = $tag_id;
+        $tag_instance->ordering = $ordering[$tag_normalized_name];
+        
+        $tag_instance_exists = get_record('tag_instance', 'tagid', $tag_id, 'itemtype', $item_type, 'itemid', $item_id);
 
-        $exists = record_exists('tag_instance', 'tagid', $tag_id, 'itemtype', $item_type, 'itemid', $item_id);
-
-        if (!$exists) {
+        if (!$tag_instance_exists) {
             insert_record('tag_instance',$tag_instance);
         }
+        else {
+            $tag_instance_exists->ordering = $ordering[$tag_normalized_name];
+            update_record('tag_instance',$tag_instance_exists);
+        }
     }
 
 
@@ -462,19 +490,24 @@ function untag_an_item($item_type, $item_id, $tag_names_or_ids_csv='') {
  * 
  * @param string $item_type name of the table where the item is stored. Ex: 'user'
  * @param string $item_id id of the item beeing queried
+ * @param string $sort an order to sort the results in, a valid SQL ORDER BY parameter (default is 'ti.order ASC')
  * @param string $fields tag fields to be selected (optional, default is 'id, name, rawname, tagtype, flag')
  * @param int $limitfrom return a subset of records, starting at this point (optional, required if $limitnum is set).
  * @param int $limitnum return a subset comprising this many records (optional, required if $limitfrom is set).
  * @return mixed an array of objects, or false if no records were found or an error occured.
  */
 
-function get_item_tags($item_type, $item_id, $fields=DEFAULT_TAG_TABLE_FIELDS, $limitfrom='', $limitnum='') {
+function get_item_tags($item_type, $item_id, $sort='ti.ordering ASC', $fields=DEFAULT_TAG_TABLE_FIELDS, $limitfrom='', $limitnum='') {
 
     global $CFG;
 
     $fields = 'tg.' . $fields;
     $fields = str_replace(',', ',tg.', $fields);
 
+    if ($sort) {
+        $sort = ' ORDER BY '. $sort;
+    }
+        
     $query = "
         SELECT
             {$fields}
@@ -486,7 +519,9 @@ function get_item_tags($item_type, $item_id, $fields=DEFAULT_TAG_TABLE_FIELDS, $
             tg.id = ti.tagid
         WHERE 
             ti.itemtype = '{$item_type}' AND
-            ti.itemid = '{$item_id}'";
+            ti.itemid = '{$item_id}'
+        {$sort}
+            ";
 
     return get_records_sql($query, $limitfrom, $limitnum);
 
@@ -704,7 +739,7 @@ function related_tags($tag_name_or_id, $limitnum=10) {
     $tag_id = tag_id_from_string($tag_name_or_id);
 
     //gets the manually added related tags
-    $manual_related_tags = get_item_tags('tag',$tag_id, DEFAULT_TAG_TABLE_FIELDS);
+    $manual_related_tags = get_item_tags('tag',$tag_id, 'ti.ordering ASC',DEFAULT_TAG_TABLE_FIELDS);
     if ($manual_related_tags == false) $manual_related_tags = array();
 
     //gets the correlated tags
index 25ee37dbd2c91697ea2eb73543803a22fd9f923f..92c27dc96c534188bda4c9a5784463ff4c27db93 100644 (file)
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-    $version = 2007082200;  // YYYYMMDD = date
+    $version = 2007082300;  // YYYYMMDD = date
                             //       XY = increments within a single day
 
     $release = '1.9 Beta +';   // Human-friendly version name