Merge branch 'MDL-39790_master' of git://github.com/totara/openbadges into MOODLE_25_...
[moodle.git] / webservice / externallib.php
blobc9d1ab5ec2ee353acdf0750227748c291b5e480d
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * external API for mobile web services
21 * @package core_webservice
22 * @category external
23 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die;
29 require_once("$CFG->libdir/externallib.php");
31 /**
32 * Web service related functions
34 * @package core_webservice
35 * @category external
36 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.2
40 class core_webservice_external extends external_api {
42 /**
43 * Returns description of method parameters
45 * @return external_function_parameters
46 * @since Moodle 2.2
48 public static function get_site_info_parameters() {
49 return new external_function_parameters(
50 array('serviceshortnames' => new external_multiple_structure (
51 new external_value(
52 PARAM_ALPHANUMEXT,
53 'service shortname'),
54 'DEPRECATED PARAMETER - it was a design error in the original implementation. \
55 It is ignored now. (parameter kept for backward compatibility)',
56 VALUE_DEFAULT,
57 array()
63 /**
64 * Return user information including profile picture + basic site information
65 * Note:
66 * - no capability checking because we return only known information about logged user
68 * @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored -
69 * it was an original design error, we keep for backward compatibility.
70 * @return array site info
71 * @since Moodle 2.2
73 public static function get_site_info($serviceshortnames = array()) {
74 global $USER, $SITE, $CFG, $DB;
76 $params = self::validate_parameters(self::get_site_info_parameters(),
77 array('serviceshortnames'=>$serviceshortnames));
79 $profileimageurl = moodle_url::make_pluginfile_url(
80 context_user::instance($USER->id)->id, 'user', 'icon', null, '/', 'f1');
82 // Site information.
83 $siteinfo = array(
84 'sitename' => $SITE->fullname,
85 'siteurl' => $CFG->wwwroot,
86 'username' => $USER->username,
87 'firstname' => $USER->firstname,
88 'lastname' => $USER->lastname,
89 'fullname' => fullname($USER),
90 'lang' => current_language(),
91 'userid' => $USER->id,
92 'userpictureurl' => $profileimageurl->out(false)
95 // Retrieve the service and functions from the web service linked to the token
96 // If you call this function directly from external (not a web service call),
97 // then it will still return site info without information about a service
98 // Note: wsusername/wspassword ws authentication is not supported.
99 $functions = array();
100 if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
101 $token = optional_param('wstoken', '', PARAM_ALPHANUM);
103 if (!empty($token)) { // No need to run if not a ws call.
104 // Retrieve service shortname.
105 $servicesql = 'SELECT s.*
106 FROM {external_services} s, {external_tokens} t
107 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
108 $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
110 $siteinfo['downloadfiles'] = $service->downloadfiles;
112 if (!empty($service)) {
113 // Return the release and version number for web service users only.
114 $siteinfo['release'] = $CFG->release;
115 $siteinfo['version'] = $CFG->version;
116 // Retrieve the functions.
117 $functionssql = "SELECT f.*
118 FROM {external_functions} f, {external_services_functions} sf
119 WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
120 $functions = $DB->get_records_sql($functionssql, array($service->id));
121 } else {
122 throw new coding_exception('No service found in get_site_info: something is buggy, \
123 it should have fail at the ws server authentication layer.');
128 // Build up the returned values of the list of functions.
129 $componentversions = array();
130 $availablefunctions = array();
131 foreach ($functions as $function) {
132 $functioninfo = array();
133 $functioninfo['name'] = $function->name;
134 if ($function->component == 'moodle' || $function->component == 'core') {
135 $version = $CFG->version; // Moodle version.
136 } else {
137 $versionpath = get_component_directory($function->component).'/version.php';
138 if (is_readable($versionpath)) {
139 // We store the component version once retrieved (so we don't load twice the version.php).
140 if (!isset($componentversions[$function->component])) {
141 include($versionpath);
142 $componentversions[$function->component] = $plugin->version;
143 $version = $plugin->version;
144 } else {
145 $version = $componentversions[$function->component];
147 } else {
148 // Function component should always have a version.php,
149 // otherwise the function should have been described with component => 'moodle'.
150 throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
153 $functioninfo['version'] = $version;
154 $availablefunctions[] = $functioninfo;
157 $siteinfo['functions'] = $availablefunctions;
159 // Mobile CSS theme and alternative login url
160 $siteinfo['mobilecssurl'] = get_config('admin', 'mobilecssurl');
162 return $siteinfo;
166 * Returns description of method result value
168 * @return external_single_structure
169 * @since Moodle 2.2
171 public static function get_site_info_returns() {
172 return new external_single_structure(
173 array(
174 'sitename' => new external_value(PARAM_RAW, 'site name'),
175 'username' => new external_value(PARAM_RAW, 'username'),
176 'firstname' => new external_value(PARAM_TEXT, 'first name'),
177 'lastname' => new external_value(PARAM_TEXT, 'last name'),
178 'fullname' => new external_value(PARAM_TEXT, 'user full name'),
179 'lang' => new external_value(PARAM_LANG, 'user language'),
180 'userid' => new external_value(PARAM_INT, 'user id'),
181 'siteurl' => new external_value(PARAM_RAW, 'site url'),
182 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
183 Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
184 In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
185 "webservice/pluginfile.php?token=WSTOKEN&file="
186 Of course the user can only see profile picture depending
187 on his/her permissions. Moreover it is recommended to use HTTPS too.'),
188 'functions' => new external_multiple_structure(
189 new external_single_structure(
190 array(
191 'name' => new external_value(PARAM_RAW, 'function name'),
192 'version' => new external_value(PARAM_TEXT,
193 'The version number of the component to which the function belongs')
194 ), 'functions that are available')
196 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
197 VALUE_OPTIONAL),
198 'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
199 'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
200 'mobilecssurl' => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL)
207 * Deprecated web service related functions
209 * @package core_webservice
210 * @category external
211 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
212 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
213 * @deprecated Moodle 2.2 MDL-29106 - please do not use this class any more.
214 * @see core_webservice_external
215 * @since Moodle 2.1
217 class moodle_webservice_external extends external_api {
220 * Returns description of method parameters
222 * @return external_function_parameters
223 * @deprecated Moodle 2.2 - please do not use this function any more.
224 * @see core_webservice_external::get_site_info_parameters
225 * @since Moodle 2.1
227 public static function get_siteinfo_parameters() {
228 return core_webservice_external::get_site_info_parameters();
232 * Return user information including profile picture + basic site information
233 * Note:
234 * - no capability checking because we return just known information by logged user
236 * @param array $serviceshortnames of service shortnames - the functions of these services will be returned
237 * @return array
238 * @deprecated Moodle 2.2 - please do not use this function any more.
239 * @see core_webservice_external::get_site_info
240 * @since Moodle 2.1
242 public function get_siteinfo($serviceshortnames = array()) {
243 return core_webservice_external::get_site_info($serviceshortnames);
247 * Returns description of method result value
249 * @return external_single_structure
250 * @deprecated Moodle 2.2 - please do not use this function any more.
251 * @see core_webservice_external::get_site_info_returns
252 * @since Moodle 2.1
254 public static function get_siteinfo_returns() {
255 return core_webservice_external::get_site_info_returns();