You are here

function opigno_learning_path_save_achievements in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 opigno_learning_path.module \opigno_learning_path_save_achievements()

Stores training achievements data.

Parameters

int $gid: Training group ID.

int $uid: User ID.

bool $with_meeting: If training contains ILT or Live Meeting.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

4 calls to opigno_learning_path_save_achievements()
OpignoLinkConditionTest::finishCurrentModuleAttempt in tests/src/Functional/OpignoLinkConditionTest.php
Force finish module current attempt.
OpignoModuleScoreTest::createAndFinishAttempt in tests/src/Functional/OpignoModuleScoreTest.php
Creates and finishes attempt.
opigno_learning_path_entity_insert in ./opigno_learning_path.module
Implements hook_entity_insert().
TrainingCompleteTest::createAnswersAndAttempt in tests/src/Functional/TrainingCompleteTest.php
Creates and answers and attempt and finish these.

File

./opigno_learning_path.module, line 3550
Contains opigno_learning_path.module.

Code

function opigno_learning_path_save_achievements($gid, $uid, $with_meeting = FALSE) {
  if (empty($gid)) {
    return FALSE;
  }
  $table_name = 'opigno_learning_path_achievements';
  $user = User::load($uid);
  $group = Group::load($gid);

  /** @var \Drupal\group\GroupMembership $group_membership */
  $group_membership = $group
    ->getMember($user);

  // Format timestamps to the storage format.
  $created_timestamp = $group_membership !== FALSE ? $group_membership
    ->getGroupContent()
    ->get('created')
    ->getValue()[0]['value'] : 0;
  $created = DrupalDateTime::createFromTimestamp($created_timestamp)
    ->format(DrupalDateTime::FORMAT);
  $completed_timestamp = opigno_learning_path_completed_on($gid, $uid);
  $completed = $completed_timestamp > 0 ? DrupalDateTime::createFromTimestamp($completed_timestamp)
    ->format(DrupalDateTime::FORMAT) : NULL;
  $latest_cert_date = LPStatus::getTrainingStartDate($group, $uid);
  $status = isset($completed) ? 'completed' : 'pending';
  $score = opigno_learning_path_get_score($gid, $uid, FALSE, $latest_cert_date);
  $progress_service = \Drupal::service('opigno_learning_path.progress');
  $progress = $progress_service
    ->getProgressRound($gid, $uid, $latest_cert_date);
  $time = opigno_learning_path_get_time_spent($gid, $uid);
  $keys = [
    'uid' => $uid,
    'gid' => $gid,
  ];
  $fields = [
    'uid' => $uid,
    'gid' => $gid,
    'name' => $group
      ->label(),
    'status' => $status,
    'score' => $score,
    'progress' => $progress,
    'time' => $time,
    'registered' => $created,
    'completed' => $completed,
  ];
  \Drupal::database()
    ->merge($table_name)
    ->keys($keys)
    ->fields($fields)
    ->execute();
}