You are here

function quiz_node_view in Quiz 8.4

Same name and namespace in other branches
  1. 6.6 quiz.module \quiz_node_view()
  2. 6.3 quiz.module \quiz_node_view()
  3. 6.5 quiz.module \quiz_node_view()

Implements hook_node_view().

File

./quiz.module, line 1415
Quiz Module

Code

function quiz_node_view(EntityInterface $node, EntityDisplay $display, $view_mode, $langcode) {
  if ($node
    ->getType() == 'quiz') {

    // drupal_alter() is deprecated in D8.
    \Drupal::moduleHandler()
      ->alter('quiz_view', $node, $teaser, $page);

    //TODO: Need to verify

    //node_invoke($node, 'prepare');

    //\Drupal::moduleHandler()->invoke('quiz', 'node_prepare_form', array($node));

    // Number of questions is needed on the statistics page.
    $node->number_of_questions = $node->number_of_random_questions + _quiz_get_num_always_questions($node
      ->getRevisionId());
    $node->content['stats'] = array(
      '#markup' => theme('quiz_view_stats', array(
        'node' => $node,
      )),
      '#weight' => 0,
    );
    $available = quiz_availability($node);
    if ($available === TRUE) {

      // Check the permission before displaying start button.
      if (\Drupal::currentUser()
        ->hasPermission('access quiz')) {

        // Add a link to the take tab as a button if this isn't a teaser view.
        if (!$teaser) {
          $quiz_form = drupal_get_form('quiz_start_quiz_button_form', $node);
          $node->content['take'] = array(
            '#markup' => drupal_render($quiz_form),
            '#weight' => 2,
          );
        }
        else {
          $node->content['take'] = array(
            '#markup' => l(t('Start quiz'), 'node/' . $node
              ->id() . '/take'),
            '#weight' => 2,
          );
        }
      }
    }
    else {
      $node->content['take'] = array(
        '#markup' => '<div class="quiz-not-available">' . $available . '</div>',
        '#weight' => 2,
      );
    }
  }
}