Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / hotpot / view.php
blob6e3c0ce4fe485f17919daad2aa5c98b305026652
1 <?PHP // $Id$
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)
5 } else {
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
14 if ($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");
25 } else {
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 require_login($course);
38 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
40 // set nextpage (for error messages)
41 $nextpage = "$CFG->wwwroot/course/view.php?id=$course->id";
42 // header strings
43 $title = format_string($course->shortname.': '.$hotpot->name, true);
44 $heading = $course->fullname;
46 $navigation = build_navigation('', $cm);
48 $button = update_module_button($cm->id, $course->id, get_string("modulename", "hotpot"));
49 $button = '<div style="font-size:0.75em;">'.$button.'</div>';
50 $loggedinas = '<span class="logininfo">'.user_login_string($course, $USER).'</span>';
51 $time = time();
52 $hppassword = optional_param('hppassword', '');
53 if (HOTPOT_FIRST_ATTEMPT && !has_capability('mod/hotpot:grade', $context)) {
54 // check this quiz is available to this student
55 // error message, if quiz is unavailable
56 $error = '';
57 // check quiz is visible
58 if (!hotpot_is_visible($cm)) {
59 $error = get_string("activityiscurrentlyhidden");
60 // check network address
61 } else if ($hotpot->subnet && !address_in_subnet($_SERVER['REMOTE_ADDR'], $hotpot->subnet)) {
62 $error = get_string("subneterror", "quiz");
63 // check number of attempts
64 } else if ($hotpot->attempts && $hotpot->attempts <= count_records_select('hotpot_attempts', 'hotpot='.$hotpot->id.' AND userid='.$USER->id, 'COUNT(DISTINCT clickreportid)')) {
65 $error = get_string("nomoreattempts", "quiz");
66 // get password
67 } else if ($hotpot->password && empty($hppassword)) {
68 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
69 print_heading($hotpot->name);
70 $boxalign = 'center';
71 $boxwidth = 500;
72 if (trim(strip_tags($hotpot->summary))) {
73 print_simple_box_start($boxalign, $boxwidth);
74 print '<div align="center">'.format_text($hotpot->summary)."</div>\n";
75 print_simple_box_end();
76 print "<br />\n";
78 print '<form id="passwordform" method="post" action="view.php?id='.$cm->id.'">'."\n";
79 print_simple_box_start($boxalign, $boxwidth);
80 print '<div align="center">';
81 print get_string('requirepasswordmessage', 'quiz').'<br /><br />';
82 print '<b>'.get_string('password').':</b> ';
83 print '<input name="hppassword" type="password" value="" /> ';
84 print '<input type="submit" value="'.get_string("ok").'" /> ';
85 print "</div>\n";
86 print_simple_box_end();
87 print "</form>\n";
88 print_footer();
89 exit;
90 // check password
91 } else if ($hotpot->password && strcmp($hotpot->password, $hppassword)) {
92 $error = get_string("passworderror", "quiz");
93 $nextpage = "view.php?id=$cm->id";
94 // check quiz is open
95 } else if ($hotpot->timeopen && $hotpot->timeopen > $time) {
96 $error = get_string("quiznotavailable", "quiz", userdate($hotpot->timeopen))."<br />\n";
97 // check quiz is not closed
98 } else if ($hotpot->timeclose && $hotpot->timeclose < $time) {
99 $error = get_string("quizclosed", "quiz", userdate($hotpot->timeclose))."<br />\n";
101 if ($error) {
102 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
103 notice($error, $nextpage);
105 // script stops here, if quiz is unavailable to student
109 $available_msg = '';
110 if (!empty($hotpot->timeclose) && $hotpot->timeclose > $time) {
111 // quiz is available until 'timeclose'
112 $available_msg = get_string("quizavailable", "quiz", userdate($hotpot->timeclose))."<br />\n";
114 // open and parse the source file
115 if(!$hp = new hotpot_xml_quiz($hotpot)) {
116 error("Quiz is unavailable at the moment");
118 $get_js = optional_param('js', '', PARAM_ALPHA);
119 $get_css = optional_param('css', '', PARAM_ALPHA);
120 $framename = optional_param('framename', '', PARAM_ALPHA);
121 // look for <frameset> (HP5 v5)
122 $frameset = '';
123 $frameset_tags = '';
124 if (preg_match_all('|<frameset([^>]*)>(.*?)</frameset>|is', $hp->html, $matches)) {
125 $last = count($matches[0])-1;
126 $frameset = $matches[2][$last];
127 $frameset_tags = $matches[1][$last];
129 // if HTML is being requested ...
130 if (empty($get_js) && empty($get_css)) {
131 if (empty($frameset)) {
132 // HP v6
133 if ($hotpot->navigation==HOTPOT_NAVIGATION_FRAME || $hotpot->navigation==HOTPOT_NAVIGATION_IFRAME) {
134 $get_html = ($framename=='main') ? true : false;
135 } else {
136 $get_html = true;
138 } else {
139 // HP5 v5
140 $get_html = empty($framename) ? true : false;
142 if ($get_html) {
144 if (HOTPOT_FIRST_ATTEMPT) {
145 add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
147 $attemptid = hotpot_add_attempt($hotpot->id);
148 if (! is_numeric($attemptid)) {
149 error('Could not insert attempt record: '.$db->ErrorMsg);
152 $hp->adjust_media_urls();
153 if (empty($frameset)) {
154 // HP6 v6
155 $targetframe = '';
156 switch ($hotpot->navigation) {
157 case HOTPOT_NAVIGATION_BUTTONS:
158 // do nothing (i.e. leave buttons as they are)
159 break;
160 case HOTPOT_NAVIGATION_GIVEUP:
161 $hp->insert_giveup_form($attemptid, '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->');
162 break;
163 case HOTPOT_NAVIGATION_FRAME:
164 $targetframe = $CFG->framename;
165 // drop through to remove nav buttons too
166 default:
167 $hp->remove_nav_buttons();
169 if (isset($hp->real_outputformat) && $hp->real_outputformat==HOTPOT_OUTPUTFORMAT_MOBILE) {
170 $hp->insert_submission_form($attemptid, '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->', true);
171 } else {
172 $hp->insert_submission_form($attemptid, '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->', false, $targetframe);
174 } else {
175 // HP5 v5
176 switch ($hotpot->navigation) {
177 case HOTPOT_NAVIGATION_BUTTONS:
178 // convert URLs in nav buttons
179 break;
180 case HOTPOT_NAVIGATION_GIVEUP:
181 // $hp->insert_giveup_form($attemptid, '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->');
182 break;
183 default:
184 // remove navigation buttons
185 $hp->html = preg_replace('#NavBar\+=(.*);#', '', $hp->html);
187 $hp->insert_submission_form($attemptid, "var NavBar='", "';");
191 //FEEDBACK = new Array();
192 //FEEDBACK[0] = ''; // url of feedback page/script
193 //FEEDBACK[1] = ''; // array of array('teachername', 'value');
194 //FEEDBACK[2] = ''; // 'student name' [formmail only]
195 //FEEDBACK[3] = ''; // 'student email' [formmail only]
196 //FEEDBACK[4] = ''; // window width
197 //FEEDBACK[5] = ''; // window height
198 //FEEDBACK[6] = ''; // 'Send a message to teacher' [prompt/button text]
199 //FEEDBACK[7] = ''; // 'Title'
200 //FEEDBACK[8] = ''; // 'Teacher'
201 //FEEDBACK[9] = ''; // 'Message'
202 //FEEDBACK[10] = ''; // 'Close this window'
203 $feedback = array();
204 switch ($hotpot->studentfeedback) {
205 case HOTPOT_FEEDBACK_NONE:
206 // do nothing
207 break;
208 case HOTPOT_FEEDBACK_WEBPAGE:
209 if (empty($hotpot->studentfeedbackurl)) {
210 $hotpot->studentfeedback = HOTPOT_FEEDBACK_NONE;
211 } else {
212 $feedback[0] = "'$hotpot->studentfeedbackurl'";
214 break;
215 case HOTPOT_FEEDBACK_FORMMAIL:
216 $teachers = hotpot_feedback_teachers($course, $hotpot);
217 if (empty($teachers) || empty($hotpot->studentfeedbackurl)) {
218 $hotpot->studentfeedback = HOTPOT_FEEDBACK_NONE;
219 } else {
220 $feedback[0] = "'$hotpot->studentfeedbackurl'";
221 $feedback[1] = $teachers;
222 $feedback[2] = "'".fullname($USER)."'";
223 $feedback[3] = "'".$USER->email."'";
224 $feedback[4] = 500; // width
225 $feedback[5] = 300; // height
227 break;
228 case HOTPOT_FEEDBACK_MOODLEFORUM:
229 $module = get_record('modules', 'name', 'forum');
230 $forums = get_records('forum', 'course', "$course->id");
231 if (empty($module) || empty($module->visible) || empty($forums)) {
232 $hotpot->studentfeedback = HOTPOT_FEEDBACK_NONE;
233 } else {
234 $feedback[0] = "'$CFG->wwwroot/mod/forum/index.php?id=$course->id'";
236 break;
237 case HOTPOT_FEEDBACK_MOODLEMESSAGING:
238 $teachers = hotpot_feedback_teachers($course, $hotpot);
239 if (empty($CFG->messaging) || empty($teachers)) {
240 $hotpot->studentfeedback = HOTPOT_FEEDBACK_NONE;
241 } else {
242 $feedback[0] = "'$CFG->wwwroot/message/discussion.php?id='";
243 $feedback[1] = $teachers;
244 $feedback[4] = 400; // width
245 $feedback[5] = 500; // height
247 break;
248 default:
249 // do nothing
251 if ($hotpot->studentfeedback != HOTPOT_FEEDBACK_NONE) {
252 $feedback[6] = "'Send a message to teacher'";
253 $feedback[7] = "'Title'";
254 $feedback[8] = "'Teacher'";
255 $feedback[9] = "'Message'";
256 $feedback[10] = "'Close this window'";
257 $js = '';
258 foreach ($feedback as $i=>$str) {
259 $js .= 'FEEDBACK['.$i."] = $str;\n";
261 $js = '<script type="text/javascript">'."\n//<![CDATA[\n"."FEEDBACK = new Array();\n".$js."//]]>\n</script>\n";
262 $hp->html = preg_replace('|</head>|i', "$js</head>", $hp->html, 1);
264 // insert hot-potatoes.js
265 $hp->insert_script(HOTPOT_JS);
266 // get Moodle pageid and pageclass
267 $pageid = '';
268 $pageclass = '';
269 if (function_exists('page_id_and_class')) {
270 page_id_and_class($pageid, $pageclass);
272 // extract first <head> tag
273 $head = '';
274 $pattern = '|<head([^>]*)>(.*?)</head>|is';
275 if (preg_match($pattern, $hp->html, $matches)) {
276 $head = $matches[2];
277 // remove <title>
278 $head = preg_replace('|<title[^>]*>(.*?)</title>|is', '', $head);
280 // extract <style> tags (and remove from $head)
281 $styles = '';
282 $pattern = '|<style([^>]*)>(.*?)</style>|is';
283 if (preg_match_all($pattern, $head, $matches)) {
284 $count = count($matches[0]);
285 for ($i=0; $i<$count; $i++) {
286 if ($pageid) {
287 $styles .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
289 $head = str_replace($matches[0][$i], '', $head);
292 // extract <script> tags (and remove from $head)
293 $scripts = '';
294 $pattern = '|<script([^>]*)>(.*?)</script>|is';
295 if (preg_match_all($pattern, $head, $matches)) {
296 $count = count($matches[0]);
297 for ($i=0; $i<$count; $i++) {
298 if ($pageid) {
299 $scripts .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
301 $head = str_replace($matches[0][$i], '', $head);
304 // extract <body> tags
305 $body = '';
306 $body_tags = '';
307 $footer = '</html>';
308 // HP6 and some HP5 (v6 and v4)
309 if (preg_match('|<body'.'([^>]*'.'onLoad=(["\'])(.*?)(\\2)'.'[^>]*)'.'>(.*)</body>|is', $hp->html, $matches)) {
310 $body = $matches[5]; // contents of first <body onload="StartUp()">...</body> block
311 if ($pageid) {
312 $body_tags = str_replace(' id="TheBody"', '', $matches[1]);
314 // workaround to ensure javascript onload routine for quiz is always executed
315 // $body_tags will only be inserted into the <body ...> tag
316 // if it is included in the theme/$CFG->theme/header.html,
317 // so some old or modified themes may not insert $body_tags
318 $body .= ""
319 . '<script type="text/javascript">'."\n"
320 . "//<![CDATA[\n"
321 . " var s = (typeof(window.onload)=='function') ? onload.toString() : '';\n"
322 . " if (s.indexOf('".$matches[3]."')<0) {\n"
323 . " if (s=='') {\n" // no previous onload
324 . " window.onload = new Function('".$matches[3]."');\n"
325 . " } else {\n"
326 . " window.onload_hotpot = onload;\n"
327 . " window.onload = new Function('window.onload_hotpot();'+'".$matches[3]."');\n"
328 . " }\n"
329 . " }\n"
330 . "//]]>\n"
331 . "</script>\n"
333 $footer = '</body>'.$footer;
334 } else if ($frameset) { // HP5 v5
335 switch ($framename) {
336 case 'top':
337 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas);
338 print $footer;
339 break;
340 default:
341 // add a HotPot navigation frame at the top of the page
342 //$rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
343 //$frameset = "\n\t".'<frame src="view.php?id='.$cm->id.'&amp;framename=top" frameborder="0" name="top"></frame>'.$frameset;
344 //$frameset_tags = preg_replace('|rows="(.*?)"|', 'rows="'.$rows.',\\1"', $frameset_tags);
345 // put navigation into var NavBar='';
346 // add form to TopFrame in "WriteFeedback" function
347 // OR add form to BottomFrame in "DisplayExercise" function
348 // submission form: '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->'
349 // give up form: '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->'
350 print "<html>\n";
351 print "<head>\n<title>$title</title>\n$styles\n$scripts</head>\n";
352 print "<frameset$frameset_tags>$frameset</frameset>\n";
353 print "</html>\n";
354 break;
355 } // end switch $framename
356 exit;
357 // other files (maybe not even a HotPots)
358 } else if (preg_match('|<body'.'([^>]*)'.'>(.*)</body>|is', $hp->html, $matches)) {
359 $body = $matches[2];
360 $body_tags = $matches[1];
362 // print the quiz to the browser
363 if ($get_js) {
364 print($scripts);
365 exit;
367 if ($get_css) {
368 print($styles);
369 exit;
371 // closing tags for "page" and "content" divs
372 $footer = '</div></div>'.$footer;
373 switch ($hotpot->navigation) {
374 case HOTPOT_NAVIGATION_BAR:
375 //update_module_button($cm->id, $course->id, $strmodulename.'" style="font-size:0.8em')
376 print_header($title, $heading, $navigation, "", $head.$styles.$scripts, true, $button, $loggedinas, false, $body_tags
378 if (!empty($available_msg)) {
379 notify($available_msg);
381 print $body.$footer;
382 break;
383 case HOTPOT_NAVIGATION_FRAME:
384 switch ($framename) {
385 case 'top':
386 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas);
387 print $footer;
388 break;
389 case 'main':
390 if (!empty($available_msg)) {
391 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
393 print $hp->html;
394 break;
395 default:
396 $txtframesetinfo = get_string('framesetinfo');
397 $txttoptitle = get_string('navigation', 'hotpot');
398 $txtmaintitle = get_string('modulename', 'hotpot');
400 $rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
402 @header('Content-Type: text/html; charset=utf-8');
403 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">\n";
404 print "<html>\n";
405 print "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
406 print "<head><title>$title</title></head>\n";
407 print "<frameset rows=$rows,*>\n";
408 print "<frame title=\"$txttoptitle\" src=\"view.php?id=$cm->id&amp;framename=top\">\n";
409 print "<frame title=\"$txtmaintitle\" src=\"view.php?id=$cm->id&amp;framename=main\">\n";
410 print "<noframes>\n";
411 print "<p>$txtframesetinfo</p>\n";
412 print "<ul><li><a href=\"view.php?id=$cm->id&amp;framename=top\">$txttoptitle</a></li>\n";
413 print "<li><a href=\"view.php?id=$cm->id&amp;framename=main\">$txtmaintitle</a></li></ul>\n";
414 print "</noframes>\n";
415 print "</frameset>\n";
416 print "</html>\n";
417 break;
418 } // end switch $framename
419 break;
420 case HOTPOT_NAVIGATION_IFRAME:
421 switch ($framename) {
422 case 'main':
423 print $hp->html;
424 break;
425 default:
426 $iframe_id = 'hotpot_iframe';
427 $body_tags = " onload=\"set_iframe_height('$iframe_id')\"";
428 $iframe_js = '<script src="iframe.js" type="text/javascript"></script>'."\n";
429 print_header(
430 $title, $heading, $navigation,
431 "", $head.$styles.$scripts.$iframe_js, true, $button,
432 $loggedinas, false, $body_tags
434 if (!empty($available_msg)) {
435 notify($available_msg);
437 print "<iframe id=\"$iframe_id\" src=\"view.php?id=$cm->id&amp;framename=main\" height=\"100%\" width=\"100%\">";
438 print "<ilayer name=\"$iframe_id\" src=\"view.php?id=$cm->id&amp;framename=main\" height=\"100%\" width=\"100%\">";
439 print "</ilayer>\n";
440 print "</iframe>\n";
441 print $footer;
442 break;
443 } // end switch $framename
444 break;
445 case HOTPOT_NAVIGATION_GIVEUP:
446 // replace charset , if necessary
447 // HotPots are plain ascii (iso-8859-1) with unicode chars encoded as HTML entities
448 $charset = get_string("thischarset");
449 if ($charset == 'iso-8859-1') {
450 // do nothing
451 } else {
452 $hp->html = preg_replace(
453 '|<meta[^>]*charset=iso-8859-1[^>]*>|is',
454 '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />',
455 $hp->html
458 // no break (continue to print html to browser)
459 default:
460 // HOTPOT_NAVIGATION_BUTTONS
461 // HOTPOT_NAVIGATION_NONE
462 if (!empty($available_msg)) {
463 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
465 print($hp->html);
467 ///////////////////////////////////
468 /// functions
469 ///////////////////////////////////
470 function hotpot_feedback_teachers(&$course, &$hotpot) {
471 global $CFG;
472 $teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'mod/hotpot:grade');
473 $teacherdetails = '';
474 if (!empty($teachers)) {
475 $details = array();
476 foreach ($teachers as $teacher) {
477 if ($hotpot->studentfeedback==HOTPOT_FEEDBACK_MOODLEMESSAGING) {
478 $detail = $teacher->id;
479 } else {
480 $detail =$teacher->email;
482 $details[] = "new Array('".fullname($teacher)."', '$detail')";
484 $teacherdetails = 'new Array('.implode(',', $details).");\n";
486 return $teacherdetails;