function Quiz::addQuestion in Quiz 6.x
Same name and namespace in other branches
- 8.6 src/Entity/Quiz.php \Drupal\quiz\Entity\Quiz::addQuestion()
- 8.5 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 422
Class
- Quiz
- Defines the Quiz entity class.
Namespace
Drupal\quiz\EntityCode
function addQuestion(QuizQuestion $quiz_question) {
$relationships = Drupal::entityTypeManager()
->getStorage('quiz_question_relationship')
->loadByProperties([
'quiz_vid' => $this
->getRevisionId(),
'question_id' => $quiz_question
->id(),
'question_vid' => $quiz_question
->getRevisionId(),
]);
$query = Drupal::database()
->select('quiz_question_relationship', 'qqr')
->condition('quiz_vid', $this
->getRevisionId());
$query
->addExpression('max(weight)', 'max_weight');
$new_weight = $query
->execute()
->fetchField();
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(),
'weight' => $new_weight + 1,
]);
$qqr
->save();
return $qqr;
}
else {
return reset($relationships);
}
// @todo update the max score of the quiz.
// quiz_update_max_score_properties(array($quiz->vid));
}