$post->action = $action;
$strformheading = get_string('updateentrywithid', 'blog');
- if ($ptags = get_records_sql_menu("SELECT t.id, t.name FROM
- {$CFG->prefix}tag t,
- {$CFG->prefix}tag_instance ti
- WHERE t.id = ti.tagid
- AND t.tagtype = 'default'
- AND ti.itemid = {$post->id}")) {
-
+ if ($ptags = records_to_menu(get_item_tags('blog', $post->id, 'ti.ordering ASC', 'id,rawname', '', '', 'default'), 'id','rawname')) {
$post->ptags = implode(', ', $ptags);
} else {
- //$idsql = " AND bti.entryid = 0";
- //was used but seems redundant.
$post->ptags = '';
}
- if ($otags = get_records_sql_menu("SELECT t.id, t.name FROM
- {$CFG->prefix}tag t,
- {$CFG->prefix}tag_instance ti
- WHERE t.id = ti.tagid
- AND t.tagtype = 'official'
- AND ti.itemid = {$post->id}")){
+
+ if ($otags = records_to_menu(get_item_tags('blog', $post->id, 'ti.ordering ASC', 'id,rawname', '', '', 'official'), 'id','rawname')) {
$post->otags = array_keys($otags);
}
break;
*/
function recordset_to_menu($rs) {
global $CFG;
-
+ $menu = array();
if ($rs && $rs->RecordCount() > 0) {
$keys = array_keys($rs->fields);
$key0=$keys[0];
}
}
+/**
+ * Utility function
+ * Similar to recordset_to_menu
+ *
+ * field1, field2 is needed because the order from get_records_sql is not reliable
+ * @param records - records from get_records_sql() or get_records()
+ * @param field1 - field to be used as menu index
+ * @param field2 - feild to be used as coresponding menu value
+ * @return mixed an associative array, or false if an error occured or the RecordSet was empty.
+ */
+function records_to_menu($records, $field1, $field2) {
+
+ $menu = array();
+ foreach ($records as $record) {
+ $menu[$record->$field1] = $record->$field2;
+ }
+
+ if (!empty($menu)) {
+ return $menu;
+ } else {
+ return false;
+ }
+}
+
/**
* Get the first two columns from a number of records as an associative array.
*
* @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, $sort='ti.ordering ASC', $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='', $tagtype='') {
global $CFG;
$sort = ' ORDER BY '. $sort;
}
+ if ($tagtype) {
+ $tagwhere = " AND tg.tagtype = '$tagtype' ";
+ } else {
+ $tagwhere = '';
+ }
+
$query = "
SELECT
{$fields}
WHERE
ti.itemtype = '{$item_type}' AND
ti.itemid = '{$item_id}'
+ $tagwhere
{$sort}
";