views_handler_field_is_online.inc 1.7 KB

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