Can not find data record in database table external_functions

Can not find data record in database table external_functions

by Charan Sri -
Number of replies: 2

Hello All,

I am trying to write a custom web service in Moodle. I took local_wstemplates_hello_world as an example and trying to implement a webservice for my custom plugin.


I have added three methods as below in externallib.php file:

1) function coupons_parameters()

2) function coupons()

3) function coupons_returns()


In Services.php file. I have added code as below:

$services = array(

        'My service' => array(

                'functions' => array ('local_wstemplate_hello_world', 'local_wstemplate_coupons'),

                'restrictedusers' => 0,

                'enabled'=>1,

        )

);


$functions = array(

        'local_wstemplate_hello_world' => array(

                'classname'   => 'local_wstemplate_external',

                'methodname'  => 'hello_world',

                'classpath'   => 'local/wstemplate/externallib.php',

                'description' => 'Return Hello World FIRSTNAME. Can change the text (Hello World) sending a new text as parameter',

                'type'        => 'read',

        ),

'local_wstemplate_coupons' => array(

        'classname'   => 'local_wstemplate_external',

        'methodname'  => 'coupons',

        'classpath'   => 'local/wstemplate/externallib.php',

        'description' => 'Return coupons details',

        'type'        => 'read'

    )

);

Now, When I want to call the method 'coupons()' from a webservice using wsfunction = local_wstemplate_coupons.


It is showing error as follows:

stdClass Object
(
    [exception] => dml_missing_record_exception
    [errorcode] => invalidrecord
    [message] => Can not find data record in database table external_functions.
    [debuginfo] => SELECT * FROM {external_functions} WHERE name = ?
[array (
  0 => 'local_wstemplate_coupons',
)]
)

Please help me on how to fix this issue?

Thanks in Advance

Regards

Sricharan


Average of ratings: -
In reply to Charan Sri

Re: Can not find data record in database table external_functions

by Noora C -

To be honest, I'm not sure if anyone has gotten a working prototype up using this template. At least I haven't found one. According to the developer himself on Github:

"I agree these examples should be polished out a bit. It is important to understand that they are not supposed to work as out-of-box tutorials themselves."

He then goes on to mention that it's quite complicated to get a new webservice up and running. After spending several days trying to get it working, I have to agree, as I still haven't got it working.

In reply to Charan Sri

Re: Can not find data record in database table external_functions

by Arshad Syed -

Your webservice is unable to recognize the please try this

folder structure

moodleroot->local->wstemplate->db->service.php

moodleroot->local->wstemplate->externallib.php

moodleroot->local->wstemplate->version.php


services.php

$functions = array(
         'local_wstemplate_hello_world' => array(
                                 'classname' => 'local_wstemplate_external',
                                  'methodname' => 'hello_world',
                                  'classpath' => 'local/wstemplate/externallib.php',
                                  'description' => ' Hello world ws example ',
                                  'type' => 'read',
                                  'capabilities' => '',
                                 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
                    ),
       'local_wstemplate_create_groups' => array(
                                  'classname' => 'local_wstemplate_external',
                                  'methodname' => 'create_groups',
                                    'classpath' => 'local/wstemplate/externallib.php',
                                  'description' => 'creating groups in moodle courses',
                                  'type' => 'read',
                                  'capabilities' => '',
                                   'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
                                  ),
                                
             );

// We define the services to install as pre-build services. A pre-build service is not editable by administrator.
$services = array(
'groups plugin webservices' => array(
'functions' => array ('local_wstemplate_hello_world','local_wstemplate_create_groups'),
                'restrictedusers' => 0,
                'shortname'=>'myservice',
                 'enabled'=>1,
                 'shortname' => 'local_groups_ws'
                   )
);

In externallib.php

<?php

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

class local_wstemplate_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;

    }


 /**

     * Returns description of method parameters

     * @return external_function_parameters

     */

    public static function hello_world_parameters() {

        return new external_function_parameters(

                array('welcomemessage' => new external_value(PARAM_TEXT, 'The welcome message. By default it is "Hello world,"', VALUE_DEFAULT, 'Hello world, '))

        );

    }


    /**

     * Returns welcome message

     * @return string welcome message

     */

    public static function hello_world($welcomemessage = 'Hello world, ') {

        global $USER;


        //Parameter validation

        //REQUIRED

        $params = self::validate_parameters(self::hello_world_parameters(),

                array('welcomemessage' => $welcomemessage));


        //Context validation

        //OPTIONAL but in most web service it should present

        $context = get_context_instance(CONTEXT_USER, $USER->id);

        self::validate_context($context);


        //Capability checking

        //OPTIONAL but in most web service it should present

        if (!has_capability('moodle/user:viewdetails', $context)) {

            throw new moodle_exception('cannotviewprofile');

        }


        return $params['welcomemessage'] . $USER->firstname ;;

    }


    /**

     * Returns description of method result value

     * @return external_description

     */

    public static function hello_world_returns() {

        return new external_value(PARAM_TEXT, 'The welcome message + user first name');

    }


}


version.php

<?php

$plugin->version = 20111012042;
$plugin->requires = 2010112400; // Requires this Moodle version - at least 2.0
$plugin->component = 'local_wstemplate';
$plugin->cron = 0;
$plugin->release = '1.0 (Build: 2011101202)';
$plugin->maturity = MATURITY_STABLE;