You are here

function userpoints_get_categories in User Points 7

Same name and namespace in other branches
  1. 5.3 userpoints.module \userpoints_get_categories()
  2. 6 userpoints.module \userpoints_get_categories()
  3. 7.2 userpoints.module \userpoints_get_categories()

Returns an array of possible categories, suitable for inclusion in FAPI.

20 calls to userpoints_get_categories()
UserpointsAdminTestCase::testAddEditPoints in tests/userpoints_api.test
UserpointsAPITestCase::testGetCategories in tests/userpoints_api.test
Tests the userpoints_get_categories() function.
UserpointsBaseTestCase::addPoints in tests/userpoints_api.test
Add points through the admin form.
userpoints_admin_settings in ./userpoints.admin.inc
Menu callback for settings form.
userpoints_admin_txn in ./userpoints.admin.inc
Form builder for add/edit userpoints transaction form.

... See full list

File

./userpoints.module, line 1642

Code

function userpoints_get_categories($account = NULL) {
  $cache = drupal_static(__FUNCTION__, array());
  $key = $account ? $account->uid : 0;
  if (!isset($cache[$key])) {

    // Create the "Uncategorized" category.
    $options = array();
    $options[0] = t('!Uncategorized', userpoints_translation());
    if (module_exists('taxonomy')) {
      $vid = userpoints_get_vid();
      if ($vid) {

        // If an account is passed, load the terms directly from the database.
        if ($account) {
          $query = db_select('taxonomy_term_data', 't')
            ->fields('t', array(
            'tid',
            'name',
          ))
            ->condition('t.vid', userpoints_get_vid())
            ->groupBy('t.tid')
            ->groupBy('t.name')
            ->groupBy('t.weight')
            ->orderBy('t.weight');
          $query
            ->join('userpoints_txn', 'p', 't.tid = p.tid AND p.uid = :uid', array(
            ':uid' => $account->uid,
          ));
          $terms = $query
            ->execute();
        }
        else {
          $terms = taxonomy_get_tree($vid);
        }
        foreach ($terms as $term) {
          $options[$term->tid] = $term->name;
        }
      }
    }
    $cache[$key] = $options;
  }
  return $cache[$key];
}