Fixed last Explorer popup bug and added user score view
[moodle.git] / mod / scorm / scormAPI.php
1 <?php
2     require_once("../../config.php");
3     require_once("lib.php");
5     optional_variable($id);    // Course Module ID, or
6     optional_variable($a);     // scorm ID
7     optional_variable($scoid); // sco ID
8     optional_variable($mode);
10     if ($id) {
11         if (! $cm = get_record("course_modules", "id", $id)) {
12             error("Course Module ID was incorrect");
13         }
15         if (! $course = get_record("course", "id", $cm->course)) {
16             error("Course is misconfigured");
17         }
19         if (! $scorm = get_record("scorm", "id", $cm->instance)) {
20             error("Course module is incorrect");
21         }
23     } else {
24         if (! $scorm = get_record("scorm", "id", $a)) {
25             error("Course module is incorrect");
26         }
27         if (! $course = get_record("course", "id", $scorm->course)) {
28             error("Course is misconfigured");
29         }
30         if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
31             error("Course Module ID was incorrect");
32         }
33     }
35     require_login($course->id);
37     if ( $scoes_user = get_records_select("scorm_sco_users","userid = ".$USER->id." AND scormid = ".$scorm->id,"scoid ASC") ) {
38         //
39         // Already user
40         //
41         if (!empty($scoid)) {   
42             // Direct sco request
43             $sco = get_record("scorm_scoes","id",$scoid);
44         } else {
45             // Search for first incomplete sco
46             foreach ( $scoes_user as $sco_user ) {
47                 if (($sco_user->cmi_core_lesson_status != "completed") && ($sco_user->cmi_core_lesson_status != "passed") && ($sco_user->cmi_core_lesson_status != "failed")) {
48                     $sco = get_record("scorm_scoes","id",$sco_user->scoid);
49                     break;
50                 } else {
51                     // If review mode get the first
52                     if ($mode == "review") {
53                         $sco = get_record("scorm_scoes","id",$sco_user->scoid);
54                         break;
55                     }
56                 }
57             }
58         }
59         if (!isset($sco)) {  // If no sco was found get the first of SCORM package
60             $scoes = get_records_select("scorm_scoes","scorm=".$scorm->id." AND launch<>'' order by id ASC");
61             $sco = each($scoes);
62         }
63     } else {
64         //
65         // A new user
66         //
67         if ($scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC")) {
68             //
69             // Create user scoes records
70             //
71             foreach ($scoes as $sco) {
72                 if ($sco->launch != "") {
73                     if (!isset($first))
74                         $first = $sco;
75                     $sco_user->userid = $USER->id;
76                     $sco_user->scoid = $sco->id;
77                     $sco_user->scormid = $scorm->id;
78                     $element = "cmi_core_lesson_status";
79                     if ($sco->type == "sco") 
80                         $sco_user->$element = "not attempted";
81                     else if (($sco->type == "sca") || ($sco->type == "asset"))
82                         $sco_user->$element = "completed";
83                     $ident = insert_record("scorm_sco_users",$sco_user);
84                 }
85             }
86             if (isset($first))
87                 $sco = $first;
88             if (!empty($scoid)) {
89                 if ($sco = get_record("scorm_scoes","id",$scoid))
90                     unset($first);
91             }
92         }
93     }
94     //
95     // Get first, last, prev and next scoes
96     //
97     $scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC");
98     $min = 0;
99     $max = 0;
100     $prevsco = 0;
101     $nextsco = 0;
102     foreach ($scoes as $fsco) {
103         if ($fsco->launch != "") {
104             if (!$min || ($min > $fsco->id))
105                 $min = $fsco->id;
106             if (!$max || ($max < $fsco->id))
107                 $max = $fsco->id;
108             if ((!$prevsco) || ($sco->id > $fsco->id)) {
109                 $prevsco = $fsco->id;
110             }
111             if ((!$nextsco) && ($sco->id < $fsco->id)) {
112                 $nextsco = $fsco->id;
113             }
114         }
115     }
116     $first = NULL;
117     $last = NULL;
118     if ($sco->id == $min)
119         $first = $sco;
120     if ($sco->id == $max)
121         $last = $sco;
123     // Get current sco User data
124     $sco_user = get_record("scorm_sco_users","userid",$USER->id,"scoid",$sco->id);
125     
126     if (scorm_external_link($sco->launch)) {
127         $result = $sco->launch;
128     } else {
129         if ($CFG->slasharguments) {
130             $result = "$CFG->wwwroot/file.php/$scorm->course/moddata/scorm$scorm->datadir/$sco->launch";
131         } else {
132             $result = "$CFG->wwwroot/file.php?file=/$scorm->course/moddata/scorm$scorm->datadir/$sco->launch";
133         }
134     }
135     $navObj = "top.";
136     if ($scorm->popup == "")
137         $navObj = "top.nav.";
138         
139     include("api1_2.php");
141 ?>
143 function hilightcurrent(popupmenu) {
144     for (i=0;i < popupmenu.options.length;i++) {
145          if ( popupmenu.options[i].value == <?php echo $sco->id; ?> )
146                 popupmenu.options[i].selected = true;
147     }
150 function SCOInitialize() { 
151 <?php
152     if ( $sco->previous || $first) {
153         print "\t".$navObj."document.navform.prev.disabled = true;\n";
154         print "\t".$navObj."document.navform.prev.style.display = 'none';\n";
155     }
156     if ( $sco->next || $last) {
157         print "\t".$navObj."document.navform.next.disabled = true;\n";
158         print "\t".$navObj."document.navform.next.style.display = 'none';\n";
159     }
160 ?>
161 <?php   
162     if ($scorm->popup == "") { 
163         echo "\t    top.main.location=\"$result\";\n";
164         echo "\t    hilightcurrent(".$navObj."document.navform.courseStructure);\n";
165     } else {
166         echo "\t    top.main = window.open('$result','main','$scorm->popup');\n";
167     }
168 ?>
169
171 function changeSco(direction) {
172         if (direction == "prev")
173             <?php echo $navObj ?>document.navform.scoid.value="<?php echo $prevsco; ?>";
174         else
175             <?php echo $navObj ?>document.navform.scoid.value="<?php echo $nextsco; ?>";
176             
177         //alert ("Prev: <?php echo $prevsco; ?>\nNext: <?php echo $nextsco; ?>\nNew SCO: "+<?php echo $navObj ?>document.navform.scoid.value);
178         <?php echo $navObj ?>document.navform.submit();
179 }