You are here

public function OpignoModule::getModuleActiveAttempt in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/Entity/OpignoModule.php \Drupal\opigno_module\Entity\OpignoModule::getModuleActiveAttempt()

Get entity if user didn't finish module.

Parameters

\Drupal\Core\Session\AccountInterface $user: User entity object.

Return value

\Drupal\Core\Entity\EntityInterface|null Entity interface.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Entity/OpignoModule.php, line 490

Class

OpignoModule
Defines the Module entity.

Namespace

Drupal\opigno_module\Entity

Code

public function getModuleActiveAttempt(AccountInterface $user, $activity_link_type = NULL) {
  $key = $this
    ->id() . '_' . $user
    ->id();
  if ($activity_link_type == 'flow') {

    // Used Opigno flow type of activity answer link.
    if (array_key_exists($key, $this->userActiveAttempt)) {
      return $this->userActiveAttempt[$key];
    }
    OpignoGroupContext::removeActivityLinkType();
  }
  else {

    // Used activity answer manual/direct link.
    if (array_key_exists($key, $this->userActiveAttempt) && !empty($this->userActiveAttempt[$key])) {
      return $this->userActiveAttempt[$key];
    }
    elseif (!empty($this->userAttempts[$key . '_last'])) {
      $last_attempt = reset($this->userAttempts[$key . '_last']);
      $this->userActiveAttempt[$key] = $last_attempt;
      return $last_attempt;
    }
  }
  $status_storage = static::entityTypeManager()
    ->getStorage('user_module_status');
  $query = $status_storage
    ->getQuery();
  $module_statuses = $query
    ->condition('module', $this
    ->id())
    ->condition('user_id', $user
    ->id())
    ->condition('finished', 0)
    ->range(0, 1)
    ->execute();
  $this->userActiveAttempt[$key] = !empty($module_statuses) ? $status_storage
    ->load(key($module_statuses)) : NULL;
  return $this->userActiveAttempt[$key];
}