Provide time tracking for nodes themselves, too
authorCSÉCSY László <[email protected]>
Mon, 3 May 2010 21:51:50 +0000 (3 23:51 +0200)
committerCSÉCSY László <[email protected]>
Mon, 3 May 2010 21:51:50 +0000 (3 23:51 +0200)
comment_timer.install
comment_timer.module
views/comment_timer.views.inc
views/comment_timer_handler_field_alltime.inc [new file with mode: 0644]

index 3e52da7..253b0ce 100644 (file)
@@ -30,7 +30,7 @@ function comment_timer_schema() {
     ),
   );
   $schema['comment_timer_node'] = array(
-    'description' => 'Stores how much time was spent on a node\'s comments.',
+    'description' => 'Stores how much time was spent on a node and it\'s comments.',
     'fields' => array(
       'nid' => array(
         'type' => 'int',
@@ -43,6 +43,11 @@ function comment_timer_schema() {
         'not null' => TRUE,
         'default' => 0,
       ),
+      'nodeseconds' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
     ),
     'primary key' => array('nid'),
     'indexes' => array(
@@ -73,3 +78,11 @@ function comment_timer_uninstall() {
   variable_del('comment_timer_display');
 }
 
+/**
+ * Implementation of hook_update_N().
+ */
+function comment_timer_update_6001() {
+  $ret = array();
+  db_add_field($ret, 'comment_timer_node', 'nodeseconds', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
+  return $ret;
+}
index 46047df..b5f2cae 100644 (file)
@@ -92,6 +92,40 @@ function comment_timer_form_comment_form_alter(&$form, &$form_state) {
 }
 
 /**
+ * Implementation of hook_form_alter().
+ */
+function comment_timer_form_alter(&$form, $form_state, $form_id) {
+  if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
+    $node_types = variable_get('comment_timer_node_types', array());
+    if (!empty($node_types[$form['#node']->type])) {
+      if (is_array($form_state['post']) && is_array($form_state['post']['comment_timer'])) {
+        // If the form is only previewed, retrieve timer information from
+        // there. If the first textfield is not empty, consider that one
+        // instead of the (auto-measured) second one.
+        if ($form_state['post']['comment_timer']['seconds'] == '') {
+          $seconds = _comment_timer_hms_to_seconds($form_state['post']['comment_timer']['elapsed']);
+        }
+        else {
+          $seconds = _comment_timer_hms_to_seconds($form_state['post']['comment_timer']['seconds']);
+        }
+      }
+      else {
+        // If the form is simply displayed, retrieve timing information from
+        // the DB.
+        if (isset($form['nid']) && is_array($form['nid']) && isset($form['nid']['#value'])) {
+          $seconds = db_result(db_query('SELECT nodeseconds FROM {comment_timer_node} WHERE nid = %d', $form['nid']['#value']));
+        }
+        else {
+          $seconds = 0;
+        }
+      }
+      comment_timer_form_timer($form, $seconds);
+      $form['#validate'][] = 'comment_timer_form_validate';
+    }
+  }
+}
+
+/**
  * Validates comment timer time information textfield.
  */
 function comment_timer_form_validate($form, &$form_state) {
@@ -154,19 +188,33 @@ function comment_timer_nodeapi(&$node, $op, $teaser, $page) {
           return;
         }
         // Display time spent on all the comments.
-        $seconds = db_result(db_query('SELECT seconds FROM {comment_timer_node} WHERE nid = %d', $node->nid));
-        if ($seconds) {
-          $node->content['comment_timer'] = array(
-            '#value' => t('Time: @time', array('@time' => _comment_timer_seconds_to_hms($seconds))),
+        $result = db_fetch_object(db_query('SELECT seconds, nodeseconds FROM {comment_timer_node} WHERE nid = %d', $node->nid));
+        if ($result->seconds || $result->nodeseconds) {
+          $node->content['comment_timer_node'] = array(
+            '#value' => t('Node time: @time', array('@time' => _comment_timer_seconds_to_hms($result->nodeseconds))),
+            // @TODO: Add this to the CCK reorder form instead (if available).
+            '#weight' => variable_get('comment_timer_weight', 0),
+            '#prefix' => '<div class="comment-timer-node">',
+            '#suffix' => '</div>',
+          );
+          $node->content['comment_timer_comment'] = array(
+            '#value' => t('Comment time: @time', array('@time' => _comment_timer_seconds_to_hms($result->seconds))),
+            // @TODO: Add this to the CCK reorder form instead (if available).
+            '#weight' => variable_get('comment_timer_weight', 0),
+            '#prefix' => '<div class="comment-timer-comment">',
+            '#suffix' => '</div>',
+          );
+          $node->content['comment_timer_all'] = array(
+            '#value' => t('All time: @time', array('@time' => _comment_timer_seconds_to_hms($result->seconds + $result->nodeseconds))),
             // @TODO: Add this to the CCK reorder form instead (if available).
             '#weight' => variable_get('comment_timer_weight', 0),
-            '#prefix' => '<div class="comment-timer">',
+            '#prefix' => '<div class="comment-timer-all">',
             '#suffix' => '</div>',
           );
         }
         break;
       case 'load':
-        return db_fetch_array(db_query('SELECT seconds FROM {comment_timer_node} WHERE nid = %d', $node->nid));
+        return db_fetch_array(db_query('SELECT seconds, nodeseconds FROM {comment_timer_node} WHERE nid = %d', $node->nid));
         break;
       case 'delete':
         // Iterate through node's comments, DELETE each comment timer comment entry.
@@ -177,6 +225,27 @@ function comment_timer_nodeapi(&$node, $op, $teaser, $page) {
         // DELETE the comment timer node entry.
         db_query('DELETE FROM {comment_timer_node} WHERE nid = %d', $node->nid);
         break;
+      case 'insert':
+        if ($node->comment_timer['seconds'] == '') {
+          $seconds = _comment_timer_hms_to_seconds($node->comment_timer['elapsed']);
+        }
+        else {
+          $seconds = _comment_timer_hms_to_seconds($node->comment_timer['seconds']);
+        }
+        db_query('INSERT INTO {comment_timer_node} (nid, seconds, nodeseconds) VALUES (%d, 0, %d)', $node->nid, $seconds);
+        break;
+      case 'update':
+        if ($node->comment_timer['seconds'] == '') {
+          $seconds = _comment_timer_hms_to_seconds($node->comment_timer['elapsed']);
+        }
+        else {
+          $seconds = _comment_timer_hms_to_seconds($node->comment_timer['seconds']);
+        }
+        db_query('UPDATE {comment_timer_node} SET nodeseconds = %d WHERE nid = %d', $seconds, $node->nid);
+        if (!db_affected_rows()) {
+          db_query('INSERT INTO {comment_timer_node} (nid, seconds, nodeseconds) VALUES (%d, 0, %d)', $node->nid, $seconds);
+        }
+        break;
     }
   }
 }
@@ -216,12 +285,12 @@ function comment_timer_comment(&$comment, $op) {
       db_query('UPDATE {comment_timer_node} SET seconds = (SELECT SUM(seconds) FROM {comments} c INNER JOIN {comment_timer_comment} ctc ON ctc.cid = c.cid WHERE c.nid = %d) WHERE nid = %d', $comment['nid'], $comment['nid']);
       if (!db_affected_rows()) {
         $seconds = db_result(db_query('SELECT SUM(seconds) FROM {comments} c INNER JOIN {comment_timer_comment} ctc ON ctc.cid = c.cid WHERE c.nid = %d', $comment['nid']));
-        db_query('INSERT INTO {comment_timer_node} (nid, seconds) VALUES (%d, %d)', $comment['nid'], $seconds);
+        db_query('INSERT INTO {comment_timer_node} (nid, seconds, nodeseconds) VALUES (%d, %d, 0)', $comment['nid'], $seconds);
       }
       break;
     case 'delete':
       db_query('DELETE FROM {comment_timer_comment} WHERE cid = %d', $comment->cid);
-      db_query('UPDATE {comment_timer_node} SET seconds = (SELECT SUM(seconds) FROM {comments} c INNER JOIN {comment_timer_comment} ctc ON ctc.cid = c.cid WHERE c.nid = %d) WHERE nid = %d', $node->nid, $node->nid);
+      db_query('UPDATE {comment_timer_node} SET seconds = (SELECT SUM(seconds) FROM {comments} c INNER JOIN {comment_timer_comment} ctc ON ctc.cid = c.cid WHERE c.nid = %d) WHERE nid = %d', $comment->nid, $comment->nid);
   }
 }
 
index 46ea65d..7e94584 100644 (file)
@@ -41,7 +41,7 @@ function comment_timer_views_data() {
     ),
   );
   $data['comment_timer_node']['seconds'] = array(
-    'title' => t('Time'),
+    'title' => t('Comment Time'),
     'help' => t('Total time spent with the node\'s comments.'),
     'field' => array(
       'handler' => 'comment_timer_handler_field_seconds',
@@ -54,6 +54,32 @@ function comment_timer_views_data() {
       'handler' => 'views_handler_filter_numeric',
     ),
   );
+  $data['comment_timer_node']['nodeseconds'] = array(
+    'title' => t('Node time'),
+    'help' => t('Total time spent with the node.'),
+    'field' => array(
+      'handler' => 'comment_timer_handler_field_seconds',
+      'click sortable' => TRUE,
+    ),
+    'sort' => array(
+      'handler' => 'views_handler_sort',
+    ),
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
+  $data['comment_timer_node']['alltime'] = array(
+    'title' => t('All time'),
+    'help' => t('Total time spent with the node and it\'s comments.'),
+    'field' => array(
+      'handler' => 'comment_timer_handler_field_alltime',
+      'notafield' => TRUE,
+    ),
+    // @TODO: Provide a solution to be able to sort on this "field".
+    'filter' => array(
+      'handler' => 'views_handler_filter_numeric',
+    ),
+  );
   return $data;
 }
 
@@ -72,6 +98,9 @@ function comment_timer_views_handlers() {
       'comment_timer_handler_field_seconds' => array(
         'parent' => 'views_handler_field_numeric',
       ),
+      'comment_timer_handler_field_alltime' => array(
+        'parent' => 'views_handler_field_numeric',
+      ),
     ),
   );
 }
diff --git a/views/comment_timer_handler_field_alltime.inc b/views/comment_timer_handler_field_alltime.inc
new file mode 100644 (file)
index 0000000..d684ff1
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+// vim: set ft=php syntax=php expandtab ts=2 sw=2 autoindent smartindent:
+
+/**
+ * @file @TODO
+ */
+
+/**
+ * Views field handler for the Comment Timer's all time field.
+ */
+class comment_timer_handler_field_alltime extends views_handler_field_numeric {
+  function query() {
+    $this->add_additional_fields(array('comment_timer_seconds_alltime' => 'seconds', 'comment_timer_nodeseconds_alltime' => 'nodeseconds'));
+  }
+
+  function render($values) {
+    if (!user_access('access comment timer')) {
+      // User is not permitted to access comment timer.
+      return;
+    }
+    $alltime = $values->seconds + $values->nodeseconds;
+    if ($this->options['format']) {
+      return $alltime;;
+    }
+    return _comment_timer_seconds_to_hms($alltime);
+  }
+
+  function options_form(&$form, &$form_state) {
+    parent::options_form($form, $form_state);
+    $form['format'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Display as seconds'),
+      '#description' => t('Time will be displayed as seconds when enabled (instead of HH:MM:SS).'),
+      '#default_value' => $this->options['format'],
+    );
+  }
+}