]> gitweb.fluxo.info Git - drupal/muamba.git/commitdiff
Adding active, created and changed db fields
authorSilvio Rhatto <rhatto@riseup.net>
Fri, 7 Oct 2011 20:01:22 +0000 (17:01 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Fri, 7 Oct 2011 20:01:22 +0000 (17:01 -0300)
muamba.business.inc
muamba.db.inc
muamba.handlers.inc
muamba.install
muamba.views.inc

index 0feeda8dbd2823200a0ddab81e2ac8547623e25f..01be0e9c48c3a6ec0ac0b3fbb5ec7e32cf4736ef 100644 (file)
@@ -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
index 3650e08c0c5fc7e9f2074c5e64afadb4e7e596f7..0500dd9d30743c6bfc7b5622bea8470687fc9e6c 100644 (file)
@@ -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();
index 16c1f38e02df462022de9155a84eff8fc22410c3..28f5753dfcd1341e7c2555a62c58544fb3ea3dd1 100644 (file)
@@ -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);
+  }
+}
index ebd737e9b321372d88c6e4a38415551b77bd12fb..2ffc573ce8aaece8fc21fd41d6a9238832b81603 100644 (file)
@@ -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,
+      )
+  );
+}
index 9eea80356709afd58a2f58135457b570b21e9434..409498ac5de1271e49e08f20c61a6b3682061240 100644 (file)
@@ -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;
 }