public function QuizResult::save in Quiz 6.x
Same name and namespace in other branches
- 8.6 src/Entity/QuizResult.php \Drupal\quiz\Entity\QuizResult::save()
- 8.5 src/Entity/QuizResult.php \Drupal\quiz\Entity\QuizResult::save()
Save the Quiz result and do any post-processing to the result.
Parameters
type $this:
\DatabaseTransaction $transaction:
Return value
bool
Overrides EntityBase::save
1 call to QuizResult::save()
- QuizResult::finalize in src/
Entity/ QuizResult.php - Score a quiz result.
File
- src/
Entity/ QuizResult.php, line 202
Class
- QuizResult
- Defines the Quiz entity class.
Namespace
Drupal\quiz\EntityCode
public function save() {
if ($this
->get('time_start')
->isEmpty()) {
$this
->set('time_start', \Drupal::time()
->getRequestTime());
}
$new = $this
->isNew();
if ($new) {
// New attempt, we need to set the attempt number if there are previous
// attempts.
if ($this
->get('uid')
->getString() == 0) {
// If anonymous, the attempt is always 1.
$this->attempt = 1;
}
else {
// Get the highest attempt number.
$efq = \Drupal::entityQuery('quiz_result');
$result = $efq
->range(0, 1)
->condition('qid', $this
->get('qid')
->getString())
->condition('uid', $this
->get('uid')
->getString())
->sort('attempt', 'DESC')
->execute();
if (!empty($result)) {
$keys = array_keys($result);
$existing = QuizResult::load(reset($keys));
$this
->set('attempt', $existing
->get('attempt')
->getString() + 1);
}
}
}
// Save the Quiz result.
if (!$new) {
$original = \Drupal::entityTypeManager()
->getStorage('quiz_result')
->loadUnchanged($this
->id());
}
parent::save();
// Post process the result.
if ($new) {
$quiz = \Drupal::entityTypeManager()
->getStorage('quiz')
->loadRevision($this
->get('vid')
->getString());
// Create question list.
$questions = $quiz
->buildLayout();
if (empty($questions)) {
\Drupal::messenger()
->addError(t('Not enough questions were found. Please add more questions before trying to take this @quiz.', array(
'@quiz' => QuizUtil::getQuizName(),
)));
return FALSE;
}
if (in_array($this->build_on_last, [
'correct',
'all',
]) && ($quiz_result_old = self::findOldResult($this))) {
// Build on the last attempt the user took. If this quiz has build on
// last attempt set, we need to search for a previous attempt with the
// same version of the current quiz.
// Now clone the answers on top of the new result.
$quiz_result_old
->copyToQuizResult($this);
}
else {
$i = 0;
$j = 0;
foreach ($questions as $question) {
$quizQuestion = \Drupal::entityTypeManager()
->getStorage('quiz_question')
->loadRevision($question['vid']);
$quiz_result_answer = QuizResultAnswer::create([
'result_id' => $this
->id(),
'question_id' => $question['qqid'],
'question_vid' => $question['vid'],
'type' => $quizQuestion
->bundle(),
'tid' => !empty($question['tid']) ? $question['tid'] : NULL,
'number' => ++$i,
'display_number' => $quizQuestion
->isQuestion() ? ++$j : NULL,
]);
$quiz_result_answer
->save();
}
}
}
if (isset($original) && !$original
->get('is_evaluated')->value && $this
->get('is_evaluated')->value) {
// Quiz is finished! Delete old results if necessary.
$this
->maintainResults();
}
}