views:

158

answers:

2

I'm trying to create a web based test (as in exam) application with CakePHP.

When a test is started the webserver stores the exact start time in session. When the test ended and the client submit the answers to the webserver, the webserver will recheck the submission time with the previous start time. If the total test time is within a specified error margin (e.g. 5 min), the test result will be accepted.

I've done this part already and currently using the jQuery Countdown Plugin to display the timer in the client (browser). This timer is also used to automatically submit the form if it's expired.

The problem is refreshing the page will reset the timer. Going back and forward will also do so (as it's essentially refreshing).

Altough the total test time taken is recorded and verified in the server (if a user tampered with the timer and submitted the test, the system will reject it), is there anyway to prevent this or at least give a warning, or even better, submit the test automatically in the event of user tampering (which of course will be rejected)?

I know I might be able to do that by using AJAX (still a beginner in this), but the higher-ups told me not do to so because of bandwidth limitation (which I don't think so). So... anyone?

Thanks before!

+1  A: 

You'll need to get server current time and test start time. Subtract both and you'll get how long user already spent into that test. Now, just subtract this value from your time limit (5 min) and you have remaining time; values less than zero means test time ended.

Rubens Farias
A: 

First, it's not so much the ajax that's required, as it is the concept of having the server set the client's timer on a page load. Yes, you could do this via ajax -- have the client request the current time on document.ready(). However, unless you need additional synchronization features (which opens up a whole nother can of worms, truthfully), there's no reason to make a separate http request via ajax when you can just set this value from the original server-supplied page.

So, if your page is /exams/page, in the ExamsController::page() method, you can do something like this:

$this->set( 'cur_time', date( 'N-d-Y', strtotime() );

Then in the view that you render, just have it do something like this:

<?php echo $cur_time; ?>

Wrap that snippet in a javascript code block that will set a JS variable to prime the jQuery counter, which can start counting again once the page has finished loading. No extra bandwidth here!

Travis Leleu
well, actually it's that synchronization feature that I want :) Have done this already tought... Thanks anyway!
Furuno
Do you mean synchronization in terms of same ending time, or do you want to re-synch while the page is laoded. The latter is what I was referring to, and it really would only apply if you're application was real-time (rather than just trying to limit the time spent on an exam). Basically, if you think the two clocks could drift during a page, then you might want to resynch. Otherwise I'd just trust the clients' clock, and not worry about synching within a page (of course, remember to check on the server whether submission should be allowed, dont trust the client JS to do it).
Travis Leleu