You are here

opigno_scorm.module in Opigno 7

Opigno SCORM API.

File

modules/scorm/opigno_scorm.module
View source
<?php

/**
 * @file
 * Opigno SCORM API.
 */
define('OPIGNO_SCORM_DIRECTORY', variable_get('opigno_scorm_directory', 'public://opigno_scorm_extracted'));

/**
 * Implements hook_menu().
 */
function opigno_scorm_menu() {
  return array(
    'opigno/scorm/%opigno_scorm_scorm/cmi/%/sco/%/%' => array(
      'page callback' => 'opigno_scorm_ajax_sco_cmi',
      'page arguments' => array(
        4,
        6,
        7,
      ),
      'access callback' => 'opigno_scorm_access',
      'access arguments' => array(
        2,
      ),
      'file' => 'includes/opigno_scorm.ajax.inc',
      'type' => MENU_CALLBACK,
    ),
  );
}

/**
 * Implements hook_library_alter().
 */
function opigno_scorm_library_alter(&$libraries, $module) {

  // Add available JavaScript tests and dependencies.
  if ($module == 'qunit') {
    $path = drupal_get_path('module', 'opigno_scorm');
    $libraries['qunit']['js'][$path . '/js/lib/api-2004.js'] = array();
    $libraries['qunit']['js'][$path . '/js/tests/api-2004.js'] = array();
  }
}

/**
 * Access callback: verify access to the SCORM object.
 *
 * Invokes hook_opigno_scorm_access() to verify access. If any module
 * GRANTS access, returns true, even if some other module DENIES access.
 *
 * @param object $scorm
 *
 * @return bool
 */
function opigno_scorm_access($scorm) {
  $access = NULL;
  foreach (module_implements('opigno_scorm_access') as $module) {
    $result = module_invoke($module, 'opigno_scorm_access', $scorm);

    // Does the module have something to say about this SCORM package ?
    // If it is NULL, skip. If it is set, check it.
    if (isset($result)) {

      // Access granted, end here.
      if ($result) {
        return TRUE;
      }
      else {

        // Access denied, but someone might still grant access.
        // Continue.
        $access = $result;
      }
    }
  }

  // If no-one had anything to say about granting access, return 'access content'.
  // Else, return the access result.
  if (isset($access)) {
    return $access;
  }
  else {
    return user_access('access content');
  }
}

/**
 * Save a SCORM package information.
 *
 * @param object $scorm
 *
 * @return bool
 */
function opigno_scorm_scorm_save($scorm) {
  if (!empty($scorm->id)) {
    return db_update('opigno_scorm_packages')
      ->fields((array) $scorm)
      ->condition('id', $scorm->id)
      ->execute();
  }
  else {
    $id = db_insert('opigno_scorm_packages')
      ->fields((array) $scorm)
      ->execute();
    $scorm->id = $id;
    return !!$id;
  }
}

/**
 * Load a SCORM package information.
 *
 * @param int $id
 *
 * @return object|false
 */
function opigno_scorm_scorm_load($id) {
  return db_select('opigno_scorm_packages', 'o')
    ->fields('o', array())
    ->condition('id', $id)
    ->execute()
    ->fetchObject();
}

/**
 * Load SCORM package information by file ID.
 *
 * @param int $fid
 *
 * @return object|false
 */
function opigno_scorm_scorm_load_by_fid($fid) {
  return db_select('opigno_scorm_packages', 'o')
    ->fields('o', array())
    ->condition('fid', $fid)
    ->execute()
    ->fetchObject();
}

/**
 * Delete a SCORM package information.
 *
 * @todo Delete all SCOs as well.
 *
 * @param object $scorm
 */
function opigno_scorm_scorm_delete($scorm) {
  db_delete('opigno_scorm_packages')
    ->condition('id', $scorm->id)
    ->execute();
}

/**
 * Save a SCO information.
 *
 * @param object $sco
 *
 * @return bool
 */
