a2ad50b378bae25d90c5b36cfa1cfaa4a26a86e1
[moodle.git] / mod / scorm / playscorm.php
1 <?PHP  // $Id$
3 /// This page prints a particular instance of scorm
4 /// (Replace scorm with the name of your module)
6     require_once("../../config.php");
7     require_once("lib.php");
9     optional_variable($id);    // Course Module ID, or
10     optional_variable($a);     // scorm ID
11     optional_variable($frameset, "");
13     if ($id) {
14         if (! $cm = get_record("course_modules", "id", $id)) {
15             error("Course Module ID was incorrect");
16         }
17     
18         if (! $course = get_record("course", "id", $cm->course)) {
19             error("Course is misconfigured");
20         }
21     
22         if (! $scorm = get_record("scorm", "id", $cm->instance)) {
23             error("Course module is incorrect");
24         }
26     } else {
27         if (! $scorm = get_record("scorm", "id", $a)) {
28             error("Course module is incorrect");
29         }
30         if (! $course = get_record("course", "id", $scorm->course)) {
31             error("Course is misconfigured");
32         }
33         if (! $cm = get_coursemodule_from_instance("scorm", $scorm->id, $course->id)) {
34             error("Course Module ID was incorrect");
35         }
36     }
38     require_login($course->id);
40     
41     $strscorms = get_string("modulenameplural", "scorm");
42     $strscorm  = get_string("modulename", "scorm");
43         
44     if ($course->category) {
45         $navigation = "<a target=\"{$CFG->framename}\" href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
46                        <a target=\"{$CFG->framename}\" href=\"index.php?id=$course->id\">$strscorms</a> ->";
47     } else {
48         $navigation = "<a target=\"{$CFG->framename}\" href=\"index.php?id=$course->id\">$strscorms</a> ->";
49     }
51     $pagetitle = strip_tags("$course->shortname: $scorm->name");
53     if (!$cm->visible and !isteacher($course->id)) {
54         print_header($pagetitle, "$course->fullname", "$navigation $scorm->name", "", "", true, 
55                      update_module_button($cm->id, $course->id, $strscorm), navmenu($course, $cm));
56         notice(get_string("activityiscurrentlyhidden"));
57     }
58     
59     if (!empty($_POST["scoid"]))
60             $scoid = "&scoid=".$_POST["scoid"];
61     if (($scorm->popup != "") && (!empty($_POST["mode"])))
62         $mode = $_POST["mode"];
63     if (($scorm->popup == "") && (!empty($_GET["mode"])))
64         $mode = $_GET["mode"];
65     
66     if (($frameset == "top") || ($scorm->popup != "")) {
67         add_to_log($course->id, "scorm", "view", "playscorm.php?id=$cm->id", "$scorm->id");
68         //
69         // Print the page header
70         //
71         $bodyscripts = "";
72         if ($scorm->popup != "") {
73             $bodyscripts = "onLoad='SCOInitialize();' onUnload='API.SaveTotalTime(); closeMain();' onbeforeUnload='API.SaveTotalTime();'";
74         }
75         print_header($pagetitle, "$course->fullname",
76                 "$navigation <a target=\"{$CFG->framename}\" href=\"view.php?id=$cm->id\" title=\"$scorm->summary\">$scorm->name</a>",
77                 "", "", true, update_module_button($cm->id, $course->id, $strscorm), navmenu($course, $cm, '_top'),"",$bodyscripts);
78         
79         echo "<table width=\"100%\">\n    <tr><td align=\"center\">".text_to_html($scorm->summary, true, false)."</td>\n";
80         if ($mode == "browse")
81             echo "<td align=\"right\" width=\"10%\" nowrap>".get_string("browsemode","scorm")."</td>\n";
82         echo "     </tr>\n</table>\n";
83         
84         if ($scorm->popup != "") {
85             echo "<script id=\"scormAPI\" language=\"JavaScript\" type=\"text/javascript\" src=\"scormAPI.php?id=$cm->id&mode=".$mode.$scoid."\"></script>\n";
86             $currentSCO = "";
87             if (!empty($_POST['scoid']))
88                 $currentSCO = $_POST['scoid'];
89         ?>
90             <br />
91             <script language="Javascript">
92             <!--
93                 function playSCO(scoid) {
94                     document.navform.scoid.value=scoid;
95                     document.navform.submit();
96                 }
97                 
98                 function expandCollide(which,list) {
99                     var nn=document.ids?true:false
100                     var w3c=document.getElementById?true:false
101                     var beg=nn?"document.ids.":w3c?"document.getElementById(":"document.all.";
102                     var mid=w3c?").style":".style";
103         
104                     if (eval(beg+list+mid+".display") != "none") {
105                         which.src = "pix/plus.gif";
106                         eval(beg+list+mid+".display='none';");
107                     } else {
108                         which.src = "pix/minus.gif";
109                         eval(beg+list+mid+".display='block';");
110                     }
111         
112                 }
113             -->
114             </script>
115             <style type="text/css">
116                 .scormlist { list-style-type:none; }
117             </style>
118         <?php
119             print_simple_box_start("CENTER");
120             echo "<table>\n";
121             echo "  <tr><th>".get_string("coursestruct","scorm")."</th></tr>\n";
122             echo "  <tr><td nowrap>\n<ul class=\"scormlist\"'>\n";
123             $incomplete = false;
124             if ($scoes = get_records_select("scorm_scoes","scorm='$scorm->id' order by id ASC")){
125                 $level=0;
126                 $sublist=0;
127                 $parents[$level]="/";
128                 foreach ($scoes as $sco) {
129                     if ($parents[$level]!=$sco->parent) {
130                         if ($level>0 && $parents[$level-1]==$sco->parent) {
131                             echo "  </ul></li>\n";
132                             $level--;
133                         } else {
134                             $i = $level;
135                             $closelist = "";
136                             while (($i > 0) && ($parents[$level] != $sco->parent)) {
137                                 $closelist .= "  </ul></li>\n";
138                                 $i--;
139                             }
140                             if (($i == 0) && ($sco->parent != "/")) {
141                                 echo "  <li><ul id='".$sublist."' class=\"scormlist\"'>\n";
142                                 $level++;
143                             } else {
144                                 echo $closelist;
145                                 $level = $i;
146                             }
147                             $parents[$level]=$sco->parent;
148                         }
149                     }
150                     echo "    <li>\n";
151                     $nextsco = next($scoes);
152                     if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
153                         $sublist++;
154                         echo "      <img src=\"pix/minus.gif\" onClick='expandCollide(this,".$sublist.");'/>\n";
155                     } else {
156                         echo "      <img src=\"pix/spacer.gif\" />\n";
157                     }
158                     
159                     if ($sco->launch) {
160                         $startbold = '';
161                         $endbold = '';
162                         if ($sco->id == $currentSCO) {
163                             $startbold = '-> <b>';
164                             $endbold = '</b> <-';
165                         }
166                         if (($currentSCO == "") && ($mode != "normal")) {
167                             $currentSCO = $sco->id;
168                             $startbold = '-> <b>';
169                             $endbold = '</b> <-';
170                         }
171                         if ($sco_user=get_record("scorm_sco_users","scoid",$sco->id,"userid",$USER->id)) {
172                             if ( $sco_user->cmi_core_lesson_status == "")
173                                 $sco_user->cmi_core_lesson_status = "not attempted";
174                             echo "      <img src=\"pix/".scorm_remove_spaces($sco_user->cmi_core_lesson_status).".gif\" alt=\"".get_string(scorm_remove_spaces($sco_user->cmi_core_lesson_status),"scorm")."\" title=\"".get_string(scorm_remove_spaces($sco_user->cmi_core_lesson_status),"scorm")."\" />\n";
175                             if (($sco_user->cmi_core_lesson_status == "not attempted") || ($sco_user->cmi_core_lesson_status == "incomplete")) {
176                                 if ($currentSCO == "") {
177                                     $incomplete = true;
178                                     $currentSCO = $sco->id;
179                                     $startbold = '-> <b>';
180                                     $endbold = '</b> <-';
181                                 }
182                             }
183                         } else {
184                             echo "      <img src=\"pix/notattempted.gif\" alt=\"".get_string("notattempted","scorm")."\" />";
185                             $incomplete = true;
186                         }
187                         $score = "";
188                         if ($sco_user->cmi_core_score_raw > 0)
189                             $score = "(".get_string("score","scorm").":&nbsp;".$sco_user->cmi_core_score_raw.")";
190                         echo "      &nbsp;$startbold<a href=\"javascript:playSCO(".$sco->id.");\">$sco->title</a> $score$endbold\n    </li>\n";
191                     } else {
192                         echo "      &nbsp;$sco->title\n    </li>\n";
193                     }
194                 }
195                 for ($i=0;$i<$level;$i++){
196                     echo "  </ul></li>\n";
197                 }
198             }
199             echo "</ul></td></tr>\n";
200             echo "</table>\n";
201             print_simple_box_end();
202             
203         }
204             
205         echo "<table width=\"100%\">\n    <tr>\n";
206         echo "          <td align=\"center\" nowrap>
207                      <iframe name=\"cmi\" width=\"1\" height=\"1\" src=\"cmi.php?id=$cm->id\" style=\"visibility: hidden\"></iframe>
208                      <form name=\"navform\" method=\"POST\" action=\"playscorm.php?id=$cm->id\" target=\"_top\">
209                         <input name=\"scoid\" type=\"hidden\" />
210                         <input name=\"mode\" type=\"hidden\" value=\"".$mode."\" />
211                         <input name=\"prev\" type=\"button\" value=\"".get_string("prev","scorm")."\" onClick=\"top.changeSco('previous');\" />&nbsp;\n";
212                         
213         if ($scorm->popup == "") {
214             if ($scoes = get_records_select("scorm_scoes","scorm='$scorm->id' order by id ASC")){
215                 $level=0;                       
216                 $parents[$level]="/";
217                 foreach ($scoes as $sco) {
218                     if ($parents[$level]!=$sco->parent) {
219                         if ($level>0 && $parents[$level-1]==$sco->parent) {
220                             $level--;
221                         } else {
222                             $i = $level;
223                             while (($i > 0) && ($parents[$level] != $sco->parent)) {
224                                 $i--;
225                             }
226                             if (($i == 0) && ($sco->parent != "/")) {
227                                 $level++;
228                             } else {
229                                 $level = $i;
230                             }
231                             $parents[$level]=$sco->parent;
232                         }
233                     }
234                     $indenting = "";
235                     for ($i=0;$i<$level;$i++) {
236                         $indenting .= "-";
237                     }
238                     $options[$sco->id] = $indenting."&gt; ".$sco->title;
239                 }
240             }
241             choose_from_menu($options, "courseStructure", "", "", "document.navform.scoid.value=document.navform.courseStructure.options[document.navform.courseStructure.selectedIndex].value;document.navform.submit();");
242         }
243         echo "          &nbsp;<input name=\"next\" type=\"button\" value=\"".get_string("next","scorm")."\" onClick=\"top.changeSco('continue')\" />\n";
244         echo "       </form>
245                 </td>\n";
246         
247         echo "</tr>\n</table>\n";
248         echo "</body>\n</html>\n";
249     } else {
250         if ($scorm->popup == "") {
251             // 
252             // Frameset
253             //
254             echo "<html>\n";
255             echo "<head><title>$course->shortname: $scorm->name</title></head>\n";
256             echo "<script id=\"scormAPI\" language=\"JavaScript\" type=\"text/javascript\" src=\"scormAPI.php?id=$cm->id&mode=".$mode.$scoid."\"></script>\n";
257             echo "<frameset rows=\"$CFG->scorm_framesize,*\" onLoad=\"SCOInitialize();\" onUnload=\"API.SaveTotalTime();\" onbeforeUnload=\"API.SaveTotalTime();\">\n";
258             echo "\t    <frame name=\"navigation\" src=\"playscorm.php?id=$cm->id&mode=".$mode."&frameset=top\">\n";
259             echo "\t    <frame name=\"main\" src=\"\">\n";
260             echo "</frameset>\n";
261             echo "</html>\n";
262         }
263     }
264 ?>