MDL-21400 finalising JS api - removing ->on_dom_ready (now bool param in js() and...
[moodle.git] / mod / hotpot / view.php
1 <?PHP
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");
12         $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
13         $hp = optional_param('hp', 0, PARAM_INT); // hotpot ID
15         if ($id) {
16             $PAGE->set_url('/mod/hotpot/report.php', array('id'=>$id));
17             if (! $cm = get_coursemodule_from_id('hotpot', $id)) {
18                 print_error('invalidcoursemodule');
19             }
20             if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
21                 print_error('coursemisconf');
22             }
23             if (! $hotpot = $DB->get_record("hotpot", array("id"=>$cm->instance))) {
24                 print_error('invalidcoursemodule');
25             }
27         } else {
28             $PAGE->set_url('/mod/hotpot/report.php', array('hp'=>$hp));
29             if (! $hotpot = $DB->get_record("hotpot", array("id"=>$hp))) {
30                 print_error('invalidhotpotid', 'hotpot');
31             }
32             if (! $course = $DB->get_record("course", array("id"=>$hotpot->course))) {
33                 print_error('coursemisconf');
34             }
35             if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
36                 print_error('invalidcoursemodule');
37             }
39         }
40         require_login($course, true, $cm);
41         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
42         require_capability('mod/hotpot:attempt', $context);
43     }
44     // set nextpage (for error messages)
45     $nextpage = "$CFG->wwwroot/course/view.php?id=$course->id";
46     // header strings
47     $title = format_string($course->shortname.': '.$hotpot->name, true);
48     $heading = $course->fullname;
50     $button = '<div style="font-size:0.75em;">'.$button.'</div>';
52     $PAGE->set_title($title);
53     $PAGE->set_heading($heading);
54     $PAGE->set_button($button);
56     $time = time();
57     $hppassword = optional_param('hppassword', '', PARAM_RAW);
58     if (HOTPOT_FIRST_ATTEMPT && !has_capability('mod/hotpot:grade', $context)) {
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
66         } else if ($hotpot->subnet && !address_in_subnet(getremoteaddr(), $hotpot->subnet)) {
67             $error = get_string("subneterror", "quiz");
68         // check number of attempts
69         } else if ($hotpot->attempts && $hotpot->attempts <= $DB->count_records_select('hotpot_attempts', 'hotpot=? AND userid=?', array($hotpot->id, $USER->id), 'COUNT(DISTINCT clickreportid)')) {
70             $error = get_string("nomoreattempts", "quiz");
71         // get password
72         } else if ($hotpot->password && empty($hppassword)) {
73             echo $OUTPUT->header();
74             echo $OUTPUT->heading($hotpot->name);
75             $boxalign = 'center';
76             $boxwidth = 500;
77             if (trim(strip_tags($hotpot->summary))) {
78                 echo $OUTPUT->box_start("generalbox boxalign$boxalign");
79                 print '<div class="mdl-align">'.format_text($hotpot->summary)."</div>\n";
80                 echo $OUTPUT->box_end();
81                 print "<br />\n";
82             }
83             print '<form id="passwordform" method="post" action="view.php?id='.$cm->id.'">'."\n";
84             echo $OUTPUT->box_start("generalbox boxalign$boxalign");
85             print '<div class="mdl-align">';
86             print get_string('requirepasswordmessage', 'quiz').'<br /><br />';
87             print '<b>'.get_string('password').':</b> ';
88             print '<input name="hppassword" type="password" value="" /> ';
89             print '<input type="submit" value="'.get_string("ok").'" /> ';
90             print "</div>\n";
91             echo $OUTPUT->box_end();
92             print "</form>\n";
93             echo $OUTPUT->footer();
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) {
107             echo $OUTPUT->header();
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)) {
121         print_error('quizunavailable', 'hotpot');
122     }
123     $get_js = optional_param('js', '', PARAM_ALPHA);
124     $get_css = optional_param('css', '', PARAM_ALPHA);
125     $framename = optional_param('framename', '', PARAM_ALPHA);
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)) {
136         if (empty($frameset)) {
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             }
143         } else {
144             // HP5 v5
145             $get_html = empty($framename) ? true : false;
146         }
147         if ($get_html) {
149             if (HOTPOT_FIRST_ATTEMPT) {
150                 add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
152                 $attemptid = hotpot_add_attempt($hotpot->id);
153             }
154             $hp->adjust_media_urls();
155             if (empty($frameset)) {
156                 // HP6 v6
157                 $targetframe = '';
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;
165                     case HOTPOT_NAVIGATION_FRAME:
166                         $targetframe = $CFG->framename;
167                         // drop through to remove nav buttons too
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 {
174                     $hp->insert_submission_form($attemptid, '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->', false, $targetframe);
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:
231             $module = $DB->get_record('modules', array('name'=>'forum'));
232             $forums = $DB->get_records('forum', array('course'=>$course->id));
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         }
263         $js = '<script type="text/javascript">'."\n//<![CDATA[\n"."FEEDBACK = new Array();\n".$js."//]]>\n</script>\n";
264         $hp->html = preg_replace('|</head>|i', "$js</head>", $hp->html, 1);
265     }
266     // insert hot-potatoes.js
267     $hp->insert_script(HOTPOT_JS);
268     // get Moodle pageid and pageclass
269     $pageid = $PAGE->pagetype;
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++) {
285             if ($pageid) {
286                 $styles .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
287             }
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++) {
297             if ($pageid) {
298                 $scripts .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
299             }
300             $head = str_replace($matches[0][$i], '', $head);
301         }
302     }
303     // extract <body> tags
304     $body = '';
305     $body_tags = '';
306     $footer = '</html>';
307     // HP6 and some HP5 (v6 and v4)
308     if (preg_match('|<body'.'([^>]*'.'onLoad=(["\'])(.*?)(\\2)'.'[^>]*)'.'>(.*)</body>|is', $hp->html, $matches)) {
309         $body = $matches[5]; // contents of first <body onload="StartUp()">...</body> block
310         if ($pageid) {
311             $body_tags = str_replace(' id="TheBody"', '', $matches[1]);
312         }
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"
319         .   "//<![CDATA[\n"
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"
329         .   "//]]>\n"
330         .   "</script>\n"
331         ;
332         $footer = '</body>'.$footer;
333     } else if ($frameset) { // HP5 v5
334         switch ($framename) {
335             case 'top':
336                 echo $OUTPUT->header();
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;
342                 //$frameset = "\n\t".'<frame src="view.php?id='.$cm->id.'&amp;framename=top" frameborder="0" name="top"></frame>'.$frameset;
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     }
370     // closing tags for "page" and "content" divs
371     $footer = '</div></div>'.$footer;
372     switch ($hotpot->navigation) {
373         case HOTPOT_NAVIGATION_BAR:
374             $PAGE->set_title($title);
375             $PAGE->set_heading($heading);
376             $PAGE->set_button($button);
377             $PAGE->set_headingmenu($loggedinas);
378             echo $OUTPUT->header();
379             if (!empty($available_msg)) {
380                 echo $OUTPUT->notification($available_msg);
381             }
382             print $body.$footer;
383         break;
384         case HOTPOT_NAVIGATION_FRAME:
385             switch ($framename) {
386                 case 'top':
387                     echo $OUTPUT->header();
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:
397                     $txtframesetinfo = get_string('framesetinfo');
398                     $txttoptitle     = get_string('navigation', 'hotpot');
399                     $txtmaintitle    = get_string('modulename', 'hotpot');
401                     $rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
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";
405                     print "<html>\n";
406                     print "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
407                     print "<head><title>$title</title></head>\n";
408                     print "<frameset rows=$rows,*>\n";
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";
411                     print "<noframes>\n";
412                     print "<p>$txtframesetinfo</p>\n";
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";
415                     print "</noframes>\n";
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';
428                     $PAGE->requires->js('/mod/hotpot/iframe.js');
429                     $PAGE->requires->js_function_call('set_iframe_height', array($iframe_id), true);
430                     echo $OUTPUT->header();
431                     if (!empty($available_msg)) {
432                         echo $OUTPUT->notification($available_msg);
433                     }
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%\">";
436                     print "</ilayer>\n";
437                     print "</iframe>\n";
438                     print $footer;
439                 break;
440             } // end switch $framename
441         break;
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(
450                     '|<meta[^>]*charset=iso-8859-1[^>]*>|is',
451                     '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />',
452                     $hp->html
453                 );
454             }
455             // no break (continue to print html to browser)
456         default:
457             // HOTPOT_NAVIGATION_BUTTONS
458             // HOTPOT_NAVIGATION_NONE
459             if (!empty($available_msg)) {
460                 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
461             }
462             print($hp->html);
463     }
464 ///////////////////////////////////
465 /// functions
466 ///////////////////////////////////
467 function hotpot_feedback_teachers(&$course, &$hotpot) {
468     global $CFG;
469     $teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'mod/hotpot:grade');
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;