You are here

public function OpignoGroupManagedContent::getSuccessfulRequiredActvities in Opigno group manager 8

Same name and namespace in other branches
  1. 3.x src/Entity/OpignoGroupManagedContent.php \Drupal\opigno_group_manager\Entity\OpignoGroupManagedContent::getSuccessfulRequiredActvities()

Returns successful required activities array.

Parameters

mixed $required_activities: Mapped array of required activities.

int $last_attempt_id: Module last attempt id.

Return value

array Successful required activities array.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 call to OpignoGroupManagedContent::getSuccessfulRequiredActvities()
OpignoGroupManagedContent::getNextStep in src/Entity/OpignoGroupManagedContent.php
Get the next LPManagedContent object according to the user score.

File

src/Entity/OpignoGroupManagedContent.php, line 677

Class

OpignoGroupManagedContent
Defines the Opigno Group Content entity.

Namespace

Drupal\opigno_group_manager\Entity

Code

public function getSuccessfulRequiredActvities($required_activities, $last_attempt_id) {
  $successful_activities = [];
  $module_id = $this
    ->getEntityId();
  $type_id = $this
    ->getGroupContentTypeId();
  if ($type_id == 'ContentTypeCourse') {
    $modules_content = $this
      ->loadByGroupId($module_id);
    $modules_ids = [];
    foreach ($modules_content as $content) {
      $modules_ids[] = $content
        ->getEntityId();
    }
  }

  // Create activities hierarchical array.
  $required_activities_array = [];
  foreach ($required_activities as $id) {
    if (strpos($id, '-') !== FALSE) {
      [
        $activity_id,
        $answer_id,
      ] = explode('-', $id);
    }
    else {
      $activity_id = $id;
    }
    if (!array_key_exists($activity_id, $required_activities_array)) {
      $required_activities_array[$activity_id] = [];
    }
    if (isset($answer_id)) {
      $required_activities_array[$activity_id][$answer_id] = $answer_id;
    }
  }

  // Get successful activities and answers.
  foreach ($required_activities_array as $id => $required_activity) {
    if (is_array($last_attempt_id)) {
      foreach ($last_attempt_id as $last_id) {
        if (!empty($last_id)) {
          if (!empty($modules_ids)) {
            foreach ($modules_ids as $module_id) {
              $this
                ->getSuccessActivities($last_id
                ->id(), $id, $module_id, $successful_activities, $required_activity);
            }
          }
          else {
            $this
              ->getSuccessActivities($last_id
              ->id(), $id, $module_id, $successful_activities, $required_activity);
          }
        }
      }
    }
  }

  // Check if user successful answers satisfy required activities.
  $success = TRUE;
  foreach ($successful_activities as $successful_activitiy) {
    if (!in_array($successful_activitiy, $required_activities)) {
      $success = FALSE;
      break;
    }
  }
  if (!($success && count($required_activities) == count($successful_activities))) {

    // Block the step if wrong answer presents.
    $successful_activities = [];
  }
  return $successful_activities;
}