From ba458143e39da7a29a8430e1c3316da606beab2f Mon Sep 17 00:00:00 2001
From: samhemelryk <samhemelryk>
Date: Thu, 25 Jun 2009 06:29:41 +0000
Subject: [PATCH] mod-lesson MDL-16706 Removed inline scripts and modified
 timer to take obj args

---
 mod/lesson/locallib.php          | 22 +++++++++-------------
 mod/lesson/timer.js              | 14 +++++++++++++-
 theme/standard/styles_layout.css |  4 ++++
 3 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php
index d475e1b2df..aa11734fe2 100644
--- a/mod/lesson/locallib.php
+++ b/mod/lesson/locallib.php
@@ -1765,25 +1765,21 @@ function lesson_print_mediafile_block($cmid, $lesson) {
  * @return void
  **/
 function lesson_print_clock_block($cmid, $lesson, $timer) {
-    global $CFG;
+    global $CFG, $PAGE;
 
     $context = get_context_instance(CONTEXT_MODULE, $cmid);
 
     // Display for timed lessons and for students only
     if($lesson->timed and !has_capability('mod/lesson:manage', $context) and !empty($timer)) {
-        $content  = '<script type="text/javascript" charset="utf-8">'."\n";
-        $content .= "<!--\n";
-        $content .= '    var starttime  = '.$timer->starttime.";\n";
-        $content .= '    var servertime = '.time().";\n";
-        $content .= '    var testlength = '.($lesson->maxtime * 60).";\n";
-        $content .= '    document.write(\'<script type="text/javascript" src="'.$CFG->wwwroot.'/mod/lesson/timer.js" charset="utf-8"><\/script>\');'."\n";
-        $content .= "    window.onload = function () { show_clock(); };\n";
-        $content .= "// -->\n";
-        $content .= "</script>\n";
-        $content .= "<noscript>\n";
+
+        $clocksettings = Array('starttime'=>$timer->starttime, 'servertime'=>time(),'testlength'=>($lesson->maxtime * 60));
+        $content = $PAGE->requires->data_for_js('clocksettings', $clocksettings)->asap();
+        $content .= $PAGE->requires->js('mod/lesson/timer.js')->asap();
+        $content .= $PAGE->requires->js_function_call('show_clock')->asap();
+        $content .= '<div class="jshidewhenenabled">';
         $content .= lesson_print_time_remaining($timer->starttime, $lesson->maxtime, true)."\n";
-        $content .= "</noscript>\n";
-    
+        $content .= '</div>';
+
         print_side_block(get_string('timeremaining', 'lesson'), $content, NULL, NULL, '', array('class' => 'clock'), get_string('timeremaining', 'lesson'));
     }
 }
diff --git a/mod/lesson/timer.js b/mod/lesson/timer.js
index 4f6290d0d5..9b0aa4f633 100644
--- a/mod/lesson/timer.js
+++ b/mod/lesson/timer.js
@@ -30,7 +30,19 @@
     var javatimeDate = new Date();
     var javatime = javatimeDate.getTime();
     javatime = Math.floor(javatime/1000);
-    
+
+    if (typeof(clocksettings)!='undefined') {
+        if (clocksettings.starttime) {
+            starttime = parseInt(clocksettings.starttime);
+        }
+        if (clocksettings.servertime) {
+            servertime =  parseInt(clocksettings.servertime);
+        }
+        if (clocksettings.testlength) {
+            testlength =  parseInt(clocksettings.testlength);
+        }
+    }
+   
     difference = javatime - servertime;    
     starttime = starttime + difference;
 
diff --git a/theme/standard/styles_layout.css b/theme/standard/styles_layout.css
index 4516387a4c..d20b5e7628 100644
--- a/theme/standard/styles_layout.css
+++ b/theme/standard/styles_layout.css
@@ -4295,6 +4295,10 @@ body#mod-forum-search .introcontent {
   text-align: left;
 }
 
+body.jsenabled .jshidewhenenabled {
+  display:none;
+}
+
 /***
  *** Modules: Quiz
  ***/
-- 
2.39.5