You are here

public function OpignoScorm::scormScoSave in Opigno SCORM 8

Same name and namespace in other branches
  1. 3.x src/OpignoScorm.php \Drupal\opigno_scorm\OpignoScorm::scormScoSave()

Save a SCO information.

Parameters

object $sco: Sco object.

Return value

bool Sco save flag.

Throws

\Exception

1 call to OpignoScorm::scormScoSave()
OpignoScorm::scormExtract in src/OpignoScorm.php
Extract and save Scorm data from Scorm package.

File

src/OpignoScorm.php, line 187

Class

OpignoScorm
Class OpignoScorm.

Namespace

Drupal\opigno_scorm

Code

public function scormScoSave($sco) {
  $connection = $this->database;

  // The attributes is not a field in the database, but
  // a representation of a table relationship.
  // Cache them here and unset the property for the
  // DB query.
  $attributes = $sco->attributes;
  unset($sco->attributes);
  if (!empty($sco->id)) {
    $res = $connection
      ->update('opigno_scorm_package_scos')
      ->fields((array) $sco)
      ->condition('id', $sco->id)
      ->execute();
  }
  else {
    $id = $connection
      ->insert('opigno_scorm_package_scos')
      ->fields((array) $sco)
      ->execute();
    $sco->id = $id;
    $res = !!$id;
  }
  if ($res && !empty($attributes)) {

    // Remove all old attributes, to prevent duplicates.
    $connection
      ->delete('opigno_scorm_package_sco_attributes')
      ->condition('sco_id', $sco->id)
      ->execute();
    foreach ($attributes as $key => $value) {
      $serialized = 0;
      if (is_array($value) || is_object($value)) {
        $value = serialize($value);
        $serialized = 1;
      }
      elseif (is_bool($value)) {
        $value = (int) $value;
      }
      $connection
        ->insert('opigno_scorm_package_sco_attributes')
        ->fields([
        'sco_id' => $sco->id,
        'attribute' => $key,
        'value' => $value,
        'serialized' => $serialized,
      ])
        ->execute();
    }
  }
  return $res;
}