You are here

public function LpSteps::getIltStep in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/LpSteps.php \Drupal\opigno_learning_path\LpSteps::getIltStep()

Builds up a training ILT step.

Parameters

int $group_id: Training group ID.

int $uid: User ID.

\Drupal\opigno_ilt\ILTInterface $ilt: Opigno ILT entity.

Return value

array Data array about step in a group for a user.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/LpSteps.php, line 349

Class

LpSteps

Namespace

Drupal\opigno_learning_path

Code

public function getIltStep($group_id, $uid, ILTInterface $ilt) {
  $id = $ilt
    ->id();
  $key = "{$group_id}_{$uid}_{$id}";
  $results =& drupal_static(__FUNCTION__);
  if (!isset($results[$key])) {

    /** @var \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent $content */
    $content = current(OpignoGroupManagedContent::loadByProperties([
      'group_id' => $group_id,
      'group_content_type_id' => 'ContentTypeILT',
      'entity_id' => $ilt
        ->id(),
    ]));
    $entityTypeManager = \Drupal::entityTypeManager();

    /** @var \Drupal\opigno_ilt\ILTResultInterface[] $ilt_results */
    $ilt_results = $entityTypeManager
      ->getStorage('opigno_ilt_result')
      ->loadByProperties([
      'user_id' => $uid,
      'opigno_ilt' => $ilt
        ->id(),
    ]);
    $presence = 0;
    if (!empty($ilt_results)) {
      $presence = current($ilt_results)
        ->getStatus();
    }
    $scores = array_map(function ($result) {

      /** @var \Drupal\opigno_ilt\ILTResultInterface $result */
      return $result
        ->getScore();
    }, $ilt_results);
    if (!empty($scores)) {
      $score = end($scores);
    }
    else {
      $score = 0;
    }
    $start_date = $ilt
      ->getStartDate();
    $end_date = $ilt
      ->getEndDate();
    $start_timestamp = isset($start_date) ? DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $start_date)
      ->getTimestamp() : 0;
    $end_timestamp = isset($end_date) ? DrupalDateTime::createFromFormat(DrupalDateTime::FORMAT, $end_date)
      ->getTimestamp() : 0;
    $description = $ilt
      ->getPlace();
    $results[$key] = [
      // OpignoGroupManagedContent id.
      'cid' => $content
        ->id(),
      // Entity id.
      'id' => $ilt
        ->id(),
      'name' => $ilt
        ->label(),
      'description' => $description,
      'typology' => 'ILT',
      'best score' => !empty($scores) ? max($scores) : 0,
      'current attempt score' => $score,
      'required score' => (int) $content
        ->getSuccessScoreMin(),
      'attempts' => count($ilt_results),
      'activities' => 0,
      'time spent' => !empty($scores) ? $end_timestamp - $start_timestamp : 0,
      'current attempt time' => !empty($scores) ? $end_timestamp - $start_timestamp : 0,
      'completed on' => !empty($scores) ? $end_timestamp : 0,
      'mandatory' => (int) $content
        ->isMandatory(),
      'presence' => $presence,
    ];
  }
  return $results[$key];
}