You are here

function _faq_ask_list_unanswered in FAQ_Ask 7

Same name and namespace in other branches
  1. 6.2 faq_ask.module \_faq_ask_list_unanswered()
  2. 6 faq_ask.module \_faq_ask_list_unanswered()

This is the code to select the Unanswered Questions for the block. It is also used by the "unanswered" page by setting a very high limit.

2 calls to _faq_ask_list_unanswered()
faq_ask_block_view in ./faq_ask.module
Implements hook_block_view().
faq_ask_list_more in ./faq_ask.module
This function lists all the unanswered questions the user is allowed to see. It is used by the "more..." link from the block, but can also be called independently.

File

./faq_ask.module, line 2035
This module is an add-on to the FAQ module that allows users with the 'ask question' permission to create a question which will be queued for an 'expert' to answer.

Code

function _faq_ask_list_unanswered($limit) {
  global $user;
  $output = '';

  // Bounce anonymous users.
  if ($user->uid == 0) {
    if ($limit < 1000) {

      // If this is a block
      return NULL;

      // Return empty content
    }
    else {

      // Snached from http://drupal.org/node/60148
      drupal_set_message(t("Access Denied: Please Login"));
      $dest = drupal_get_destination();
      drupal_goto('user/login', $dest);

      // this remembers where the user is coming from
    }
  }

  // What permissions does this user have?
  $can_edit = user_access('administer faq') || user_access('administer nodes');
  $is_expert = user_access('answer question');

  // Find the vocabulary to search for...
  $vocabulary = taxonomy_vocabulary_load_multiple(variable_get('faq_ask_vocabularies', 0));

  // Join the term_data table to select based on tid.
  $query = db_select('node', 'n');
  $query
    ->leftJoin('faq_ask_term_index', 'ti', 'n.nid = ti.nid OR ti.tid IS NULL');
  $query
    ->addField('n', 'nid');
  $query
    ->addField('ti', 'tid');
  $query
    ->condition('n.status', 0);
  $query
    ->condition('n.type', 'faq');
  $mode = 'edit';

  // Note: If the admin is also an expert, the expert-ness prevails.
  if ($is_expert) {
    $mode = 'answer';

    // Get all the expert's terms from the database into a keyed array of term indexes keyed by the term index: $terms[tid] = tid
    $terms = db_select('faq_expert', 'fe')
      ->condition('uid', $user->uid)
      ->fields('fe')
      ->execute()
      ->fetchAllKeyed(1, 1);

    // Check if this expert has any categories.
    if (count($terms) == 0) {
      if ($limit > 1000) {
        return '<p>' . t("For some strange reason, I couldn't find any categories for you.") . '</p>';
      }
      else {
        return NULL;
      }
    }

    // find the nodes that are in our terms or does not have a term
    $query
      ->condition(db_or()
      ->condition('tid', $terms, 'IN')
      ->isNull('tid'));
  }
  elseif (!$can_edit) {

    // If not expert and cannot edit the node by permission - edit own
    $query
      ->condition('n.uid', $user->uid);

    // AND n.uid = $user->uid (the user and the node owner are the same)
  }

  // A high limit means we are doing the "unanswered" page.
  if ($limit < 1000) {
    $totalcount = $query
      ->countQuery()
      ->execute()
      ->fetchField();

    // Find the total number of items w/o limit
    $query
      ->range(0, $limit);

    // We are only displaying a block
    $query
      ->orderBy('n.created');
    $nids = $query
      ->execute()
      ->fetchCol();

    // Get nids
    if ($totalcount) {
      return theme('faq_ask_unanswered_block', array(
        'data' => $nids,
        'more_link' => $totalcount > $limit,
        'mode' => $mode,
      ));
    }
    else {
      return '';
    }
  }
  $query
    ->orderBy('tid');

  // Only need the nid column.
  $result = $query
    ->execute()
    ->fetchAllKeyed();

  // Get fts
  $data = array();

  // Rearrange so that we have an array indexed by tid => array(nids)
  foreach ($result as $nid => $tid) {
    if (empty($data[$tid])) {
      $data[$tid] = array();
    }
    $data[$tid][] = $nid;
  }
  foreach ($data as $tid => $nodes) {

    // Output via theme each block of nodes
    $output .= theme('faq_ask_unanswered', array(
      'data' => $nodes,
      'term' => $tid,
      'mode' => $mode,
    ));
  }
  return $output;
}