You are here

public function LearningPathAchievementTrait::getActivities in Opigno Learning path 3.x

Gets the activities list by the group and module.

1 call to LearningPathAchievementTrait::getActivities()
StepsBlock::processActivityList in src/Plugin/Block/StepsBlock.php
Converts a step array to a renderable array.

File

src/Traits/LearningPathAchievementTrait.php, line 135

Class

LearningPathAchievementTrait
LearningPathAchievementTrait trait.

Namespace

Drupal\opigno_learning_path\Traits

Code

public function getActivities(GroupInterface $training, GroupInterface $course = NULL, OpignoModule $module) : array {
  $user = $this
    ->currentUser();

  // @codingStandardsIgnoreLine
  $moduleHandler = \Drupal::moduleHandler();

  // Get training latest certification timestamp.
  $latest_cert_date = LPStatus::getTrainingStartDate($training, $user
    ->id());
  $parent = isset($course) ? $course : $training;
  $step = opigno_learning_path_get_module_step($parent
    ->id(), $user
    ->id(), $module, $latest_cert_date);

  /** @var \Drupal\opigno_module\Entity\OpignoModule $module */
  $module = OpignoModule::load($step['id']);

  /** @var \Drupal\opigno_module\Entity\UserModuleStatus[] $attempts */
  $attempts = $module
    ->getModuleAttempts($user, NULL, $latest_cert_date);
  if ($moduleHandler
    ->moduleExists('opigno_skills_system') && $module
    ->getSkillsActive() && $module
    ->getModuleSkillsGlobal() && !empty($attempts)) {
    $activities_from_module = $module
      ->getModuleActivities();
    $activity_ids = array_keys($activities_from_module);
    $attempt = $this
      ->getTargetAttempt($attempts, $module);

    // @codingStandardsIgnoreLine
    $db_connection = \Drupal::service('database');
    $query = $db_connection
      ->select('opigno_answer_field_data', 'o_a_f_d');
    $query
      ->leftJoin('opigno_module_relationship', 'o_m_r', 'o_a_f_d.activity = o_m_r.child_id');
    $query
      ->addExpression('MAX(o_a_f_d.activity)', 'id');
    $query
      ->condition('o_a_f_d.user_id', $user
      ->id())
      ->condition('o_a_f_d.module', $module
      ->id());
    if (!$module
      ->getModuleSkillsGlobal()) {
      $query
        ->condition('o_a_f_d.activity', $activity_ids, 'IN');
    }
    $query
      ->condition('o_a_f_d.user_module_status', $attempt
      ->id())
      ->groupBy('o_a_f_d.activity');
    $activities = $query
      ->execute()
      ->fetchAllAssoc('id');
  }
  else {
    $activities = $module
      ->getModuleActivities();
  }

  /** @var \Drupal\opigno_module\Entity\OpignoActivity[] $activities */
  $activities = array_map(function ($activity) {

    /** @var \Drupal\opigno_module\Entity\OpignoActivity $activity */
    return OpignoActivity::load($activity->id);
  }, $activities);
  return [
    $activities,
    $attempts,
  ];
}