Fix for Fix for is_online handler for Views 2

  1. <?php
  2. /**
  3.  * Is user online handler.
  4.  */
  5. class views_handler_field_is_online extends views_handler_field_boolean {
  6.   function construct() {
  7.     parent::construct();
  8.     $this->additional_fields['uid'] = array('field' => 'uid');
  9.   }
  10.  
  11.   function query() {
  12.     $this->add_additional_fields();
  13.     // Currently Views has no support for/information on the {sessions} table.
  14.     $join = new views_join;
  15.     $join->construct('sessions', $this->table_alias, 'uid', 'uid', array(), 'INNER');
  16.     $session = $this->query->ensure_table('sessions', NULL, $join);
  17.  
  18.     $this->query->distinct = TRUE;
  19.     // We use an IF for MySQL/PostgreSQL compatibility. Otherwise PostgreSQL
  20.     // would return 'f' and 't'.
  21.     $sql = "IF((". time() ." - MAX($session.timestamp)) < 900, 1, 0)";
  22.     // We liberally steal from views_handler_sort_formula and
  23.     // views_handler_filter_search here.
  24.     $this->field_alias = $this->query->add_field(NULL, $sql, $this->table_alias .'_'. $this->field, array('aggregate' => TRUE));
  25.   }
  26.  
  27.   function option_definition() {
  28.     $options = parent::option_definition();
  29.  
  30.     $options['type'] = array('default' => 'online-offline');
  31.  
  32.     return $options;
  33.   }
  34.  
  35.   /**
  36.    * Add the online-offline type to options form.
  37.    */
  38.   function options_form(&$form, &$form_state) {
  39.     parent::options_form($form, $form_state);
  40.     $form['type']['#options']['online-offline'] = t('Online/Offline');
  41.   }
  42.  
  43.   function render($values) {
  44.     $value = $values->{$this->field_alias};
  45.     if (!empty($this->options['not'])) {
  46.       $value = !$value;
  47.     }
  48.     if ($this->options['type'] == 'online-offline') {
  49.       return $value ? t('Online') : t('Offline');
  50.     }
  51.     else {
  52.       return parent::render($values);
  53.     }
  54.   }
  55. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.