Commit b5378b04 by Brian Wilson

change timer to calculate relative duration in javascript

parent e07b667c
...@@ -86,8 +86,8 @@ class TimeLimitModule(XModule): ...@@ -86,8 +86,8 @@ class TimeLimitModule(XModule):
modified_duration = self._get_accommodated_duration(duration) modified_duration = self._get_accommodated_duration(duration)
self.ending_at = self.beginning_at + modified_duration self.ending_at = self.beginning_at + modified_duration
def get_end_time_in_ms(self): def get_remaining_time_in_ms(self):
return int(self.ending_at * 1000) return int((self.ending_at - time()) * 1000)
def get_instance_state(self): def get_instance_state(self):
state = {} state = {}
......
...@@ -177,10 +177,10 @@ def check_for_active_timelimit_module(request, course_id, course): ...@@ -177,10 +177,10 @@ def check_for_active_timelimit_module(request, course_id, course):
raise Http404("No {0} metadata at this location: {1} ".format('time_expired_redirect_url', location)) raise Http404("No {0} metadata at this location: {1} ".format('time_expired_redirect_url', location))
time_expired_redirect_url = timelimit_descriptor.metadata.get('time_expired_redirect_url') time_expired_redirect_url = timelimit_descriptor.metadata.get('time_expired_redirect_url')
context['time_expired_redirect_url'] = time_expired_redirect_url context['time_expired_redirect_url'] = time_expired_redirect_url
# Fetch the end time (in GMT) as stored in the module when it was started. # Fetch the remaining time relative to the end time as stored in the module when it was started.
# This value should be UTC time as number of milliseconds since epoch. # This value should be in milliseconds.
end_date = timelimit_module.get_end_time_in_ms() remaining_time = timelimit_module.get_remaining_time_in_ms()
context['timer_expiration_datetime'] = end_date context['timer_expiration_duration'] = remaining_time
if 'suppress_toplevel_navigation' in timelimit_descriptor.metadata: if 'suppress_toplevel_navigation' in timelimit_descriptor.metadata:
context['suppress_toplevel_navigation'] = timelimit_descriptor.metadata['suppress_toplevel_navigation'] context['suppress_toplevel_navigation'] = timelimit_descriptor.metadata['suppress_toplevel_navigation']
return_url = reverse('jump_to', kwargs={'course_id':course_id, 'location':location}) return_url = reverse('jump_to', kwargs={'course_id':course_id, 'location':location})
...@@ -191,7 +191,7 @@ def update_timelimit_module(user, course_id, student_module_cache, timelimit_des ...@@ -191,7 +191,7 @@ def update_timelimit_module(user, course_id, student_module_cache, timelimit_des
''' '''
Updates the state of the provided timing module, starting it if it hasn't begun. Updates the state of the provided timing module, starting it if it hasn't begun.
Returns dict with timer-related values to enable display of time remaining. Returns dict with timer-related values to enable display of time remaining.
Returns 'timer_expiration_datetime' in dict if timer is still active, and not if timer has expired. Returns 'timer_expiration_duration' in dict if timer is still active, and not if timer has expired.
''' '''
context = {} context = {}
# determine where to go when the exam ends: # determine where to go when the exam ends:
...@@ -215,10 +215,9 @@ def update_timelimit_module(user, course_id, student_module_cache, timelimit_des ...@@ -215,10 +215,9 @@ def update_timelimit_module(user, course_id, student_module_cache, timelimit_des
instance_module.save() instance_module.save()
# the exam has been started, either because the student is returning to the # the exam has been started, either because the student is returning to the
# exam page, or because they have just visited it. Fetch the end time (in GMT) as stored # exam page, or because they have just visited it. Fetch the remaining time relative to the
# in the module when it was started. # end time as stored in the module when it was started.
# This value should be UTC time as number of milliseconds since epoch. context['timer_expiration_duration'] = timelimit_module.get_remaining_time_in_ms()
context['timer_expiration_datetime'] = timelimit_module.get_end_time_in_ms()
# also use the timed module to determine whether top-level navigation is visible: # also use the timed module to determine whether top-level navigation is visible:
if 'suppress_toplevel_navigation' in timelimit_descriptor.metadata: if 'suppress_toplevel_navigation' in timelimit_descriptor.metadata:
context['suppress_toplevel_navigation'] = timelimit_descriptor.metadata['suppress_toplevel_navigation'] context['suppress_toplevel_navigation'] = timelimit_descriptor.metadata['suppress_toplevel_navigation']
...@@ -325,7 +324,7 @@ def index(request, course_id, chapter=None, section=None, ...@@ -325,7 +324,7 @@ def index(request, course_id, chapter=None, section=None,
if section_module.category == 'timelimit': if section_module.category == 'timelimit':
timer_context = update_timelimit_module(request.user, course_id, student_module_cache, timer_context = update_timelimit_module(request.user, course_id, student_module_cache,
section_descriptor, section_module) section_descriptor, section_module)
if 'timer_expiration_datetime' in timer_context: if 'timer_expiration_duration' in timer_context:
context.update(timer_context) context.update(timer_context)
else: else:
# if there is no expiration defined, then we know the timer has expired: # if there is no expiration defined, then we know the timer has expired:
......
...@@ -60,14 +60,12 @@ ...@@ -60,14 +60,12 @@
}); });
</script> </script>
% if timer_expiration_datetime: % if timer_expiration_duration:
<script type="text/javascript"> <script type="text/javascript">
var timer = { var timer = {
timer_inst : null, timer_inst : null,
get_remaining_secs : function() { end_time : null,
// set the end time when the template is rendered. get_remaining_secs : function(endTime) {
// This value should be UTC time as number of milliseconds since epoch.
var endTime = new Date(${timer_expiration_datetime});
var currentTime = new Date(); var currentTime = new Date();
var remaining_secs = Math.floor((endTime - currentTime)/1000); var remaining_secs = Math.floor((endTime - currentTime)/1000);
return remaining_secs; return remaining_secs;
...@@ -87,13 +85,16 @@ ...@@ -87,13 +85,16 @@
return remainingTimeString; return remainingTimeString;
}, },
update_time : function(self) { update_time : function(self) {
remaining_secs = self.get_remaining_secs(); remaining_secs = self.get_remaining_secs(self.end_time);
if (remaining_secs <= 0) { if (remaining_secs <= 0) {
self.end(self); self.end(self);
} }
$('#exam_timer').text(self.get_time_string(remaining_secs)); $('#exam_timer').text(self.get_time_string(remaining_secs));
}, },
start : function() { var that = this; start : function() { var that = this;
// set the end time when the template is rendered.
// This value should be UTC time as number of milliseconds since epoch.
this.end_time = new Date((new Date()).getTime() + ${timer_expiration_duration});
this.timer_inst = setInterval(function(){ that.update_time(that); }, 1000); this.timer_inst = setInterval(function(){ that.update_time(that); }, 1000);
}, },
end : function(self) { end : function(self) {
...@@ -109,7 +110,7 @@ ...@@ -109,7 +110,7 @@
</%block> </%block>
% if timer_expiration_datetime: % if timer_expiration_duration:
<div class="timer-main"> <div class="timer-main">
<div id="timer_wrapper"> <div id="timer_wrapper">
% if timer_navigation_return_url: % if timer_navigation_return_url:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment