renamed external to updatefreq
authorbobopinna <bobopinna>
Wed, 24 Jan 2007 10:39:24 +0000 (10:39 +0000)
committerbobopinna <bobopinna>
Wed, 24 Jan 2007 10:39:24 +0000 (10:39 +0000)
renamed md5_result to md5hash

mod/scorm/lib.php
mod/scorm/locallib.php
mod/scorm/mod_form.php
mod/scorm/version.php

index 7e2f4c1344b9078bb0e3ad6b7609b2d43d2581cc..93b205bc7e84229d58f34b66482fde7f10092e7a 100755 (executable)
 */
 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);
@@ -67,13 +52,14 @@ function scorm_add_instance($scorm) {
 
         // 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'));
     }
 }
 
@@ -88,24 +74,21 @@ function scorm_add_instance($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;
         }
@@ -113,11 +96,7 @@ function scorm_update_instance($scorm) {
 
     $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);
@@ -129,7 +108,6 @@ function scorm_update_instance($scorm) {
 
     // 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);
@@ -397,28 +375,33 @@ function scorm_print_recent_activity(&$logs, $isteacher=false) {
 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;
 }
 
index ba71c77f86aabe33efe9871603976c90e5452d9c..e94cabace3b0bc5a2dbd1826945e2cd63f1a74e8 100755 (executable)
@@ -1,7 +1,11 @@
 <?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);
@@ -124,7 +128,7 @@ function scorm_scandir($directory) {
 * @param string $strpath The scorm data directory
 * @return string/boolean
 */
-function scorm_datadir($strPath)
+function scorm_tempdir($strPath)
 {
     global $CFG;
 
@@ -203,6 +207,14 @@ function scorm_dirname($location) {
     }
 }
 
+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
 *
@@ -626,10 +638,10 @@ function scorm_parse($scorm) {
 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;
 }
@@ -642,21 +654,21 @@ function scorm_validate_manifest($manifest) {
 */
 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;
 }
@@ -665,30 +677,135 @@ function scorm_validate_aicc($packagedir) {
 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;
         }
 
@@ -697,24 +814,19 @@ function scorm_validate($data) {
         // 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] == '#') {
@@ -724,18 +836,17 @@ function scorm_validate($data) {
                         } 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 {
@@ -743,28 +854,26 @@ function scorm_validate($data) {
                         }
                     }
                 } 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));
@@ -776,31 +885,39 @@ function scorm_validate($data) {
                                 $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));
@@ -809,8 +926,7 @@ function scorm_validate($data) {
             }
         }
     } else {
-        $validation->launch = -1;
-        $validation->result = 'badrequest';
+        $validation = null;
     }
     return $validation;
 }
index 7ebe7cf7e0e5eb1826948a63b06ca88d38f0cbca..9ea0da1912d245913e4127dfc88d3d2e747c4b75 100644 (file)
@@ -168,14 +168,15 @@ class mod_scorm_mod_form extends moodleform_mod {
         $mform->setDefault('auto', 0);
         $mform->setAdvanced('auto');
 
-// Update external packages timing
+// Update packages timing
         $options = array();
-        $options[0]=get_string('onchanges','scorm');
-        $options[1]=get_string('everyday','scorm');
-        $options[2]=get_string('everytime','scorm');
-        $mform->addElement('select', 'external', get_string('external', 'scorm'), $options);
-        $mform->setDefault('external', 0);
-        $mform->setAdvanced('external');
+        $options[0]=get_string('never','scorm');
+        $options[1]=get_string('onchanges','scorm');
+        $options[2]=get_string('everyday','scorm');
+        $options[3]=get_string('everytime','scorm');
+        $mform->addElement('select', 'updatefreq', get_string('updatefreq', 'scorm'), $options);
+        $mform->setDefault('updatefreq', 0);
+        $mform->setAdvanced('updatefreq');
 
 //-------------------------------------------------------------------------------
 // Hidden Settings
@@ -232,5 +233,15 @@ class mod_scorm_mod_form extends moodleform_mod {
         }
     }
 
+    function validation($data) {
+        $validate = scorm_validate($data);
+
+        if ($validate->result) {
+            return true;
+        } else {
+            return $validate->errors;
+        }
+    }
+
 }
 ?>
index 5af382519d1e012aa820a3de85e2d2bad067770e..78c84c8466b9ded55d3451dab30b78f2a0e4ab89 100755 (executable)
@@ -10,7 +10,7 @@
 //       catch up now, so until 27th October please only increment in very tiny steps 
 //       in HEAD, until we get past that date..
 
-$module->version  = 2007011800;   // The (date) version of this module
+$module->version  = 2007012400;   // The (date) version of this module
 $module->requires = 2006080900;   // The version of Moodle that is required
 $module->cron     = 300;            // How often should cron check this module (seconds)?