You are here

userpoints_views.module in User Points 5.2

Same filename and directory in other branches
  1. 5.3 userpoints_views.module

File

userpoints_views.module
View source
<?php

/**
 * This include file implements views functionality on behalf of userpoints.module
 */
function userpoints_views_views_tables() {
  $tables['userpoints'] = array(
    'name' => 'userpoints',
    'provider' => 'internal',
    // won't show up in external list.
    'join' => array(
      'left' => array(
        'table' => 'node',
        'field' => 'uid',
      ),
      'right' => array(
        'field' => 'uid',
      ),
    ),
    'fields' => array(
      'points' => array(
        'name' => t('Node: Author Points'),
        'sortable' => TRUE,
        'option' => 'integer',
        'handler' => 'views_handler_field_int',
        'help' => t('Displays the number of userpoints that the author of the node currently has.'),
      ),
      'max_points' => array(
        'name' => t('Node: Author Maximum Points'),
        'sortable' => TRUE,
        'option' => 'integer',
        'handler' => 'views_handler_field_int',
        'help' => t('Displays the maximum number of userpoints of the author of the node.'),
      ),
    ),
    'sorts' => array(
      'points' => array(
        'name' => t('Node: Author Points'),
        'help' => t("Sort by the node's author current number of userpoints"),
      ),
      'max_points' => array(
        'name' => t('Node: Author Maximum Points'),
        'help' => t("Sort by the node's author maximum number of userpoints"),
      ),
    ),
    'filters' => array(
      'points' => array(
        'name' => t('Node: Author Points'),
        'value-type' => 'integer',
        'operator' => 'views_handler_operator_gtlt',
        'help' => t('Filter node authors based on points.'),
      ),
    ),
  );
  $tables['userpoints_comments'] = array(
    'name' => 'userpoints',
    'provider' => 'internal',
    // won't show up in external list.
    'join' => array(
      'left' => array(
        'table' => 'comments',
        'field' => 'uid',
      ),
      'right' => array(
        'field' => 'uid',
      ),
    ),
    'fields' => array(
      'points' => array(
        'name' => t('Comment: Author Points'),
        'sortable' => TRUE,
        'option' => 'integer',
        'handler' => 'views_handler_field_int',
        'help' => t('Displays the number of userpoints that the author of the comment currently has.'),
      ),
      'max_points' => array(
        'name' => t('Comment: Author Maximum Points'),
        'sortable' => TRUE,
        'option' => 'integer',
        'handler' => 'views_handler_field_int',
        'help' => t('Displays the maximum number of userpoints of the author of the comment.'),
      ),
    ),
    'sorts' => array(
      'points' => array(
        'name' => t('Comment: Author Points'),
        'help' => t("Sort by the comment's author current number of userpoints"),
      ),
      'max_points' => array(
        'name' => t('Comment: Author Maximum Points'),
        'help' => t("Sort by the comment's author maximum number of userpoints"),
      ),
    ),
    'filters' => array(
      'points' => array(
        'name' => t('Comment: Author Points'),
        'value-type' => 'integer',
        'operator' => 'views_handler_operator_gtlt',
        'help' => t('Filter comment authors based on points.'),
      ),
    ),
  );
  $tables['userpoints_txn'] = array(
    'provider' => 'internal',
    // won't show up in external list.
    'join' => array(
      'left' => array(
        'table' => 'userpoints',
        'field' => 'uid',
      ),
      'right' => array(
        'field' => 'uid',
      ),
      'type' => 'inner',
    ),
    'fields' => array(
      'points' => array(
        'name' => t('Userpoints: Transaction Points'),
        'sortable' => TRUE,
        'option' => 'integer',
        'handler' => 'views_handler_field_int',
        'help' => t('Displays the number of userpoints assigned in the transaction.'),
      ),
      'time_stamp' => array(
        'name' => t('Userpoints: Transaction Time'),
        'sortable' => TRUE,
        'handler' => views_handler_field_dates(),
        'option' => 'string',
        'help' => t('Displays the date and time of the transaction.'),
      ),
    ),
    'sorts' => array(
      'points' => array(
        'name' => t('Userpoints: Transaction Points'),
        'help' => t("Sort by the number of userpoints assigned in the transaction"),
      ),
      'time_stamp' => array(
        'name' => t('Userpoints: Transaction Time'),
        'handler' => 'views_handler_sort_date',
        'option' => views_handler_sort_date_options(),
        'help' => t("Sort by the transaction time"),
      ),
    ),
    'filters' => array(),
  );
  return $tables;
}
function userpoints_views_views_arguments() {
  $arguments = array(
    'userpoints_uid' => array(
      'name' => t('Userpoints: User ID'),
      'handler' => 'views_handler_arg_userpoints_uid',
      'option' => 'integer',
      'help' => t('The User ID argument allows to filter userpoints transactions by the specified user ID.'),
    ),
  );
  return $arguments;
}
function userpoints_views_views_default_views() {
  $view = new stdClass();
  $view->name = 'users_points';
  $view->description = 'Lists users by their points';
  $view->access = array();
  $view->view_args_php = '';
  $view->page = TRUE;
  $view->page_title = 'Users by Points';
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'table';
  $view->url = 'views/users_points';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->menu = TRUE;
  $view->menu_title = 'View - Users by Points';
  $view->menu_tab = FALSE;
  $view->menu_tab_weight = '0';
  $view->menu_tab_default = FALSE;
  $view->menu_tab_default_parent = NULL;
  $view->menu_tab_default_parent_type = 'tab';
  $view->menu_parent_tab_weight = '0';
  $view->menu_parent_title = '';
  $view->sort = array(
    array(
      'tablename' => 'userpoints',
      'field' => 'points',
      'sortorder' => 'ASC',
      'options' => '',
    ),
  );
  $view->argument = array();
  $view->field = array(
    array(
      'tablename' => 'usernode_users',
      'field' => 'name',
      'label' => 'User',
      'handler' => 'usernode_views_handler_field_username_text',
      'sortable' => '1',
    ),
    array(
      'tablename' => 'userpoints',
      'field' => 'points',
      'label' => 'Points',
      'sortable' => '1',
    ),
    array(
      'tablename' => 'userpoints',
      'field' => 'max_points',
      'label' => 'Max. Points',
      'sortable' => '1',
    ),
  );
  $view->filter = array();
  $view->exposed_filter = array();
  $view->requires = array(
    userpoints,
    usernode_users,
  );
  $views[$view->name] = $view;
  $view = new stdClass();
  $view->name = 'userpoints_txns';
  $view->description = 'Displays the userpoints transactions for a given user';
  $view->access = array();
  $view->view_args_php = '';
  $view->page = TRUE;
  $view->page_title = 'Transactions';
  $view->page_header = '';
  $view->page_header_format = '1';
  $view->page_footer = '';
  $view->page_footer_format = '1';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'table';
  $view->url = 'views/users_transactions/$arg';
  $view->use_pager = TRUE;
  $view->nodes_per_page = '10';
  $view->sort = array();
  $view->argument = array(
    array(
      'type' => 'userpoints_uid',
      'argdefault' => '1',
      'title' => '',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array(
    array(
      'tablename' => 'usernode_users',
      'field' => 'name',
      'label' => 'User',
      'handler' => 'usernode_views_handler_field_username_text',
    ),
    array(
      'tablename' => 'userpoints_txn',
      'field' => 'points',
      'label' => 'Points',
      'sortable' => '1',
    ),
    array(
      'tablename' => 'userpoints_txn',
      'field' => 'time_stamp',
      'label' => 'Time',
      'handler' => 'views_handler_field_date_small',
      'sortable' => '1',
      'defaultsort' => 'DESC',
    ),
  );
  $view->filter = array();
  $view->exposed_filter = array();
  $view->requires = array(
    usernode_users,
    userpoints_txn,
  );
  $views[$view->name] = $view;
  return $views;
}
function views_handler_arg_userpoints_uid($op, &$query, $argtype, $arg = '') {
  switch ($op) {
    case 'summary':
      $table_data = _views_get_tables();
      $joininfo = $table_data['userpoints_txn']['join'];
      $joininfo['type'] = 'inner';
      $query
        ->add_table('userpoints_txn', true, 1, $joininfo);
      $query
        ->add_field('uid', 'userpoints_txn');
      $fieldinfo['field'] = "userpoints_txn.uid";
      return $fieldinfo;
      break;
    case 'sort':
      $query
        ->add_orderby('userpoints_txn', 'uid', $argtype);
      break;
    case 'link':
      return l($query->name, "{$arg}/" . intval($query->uid));
    case 'filter':
      $uid = intval($arg);
      $table_data = _views_get_tables();
      $joininfo = $table_data['userpoints_txn']['join'];
      $joininfo['extra'] = array(
        'uid' => $uid,
      );
      $query
        ->add_table("userpoints_txn", true, 1, $joininfo);
      break;
    case 'title':
      $user = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = '%d'", $query));
      return $user->name;
  }
}