You are here

function _faq_ask_list_unanswered in FAQ_Ask 6

Same name and namespace in other branches
  1. 6.2 faq_ask.module \_faq_ask_list_unanswered()
  2. 7 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 "more" page by setting a very high limit.

2 calls to _faq_ask_list_unanswered()
faq_ask_block in ./faq_ask.module
Implementation of hook_block(). This creates and populates the "unanswered questions" block.
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 1072
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;

  // Bounce anonymous users.
  if ($user->uid == 0) {
    return NULL;
  }

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

  // A high limit means we are doing the "more" page.
  if ($limit > 1000) {
    $order = 'tn.tid ASC, n.created ASC';
  }
  else {
    $order = 'n.created ASC';
  }

  // Note: If the admin is also an expert, the expert-ness prevails.
  if ($is_expert) {
    $mode = 'answer';
    if ($limit < 1000) {
      $extra_msg = '<p class="faq_ask_expert_advice">' . filter_xss_admin(variable_get('faq_ask_expert_advice', _faq_ask_advice_default('expert'))) . '</p>';
    }

    // Get the expert's terms.
    $tresult = db_query('SELECT tid FROM {faq_expert} WHERE uid=%d', $user->uid);
    $terms = array();
    while ($row = db_fetch_object($tresult)) {
      $terms[$row->tid] = $row->tid;
    }

    // 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;
      }
    }

    // Join the term_data table to select based on tid.
    $result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n LEFT JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND (tn.tid IN (" . db_placeholders($terms) . ") OR tn.tid IS NULL) ORDER BY " . $order, $terms);
  }
  elseif ($can_edit) {
    $result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 ORDER BY " . $order, $terms);
    if ($limit < 1000) {
      $extra_msg = '<p class="faq_ask_expert_advice">' . filter_xss_admin(variable_get('faq_ask_admin_advice', _faq_ask_advice_default('admin'))) . '</p>';
    }
  }
  else {

    // Edit own.
    $result = db_query("SELECT n.title, n.nid, tn.tid FROM {node} n JOIN {term_node} tn USING (nid) WHERE n.type='faq' AND n.status=0 AND n.uid=%d ORDER BY " . $order, $user->uid);
    if ($limit < 1000) {
      $extra_msg = '<p class="faq_ask_expert_advice">' . filter_xss_admin(variable_get('faq_ask_asker_advice', _faq_ask_advice_default('asker'))) . '</p>';
    }
  }

  // Get unpublished nodes that are type='faq'.
  $items = array();
  $i = 0;
  $prev_cat = -1;
  $voc_list = variable_get('faq_ask_vocabularies', array());
  while ($node = db_fetch_array($result)) {
    ++$i;

    // If we've reached the limit, then quit.
    if ($i > $limit) {
      break;
    }
    $tid = $node['tid'];
    if ($tid) {

      // We need to skip terms that are not in our vocabularies.
      $term = taxonomy_get_term($tid);
      if (!in_array($term->vid, $voc_list)) {
        --$i;
        continue;
      }
    }
    if ($prev_cat == $tid || $limit < 1000) {
      $items[] = l($node['title'], 'faq_ask/' . $mode . '/' . $node['nid']);
    }
    else {
      if (count($items) > 0) {
        $output .= theme('item_list', $items);
        $items = array();
      }
      if ($tid) {
        $term = taxonomy_get_term($tid);
        $output .= '<br/><h3>' . check_plain($term->name) . '</h3>';
      }
      else {
        $output .= '<br/><h3>' . t('Uncategorized') . '</h3>';
      }
      $prev_cat = $tid;
      $items[] = l($node['title'], 'faq_ask/' . $mode . '/' . $node['nid']);
    }
  }
  if ($i) {
    $output .= theme('item_list', $items) . ($i > $limit ? l(t('more...'), 'faq_ask/more', array(
      'class' => 'faq_ask_more_link',
    )) : NULL) . ($limit < 1000 ? $extra_msg : NULL);
  }
  elseif ($limit > 1000) {
    $output .= '<p>' . t('Currently there are no unanswered questions for you to view.') . '</p>';
  }
  return $output;
}