2 /// This page prints a hotpot quiz
3 if (defined('HOTPOT_FIRST_ATTEMPT') && HOTPOT_FIRST_ATTEMPT
==false) {
4 // this script is being included (by attempt.php)
6 // this script is being called directly from the browser
7 define('HOTPOT_FIRST_ATTEMPT', true);
8 require_once("../../config.php");
9 require_once("lib.php");
11 $id = optional_param('id', 0, PARAM_INT
); // Course Module ID, or
12 $hp = optional_param('hp', 0, PARAM_INT
); // hotpot ID
15 if (! $cm = get_coursemodule_from_id('hotpot', $id)) {
16 error("Course Module ID was incorrect");
18 if (! $course = get_record("course", "id", $cm->course
)) {
19 error("Course is misconfigured");
21 if (! $hotpot = get_record("hotpot", "id", $cm->instance
)) {
22 error("Course module is incorrect");
26 if (! $hotpot = get_record("hotpot", "id", $hp)) {
27 error("Course module is incorrect");
29 if (! $course = get_record("course", "id", $hotpot->course
)) {
30 error("Course is misconfigured");
32 if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id
, $course->id
)) {
33 error("Course Module ID was incorrect");
37 // make sure this user is enrolled in this course and can access this HotPot
38 require_login($course);
39 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
40 require_capability('mod/hotpot:attempt', $context, $USER->id
);
42 // set nextpage (for error messages)
43 $nextpage = "$CFG->wwwroot/course/view.php?id=$course->id";
45 $title = format_string($course->shortname
.': '.$hotpot->name
, true);
46 $heading = $course->fullname
;
48 $navigation = build_navigation('', $cm);
50 $button = update_module_button($cm->id
, $course->id
, get_string("modulename", "hotpot"));
51 $button = '<div style="font-size:0.75em;">'.$button.'</div>';
52 $loggedinas = '<span class="logininfo">'.user_login_string($course, $USER).'</span>';
54 $hppassword = optional_param('hppassword', '');
55 if (HOTPOT_FIRST_ATTEMPT
&& !has_capability('mod/hotpot:grade', $context)) {
56 // check this quiz is available to this student
57 // error message, if quiz is unavailable
59 // check quiz is visible
60 if (!hotpot_is_visible($cm)) {
61 $error = get_string("activityiscurrentlyhidden");
62 // check network address
63 } else if ($hotpot->subnet
&& !address_in_subnet(getremoteaddr(), $hotpot->subnet
)) {
64 $error = get_string("subneterror", "quiz");
65 // check number of attempts
66 } else if ($hotpot->attempts
&& $hotpot->attempts
<= count_records_select('hotpot_attempts', 'hotpot='.$hotpot->id
.' AND userid='.$USER->id
, 'COUNT(DISTINCT clickreportid)')) {
67 $error = get_string("nomoreattempts", "quiz");
69 } else if ($hotpot->password
&& empty($hppassword)) {
70 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
71 print_heading($hotpot->name
);
74 if (trim(strip_tags($hotpot->summary
))) {
75 print_simple_box_start($boxalign, $boxwidth);
76 print '<div class="mdl-align">'.format_text($hotpot->summary
)."</div>\n";
77 print_simple_box_end();
80 print '<form id="passwordform" method="post" action="view.php?id='.$cm->id
.'">'."\n";
81 print_simple_box_start($boxalign, $boxwidth);
82 print '<div class="mdl-align">';
83 print get_string('requirepasswordmessage', 'quiz').'<br /><br />';
84 print '<b>'.get_string('password').':</b> ';
85 print '<input name="hppassword" type="password" value="" /> ';
86 print '<input type="submit" value="'.get_string("ok").'" /> ';
88 print_simple_box_end();
93 } else if ($hotpot->password
&& strcmp($hotpot->password
, $hppassword)) {
94 $error = get_string("passworderror", "quiz");
95 $nextpage = "view.php?id=$cm->id";
97 } else if ($hotpot->timeopen
&& $hotpot->timeopen
> $time) {
98 $error = get_string("quiznotavailable", "quiz", userdate($hotpot->timeopen
))."<br />\n";
99 // check quiz is not closed
100 } else if ($hotpot->timeclose
&& $hotpot->timeclose
< $time) {
101 $error = get_string("quizclosed", "quiz", userdate($hotpot->timeclose
))."<br />\n";
104 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
105 notice($error, $nextpage);
107 // script stops here, if quiz is unavailable to student
112 if (!empty($hotpot->timeclose
) && $hotpot->timeclose
> $time) {
113 // quiz is available until 'timeclose'
114 $available_msg = get_string("quizavailable", "quiz", userdate($hotpot->timeclose
))."<br />\n";
116 // open and parse the source file
117 if(!$hp = new hotpot_xml_quiz($hotpot)) {
118 error("Quiz is unavailable at the moment");
120 $get_js = optional_param('js', '', PARAM_ALPHA
);
121 $get_css = optional_param('css', '', PARAM_ALPHA
);
122 $framename = optional_param('framename', '', PARAM_ALPHA
);
123 // look for <frameset> (HP5 v5)
126 if (preg_match_all('|<frameset([^>]*)>(.*?)</frameset>|is', $hp->html
, $matches)) {
127 $last = count($matches[0])-1;
128 $frameset = $matches[2][$last];
129 $frameset_tags = $matches[1][$last];
131 // if HTML is being requested ...
132 if (empty($get_js) && empty($get_css)) {
133 if (empty($frameset)) {
135 if ($hotpot->navigation
==HOTPOT_NAVIGATION_FRAME ||
$hotpot->navigation
==HOTPOT_NAVIGATION_IFRAME
) {
136 $get_html = ($framename=='main') ?
true : false;
142 $get_html = empty($framename) ?
true : false;
147 if (HOTPOT_FIRST_ATTEMPT
) {
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 $hp->adjust_media_urls();
156 if (empty($frameset)) {
159 switch ($hotpot->navigation
) {
160 case HOTPOT_NAVIGATION_BUTTONS
:
161 // do nothing (i.e. leave buttons as they are)
163 case HOTPOT_NAVIGATION_GIVEUP
:
164 $hp->insert_giveup_form($attemptid, '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->');
166 case HOTPOT_NAVIGATION_FRAME
:
167 case HOTPOT_NAVIGATION_IFRAME
:
168 if (empty($CFG->framename
)) {
169 $targetframe = '_top';
171 $targetframe = $CFG->framename
;
173 if ($pos = strpos($hp->html
, '</body>')) {
175 .'<script type="text/javascript">'."\n"
177 ."var obj = document.getElementsByTagName('a');\n"
179 ." var i_max = obj.length;\n"
180 ." for (var i=0; i<i_max; i++) {\n"
181 ." if (obj[i].href && ! obj[i].target) {\n"
182 ." obj[i].target = '$targetframe';\n"
185 ." var obj = null;\n"
187 ."var obj = document.getElementsByTagName('form');\n"
189 ." var i_max = obj.length;\n"
190 ." for (var i=0; i<i_max; i++) {\n"
191 ." if (obj[i].action && ! obj[i].target) {\n"
192 ." obj[i].target = '$targetframe';\n"
195 ." var obj = null;\n"
200 $hp->html
= substr_replace($hp->html
, $insert, $pos, 0);
202 $hp->remove_nav_buttons();
205 $hp->remove_nav_buttons();
207 if (isset($hp->real_outputformat
) && $hp->real_outputformat
==HOTPOT_OUTPUTFORMAT_MOBILE
) {
208 $hp->insert_submission_form($attemptid, '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->', true);
210 $hp->insert_submission_form($attemptid, '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->', false, $targetframe);
214 switch ($hotpot->navigation
) {
215 case HOTPOT_NAVIGATION_BUTTONS
:
216 // convert URLs in nav buttons
218 case HOTPOT_NAVIGATION_GIVEUP
:
219 // $hp->insert_giveup_form($attemptid, '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->');
222 // remove navigation buttons
223 $hp->html
= preg_replace('#NavBar\+=(.*);#', '', $hp->html
);
225 $hp->insert_submission_form($attemptid, "var NavBar='", "';");
229 //FEEDBACK = new Array();
230 //FEEDBACK[0] = ''; // url of feedback page/script
231 //FEEDBACK[1] = ''; // array of array('teachername', 'value');
232 //FEEDBACK[2] = ''; // 'student name' [formmail only]
233 //FEEDBACK[3] = ''; // 'student email' [formmail only]
234 //FEEDBACK[4] = ''; // window width
235 //FEEDBACK[5] = ''; // window height
236 //FEEDBACK[6] = ''; // 'Send a message to teacher' [prompt/button text]
237 //FEEDBACK[7] = ''; // 'Title'
238 //FEEDBACK[8] = ''; // 'Teacher'
239 //FEEDBACK[9] = ''; // 'Message'
240 //FEEDBACK[10] = ''; // 'Close this window'
242 switch ($hotpot->studentfeedback
) {
243 case HOTPOT_FEEDBACK_NONE
:
246 case HOTPOT_FEEDBACK_WEBPAGE
:
247 if (empty($hotpot->studentfeedbackurl
)) {
248 $hotpot->studentfeedback
= HOTPOT_FEEDBACK_NONE
;
250 $feedback[0] = "'$hotpot->studentfeedbackurl'";
253 case HOTPOT_FEEDBACK_FORMMAIL
:
254 $teachers = hotpot_feedback_teachers($course, $hotpot);
255 if (empty($teachers) ||
empty($hotpot->studentfeedbackurl
)) {
256 $hotpot->studentfeedback
= HOTPOT_FEEDBACK_NONE
;
258 $feedback[0] = "'$hotpot->studentfeedbackurl'";
259 $feedback[1] = $teachers;
260 $feedback[2] = "'".fullname($USER)."'";
261 $feedback[3] = "'".$USER->email
."'";
262 $feedback[4] = 500; // width
263 $feedback[5] = 300; // height
266 case HOTPOT_FEEDBACK_MOODLEFORUM
:
267 $module = get_record('modules', 'name', 'forum');
268 $forums = get_records('forum', 'course', "$course->id");
269 if (empty($module) ||
empty($module->visible
) ||
empty($forums)) {
270 $hotpot->studentfeedback
= HOTPOT_FEEDBACK_NONE
;
272 $feedback[0] = "'$CFG->wwwroot/mod/forum/index.php?id=$course->id'";
275 case HOTPOT_FEEDBACK_MOODLEMESSAGING
:
276 $teachers = hotpot_feedback_teachers($course, $hotpot);
277 if (empty($CFG->messaging
) ||
empty($teachers)) {
278 $hotpot->studentfeedback
= HOTPOT_FEEDBACK_NONE
;
280 $feedback[0] = "'$CFG->wwwroot/message/discussion.php?id='";
281 $feedback[1] = $teachers;
282 $feedback[4] = 400; // width
283 $feedback[5] = 500; // height
289 if ($hotpot->studentfeedback
!= HOTPOT_FEEDBACK_NONE
) {
290 $feedback[6] = "'Send a message to teacher'";
291 $feedback[7] = "'Title'";
292 $feedback[8] = "'Teacher'";
293 $feedback[9] = "'Message'";
294 $feedback[10] = "'Close this window'";
296 foreach ($feedback as $i=>$str) {
297 $js .= 'FEEDBACK['.$i."] = $str;\n";
299 $js = '<script type="text/javascript">'."\n//<![CDATA[\n"."FEEDBACK = new Array();\n".$js."//]]>\n</script>\n";
300 $hp->html
= preg_replace('|</head>|i', "$js</head>", $hp->html
, 1);
302 // insert hot-potatoes.js
303 $hp->insert_script(HOTPOT_JS
);
304 // get Moodle pageid and pageclass
307 if (function_exists('page_id_and_class')) {
308 page_id_and_class($pageid, $pageclass);
310 // extract first <head> tag
312 $pattern = '|<head([^>]*)>(.*?)</head>|is';
313 if (preg_match($pattern, $hp->html
, $matches)) {
316 $head = preg_replace('|<title[^>]*>(.*?)</title>|is', '', $head);
318 // extract <style> tags (and remove from $head)
320 $pattern = '|<style([^>]*)>(.*?)</style>|is';
321 if (preg_match_all($pattern, $head, $matches)) {
322 $count = count($matches[0]);
323 for ($i=0; $i<$count; $i++
) {
325 $styles .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
327 $head = str_replace($matches[0][$i], '', $head);
330 // extract <script> tags (and remove from $head)
332 $pattern = '|<script([^>]*)>(.*?)</script>|is';
333 if (preg_match_all($pattern, $head, $matches)) {
334 $count = count($matches[0]);
335 for ($i=0; $i<$count; $i++
) {
337 $scripts .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
339 $head = str_replace($matches[0][$i], '', $head);
342 // extract <body> tags
346 // HP6 and some HP5 (v6 and v4)
347 if (preg_match('|<body'.'([^>]*'.'onLoad=(["\'])(.*?)(\\2)'.'[^>]*)'.'>(.*)</body>|is', $hp->html
, $matches)) {
348 $body = $matches[5]; // contents of first <body onload="StartUp()">...</body> block
350 $body_tags = str_replace(' id="TheBody"', '', $matches[1]);
352 // workaround to ensure javascript onload routine for quiz is always executed
353 // $body_tags will only be inserted into the <body ...> tag
354 // if it is included in the theme/$CFG->theme/header.html,
355 // so some old or modified themes may not insert $body_tags
357 . '<script type="text/javascript">'."\n"
359 . " var s = (typeof(window.onload)=='function') ? onload.toString() : '';\n"
360 . " if (s.indexOf('".$matches[3]."')<0) {\n"
361 . " if (s=='') {\n" // no previous onload
362 . " window.onload = new Function('".$matches[3]."');\n"
364 . " window.onload_hotpot = onload;\n"
365 . " window.onload = new Function('window.onload_hotpot();'+'".$matches[3]."');\n"
371 $footer = '</body>'.$footer;
372 } else if ($frameset) { // HP5 v5
373 switch ($framename) {
375 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas);
379 // add a HotPot navigation frame at the top of the page
380 //$rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
381 //$frameset = "\n\t".'<frame src="view.php?id='.$cm->id.'&framename=top" frameborder="0" name="top"></frame>'.$frameset;
382 //$frameset_tags = preg_replace('|rows="(.*?)"|', 'rows="'.$rows.',\\1"', $frameset_tags);
383 // put navigation into var NavBar='';
384 // add form to TopFrame in "WriteFeedback" function
385 // OR add form to BottomFrame in "DisplayExercise" function
386 // submission form: '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->'
387 // give up form: '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->'
389 print "<head>\n<title>$title</title>\n$styles\n$scripts</head>\n";
390 print "<frameset$frameset_tags>$frameset</frameset>\n";
393 } // end switch $framename
395 // other files (maybe not even a HotPots)
396 } else if (preg_match('|<body'.'([^>]*)'.'>(.*)</body>|is', $hp->html
, $matches)) {
398 $body_tags = $matches[1];
400 // print the quiz to the browser
409 // closing tags for "page" and "content" divs
410 $footer = '</div></div>'.$footer;
411 switch ($hotpot->navigation
) {
412 case HOTPOT_NAVIGATION_BAR
:
413 //update_module_button($cm->id, $course->id, $strmodulename.'" style="font-size:0.8em')
415 $title, $heading, $navigation, "", $head.$styles.$scripts, true, $button, $loggedinas, false, $body_tags
417 if (!empty($available_msg)) {
418 notify($available_msg);
422 case HOTPOT_NAVIGATION_FRAME
:
423 switch ($framename) {
425 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas);
429 if (!empty($available_msg)) {
430 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
435 $txtframesetinfo = get_string('framesetinfo');
436 $txttoptitle = get_string('navigation', 'hotpot');
437 $txtmaintitle = get_string('modulename', 'hotpot');
439 $rows = empty($CFG->resource_framesize
) ?
85 : $CFG->resource_framesize
;
441 @header
('Content-Type: text/html; charset=utf-8');
442 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">\n";
444 print "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
445 print "<head><title>$title</title></head>\n";
446 print "<frameset rows=$rows,*>\n";
447 print "<frame title=\"$txttoptitle\" src=\"view.php?id=$cm->id&framename=top\">\n";
448 print "<frame title=\"$txtmaintitle\" src=\"view.php?id=$cm->id&framename=main\">\n";
449 print "<noframes>\n";
450 print "<p>$txtframesetinfo</p>\n";
451 print "<ul><li><a href=\"view.php?id=$cm->id&framename=top\">$txttoptitle</a></li>\n";
452 print "<li><a href=\"view.php?id=$cm->id&framename=main\">$txtmaintitle</a></li></ul>\n";
453 print "</noframes>\n";
454 print "</frameset>\n";
457 } // end switch $framename
459 case HOTPOT_NAVIGATION_IFRAME
:
460 switch ($framename) {
465 // set iframe attributes
466 $iframe_id = 'hotpot_embed_object';
467 $iframe_name = 'hotpot_embed_iframe';
468 $iframe_width = '100%';
469 $iframe_height = '100%';
470 $iframe_src = $CFG->wwwroot
.'/mod/hotpot/view.php?id='.$cm->id
.'&framename=main';
471 $iframe_onload_function = 'set_embed_object_height';
472 $iframe_js = '<script src="'.$CFG->wwwroot
.'/mod/hotpot/iframe.js" type="text/javascript"></script>'."\n";
475 $title, $heading, $navigation,
476 "", $head.$styles.$scripts.$iframe_js, true, $button,
479 if (!empty($available_msg)) {
480 notify($available_msg);
483 // for XHTML 1.0 Strict compatability, the embedded page should be implemented
484 // using an <object> not an <iframe>. However, IE <object>'s are problematic
485 // (links and forms cannot escape), so we use conditional comments to display
486 // an <iframe> in IE and an <object> in other browsers
488 // print the html element to hold the embedded html page
489 // Note: the iframe in IE needs a "name" attribute for the resizing to work
490 print '<!--[if IE]>'."\n";
491 print '<iframe name="'.$iframe_name.'" id="'.$iframe_id.'" src="'.$iframe_src.'" width="'.$iframe_width.'" height="'.$iframe_height.'"></iframe>'."\n";
492 print '<![endif]-->'."\n";
493 print '<!--[if !IE]> <-->'."\n";
494 print '<object id="'.$iframe_id.'" type="text/html" data="'.$iframe_src.'" width="'.$iframe_width.'" height="'.$iframe_height.'"></object>'."\n";
495 print '<!--> <![endif]-->'."\n";
497 // print javascript to add onload event handler - we do this here because
498 // an object tag should have no onload attribute in XHTML 1.0 Strict
499 print '<script type="text/javascript">'."\n";
500 print '//<![CDATA['."\n";
501 print "var obj = document.getElementById('$iframe_id');\n";
502 print "if (obj) {\n";
503 print " if (obj.addEventListener) {\n";
504 print " obj.addEventListener('load', $iframe_onload_function, false);\n";
505 print " } else if (obj.attachEvent) {\n";
506 print " obj.attachEvent('onload', $iframe_onload_function);\n";
508 print " obj['onload'] = $iframe_onload_function;\n";
511 print "obj = null;\n";
513 print '</script>'."\n";
516 } // end switch $framename
518 case HOTPOT_NAVIGATION_GIVEUP
:
519 // replace charset , if necessary
520 // HotPots are plain ascii (iso-8859-1) with unicode chars encoded as HTML entities
521 $charset = get_string("thischarset");
522 if ($charset == 'iso-8859-1') {
525 $hp->html
= preg_replace(
526 '|<meta[^>]*charset=iso-8859-1[^>]*>|is',
527 '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />',
531 // no break (continue to print html to browser)
533 // HOTPOT_NAVIGATION_BUTTONS
534 // HOTPOT_NAVIGATION_NONE
535 if (!empty($available_msg)) {
536 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
540 ///////////////////////////////////
542 ///////////////////////////////////
543 function hotpot_feedback_teachers(&$course, &$hotpot) {
545 $teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE
, $course->id
), 'mod/hotpot:grade');
546 $teacherdetails = '';
547 if (!empty($teachers)) {
549 foreach ($teachers as $teacher) {
550 if ($hotpot->studentfeedback
==HOTPOT_FEEDBACK_MOODLEMESSAGING
) {
551 $detail = $teacher->id
;
553 $detail =$teacher->email
;
555 $details[] = "new Array('".fullname($teacher)."', '$detail')";
557 $teacherdetails = 'new Array('.implode(',', $details).");\n";
559 return $teacherdetails;