Committing Luiz's code for MDL-10968 to add ordering for the tags
This commit is contained in:
+3
-2
@@ -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"
|
||||
>
|
||||
@@ -1652,7 +1652,8 @@
|
||||
<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"/>
|
||||
|
||||
@@ -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
|
||||
|
||||
+44
-9
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user