You are here

function opigno_scorm_scorm_cmi_set in Opigno SCORM 3.x

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

Set a CMI data value for the given SCORM.

Parameters

int $uid: User ID.

int $scorm_id: Scorn ID.

string $cmi_key: Cmi key.

string $value: Value.

Return value

bool Scorm cmi set flag.

Throws

\Exception

1 call to opigno_scorm_scorm_cmi_set()
opigno_scorm_opigno_scorm_commit in ./opigno_scorm.module
Implements hook_opigno_scorm_commit().

File

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

Code

function opigno_scorm_scorm_cmi_set($uid, $scorm_id, $cmi_key, $value) {
  if (isset($value)) {
    $serialized = 0;
    if (is_array($value) || is_object($value)) {
      $value = serialize($value);
      $serialized = 1;
    }
    elseif (is_bool($value)) {
      $value = (int) $value;
    }
    $result = \Drupal::database()
      ->merge('opigno_scorm_scorm_cmi_data')
      ->keys([
      'uid' => $uid,
      'scorm_id' => $scorm_id,
      'cmi_key' => $cmi_key,
    ])
      ->fields([
      'uid' => $uid,
      'scorm_id' => $scorm_id,
      'cmi_key' => $cmi_key,
      'value' => $value,
      'serialized' => $serialized,
    ])
      ->execute();
    return !!$result;
  }
  else {
    return TRUE;
  }
}