MatchingResponse.php in Quiz 8.5
File
question_types/quiz_matching/src/Plugin/quiz/QuizQuestion/MatchingResponse.php
View source
<?php
namespace Drupal\quiz_matching\Plugin\quiz\QuizQuestion;
use stdClass;
use function _quiz_question_response_get_instance;
use function db_delete;
use function db_insert;
use function db_query;
use function node_load;
class MatchingResponse extends \Drupal\quiz\Entity\QuizResultAnswer {
public function score(array $values) {
$this
->get('matching_user_answer')
->setValue(NULL);
foreach ($this
->getQuizQuestion()
->get('quiz_matching')
->referencedEntities() as $paragraph) {
$left_paragraph = $paragraph;
$right_paragraph = \Drupal::entityTypeManager()
->getStorage('paragraph')
->loadRevision($values['answer']['user_answer'][$paragraph
->getRevisionId()]);
$new_paragraph = \Drupal\paragraphs\Entity\Paragraph::create([
'type' => 'quiz_matching_answer',
'matching_user_question' => $left_paragraph,
'matching_user_answer' => $right_paragraph,
]);
$this
->get('matching_user_answer')
->appendItem($new_paragraph);
}
$answers = $this
->getQuizQuestion()
->getCorrectAnswer();
foreach ($this
->get('matching_user_answer')
->referencedEntities() as $pair) {
if ($pair
->get('matching_user_answer')
->getValue()) {
$response[$pair
->get('matching_user_question')
->getValue()[0]['target_revision_id']] = $pair
->get('matching_user_answer')
->getValue()[0]['target_revision_id'];
}
else {
$response[$pair
->get('matching_user_question')
->getValue()[0]['target_revision_id']] = -1;
}
}
$score = 0;
foreach ($answers as $vid => $answer) {
if ($response[$vid] == $vid) {
$score++;
}
elseif ($response[$vid] != -1 && $this
->getQuizQuestion()
->get('choice_penalty')
->getString()) {
$score -= 1;
}
}
return $score > 0 ? $score : 0;
}
public function getResponse() {
return $this
->get('matching_user_answer')
->referencedEntities();
}
public function getFeedbackValues() {
$data = array();
$answers = $this
->getQuizQuestion()
->getCorrectAnswer();
foreach ($this
->get('matching_user_answer')
->referencedEntities() as $pair) {
if ($pair
->get('matching_user_answer')
->getValue()) {
$response[$pair
->get('matching_user_question')
->getValue()[0]['target_revision_id']] = $pair
->get('matching_user_answer')
->getValue()[0]['target_revision_id'];
}
}
foreach ($this
->getQuizQuestion()
->get('quiz_matching')
->referencedEntities() as $paragraph) {
$vid = $paragraph
->getRevisionId();
$data[] = array(
'choice' => $paragraph
->get('matching_question')
->getString(),
'attempt' => isset($response[$paragraph
->getRevisionId()]) && $answers[$response[$paragraph
->getRevisionId()]] ? $answers[$response[$paragraph
->getRevisionId()]]
->get('matching_answer')
->getString() : '',
'correct' => ($response[$vid] ?? -1) == $vid ? \Drupal\quiz\Util\QuizUtil::icon('correct') : \Drupal\quiz\Util\QuizUtil::icon('incorrect'),
'score' => ($response[$vid] ?? -1) == $vid ? 1 : !empty($response[$vid]) && $this
->getQuizQuestion()
->get('choice_penalty')
->getString() ? -1 : 0,
'answer_feedback' => 'placeholder',
'solution' => $paragraph
->get('matching_answer')
->getString(),
);
}
return $data;
}
public static function viewsGetAnswers(array $result_answer_ids = array()) {
$items = array();
foreach ($result_answer_ids as $result_answer_id) {
$ra = entity_load_single('quiz_result_answer', $result_answer_id);
$question = node_load($ra->question_nid, $ra->question_vid);
$ra_i = _quiz_question_response_get_instance($ra->result_id, $question);
$matches = array();
foreach ($question->match as $match) {
$matches[$match['match_id']] = $match;
}
foreach ($ra_i
->getResponse() as $question_id => $answer_id) {
if (!empty($answer_id)) {
$items[$ra->result_id][] = array(
'answer' => $matches[$question_id]['question'] . ': ' . $matches[$answer_id]['answer'],
);
}
}
}
return $items;
}
}