New styles added
[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) {
ec81373f 11 if (! $cm = get_record("course_modules", "id", $id)) {
12 error("Course Module ID was incorrect");
13 }
98ca59f6 14
ec81373f 15 if (! $course = get_record("course", "id", $cm->course)) {
16 error("Course is misconfigured");
17 }
98ca59f6 18
ec81373f 19 if (! $scorm = get_record("scorm", "id", $cm->instance)) {
20 error("Course module is incorrect");
21 }
98ca59f6 22
23 } else {
ec81373f 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 }
98ca59f6 33 }
34
ec81373f 35 require_login($course->id, false, $cm);
98ca59f6 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 //
ec81373f 41 if (!empty($scoid)) {
42 // Direct sco request
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 }
58 } else {
59 // Search for first incomplete sco
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 {
65 // If review mode get the first
66 if ($mode == "review") {
67 $sco = get_record("scorm_scoes","id",$sco_user->scoid);
68 break;
69 }
70 }
71 }
72 }
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 }
5d8790cb 77 } else {
78 //
79 // A new user
80 //
ec81373f 81 if ($scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC")) {
82 //
83 // Create user scoes records
84 //
85 foreach ($scoes as $sco) {
86 if (($sco->launch != "") && ($sco->type != "sca") && ($sco->type != "asset")){
87 if (!isset($first)) {
88 $first = $sco;
89 }
90 $sco_user->userid = $USER->id;
91 $sco_user->scoid = $sco->id;
92 $sco_user->scormid = $scorm->id;
93 $element = "cmi_core_lesson_status";
94 $sco_user->$element = "not attempted";
95 $ident = insert_record("scorm_sco_users",$sco_user);
96 }
97 }
98 if (isset($first)) {
99 $sco = $first;
100 }
101 if (!empty($scoid)) {
102 if ($sco = get_record("scorm_scoes","id",$scoid)) {
103 unset($first);
104 }
105 }
106 }
98ca59f6 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) {
ec81373f 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 }
98ca59f6 129 }
fc1b2663 130 $first = NULL;
131 $last = NULL;
98ca59f6 132 if ($sco->id == $min)
ec81373f 133 $first = $sco;
98ca59f6 134 if ($sco->id == $max)
ec81373f 135 $last = $sco;
98ca59f6 136
c5163bfd 137 // Get current sco User data
98ca59f6 138 $sco_user = get_record("scorm_sco_users","userid",$USER->id,"scoid",$sco->id);
ec81373f 139
98ca59f6 140 if (scorm_external_link($sco->launch)) {
ec81373f 141 $result = $sco->launch;
98ca59f6 142 } else {
ec81373f 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 }
98ca59f6 148 }
b98eebbf 149 $navObj = "top.";
d64bc8ae 150 if ($scorm->popup == "") {
ec81373f 151 $navObj = "top.navigation.";
d64bc8ae 152 }
ec81373f 153
d64bc8ae 154 if ($sco->type == 'sco') {
ec81373f 155 include("api1_2.php");
d64bc8ae 156 }
c5163bfd 157
98ca59f6 158?>
76d35423 159
5d8790cb 160function hilightcurrent(popupmenu) {
161 for (i=0;i < popupmenu.options.length;i++) {
ec81373f 162 if ( popupmenu.options[i].value == <?php echo $sco->id; ?> )
163 popupmenu.options[i].selected = true;
5d8790cb 164 }
165}
c5163bfd 166
ec81373f 167function SCOInitialize() {
c5163bfd 168<?php
169 if ( $sco->previous || $first) {
ec81373f 170 print "\t".$navObj."document.navform.prev.disabled = true;\n";
171 print "\t".$navObj."document.navform.prev.style.display = 'none';\n";
c5163bfd 172 }
173 if ( $sco->next || $last) {
ec81373f 174 print "\t".$navObj."document.navform.next.disabled = true;\n";
175 print "\t".$navObj."document.navform.next.style.display = 'none';\n";
c5163bfd 176 }
177?>
ec81373f 178<?php
179 if ($scorm->popup == "") {
180 echo "\t top.main.location=\"$result\";\n";
181 echo "\t hilightcurrent(".$navObj."document.navform.courseStructure);\n";
02f5d439 182 } else {
1a12b1f1 183 $popuplocation = '';
184 if (isset($_COOKIE["SCORMpopup"])) {
185 $popuplocation = $_COOKIE["SCORMpopup"];
186 }
ec81373f 187 echo "\t top.main = window.open('$result','main','$scorm->popup$popuplocation');\n";
02f5d439 188 }
189?>
ec81373f 190}
c5163bfd 191
d64bc8ae 192function SCOFinish(){
193 if (typeof API != "undefined") {
ec81373f 194 API.SaveTotalTime();
d64bc8ae 195 }
196}
197
c5163bfd 198function changeSco(direction) {
1a12b1f1 199 if (direction == "previous")
ec81373f 200 <?php echo $navObj ?>document.navform.scoid.value="<?php echo $prevsco; ?>";
1a12b1f1 201 else
ec81373f 202 <?php echo $navObj ?>document.navform.scoid.value="<?php echo $nextsco; ?>";
203
1a12b1f1 204 //alert ("Prev: <?php echo $prevsco; ?>\nNext: <?php echo $nextsco; ?>\nNew SCO: "+<?php echo $navObj ?>document.navform.scoid.value);
205 <?php echo $navObj ?>document.navform.submit();
206}
207
208function closeMain() {
209 if (document.all) {
ec81373f 210 document.cookie = "SCORMpopup=" + escape(",top="+top.main.screenTop+",left="+top.main.screenLeft);
1a12b1f1 211 } else {
ec81373f 212 document.cookie = "SCORMpopup=" + escape(",top="+top.main.screenY+",left="+top.main.screenX);
1a12b1f1 213 }
214 top.main.close();
215}