You are here

private function MultichoiceQuestion::warn in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::warn()
  2. 6.x question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceQuestion::warn()

Warn the user about possible user errors.

1 call to MultichoiceQuestion::warn()
MultichoiceQuestion::saveNodeProperties in question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php
Implementation of saveNodeProperties().

File

question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceQuestion.php, line 79

Class

MultichoiceQuestion
@QuizQuestion ( id = "multichoice", label = Plugin annotation @Translation("Multiple choice question"), handlers = { "response" = "\Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse" } )

Namespace

Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion

Code

private function warn() {

  // Count the number of correct answers.
  $num_corrects = 0;
  for ($i = 0; isset($this->node->alternatives[$i]); $i++) {
    $alt =& $this->node->alternatives[$i];
    if ($alt['score_if_chosen'] > $alt['score_if_not_chosen']) {
      $num_corrects++;
    }
  }
  if ($num_corrects == 1 && $this->node->choice_multi == 1 || $num_corrects > 1 && $this->node->choice_multi == 0) {
    $link_options = array();
    if (isset($_GET['destination'])) {
      $link_options['query'] = array(
        'destination' => $_GET['destination'],
      );
    }
    $go_back = l(t('go back'), 'quiz/' . $this->node->nid . '/edit', $link_options);
    if ($num_corrects == 1) {
      Drupal::messenger()
        ->addWarning(t("Your question allows multiple answers. Only one of the alternatives have been marked as correct. If this wasn't intended please !go_back and correct it.", array(
        '!go_back' => $go_back,
      )), 'warning');
    }
    else {
      Drupal::messenger()
        ->addWarning(t("Your question doesn't allow multiple answers. More than one of the alternatives have been marked as correct. If this wasn't intended please !go_back and correct it.", array(
        '!go_back' => $go_back,
      )), 'warning');
    }
  }
}