public function UserModuleStatus::finishAttempt in Opigno module 8
Same name and namespace in other branches
- 3.x src/Entity/UserModuleStatus.php \Drupal\opigno_module\Entity\UserModuleStatus::finishAttempt()
Finish user attempt.
File
- src/
Entity/ UserModuleStatus.php, line 361
Class
- UserModuleStatus
- Defines the User module status entity.
Namespace
Drupal\opigno_module\EntityCode
public function finishAttempt() {
/* @var \Drupal\opigno_module\Entity\OpignoModule $module */
$module = $this
->getModule();
$which_score_keep = $module
->getKeepResultsOption();
$this
->setFinished(time());
$this
->setCurrentActivity();
// Calculate both score and maximum score.
$score = $this
->calculateScore();
$max_score = $this
->calculateMaxScore();
if ($max_score > 0) {
$percents = round($score / $max_score * 100);
}
elseif ($which_score_keep == 'best') {
// Check if there are created attempts.
if ($module
->getModuleAttempts($this
->getOwner())) {
$best_attempt_score = $this
->calculateBestScore();
$percents = isset($percents) && $best_attempt_score < $percents ? $percents : $best_attempt_score;
}
}
else {
$percents = 100;
}
$this
->setScore((int) $percents);
$this
->setMaxScore($max_score);
// Check if attempt must be evaluated.
$answers = $this
->getAnswers();
$attempt_evaluated = 1;
if (!empty($answers)) {
foreach ($answers as $answer) {
// At least one answer is not evaluated.
if (!$answer
->isEvaluated()) {
$attempt_evaluated = 0;
break;
}
}
}
$this
->setEvaluated($attempt_evaluated);
$this
->save();
return $this;
}