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
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");
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
);
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>';
56 if (!isteacher($course->id
)) {
57 // check this quiz is available to this student
59 // error message, if quiz is unavailable
62 // check quiz is 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");
75 } else if ($hotpot->password
&& empty($_POST['hppassword'])) {
77 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
78 print_heading($hotpot->name
);
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();
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").'"> ';
100 print_simple_box_end();
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";
121 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
122 notice($error, $nextpage);
124 // script stops here, if quiz is unavailable to student
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
174 $pattern = '|^(.*)<head([^>]*)>(.*?)</head>|is';
175 if (preg_match($pattern, $hp->html
, $matches)) {
179 // extract <style> tags
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
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
204 $pattern = '|^(.*)<body([^>]*)>(.*?)</body>|is';
205 if (preg_match($pattern, $hp->html
, $matches)) {
206 $bodytags = $matches[2];
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)) {
215 .'<SCRIPT type="text/javascript">'."\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"
222 ." window.onload_hotpot = onload;\n"
223 ." window.onload = new Function('window.onload_hotpot();' + '".$matches[2]."');\n"
232 $footer = '</body></html>';
234 // print the quiz to the browser
236 if (isset($_GET['js'])) {
240 if (isset($_GET['css'])) {
245 switch ($hotpot->navigation
) {
247 case HOTPOT_NAVIGATION_BAR
:
248 //update_module_button($cm->id, $course->id, $strmodulename.'" style="font-size:0.8em')
250 $title, $heading, $navigation,
251 "", $head.$styles.$scripts, true, $button,
252 $loggedinas, false, $bodytags
254 if (!empty($available_msg)) {
255 notify($available_msg);
260 case HOTPOT_NAVIGATION_FRAME
:
265 $title, $heading, $navigation,
266 "", "", true, $button,
273 if (!empty($available_msg)) {
274 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
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";
288 } // end switch $frameset
291 case HOTPOT_NAVIGATION_IFRAME
:
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";
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%\">";
318 } // end switch $frameset
322 // HOTPOT_NAVIGATION_BUTTONS
323 // HOTPOT_NAVIGATION_GIVEUP
324 // HOTPOT_NAVIGATION_NONE
326 if (!empty($available_msg)) {
327 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);