Added multiorg display and patched some typo
[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 (!empty($_POST['currentorg'])) {
62         $currentorg = $_POST['currentorg'];
63     }
64     if (($scorm->popup != "") && (!empty($_POST["mode"])))
65         $mode = $_POST["mode"];
66     if (($scorm->popup == "") && (!empty($_GET["mode"])))
67         $mode = $_GET["mode"];
68     
69     if (($frameset == "top") || ($scorm->popup != "")) {
70         add_to_log($course->id, "scorm", "view", "playscorm.php?id=$cm->id", "$scorm->id");
71         //
72         // Print the page header
73         //
74         $bodyscripts = "";
75         if ($scorm->popup != "") {
76             $bodyscripts = "onLoad='SCOInitialize();' onUnload='API.SaveTotalTime(); closeMain();' onbeforeUnload='API.SaveTotalTime();'";
77         }
78         print_header($pagetitle, "$course->fullname",
79                 "$navigation <a target=\"{$CFG->framename}\" href=\"view.php?id=$cm->id\" title=\"$scorm->summary\">$scorm->name</a>",
80                 "", "", true, update_module_button($cm->id, $course->id, $strscorm), navmenu($course, $cm, '_top'),"",$bodyscripts);
81         
82         echo "<table width=\"100%\">\n    <tr><td align=\"center\">".text_to_html($scorm->summary, true, false)."</td>\n";
83         if ($mode == "browse")
84             echo "<td align=\"right\" width=\"10%\" nowrap>".get_string("browsemode","scorm")."</td>\n";
85         echo "     </tr>\n</table>\n";
86         
87         if ($scorm->popup != "") {
88             echo "<script id=\"scormAPI\" language=\"JavaScript\" type=\"text/javascript\" src=\"scormAPI.php?id=$cm->id&mode=".$mode.$scoid."\"></script>\n";
89             $currentSCO = "";
90             if (!empty($_POST['scoid']))
91                 $currentSCO = $_POST['scoid'];
92         ?>
93             <br />
94             <script language="Javascript">
95             <!--
96                 function playSCO(scoid) {
97                     document.navform.scoid.value=scoid;
98                     document.navform.submit();
99                 }
100                 
101                 function expandCollide(which,list) {
102                     var nn=document.ids?true:false
103                     var w3c=document.getElementById?true:false
104                     var beg=nn?"document.ids.":w3c?"document.getElementById(":"document.all.";
105                     var mid=w3c?").style":".style";
106         
107                     if (eval(beg+list+mid+".display") != "none") {
108                         which.src = "pix/plus.gif";
109                         eval(beg+list+mid+".display='none';");
110                     } else {
111                         which.src = "pix/minus.gif";
112                         eval(beg+list+mid+".display='block';");
113                     }
114         
115                 }
116             -->
117             </script>
118             <style type="text/css">
119                 .scormlist { list-style-type:none; }
120             </style>
121         <?php
122             print_simple_box_start("CENTER");
123             echo "<table>\n";
124             echo "  <tr><th>".get_string("coursestruct","scorm")."</th></tr>\n";
125             echo "  <tr><td nowrap>\n<ul class=\"scormlist\"'>\n";
126             $incomplete = false;
127             if ($scoes = get_records_select("scorm_scoes","scorm='$scorm->id' AND organization='$currentorg' order by id ASC")){
128                 $level=0;
129                 $sublist=0;
130                 $parents[$level]="/";
131                 foreach ($scoes as $sco) {
132                     if ($parents[$level]!=$sco->parent) {
133                         if ($level>0 && $parents[$level-1]==$sco->parent) {
134                             echo "  </ul></li>\n";
135                             $level--;
136                         } else {
137                             $i = $level;
138                             $closelist = "";
139                             while (($i > 0) && ($parents[$level] != $sco->parent)) {
140                                 $closelist .= "  </ul></li>\n";
141                                 $i--;
142                             }
143                             if (($i == 0) && ($sco->parent != $currentorg)) {
144                                 echo "  <li><ul id='".$sublist."' class=\"scormlist\"'>\n";
145                                 $level++;
146                             } else {
147                                 echo $closelist;
148                                 $level = $i;
149                             }
150                             $parents[$level]=$sco->parent;
151                         }
152                     }
153                     echo "    <li>\n";
154                     $nextsco = next($scoes);
155                     if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
156                         $sublist++;
157                         echo "      <img src=\"pix/minus.gif\" onClick='expandCollide(this,".$sublist.");'/>\n";
158                     } else {
159                         echo "      <img src=\"pix/spacer.gif\" />\n";
160                     }
161                     
162                     if ($sco->launch) {
163                         $startbold = '';
164                         $endbold = '';
165                         if ($sco->id == $currentSCO) {
166                             $startbold = '-> <b>';
167                             $endbold = '</b> <-';
168                         }
169                         if (($currentSCO == "") && ($mode != "normal")) {
170                             $currentSCO = $sco->id;
171                             $startbold = '-> <b>';
172                             $endbold = '</b> <-';
173                         }
174                         $score = "";
175                         if ($sco_user=get_record("scorm_sco_users","scoid",$sco->id,"userid",$USER->id)) {
176                             if ( $sco_user->cmi_core_lesson_status == "")
177                                 $sco_user->cmi_core_lesson_status = "not attempted";
178                             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";
179                             if (($sco_user->cmi_core_lesson_status == "not attempted") || ($sco_user->cmi_core_lesson_status == "incomplete")) {
180                                 if ($currentSCO == "") {
181                                     $incomplete = true;
182                                     $currentSCO = $sco->id;
183                                     $startbold = '-> <b>';
184                                     $endbold = '</b> <-';
185                                 }
186                             }
187                             if ($sco_user->cmi_core_score_raw > 0) {
188                                 $score = "(".get_string("score","scorm").":&nbsp;".$sco_user->cmi_core_score_raw.")";
189                             }
190                         } else {
191                             echo "      <img src=\"pix/notattempted.gif\" alt=\"".get_string("notattempted","scorm")."\" />";
192                             $incomplete = true;
193                         }
194                         echo "      &nbsp;$startbold<a href=\"javascript:playSCO(".$sco->id.");\">$sco->title</a> $score$endbold\n    </li>\n";
195                     } else {
196                         echo "      &nbsp;$sco->title\n    </li>\n";
197                     }
198                 }
199                 for ($i=0;$i<$level;$i++){
200                     echo "  </ul></li>\n";
201                 }
202             }
203             echo "</ul></td></tr>\n";
204             echo "</table>\n";
205             print_simple_box_end();
206             
207         }
208             
209         echo "<table width=\"100%\">\n    <tr>\n";
210         echo "          <td align=\"center\" nowrap>
211                      <iframe name=\"cmi\" width=\"1\" height=\"1\" src=\"cmi.php?id=$cm->id\" style=\"visibility: hidden\"></iframe>
212                      <form name=\"navform\" method=\"POST\" action=\"playscorm.php?id=$cm->id\" target=\"_top\">
213                         <input name=\"scoid\" type=\"hidden\" />
214                         <input name=\"currentorg\" type=\"hidden\" value=\"$currentorg\" />
215                         <input name=\"mode\" type=\"hidden\" value=\"".$mode."\" />
216                         <input name=\"prev\" type=\"button\" value=\"".get_string("prev","scorm")."\" onClick=\"top.changeSco('previous');\" />&nbsp;\n";
217                         
218         if ($scorm->popup == "") {
219             $currentorg = '';
220             if (isset($_GET['currentorg'])) {
221                 $currentorg = $_GET['currentorg'];
222             }
223             if ($scoes = get_records_select("scorm_scoes","scorm='$scorm->id' AND organization='$currentorg' order by id ASC")){
224                 $level=0;                       
225                 $parents[$level]="/";
226                 foreach ($scoes as $sco) {
227                     if ($parents[$level]!=$sco->parent) {
228                         if ($level>0 && $parents[$level-1]==$sco->parent) {
229                             $level--;
230                         } else {
231                             $i = $level;
232                             while (($i > 0) && ($parents[$level] != $sco->parent)) {
233                                 $i--;
234                             }
235                             if (($i == 0) && ($sco->parent != $currentorg)) {
236                                 $level++;
237                             } else {
238                                 $level = $i;
239                             }
240                             $parents[$level]=$sco->parent;
241                         }
242                     }
243                     $indenting = "";
244                     for ($i=0;$i<$level;$i++) {
245                         $indenting .= "-";
246                     }
247                     $options[$sco->id] = $indenting."&gt; ".$sco->title;
248                 }
249             }
250             choose_from_menu($options, "courseStructure", "", "", "document.navform.scoid.value=document.navform.courseStructure.options[document.navform.courseStructure.selectedIndex].value;document.navform.submit();");
251         }
252         echo "          &nbsp;<input name=\"next\" type=\"button\" value=\"".get_string("next","scorm")."\" onClick=\"top.changeSco('continue')\" />\n";
253         echo "       </form>
254                 </td>\n";
255         
256         echo "</tr>\n</table>\n";
257         if ($scorm->popup == "") {
258             echo "</body>\n</html>\n";
259         } else {
260             print_footer($course);
261         }
262     } else {
263         if ($scorm->popup == "") {
264             // 
265             // Frameset
266             //
267             echo "<html>\n";
268             echo "<head><title>$course->shortname: $scorm->name</title></head>\n";
269             echo "<script id=\"scormAPI\" language=\"JavaScript\" type=\"text/javascript\" src=\"scormAPI.php?id=$cm->id&mode=".$mode.$scoid."\"></script>\n";
270             echo "<frameset rows=\"$CFG->scorm_framesize,*\" onLoad=\"SCOInitialize();\" onUnload=\"API.SaveTotalTime();\" onbeforeUnload=\"API.SaveTotalTime();\">\n";
271             echo "\t    <frame name=\"navigation\" src=\"playscorm.php?id=$cm->id&mode=".$mode.'&currentorg='.$currentorg."&frameset=top\">\n";
272             echo "\t    <frame name=\"main\" src=\"\">\n";
273             echo "</frameset>\n";
274             echo "</html>\n";
275         }
276     }
277 ?>