Add (an utterly ugly) comment_timer summary field to Views2 node lists
authorCSÉCSY László <[email protected]>
Sat, 28 Nov 2009 08:58:43 +0000 (28 09:58 +0100)
committerCSÉCSY László <[email protected]>
Sat, 28 Nov 2009 08:58:43 +0000 (28 09:58 +0100)
views/comment_timer.views.inc
views/comment_timer_handler_field_comment_timer.inc [new file with mode: 0644]

index 9f575c6..506d200 100644 (file)
@@ -15,6 +15,7 @@ function comment_timer_views_data() {
     'comments' => array(
       'left_field' => 'cid',
       'field' => 'cid',
+      'required' => TRUE,
     ),
   );
   $data['comment_timer']['seconds'] = array(
@@ -31,6 +32,13 @@ function comment_timer_views_data() {
       'handler' => 'views_handler_filter_numeric',
     ),
   );
+  $data['node']['comment_timer'] = array(
+    'title' => t('Time'),
+    'help' => t('Time spent with the comments of the node.'),
+    'field' => array(
+      'handler' => 'comment_timer_handler_field_comment_timer',
+    ),
+  );
   return $data;
 }
 
@@ -49,6 +57,9 @@ function comment_timer_views_handlers() {
       'comment_timer_handler_field_seconds' => array(
         'parent' => 'views_handler_field_numeric',
       ),
+      'comment_timer_handler_field_comment_timer' => array(
+        'parent' => 'views_handler_field_numeric',
+      ),
     ),
   );
 }
diff --git a/views/comment_timer_handler_field_comment_timer.inc b/views/comment_timer_handler_field_comment_timer.inc
new file mode 100644 (file)
index 0000000..703a8cf
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+// vim: set ft=php syntax=php expandtab ts=2 sw=2 autoindent smartindent:
+
+/**
+ * @file @TODO
+ */
+
+/**
+ * Views field handler for the Comment Timer's (bogus) summary field.
+ */
+class comment_timer_handler_field_comment_timer extends views_handler_field_numeric {
+  function render($values) {
+    if (!user_access('access comment timer')) {
+      // User is not permitted to access comment timer.
+      return;
+    }
+    return _comment_timer_seconds_to_hms($values->{$this->field_alias});
+  }
+  function query() {
+    // @TODO: This is terribly ugly. Please teach me the proper way to
+    // achieve the same result.
+    // @TODO: The same applies to sort and filter capabilities, which are
+    // missing, too.
+    $this->query->ensure_table('comments');
+    $this->query->tables['comment_timer'] = array('count' => 1, 'alias' => 'comment_timer');
+    $join = new views_join();
+    $join->construct('comment_timer', 'comments', 'cid', 'cid', array(), 'LEFT');
+    $this->query->table_queue['comment_timer'] = array(
+      'table' => 'comment_timer',
+      'num' => '1',
+      'alias' => 'comment_timer',
+      'join' => $join,
+      'relationship' => 'comment_timer',
+    );
+    $this->query->add_groupby('nid');
+    parent::query();
+    $this->query->fields[$this->field_alias]['field'] = 'seconds)';
+    $this->query->fields[$this->field_alias]['table'] = 'SUM(comment_timer';
+    $this->query->fields[$this->field_alias]['aggregate'] = TRUE;
+  }
+}