You are here

function QuizStorage::doPreSave in Quiz 6.x

Same name and namespace in other branches
  1. 8.6 src/Storage/QuizStorage.php \Drupal\quiz\Storage\QuizStorage::doPreSave()
  2. 8.5 src/Storage/QuizStorage.php \Drupal\quiz\Storage\QuizStorage::doPreSave()

Performs presave entity processing.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The saved entity.

Return value

int|string The processed entity identifier.

Throws

\Drupal\Core\Entity\EntityStorageException If the entity identifier is invalid.

Overrides ContentEntityStorageBase::doPreSave

File

src/Storage/QuizStorage.php, line 10

Class

QuizStorage

Namespace

Drupal\quiz\Storage

Code

function doPreSave(EntityInterface $entity) {
  $max_score = 0;
  if ($entity
    ->get('randomization')->value < 2) {
    $entity
      ->set('number_of_random_questions', 0);
  }
  if ($entity
    ->get('randomization')->value == 2) {
    $max_score = $entity
      ->get('number_of_random_questions')->value * $entity
      ->get('max_score_for_random')->value;
    $entity
      ->set('max_score', $max_score);
  }
  if ($entity
    ->get('randomization')->value == 3) {
    $num_questions = 0;
    foreach ($entity
      ->get('quiz_terms')
      ->referencedEntities() as $ref) {
      $max_score += $ref
        ->get('quiz_question_max_score')->value * $ref
        ->get('quiz_question_number')->value;
      $num_questions += $ref
        ->get('quiz_question_number')->value;
    }
    $entity
      ->set('number_of_random_questions', $num_questions);
    $entity
      ->set('max_score', $max_score);
  }
  return parent::doPreSave($entity);
}