You are here

function opigno_h5p_opigno_answer_update in Opigno module 3.x

Same name and namespace in other branches
  1. 8 ActivityTypes/opigno_h5p/opigno_h5p.module \opigno_h5p_opigno_answer_update()

Implements hook_ENTITY_TYPE_update() for opigno_answer.

Insert into database xAPI data for h5p activities.

File

ActivityTypes/opigno_h5p/opigno_h5p.module, line 155
Module main functionality.

Code

function opigno_h5p_opigno_answer_update(EntityInterface $entity) {

  // Save xAPIData into Database.

  /* @var \Drupal\opigno_module\Entity\OpignoAnswer $answer*/
  $answer = $entity;
  $answer_type = $answer
    ->getType();
  if ($answer_type == 'opigno_h5p') {
    $activity = $answer
      ->getActivity();

    // Check if there are some results.

    /* @var $db_connection \Drupal\Core\Database\Connection */
    $db_connection = \Drupal::service('database');

    // Save statement data.
    $data = $db_connection
      ->select('opigno_h5p_user_answer_results', 'ohuar')
      ->fields('ohuar', [
      'answer_id',
    ])
      ->condition('answer_id', $answer
      ->id())
      ->condition('answer_vid', $answer
      ->getLoadedRevisionId())
      ->execute()
      ->fetchObject();
    if ($data) {

      // Delete previous results.
      $previous_results_deleted = $db_connection
        ->delete('opigno_h5p_user_answer_results')
        ->condition('answer_id', $answer
        ->id())
        ->condition('answer_vid', $answer
        ->getLoadedRevisionId())
        ->execute();
    }
    $xapi_data = $answer
      ->get('field_xapidata')
      ->getValue()[0]['value'];
    $h5p_report_obj = new H5PReportXAPIData($activity, $answer, json_decode($xapi_data));
    $h5p_report_obj
      ->saveXAPIData();
  }
}