From 1acd661d07e35c0814ff3a5c845b2c8a2887877c Mon Sep 17 00:00:00 2001 From: moodler Date: Fri, 24 Aug 2007 03:20:12 +0000 Subject: [PATCH] Committing Luiz's code for MDL-10968 to add ordering for the tags --- lib/db/install.xml | 5 +++-- lib/db/upgrade.php | 11 ++++++++++ tag/lib.php | 53 ++++++++++++++++++++++++++++++++++++++-------- version.php | 2 +- 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/lib/db/install.xml b/lib/db/install.xml index 131985f1f6..d1d10a032f 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -1652,7 +1652,8 @@ - + + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index e6cb1ab4bd..7723e5c192 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -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 diff --git a/tag/lib.php b/tag/lib.php index a9cc9b4a5b..da49934a0c 100644 --- a/tag/lib.php +++ b/tag/lib.php @@ -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 diff --git a/version.php b/version.php index 25ee37dbd2..92c27dc96c 100644 --- a/version.php +++ b/version.php @@ -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 -- 2.39.5