How can I refresh browser sessions without refreshing the page?

How can I refresh browser sessions without refreshing the page?

by Matt Bury -
Number of replies: 3
Picture of Plugin developers

Hi,

I'm sure we're all familiar with the issue of online learning activities taking longer than the $CFG->sessiontimeout variable allows: the learner clicks next or submit only to be met by a login page and their work may be lost.

Is there a way to refresh the browser session without refreshing the page, e.g. an API that can be called with JS or from a web client?

My idea is to monitor keyboard and mouse use. As long as they're active, i.e. someone's working on the page, the client keeps the session alive by refreshing it whenever it gets near to the $CFG->sessiontimeout (on a timer). If they're inactive and it gets close to the end on the session, the client sounds an alarm. If the user doesn't respond, it times out. If there's mouse/keyboard activity, it refreshes the session.

Does that make sense? Is there a function I could call to do this without leaving the page or opening a new page?

Thanks in advance smile

Average of ratings: Useful (1)
In reply to Matt Bury

Re: How can I refresh browser sessions without refreshing the page?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

As far as I'm aware, an AJAX call to a PHP script that simply calls 'require_once('/path/to/config.php'); require_login();' should do that. (probably with a define('AJAX_SCRIPT', true); at the top of it)

The Y.io function would be the best way to make that call.

In reply to Matt Bury

Re: How can I refresh browser sessions without refreshing the page?

by Alfonso Roman -

I've implemented this as a YUI module (inside our local plugin) that prevents form submission if session timed out.  The form submission is stopped and an ajax call is made to the server to check that the user's session is still valid.  If the session is valid, then the form submits, otherwise the user gets a login prompt and allowed to submit the form once they've logged in again. 

We've activated this only for forums, but it's easily extendable to work for all moodle forms.  The code for this is available in this gist:

https://gist.github.com/alroman/5715465