You are here

private function TrainingCompleteTest::createAnswersAndAttempt in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x tests/src/Functional/TrainingCompleteTest.php \Drupal\Tests\opigno_learning_path\Functional\TrainingCompleteTest::createAnswersAndAttempt()

Creates and answers and attempt and finish these.

1 call to TrainingCompleteTest::createAnswersAndAttempt()
TrainingCompleteTest::testTrainingComplete in tests/src/Functional/TrainingCompleteTest.php
Tests training visibility for users.

File

tests/src/Functional/TrainingCompleteTest.php, line 92

Class

TrainingCompleteTest
Tests for training complete process.

Namespace

Drupal\Tests\opigno_learning_path\Functional

Code

private function createAnswersAndAttempt($training, $module, $user, $score) {
  $attempt = UserModuleStatus::create([]);
  $attempt
    ->setModule($module);
  $attempt
    ->setScore($score);
  $attempt
    ->setOwnerId($user
    ->id());
  $attempt
    ->setEvaluated(1);
  $attempt
    ->setFinished(time());
  $attempt
    ->save();
  $activities_ids = array_keys($module
    ->getModuleActivities());
  if (count($activities_ids) > 0) {
    $activities_storage = \Drupal::entityTypeManager()
      ->getStorage('opigno_activity');
    $activities = $activities_storage
      ->loadMultiple($activities_ids);

    // Each Activity and attempt should have answer to complete step.
    foreach ($activities as $activity) {
      $this
        ->createAnswer($activity, $module, $attempt, $user
        ->id(), $score);
    }
  }

  // Reset all static variables.
  drupal_static_reset();

  // Save all achievements.
  $step = opigno_learning_path_get_module_step($training
    ->id(), $user
    ->id(), $module);
  $step['completed on'] = time();
  $step['passed'] = TRUE;
  opigno_learning_path_save_step_achievements($training
    ->id(), $user
    ->id(), $step);
  opigno_learning_path_save_achievements($training
    ->id(), $user
    ->id());
  return $attempt;
}