You are here

function opigno_tincan_ilt_opigno_ilt_result_presave in Opigno TinCan API 8

Same name and namespace in other branches
  1. 3.x modules/opigno_tincan_ilt/opigno_tincan_ilt.module \opigno_tincan_ilt_opigno_ilt_result_presave()

Implements hook_ENTITY_TYPE_presave().

Here tincan statement sends when teacher update the meeting score.

File

modules/opigno_tincan_ilt/opigno_tincan_ilt.module, line 38
Contains opigno_tincan_ilt.module.

Code

function opigno_tincan_ilt_opigno_ilt_result_presave(EntityInterface $entity) {

  // Check if Tincan PHP library is installed.
  $has_library = opigno_tincan_api_tincanphp_is_installed();
  if (!$has_library) {
    return;
  }

  // Prevent to send results without changes.
  $current_result = $entity;
  $db_connection = \Drupal::service('database');
  $query = $db_connection
    ->select('opigno_ilt_result', 'oir')
    ->fields('oir', [
    'status',
    'score',
  ])
    ->condition('id', $current_result
    ->id())
    ->execute();
  $previous_result = $query
    ->fetchAssoc();
  if ($previous_result) {
    if ($current_result
      ->getStatus() == $previous_result['status'] && $current_result
      ->getScore() == $previous_result['score']) {

      // Result are the same as previous. Return.
      return;
    }
  }

  // Create and sent tincan statements.
  _opigno_tincan_ilt_create_and_send_tincan_statement($entity);
  return $entity;
}