You are here

public function OpignoActivity::getUserAnswer in Opigno module 3.x

Same name and namespace in other branches
  1. 8 src/Entity/OpignoActivity.php \Drupal\opigno_module\Entity\OpignoActivity::getUserAnswer()

Returns user answer.

Parameters

\Drupal\opigno_module\Entity\OpignoModuleInterface $opigno_module: Opigno module object.

\Drupal\opigno_module\Entity\UserModuleStatusInterface $attempt: Attempt object.

\Drupal\Core\Session\AccountInterface $account: User object.

null|int $latest_cert_date: Latest certification date.

Return value

mixed Opigno answer object.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Entity/OpignoActivity.php, line 247

Class

OpignoActivity
Defines the Activity entity.

Namespace

Drupal\opigno_module\Entity

Code

public function getUserAnswer(OpignoModuleInterface $opigno_module, UserModuleStatusInterface $attempt, AccountInterface $account, $latest_cert_date = NULL) {
  $cid = $opigno_module
    ->id() . '-' . $attempt
    ->id() . '-' . $account
    ->id();
  $answer_storage = static::entityTypeManager()
    ->getStorage('opigno_answer');
  if (isset($this->userAnswers[$cid])) {
    return $this->userAnswers[$cid] ? $answer_storage
      ->load($this->userAnswers[$cid]) : NULL;
  }
  $query = $answer_storage
    ->getQuery();
  $query
    ->condition('user_id', $account
    ->id())
    ->condition('user_module_status', $attempt
    ->id())
    ->condition('module', $opigno_module
    ->id())
    ->condition('activity', $this
    ->id());
  if ($latest_cert_date) {
    $query
      ->condition('created', $latest_cert_date, '>');
  }
  $aid = $query
    ->range(0, 1)
    ->execute();
  $id = reset($aid);
  $this->userAnswers[$cid] = $id;
  return $this->userAnswers[$cid] ? $answer_storage
    ->load($this->userAnswers[$cid]) : NULL;
}