translated one
[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
6d9c91c7 43 //$sco = get_record("scorm_scoes","id",$scoid);
44 if ($sco = get_record("scorm_scoes","id",$scoid)) {
45 if ($sco->launch == '') {
46 // Search for th first launchable sco
47 if ($scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC")) {
48 $sco = current($scoes);
49 while ($sco->id < $scoid) {
50 $sco = next($scoes);
51 }
52 while ($sco->launch == '') {
53 $sco = next($scoes);
54 }
55 }
56 }
57 }
98ca59f6 58 } else {
5d8790cb 59 // Search for first incomplete sco
98ca59f6 60 foreach ( $scoes_user as $sco_user ) {
61 if (($sco_user->cmi_core_lesson_status != "completed") && ($sco_user->cmi_core_lesson_status != "passed") && ($sco_user->cmi_core_lesson_status != "failed")) {
62 $sco = get_record("scorm_scoes","id",$sco_user->scoid);
63 break;
64 } else {
5d8790cb 65 // If review mode get the first
fc1b2663 66 if ($mode == "review") {
98ca59f6 67 $sco = get_record("scorm_scoes","id",$sco_user->scoid);
68 break;
69 }
70 }
71 }
72 }
5d8790cb 73 if (!isset($sco)) { // If no sco was found get the first of SCORM package
74 $scoes = get_records_select("scorm_scoes","scorm=".$scorm->id." AND launch<>'' order by id ASC");
75 $sco = each($scoes);
76 }
77 } else {
78 //
79 // A new user
80 //
98ca59f6 81 if ($scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC")) {
5d8790cb 82 //
83 // Create user scoes records
84 //
98ca59f6 85 foreach ($scoes as $sco) {
86 if ($sco->launch != "") {
5d8790cb 87 if (!isset($first))
98ca59f6 88 $first = $sco;
89 $sco_user->userid = $USER->id;
90 $sco_user->scoid = $sco->id;
91 $sco_user->scormid = $scorm->id;
92 $element = "cmi_core_lesson_status";
93 if ($sco->type == "sco")
94 $sco_user->$element = "not attempted";
5d8790cb 95 else if (($sco->type == "sca") || ($sco->type == "asset"))
98ca59f6 96 $sco_user->$element = "completed";
97 $ident = insert_record("scorm_sco_users",$sco_user);
c5163bfd 98 }
98ca59f6 99 }
5d8790cb 100 if (isset($first))
101 $sco = $first;
102 if (!empty($scoid)) {
fc1b2663 103 if ($sco = get_record("scorm_scoes","id",$scoid))
c5163bfd 104 unset($first);
105 }
98ca59f6 106 }
107 }
108 //
109 // Get first, last, prev and next scoes
110 //
111 $scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC");
fc1b2663 112 $min = 0;
113 $max = 0;
114 $prevsco = 0;
115 $nextsco = 0;
98ca59f6 116 foreach ($scoes as $fsco) {
117 if ($fsco->launch != "") {
118 if (!$min || ($min > $fsco->id))
119 $min = $fsco->id;
120 if (!$max || ($max < $fsco->id))
121 $max = $fsco->id;
122 if ((!$prevsco) || ($sco->id > $fsco->id)) {
123 $prevsco = $fsco->id;
124 }
125 if ((!$nextsco) && ($sco->id < $fsco->id)) {
126 $nextsco = $fsco->id;
127 }
128 }
129 }
fc1b2663 130 $first = NULL;
131 $last = NULL;
98ca59f6 132 if ($sco->id == $min)
133 $first = $sco;
134 if ($sco->id == $max)
135 $last = $sco;
136
c5163bfd 137 // Get current sco User data
98ca59f6 138 $sco_user = get_record("scorm_sco_users","userid",$USER->id,"scoid",$sco->id);
c5163bfd 139
98ca59f6 140 if (scorm_external_link($sco->launch)) {
141 $result = $sco->launch;
142 } else {
143 if ($CFG->slasharguments) {
144 $result = "$CFG->wwwroot/file.php/$scorm->course/moddata/scorm$scorm->datadir/$sco->launch";
145 } else {
146 $result = "$CFG->wwwroot/file.php?file=/$scorm->course/moddata/scorm$scorm->datadir/$sco->launch";
147 }
148 }
b98eebbf 149 $navObj = "top.";
150 if ($scorm->popup == "")
6d9c91c7 151 $navObj = "top.navigation.";
b98eebbf 152
98ca59f6 153 include("api1_2.php");
c5163bfd 154
98ca59f6 155?>
76d35423 156
5d8790cb 157function hilightcurrent(popupmenu) {
158 for (i=0;i < popupmenu.options.length;i++) {
159 if ( popupmenu.options[i].value == <?php echo $sco->id; ?> )
160 popupmenu.options[i].selected = true;
161 }
162}
c5163bfd 163
164function SCOInitialize() {
165<?php
166 if ( $sco->previous || $first) {
b98eebbf 167 print "\t".$navObj."document.navform.prev.disabled = true;\n";
168 print "\t".$navObj."document.navform.prev.style.display = 'none';\n";
c5163bfd 169 }
170 if ( $sco->next || $last) {
b98eebbf 171 print "\t".$navObj."document.navform.next.disabled = true;\n";
172 print "\t".$navObj."document.navform.next.style.display = 'none';\n";
c5163bfd 173 }
174?>
02f5d439 175<?php
176 if ($scorm->popup == "") {
177 echo "\t top.main.location=\"$result\";\n";
178 echo "\t hilightcurrent(".$navObj."document.navform.courseStructure);\n";
179 } else {
1a12b1f1 180 $popuplocation = '';
181 if (isset($_COOKIE["SCORMpopup"])) {
182 $popuplocation = $_COOKIE["SCORMpopup"];
183 }
184 echo "\t top.main = window.open('$result','main','$scorm->popup$popuplocation');\n";
02f5d439 185 }
186?>
c5163bfd 187}
188
189function changeSco(direction) {
1a12b1f1 190 if (direction == "previous")
191 <?php echo $navObj ?>document.navform.scoid.value="<?php echo $prevsco; ?>";
192 else
193 <?php echo $navObj ?>document.navform.scoid.value="<?php echo $nextsco; ?>";
c5163bfd 194
1a12b1f1 195 //alert ("Prev: <?php echo $prevsco; ?>\nNext: <?php echo $nextsco; ?>\nNew SCO: "+<?php echo $navObj ?>document.navform.scoid.value);
196 <?php echo $navObj ?>document.navform.submit();
197}
198
199function closeMain() {
200 if (document.all) {
201 document.cookie = "SCORMpopup=" + escape(",top="+top.main.screenTop+",left="+top.main.screenLeft);
202 } else {
203 document.cookie = "SCORMpopup=" + escape(",top="+top.main.screenY+",left="+top.main.screenX);
204 }
205 top.main.close();
206}