diff --git a/perguntations/app.py b/perguntations/app.py index 2601430..eb9fd2a 100644 --- a/perguntations/app.py +++ b/perguntations/app.py @@ -257,6 +257,17 @@ class App(): return test # ------------------------------------------------------------------------ + def event_test(self, uid, cmd, value): + '''handle browser events the occur during the test''' + if cmd == 'focus': + logger.info('Student %s: focus %s', uid, value) + elif cmd == 'size': + scr_y, scr_x, win_y, win_x = value + area = win_x * win_y / (scr_x * scr_y) * 100 + logger.info('Student %s: area=%g%%, window=%dx%d, screen=%dx%d', + uid, area, win_x, win_y, scr_x, scr_y) + + # ------------------------------------------------------------------------ # --- helpers (getters) # def get_student_name(self, uid): diff --git a/perguntations/serve.py b/perguntations/serve.py index 8a7e6b8..3919ab0 100644 --- a/perguntations/serve.py +++ b/perguntations/serve.py @@ -154,11 +154,18 @@ class BaseHandler(tornado.web.RequestHandler): # AdminSocketHandler.send_updates(chat) # send to clients class StudentWebservice(BaseHandler): + ''' + Receive ajax from students in the test: + focus, unfocus + ''' + @tornado.web.authenticated - async def post(self): + def post(self): + '''handle ajax post''' uid = self.current_user cmd = self.get_body_argument('cmd', None) - logging.info('%s %s', uid, cmd) + value = json.loads(self.get_body_argument('value', None)) + self.testapp.event_test(uid, cmd, value) # ---------------------------------------------------------------------------- class AdminHandler(BaseHandler): diff --git a/perguntations/static/js/detect_unfocus.js b/perguntations/static/js/detect_unfocus.js index 0799934..f109e84 100644 --- a/perguntations/static/js/detect_unfocus.js +++ b/perguntations/static/js/detect_unfocus.js @@ -12,28 +12,43 @@ jQuery.postJSON = function(url, args) { $(document).ready(function() { + $.postJSON("/studentwebservice", { + "cmd": "size", + "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), + }); + $(window).focus(function(){ $.postJSON("/studentwebservice", { "cmd": "focus", + "value": true, }); }); $(window).blur(function(){ $.postJSON("/studentwebservice", { - "cmd": "unfocus", + "cmd": "focus", + "value": false, }); }); - // $(window).resize(function(){ - // var n = $(window).scrollTop(); - // $.ajax({ - // type: "POST", - // url: "/adminwebservice", - // data: { - // "cmd": "resize", - // "name": $("#number").text(), - // "scroll": n - // } - // }); - // }); -}); \ No newline at end of file + $(window).resize(function(){ + $.postJSON("/studentwebservice", { + "cmd": "size", + "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), + }); + + + // // var n = $(window).scrollTop(); + // $.postJSON({"/studentwebservice", { + // "cmd": "size", + // "value": JSON.stringify([screen.height, screen.width, $(window).height(), $(window).width()]), + // }}); + // // data: { + // // "cmd": "resize", + // // "name": $("#number").text(), + // // "scroll": n + // // } + // // }); + }); + +}); -- libgit2 0.21.2