You are here

function opigno_learning_path_save_step_achievements in Opigno Learning path 3.x

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

Stores step achievements data.

Parameters

int $gid: Training group ID.

int $uid: User ID.

array $step: Step info array returned by the opigno_learning_path_get_steps().

int $parent_id: ID of the parent row in the opigno_learning_path_step_achievements table.

Return value

int Row ID.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

3 calls to opigno_learning_path_save_step_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.
TrainingCompleteTest::createAnswersAndAttempt in tests/src/Functional/TrainingCompleteTest.php
Creates and answers and attempt and finish these.

File

./opigno_learning_path.module, line 3399
Contains opigno_learning_path.module.

Code

function opigno_learning_path_save_step_achievements($gid, $uid, array $step, $parent_id = 0) {
  if (empty($gid)) {
    return FALSE;
  }
  $table_name = 'opigno_learning_path_step_achievements';

  // Format timestamp to the storage format.
  $completed_timestamp = $step['completed on'];
  $completed = $completed_timestamp > 0 ? DrupalDateTime::createFromTimestamp($completed_timestamp)
    ->format(DrupalDateTime::FORMAT) : NULL;
  $group = Group::load($gid);
  $latest_cert_date = LPStatus::getTrainingStartDate($group, $uid);
  $status = opigno_learning_path_get_step_status($step, $uid, TRUE, $latest_cert_date);

  // Check the absence of the user at the live meeting or at the ILT.
  switch ($step['typology']) {
    case 'Meeting':
      $entityTypeManager = \Drupal::entityTypeManager();

      /** @var \Drupal\opigno_moxtra\MeetingResultInterface[] $meeting_results */
      $meeting_results = $entityTypeManager
        ->getStorage('opigno_moxtra_meeting_result')
        ->loadByProperties([
        'user_id' => $uid,
        'meeting' => $step['id'],
      ]);
      if (!empty($meeting_results)) {
        $presence = current($meeting_results)
          ->getStatus();
        if ($presence == 0) {
          $completed = NULL;
          $status = 'failed';
        }
      }
      break;
    case 'ILT':
      $entityTypeManager = \Drupal::entityTypeManager();

      /** @var \Drupal\opigno_ilt\ILTResultInterface[] $ilt_results */
      $ilt_results = $entityTypeManager
        ->getStorage('opigno_ilt_result')
        ->loadByProperties([
        'user_id' => $uid,
        'opigno_ilt' => $step['id'],
      ]);
      if (!empty($ilt_results)) {
        $presence = current($ilt_results)
          ->getStatus();
        if ($presence == 0) {
          $completed = NULL;
          $status = 'failed';
        }
      }
      break;
  }

  // Fix empty scores.
  if (empty($step['best score'])) {
    $step['best score'] = 0;
  }

  // Save step results in the opigno_learning_path_step_achievements.
  $keys = [
    'uid' => $uid,
    'gid' => $gid,
    'typology' => $step['typology'],
    'entity_id' => $step['id'],
    'parent_id' => $parent_id,
  ];
  $fields = [
    'uid' => $uid,
    'gid' => $gid,
    'entity_id' => $step['id'],
    'parent_id' => $parent_id,
    'name' => $step['name'],
    'typology' => $step['typology'],
    'mandatory' => $step['mandatory'],
    'status' => $status,
    'score' => $step['best score'],
    'time' => $step['time spent'],
    'completed' => $completed,
    'position' => isset($step['position']) ? $step['position'] : 0,
  ];
  $query = \Drupal::database()
    ->merge($table_name)
    ->keys($keys)
    ->fields($fields);
  if (isset($step['current attempt time']) && $step['typology'] != 'Meeting' && $step['typology'] != 'ILT') {
    $query = $query
      ->expression('time', 'time + :time', [
      ':time' => $step['current attempt time'],
    ]);
  }

  // Set score for Module.
  if ($step['typology'] == 'Module') {
    $module = OpignoModule::load($step['id']);
    $user = User::load($uid);
    $moduleHandler = \Drupal::service('module_handler');
    if (!$moduleHandler
      ->moduleExists('opigno_skills_system') || !$module
      ->getSkillsActive()) {
      $fields['score'] = $module
        ->getUserScore($user, $latest_cert_date);
    }
  }
  elseif (isset($step['current attempt score']) && $step['typology'] != 'Meeting' && $step['typology'] != 'ILT') {
    $query = $query
      ->expression('score', 'GREATEST(score, :score, :best_score)', [
      ':score' => $step['current attempt score'],
      ':best_score' => $fields['score'],
    ]);
  }
  $query
    ->execute();

  // Get saved ID.
  $query = \Drupal::database()
    ->select($table_name, 'a')
    ->fields('a', [
    'id',
  ]);
  foreach ($keys as $field => $value) {
    $query = $query
      ->condition($field, $value);
  }
  return $query
    ->execute()
    ->fetchField();
}