Quantcast
Channel: Web services
Viewing all articles
Browse latest Browse all 2361

Custom webservice function returns invalidparameter null

$
0
0

by Alex Martínez.  

Hi,


I'm trying to create my own custom function for webservice using this tutorial:

https://docs.moodle.org/dev/Adding_a_web_service_to_a_plugin


But when i call it it shows this error:

{"exception":"invalid_parameter_exception","errorcode":"invalidparameter","message":"Detectado valor de par\u00e1metro no v\u00e1lido","debuginfo":null}

I'm calling this way:


<?php

$token = '48b69d293d73a973997d5f95';

$functionname = 'local_myplugin_create_groups';

$restformat = 'json';



$group = new stdClass();

$group->name = 'Nou grup prueba';

$group->courseid = 8;

$group->description='grupo prueba función';

$group->enrolmentkey='dadss';


$groups = array( $group);

$params = array('groups' => $groups);


/// REST CALL

header('Content-Type: text/plain');

$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;

require_once('./curl.php');

$curl = new curl;


$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';

$resp = $curl->post($serverurl . $restformat, $params);

print_r($resp);

?>


And my externallib.php :


require_once($CFG->libdir . "/externallib.php");


class local_myplugin_external extends external_api {

 

    /**

     * Returns description of method parameters

     * @return external_function_parameters

     */

    public static function create_groups_parameters() {

        return new external_function_parameters(

            array(

                'groups' => new external_multiple_structure(

                    new external_single_structure(

                        array(

                            'courseid' => new external_value(PARAM_INT, 'id of course'),

                            'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),

                            'description' => new external_value(PARAM_RAW, 'group description text'),

                            'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),

                        )

                    )

                )

            )

        );

    }

    

    public static function create_groups_returns() {

        return new external_multiple_structure(

            new external_single_structure(

                array(

                    'id' => new external_value(PARAM_INT, 'group record id'),

                    'courseid' => new external_value(PARAM_INT, 'id of course'),

                    'name' => new external_value(PARAM_TEXT, 'multilang compatible name, course unique'),

                    'description' => new external_value(PARAM_RAW, 'group description text'),

                    'enrolmentkey' => new external_value(PARAM_RAW, 'group enrol secret phrase'),

                )

            )

        );

    }

    

    /**

     * Create groups

     * @param array $groups array of group description arrays (with keys groupname and courseid)

     * @return array of newly created groups

     */

    public static function create_groups($groups) { //Don't forget to set it as static

        global $CFG, $DB;

        require_once("$CFG->dirroot/group/lib.php");

 

        $params = self::validate_parameters(self::create_groups_parameters(), array('groups'=>$groups));

 

        $transaction = $DB->start_delegated_transaction(); //If an exception is thrown in the below code, all DB queries in this code will be rollback.

 

        $groups = array();

 

        foreach ($params['groups'] as $group) {

            $group = (object)$group;

 

            if (trim($group->name) == '') {

                throw new invalid_parameter_exception('Invalid group name');

            }

            if ($DB->get_record('groups', array('courseid'=>$group->courseid, 'name'=>$group->name))) {

                throw new invalid_parameter_exception('Group with the same name already exists in the course');

            }

 

            // now security checks

            $context = get_context_instance(CONTEXT_COURSE, $group->courseid);

            self::validate_context($context);

            require_capability('moodle/course:managegroups', $context);

 

            // finally create the group

            $group->id = groups_create_group($group, false);

            $groups[] = (array)$group;

        }

 

        $transaction->allow_commit();

 

        return $groups;

    }

       

}



Anyone can help me?


Thanks


Viewing all articles
Browse latest Browse all 2361

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>