You are here

function Quiz::addQuestion in Quiz 8.6

Same name and namespace in other branches
  1. 8.5 src/Entity/Quiz.php \Drupal\quiz\Entity\Quiz::addQuestion()
  2. 6.x src/Entity/Quiz.php \Drupal\quiz\Entity\Quiz::addQuestion()

Add a question to this quiz.

@todo return value may change

Parameters

QuizQuestion $quiz_question:

Return value

\Drupal\quiz\Entity\QuizQuestionRelationship Newly created or found QuizQuestionRelationship.

File

src/Entity/Quiz.php, line 425

Class

Quiz
Defines the Quiz entity class.

Namespace

Drupal\quiz\Entity

Code

function addQuestion(QuizQuestion $quiz_question) {
  $relationships = Drupal::entityTypeManager()
    ->getStorage('quiz_question_relationship')
    ->loadByProperties([
    'quiz_id' => $this
      ->id(),
    'quiz_vid' => $this
      ->getRevisionId(),
    'question_id' => $quiz_question
      ->id(),
    'question_vid' => $quiz_question
      ->getRevisionId(),
  ]);
  if (empty($relationships)) {

    // Save a new relationship.
    $qqr = QuizQuestionRelationship::create([
      'quiz_id' => $this
        ->id(),
      'quiz_vid' => $this
        ->getRevisionId(),
      'question_id' => $quiz_question
        ->id(),
      'question_vid' => $quiz_question
        ->getRevisionId(),
    ]);
    $qqr
      ->save();
    return $qqr;
  }
  else {
    return reset($relationships);
  }

  // @todo update the max score of the quiz.
  // quiz_update_max_score_properties(array($quiz->vid));
}