*/
require_once('locallib.php');
function scorm_add_instance($scorm) {
- $validate = scorm_validate($scorm);
-
- $errors = array();
- if (($validate->result != "regular") && ($validate->result != "found")) {
- $errors[] = $validate->result;
- if (isset($validate->errors) && (count($validate->errors[0]) > 0)) {
- foreach ($validate->errors as $error) {
- $errors[] = $error;
- }
- }
- } else {
- $scorm->pkgtype = $validate->pkgtype;
- $scorm->datadir = $validate->datadir;
- $scorm->launch = $validate->launch;
+ global $CFG;
+
+ require_once('locallib.php');
+
+ if (($packagedata = scorm_check_package($scorm)) != null) {
+ $scorm->pkgtype = $packagedata->pkgtype;
+ $scorm->datadir = $packagedata->datadir;
+ $scorm->launch = $packagedata->launch;
$scorm->parse = 1;
- }
- if(empty($scorm->datadir)) { //check to make sure scorm object is valid BEFORE entering it in the database.
- $errorstr = '';
- if (!empty($errors)) {
- foreach ($errors as $error) {
- $errorstr .= get_string($error,'scorm').'<br />';
- }
- error($errorstr);
+ $scorm->timemodified = time();
+ if (!scorm_external_link($scorm->reference)) {
+ $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
} else {
- error(get_string('badpackage', 'scorm'));
+ $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
+ $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
}
- } else {
- global $CFG;
- $scorm->timemodified = time();
- if(substr($scorm->reference,0,7)== 'http://'){
- $scorm->md5_result=md5_file($scorm->reference);
- }
+
$scorm = scorm_option2text($scorm);
$scorm->width = str_replace('%','',$scorm->width);
$scorm->height = str_replace('%','',$scorm->height);
// Parse scorm manifest
if ($scorm->parse == 1) {
- require_once('locallib.php');
$scorm->id = $id;
$scorm->launch = scorm_parse($scorm);
set_field('scorm','launch',$scorm->launch,'id',$scorm->id);
}
return $id;
+ } else {
+ error(get_string('badpackage','scorm'));
}
}
function scorm_update_instance($scorm) {
global $CFG;
- $validate = scorm_validate($scorm);
+ require_once('locallib.php');
- $errors = array();
- if (($validate->result != "regular") && ($validate->result != "found")) {
- $errorstr = get_string($validate->result,'scorm');
- if (isset($validate->errors) && (count($validate->errors[0]) > 0)) {
- foreach ($validate->errors as $error) {
- $errorstr .= '<br />'.get_string($error,'scorm');
- }
- }
- error($errorstr);
- exit();
- } else {
- $scorm->pkgtype = $validate->pkgtype;
- if ($validate->launch == 0) {
- $scorm->launch = $validate->launch;
- $scorm->datadir = $validate->datadir;
+ if (($packagedata = scorm_check_package($scorm)) != null) {
+ $scorm->pkgtype = $packagedata->pkgtype;
+ if ($packagedata->launch == 0) {
+ $scorm->launch = $packagedata->launch;
+ $scorm->datadir = $packagedata->datadir;
$scorm->parse = 1;
+ if (!scorm_external_link($scorm->reference)) {
+ $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
+ } else {
+ $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
+ $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
+ }
+ mtrace($scorm->md5hash);
} else {
$scorm->parse = 0;
}
$scorm->timemodified = time();
$scorm->id = $scorm->instance;
- if(substr($scorm->reference,0,7)== 'http://'){
- $scorm->md5_result=md5_file($scorm->reference);
- mtrace($scorm->md5_result);
- }
$scorm = scorm_option2text($scorm);
$scorm->width = str_replace('%','',$scorm->width);
$scorm->height = str_replace('%','',$scorm->height);
// Check if scorm manifest needs to be reparsed
if ($scorm->parse == 1) {
- require_once('locallib.php');
$scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
if (is_dir($scorm->dir.'/'.$scorm->id)) {
scorm_delete_files($scorm->dir.'/'.$scorm->id);
function scorm_cron () {
global $CFG;
- $sitetimezone = $CFG->timezone;
- /// Now see if there are any digest mails waiting to be sent, and if we should send them
+
+ require_once('locallib.php');
+
+ $sitetimezone = $CFG->timezone;
+ /// Now see if there are any digest mails waiting to be sent, and if we should send them
if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
set_config('scorm_updatetimelast', 0);
}
- $timenow = time();
+ $timenow = time();
$updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
- if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
-
+
+ if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
+
set_config('scorm_updatetimelast', $timenow);
mtrace('Updating scorm packages which require daily update');//"estamos actualizando"
- $scormsupdate = get_records_select("scorm","external=1");
- if(!empty($scormsupdate)) {
- foreach($scormsupdate as $scormupdate) {
- $scormupdate->instance = $scormupdate->id;
- $scormupinst = scorm_update_instance($scormupdate);
- }
- }
- }
+ $scormsupdate = get_records('scorm','updatefreq',UPDATE_EVERYDAY);
+ if (!empty($scormsupdate)) {
+ foreach($scormsupdate as $scormupdate) {
+ $scormupdate->instance = $scormupdate->id;
+ $id = scorm_update_instance($scormupdate);
+ }
+ }
+ }
+
return true;
}
<?php // $Id$
/// Constants and settings for module scorm
-require_once('lib.php');
+define('UPDATE_NEVER', '0');
+define('UPDATE_ONCHANGE', '1');
+define('UPDATE_EVERYDAY', '2');
+define('UPDATE_EVERYTIME', '3');
+
define('SCO_ALL', 0);
define('SCO_DATA', 1);
define('SCO_ONLY', 2);
* @param string $strpath The scorm data directory
* @return string/boolean
*/
-function scorm_datadir($strPath)
+function scorm_tempdir($strPath)
{
global $CFG;
}
}
+function scorm_basename($location) {
+ if (scorm_external_link($location)) {
+ return substr($location,strrpos($location,'/')+1);
+ } else {
+ return basename($location);
+ }
+}
+
/**
* Returns an object containing all datas relative to the given sco ID
*
function scorm_validate_manifest($manifest) {
$validation = new stdClass();
if (is_file($manifest)) {
- $validation->result = 'found';
+ $validation->result = true;
} else {
- $validation->result = 'notfound';
- $validation->launch = -1;
+ $validation->result = false;
+ $validation->errors['reference'] = get_string('nomanifest','scorm');
}
return $validation;
}
*/
function scorm_validate_aicc($packagedir) {
$validation = new stdClass();
- $validation->result = 'notfound';
+ $validation->result = false;
if (is_dir($packagedir)) {
if ($handle = opendir($packagedir)) {
while (($file = readdir($handle)) !== false) {
$ext = substr($file,strrpos($file,'.'));
if (strtolower($ext) == '.cst') {
- $validation->result = 'found';
+ $validation->result = true;
break;
}
}
closedir($handle);
}
}
- if ($validation->result == 'notfound') {
- $validation->launch = -1;
+ if ($validation->result == false) {
+ $validation->errors['reference'] = get_string('nomanifest','scorm');
}
return $validation;
}
function scorm_validate($data) {
global $CFG;
+ $validation = new stdClass();
+ $validation->errors = array();
+
+ if (!isset($data['course']) || empty($data['course'])) {
+ $validation->errors['reference'] = get_string('missingparam','scorm');
+ $validation->result = false;
+ return $validation;
+ }
+ $courseid = $data['course']; // Course Module ID
+
+ if (!isset($data['reference']) || empty($data['reference'])) {
+ $validation->errors['reference'] = get_string('packagefile','scorm');
+ $validation->result = false;
+ return $validation;
+ }
+ $reference = $data['reference']; // Package/manifest path/location
+
+ $scormid = $data['instance']; // scorm ID
+ $scorm = new stdClass();
+ if (!empty($scormid)) {
+ if (!$scorm = get_record("scorm","id",$scormid)) {
+ $validation->errors['reference'] = get_string('missingparam','scorm');
+ $validation->result = false;
+ return $validation;
+ }
+ }
+
+ if ($reference[0] == '#') {
+ require_once($repositoryconfigfile);
+ if ($CFG->repositoryactivate) {
+ $reference = $CFG->repository.substr($reference,1).'/imsmanifest.xml';
+ } else {
+ $validation->errors['reference'] = get_string('badpackage','scorm');
+ $validation->result = false;
+ return $validation;
+ }
+ } else if (!scorm_external_link($reference)) {
+ $reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
+ }
+
+ // Create a temporary directory to unzip package or copy manifest and validate package
+ $tempdir = '';
+ $scormdir = '';
+ if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
+ if ($tempdir = scorm_tempdir($scormdir)) {
+ $localreference = $tempdir."/".scorm_basename($reference);
+ copy ("$reference", $localreference);
+ if (!is_file($localreference)) {
+ $validation->errors['reference'] = get_string('badpackage','scorm');
+ $validation->result = false;
+ } else {
+ $ext = strtolower(substr(basename($localreference),strrpos(basename($localreference),'.')));
+ switch ($ext) {
+ case '.pif':
+ case '.zip':
+ if (!unzip_file($localreference, $tempdir, false)) {
+ $validation->errors['reference'] = get_string('unziperror','scorm');
+ $validation->result = false;
+ } else {
+ unlink ($localreference);
+ if (is_file($tempdir.'/imsmanifest.xml')) {
+ $validation = scorm_validate_manifest($tempdir.'/imsmanifest.xml');
+ $validation->pkgtype = 'SCORM';
+ } else {
+ $validation = scorm_validate_aicc($tempdir);
+ if (($validation->result == 'regular') || ($validation->result == 'found')) {
+ $validation->pkgtype = 'AICC';
+ } else {
+ $validation->errors['reference'] = get_string('nomanifest','scorm');
+ $validation->result = false;
+ }
+ }
+ }
+ break;
+ case '.xml':
+ if (basename($localreference) == 'imsmanifest.xml') {
+ $validation = scorm_validate_manifest($localreference);
+ } else {
+ $validation->errors['reference'] = get_string('nomanifest','scorm');
+ $validation->result = false;
+ }
+ break;
+ default:
+ $validation->errors['reference'] = get_string('badpackage','scorm');
+ $validation->result = false;
+ break;
+ }
+ }
+ if (is_dir($tempdir)) {
+ // Delete files and temporary directory
+ scorm_delete_files($tempdir);
+ }
+ } else {
+ $validation->errors['reference'] = get_string('packagedir','scorm');
+ $validation->result = false;
+ }
+ } else {
+ $validation->errors['reference'] = get_string('datadir','scorm');
+ $validation->result = false;
+ }
+ return $validation;
+}
+
+function scorm_check_package($data) {
+ global $CFG;
+
$courseid = $data->course; // Course Module ID
$reference = $data->reference; // Package path
$scormid = $data->instance; // scorm ID
$validation = new stdClass();
$fstat=array("mtime"=>0);
-
- if (!empty($courseid) && !empty($reference)) {
+
+ if (!empty($courseid) && !empty($reference)) {
+ $externalpackage = scorm_external_link($reference);
+
$validation->launch = 0;
- $validation->errors = array();
$referencefield = $reference;
if (empty($reference)) {
- $validation->launch = -1;
- $validation->result = "packagefile";
+ $validation = null;
} else if ($reference[0] == '#') {
require_once($repositoryconfigfile);
if ($CFG->repositoryactivate) {
$referencefield = $reference.'/imsmanfest.xml';
$reference = $CFG->repository.substr($reference,1).'/imsmanifest.xml';
} else {
- $validation->launch = -1;
- $validation->result = "packagefile";
+ $validation = null;
}
- } else if (substr($reference,0,7) != 'http://') {
+ } else if (!$externalpackage) {
$reference = $CFG->dataroot.'/'.$courseid.'/'.$reference;
}
// SCORM Update
//
//if (($validation->launch != -1) && is_file($reference)) {
- if (($validation->launch != -1) && (is_file($reference) || (substr($reference,0,7) == 'http://'))){
+ if ((!empty($validation)) && (is_file($reference) || $externalpackage)){
- if (substr($reference,0,7) != 'http://') {
- $fp = fopen($reference,"r");
- $fstat = fstat($fp);
- fclose($fp);
-
- }
- else if(substr($reference,0,7) == 'http://'){
- if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
- if ($tempdir = scorm_datadir($scormdir)) {
+ if (!$externalpackage) {
+ $mdcheck = md5_file($reference);
+ } else if ($externalpackage){
+ if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
+ if ($tempdir = scorm_tempdir($scormdir)) {
copy ("$reference", $tempdir."/".basename($reference));
- $mdcheck=md5_file($tempdir."/".basename($reference));
- unlink ($tempdir."/".basename($reference));
- rmdir($tempdir);
- }
- }
- }
+ $mdcheck = md5_file($tempdir."/".basename($reference));
+ scorm_delete_files($tempdir);
+ }
+ }
+ }
if ($scorm = get_record("scorm","id",$scormid)) {
if ($scorm->reference[0] == '#') {
} else {
$oldreference = $scorm->reference;
}
- } else if (substr($reference,0,7) != 'http://') {
+ } else if (!scorm_external_link($scorm->reference)) {
$oldreference = $CFG->dataroot.'/'.$courseid.'/'.$scorm->reference;
- } else{
- $oldreference = $scorm->reference;
- }
+ } else {
+ $oldreference = $scorm->reference;
+ }
$validation->launch = $scorm->launch;
- if ((($scorm->timemodified < $fstat["mtime"]) && ($oldreference == $reference) && (substr($reference,0,7) != 'http://')) || ($oldreference != $reference) || ((substr($reference,0,7) == 'http://') && ($mdcheck != $scorm->md5_result)&& ($oldreference == $reference))) {
+ if ((($oldreference == $reference) && ($mdcheck != $scorm->md5hash)) || ($oldreference != $reference)) {
// This is a new or a modified package
$validation->launch = 0;
} else {
// Old package already validated
- $validation->result = 'found';
if (strpos($scorm->version,'AICC') !== false) {
$validation->pkgtype = 'AICC';
} else {
}
}
} else {
- $validation->result = 'badinstance';
- $validation->launch = -1;
+ $validation = null;
}
} else {
- $validation->result = 'badreference';
- $validation->launch = -1;
+ $validation = null;
}
}
//$validation->launch = 0;
- if ($validation->launch == 0) {
+ if (($validation != null) && ($validation->launch == 0)) {
//
// Package must be validated
//
$ext = strtolower(substr(basename($reference),strrpos(basename($reference),'.')));
+ $tempdir = '';
switch ($ext) {
case '.pif':
case '.zip':
// Create a temporary directory to unzip package and validate package
- $tempdir = '';
$scormdir = '';
if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
- if ($tempdir = scorm_datadir($scormdir)) {
+ if ($tempdir = scorm_tempdir($scormdir)) {
copy ("$reference", $tempdir."/".basename($reference));
unzip_file($tempdir."/".basename($reference), $tempdir, false);
unlink ($tempdir."/".basename($reference));
$validation->pkgtype = 'AICC';
}
} else {
- $validation->result = "packagedir";
+ $validation = null;
}
} else {
- $validation->result = "datadir";
+ $validation = null;
}
break;
case '.xml':
if (basename($reference) == 'imsmanifest.xml') {
- $validation = scorm_validate_manifest($reference);
+ if ($externalpackage) {
+ if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
+ if ($tempdir = scorm_tempdir($scormdir)) {
+ copy ("$reference", $tempdir."/".basename($reference));
+ $validation = scorm_validate_manifest($tempdir.'/'.$reference);
+ }
+ }
+ } else {
+ $validation = scorm_validate_manifest($CFG->datadir.'/'.$course->id.'/'.$reference);
+ }
} else {
- $validation->result = "manifestfile";
+ $validation = null;
}
break;
default:
- $validation->result = "packagefile";
+ $validation = null;
break;
}
- if (($validation->result != "regular") && ($validation->result != "found")) {
+ if ($validation == null) {
if (is_dir($tempdir)) {
// Delete files and temporary directory
scorm_delete_files($tempdir);
}
- $validation->launch = -1;
} else {
- if ($ext == '.xml') {
+ if (($ext == '.xml') && (!$externalpackage)){
$validation->datadir = dirname($referencefield);
} else {
$validation->datadir = substr($tempdir,strlen($scormdir));
}
}
} else {
- $validation->launch = -1;
- $validation->result = 'badrequest';
+ $validation = null;
}
return $validation;
}