Commit 217cdcea authored by casey's avatar casey Committed by djdevin
Browse files

Issue #3220069 by ankithashetty, casey: Use interfaces to define behavior

parent a09271dc
......@@ -48,28 +48,28 @@ class QuizDirectionsQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getMaximumScore() {
public function getMaximumScore(): int {
return 0;
}
/**
* {@inheritdoc}
*/
public function isGraded() {
public function isGraded(): bool {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function hasFeedback() {
public function hasFeedback(): bool {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function isQuestion() {
public function isQuestion(): bool {
return FALSE;
}
......
......@@ -10,20 +10,16 @@ use Drupal\quiz\Entity\QuizResultAnswer;
class QuizDirectionsResponse extends QuizResultAnswer {
/**
* Implementation of score().
*
* @see QuizQuestionResponse::score()
* {@inheritdoc}
*/
public function score(array $values) {
// Set the score.
$this->score = 0;
public function score(array $values): ?int {
return 0;
}
/**
* {@inheritdoc}
*/
public function isCorrect() {
public function isCorrect(): bool {
return TRUE;
}
......
......@@ -47,7 +47,7 @@ class LongAnswerQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer): array {
$element = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
$element += [
......@@ -124,7 +124,7 @@ class LongAnswerQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getMaximumScore() {
public function getMaximumScore(): int {
return Drupal::config('quiz_long_answer.settings')
->get('default_max_score');
}
......
......@@ -11,15 +11,17 @@ use function check_markup;
*/
class LongAnswerResponse extends QuizResultAnswer {
public function score(array $values) {
/**
* {@inheritdoc}
*/
public function score(array $values): ?int {
$this->set('long_answer', $values['answer']);
$this->setEvaluated(FALSE);
return NULL;
}
/**
* Implementation of getResponse().
*
* @see QuizQuestionResponse::getResponse()
* {@inheritdoc}
*/
public function getResponse() {
return $this->get('long_answer')->getString();
......@@ -28,10 +30,10 @@ class LongAnswerResponse extends QuizResultAnswer {
/**
* {@inheritdoc}
*/
public function getFeedbackValues() {
public function getFeedbackValues(): array {
$data = [];
$score = $this->getPoints();
$max = $this->getMaxScore(FALSE);
$max = $this->getMaxScore();
if ($this->evaluated) {
// Question has been graded.
......
......@@ -34,11 +34,9 @@ use function db_update;
class MatchingQuestion extends QuizQuestion {
/**
* Implementation of getAnsweringForm().
*
* @see QuizQuestion::getAnsweringForm()
* {@inheritdoc}
*/
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer): array {
$form = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
$answers[''] = '';
......@@ -109,7 +107,7 @@ class MatchingQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getMaximumScore() {
public function getMaximumScore(): int {
return count($this->get('quiz_matching')->referencedEntities());
}
......
......@@ -21,7 +21,7 @@ class MatchingResponse extends QuizResultAnswer {
*
* @see QuizQuestionResponse::score()
*/
public function score(array $values) {
public function score(array $values): ?int {
// Reset whatever was here already.
$this->get('matching_user_answer')->setValue(NULL);
......@@ -79,7 +79,7 @@ class MatchingResponse extends QuizResultAnswer {
*
* @see QuizQuestionResponse::getFeedbackValues()
*/
public function getFeedbackValues() {
public function getFeedbackValues(): array {
$data = [];
$answers = $this->getQuizQuestion()->getCorrectAnswer();
......@@ -129,7 +129,7 @@ class MatchingResponse extends QuizResultAnswer {
*
* @see views_handler_field_prerender_list for the expected return value.
*/
public static function viewsGetAnswers(array $result_answer_ids = []) {
public static function viewsGetAnswers(array $result_answer_ids = []): array {
$items = [];
foreach (QuizResultAnswer::loadMultiple($result_answer_ids) as $qra) {
......
......@@ -143,7 +143,7 @@ class MultichoiceQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer): array {
$element = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
foreach ($this->get('alternatives')->referencedEntities() as $alternative) {
......@@ -217,7 +217,7 @@ class MultichoiceQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getMaximumScore() {
public function getMaximumScore(): int {
if ($this->get('choice_boolean')->getString()) {
// Simple scoring - can only be worth 1 point.
return 1;
......
......@@ -27,7 +27,7 @@ class MultichoiceResponse extends QuizResultAnswer {
/**
* {@inheritdoc}
*/
public function score(array $response) {
public function score(array $response): ?int {
if (!is_array($response['answer']['user_answer'])) {
$selected_vids = [$response['answer']['user_answer']];
}
......@@ -91,9 +91,7 @@ class MultichoiceResponse extends QuizResultAnswer {
}
/**
* Implementation of getResponse().
*
* @see QuizQuestionResponse::getResponse()
* {@inheritdoc}
*/
public function getResponse() {
$vids = [];
......@@ -106,7 +104,7 @@ class MultichoiceResponse extends QuizResultAnswer {
/**
* {@inheritdoc}
*/
public function getFeedbackValues() {
public function getFeedbackValues(): array {
// @todo d8
//$this->orderAlternatives($this->question->alternatives);
$simple_scoring = $this->getQuizQuestion()
......@@ -180,7 +178,7 @@ class MultichoiceResponse extends QuizResultAnswer {
*
* @see views_handler_field_prerender_list for the expected return value.
*/
public static function viewsGetAnswers(array $result_answer_ids = []) {
public static function viewsGetAnswers(array $result_answer_ids = []): array {
$items = [];
foreach (QuizResultAnswer::loadMultiple($result_answer_ids) as $qra) {
......
......@@ -29,11 +29,9 @@ use Drupal\quiz\Entity\QuizResultAnswer;
class QuizPageQuestion extends QuizQuestion {
/**
* Implementation of getAnsweringForm().
*
* @see QuizQuestion::getAnsweringForm()
* {@inheritdoc}
*/
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer): array {
$element = [
'#type' => 'hidden',
];
......@@ -50,38 +48,30 @@ class QuizPageQuestion extends QuizQuestion {
}
/**
* Implementation of getMaximumScore().
*
* @see QuizQuestion::getMaximumScore()
* {@inheritdoc}
*/
public function getMaximumScore() {
public function getMaximumScore(): int {
return 0;
}
/**
* Implementation of isGraded().
*
* @see QuizQuestion::isGraded()
* {@inheritdoc}
*/
public function isGraded() {
public function isGraded(): bool {
return FALSE;
}
/**
* Implementation of hasFeedback().
*
* @see QuizQuestion::hasFeedback()
* {@inheritdoc}
*/
public function hasFeedback() {
public function hasFeedback(): bool {
return FALSE;
}
/**
* Implementation of isQuestion().
*
* @see QuizQuestion::hasFeedback()
* {@inheritdoc}
*/
public function isQuestion() {
public function isQuestion(): bool {
return FALSE;
}
......
......@@ -9,34 +9,31 @@ use Drupal\quiz\Entity\QuizResultAnswer;
*/
class QuizPageResponse extends QuizResultAnswer {
public function score(array $values) {
/**
* {@inheritdoc}
*/
public function score(array $values): ?int {
return NULL;
}
/**
* Implementation of isCorrect().
*
* @see QuizQuestionResponse::isCorrect()
* {@inheritdoc}
*/
public function isCorrect() {
public function isCorrect(): bool {
return TRUE;
}
/**
* Implementation of getResponse().
*
* @see QuizQuestionResponse::getResponse()
* {@inheritdoc}
*/
public function getResponse() {
return $this->answer;
return NULL;
}
/**
* Implementation of getReportForm().
*
* @see QuizQuestionResponse::getReportForm()
* {@inheritdoc}
*/
public function getReportForm() {
public function getReportForm(): array {
return [
'#no_report' => TRUE,
];
......
......@@ -63,7 +63,7 @@ class ShortAnswerQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer): array {
$element = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
$element += [
......@@ -96,7 +96,7 @@ class ShortAnswerQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getMaximumScore() {
public function getMaximumScore(): int {
return Drupal::config('quiz_short_answer.settings')
->get('default_max_score');
}
......
......@@ -13,7 +13,7 @@ class ShortAnswerResponse extends QuizResultAnswer {
/**
* {@inheritdoc}
*/
public function score(array $values) {
public function score(array $values): ?int {
$question = $this->getQuizQuestion();
$correct = $question->get('short_answer_correct')->getString();
......@@ -45,12 +45,12 @@ class ShortAnswerResponse extends QuizResultAnswer {
}
break;
}
return NULL;
}
/**
* Implementation of getResponse().
*
* @see QuizQuestionResponse::getResponse()
* {@inheritdoc}
*/
public function getResponse() {
return $this->get('short_answer')->getString();
......@@ -59,10 +59,10 @@ class ShortAnswerResponse extends QuizResultAnswer {
/**
* {@inheritdoc}
*/
public function getFeedbackValues() {
public function getFeedbackValues(): array {
$data = [];
$score = $this->getPoints();
$max = $this->getMaxScore(FALSE);
$max = $this->getMaxScore();
if ($this->isEvaluated()) {
// Question has been graded.
......
......@@ -39,7 +39,7 @@ class TrueFalseQuestion extends QuizQuestion {
/**
* {@inheritdoc}
*/
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer): array {
$element = parent::getAnsweringForm($form_state, $quizQuestionResultAnswer);
$element += [
'#type' => 'radios',
......@@ -94,7 +94,7 @@ class TrueFalseQuestion extends QuizQuestion {
*
* The maximum points for a true/false question is always 1.
*/
public function getMaximumScore() {
public function getMaximumScore(): int {
return 1;
}
......@@ -106,7 +106,7 @@ class TrueFalseQuestion extends QuizQuestion {
* @return bool
* Boolean indicating if the correct answer is TRUE or FALSE
*/
public function getCorrectAnswer() {
public function getCorrectAnswer(): bool {
return $this->get('truefalse_correct')->getString();
}
......
......@@ -14,12 +14,11 @@ class TrueFalseResponse extends QuizResultAnswer {
/**
* {@inheritdoc}
*/
public function score(array $response) {
public function score(array $response): ?int {
$tfQuestion = $this->getQuizQuestion();
$this->set('truefalse_answer', $response['answer']);
$this->setEvaluated();
if ($response['answer'] == $tfQuestion->getCorrectAnswer()) {
return $tfQuestion->getMaximumScore();
}
......@@ -29,20 +28,16 @@ class TrueFalseResponse extends QuizResultAnswer {
}
/**
* Implementation of getResponse().
*
* @see QuizQuestionResponse::getResponse()
* {@inheritdoc}
*/
public function getResponse() {
return $this->get('truefalse_answer')->getString();
}
/**
* Implementation of getFeedbackValues().
*
* @see QuizQuestionResponse::getFeedbackValues()
* {@inheritdoc}
*/
public function getFeedbackValues() {
public function getFeedbackValues(): array {
$answer = $this->getResponse();
if (is_numeric($answer)) {
......@@ -81,7 +76,7 @@ class TrueFalseResponse extends QuizResultAnswer {
*
* @see views_handler_field_prerender_list for the expected return value.
*/
public static function viewsGetAnswers(array $result_answer_ids = []) {
public static function viewsGetAnswers(array $result_answer_ids = []): array {
$items = [];
foreach (QuizResultAnswer::loadMultiple($result_answer_ids) as $qra) {
$items[$qra->get('result_id')->getString()][] = [
......
......@@ -5,6 +5,7 @@ namespace Drupal\quiz\Entity;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\quiz\QuizQuestionInterface;
/**
* Defines the Quiz question entity class.
......@@ -66,7 +67,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
* }
* )
*/
abstract class QuizQuestion extends EditorialContentEntityBase {
abstract class QuizQuestion extends EditorialContentEntityBase implements QuizQuestionInterface {
/**
* Define question statuses...
......
......@@ -4,7 +4,7 @@ namespace Drupal\quiz\Entity;
class QuizQuestionBroken extends QuizQuestion {
public function getMaximumScore() {
public function getMaximumScore(): int {
return 0;
}
......
......@@ -36,32 +36,14 @@ trait QuizQuestionEntityTrait {
}
/**
* Get the form through which the user will answer the question.
*
* Question types should populate the form with selected values from the
* current result if possible.
*
* @param FormStateInterface $form_state
* Form state.
* @param QuizResultAnswer $quizQuestionResultAnswer
* The quiz result answer.
*
* @return array
* Form array.
* {@inheritdoc}
*/
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer) {
public function getAnsweringForm(FormStateInterface $form_state, QuizResultAnswer $quizQuestionResultAnswer): array {
$form = [];
$form['#element_validate'] = [[static::class, 'getAnsweringFormValidate']];
return $form;
}
/**
* Get the maximum possible score for this question.
*
* @return int
*/
abstract public function getMaximumScore();
/**
* Finds out if a question has been answered or not.
*
......@@ -159,51 +141,30 @@ trait QuizQuestionEntityTrait {
}
/**
* Is this question graded?
*
* Questions like Quiz Directions, Quiz Page, and Scale are not.
*
* By default, questions are expected to be gradeable
*
* @return bool
* {@inheritdoc}
*/
public function isGraded() {
public function isGraded(): bool {
return TRUE;
}
/**
* Does this question type give feedback?
*
* Questions like Quiz Directions and Quiz Pages do not.
*
* By default, questions give feedback
*
* @return bool
* {@inheritdoc}
*/
public function hasFeedback() {
public function hasFeedback(): bool {
return TRUE;
}
/**
* Is this "question" an actual question?
*
* For example, a Quiz Page is not a question, neither is a "quiz directions".
*
* Returning FALSE here means that the question will not be numbered, and
* possibly other things.
*
* @return bool
* {@inheritdoc}
*/
public function isQuestion() {
public function isQuestion(): bool {
return TRUE;
}
/**
* Get the response to this question in a quiz result.
*
* @return QuizResultAnswer
* {@inheritdoc}
*/
public function getResponse(QuizResult $quiz_result) {
public function getResponse(QuizResult $quiz_result): ?QuizResultAnswer {
$entities = \Drupal::entityTypeManager()
->getStorage('quiz_result_answer')
->loadByProperties([
......
......@@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\quiz\QuizAnswerInterface;
/**
* Defines the Quiz entity class.
......@@ -51,7 +52,7 @@ use Drupal\Core\Field\BaseFieldDefinition;
* }
* )
*/
abstract class QuizResultAnswer extends ContentEntityBase {
abstract class QuizResultAnswer extends ContentEntityBase implements QuizAnswerInterface {
use QuizResultAnswerEntityTrait;
......
......@@ -5,11 +5,11 @@ namespace Drupal\quiz\Entity;
class QuizResultAnswerBroken extends QuizResultAnswer {
public function getResponse() {