You are here

function opigno_scorm_extract in Opigno 7

Extract the SCORM package from the file.

Given a file ID (which should be a valid SCORM ZIP file), this function extracts the ZIP to OPIGNO_SCORM_DIRECTORY, finds the manifest file and parses it. Once parsed, it stores the manifest data.

Returns the id of the extracted SCORM package, or FALSE if an error occurs.

Parameters

int $fid: The file ID that we'll extract.

Return value

int|false

2 calls to opigno_scorm_extract()
opigno_scorm_ui_field_insert in modules/scorm/ui/opigno_scorm_ui.module
Implements hook_field_insert().
opigno_scorm_ui_field_update in modules/scorm/ui/opigno_scorm_ui.module
Implements hook_field_update().

File

modules/scorm/includes/opigno_scorm.manifest.inc, line 58
Manifest file extraction logic.

Code

function opigno_scorm_extract($fid) {
  $file = file_load($fid);
  $path = drupal_realpath($file->uri);
  $zip = new ZipArchive();
  $result = $zip
    ->open($path);
  if ($result === TRUE) {
    $extract_dir = OPIGNO_SCORM_DIRECTORY . '/scorm_' . $fid;
    $zip
      ->extractTo($extract_dir);
    $zip
      ->close();

    // This is a standard: the manifest file will always be here.
    $manifest_file = $extract_dir . '/imsmanifest.xml';
    if (file_exists($manifest_file)) {

      // Prepare the Scorm DB entry.
      $scorm = (object) array(
        'fid' => $fid,
        'extracted_dir' => $extract_dir,
        'manifest_file' => $manifest_file,
        'manifest_id' => '',
        'metadata' => '',
      );

      // Parse the manifest file and extract the data.
      $manifest_data = opigno_scorm_extract_manifest_data($manifest_file);

      // Get the manifest ID, if it's given.
      if (!empty($manifest_data['manifest_id'])) {
        $scorm->manifest_id = $manifest_data['manifest_id'];
      }

      // If the file contains (global) metadata, serialize it.
      if (!empty($manifest_data['metadata'])) {
        $scorm->metadata = serialize($manifest_data['metadata']);
      }

      // Try saving the SCORM to the DB.
      if (opigno_scorm_scorm_save($scorm)) {

        // Store each SCO.
        if (!empty($manifest_data['scos']['items'])) {
          foreach ($manifest_data['scos']['items'] as $i => $sco_item) {
            $sco = (object) array(
              'scorm_id' => $scorm->id,
              'organization' => $sco_item['organization'],
              'identifier' => $sco_item['identifier'],
              'parent_identifier' => $sco_item['parent_identifier'],
              'launch' => $sco_item['launch'],
              'type' => $sco_item['type'],
              'scorm_type' => $sco_item['scorm_type'],
              'title' => $sco_item['title'],
              'weight' => empty($sco_item['weight']) ? $sco_item['weight'] : 0,
              'attributes' => $sco_item['attributes'],
            );
            if (opigno_scorm_sco_save($sco)) {

              // @todo Store SCO attributes.
            }
            else {
              watchdog('opigno_scorm', "An error occured when saving an SCO.", array(), WATCHDOG_ERROR);
            }
          }
        }
        return TRUE;
      }
      else {
        watchdog('opigno_scorm', "An error occured when saving the SCORM package data.", array(), WATCHDOG_ERROR);
      }
    }
  }
  else {
    $error = 'none';
    switch ($result) {
      case ZipArchive::ER_EXISTS:
        $error = 'ER_EXISTS';
        break;
      case ZipArchive::ER_INCONS:
        $error = 'ER_INCONS';
        break;
      case ZipArchive::ER_INVAL:
        $error = 'ER_INVAL';
        break;
      case ZipArchive::ER_NOENT:
        $error = 'ER_NOENT';
        break;
      case ZipArchive::ER_NOZIP:
        $error = 'ER_NOZIP';
        break;
      case ZipArchive::ER_OPEN:
        $error = 'ER_OPEN';
        break;
      case ZipArchive::ER_READ:
        $error = 'ER_READ';
        break;
      case ZipArchive::ER_SEEK:
        $error = 'ER_SEEK';
        break;
    }
    watchdog('opigno_scorm', "An error occured when unziping the SCORM package data. Error: !error", array(
      '!error' => $error,
    ), WATCHDOG_ERROR);
  }
  return FALSE;
}