function opigno_scorm_sco_save($sco) {

  // 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 = db_update('opigno_scorm_package_scos')
      ->fields((array) $sco)
      ->condition('id', $sco->id)
      ->execute();
  }
  else {
    $id = db_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.
    db_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;
      }
      db_insert('opigno_scorm_package_sco_attributes')
        ->fields(array(
        'sco_id' => $sco->id,
        'attribute' => $key,
        'value' => $value,
        'serialized' => $serialized,
      ))
        ->execute();
    }
  }
  return $res;
}

/**
 * Load a SCO information.
 *
 * @param int $id
 *
 * @return object|false
 */
function opigno_scorm_sco_load($id) {
  $sco = db_select('opigno_scorm_package_scos', 'o')
    ->fields('o', array())
    ->condition('id', $id)
    ->execute()
    ->fetchObject();
  if ($sco) {
    $sco->attributes = _opigno_scorm_sco_load_attributes($sco->id);
  }
  return $sco;
}

/**
 * Load a SCO information by SCORM id.
 *
 * @param int $scorm_id
 *
 * @return object|false
 */
function opigno_scorm_sco_load_by_scorm_id($scorm_id) {
  $sco = db_select('opigno_scorm_package_scos', 'o')
    ->fields('o', array())
    ->condition('scorm_id', $scorm_id)
    ->execute()
    ->fetchObject();
  if ($sco) {
    $sco->attributes = _opigno_scorm_sco_load_attributes($sco->id);
  }
  return $sco;
}

/**
 * Helper function to load a SCO attributes.
 *
 * @param int $sco_id
 *
 * @return array
 */
function _opigno_scorm_sco_load_attributes($sco_id) {
  $attributes = array();
  $result = db_select('opigno_scorm_package_sco_attributes', 'o')
    ->fields('o', array(
    'attribute',
    'value',
    'serialized',
  ))
    ->condition('sco_id', $sco_id)
    ->execute();
  while ($row = $result
    ->fetchObject()) {
    $attributes[$row->attribute] = !empty($row->serialized) ? unserialize($row->value) : $row->value;
  }
  return $attributes;
}

/**
 * Delete a SCO package information.
 *
 * @param object $sco
 */
function opigno_scorm_sco_delete($sco) {
  db_delete('opigno_scorm_package_scos')
    ->condition('id', $sco->id)
    ->execute();

  // Remove all old attributes, to prevent duplicates.
  db_delete('opigno_scorm_package_sco_attributes')
    ->condition('sco_id', $sco->id)
    ->execute();
}

/**
 * Set a CMI data value for the given SCORM.
 *
 * @param int $uid
 * @param int $scorm_id
 * @param string $cmi_key
 * @param string $value
 *
 * @return bool
 */
function opigno_scorm_scorm_cmi_set($uid, $scorm_id, $cmi_key, $value) {

  // Allow modules to alter the value. If the $value is set to NULL, it is assumed
  // a module takes over the persisting of the data and the insertion query
  // will be skipped.
  $context = array(
    'uid' => $uid,
    'scorm_id' => $scorm_id,
    'original_value' => $value,
  );
  drupal_alter('opigno_scorm_scorm_cmi_set', $value, $cmi_key, $context);
  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 = db_merge('opigno_scorm_scorm_cmi_data')
      ->key(array(
      'uid' => $uid,
      'scorm_id' => $scorm_id,
      'cmi_key' => $cmi_key,
    ))
      ->fields(array(
      'uid' => $uid,
      'scorm_id' => $scorm_id,
      'cmi_key' => $cmi_key,
      'value' => $value,
      'serialized' => $serialized,
    ))
      ->execute();
    return !!$result;
  }
  else {
    return TRUE;
  }
}

/**
 * Get a CMI data value for the given SCORM.
 *
 * @param int $uid
 * @param int $scorm_id
 * @param string $cmi_key
 *
 * @return mixed|null
 */
