Hello everyone,
I'm trying to create a block plugin for moodle which gets continual updates via ajax calls.
To this end I have tried employing built in functionality in moodle: Requesting lib/ajax/blocks.php in order to get the contents of one particular block. But this functionality seems to not be implemented (I deduce this from reading the source code of lib/ajax/blocks.php Please correct me if I'm wrong).
My next step was to try to create my own PHP script which would cater to my javascript code and provide the block contents in JSON format. However, very early I got stuck with the following problem:
{"error":"Incorrect sesskey submitted, form not accepted!"}
For a very simple PHP script which has exactly three lines:
<?php
define('AJAX_SCRIPT', true);
require_once(dirname(__FILE__) . '/../../config.php');
require_sesskey();
Thinking I might've messed up the javascript code, I decided to try to query another AJAX_SCRIPT from moodle, namely, comment/comment_ajax.php. This, however worked (well, it passed the session test and failed somewhere else, but the point is the session test worked).
Desperate to find the problem, I copied (verbatim) the source file from comment/comment_ajax.php into my block-plugin directory, changed only the include paths so that they would work, and BOOM, getting "Incorrect sesskey submitted" again. Seems like the path where the AJAX_SCRIPT sits somehow affects the session verification???
I even made a duplicate of comment/comment_ajax2.php (same file exactly, just a different path) and it passed the session test. It's as if there is something about my block-plugin directory that moodle doesn't want to recognize as a valid session.
Does anyone have any idea what I'm doing wrong?
In case anyone wants to look, here are the source files:
https://github.com/jacobshapiro/moodle-block_eduappup/tree/ajax
(note: I know the javascript file is a mess and I will fix that, but as I explained above, it is probably not the cause to the session problem)
Thank you very much in advance!