Fix for is_online handler for Views 2

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

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.