External functions API -> Errorcode: invalidparameter

External functions API -> Errorcode: invalidparameter

by Alexander Dominicus -
Number of replies: 4
Picture of Testers

Hello Moodlers,

I'm trying to modify an existing plugin

https://github.com/kulmann/moodle-mod_philosophers

in ordert to get control over the order questions are asked to the gamer.


The plugin uses the external function API external function API in order to connect functions from the fronted of the plugin, which is written in vue.js to the PHP-code in the moodle core.

The problem is restricted to two main files:

gamesession.php:

First important function here

public static function get_question($coursemoduleid, $gamesessionid, $levelid, $givenchapter)
        $params = [
         coursemoduleid' => $coursemoduleid,
            'gamesessionid' => $gamesessionid,
            'levelid' => $levelid,
            'mychapter' => $givenchapter
        ];
       self::validate_parameters(self::get_question_parameters(), $params);

[....]

}


Next important function here

  public static function get_question_parameters() {
        return new external_function_parameters([
          coursemoduleid' => new external_value(PARAM_INT, 'course module id'),
            'gamesessionid' => new external_value(PARAM_INT, 'game session id'),
            'levelid' => new external_value(PARAM_INT, 'id of the level'),
            'mychapter' => new external_value(PARAM_INT, 'chapter given')
        ]);
    }


And the store.js file:

    async fetchQuestion(context, levelId, givenChapter) {
            let args = {
                gamesessionid: this.state.gameSession.id,
                levelid: levelId,
                givenchapter:  givenChapter
            };
          const question = await ajax('mod_philosophers_get_question_depending_on_chapter', args);
        [...]
        }


In db/services.php I have added the function "mod_philosophers_get_question_depending_on_chapter".


When I'm running the programm the following errocode appears:

Missing required key in single structure: mychapter
Error code: invalidparameter
* line 352 of /lib/externallib.php: invalid_parameter_exception thrown
* line 243 of /lib/externallib.php: call to external_api::validate_parameters()
* line 81 of /lib/ajax/service.php: call to external_api::call_external_function()


Unfortunately, to me it is absolutely not clear what the errocode means:

What is a "key" is this context? Do I have to initialise the variable "mychapter" in some way?

Do I have to edit the function get_question_parameters()? Since in my opinion the errorcode is thrown by this function.


Thank you very much in advance for your help!


Best,

Alex






Average of ratings: -
In reply to Alexander Dominicus

Re: External functions API -> Errorcode: invalidparameter

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
According to get_question_parameters(), the expected parameters for the webservice function are:
  • coursemoduleid
  • gamesessionid
  • levelid
  • mychapter

All of these parameters are required and all of these declare they of type "integer".

Your javascript function appears to send these 3 parameters:

  • gamesessionid (correct)
  • levelid (also correct)
  • givenchapter (looks like this should be 'mychapter'?)

You don't appear to be sending 'coursemoduleid' at all (unless there is some other code that you haven't shared which is slotting that in before the request is sent).


In reply to Davo Smith

Re: External functions API -> Errorcode: invalidparameter

by Alexander Dominicus -
Picture of Testers
Hey Davo,
thank you for your reply. There's a wrapper which gives the coursemoduleid:

/**
* Wrapper for ajax call to Moodle.
*/
export async function ajax(method, args) {
const request = {
methodname: method,
args: Object.assign({
coursemoduleid: store.state.courseModuleID
}, args),
};

try {
return await moodleAjax.call([request])[0];
} catch (e) {
Notification.exception(e);
throw e;
}
}


About the naming: originally "mychapter" was named "chapter". But for debugging I renamed this variable since I supposed that there could be a conflict with the vue.js-store which has also a state called "chapter".


Can you say anything about the meaningof the mentioned "keys"?

Best,
Alex
In reply to Alexander Dominicus

Re: External functions API -> Errorcode: invalidparameter

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Yes, the "keys" are names of the items parameter structure.

So, the function call expects a single structure with the "keys" (item names) "coursemoduleid", "gamesessionid", "levelid" and "mychapter".

The error message clearly states that the parameter structure provided by your code is missing the "key" called "mychapter" (which it is, because it is sending something called "givenchapter" instead).
In reply to Davo Smith

Re: External functions API -> Errorcode: invalidparameter

by Alexander Dominicus -
Picture of Testers
Hi Davo,

tahnk you very much. Now, the code is working.
I added a
let chapter = 1;
To the function-definiton which is clicked by the user and removed the "chapter"-variable from it. The chapter variable is then passed through the function which is calling the functions in the gamesession.php file.

I'm not perfetcly sure why, but now it is running as I expected it smile.

Best,
Alex