You are here

protected function OpignoGroupManagedContent::getSuccessActivities 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::getSuccessActivities()

Get Success Activities from DB.

1 call to OpignoGroupManagedContent::getSuccessActivities()
OpignoGroupManagedContent::getSuccessfulRequiredActvities in src/Entity/OpignoGroupManagedContent.php
Returns successful required activities array.

File

src/Entity/OpignoGroupManagedContent.php, line 745

Class

OpignoGroupManagedContent
Defines the Opigno Group Content entity.

Namespace

Drupal\opigno_group_manager\Entity

Code

protected function getSuccessActivities($last_attempt_id, $id, $module_id, &$successful_activities, $required_activity = NULL) {
  $answer = \Drupal::entityTypeManager()
    ->getStorage('opigno_answer')
    ->loadByProperties([
    'user_module_status' => $last_attempt_id,
    'activity' => $id,
    'module' => $module_id,
  ]);
  if ($answer) {
    $user_responses_array = [];
    $answer = array_pop($answer);
    $xapi_data = json_decode($answer->field_xapidata->value);
    $wrong_activity = TRUE;
    if (empty($required_activity)) {

      // If no required answers in activity.
      // Check activity is successful.
      if (!empty($xapi_data->statement->result) && $xapi_data->statement->result->completion && $xapi_data->statement->result->success) {
        $wrong_activity = FALSE;
        if (!in_array($id, $successful_activities)) {

          // Add successful activity.
          $successful_activities[] = (string) $id;
        }
      }
    }
    if ($wrong_activity) {

      // If there are required answers in activity.
      if (!in_array($id, $successful_activities)) {

        // Add successful activity.
        $successful_activities[] = (string) $id;
      }
      $user_response = explode('[,]', $xapi_data->statement->result->response);
      if ($user_response) {
        if (!in_array($id, $user_responses_array)) {
          $user_responses_array[] = (string) $id;
        }
        foreach ($user_response as $item) {
          if ($item === 'true') {
            $item = '0';
          }
          if ($item === 'false') {
            $item = '1';
          }
          $user_responses_array[] = $id . '-' . $item;
          $successful_activities[] = $id . '-' . $item;
        }
      }
    }
  }
  return $successful_activities;
}