Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / hotpot / view.php
blob4a906f1fa84272537fedc3ddb3132a5d40e3650f
1 <?PHP
2 // $Id$
4 /// This page prints a hotpot quiz
6 require_once("../../config.php");
7 require_once("lib.php");
9 $id = optional_param("id"); // Course Module ID, or
10 $hp = optional_param("hp"); // hotpot ID
12 if ($id) {
13 if (! $cm = get_record("course_modules", "id", $id)) {
14 error("Course Module ID was incorrect");
16 if (! $course = get_record("course", "id", $cm->course)) {
17 error("Course is misconfigured");
19 if (! $hotpot = get_record("hotpot", "id", $cm->instance)) {
20 error("Course module is incorrect");
23 } else {
24 if (! $hotpot = get_record("hotpot", "id", $hp)) {
25 error("Course module is incorrect");
27 if (! $course = get_record("course", "id", $hotpot->course)) {
28 error("Course is misconfigured");
30 if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
31 error("Course Module ID was incorrect");
35 // set nextpage (for error messages)
36 $nextpage = "$CFG->wwwroot/course/view.php?id=$course->id";
38 require_login($course->id);
40 // header strings
41 $title = strip_tags($course->shortname.': '.$hotpot->name);
42 $heading = "$course->fullname";
44 $target = empty($CFG->framename) ? '' : ' target="'.$CFG->framename.'"';
45 $navigation = '<a'.$target.' href="'.$CFG->wwwroot.'/mod/hotpot/index.php?id='.$course->id.'">'.get_string("modulenameplural", "hotpot")."</a> -> $hotpot->name";
46 if ($course->category) {
47 $navigation = '<a'.$target.' href="'.$CFG->wwwroot.'/course/view.php?id='.$course->id.'">'.$course->shortname.'</a> -> '.$navigation;
50 $button = update_module_button($cm->id, $course->id, get_string("modulename", "hotpot").'" style="font-size:0.75em;');
51 $loggedinas = '<span class="logininfo">'.user_login_string($course, $USER).'</span>';
54 $time = time();
56 if (!isteacher($course->id)) {
57 // check this quiz is available to this student
59 // error message, if quiz is unavailable
60 $error = '';
62 // check quiz is visible
63 if (!$cm->visible) {
64 $error = get_string("activityiscurrentlyhidden");
66 // check network address
67 } else if ($hotpot->subnet && !address_in_subnet($_SERVER['REMOTE_ADDR'], $hotpot->subnet)) {
68 $error = get_string("subneterror", "quiz");
70 // check number of attempts
71 } else if ($hotpot->attempts && $hotpot->attempts <= count_records('hotpot_attempts', 'hotpot', $hotpot->id, 'userid', $USER->id)) {
72 $error = get_string("nomoreattempts", "quiz");
74 // get password
75 } else if ($hotpot->password && empty($_POST['hppassword'])) {
77 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
78 print_heading($hotpot->name);
80 $boxalign = 'center';
81 $boxwidth = 500;
83 if (trim(strip_tags($hotpot->summary))) {
84 print_simple_box_start($boxalign, $boxwidth);
85 print '<div align="center">'.format_text($hotpot->summary)."</div>\n";
86 print_simple_box_end();
87 print "<br />\n";
90 print '<form name="passwordform" method="post" action="view.php?id='.$cm->id.'">'."\n";
91 print_simple_box_start($boxalign, $boxwidth);
93 print '<div align="center">';
94 print get_string('requirepasswordmessage', 'quiz').'<br /><br />';
95 print '<b>'.get_string('password').':</b> ';
96 print '<input name="hppassword" type="password" value=""> ';
97 print '<input type="submit" value="'.get_string("ok").'"> ';
98 print "</div>\n";
100 print_simple_box_end();
101 print "</form>\n";
103 print_footer();
104 exit;
106 // check password
107 } else if ($hotpot->password && strcmp($hotpot->password, $_POST['hppassword'])) {
108 $error = get_string("passworderror", "quiz");
109 $nextpage = "view.php?id=$cm->id";
111 // check quiz is open
112 } else if ($hotpot->timeopen && $hotpot->timeopen > $time) {
113 $error = get_string("quiznotavailable", "quiz", userdate($hotpot->timeopen))."<BR>\n";
115 // check quiz is not closed
116 } else if ($hotpot->timeclose && $hotpot->timeclose < $time) {
117 $error = get_string("quizclosed", "quiz", userdate($hotpot->timeclose))."<BR>\n";
120 if ($error) {
121 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
122 notice($error, $nextpage);
124 // script stops here, if quiz is unavailable to student
129 $available_msg = '';
130 if (!empty($hotpot->timeclose) && $hotpot->timeclose > $time) {
131 // quiz is available until 'timeclose'
132 $available_msg = get_string("quizavailable", "quiz", userdate($hotpot->timeclose))."<BR>\n";
135 // open and parse the source file
136 if(!$hp = new hotpot_xml_quiz($hotpot)) {
137 error("Quiz is unavailable at the moment");
140 $frameset = isset($_GET['frameset']) ? $_GET['frameset'] : '';
142 // if HTML is being requested ...
143 if (empty($_GET['js']) && empty($_GET['css'])) {
145 $n = $hotpot->navigation;
146 if (($n!=HOTPOT_NAVIGATION_FRAME && $n!=HOTPOT_NAVIGATION_IFRAME) || $frameset=='main') {
148 add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
150 $attemptid = hotpot_add_attempt($hotpot->id);
151 if (! is_numeric($attemptid)) {
152 error('Could not insert attempt record: '.$db->ErrorMsg);
155 if ($n!=HOTPOT_NAVIGATION_BUTTONS) {
156 $hp->remove_nav_buttons();
159 $hp->adjust_media_urls();
161 $hp->insert_submission_form($attemptid);
163 if ($n==HOTPOT_NAVIGATION_GIVEUP) {
164 $hp->insert_giveup_form($attemptid);
169 // insert hot-potatoes.js
170 $hp->insert_script(HOTPOT_JS);
172 // extract <head> tag
173 $head = '';
174 $pattern = '|^(.*)<head([^>]*)>(.*?)</head>|is';
175 if (preg_match($pattern, $hp->html, $matches)) {
176 $head = $matches[3];
179 // extract <style> tags
180 $styles = '';
181 $pattern = '|<style([^>]*)>(.*?)</style>|is';
182 if (preg_match_all($pattern, $head, $matches)) {
183 $count = count($matches[0]);
184 for ($i=0; $i<$count; $i++) {
185 $styles .= $matches[0][$i]."\n";
186 $head = str_replace($matches[0][$i], '', $head);
190 // extract <script> tags
191 $scripts = '';
192 $pattern = '|<script([^>]*)>(.*?)</script>|is';
193 if (preg_match_all($pattern, $head, $matches)) {
194 $count = count($matches[0]);
195 for ($i=0; $i<$count; $i++) {
196 $scripts .= $matches[0][$i]."\n";
197 $head = str_replace($matches[0][$i], '', $head);
201 // extract <body> tags
202 $body = '';
203 $bodytags = '';
204 $pattern = '|^(.*)<body([^>]*)>(.*?)</body>|is';
205 if (preg_match($pattern, $hp->html, $matches)) {
206 $bodytags = $matches[2];
207 $body = $matches[3];
209 // workaround to ensure javascript onload routine for quiz is always executed
210 // $bodytags will only be inserted into the <body ...> tag
211 // if it is included in the theme/$CFG->theme/header.html,
212 // so some old or modified themes may not insert $bodytags
213 if (preg_match('/onload=("|\')(.*?)(\\1)/i', $bodytags, $matches)) {
214 $body .= ""
215 .'<SCRIPT type="text/javascript">'."\n"
216 ."<!--\n"
217 ." var s = (typeof(window.onload)=='function') ? onload.toString() : '';\n"
218 ." if (s.indexOf('".$matches[2]."')<0) {\n"
219 ." if (s=='') {\n" // no previous onload
220 ." window.onload = new Function('".$matches[2]."');\n"
221 ." } else {\n"
222 ." window.onload_hotpot = onload;\n"
223 ." window.onload = new Function('window.onload_hotpot();' + '".$matches[2]."');\n"
224 ." }\n"
225 ." }\n"
226 ."//-->\n"
227 ."</SCRIPT>\n"
232 $footer = '</body></html>';
234 // print the quiz to the browser
236 if (isset($_GET['js'])) {
237 print($scripts);
238 exit;
240 if (isset($_GET['css'])) {
241 print($styles);
242 exit;
245 switch ($hotpot->navigation) {
247 case HOTPOT_NAVIGATION_BAR:
248 //update_module_button($cm->id, $course->id, $strmodulename.'" style="font-size:0.8em')
249 print_header(
250 $title, $heading, $navigation,
251 "", $head.$styles.$scripts, true, $button,
252 $loggedinas, false, $bodytags
254 if (!empty($available_msg)) {
255 notify($available_msg);
257 print $body.$footer;
258 break;
260 case HOTPOT_NAVIGATION_FRAME:
262 switch ($frameset) {
263 case 'top':
264 print_header(
265 $title, $heading, $navigation,
266 "", "", true, $button,
267 $loggedinas
269 print $footer;
270 break;
272 case 'main';
273 if (!empty($available_msg)) {
274 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
276 print $hp->html;
277 break;
279 default:
280 print "<HTML>\n";
281 print "<HEAD><TITLE>$title</TITLE></HEAD>\n";
282 print "<FRAMESET rows=85,*>\n";
283 print "<FRAME src=\"view.php?id=$cm->id&frameset=top\">\n";
284 print "<FRAME src=\"view.php?id=$cm->id&frameset=main\">\n";
285 print "</FRAMESET>\n";
286 print "</HTML>\n";
287 break;
288 } // end switch $frameset
289 break;
291 case HOTPOT_NAVIGATION_IFRAME:
293 switch ($frameset) {
294 case 'main';
295 print $hp->html;
296 break;
298 default:
299 $iframe_id = 'hotpot_iframe';
300 $bodytags = " onload=\"set_iframe_height('$iframe_id')\"";
302 $iframe_js = '<SCRIPT src="iframe.js" type="text/javascript" language="javascript">'."\n";
304 print_header(
305 $title, $heading, $navigation,
306 "", $head.$styles.$scripts.$iframe_js, true, $button,
307 $loggedinas, false, $bodytags
309 if (!empty($available_msg)) {
310 notify($available_msg);
312 print "<IFRAME id=\"$iframe_id\" src=\"view.php?id=$cm->id&frameset=main\" height=\"100%\" width=\"100%\">";
313 print "<ILAYER name=\"$iframe_id\" src=\"view.php?id=$cm->id&frameset=main\" height=\"100%\" width=\"100%\">";
314 print "</ILAYER>\n";
315 print "</IFRAME>\n";
316 print $footer;
317 break;
318 } // end switch $frameset
319 break;
321 default:
322 // HOTPOT_NAVIGATION_BUTTONS
323 // HOTPOT_NAVIGATION_GIVEUP
324 // HOTPOT_NAVIGATION_NONE
326 if (!empty($available_msg)) {
327 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
329 print($hp->html);