You are here

function _faq_ask_list_unanswered in FAQ_Ask 6.2

Same name and namespace in other branches
  1. 6 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 "unanswered" 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().
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 1554
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) {
    if ($limit < 1000) {

      // If this is a block
      return NULL;

      // Return empty content
    }
    else {

      // Snached from http://drupal.org/node/60148
      drupal_set_message("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');
  $mode = 'edit';
  $output = $extra_msg = NULL;

  // A high limit means we are doing the "unanswered" 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.
    $terms = array();
    $tresult = db_query('SELECT tid FROM {faq_expert} WHERE uid=%d', $user->uid);
    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($qry = db_rewrite_sql("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(db_rewrite_sql("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));
    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(db_rewrite_sql("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=" . $user->uid . " ORDER BY " . $order));
    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());

  // Issue #1479352 by stenjo: Compact display of unanswered questions
  // Issue #1559296 by stenjo: Display compact unanswered list threshold does not work
  global $db_type;
  $count = 1;
  if (is_object($result)) {

    // If this is an object
    $count = $result->num_rows;
  }
  else {
    if ($db_type == 'mysql') {
      $count = mysql_num_rows($result);
    }
    else {
      if ($db_type == 'pgsql') {

        // Issue #1559296 by stenjo: Display compact unanswered list threshold does not work
        $count = pg_num_rows($result);
      }
    }
  }

  // Make this postgresql safe!!!
  $compact_limit = variable_get('faq_ask_compact_limit', '');

  // What is the limit for compact display?
  $compact = $count > $compact_limit && $compact_limit > 0 ? TRUE : FALSE;

  // If in a block
  if ($limit < 1000) {
    $node = NULL;
    while ($i < $limit && ($node = db_fetch_array($result))) {
      $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;
        }
      }
      $token = _faq_ask_get_token('faq_ask/answer/' . $node['nid']);
      $options = array(
        'query' => "token={$token}",
      );
      if ($mode == 'edit') {
        $options['query'] .= '&ask=TRUE';
      }
      $items[] = l($node['title'], "faq_ask/{$mode}/" . $node['nid'], $options);
      $i++;
    }
    if (db_fetch_array($result)) {
      $i++;
    }
  }
  else {

    // We're on a page
    while ($node = db_fetch_array($result)) {
      ++$i;

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

      //Issue #1479362 by stenjo: Indication of asker notification
      $notify = _faq_ask_get_faq_notification_email($node['nid']) ? variable_get('faq_ask_asker_notify_indication', ' [n]') : '';
      $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;
        }
      }
      $token = _faq_ask_get_token('faq_ask/answer/' . $node['nid']);
      $options = array(
        'query' => "token={$token}",
      );
      if ($mode == 'edit') {
        $options['query'] .= '&ask=TRUE';
      }
      if ($prev_cat == $tid) {
        $edit_link = l($node['title'], "faq_ask/{$mode}/" . $node['nid'], $options);

        // Issue #1468970 by jlea9378: Access Denied error when picking up unanswered question
        $node_html = node_view(node_load($node['nid']));

        // Modify link in node title to point to the answer page
        $node_html = str_replace(url('node/' . $node['nid']), url("faq_ask/{$mode}/" . $node['nid'], $options), $node_html);

        //Issue #1479362 by stenjo: Indication of asker notification
        $node_html = str_replace($node['title'], $node['title'] . $notify, $node_html);
        if ($compact) {
          $items[] = l($node['title'] . $notify, "faq_ask/{$mode}/" . $node['nid'], $options);
        }
        else {
          $items[] = $node_html;
        }
      }
      else {
        if (count($items) > 0) {

          // If this is first item under a new term
          $output .= theme('item_list', $items);

          // Output what's created so far
          $items = array();

          // Clear list of items
        }
        if ($tid) {

          // If term exists
          $term = taxonomy_get_term($tid);
          $output .= '<br/><h3>' . check_plain(faq_tt("taxonomy:term:{$term->tid}:name", $term->name)) . '</h3>';
        }
        else {

          // Else this is not categorized at all
          $output .= '<br/><h3>' . t('Uncategorized') . '</h3>';
        }
        $prev_cat = $tid;
        $edit_link = l($node['title'], "faq_ask/{$mode}/" . $node['nid'], $options);
        $node_html = node_view(node_load($node['nid']));
        $node_html = str_replace(url('node/' . $node['nid']), url("faq_ask/{$mode}/" . $node['nid'], $options), $node_html);

        //Issue #1479362 by stenjo: Indication of asker notification
        $node_html = str_replace($node['title'], $node['title'] . $notify, $node_html);
        if ($compact) {
          $items[] = l($node['title'] . $notify, "faq_ask/{$mode}/" . $node['nid'], $options);
        }
        else {
          $items[] = $node_html;
        }
      }
    }
  }
  if ($i) {
    $output .= ($limit < 1000 ? $extra_msg : NULL) . theme('item_list', $items) . ($i > $limit ? l(t('more...'), 'faq_ask/unanswered', array(
      'class' => 'faq_ask_more_link',
    )) : NULL);
  }
  elseif ($limit > 1000) {
    $output .= '<p>' . t('Currently there are no unanswered questions for you to view.') . '</p>';
  }
  return $output;
}