You are here

protected function StatisticsPageTrait::getExpiredUsers in Opigno statistics 8

Same name and namespace in other branches
  1. 3.x src/StatisticsPageTrait.php \Drupal\opigno_statistics\StatisticsPageTrait::getExpiredUsers()

Returns users with the training expired certification.

They shouldn't have an attempts after expiration date.

Parameters

\Drupal\group\Entity\Group $group: Group object.

Return value

array Users IDs.

1 call to StatisticsPageTrait::getExpiredUsers()
TrainingForm::buildForm in src/Form/TrainingForm.php
Form constructor.

File

src/StatisticsPageTrait.php, line 189

Class

StatisticsPageTrait
Common helper methods for a statistics pages.

Namespace

Drupal\opigno_statistics

Code

protected function getExpiredUsers(Group $group) {
  $output = [];
  $gid = $group
    ->id();
  try {

    // Get users with the training expired certification.
    $output = $this->database
      ->select('user_lp_status_expire', 'lpe')
      ->fields('lpe', [
      'uid',
    ])
      ->condition('gid', $gid)
      ->condition('expire', time(), '<')
      ->execute()
      ->fetchCol();
  } catch (\Exception $e) {
    \Drupal::logger('opigno_statistics')
      ->error($e
      ->getMessage());
    \Drupal::messenger()
      ->addMessage($e
      ->getMessage(), 'error');
  }
  if ($output) {

    // Filter users with no attempts after expiration date.
    $output = array_filter($output, function ($uid) use ($group) {
      $gid = $group
        ->id();
      $expiration_set = LPStatus::isCertificateExpireSet($group);
      if ($expiration_set) {
        $expire_timestamp = LPStatus::getCertificateExpireTimestamp($gid, $uid);
        if ($expire_timestamp) {
          $result = $this->database
            ->select('user_module_status', 'ums')
            ->fields('ums', [
            'id',
          ])
            ->condition('learning_path', $gid)
            ->condition('user_id', $uid)
            ->condition('finished', $expire_timestamp, '>')
            ->execute()
            ->fetchField();
          if ($result) {
            return FALSE;
          }
        }
      }
      return TRUE;
    });
  }
  return $output;
}