Fix a possible race condition in the PaintWeb DML code.
[moodle/mihaisucan.git] / mod / hotpot / view.php
blobf3110da542b9978ae97f4149331dde083fed58d2
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 // 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);
42 // set nextpage (for error messages)
43 $nextpage = "$CFG->wwwroot/course/view.php?id=$course->id";
44 // header strings
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>';
53 $time = time();
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
58 $error = '';
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");
68 // get password
69 } else if ($hotpot->password && empty($hppassword)) {
70 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
71 print_heading($hotpot->name);
72 $boxalign = 'center';
73 $boxwidth = 500;
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();
78 print "<br />\n";
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").'" /> ';
87 print "</div>\n";
88 print_simple_box_end();
89 print "</form>\n";
90 print_footer();
91 exit;
92 // check password
93 } else if ($hotpot->password && strcmp($hotpot->password, $hppassword)) {
94 $error = get_string("passworderror", "quiz");
95 $nextpage = "view.php?id=$cm->id";
96 // check quiz is open
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";
103 if ($error) {
104 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
105 notice($error, $nextpage);
107 // script stops here, if quiz is unavailable to student
111 $available_msg = '';
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)
124 $frameset = '';
125 $frameset_tags = '';
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)) {
134 // HP v6
135 if ($hotpot->navigation==HOTPOT_NAVIGATION_FRAME || $hotpot->navigation==HOTPOT_NAVIGATION_IFRAME) {
136 $get_html = ($framename=='main') ? true : false;
137 } else {
138 $get_html = true;
140 } else {
141 // HP5 v5
142 $get_html = empty($framename) ? true : false;
144 if ($get_html) {
146 if (HOTPOT_FIRST_ATTEMPT) {
147 add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
149 $attemptid = hotpot_add_attempt($hotpot->id);
150 if (! is_numeric($attemptid)) {
151 error('Could not insert attempt record: '.$db->ErrorMsg);
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();
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);
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);
189 $hp->insert_submission_form($attemptid, "var NavBar='", "';");
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'";
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
229 break;
230 case HOTPOT_FEEDBACK_MOODLEFORUM:
231 $module = get_record('modules', 'name', 'forum');
232 $forums = get_records('forum', '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'";
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
249 break;
250 default:
251 // do nothing
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";
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);
266 // insert hot-potatoes.js
267 $hp->insert_script(HOTPOT_JS);
268 // get Moodle pageid and pageclass
269 $pageid = '';
270 $pageclass = '';
271 if (function_exists('page_id_and_class')) {
272 page_id_and_class($pageid, $pageclass);
274 // extract first <head> tag
275 $head = '';
276 $pattern = '|<head([^>]*)>(.*?)</head>|is';
277 if (preg_match($pattern, $hp->html, $matches)) {
278 $head = $matches[2];
279 // remove <title>
280 $head = preg_replace('|<title[^>]*>(.*?)</title>|is', '', $head);
282 // extract <style> tags (and remove from $head)
283 $styles = '';
284 $pattern = '|<style([^>]*)>(.*?)</style>|is';
285 if (preg_match_all($pattern, $head, $matches)) {
286 $count = count($matches[0]);
287 for ($i=0; $i<$count; $i++) {
288 if ($pageid) {
289 $styles .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
291 $head = str_replace($matches[0][$i], '', $head);
294 // extract <script> tags (and remove from $head)
295 $scripts = '';
296 $pattern = '|<script([^>]*)>(.*?)</script>|is';
297 if (preg_match_all($pattern, $head, $matches)) {
298 $count = count($matches[0]);
299 for ($i=0; $i<$count; $i++) {
300 if ($pageid) {
301 $scripts .= str_replace('TheBody', $pageid, $matches[0][$i])."\n";
303 $head = str_replace($matches[0][$i], '', $head);
306 // extract <body> tags
307 $body = '';
308 $body_tags = '';
309 $footer = '</html>';
310 // HP6 and some HP5 (v6 and v4)
311 if (preg_match('|<body'.'([^>]*'.'onLoad=(["\'])(.*?)(\\2)'.'[^>]*)'.'>(.*)</body>|is', $hp->html, $matches)) {
312 $body = $matches[5]; // contents of first <body onload="StartUp()">...</body> block
313 if ($pageid) {
314 $body_tags = str_replace(' id="TheBody"', '', $matches[1]);
316 // workaround to ensure javascript onload routine for quiz is always executed
317 // $body_tags will only be inserted into the <body ...> tag
318 // if it is included in the theme/$CFG->theme/header.html,
319 // so some old or modified themes may not insert $body_tags
320 $body .= ""
321 . '<script type="text/javascript">'."\n"
322 . "//<![CDATA[\n"
323 . " var s = (typeof(window.onload)=='function') ? onload.toString() : '';\n"
324 . " if (s.indexOf('".$matches[3]."')<0) {\n"
325 . " if (s=='') {\n" // no previous onload
326 . " window.onload = new Function('".$matches[3]."');\n"
327 . " } else {\n"
328 . " window.onload_hotpot = onload;\n"
329 . " window.onload = new Function('window.onload_hotpot();'+'".$matches[3]."');\n"
330 . " }\n"
331 . " }\n"
332 . "//]]>\n"
333 . "</script>\n"
335 $footer = '</body>'.$footer;
336 } else if ($frameset) { // HP5 v5
337 switch ($framename) {
338 case 'top':
339 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas);
340 print $footer;
341 break;
342 default:
343 // add a HotPot navigation frame at the top of the page
344 //$rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
345 //$frameset = "\n\t".'<frame src="view.php?id='.$cm->id.'&amp;framename=top" frameborder="0" name="top"></frame>'.$frameset;
346 //$frameset_tags = preg_replace('|rows="(.*?)"|', 'rows="'.$rows.',\\1"', $frameset_tags);
347 // put navigation into var NavBar='';
348 // add form to TopFrame in "WriteFeedback" function
349 // OR add form to BottomFrame in "DisplayExercise" function
350 // submission form: '<!-- BeginSubmissionForm -->', '<!-- EndSubmissionForm -->'
351 // give up form: '<!-- BeginTopNavButtons -->', '<!-- EndTopNavButtons -->'
352 print "<html>\n";
353 print "<head>\n<title>$title</title>\n$styles\n$scripts</head>\n";
354 print "<frameset$frameset_tags>$frameset</frameset>\n";
355 print "</html>\n";
356 break;
357 } // end switch $framename
358 exit;
359 // other files (maybe not even a HotPots)
360 } else if (preg_match('|<body'.'([^>]*)'.'>(.*)</body>|is', $hp->html, $matches)) {
361 $body = $matches[2];
362 $body_tags = $matches[1];
364 // print the quiz to the browser
365 if ($get_js) {
366 print($scripts);
367 exit;
369 if ($get_css) {
370 print($styles);
371 exit;
373 // closing tags for "page" and "content" divs
374 $footer = '</div></div>'.$footer;
375 switch ($hotpot->navigation) {
376 case HOTPOT_NAVIGATION_BAR:
377 //update_module_button($cm->id, $course->id, $strmodulename.'" style="font-size:0.8em')
378 print_header($title, $heading, $navigation, "", $head.$styles.$scripts, true, $button, $loggedinas, false, $body_tags
380 if (!empty($available_msg)) {
381 notify($available_msg);
383 print $body.$footer;
384 break;
385 case HOTPOT_NAVIGATION_FRAME:
386 switch ($framename) {
387 case 'top':
388 print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas);
389 print $footer;
390 break;
391 case 'main':
392 if (!empty($available_msg)) {
393 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
395 print $hp->html;
396 break;
397 default:
398 $txtframesetinfo = get_string('framesetinfo');
399 $txttoptitle = get_string('navigation', 'hotpot');
400 $txtmaintitle = get_string('modulename', 'hotpot');
402 $rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
404 @header('Content-Type: text/html; charset=utf-8');
405 print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" \"http://www.w3.org/TR/html4/frameset.dtd\">\n";
406 print "<html>\n";
407 print "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n";
408 print "<head><title>$title</title></head>\n";
409 print "<frameset rows=$rows,*>\n";
410 print "<frame title=\"$txttoptitle\" src=\"view.php?id=$cm->id&amp;framename=top\">\n";
411 print "<frame title=\"$txtmaintitle\" src=\"view.php?id=$cm->id&amp;framename=main\">\n";
412 print "<noframes>\n";
413 print "<p>$txtframesetinfo</p>\n";
414 print "<ul><li><a href=\"view.php?id=$cm->id&amp;framename=top\">$txttoptitle</a></li>\n";
415 print "<li><a href=\"view.php?id=$cm->id&amp;framename=main\">$txtmaintitle</a></li></ul>\n";
416 print "</noframes>\n";
417 print "</frameset>\n";
418 print "</html>\n";
419 break;
420 } // end switch $framename
421 break;
422 case HOTPOT_NAVIGATION_IFRAME:
423 switch ($framename) {
424 case 'main':
425 print $hp->html;
426 break;
427 default:
428 $iframe_id = 'hotpot_iframe';
429 $body_tags = " onload=\"set_iframe_height('$iframe_id')\"";
430 $iframe_js = '<script src="iframe.js" type="text/javascript"></script>'."\n";
431 print_header(
432 $title, $heading, $navigation,
433 "", $head.$styles.$scripts.$iframe_js, true, $button,
434 $loggedinas, false, $body_tags
436 if (!empty($available_msg)) {
437 notify($available_msg);
439 print "<iframe id=\"$iframe_id\" src=\"view.php?id=$cm->id&amp;framename=main\" height=\"100%\" width=\"100%\">";
440 print "<ilayer name=\"$iframe_id\" src=\"view.php?id=$cm->id&amp;framename=main\" height=\"100%\" width=\"100%\">";
441 print "</ilayer>\n";
442 print "</iframe>\n";
443 print $footer;
444 break;
445 } // end switch $framename
446 break;
447 case HOTPOT_NAVIGATION_GIVEUP:
448 // replace charset , if necessary
449 // HotPots are plain ascii (iso-8859-1) with unicode chars encoded as HTML entities
450 $charset = get_string("thischarset");
451 if ($charset == 'iso-8859-1') {
452 // do nothing
453 } else {
454 $hp->html = preg_replace(
455 '|<meta[^>]*charset=iso-8859-1[^>]*>|is',
456 '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'" />',
457 $hp->html
460 // no break (continue to print html to browser)
461 default:
462 // HOTPOT_NAVIGATION_BUTTONS
463 // HOTPOT_NAVIGATION_NONE
464 if (!empty($available_msg)) {
465 $hp->insert_message('<!-- BeginTopNavButtons -->', $available_msg);
467 print($hp->html);
469 ///////////////////////////////////
470 /// functions
471 ///////////////////////////////////
472 function hotpot_feedback_teachers(&$course, &$hotpot) {
473 global $CFG;
474 $teachers = get_users_by_capability(get_context_instance(CONTEXT_COURSE, $course->id), 'mod/hotpot:grade');
475 $teacherdetails = '';
476 if (!empty($teachers)) {
477 $details = array();
478 foreach ($teachers as $teacher) {
479 if ($hotpot->studentfeedback==HOTPOT_FEEDBACK_MOODLEMESSAGING) {
480 $detail = $teacher->id;
481 } else {
482 $detail =$teacher->email;
484 $details[] = "new Array('".fullname($teacher)."', '$detail')";
486 $teacherdetails = 'new Array('.implode(',', $details).");\n";
488 return $teacherdetails;