public function MultichoiceResponse::score in Quiz 8.6
Same name and namespace in other branches
- 8.5 question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse::score()
- 6.x question_types/quiz_multichoice/src/Plugin/quiz/QuizQuestion/MultichoiceResponse.php \Drupal\quiz_multichoice\Plugin\quiz\QuizQuestion\MultichoiceResponse::score()
Calculate the unscaled score in points for this question response.
Parameters
array $values: A part of form state values with the question input from the user.
Return value
int The unscaled point value of the answer.
Overrides QuizResultAnswerEntityTrait::score
File
- question_types/
quiz_multichoice/ src/ Plugin/ quiz/ QuizQuestion/ MultichoiceResponse.php, line 52
Class
- MultichoiceResponse
- Extension of QuizQuestionResponse.
Namespace
Drupal\quiz_multichoice\Plugin\quiz\QuizQuestionCode
public function score($response) {
if (!is_array($response['answer']['user_answer'])) {
$selected_vids = [
$response['answer']['user_answer'],
];
}
else {
$selected_vids = $response['answer']['user_answer'];
}
// Reset whatever was here already.
$this
->get('multichoice_answer')
->setValue(NULL);
// The answer ID is the revision ID of the Paragraph item of the MCQ.
// Fun!
foreach ($selected_vids as $vid) {
// Loop through all selected answers and append them to the paragraph
// revision reference.
$paragraph = \Drupal::entityTypeManager()
->getStorage('paragraph')
->loadRevision($vid);
$this
->get('multichoice_answer')
->appendItem($paragraph);
}
$simple = $this
->getQuizQuestion()
->get('choice_boolean')
->getString();
$multi = $this
->getQuizQuestion()
->get('choice_multi')
->getString();
$score = 0;
foreach ($this
->getQuizQuestion()
->get('alternatives')
->referencedEntities() as $alternative) {
// Take action on each alternative being selected (or not).
$vid = $alternative
->getRevisionId();
// If this alternative was selected.
$selected = in_array($vid, $selected_vids);
$correct = $alternative
->get('multichoice_correct')
->getString();
if (!$selected && $simple && $correct) {
// Selected this answer, simple scoring on, and the answer was incorrect.
$score = 0;
break;
}
if ($selected && $correct && !$multi) {
// User selected a correct answer and this is not a multiple answer
// question. User gets the point value of the question.
$score = $alternative
->get('multichoice_score_chosen')
->getString();
break;
}
if ($multi) {
// In multiple answer questions we sum up all the points.
if ($selected) {
// Add (or subtract) some points.
$score += $alternative
->get('multichoice_score_chosen')
->getString();
}
else {
$score += $alternative
->get('multichoice_score_not_chosen')
->getString();
}
}
}
return $score;
}