You are here

function opigno_learning_path_get_activities in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x opigno_learning_path.module \opigno_learning_path_get_activities()

Get activities in a group for a user.

Parameters

int $group_id: Group ID.

int $uid: User ID.

int $latest_cert_date: Latest certification date.

Return value

array Info about each activity status for a user.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

2 calls to opigno_learning_path_get_activities()
opigno_learning_path_get_step_progress in ./opigno_learning_path.module
Returns step progress.
Progress::getProgress in src/Progress.php
Calculates progress in a group for a user.

File

./opigno_learning_path.module, line 3037
Contains opigno_learning_path.module.

Code

function opigno_learning_path_get_activities($group_id, $uid, $latest_cert_date = NULL) {
  $activities = [];
  $steps = opigno_learning_path_get_steps($group_id, $uid, NULL, $latest_cert_date);
  foreach ($steps as $step) {
    if ($step['typology'] === 'Module') {
      $module_activities = opigno_learning_path_get_module_activities($step['id'], $uid, FALSE, $latest_cert_date);

      // Check if the module in skills system.
      $module = \Drupal::entityTypeManager()
        ->getStorage('opigno_module')
        ->load($step['id']);
      $moduleHandler = \Drupal::service('module_handler');
      if ($moduleHandler
        ->moduleExists('opigno_skills_system') && $module
        ->getSkillsActive() && $step['completed on'] != 0) {
        foreach ($module_activities as $key => $m_activity) {
          $module_activities[$key]['answers'] = 1;
        }
      }
      $activities = array_merge($activities, $module_activities);

      // Limit random activities.
      if ($module
        ->getRandomization() == 2) {
        $unanswered_activities = array_filter($activities, function ($activity) {
          return !$activity['answers'];
        });
        $answered_activities = array_filter($activities, function ($activity) {
          return $activity['answers'];
        });
        $activities = array_slice($answered_activities, 0, $module
          ->getRandomActivitiesCount());
        if (count($activities) < $module
          ->getRandomActivitiesCount()) {
          $diff_num = $module
            ->getRandomActivitiesCount() - count($activities);
          $difference = array_slice($unanswered_activities, 0, $diff_num);
          array_merge($activities, $difference);
        }
      }
    }
    elseif ($step['typology'] === 'Course') {
      $course_activities = opigno_learning_path_get_activities($step['id'], $uid, $latest_cert_date);
      $activities = array_merge($activities, $course_activities);
    }
    elseif (($step['typology'] === 'ILT' || $step['typology'] === 'Meeting') && $step['mandatory'] == 1) {
      if ($step['time spent'] > 0 && $step['presence'] > 0) {
        $answer = 1;
      }
      else {
        $answer = 0;
      }
      $activities[] = [
        'answers' => $answer,
      ];
    }
  }
  return $activities;
}