Skip to main content

Award points with the Moodle web services API

This feature requires Level Up XP+ Premium.

You can use local_xp_award_points in the Moodle web services API to award points from an external system.

Your XP+ plan must include API support. You also need a Moodle web service and a token. To create a service, create a web service.

Award points

The local_xp_award_points function requires these arguments:

ArgumentTypeDescription
useridIntegerThe ID of the Moodle user who receives the points.
pointsIntegerThe number of points to award. The value must be greater than 0.
courseidIntegerThe course ID. If you use XP sitewide, use 1.

If you do not know the userid, find the user ID before you award the points.

info

This page uses Moodle's standard REST protocol. If you use a different web service protocol, see its documentation.

Send a POST request to this URL:

https://moodle.example.com/webservice/rest/server.php

Replace https://moodle.example.com with the address of your Moodle site. Encode the request data as application/x-www-form-urlencoded. Do not encode the request data as JSON.

Include these form fields:

  • Set wstoken to your web service token.
  • Set wsfunction to local_xp_award_points.
  • Set moodlewsrestformat to json.
  • Set userid, points, and courseid to the required values.

Example request

This example awards 10 points to user 20 in course 30. Replace YOUR_TOKEN with your web service token.

http -f POST https://moodle.example.com/webservice/rest/server.php \
wstoken=YOUR_TOKEN \
wsfunction=local_xp_award_points \
moodlewsrestformat=json \
points=10 \
userid=20 \
courseid=30

Get HTTPie CLI.

Response

Moodle returns the user's new points total in xp. In this example, the new total is 1200:

{
"xp": 1200
}

Find a user ID

The award function requires the numeric Moodle user ID. If you know another identifier, use core_user_get_users_by_field to find the ID.

Set field to id, username, idnumber, or email. The values argument must always be an array. For one value, send one array item.

This example finds the user with the username admin:

http -f POST https://moodle.example.com/webservice/rest/server.php \
wstoken=YOUR_TOKEN \
wsfunction=core_user_get_users_by_field \
moodlewsrestformat=json \
field=username \
values[]=admin

Moodle returns only the users and fields that the user calling the web service can view. Moodle can return an empty array if the specified value does not match a user or if that user cannot view the selected field. This shortened response shows the values that you need:

[
{
"id": 2,
"username": "admin"
}
]

Use the id value from the matching object as userid. In this example, the userid is 2.

Create a web service

These steps create a new service that can award points:

  1. Enable Moodle web services and the web service protocol that you want to use, see the Moodle documentation.
  2. Go to Site administration > Server > Web services > External services, and then select Add.
  3. Enter a name, select Enabled and Authorised users only, and then select Add service.
  4. Select Add functions, and add local_xp_award_points.
  5. Add core_user_get_users_by_field if you want to find user IDs with the API.
  6. Return to External services, select Authorised users for the new service, and add the user who will call the web service.
  7. Get a web service token for the user who will call the web service.

For complete setup instructions, see Using web services in the Moodle documentation.

Required capabilities

CapabilityRequired forScope or details
block/xp:earnxpPoints recipientIn the course context, or the system context when XP is used sitewide.
block/xp:manageUser calling the web serviceIn the course context, or the system context when XP is used sitewide.
Web service protocol capabilityUser calling the web serviceFor REST, use webservice/rest:use. Check the documentation for other protocols.
Capabilities for core_user_get_users_by_fieldUser calling the web serviceMoodle lists the required capabilities on the service's Functions page.