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;