You are here

public function OpignoScormController::scormCommit in Opigno SCORM 3.x

Same name and namespace in other branches
  1. 8 src/Controller/OpignoScormController.php \Drupal\opigno_scorm\Controller\OpignoScormController::scormCommit()

Scorm data commit method.

1 string reference to 'OpignoScormController::scormCommit'
opigno_scorm.routing.yml in ./opigno_scorm.routing.yml
opigno_scorm.routing.yml

File

src/Controller/OpignoScormController.php, line 68

Class

OpignoScormController
Class OpignoScormController.

Namespace

Drupal\opigno_scorm\Controller

Code

public function scormCommit($opigno_scorm_id, $opigno_scorm_sco_id) {
  $data_content = $GLOBALS['request']
    ->getContent();
  if (!empty($_POST['data'])) {
    $data = json_decode($_POST['data']);
  }
  elseif ($data_content) {
    $data = json_decode($data_content);
  }
  if (!empty($data)) {
    if (!empty($data->cmi->interactions)) {
      $_SESSION['scorm_answer_results'] = [
        'opigno_scorm_id' => $opigno_scorm_id,
        'opigno_scorm_sco_id' => $opigno_scorm_sco_id,
        'data' => $data,
      ];
    }
    $scorm_service = \Drupal::service('opigno_scorm.scorm');
    $scorm = $scorm_service
      ->scormLoadById($opigno_scorm_id);
    \Drupal::moduleHandler()
      ->invokeAll('opigno_scorm_commit', [
      $scorm,
      $opigno_scorm_sco_id,
      $data,
    ]);
    return new JsonResponse([
      'success' => 1,
    ]);
  }
  else {
    return new JsonResponse([
      'error' => 1,
      'message' => 'no data received',
    ]);
  }
}