Fixed last Explorer popup bug and added user score view
[moodle.git] / mod / scorm / scormAPI.php
CommitLineData
98ca59f6 1<?php
2 require_once("../../config.php");
3 require_once("lib.php");
4
5 optional_variable($id); // Course Module ID, or
6 optional_variable($a); // scorm ID
fc1b2663 7 optional_variable($scoid); // sco ID
8 optional_variable($mode);
98ca59f6 9
10 if ($id) {
11 if (! $cm = get_record("course_modules", "id", $id)) {
12 error("Course Module ID was incorrect");
13 }
14
15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course is misconfigured");
17 }
18
19 if (! $scorm = get_record("scorm", "id", $cm->instance)) {
20 error("Course module is incorrect");
21 }
22
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 }
34
35 require_login($course->id);
36
37 if ( $scoes_user = get_records_select("scorm_sco_users","userid = ".$USER->id." AND scormid = ".$scorm->id,"scoid ASC") ) {
5d8790cb 38 //
39 // Already user
40 //
41 if (!empty($scoid)) {
42 // Direct sco request
fc1b2663 43 $sco = get_record("scorm_scoes","id",$scoid);
98ca59f6 44 } else {
5d8790cb 45 // Search for first incomplete sco
98ca59f6 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 {
5d8790cb 51 // If review mode get the first
fc1b2663 52 if ($mode == "review") {
98ca59f6 53 $sco = get_record("scorm_scoes","id",$sco_user->scoid);
54 break;
55 }
56 }
57 }
58 }
5d8790cb 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 //
98ca59f6 67 if ($scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC")) {
5d8790cb 68 //
69 // Create user scoes records
70 //
98ca59f6 71 foreach ($scoes as $sco) {
72 if ($sco->launch != "") {
5d8790cb 73 if (!isset($first))
98ca59f6 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";
5d8790cb 81 else if (($sco->type == "sca") || ($sco->type == "asset"))
98ca59f6 82 $sco_user->$element = "completed";
83 $ident = insert_record("scorm_sco_users",$sco_user);
c5163bfd 84 }
98ca59f6 85 }
5d8790cb 86 if (isset($first))
87 $sco = $first;
88 if (!empty($scoid)) {
fc1b2663 89 if ($sco = get_record("scorm_scoes","id",$scoid))
c5163bfd 90 unset($first);
91 }
98ca59f6 92 }
93 }
94 //
95 // Get first, last, prev and next scoes
96 //
97 $scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC");
fc1b2663 98 $min = 0;
99 $max = 0;
100 $prevsco = 0;
101 $nextsco = 0;
98ca59f6 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 }
fc1b2663 116 $first = NULL;
117 $last = NULL;
98ca59f6 118 if ($sco->id == $min)
119 $first = $sco;
120 if ($sco->id == $max)
121 $last = $sco;
122
c5163bfd 123 // Get current sco User data
98ca59f6 124 $sco_user = get_record("scorm_sco_users","userid",$USER->id,"scoid",$sco->id);
c5163bfd 125
98ca59f6 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 }
b98eebbf 135 $navObj = "top.";
136 if ($scorm->popup == "")
137 $navObj = "top.nav.";
138
98ca59f6 139 include("api1_2.php");
c5163bfd 140
98ca59f6 141?>
76d35423 142
5d8790cb 143function 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 }
148}
c5163bfd 149
150function SCOInitialize() {
151<?php
152 if ( $sco->previous || $first) {
b98eebbf 153 print "\t".$navObj."document.navform.prev.disabled = true;\n";
154 print "\t".$navObj."document.navform.prev.style.display = 'none';\n";
c5163bfd 155 }
156 if ( $sco->next || $last) {
b98eebbf 157 print "\t".$navObj."document.navform.next.disabled = true;\n";
158 print "\t".$navObj."document.navform.next.style.display = 'none';\n";
c5163bfd 159 }
160?>
02f5d439 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?>
c5163bfd 169}
170
171function changeSco(direction) {
172 if (direction == "prev")
b98eebbf 173 <?php echo $navObj ?>document.navform.scoid.value="<?php echo $prevsco; ?>";
c5163bfd 174 else
b98eebbf 175 <?php echo $navObj ?>document.navform.scoid.value="<?php echo $nextsco; ?>";
c5163bfd 176
b98eebbf 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();
c5163bfd 179}