You are here

function _opigno_scorm_save_interaction in Opigno SCORM 3.x

Same name and namespace in other branches
  1. 8 opigno_scorm.module \_opigno_scorm_save_interaction()

Recursive save of xAPI data.

Parameters

mixed $interaction: Interaction object array.

\Drupal\opigno_module\Entity\OpignoAnswer $answer: OpignoAnswer object.

1 call to _opigno_scorm_save_interaction()
_opigno_scorm_form_submit in ./opigno_scorm.module
Form submit callback, used in opigno_h5p_form_alter().

File

./opigno_scorm.module, line 354
Module functionality implementation.

Code

function _opigno_scorm_save_interaction($interaction, OpignoAnswer $answer) {
  $db_connection = \Drupal::service('database');
  $activity = $answer
    ->getActivity();
  $response = '';
  if (isset($interaction->learner_response)) {
    $response = $interaction->learner_response;
  }
  elseif (isset($interaction->student_response)) {
    $response = $interaction->student_response;
  }
  if ($response != '') {
    $response = _opigno_scorm_format_string_length($response, 255);
  }
  if (!empty($interaction->correct_responses) && key_exists(0, $interaction->correct_responses)) {
    try {
      $correct_responses_pattern = $interaction->correct_responses[0]->pattern;
    } catch (\Exception $e) {
      \Drupal::logger('opigno_scorm')
        ->error($e
        ->getMessage());
      \Drupal::messenger()
        ->addMessage($e
        ->getMessage(), 'error');
    }
  }
  try {

    // Save SCORM interaction data.
    $db_connection
      ->insert('opigno_scorm_user_answer_results')
      ->fields([
      'interaction_id' => _opigno_scorm_format_string_length($interaction->id, 191),
      'question_id' => $activity
        ->id(),
      'question_vid' => $activity
        ->getLoadedRevisionId(),
      'answer_id' => $answer
        ->id(),
      'answer_vid' => $answer
        ->getLoadedRevisionId(),
      'interaction_type' => _opigno_scorm_format_string_length($interaction->type, 32),
      'description' => _opigno_scorm_format_string_length($interaction->description, 255),
      'correct_responses_pattern' => isset($correct_responses_pattern) ? $correct_responses_pattern : '',
      'response' => $response,
      'result' => _opigno_scorm_format_string_length($interaction->result, 16),
      'timestamp' => !empty($interaction->timestamp) ? strtotime($interaction->timestamp) : time(),
    ])
      ->execute();
  } catch (Exception $e) {
    \Drupal::logger('opigno_scorm')
      ->error($e
      ->getMessage());
  }
}