function opigno_scorm_scorm_cmi_get($uid, $scorm_id, $cmi_key) {
  $data = NULL;
  $result = db_select('opigno_scorm_scorm_cmi_data', 'o')
    ->fields('o', array(
    'value',
    'serialized',
  ))
    ->condition('o.uid', $uid)
    ->condition('o.scorm_id', $scorm_id)
    ->condition('o.cmi_key', $cmi_key)
    ->execute()
    ->fetchObject();
  if (isset($result->value)) {
    $data = !empty($result->serialized) ? unserialize($result->value) : $result->value;
  }

  // Allow modules to alter the data (or even set it if it doesn't exist).
  $context = array(
    'uid' => $uid,
    'scorm_id' => $scorm_id,
    'original_value' => $data,
  );
  drupal_alter('opigno_scorm_scorm_cmi_get', $data, $cmi_key, $context);
  return $data;
}

/**
 * Set a CMI data value for the given SCO.
 *
 * @param int $uid
 * @param int $sco_id
 * @param string $cmi_key
 * @param string $value
 *
 * @return bool
 */
function opigno_scorm_sco_cmi_set($uid, $sco_id, $cmi_key, $value) {

  // Allow modules to alter the value. If the $value is set to NULL, it is assumed
  // a module takes over the persisting of the data and the insertion query
  // will be skipped.
  $context = array(
    'uid' => $uid,
    'sco_id' => $sco_id,
    'original_value' => $value,
  );
  drupal_alter('opigno_scorm_sco_cmi_set', $value, $cmi_key, $context);
  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 = db_merge('opigno_scorm_sco_cmi_data')
      ->key(array(
      'uid' => $uid,
      'sco_id' => $sco_id,
      'cmi_key' => $cmi_key,
    ))
      ->fields(array(
      'uid' => $uid,
      'sco_id' => $sco_id,
      'cmi_key' => $cmi_key,
      'value' => $value,
      'serialized' => $serialized,
    ))
      ->execute();
    return !!$result;
  }
  else {
    return TRUE;
  }
}

/**
 * Get a CMI data value for the given SCO.
 *
 * @param int $uid
 * @param int $sco_id
 * @param string $cmi_key
 *
 * @return mixed|null
 */
function opigno_scorm_sco_cmi_get($uid, $sco_id, $cmi_key) {
  $data = NULL;
  $result = db_select('opigno_scorm_sco_cmi_data', 'o')
    ->fields('o', array(
    'value',
    'serialized',
  ))
    ->condition('o.uid', $uid)
    ->condition('o.sco_id', $sco_id)
    ->condition('o.cmi_key', $cmi_key)
    ->execute()
    ->fetchObject();
  if (isset($result->value)) {
    $data = !empty($result->serialized) ? unserialize($result->value) : $result->value;
  }

  // Allow modules to alter the data (or even set it if it doesn't exist).
  $context = array(
    'uid' => $uid,
    'sco_id' => $sco_id,
    'original_value' => $data,
  );
  drupal_alter('opigno_scorm_sco_cmi_get', $data, $cmi_key, $context);
  return $data;
}

Functions

Namesort descending Description
opigno_scorm_access Access callback: verify access to the SCORM object.
opigno_scorm_library_alter Implements hook_library_alter().
opigno_scorm_menu Implements hook_menu().
opigno_scorm_scorm_cmi_get Get a CMI data value for the given SCORM.
opigno_scorm_scorm_cmi_set Set a CMI data value for the given SCORM.
opigno_scorm_scorm_delete Delete a SCORM package information.
opigno_scorm_scorm_load Load a SCORM package information.
opigno_scorm_scorm_load_by_fid Load SCORM package information by file ID.
opigno_scorm_scorm_save Save a SCORM package information.
opigno_scorm_sco_cmi_get Get a CMI data value for the given SCO.
opigno_scorm_sco_cmi_set Set a CMI data value for the given SCO.
opigno_scorm_sco_delete Delete a SCO package information.
opigno_scorm_sco_load Load a SCO information.
opigno_scorm_sco_load_by_scorm_id Load a SCO information by SCORM id.
opigno_scorm_sco_save Save a SCO information.
_opigno_scorm_sco_load_attributes Helper function to load a SCO attributes.

Constants

Namesort descending Description
OPIGNO_SCORM_DIRECTORY @file Opigno SCORM API.