changed all the POST with option/required_param functions
[moodle.git] / mod / scorm / validate.php
1 <?php // $Id$
3     require_once("../../config.php");
4     require_once("lib.php");
6     $courseid = required_param('id', '', PARAM_INT);             // Course Module ID, or
7     $reference = required_param('reference', '', PARAM_PATH);    // Package path
8     $scormid = optional_param('instance', '', PARAM_INT);       // scorm ID
10     require_login($course->id, false, $cm);
12 if (confirm_sesskey() && !empty($courseid)) {
13     $launch = 0;
14     $validation = new stdClass();
15     if (empty($reference)) {
16         $launch = -1;
17         $validation->result = "packagefile";
18     }
19     if (!empty($scormid)) {  
20         //
21         // SCORM Update
22         //
23         if (is_file($CFG->dataroot.'/'.$courseid.'/'.$reference)) {
24             $fp = fopen($CFG->dataroot.'/'.$courseid.'/'.$reference,"r");
25             $fstat = fstat($fp);
26             fclose($fp);
27             if ($scorm = get_record("scorm","id",$scormid)) {
28                 $launch = $scorm->launch;
29                 if ((($scorm->timemodified < $fstat["mtime"]) && ($scorm->reference == $reference)) || ($scorm->reference != $reference)) {
30                     // This is a new package
31                     $launch = 0;
32                 } else {
33                     // Old package already validated
34                     $validation->result = 'found';
35                     if (strpos($scorm->version,'AICC') !== false) {
36                         $validation->pkgtype = 'AICC';
37                     } else {
38                         $validation->pkgtype = 'SCORM';
39                     }
40                 }
41             } else {
42                 $validation->result = 'badinstance';
43                 $launch = -1;
44             }
45         } else {
46             $validation->result = 'badreference';
47             $launch = -1;
48         }
49     }
50     //$launch = 0;
51     if ($launch == 0) {
52         //
53         // Package must be validated
54         //
56         // Create a temporary directory to unzip package and validate package
57         $tempdir = '';
58         $scormdir = '';
59         if ($scormdir = make_upload_directory("$courseid/$CFG->moddata/scorm")) {
60             if ($tempdir = scorm_datadir($scormdir)) {
61                 copy ("$CFG->dataroot/$courseid/$reference", $tempdir."/".basename($reference));
62                 $ext = strtolower(substr(basename($reference),strrpos(basename($reference),'.')));
63                 if (($ext == '.zip') || ($ext == '.pif')) {
64                     unzip_file($tempdir."/".basename($reference), $tempdir, false);
65                     unlink ($tempdir."/".basename($reference));
66                     $validation = scorm_validate($tempdir);
67                 } else {
68                     $validation->result = "packagefile";
69                 }
70             } else {
71                 $validation->result = "packagedir";
72             }
73         } else {
74             $validation->result = "datadir";
75         }
76         if (($validation->result != "regular") && ($validation->result != "found")) {
77             $validation->result = get_string($validation->result,'scorm');
78             if (is_dir($tempdir)) {
79                 // Delete files and temporary directory
80                 scorm_delete_files($tempdir);
81             }
82         } else {
83             $datadir = substr($tempdir,strlen($scormdir));
84         }
85     }
86     //
87     // Print validation result
88     //
89     echo 'result=' . $validation->result . "\n";
90     echo 'launch=' . $launch . "\n";
91     if (isset($validation->pkgtype)) {
92         echo 'pkgtype=' . $validation->pkgtype . "\n";
93     }
94     if (isset($datadir)) {
95         echo 'datadir=' . $datadir . "\n";
96     }
97     if (isset($validation->errors[1])) {
98         echo 'errorlogs='."\n";
99         foreach($validation->errors as $error) {
100             echo get_string($error->type,"scorm",$error->data) . "\n";
101         }
102     }
103 } else {
104     echo 'result=' . get_string('badrequest','scorm') . "\n";
106 ?>