Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

Support for external API

Copyright: 2009 Petr Skodak
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 222 lines (10 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 8 functions


Functions that are not part of a class:

external_generate_token($tokentype, $serviceorid, $userid, $contextorid, $validuntil = 0, $iprestriction = '')   X-Ref
Generate a token

param: string $tokentype EXTERNAL_TOKEN_EMBEDDED|EXTERNAL_TOKEN_PERMANENT
param: stdClass|int $serviceorid service linked to the token
param: int $userid user linked to the token
param: stdClass|int $contextorid
param: int $validuntil date when the token expired
param: string $iprestriction allowed ip - if 0 or empty then all ips are allowed
return: string generated token

external_create_service_token($servicename, $contextid)   X-Ref
Create and return a session linked token. Token to be used for html embedded client apps that want to communicate
with the Moodle server through web services. The token is linked to the current session for the current page request.
It is expected this will be called in the script generating the html page that is embedding the client app and that the
returned token will be somehow passed into the client app being embedded in the page.

param: string $servicename name of the web service. Service name as defined in db/services.php
param: int $context context within which the web service can operate.
return: int returns token id.

external_delete_descriptions($component)   X-Ref
Delete all pre-built services (+ related tokens) and external functions information defined in the specified component.

param: string $component name of component (moodle, etc.)

external_validate_format($format)   X-Ref
Validate text field format against known FORMAT_XXX

param: array $format the format to validate
return: the validated format

external_format_string($str, $context, $striplinks = true, $options = [])   X-Ref
Format the string to be returned properly as requested by the either the web service server,
either by an internally call.
The caller can change the format (raw) with the external_settings singleton
All web service servers must set this singleton when parsing the $_GET and $_POST.

<pre>
Options are the same that in {@link format_string()} with some changes:
filter      : Can be set to false to force filters off, else observes {@link external_settings}.
</pre>

param: string $str The string to be filtered. Should be plain text, expect
param: boolean $striplinks To strip any link in the result text. Moodle 1.8 default changed from false to true! MDL-8713
param: context|int $contextorid The id of the context for the string or the context (affects filters).
param: array $options options array/object or courseid
return: string text

external_format_text($text, $textformat, $context, $component = null, $filearea = null, $itemid = null, $options = null)   X-Ref
Format the text to be returned properly as requested by the either the web service server,
either by an internally call.
The caller can change the format (raw, filter, file, fileurl) with the external_settings singleton
All web service servers must set this singleton when parsing the $_GET and $_POST.

<pre>
Options are the same that in {@link format_text()} with some changes in defaults to provide backwards compatibility:
trusted     :   If true the string won't be cleaned. Default false.
noclean     :   If true the string won't be cleaned only if trusted is also true. Default false.
nocache     :   If true the string will not be cached and will be formatted every call. Default false.
filter      :   Can be set to false to force filters off, else observes {@link external_settings}.
para        :   If true then the returned string will be wrapped in div tags. Default (different from format_text) false.
Default changed because div tags are not commonly needed.
newlines    :   If true then lines newline breaks will be converted to HTML newline breaks. Default true.
context     :   Not used! Using contextid parameter instead.
overflowdiv :   If set to true the formatted text will be encased in a div with the class no-overflow before being
returned. Default false.
allowid     :   If true then id attributes will not be removed, even when using htmlpurifier. Default (different from
format_text) true. Default changed id attributes are commonly needed.
blanktarget :   If true all <a> tags will have target="_blank" added unless target is explicitly specified.
</pre>

param: string $text The content that may contain ULRs in need of rewriting.
param: int $textformat The text format.
param: context|int $context This parameter and the next two identify the file area to use.
param: string $component
param: string $filearea helps identify the file area.
param: int $itemid helps identify the file area.
param: object/array $options text formatting options
return: array text + textformat

external_generate_token_for_current_user($service)   X-Ref
Generate or return an existing token for the current authenticated user.
This function is used for creating a valid token for users authenticathing via login/token.php or admin/tool/mobile/launch.php.

param: stdClass $service external service object
return: stdClass token object

external_log_token_request($token)   X-Ref
Set the last time a token was sent and trigger the \core\event\webservice_token_sent event.

This function is used when a token is generated by the user via login/token.php or admin/tool/mobile/launch.php.
In order to protect the privatetoken, we remove it from the event params.

param: stdClass $token token object