From eccd915b23e2ea7a1485ecceae9e89ecc2e5c3f7 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Fri, 7 Oct 2011 17:01:22 -0300 Subject: Adding active, created and changed db fields --- muamba.business.inc | 9 ++++--- muamba.db.inc | 25 ++++++++++++++++-- muamba.handlers.inc | 21 +++++++++++++++ muamba.install | 73 ++++++++++++++++++++++++++++++++++++++++++++++++----- muamba.views.inc | 48 +++++++++++++++++++++++++++++++++++ 5 files changed, 164 insertions(+), 12 deletions(-) diff --git a/muamba.business.inc b/muamba.business.inc index 0feeda8..01be0e9 100644 --- a/muamba.business.inc +++ b/muamba.business.inc @@ -139,10 +139,11 @@ function muamba_request($nid) { // Create transaction $transaction = array( - 'nid' => $nid, - 'owner' => $node->uid, - 'uid' => $user->uid, - 'status' => MUAMBA_REQUESTED, + 'nid' => $nid, + 'owner' => $node->uid, + 'uid' => $user->uid, + 'status' => MUAMBA_REQUESTED, + 'created' => REQUEST_TIME, ); // Issue item request diff --git a/muamba.db.inc b/muamba.db.inc index 3650e08..0500dd9 100644 --- a/muamba.db.inc +++ b/muamba.db.inc @@ -128,7 +128,7 @@ function muamba_current_transaction($data) { $query ->condition('m.nid', $nid, '=') - ->condition('m.status', array(muamba_ongoing()), 'IN'); + ->condition('m.active', '1', '='); $results = $query->execute()->fetchAll(); @@ -180,7 +180,28 @@ function muamba_check_availability($data) { function muamba_update_status($mid, $status) { $update = db_update('muamba') ->fields(array( - 'status' => $status, + 'status' => $status, + 'changed' => REQUEST_TIME, + )) + ->condition('mid', $mid, '=') + ->execute(); + + // Close transaction depending on the status. + if (!in_array($status, muamba_ongoing())) { + muamba_finish($mid); + } +} + +/** + * Finish a transaction. + * + * @param $mid + * Transaction id. + */ +function muamba_finish($mid) { + $update = db_update('muamba') + ->fields(array( + 'active' => 0, )) ->condition('mid', $mid, '=') ->execute(); diff --git a/muamba.handlers.inc b/muamba.handlers.inc index 16c1f38..28f5753 100644 --- a/muamba.handlers.inc +++ b/muamba.handlers.inc @@ -30,3 +30,24 @@ class views_handler_filter_muamba_status extends views_handler_filter_in_operato } } } + +/** + * Active transactions. + * + * @todo + */ +class views_handler_field_muamba_active extends views_handler_field_boolean { + /** + * Override parent::query() and don't alter query. + */ + function query() { + $this->field_alias = 'muamba_active_'. $this->position; + } + + /** + * Renders the field. + */ + function render($values) { + parent::render($values); + } +} diff --git a/muamba.install b/muamba.install index ebd737e..2ffc573 100644 --- a/muamba.install +++ b/muamba.install @@ -18,13 +18,13 @@ function muamba_schema() { $schema['muamba'] = array( 'description' => 'The base table for muamba assets.', 'fields' => array( - 'mid' => array( + 'mid' => array( 'description' => t('The primary identifier for a muamba transaction.'), 'type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE, ), - 'nid' => array( + 'nid' => array( 'description' => t('The {node}.nid of the borrowed item.'), 'type' => 'int', 'unsigned' => TRUE, @@ -33,34 +33,55 @@ function muamba_schema() { ), // This assumes that the node won't have his author changed, // which might be reasonable depending on the application. - 'owner' => array( + 'owner' => array( 'description' => t('The {user}.owner owner of an item.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, ), - 'uid' => array( + 'uid' => array( 'description' => t('The {user}.uid requesting an item.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, ), - 'thread_id' => array( + 'thread_id' => array( 'description' => t('The {thread}.thread_id for the transaction.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, ), - 'status' => array( + 'status' => array( 'description' => t('Transaction status.'), 'type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, ), + 'active' => array( + 'description' => t('Whether it is an active transaction.'), + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 1, + ), + 'created' => array( + 'description' => 'Timestamp of the transaction request', + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE, + 'default' => 0, + ), + 'changed' => array( + 'description' => 'Timestamp of the latest update', + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE, + 'default' => 0, + ), ), 'foreign keys' => array( 'node' => array( @@ -160,3 +181,43 @@ function muamba_update_7002(&$sandbox) { ) ); } + +/** + * Adds active field. + */ +function muamba_update_7003(&$sandbox) { + db_add_field('muamba', 'active', + array( + 'description' => t('Whether it is an active transaction.'), + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 1, + ) + ); +} + +/** + * Adds timestamp fields. + */ +function muamba_update_7004(&$sandbox) { + db_add_field('muamba', 'created', + array( + 'description' => 'Timestamp of the transaction request', + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE, + 'default' => 0, + ) + ); + + db_add_field('muamba', 'changed', + array( + 'description' => 'Timestamp of the latest update', + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE, + 'default' => 0, + ) + ); +} diff --git a/muamba.views.inc b/muamba.views.inc index 9eea803..409498a 100644 --- a/muamba.views.inc +++ b/muamba.views.inc @@ -106,5 +106,53 @@ function muamba_views_data() { ), ); + // Active transactions. + $data['muamba']['active'] = array( + 'title' => t('Active'), + 'help' => t('Active or inactive transactions'), + 'field' => array( + 'handler' => 'views_handler_field_muamba_active', + 'click sortable' => TRUE, + ), + 'filter' => array( + 'handler' => 'views_handler_filter_muamba_active', + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + ); + + // Created. + $data['muamba']['created'] = array( + 'title' => t('Created'), + 'help' => t('When the transaction was created.'), + 'field' => array( + 'handler' => 'views_handler_field_date', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort_date', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_date', + ), + ); + + // Changed. + $data['muamba']['changed'] = array( + 'title' => t('Created'), + 'help' => t('When the transaction was last changed.'), + 'field' => array( + 'handler' => 'views_handler_field_date', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort_date', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_date', + ), + ); + return $data; } -- cgit v1.2.3