You are here

public function OpignoScormQuizResponse::save in Opigno 7

@copydoc QuizQuestionResponse::save()

File

modules/scorm/quiz/includes/opigno_scorm_quiz.response.inc, line 13
Response type class definition.

Class

OpignoScormQuizResponse
@file Response type class definition.

Code

public function save() {
  global $user;
  if (!empty($this->question->opigno_scorm_package[LANGUAGE_NONE][0]['fid'])) {
    $scorm = opigno_scorm_scorm_load_by_fid($this->question->opigno_scorm_package[LANGUAGE_NONE][0]['fid']);

    // Get SCORM API version.
    $metadata = unserialize($scorm->metadata);
    if (strpos($metadata['schemaversion'], '1.2') !== FALSE) {
      $scorm_version = '1.2';
      $completion_key = 'cmi.core.lesson_status';
      $raw_key = 'cmi.core.score.raw';
    }
    else {
      $scorm_version = '2004';
      $completion_key = 'cmi.completion_status';
      $raw_key = 'cmi.score.raw';
    }

    // We get the latest result. The way the SCORM API works always overwrites attempts
    // for the global CMI storage. The result stored is always the latest. Get it,
    // and presist it again in the user results table so we can track results through
    // time.
    module_load_include('module', 'opigno_scorm_ui');
    $scaled = opigno_scorm_ui_scorm_cmi_get($user->uid, $scorm->id, 'cmi.score.scaled', '');
  }
  $completion = opigno_scorm_ui_scorm_cmi_get($user->uid, $scorm->id, $completion_key, '');
  $raw = opigno_scorm_ui_scorm_cmi_get($user->uid, $scorm->id, $raw_key, '');
  if ($scorm_version == '1.2' && !empty($raw)) {
    $scaled = $raw / 100;
  }
  if (empty($completion)) {
    $scaled = 0;
  }
  if ($completion == "completed" && empty($raw) && !is_numeric($raw)) {
    $scaled = 1;
  }
  if ($completion == "incomplete" && empty($raw) && !is_numeric($raw)) {
    $scaled = 0;
  }

  // Something went wrong. Set a score of -1.
  if (!isset($scaled) || !is_numeric($scaled)) {
    $scaled = -1;
  }
  db_insert('opigno_scorm_quiz_user_results')
    ->fields(array(
    'question_nid' => $this->question->nid,
    'question_vid' => $this->question->vid,
    'result_id' => $this->rid,
    'score_scaled' => $scaled,
  ))
    ->execute();

  // Delete all saved suspend data.
  db_delete('opigno_scorm_scorm_cmi_data')
    ->condition('uid', $user->uid)
    ->condition('scorm_id', $scorm->id)
    ->condition('cmi_key', 'cmi.suspend_data%', 'LIKE')
    ->execute();

  // Delete last user sco id.
  db_delete('opigno_scorm_scorm_cmi_data')
    ->condition('uid', $user->uid)
    ->condition('scorm_id', $scorm->id)
    ->condition('cmi_key', 'user.sco')
    ->execute();
}