Moodle
  1. Moodle
  2. MDL-39246

Better synchronise times on the server with times in the browser (e.g. quiz timer)

    Details

    • Rank:
      49857

      Description

      If you try to initialise a timer in the browser by using yui_module or js_init_call, then there the timer will not acutally start running until those script files have loaded, which could be a delay of several seconds.

      It would be better to capture the current time ASAP in <head> using some inline JS like
      M.pageloadstarttime = Date();

      That lets client-side JS get a timestamp that is as close as possible to when Moodle served the page.

      We need this in the quiz module to set make the count-down timer as accurate as possible.

        Activity

        Hide
        Tim Hunt added a comment -
        Show
        Tim Hunt added a comment - Dev docs probably need to go http://docs.moodle.org/dev/JavaScript_usage_guide#Description_of_M_namespace
        Hide
        Tim Hunt added a comment -

        OK, here is my proposed implementation. I have done separate commits for the changes in lib/outputrequirementslib.php, and the use of this in the quiz.

        I have added Petr as a watcher becuase I thought he might be interested in this, and because if he has time to comment I would be interested to know what he thinks.

        I am asking Andrew to peer review this, as Ajax component lead.

        My inclination is to try to back-port this. I have seen some vaguely worrying reports in the quiz forum which I guess might be time problems caused by slowly loading JavaScript.

        Show
        Tim Hunt added a comment - OK, here is my proposed implementation. I have done separate commits for the changes in lib/outputrequirementslib.php, and the use of this in the quiz. I have added Petr as a watcher becuase I thought he might be interested in this, and because if he has time to comment I would be interested to know what he thinks. I am asking Andrew to peer review this, as Ajax component lead. My inclination is to try to back-port this. I have seen some vaguely worrying reports in the quiz forum which I guess might be time problems caused by slowly loading JavaScript.
        Hide
        Andrew Nicols added a comment -

        This gets my +1.

        I guess the backport will have to go as a separate issue (or not) once this has landed according to the new backporting policy?

        As an aside and quiz suggestion, it would be good if there was a warning like the formchangechecker when you try to leave an in-progress timed quiz. It should be possible to just call M.core_formchangechecker.set_form_changed() from the init function, though the message where shown won't really be quite so relevant.

        Show
        Andrew Nicols added a comment - This gets my +1. I guess the backport will have to go as a separate issue (or not) once this has landed according to the new backporting policy? As an aside and quiz suggestion, it would be good if there was a warning like the formchangechecker when you try to leave an in-progress timed quiz. It should be possible to just call M.core_formchangechecker.set_form_changed() from the init function, though the message where shown won't really be quite so relevant.
        Hide
        Tim Hunt added a comment -

        OK, submitting for integration.

        Since I have seen timing-related problems in the forums that I think are caused by this, I have backported to 2.4.

        Show
        Tim Hunt added a comment - OK, submitting for integration. Since I have seen timing-related problems in the forums that I think are caused by this, I have backported to 2.4.
        Hide
        Damyon Wiese added a comment -

        Thanks Tim,

        I have 2 questions:

        1. If we want the time at the start of the request wouldn't it be better to use: $_SERVER['REQUEST_TIME']. If you do this - you can keep it contained in the quiz.
        2. Why does it make sense to start the timer when the request starts (e.g. If I have 1 minute to complete a quiz and it takes 15 seconds to load the page and all the js, should I have only 45 seconds left?)
        Show
        Damyon Wiese added a comment - Thanks Tim, I have 2 questions: If we want the time at the start of the request wouldn't it be better to use: $_SERVER ['REQUEST_TIME'] . If you do this - you can keep it contained in the quiz. Why does it make sense to start the timer when the request starts (e.g. If I have 1 minute to complete a quiz and it takes 15 seconds to load the page and all the js, should I have only 45 seconds left?)
        Hide
        Tim Hunt added a comment -

        Note that timing for the quiz is enforced by the clock on the Moodle server. The end time for a quiz is either quiz close date or quiz attempt start time + time limit.

        The client-side count-down timer is a hint to students, to help them submit the quiz before time expires. For that to be useful, the time remaining it shows has to closely correspond to the time on the server.

        (So, yes, slow servers / internet will mean that the student has less time. There is nothing we can do about that.)

        1. Given the above explanation, I think you can see the answer to this is No. In fact, given you asked this question, I can't help feeling you failed to understand what we were trying to achieve with this change.

        2. You cannot trust the timing done on the student's PC. It would be like having a three hours exam in the student's own room, that starts at 9:00am, and we tell the student that they have to stop before the clock on the wall says noon. The student can just keep setting the time back to 11:00am indefinitely, and take as long as they like over the quiz.

        Show
        Tim Hunt added a comment - Note that timing for the quiz is enforced by the clock on the Moodle server. The end time for a quiz is either quiz close date or quiz attempt start time + time limit. The client-side count-down timer is a hint to students, to help them submit the quiz before time expires. For that to be useful, the time remaining it shows has to closely correspond to the time on the server. (So, yes, slow servers / internet will mean that the student has less time. There is nothing we can do about that.) 1. Given the above explanation, I think you can see the answer to this is No. In fact, given you asked this question, I can't help feeling you failed to understand what we were trying to achieve with this change. 2. You cannot trust the timing done on the student's PC. It would be like having a three hours exam in the student's own room, that starts at 9:00am, and we tell the student that they have to stop before the clock on the wall says noon. The student can just keep setting the time back to 11:00am indefinitely, and take as long as they like over the quiz.
        Hide
        Damyon Wiese added a comment -

        Hi Tim,

        Yes - sorry I twigged now. This does seem to be the only way to make it work for all browsers. Will re-look at this patch in a minute.

        Show
        Damyon Wiese added a comment - Hi Tim, Yes - sorry I twigged now. This does seem to be the only way to make it work for all browsers. Will re-look at this patch in a minute.
        Hide
        Damyon Wiese added a comment -

        Thanks Tim,

        Changed the issue to a bug and have integrated it to 24 and master branches. I tested it fully in integration so will pass this.

        Show
        Damyon Wiese added a comment - Thanks Tim, Changed the issue to a bug and have integrated it to 24 and master branches. I tested it fully in integration so will pass this.
        Hide
        Damyon Wiese added a comment -

        Tested on all branches in integration and the test instructions pass.

        Show
        Damyon Wiese added a comment - Tested on all branches in integration and the test instructions pass.
        Hide
        Tim Hunt added a comment -

        Thanks Damyon.

        Show
        Tim Hunt added a comment - Thanks Damyon.
        Hide
        Tim Hunt added a comment -

        New dev docs at http://docs.moodle.org/dev/JavaScript_guidelines#Accessing_useful_information. I don't think that is great. I am hoping that Andrew can find a better home for that as he continues to refactor the JS docs.

        Show
        Tim Hunt added a comment - New dev docs at http://docs.moodle.org/dev/JavaScript_guidelines#Accessing_useful_information . I don't think that is great. I am hoping that Andrew can find a better home for that as he continues to refactor the JS docs.
        Hide
        Andrew Nicols added a comment -

        Yes - hopefully I'll find a sane way of documenting the JS generated by PHP - most likely in a fake module lib lib/yui/src/core or something appropriate.

        Show
        Andrew Nicols added a comment - Yes - hopefully I'll find a sane way of documenting the JS generated by PHP - most likely in a fake module lib lib/yui/src/core or something appropriate.
        Hide
        Dan Poltawski added a comment -

        Thanks! You're changes are now spread to the world through this git and our source control repositories.

        No time to rest though, we've got days to make 2.5 the best yet!

        ciao

        Show
        Dan Poltawski added a comment - Thanks! You're changes are now spread to the world through this git and our source control repositories. No time to rest though, we've got days to make 2.5 the best yet! ciao

          People

          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: