MDL-21400 finalising JS api - removing ->on_dom_ready (now bool param in js() and...
[moodle.git] / mod / hotpot / view.php
CommitLineData
3b532ae6 1<?PHP
2
dabfd0ed 3 /// This page prints a hotpot quiz
4 if (defined('HOTPOT_FIRST_ATTEMPT') && HOTPOT_FIRST_ATTEMPT==false) {
5 // this script is being included (by attempt.php)
6 } else {
7 // this script is being called directly from the browser
8 define('HOTPOT_FIRST_ATTEMPT', true);
9 require_once("../../config.php");
10 require_once("lib.php");
acfc768e 11
fb414b62 12 $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
13 $hp = optional_param('hp', 0, PARAM_INT); // hotpot ID
acfc768e 14
dabfd0ed 15 if ($id) {
a6855934 16 $PAGE->set_url('/mod/hotpot/report.php', array('id'=>$id));
dabfd0ed 17 if (! $cm = get_coursemodule_from_id('hotpot', $id)) {
50fb3673 18 print_error('invalidcoursemodule');
dabfd0ed 19 }
621fea38 20 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
50fb3673 21 print_error('coursemisconf');
dabfd0ed 22 }
621fea38 23 if (! $hotpot = $DB->get_record("hotpot", array("id"=>$cm->instance))) {
50fb3673 24 print_error('invalidcoursemodule');
dabfd0ed 25 }
acfc768e 26
dabfd0ed 27 } else {
a6855934 28 $PAGE->set_url('/mod/hotpot/report.php', array('hp'=>$hp));
621fea38 29 if (! $hotpot = $DB->get_record("hotpot", array("id"=>$hp))) {
50fb3673 30 print_error('invalidhotpotid', 'hotpot');
dabfd0ed 31 }
621fea38 32 if (! $course = $DB->get_record("course", array("id"=>$hotpot->course))) {
50fb3673 33 print_error('coursemisconf');
dabfd0ed 34 }
35 if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
50fb3673 36 print_error('invalidcoursemodule');
dabfd0ed 37 }
0be6f678 38
dabfd0ed 39 }
bb5b7224 40 require_login($course, true, $cm);
81e956b9 41 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
bf38af3f 42 require_capability('mod/hotpot:attempt', $context);
dabfd0ed 43 }
44 // set nextpage (for error messages)
45 $nextpage = "$CFG->wwwroot/course/view.php?id=$course->id";
46 // header strings
6ba65fa0 47 $title = format_string($course->shortname.': '.$hotpot->name, true);
48 $heading = $course->fullname;
0be6f678 49
845f45ec 50 $button = '<div style="font-size:0.75em;">'.$button.'</div>';
cfe1d039 51
52 $PAGE->set_title($title);
53 $PAGE->set_heading($heading);
54 $PAGE->set_button($button);
cfe1d039 55
dabfd0ed 56 $time = time();
622365d2 57 $hppassword = optional_param('hppassword', '', PARAM_RAW);
81e956b9 58 if (HOTPOT_FIRST_ATTEMPT && !has_capability('mod/hotpot:grade', $context)) {
dabfd0ed 59 // check this quiz is available to this student
60 // error message, if quiz is unavailable
61 $error = '';
62 // check quiz is visible
63 if (!hotpot_is_visible($cm)) {
64 $error = get_string("activityiscurrentlyhidden");
65 // check network address
b8aa76c1 66 } else if ($hotpot->subnet && !address_in_subnet(getremoteaddr(), $hotpot->subnet)) {
dabfd0ed 67 $error = get_string("subneterror", "quiz");
68 // check number of attempts
86342d63 69 } else if ($hotpot->attempts && $hotpot->attempts <= $DB->count_records_select('hotpot_attempts', 'hotpot=? AND userid=?', array($hotpot->id, $USER->id), 'COUNT(DISTINCT clickreportid)')) {
dabfd0ed 70 $error = get_string("nomoreattempts", "quiz");
71 // get password
72 } else if ($hotpot->password && empty($hppassword)) {
cfe1d039 73 echo $OUTPUT->header();
bd9df7be 74 echo $OUTPUT->heading($hotpot->name);
dabfd0ed 75 $boxalign = 'center';
76 $boxwidth = 500;
77 if (trim(strip_tags($hotpot->summary))) {
7eeedf59 78 echo $OUTPUT->box_start("generalbox boxalign$boxalign");
85db96c5 79 print '<div class="mdl-align">'.format_text($hotpot->summary)."</div>\n";
7eeedf59 80 echo $OUTPUT->box_end();
dabfd0ed 81 print "<br />\n";
82 }
b7dc2256 83 print '<form id="passwordform" method="post" action="view.php?id='.$cm->id.'">'."\n";
7eeedf59 84 echo $OUTPUT->box_start("generalbox boxalign$boxalign");
85db96c5 85 print '<div class="mdl-align">';
dabfd0ed 86 print get_string('requirepasswordmessage', 'quiz').'<br /><br />';
87 print '<b>'.get_string('password').':</b> ';
60af2703 88 print '<input name="hppassword" type="password" value="" /> ';
89 print '<input type="submit" value="'.get_string("ok").'" /> ';
dabfd0ed 90 print "</div>\n";
7eeedf59 91 echo $OUTPUT->box_end();
dabfd0ed 92 print "</form>\n";
f8302e2d 93 echo $OUTPUT->footer();
dabfd0ed 94 exit;
95 // check password
96 } else if ($hotpot->password && strcmp($hotpot->password, $hppassword)) {
97 $error = get_string("passworderror", "quiz");
98 $nextpage = "view.php?id=$cm->id";
99 // check quiz is open
100 } else if ($hotpot->timeopen && $hotpot->timeopen > $time) {
101 $error = get_string("quiznotavailable", "quiz", userdate($hotpot->timeopen))."<br />\n";
102 // check quiz is not closed
103 } else if ($hotpot->timeclose && $hotpot->timeclose < $time) {
104 $error = get_string("quizclosed", "quiz", userdate($hotpot->timeclose))."<br />\n";
105 }
106 if ($error) {
cfe1d039 107 echo $OUTPUT->header();
dabfd0ed 108 notice($error, $nextpage);
109 //
110 // script stops here, if quiz is unavailable to student
111 //
112 }
113 }
114 $available_msg = '';
115 if (!empty($hotpot->timeclose) && $hotpot->timeclose > $time) {
116 // quiz is available until 'timeclose'
117 $available_msg = get_string("quizavailable", "quiz", userdate($hotpot->timeclose))."<br />\n";
118 }
119 // open and parse the source file
120 if(!$hp = new hotpot_xml_quiz($hotpot)) {
50fb3673 121 print_error('quizunavailable', 'hotpot');
dabfd0ed 122 }
fb414b62 123 $get_js = optional_param('js', '', PARAM_ALPHA);
124 $get_css = optional_param('css', '', PARAM_ALPHA);
125 $framename = optional_param('framename', '', PARAM_ALPHA);
dabfd0ed 126 // look for <frameset> (HP5 v5)
127 $frameset = '';
128 $frameset_tags = '';
129 if (preg_match_all('|<frameset([^>]*)>(.*?)</frameset>|is', $hp->html, $matches)) {
130 $last = count($matches[0])-1;
131 $frameset = $matches[2][$last];
132 $frameset_tags = $matches[1][$last];
133 }
134 // if HTML is being requested ...
135 if (empty($get_js) && empty($get_css)) {
0be6f678 136 if (empty($frameset)) {
dabfd0ed 137 // HP v6
138 if ($hotpot->navigation==HOTPOT_NAVIGATION_FRAME || $hotpot->navigation==HOTPOT_NAVIGATION_IFRAME) {
139 $get_html = ($framename=='main') ? true : false;
140 } else {
141 $get_html = true;
142 }
0be6f678 143 } else {
dabfd0ed 144 // HP5 v5
145 $get_html = empty($framename) ? true : false;
146 }
147 if ($get_html) {
0be6f678 148
dabfd0ed 149 if (HOTPOT_FIRST_ATTEMPT) {
150 add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
acfc768e 151
dabfd0ed 152 $attemptid = hotpot_add_attempt($hotpot->id);
dabfd0ed 153 }
154 $hp->adjust_media_urls();
155 if (empty($frameset)) {
156 // HP6 v6
b3687f28 157 $targetframe = '';
dabfd0ed 158 switch ($hotpot->navigation) {
159 case HOTPOT_NAVIGATION_BUTTONS:
160 // do nothing (i.e. leave buttons as they are)
161 break;
162 case HOTPOT_NAVIGATION_GIVEUP:
163 $hp->insert_giveup_form($attemptid, '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->');
164 break;
b3687f28 165 case HOTPOT_NAVIGATION_FRAME:
166 $targetframe = $CFG->framename;
167 // drop through to remove nav buttons too
dabfd0ed 168 default:
169 $hp->remove_nav_buttons();
170 }
171 if (isset($hp->real_outputformat) && $hp->real_outputformat==HOTPOT_OUTPUTFORMAT_MOBILE) {
172 $hp->insert_submission_form($attemptid, '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->', true);
173 } else {
b3687f28 174 $hp->insert_submission_form($attemptid, '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->', false, $targetframe);
dabfd0ed 175 }
176 } else {
177 // HP5 v5
178 switch ($hotpot->navigation) {
179 case HOTPOT_NAVIGATION_BUTTONS:
180 // convert URLs in nav buttons
181 break;
182 case HOTPOT_NAVIGATION_GIVEUP:
183 // $hp->insert_giveup_form($attemptid, '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->');
184 break;
185 default:
186 // remove navigation buttons
187 $hp->html = preg_replace('#NavBar\+=(.*);#', '', $hp->html);
188 }
189 $hp->insert_submission_form($attemptid, "var NavBar='", "';");
190 }
191 }
192 }
193 //FEEDBACK = new Array();
194 //FEEDBACK[0] = ''; // url of feedback page/script
195 //FEEDBACK[1] = ''; // array of array('teachername', 'value');
196 //FEEDBACK[2] = ''; // 'student name' [formmail only]
197 //FEEDBACK[3] = ''; // 'student email' [formmail only]
198 //FEEDBACK[4] = ''; // window width
199 //FEEDBACK[5] = ''; // window height
200 //FEEDBACK[6] = ''; // 'Send a message to teacher' [prompt/button text]
201 //FEEDBACK[7] = ''; // 'Title'
202 //FEEDBACK[8] = ''; // 'Teacher'
203 //FEEDBACK[9] = ''; // 'Message'
204 //FEEDBACK[10] = ''; // 'Close this window'
205 $feedback = array();
206 switch ($hotpot->studentfeedback) {
207 case HOTPOT_FEEDBACK_NONE:
208 // do nothing
209 break;
210 case HOTPOT_FEEDBACK_WEBPAGE:
211 if (empty($hotpot->studentfeedbackurl)) {
212 $hotpot->studentfeedback = HOTPOT_FEEDBACK_NONE;
213 } else {
214 $feedback[0] = "'$hotpot->studentfeedbackurl'";
215 }
216 break;
217 case HOTPOT_FEEDBACK_FORMMAIL:
218 $teachers = hotpot_feedback_teachers($course, $hotpot);
219 if (empty($teachers) || empty($hotpot->studentfeedbackurl)) {
220 $hotpot->studentfeedback = HOTPOT_FEEDBACK_NONE;
221 } else {
222 $feedback[0] = "'$hotpot->studentfeedbackurl'";
223 $feedback[1] = $teachers;
224 $feedback[2] = "'".fullname($USER)."'";
225 $feedback[3] = "'".$USER->email."'";
226 $feedback[4] = 500; // width
227 $feedback[5] = 300; // height
228 }
229 break;
230 case HOTPOT_FEEDBACK_MOODLEFORUM:
621fea38 231 $module = $DB->get_record('modules', array('name'=>'forum'));
232 $forums = $DB->get_records('forum', array('course'=>$course->id));
dabfd0ed 233 if (empty($module) || empty($module->visible) || empty($forums)) {
234 $hotpot->studentfeedback = HOTPOT_FEEDBACK_NONE;
235 } else {
236 $feedback[0] = "'$CFG->wwwroot/mod/forum/index.php?id=$course->id'";
237 }
238 break;
239 case HOTPOT_FEEDBACK_MOODLEMESSAGING:
240 $teachers = hotpot_feedback_teachers($course, $hotpot);
241 if (empty($CFG->messaging) || empty($teachers)) {
242 $hotpot->studentfeedback = HOTPOT_FEEDBACK_NONE;
243 } else {
244 $feedback[0] = "'$CFG->wwwroot/message/discussion.php?id='";
245 $feedback[1] = $teachers;
246 $feedback[4] = 400; // width
247 $feedback[5] = 500; // height
248 }
249 break;
250 default:
251 // do nothing
252 }
253 if ($hotpot->studentfeedback != HOTPOT_FEEDBACK_NONE) {
254 $feedback[6] = "'Send a message to teacher'";
255 $feedback[7] = "'Title'";
256 $feedback[8] = "'Teacher'";
257 $feedback[9] = "'Message'";
258 $feedback[10] = "'Close this window'";
259 $js = '';
260 foreach ($feedback as $i=>$str) {
261 $js .= 'FEEDBACK['.$i."] = $str;\n";
262 }
f4ba7e1a 263 $js = '<script type="text/javascript">'."\n//<![CDATA[\n"."FEEDBACK = new Array();\n".$js."//]]>\n</script>\n";
dabfd0ed 264 $hp->html = preg_replace('|</head>|i', "$js</head>", $hp->html, 1);
265 }
266 // insert hot-potatoes.js
267 $hp->insert_script(HOTPOT_JS);
111a18eb 268 // get Moodle pageid and pageclass
d529807a 269 $pageid = $PAGE->pagetype;
270
dabfd0ed 271 // extract first <head> tag
272 $head = '';
273 $pattern = '|<head([^>]*)>(.*?)</head>|is';
274 if (preg_match($pattern, $hp->html, $matches)) {
275 $head = $matches[2];
276 // remove <title>
277 $head = preg_replace('|<title[^>]*>(.*?)</title>|is', '', $head);
278 }
279 // extract <style> tags (and remove from $head)
280 $styles = '';
281 $pattern = '|<style([^>]*)>(.*?)</style>|is';
282 if (preg_match_all($pattern, $head, $matches)) {
283 $count = count($matches[0]);
284 for ($i=0; $i<$count; $i++) {
111a18eb 285 if ($pageid) {
286 $styles .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
e73457e4 287 }
dabfd0ed 288 $head = str_replace($matches[0][$i], '', $head);
289 }
290 }
291 // extract <script> tags (and remove from $head)
292 $scripts = '';
293 $pattern = '|<script([^>]*)>(.*?)</script>|is';
294 if (preg_match_all($pattern, $head, $matches)) {
295 $count = count($matches[0]);
296 for ($i=0; $i<$count; $i++) {
111a18eb 297 if ($pageid) {
298 $scripts .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
e73457e4 299 }
dabfd0ed 300 $head = str_replace($matches[0][$i], '', $head);
301 }
302 }
303 // extract <body> tags
304 $body = '';
305 $body_tags = '';
306 $footer = '</html>';
0be6f678 307 // HP6 and some HP5 (v6 and v4)
dabfd0ed 308 if (preg_match('|<body'.'([^>]*'.'onLoad=(["\'])(.*?)(\\2)'.'[^>]*)'.'>(.*)</body>|is', $hp->html, $matches)) {
309 $body = $matches[5]; // contents of first <body onload="StartUp()">...</body> block
111a18eb 310 if ($pageid) {
311 $body_tags = str_replace(' id="TheBody"', '', $matches[1]);
e73457e4 312 }
dabfd0ed 313 // workaround to ensure javascript onload routine for quiz is always executed
314 // $body_tags will only be inserted into the <body ...> tag
315 // if it is included in the theme/$CFG->theme/header.html,
316 // so some old or modified themes may not insert $body_tags
317 $body .= ""
318 . '<script type="text/javascript">'."\n"
5e87b920 319 . "//<![CDATA[\n"
dabfd0ed 320 . " var s = (typeof(window.onload)=='function') ? onload.toString() : '';\n"
321 . " if (s.indexOf('".$matches[3]."')<0) {\n"
322 . " if (s=='') {\n" // no previous onload
323 . " window.onload = new Function('".$matches[3]."');\n"
324 . " } else {\n"
325 . " window.onload_hotpot = onload;\n"
326 . " window.onload = new Function('window.onload_hotpot();'+'".$matches[3]."');\n"
327 . " }\n"
328 . " }\n"
5e87b920 329 . "//]]>\n"
dabfd0ed 330 . "</script>\n"
331 ;
332 $footer = '</body>'.$footer;
333 } else if ($frameset) { // HP5 v5
334 switch ($framename) {
335 case 'top':
cfe1d039 336 echo $OUTPUT->header();
dabfd0ed 337 print $footer;
338 break;
339 default:
340 // add a HotPot navigation frame at the top of the page
341 //$rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
f6c4bee3 342 //$frameset = "\n\t".'<frame src="view.php?id='.$cm->id.'&amp;framename=top" frameborder="0" name="top"></frame>'.$frameset;
dabfd0ed 343 //$frameset_tags = preg_replace('|rows="(.*?)"|', 'rows="'.$rows.',\\1"', $frameset_tags);
344 // put navigation into var NavBar='';
345 // add form to TopFrame in "WriteFeedback" function
346 // OR add form to BottomFrame in "DisplayExercise" function
347 // submission form: '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->'
348 // give up form: '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->'
349 print "<html>\n";
350 print "<head>\n<title>$title</title>\n$styles\n$scripts</head>\n";
351 print "<frameset$frameset_tags>$frameset</frameset>\n";
352 print "</html>\n";
353 break;
354 } // end switch $framename
355 exit;
356 // other files (maybe not even a HotPots)
357 } else if (preg_match('|<body'.'([^>]*)'.'>(.*)</body>|is', $hp->html, $matches)) {
358 $body = $matches[2];
359 $body_tags = $matches[1];
360 }
361 // print the quiz to the browser
362 if ($get_js) {
363 print($scripts);
364 exit;
365 }
366 if ($get_css) {
367 print($styles);
368 exit;
369 }
f6c4bee3 370 // closing tags for "page" and "content" divs
371 $footer = '</div></div>'.$footer;
dabfd0ed 372 switch ($hotpot->navigation) {
373 case HOTPOT_NAVIGATION_BAR:
3b532ae6 374 $PAGE->set_title($title);
375 $PAGE->set_heading($heading);
376 $PAGE->set_button($button);
377 $PAGE->set_headingmenu($loggedinas);
378 echo $OUTPUT->header();
dabfd0ed 379 if (!empty($available_msg)) {
7eeedf59 380 echo $OUTPUT->notification($available_msg);
dabfd0ed 381 }
382 print $body.$footer;
383 break;
384 case HOTPOT_NAVIGATION_FRAME:
385 switch ($framename) {
386 case 'top':
cfe1d039 387 echo $OUTPUT->header();
dabfd0ed 388 print $footer;
389 break;
390 case 'main':
391 if (!empty($available_msg)) {
392 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
393 }
394 print $hp->html;
395 break;
396 default:
b8db24a5 397 $txtframesetinfo = get_string('framesetinfo');
398 $txttoptitle = get_string('navigation', 'hotpot');
399 $txtmaintitle = get_string('modulename', 'hotpot');
400
dabfd0ed 401 $rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
b8db24a5 402
403 @header('Content-Type: text/html; charset=utf-8');
404 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">\n";
dabfd0ed 405 print "<html>\n";
b8db24a5 406 print "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
dabfd0ed 407 print "<head><title>$title</title></head>\n";
408 print "<frameset rows=$rows,*>\n";
f6c4bee3 409 print "<frame title=\"$txttoptitle\" src=\"view.php?id=$cm->id&amp;framename=top\">\n";
410 print "<frame title=\"$txtmaintitle\" src=\"view.php?id=$cm->id&amp;framename=main\">\n";
b8db24a5 411 print "<noframes>\n";
412 print "<p>$txtframesetinfo</p>\n";
f6c4bee3 413 print "<ul><li><a href=\"view.php?id=$cm->id&amp;framename=top\">$txttoptitle</a></li>\n";
414 print "<li><a href=\"view.php?id=$cm->id&amp;framename=main\">$txtmaintitle</a></li></ul>\n";
b8db24a5 415 print "</noframes>\n";
dabfd0ed 416 print "</frameset>\n";
417 print "</html>\n";
418 break;
419 } // end switch $framename
420 break;
421 case HOTPOT_NAVIGATION_IFRAME:
422 switch ($framename) {
423 case 'main':
424 print $hp->html;
425 break;
426 default:
427 $iframe_id = 'hotpot_iframe';
9dec75db 428 $PAGE->requires->js('/mod/hotpot/iframe.js');
593f9b87 429 $PAGE->requires->js_function_call('set_iframe_height', array($iframe_id), true);
cfe1d039 430 echo $OUTPUT->header();
dabfd0ed 431 if (!empty($available_msg)) {
7eeedf59 432 echo $OUTPUT->notification($available_msg);
dabfd0ed 433 }
f6c4bee3 434 print "<iframe id=\"$iframe_id\" src=\"view.php?id=$cm->id&amp;framename=main\" height=\"100%\" width=\"100%\">";
435 print "<ilayer name=\"$iframe_id\" src=\"view.php?id=$cm->id&amp;framename=main\" height=\"100%\" width=\"100%\">";
dabfd0ed 436 print "</ilayer>\n";
437 print "</iframe>\n";
438 print $footer;
439 break;
440 } // end switch $framename
441 break;
34caf003 442 case HOTPOT_NAVIGATION_GIVEUP:
443 // replace charset , if necessary
444 // HotPots are plain ascii (iso-8859-1) with unicode chars encoded as HTML entities
445 $charset = get_string("thischarset");
446 if ($charset == 'iso-8859-1') {
447 // do nothing
448 } else {
449 $hp->html = preg_replace(
0be6f678 450 '|<meta[^>]*charset=iso-8859-1[^>]*>|is',
451 '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />',
34caf003 452 $hp->html
453 );
454 }
455 // no break (continue to print html to browser)
dabfd0ed 456 default:
457 // HOTPOT_NAVIGATION_BUTTONS
dabfd0ed 458 // HOTPOT_NAVIGATION_NONE
459 if (!empty($available_msg)) {
460 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
461 }
462 print($hp->html);
463 }
acfc768e 464///////////////////////////////////
dabfd0ed 465/// functions
acfc768e 466///////////////////////////////////
467function hotpot_feedback_teachers(&$course, &$hotpot) {
dabfd0ed 468 global $CFG;
dc558690 469 $teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'mod/hotpot:grade');
dabfd0ed 470 $teacherdetails = '';
471 if (!empty($teachers)) {
472 $details = array();
473 foreach ($teachers as $teacher) {
474 if ($hotpot->studentfeedback==HOTPOT_FEEDBACK_MOODLEMESSAGING) {
475 $detail = $teacher->id;
476 } else {
477 $detail =$teacher->email;
478 }
479 $details[] = "new Array('".fullname($teacher)."', '$detail')";
480 }
481 $teacherdetails = 'new Array('.implode(',', $details).");\n";
482 }
483 return $teacherdetails;
acfc768e 484}
86342d63 485