You are here

function opigno_learning_path_get_module_activities in Opigno Learning path 8

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

Get activities in a module for a user.

Parameters

int $module_id: Module ID.

int $uid: User ID.

bool $step_state_counting: TRUE if function called for calculating step state, FALSE otherwise.

int $latest_cert_date: Latest certification date.

Return value

array Info about each activity status for a user.

2 calls to opigno_learning_path_get_module_activities()
opigno_learning_path_get_activities in ./opigno_learning_path.module
Get activities in a group for a user.
opigno_learning_path_get_step_progress in ./opigno_learning_path.module
Returns step progress.

File

./opigno_learning_path.module, line 2978
Contains opigno_learning_path.module.

Code

function opigno_learning_path_get_module_activities($module_id, $uid, $step_state_counting = FALSE, $latest_cert_date = NULL) {
  $user = User::load($uid);

  /** @var \Drupal\opigno_module\Entity\OpignoModule $module */
  $module = OpignoModule::load($module_id);

  /** @var \Drupal\opigno_module\Entity\UserModuleStatus[] $attempts */
  $attempts = $module
    ->getModuleAttempts($user, NULL, $latest_cert_date);

  // Load activities for a module.
  $module_activities = $module
    ->getModuleActivities();
  $activities = array_map(function ($activity) use ($user, $module, $attempts, $step_state_counting) {

    // Value returned by the OpignoModule::getModuleActivities()
    // is not an OpignoActivity.

    /** @var \Drupal\opigno_module\Entity\OpignoActivity $activity */
    $activity = OpignoActivity::load($activity->id);

    /** @var \Drupal\opigno_module\Entity\OpignoAnswer[] $answers */
    $answers = array_map(function ($attempt) use ($user, $module, $activity, $step_state_counting) {

      /** @var \Drupal\opigno_module\Entity\OpignoActivity $activity */
      $answer = $activity
        ->getUserAnswer($module, $attempt, $user);
      if ($answer && $step_state_counting && $activity
        ->hasField('opigno_evaluation_method') && $activity
        ->get('opigno_evaluation_method')->value && !$answer
        ->isEvaluated()) {
        $answer = NULL;
      }
      return $answer;
    }, $attempts);

    // The OpignoActivity::getUserAnswer() may return NULL.
    $answers = array_filter($answers, function ($answer) {
      return isset($answer);
    });
    return [
      'activity_id' => $activity
        ->id(),
      'module_id' => $module
        ->id(),
      'answers' => count($answers),
    ];
  }, $module_activities);
  return $activities;
}