Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / hotpot / view.php
blob54d22819592654e1e3c0524f845553b307b781fc
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->id);
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 $navlinks = array();
47 $navlinks[] = array('name' => get_string("modulenameplural", "hotpot"), 'link' => $CFG->wwwroot.'/mod/hotpot/index.php?id='.$course->id, 'type' => 'activity');
48 $navlinks[] = array('name' => $hotpot->name, 'link' => '', 'type' => 'activityinstance');
50 $navigation = build_navigation($navlinks);
52 $button = update_module_button($cm->id, $course->id, get_string("modulename", "hotpot"));
53 $button = '<div style="font-size:0.75em;">'.$button.'</div>';
54 $loggedinas = '<span class="logininfo">'.user_login_string($course, $USER).'</span>';
55 $time = time();
56 $hppassword = optional_param('hppassword', '');
57 if (HOTPOT_FIRST_ATTEMPT && !has_capability('mod/hotpot:grade', $context)) {
58 // check this quiz is available to this student
59 // error message, if quiz is unavailable
60 $error = '';
61 // check quiz is visible
62 if (!hotpot_is_visible($cm)) {
63 $error = get_string("activityiscurrentlyhidden");
64 // check network address
65 } else if ($hotpot->subnet && !address_in_subnet($_SERVER['REMOTE_ADDR'], $hotpot->subnet)) {
66 $error = get_string("subneterror", "quiz");
67 // check number of attempts
68 } else if ($hotpot->attempts && $hotpot->attempts <= count_records('hotpot_attempts', 'hotpot', $hotpot->id, 'userid', $USER->id)) {
69 $error = get_string("nomoreattempts", "quiz");
70 // get password
71 } else if ($hotpot->password && empty($hppassword)) {
72 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
73 print_heading($hotpot->name);
74 $boxalign = 'center';
75 $boxwidth = 500;
76 if (trim(strip_tags($hotpot->summary))) {
77 print_simple_box_start($boxalign, $boxwidth);
78 print '<div align="center">'.format_text($hotpot->summary)."</div>\n";
79 print_simple_box_end();
80 print "<br />\n";
82 print '<form id="passwordform" method="post" action="view.php?id='.$cm->id.'">'."\n";
83 print_simple_box_start($boxalign, $boxwidth);
84 print '<div align="center">';
85 print get_string('requirepasswordmessage', 'quiz').'<br /><br />';
86 print '<b>'.get_string('password').':</b> ';
87 print '<input name="hppassword" type="password" value="" /> ';
88 print '<input type="submit" value="'.get_string("ok").'" /> ';
89 print "</div>\n";
90 print_simple_box_end();
91 print "</form>\n";
92 print_footer();
93 exit;
94 // check password
95 } else if ($hotpot->password && strcmp($hotpot->password, $hppassword)) {
96 $error = get_string("passworderror", "quiz");
97 $nextpage = "view.php?id=$cm->id";
98 // check quiz is open
99 } else if ($hotpot->timeopen && $hotpot->timeopen > $time) {
100 $error = get_string("quiznotavailable", "quiz", userdate($hotpot->timeopen))."<br />\n";
101 // check quiz is not closed
102 } else if ($hotpot->timeclose && $hotpot->timeclose < $time) {
103 $error = get_string("quizclosed", "quiz", userdate($hotpot->timeclose))."<br />\n";
105 if ($error) {
106 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
107 notice($error, $nextpage);
109 // script stops here, if quiz is unavailable to student
113 $available_msg = '';
114 if (!empty($hotpot->timeclose) && $hotpot->timeclose > $time) {
115 // quiz is available until 'timeclose'
116 $available_msg = get_string("quizavailable", "quiz", userdate($hotpot->timeclose))."<br />\n";
118 // open and parse the source file
119 if(!$hp = new hotpot_xml_quiz($hotpot)) {
120 error("Quiz is unavailable at the moment");
122 $get_js = optional_param('js', '', PARAM_ALPHA);
123 $get_css = optional_param('css', '', PARAM_ALPHA);
124 $framename = optional_param('framename', '', PARAM_ALPHA);
125 // look for <frameset> (HP5 v5)
126 $frameset = '';
127 $frameset_tags = '';
128 if (preg_match_all('|<frameset([^>]*)>(.*?)</frameset>|is', $hp->html, $matches)) {
129 $last = count($matches[0])-1;
130 $frameset = $matches[2][$last];
131 $frameset_tags = $matches[1][$last];
133 // if HTML is being requested ...
134 if (empty($get_js) && empty($get_css)) {
135 if (empty($frameset)) {
136 // HP v6
137 if ($hotpot->navigation==HOTPOT_NAVIGATION_FRAME || $hotpot->navigation==HOTPOT_NAVIGATION_IFRAME) {
138 $get_html = ($framename=='main') ? true : false;
139 } else {
140 $get_html = true;
142 } else {
143 // HP5 v5
144 $get_html = empty($framename) ? true : false;
146 if ($get_html) {
148 if (HOTPOT_FIRST_ATTEMPT) {
149 add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
151 $attemptid = hotpot_add_attempt($hotpot->id);
152 if (! is_numeric($attemptid)) {
153 error('Could not insert attempt record: '.$db->ErrorMsg);
156 $hp->adjust_media_urls();
157 if (empty($frameset)) {
158 // HP6 v6
159 switch ($hotpot->navigation) {
160 case HOTPOT_NAVIGATION_BUTTONS:
161 // do nothing (i.e. leave buttons as they are)
162 break;
163 case HOTPOT_NAVIGATION_GIVEUP:
164 $hp->insert_giveup_form($attemptid, '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->');
165 break;
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 -->');
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.'&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 switch ($hotpot->navigation) {
372 case HOTPOT_NAVIGATION_BAR:
373 //update_module_button($cm->id, $course->id, $strmodulename.'" style="font-size:0.8em')
374 print_header(
375 $title, $heading, $navigation,
376 "", $head.$styles.$scripts, true, $button,
377 $loggedinas, false, $body_tags
379 if (!empty($available_msg)) {
380 notify($available_msg);
382 print $body.$footer;
383 break;
384 case HOTPOT_NAVIGATION_FRAME:
385 switch ($framename) {
386 case 'top':
387 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas);
388 print $footer;
389 break;
390 case 'main':
391 if (!empty($available_msg)) {
392 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
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&framename=top\">\n";
410 print "<frame title=\"$txtmaintitle\" src=\"view.php?id=$cm->id&framename=main\">\n";
411 print "<noframes>\n";
412 print "<p>$txtframesetinfo</p>\n";
413 print "<ul><li><a href=\"view.php?id=$cm->id&framename=top\">$txttoptitle</a></li>\n";
414 print "<li><a href=\"view.php?id=$cm->id&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 $body_tags = " onload=\"set_iframe_height('$iframe_id')\"";
429 $iframe_js = '<script src="iframe.js" type="text/javascript"></script>'."\n";
430 print_header(
431 $title, $heading, $navigation,
432 "", $head.$styles.$scripts.$iframe_js, true, $button,
433 $loggedinas, false, $body_tags
435 if (!empty($available_msg)) {
436 notify($available_msg);
438 print "<iframe id=\"$iframe_id\" src=\"view.php?id=$cm->id&framename=main\" height=\"100%\" width=\"100%\">";
439 print "<ilayer name=\"$iframe_id\" src=\"view.php?id=$cm->id&framename=main\" height=\"100%\" width=\"100%\">";
440 print "</ilayer>\n";
441 print "</iframe>\n";
442 print $footer;
443 break;
444 } // end switch $framename
445 break;
446 case HOTPOT_NAVIGATION_GIVEUP:
447 // replace charset , if necessary
448 // HotPots are plain ascii (iso-8859-1) with unicode chars encoded as HTML entities
449 $charset = get_string("thischarset");
450 if ($charset == 'iso-8859-1') {
451 // do nothing
452 } else {
453 $hp->html = preg_replace(
454 '|<meta[^>]*charset=iso-8859-1[^>]*>|is',
455 '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />',
456 $hp->html
459 // no break (continue to print html to browser)
460 default:
461 // HOTPOT_NAVIGATION_BUTTONS
462 // HOTPOT_NAVIGATION_NONE
463 if (!empty($available_msg)) {
464 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
466 print($hp->html);
468 ///////////////////////////////////
469 /// functions
470 ///////////////////////////////////
471 function hotpot_feedback_teachers(&$course, &$hotpot) {
472 global $CFG;
473 $teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'mod/hotpot:grade');
474 $teacherdetails = '';
475 if (!empty($teachers)) {
476 $details = array();
477 foreach ($teachers as $teacher) {
478 if ($hotpot->studentfeedback==HOTPOT_FEEDBACK_MOODLEMESSAGING) {
479 $detail = $teacher->id;
480 } else {
481 $detail =$teacher->email;
483 $details[] = "new Array('".fullname($teacher)."', '$detail')";
485 $teacherdetails = 'new Array('.implode(',', $details).");\n";
487 return $teacherdetails;