View source
<?php
define('OPIGNO_SCORM_DIRECTORY', variable_get('opigno_scorm_directory', 'public://opigno_scorm_extracted'));
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,
),
);
}
function opigno_scorm_library_alter(&$libraries, $module) {
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();
}
}
function opigno_scorm_access($scorm) {
$access = NULL;
foreach (module_implements('opigno_scorm_access') as $module) {
$result = module_invoke($module, 'opigno_scorm_access', $scorm);
if (isset($result)) {
if ($result) {
return TRUE;
}
else {
$access = $result;
}
}
}
if (isset($access)) {
return $access;
}
else {
return user_access('access content');
}
}
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;
}
}
function opigno_scorm_scorm_load($id) {
return db_select('opigno_scorm_packages', 'o')
->fields('o', array())
->condition('id', $id)
->execute()
->fetchObject();
}
function opigno_scorm_scorm_load_by_fid($fid) {
return db_select('opigno_scorm_packages', 'o')
->fields('o', array())
->condition('fid', $fid)
->execute()
->fetchObject();
}
function opigno_scorm_scorm_delete($scorm) {
db_delete('opigno_scorm_packages')
->condition('id', $scorm->id)
->execute();
}
function opigno_scorm_sco_save($sco) {
$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)) {
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;
}
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;
}
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;
}
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;
}
function opigno_scorm_sco_delete($sco) {
db_delete('opigno_scorm_package_scos')
->condition('id', $sco->id)
->execute();
db_delete('opigno_scorm_package_sco_attributes')
->condition('sco_id', $sco->id)
->execute();
}
function opigno_scorm_scorm_cmi_set($uid, $scorm_id, $cmi_key, $value) {
$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;
}
}
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;
}
$context = array(
'uid' => $uid,
'scorm_id' => $scorm_id,
'original_value' => $data,
);
drupal_alter('opigno_scorm_scorm_cmi_get', $data, $cmi_key, $context);
return $data;
}
function opigno_scorm_sco_cmi_set($uid, $sco_id, $cmi_key, $value) {
$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;
}
}
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;
}
$context = array(
'uid' => $uid,
'sco_id' => $sco_id,
'original_value' => $data,
);
drupal_alter('opigno_scorm_sco_cmi_get', $data, $cmi_key, $context);
return $data;
}