ShortAnswerQuestion.php in Quiz 8.5
File
question_types/quiz_short_answer/src/Plugin/quiz/QuizQuestion/ShortAnswerQuestion.php
View source
<?php
namespace Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion;
use Drupal;
use Drupal\Core\Form\FormStateInterface;
use Drupal\quiz\Entity\QuizQuestion;
use Drupal\quiz\Entity\QuizResultAnswer;
class ShortAnswerQuestion extends QuizQuestion {
const ANSWER_MATCH = 0;
const ANSWER_INSENSITIVE_MATCH = 1;
const ANSWER_REGEX = 2;
const ANSWER_MANUAL = 3;
public function validateNode(array &$form) {
if ($this->node->correct_answer_evaluation != self::ANSWER_MANUAL && empty($this->node->correct_answer)) {
form_set_error('correct_answer', t('An answer must be specified for any evaluation type other than manual scoring.'));
}
}
public function getNodeView() {
$content = parent::getNodeView();
if ($this
->viewCanRevealCorrect()) {
$content['answers'] = array(
'#markup' => '<div class="quiz-solution">' . check_plain($this->node->correct_answer) . '</div>',
'#weight' => 2,
);
}
else {
$content['answers'] = array(
'#markup' => '<div class="quiz-answer-hidden">Answer hidden</div>',
'#weight' => 2,
);
}
return $content;
}
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
$element = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
$element += array(
'#type' => 'textfield',
'#title' => t('Answer'),
'#description' => t('Enter your answer here'),
'#default_value' => '',
'#size' => 60,
'#maxlength' => 256,
'#required' => FALSE,
'#attributes' => array(
'autocomplete' => 'off',
),
);
if ($quizQuestionResultAnswer
->isAnswered()) {
$element['#default_value'] = $quizQuestionResultAnswer
->getResponse();
}
return $element;
}
public static function getAnsweringFormValidate(array &$element, Drupal\Core\Form\FormStateInterface $form_state) {
if ($element['#value'] == '') {
$form_state
->setError($element, t('You must provide an answer.'));
}
}
public function getMaximumScore() {
return Drupal::config('quiz.settings.question.short_answer')
->get('default_max_score');
}
}
Classes
Name |
Description |
ShortAnswerQuestion |
@QuizQuestion (
id = "short_answer",
label =
Plugin annotation
@Translation("Short answer question"),
handlers = {
"response" = "\Drupal\quiz_short_answer\Plugin\quiz\QuizQuestion\ShortAnswerResponse"
}
) |