You are here

public function Utility::faqAskListUnanswered in FAQ_Ask 8

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 Utility::faqAskListUnanswered()
FaqAskController::askUnanswerSettings in src/Controller/FaqAskController.php
This function lists all the unanswered questions.
FaqAskUnansweredBlock::build in src/Plugin/Block/FaqAskUnansweredBlock.php
Implements \Drupal\block\BlockBase::blockBuild().

File

src/Utility.php, line 60
Contains \Drupal\faq_ask\Utility.

Class

Utility
Contains static helper functions for FAQ module.

Namespace

Drupal\faq_ask

Code

public function faqAskListUnanswered($limit) {
  $user = \Drupal::currentUser();

  // Bounce anonymous users.
  if ($user
    ->id() == 0) {

    // If this is a block.
    if ($limit < 1000) {

      // Return empty content.
      return NULL;
    }
    else {

      // Snached from http://drupal.org/node/60148.
      drupal_set_message(t("Access Denied: Please Login"));

      // This remembers where the user is coming from.
      return new RedirectResponse(URL::fromUserInput('/user/login', array(
        'query' => drupal_get_destination(),
      ))
        ->toString());
    }
  }

  // What permissions does this user have.
  $can_edit = $user
    ->hasPermission('administer faq') || $user
    ->hasPermission('administer nodes');
  $is_expert = $user
    ->hasPermission('answer question');
  $faq_ask_settings = \Drupal::config('faq_ask.settings');

  // Join the term_data table to select based on tid.
  $query = \Drupal::database()
    ->select('node_field_data', '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 into a keyed array of terms
    // indexes keyed by the term index: $terms[tid] = tid.
    $terms = \Drupal::database()
      ->select('faq_expert', 'fe')
      ->condition('uid', $user
      ->id())
      ->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.
    // AND n.uid = $user->uid (the user and the node owner are the same).
    $query
      ->condition('n.uid', $user
      ->id());
  }

  // A high limit means we are doing the "unanswered" page.
  if ($limit < 1000) {

    // Find the total number of items w/o limit.
    $totalcount = $query
      ->countQuery()
      ->execute()
      ->fetchField();

    // We are only displaying a block.
    $query
      ->range(0, $limit);
    $query
      ->orderBy('n.created');

    // Get nids.
    $nids = $query
      ->execute()
      ->fetchCol();
    if ($totalcount) {

      // Output via theme each block of nodes.
      $markup = array(
        '#theme' => 'faq_ask_unanswered_block',
        '#data' => $nids,
        '#more_link' => $totalcount > $limit,
        '#mode' => $mode,
      );
      return drupal_render($markup);
    }
    else {
      return '';
    }
  }
  $query
    ->orderBy('tid');

  // Only need the nid column.
  $result = $query
    ->execute()
    ->fetchAllKeyed();
  $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.
    $markup = array(
      '#theme' => 'faq_ask_unanswered',
      '#data' => $nodes,
      '#term' => $tid,
      '#mode' => $mode,
    );
  }
  return drupal_render($markup);
}