View source
<?php
namespace Drupal\opigno_module\Entity;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\RevisionableContentEntityBase;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
class OpignoAnswer extends RevisionableContentEntityBase implements OpignoAnswerInterface {
use EntityChangedTrait;
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
$values += [
'user_id' => \Drupal::currentUser()
->id(),
];
}
public function getType() {
return $this
->bundle();
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getOwner() {
return $this
->get('user_id')->entity;
}
public function getOwnerId() {
return $this
->get('user_id')->target_id;
}
public function setOwnerId($uid) {
$this
->set('user_id', $uid);
return $this;
}
public function setOwner(UserInterface $account) {
$this
->set('user_id', $account
->id());
return $this;
}
public function getActivity() {
return $this
->get('activity')->entity;
}
public function getModule() {
return $this
->get('module')->entity;
}
public function setUserModuleStatus(UserModuleStatusInterface $user_attempt) {
$this
->set('user_module_status', $user_attempt);
return $this;
}
public function getUserModuleStatus() {
return $this
->get('user_module_status')->entity;
}
public function isEvaluated() {
return (bool) $this
->get('evaluated')->value;
}
public function setEvaluated($value) {
$this
->set('evaluated', $value);
return $this;
}
public function setScore($value) {
$this
->set('score', $value);
return $this;
}
public function getScore() {
return (int) $this
->get('score')->value;
}
public function setSkillId($value) {
$this
->set('skills_mode', $value);
return $this;
}
public function getSkillId() {
return (int) $this
->get('skills_mode')->value;
}
public function setSkillLevel($value) {
$this
->set('skill_level', $value);
return $this;
}
public function preSave(EntityStorageInterface $storage) {
if (!$this
->getRevisionUser()) {
$this
->setRevisionUserId($this
->getOwnerId());
}
parent::preSave($storage);
}
public function preSaveRevision(EntityStorageInterface $storage, \stdClass $record) {
parent::preSaveRevision($storage, $record);
$revision_log_field_name = static::getRevisionMetadataKey($this
->getEntityType(), 'revision_log_message');
$revision_created_field_name = static::getRevisionMetadataKey($this
->getEntityType(), 'revision_created');
$new_revision = $this
->isNewRevision();
if (!$new_revision && isset($this->original) && (!isset($record->{$revision_log_field_name}) || $record->{$revision_log_field_name} === '')) {
$record->{$revision_log_field_name} = $this->original
->getRevisionLogMessage();
}
if ($new_revision && (!isset($record->{$revision_created_field_name}) || empty($record->{$revision_created_field_name}))) {
$record->{$revision_created_field_name} = $record->id == $this
->id() ? $this
->getCreatedTime() : \Drupal::time()
->getRequestTime();
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the Answer entity.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default');
$fields['activity'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Activity'))
->setSetting('target_type', 'opigno_activity')
->setDisplayConfigurable('view', TRUE)
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
]);
$fields['module'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Opigno module'))
->setSetting('target_type', 'opigno_module')
->setDisplayConfigurable('view', TRUE)
->setReadOnly(TRUE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
]);
$fields['evaluated'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Evaluation status'))
->setDescription(t('A boolean indicating whether the answer is evaluated.'))
->setDefaultValue(FALSE);
$fields['user_module_status'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('User Module status'))
->setSetting('target_type', 'user_module_status');
$fields['score'] = BaseFieldDefinition::create('integer')
->setLabel(t('Score'))
->setDescription(t('The score the user obtained for this answer'));
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the entity was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['skills_mode'] = BaseFieldDefinition::create('integer')
->setLabel(t('Skills mode'))
->setDescription(t('Show if skill mode is activated.'));
$fields['skill_level'] = BaseFieldDefinition::create('integer')
->setLabel(t('Skill level'))
->setDescription(t('Show skill level.'));
return $fields;
}
}