[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/hotpot/ -> view.php (source)

   1  <?PHP // $Id: view.php,v 1.38.2.5 2008/03/06 07:34:03 gbateson Exp $
   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");
  10  
  11          $id = optional_param('id', 0, PARAM_INT); // Course Module ID, or
  12          $hp = optional_param('hp', 0, PARAM_INT); // hotpot ID
  13  
  14          if ($id) {
  15              if (! $cm = get_coursemodule_from_id('hotpot', $id)) {
  16                  error("Course Module ID was incorrect");
  17              }
  18              if (! $course = get_record("course", "id", $cm->course)) {
  19                  error("Course is misconfigured");
  20              }
  21              if (! $hotpot = get_record("hotpot", "id", $cm->instance)) {
  22                  error("Course module is incorrect");
  23              }
  24  
  25          } else {
  26              if (! $hotpot = get_record("hotpot", "id", $hp)) {
  27                  error("Course module is incorrect");
  28              }
  29              if (! $course = get_record("course", "id", $hotpot->course)) {
  30                  error("Course is misconfigured");
  31              }
  32              if (! $cm = get_coursemodule_from_instance("hotpot", $hotpot->id, $course->id)) {
  33                  error("Course Module ID was incorrect");
  34              }
  35  
  36          }
  37          require_login($course);
  38          $context = get_context_instance(CONTEXT_MODULE, $cm->id);
  39      }
  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;
  45  
  46      $navigation = build_navigation('', $cm);
  47  
  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";
  77              }
  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";
 100          }
 101          if ($error) {
 102              print_header($title, $heading, $navigation, "", "", true, $button, $loggedinas, false);
 103              notice($error, $nextpage);
 104              //
 105              // script stops here, if quiz is unavailable to student
 106              //
 107          }
 108      }
 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";
 113      }
 114      // open and parse the source file
 115      if(!$hp = new hotpot_xml_quiz($hotpot)) {
 116          error("Quiz is unavailable at the moment");
 117      }
 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];
 128      }
 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;
 137              }
 138          } else {
 139              // HP5 v5
 140              $get_html = empty($framename) ? true : false;
 141          }
 142          if ($get_html) {
 143  
 144              if (HOTPOT_FIRST_ATTEMPT) {
 145                  add_to_log($course->id, "hotpot", "view", "view.php?id=$cm->id", "$hotpot->id", "$cm->id");
 146  
 147                  $attemptid = hotpot_add_attempt($hotpot->id);
 148                  if (! is_numeric($attemptid)) {
 149                      error('Could not insert attempt record: '.$db->ErrorMsg);
 150                  }
 151              }
 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();
 168                  }
 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);
 173                  }
 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);
 186                  }
 187                  $hp->insert_submission_form($attemptid, "var NavBar='", "';");
 188              }
 189          }
 190      }
 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'";
 213              }
 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
 226              }
 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'";
 235              }
 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
 246              }
 247              break;
 248          default:
 249              // do nothing
 250      }
 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";
 260          }
 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);
 263      }
 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);
 271      }
 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);
 279      }
 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";
 288              }
 289              $head = str_replace($matches[0][$i], '', $head);
 290          }
 291      }
 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";
 300              }
 301              $head = str_replace($matches[0][$i], '', $head);
 302          }
 303      }
 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]);
 313          }
 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"
 332          ;
 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];
 361      }
 362      // print the quiz to the browser
 363      if ($get_js) {
 364          print($scripts);
 365          exit;
 366      }
 367      if ($get_css) {
 368          print($styles);
 369          exit;
 370      }
 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
 377              );
 378              if (!empty($available_msg)) {
 379                  notify($available_msg);
 380              }
 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);
 392                      }
 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');
 399  
 400                      $rows = empty($CFG->resource_framesize) ? 85 : $CFG->resource_framesize;
 401  
 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
 433                      );
 434                      if (!empty($available_msg)) {
 435                          notify($available_msg);
 436                      }
 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
 456                  );
 457              }
 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);
 464              }
 465              print($hp->html);
 466      }
 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;
 481              }
 482              $details[] = "new Array('".fullname($teacher)."', '$detail')";
 483          }
 484          $teacherdetails = 'new Array('.implode(',', $details).");\n";
 485      }
 486      return $teacherdetails;
 487  }
 488  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7