]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16706 - eliminate miscellaneous inine JavaScript
authortjhunt <tjhunt>
Fri, 26 Sep 2008 10:49:31 +0000 (10:49 +0000)
committertjhunt <tjhunt>
Fri, 26 Sep 2008 10:49:31 +0000 (10:49 +0000)
This does the quiz (apart from the bloody 'secure' window), the question bank and a couple of others.

lib/weblib.php
message/refresh.php
mod/quiz/accessrules.php
mod/quiz/quiz.js
mod/quiz/review.php
question/export.php

index 0bf3b5f40b0cea754760ba2a032c82a45d8cb2dd..2d47911098764cd4ae6d3bfbf92b3723baf141b1 100644 (file)
@@ -2720,6 +2720,33 @@ function print_js_call($function, $args = array(), $return = false) {
     }
 }
 
+/**
+ * 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
@@ -6144,7 +6171,7 @@ function redirect($url, $message='', $delay=-1) {
         @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;
     }
 
@@ -6164,17 +6191,7 @@ function redirect($url, $message='', $delay=-1) {
     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.
index b2c10748759d2e8c5c7cda082546bc6f2b907c34..009c7c3a110e440ef2c12b42bd5d2521b2125394 100644 (file)
     }
 
     // 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
index dab4edd7ca7490542e4a5a2e3d1e654754155eb5..4940e8e2c25f9f7b8207bcb504cfed30b74f0caa 100644 (file)
@@ -168,9 +168,7 @@ class quiz_access_manager {
         /// 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));
         }
     }
 
@@ -232,9 +230,7 @@ class quiz_access_manager {
                 $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 {
index 74e08402f7c1ff3dd63817fc8a6de843644c8ec1..93b484e70bc46368101a5793fdf0c94c458949aa 100644 (file)
@@ -185,7 +185,7 @@ quiz_secure_window = {
             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,
@@ -213,7 +213,7 @@ quiz_secure_window = {
             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();
index f481b6f1afe5e3db965cd3435a9504281b98a3d3..7316a0ad6a5fa10a7304e18c2edae2b3821b082e 100644 (file)
                 '<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.
index 7836e4a4b9c4afc61ac8aef5e74a4afa06c7de74..24b5de9da872c126bc0a123e18def3cc35193154 100644 (file)
         } 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());