QuizResultAnswer.php in Quiz 6.x
File
src/Entity/QuizResultAnswer.php
View source
<?php
namespace Drupal\quiz\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\quiz\QuizAnswerInterface;
abstract class QuizResultAnswer extends ContentEntityBase implements QuizAnswerInterface {
use QuizResultAnswerEntityTrait;
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['result_id'] = BaseFieldDefinition::create('entity_reference')
->setSetting('target_type', 'quiz_result')
->setLabel('Quiz result ID');
$fields['type'] = BaseFieldDefinition::create('string')
->setLabel('Answer type');
$fields['question_id'] = BaseFieldDefinition::create('entity_reference')
->setSetting('target_type', 'quiz_question')
->setLabel('Question ID');
$fields['question_vid'] = BaseFieldDefinition::create('integer')
->setLabel('Question revision ID');
$fields['tid'] = BaseFieldDefinition::create('entity_reference')
->setLabel('Taxonomy term');
$fields['is_correct'] = BaseFieldDefinition::create('boolean')
->setLabel('Correct');
$fields['is_skipped'] = BaseFieldDefinition::create('boolean')
->setLabel('Skipped');
$fields['points_awarded'] = BaseFieldDefinition::create('integer')
->setLabel('Scaled points awarded');
$fields['answer_timestamp'] = BaseFieldDefinition::create('timestamp')
->setLabel('Date answered');
$fields['number'] = BaseFieldDefinition::create('integer')
->setLabel('Question number');
$fields['display_number'] = BaseFieldDefinition::create('integer')
->setLabel('Display number');
$fields['is_doubtful'] = BaseFieldDefinition::create('boolean')
->setLabel('Doubtful');
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel('Created');
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel('Changed');
$fields['is_evaluated'] = BaseFieldDefinition::create('boolean')
->setDefaultValue(0)
->setLabel('Evaluated');
$fields['answer_feedback'] = BaseFieldDefinition::create('text_long')
->setLabel(t('Answer feedback'))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setDisplayOptions('form', [
'type' => 'text_textarea',
]);
return $fields;
}
function getNext() {
$result = $this
->get('result_id')
->referencedEntities()[0];
foreach ($result
->getLayout() as $idx => $qra) {
if ($qra == $this) {
if (isset($result
->getLayout()[$idx + 1])) {
return $result
->getLayout()[$idx + 1];
}
}
}
}
function getPrevious() {
$result = $this
->get('result_id')
->referencedEntities()[0];
foreach ($result
->getLayout() as $idx => $qra) {
if ($qra == $this) {
if (isset($result
->getLayout()[$idx - 1])) {
return $result
->getLayout()[$idx - 1];
}
}
}
}
public function toUrl($rel = 'canonical', array $options = []) {
$url = parent::toUrl($rel, $options);
$url
->setRouteParameter('quiz', $this
->getQuizResult()
->getQuiz()
->id());
$url
->setRouteParameter('quiz_result', $this
->getQuizResult()
->id());
return $url;
}
}