This does the quiz (apart from the bloody 'secure' window), the question bank and a couple of others.
}
}
+/**
+ * Generate the HTML for calling a javascript funtion after a time delay.
+ * In other respects, this function is the same as print_js_call.
+ *
+ * @param integer $delay the desired delay in seconds.
+ * @param string $function the name of the JavaScript function to call.
+ * @param array $args an optional list of arguments to the function call.
+ * @param boolean $return if true, return the HTML code, otherwise output it.
+ * @return mixed string if $return is true, otherwise nothing.
+ */
+function print_delayed_js_call($delay, $function, $args = array(), $return = false) {
+ $quotedargs = array();
+ foreach ($args as $arg) {
+ $quotedargs[] = json_encode($arg);
+ }
+ $html = '';
+ $html .= '<script type="text/javascript">//<![CDATA[' . "\n";
+ $html .= 'setTimeout(function() {' . $function . '(' .
+ implode(', ', $quotedargs) . ');}, ' . ($delay * 1000) . ");\n";
+ $html .= "//]]></script>\n";
+ if ($return) {
+ return $html;
+ } else {
+ echo $html;
+ }
+}
+
/**
* Sometimes you need access to some values in your JavaScript that you can only
* get from PHP code. You can handle this by generating your JS in PHP, but a
@header('Location: '.$url);
//another way for older browsers and already sent headers (eg trailing whitespace in config.php)
echo '<meta http-equiv="refresh" content="'. $delay .'; url='. $encodedurl .'" />';
- echo '<script type="text/javascript">'. "\n" .'//<![CDATA['. "\n". "location.replace('".addslashes_js($url)."');". "\n". '//]]>'. "\n". '</script>'; // To cope with Mozilla bug
+ print_js_call('document.location.replace', array($url));
die;
}
echo '</div>';
if (!$errorprinted) {
-?>
-<script type="text/javascript">
-//<![CDATA[
-
- function redirect() {
- document.location.replace('<?php echo addslashes_js($url) ?>');
- }
- setTimeout("redirect()", <?php echo ($delay * 1000) ?>);
-//]]>
-</script>
-<?php
+ print_delayed_js_call($delay, 'document.location.replace', array($url));
}
$CFG->docroot = false; // to prevent the link to moodle docs from being displayed on redirect page.
}
// Javascript for Mozilla to cope with the redirect bug from editor being on in this page
- ?>
-
-<script type="text/javascript">
-<!--
-
- function redirect() {
- document.location.replace('refresh.php?id=<?php echo $userid ?>&name=<?php echo urlencode($userfullname) ?>&wait=<?php echo $wait ?>');
- }
-
- setTimeout("redirect()", <?php echo ($wait*1000) ?>);
--->
-</script>
-
-</body>
-</html>
+ print_delayed_js_call($wait, 'document.location.replace', array(
+ "refresh.php?id=$userid&name=" . urlencode($userfullname) . "&wait=$wait"));
+ echo '</body>'."\n";
+ echo '</html>'."\n";
+?>
\ No newline at end of file
/// Make sure the timer starts just above zero. If $timeleft was <= 0, then
/// this will just have the effect of causing the quiz to be submitted immediately.
$timerstartvalue = max($timeleft, 1);
- echo '<script type="text/javascript">';
- echo "quiz_timer.initialise('", get_string('timesup','quiz'), "', ", $timerstartvalue, ");";
- echo "</script>\n";
+ print_js_call('quiz_timer.initialise', array(get_string('timesup','quiz'), $timerstartvalue));
}
}
$delay = 0;
}
print_box_end();
- echo '<script type="text/javascript">';
- echo 'quiz_secure_window.close(', addslashes_js(htmlspecialchars($url)), ', ', $delay, ')';
- echo '</script>';
+ print_js_call('quiz_secure_window.close', array($url, $delay));
print_footer('empty');
die();
} else {
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown = quiz_secure_window.intercept_click;
- document.oncontextmenu = new Function("alert(quiz_secure_window.protection_message); return false")
+ document.oncontextmenu = function() {alert(quiz_secure_window.protection_message); return false;};
},
// Code for secure window. This used to be in protect_js.php. I don't understand it,
quiz_secure_window.close_next_url = url;
}
if (delay > 0) {
- setTimeout('quiz_close_securewindow("", 0)', delay*1000);
+ setTimeout(function() {quiz_secure_window.close('eval (x)', 0);}, delay*1000);
} else {
if (window.opener) {
window.opener.document.location.reload();
'<input type="submit" id="savingflagssubmit" name="savingflags" value="' .
get_string('saveflags', 'question') . '" />' .
"</div>\n" .
- "\n</div></form>\n" .
- '<script type="text/javascript">' .
- "\nquestion_flag_changer.init_flag_save_form('savingflagssubmit');\n" .
- "</script>\n";
+ "\n</div></form>\n";
+ print_js_call('question_flag_changer.init_flag_save_form', array('savingflagssubmit'));
}
/// Print a link to the next page.
} else {
$efile = get_file_url($filename, null, 'questionfile');
echo '<p><div class="boxaligncenter">' .
- get_string('yourfileshoulddownload', 'question', $efile) . '</a></div></p>';
- echo '
-<script type="text/javascript">
-//<![CDATA[
-
- function redirect() {
- document.location.replace("' . addslashes_js($efile) . '");
- }
- setTimeout("redirect()", 1000);
-//]]>
-</script>';
+ get_string('yourfileshoulddownload', 'question', $efile) . '</div></p>';
+ print_delayed_js_call(1, 'document.location.replace', array($efile));
}
print_continue('edit.php?' . $thispageurl->get_query_string());