Updated versions modifying resource and dialogue
[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
7
8 if ($id) {
9 if (! $cm = get_record("course_modules", "id", $id)) {
10 error("Course Module ID was incorrect");
11 }
12
13 if (! $course = get_record("course", "id", $cm->course)) {
14 error("Course is misconfigured");
15 }
16
17 if (! $scorm = get_record("scorm", "id", $cm->instance)) {
18 error("Course module is incorrect");
19 }
20
21 } else {
22 if (! $scorm = get_record("scorm", "id", $a)) {
23 error("Course module is incorrect");
24 }
25 if (! $course = get_record("course", "id", $scorm->course)) {
26 error("Course is misconfigured");
27 }
28 if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
29 error("Course Module ID was incorrect");
30 }
31 }
32
33 require_login($course->id);
34
35 if ( $scoes_user = get_records_select("scorm_sco_users","userid = ".$USER->id." AND scormid = ".$scorm->id,"scoid ASC") ) {
36 if ($_GET["scoid"]) {
37 $sco = get_record("scorm_scoes","id",$_GET["scoid"]);
38 } else {
39 foreach ( $scoes_user as $sco_user ) {
40 if (($sco_user->cmi_core_lesson_status != "completed") && ($sco_user->cmi_core_lesson_status != "passed") && ($sco_user->cmi_core_lesson_status != "failed")) {
41 $sco = get_record("scorm_scoes","id",$sco_user->scoid);
42 break;
43 } else {
44 if ($_GET["mode"] == "review") {
45 $sco = get_record("scorm_scoes","id",$sco_user->scoid);
46 break;
47 }
48 }
49 }
50 }
51 if (!$sco)
52 $sco = get_record_select("scorm_scoes","scorm=".$scorm->id." AND launch<>'' order by id ASC");
53 } else {
54 if ($scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC")) {
55 foreach ($scoes as $sco) {
56 if ($sco->launch != "") {
57 if (!$first)
58 $first = $sco;
59 $sco_user->userid = $USER->id;
60 $sco_user->scoid = $sco->id;
61 $sco_user->scormid = $scorm->id;
62 $element = "cmi_core_lesson_status";
63 if ($sco->type == "sco")
64 $sco_user->$element = "not attempted";
65 else if ($sco->type == "sca")
66 $sco_user->$element = "completed";
67 $ident = insert_record("scorm_sco_users",$sco_user);
c5163bfd 68 }
98ca59f6 69 }
70 $sco = $first;
c5163bfd 71 if ($_GET["scoid"]) {
72 if ($sco = get_record("scorm_scoes","id",$_GET["scoid"]))
73 unset($first);
74 }
98ca59f6 75 }
76 }
77 //
78 // Get first, last, prev and next scoes
79 //
80 $scoes = get_records("scorm_scoes","scorm",$scorm->id,"id ASC");
81 foreach ($scoes as $fsco) {
82 if ($fsco->launch != "") {
83 if (!$min || ($min > $fsco->id))
84 $min = $fsco->id;
85 if (!$max || ($max < $fsco->id))
86 $max = $fsco->id;
87 if ((!$prevsco) || ($sco->id > $fsco->id)) {
88 $prevsco = $fsco->id;
89 }
90 if ((!$nextsco) && ($sco->id < $fsco->id)) {
91 $nextsco = $fsco->id;
92 }
93 }
94 }
95 if ($sco->id == $min)
96 $first = $sco;
97 if ($sco->id == $max)
98 $last = $sco;
99
c5163bfd 100 // Get current sco User data
98ca59f6 101 $sco_user = get_record("scorm_sco_users","userid",$USER->id,"scoid",$sco->id);
c5163bfd 102
98ca59f6 103 if (scorm_external_link($sco->launch)) {
104 $result = $sco->launch;
105 } else {
106 if ($CFG->slasharguments) {
107 $result = "$CFG->wwwroot/file.php/$scorm->course/moddata/scorm$scorm->datadir/$sco->launch";
108 } else {
109 $result = "$CFG->wwwroot/file.php?file=/$scorm->course/moddata/scorm$scorm->datadir/$sco->launch";
110 }
111 }
112 include("api1_2.php");
c5163bfd 113
98ca59f6 114?>
c5163bfd 115
116function SCOInitialize() {
117<?php
118 if ( $sco->previous || $first) {
119 print "\ttop.nav.document.navform.prev.disabled = true;\n";
120 print "\ttop.nav.document.navform.prev.style.display = 'none';\n";
121 }
122 if ( $sco->next || $last) {
123 print "\ttop.nav.document.navform.next.disabled = true;\n";
124 print "\ttop.nav.document.navform.next.style.display = 'none';\n";
125 }
126?>
127 top.main.location="<?php echo $result ?>";
128 for (i=0;i<top.nav.document.navform.courseStructure.options.length;i++) {
129 if ( top.nav.document.navform.courseStructure.options[i].value == <?php echo $sco->id; ?> )
130 top.nav.document.navform.courseStructure.options[i].selected = true;
131 }
132}
133
134function changeSco(direction) {
135 if (direction == "prev")
136 top.nav.document.navform.scoid.value="<?php echo $prevsco ?>";
137 else
138 top.nav.document.navform.scoid.value="<?php echo $nextsco ?>";
139
140 //alert ("Prev: <?php echo $prevsco ?>\nNext: <?php echo $nextsco ?>\nNew SCO: "+top.nav.document.navform.scoid.value);
141 top.nav.document.navform.submit();
142}