Moodle Error - Web Services Client - No access rights in module context

809 views Asked by At

I have got the following setup in Moodle:

  • I have enabled Web Services
  • I have enabled the REST protocol
  • I have added a service called grades and enabled it
  • I have added the function mod_assign_get_grades to the service
  • I have assigned the webservice user as an authorized user for the service
  • I have also created and assigned a role to the user that will allow the user of the rest protocol
  • I have also created a token for the user and service

I have used the code below to make a request to the API:

/// SETUP - NEED TO BE CHANGED
$token = '068183c4c700bdcfe2b0bfe24a8043e2';
$domainname = 'http://localhost/Project/moodle';
$functionname = 'mod_assign_get_grades';

// REST RETURNED VALUES FORMAT
$restformat = 'xml'; //Also possible in Moodle 2.2 and later: 'json'
                 //Setting it to 'json' will fail all calls on earlier Moodle version

/// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
$params = array('assignmentids' => array(1));

/// REST CALL
header('Content-Type: text/plain');
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');
$curl = new curl;
//if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
$resp = $curl->post($serverurl . $restformat, $params);
print_r($resp);

Upon execution I get the following error:

No access rights in module context

As far as I can tell I have assigned the appropriate capabilities and permissions and don't know where I'm going wrong?

1

There are 1 answers

0
Usaamah Patel On

Looks like the web service user I created did not have the privileges to access student grades. Using the admin account that I created when I used moodle I am able to run the script and return a response.