Merge branch 'MDL-52835-m29' of git://github.com/deraadt/moodle into MOODLE_29_STABLE
[moodle.git] / webservice / externallib.php
blobb4b7feb8dfbc15dbd32733157a3938ae73edfaaf
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 $context = context_user::instance($USER->id);
80 $profileimageurl = moodle_url::make_pluginfile_url(
81 $context->id, 'user', 'icon', null, '/', 'f1');
83 // Site information.
84 $siteinfo = array(
85 'sitename' => $SITE->fullname,
86 'siteurl' => $CFG->wwwroot,
87 'username' => $USER->username,
88 'firstname' => $USER->firstname,
89 'lastname' => $USER->lastname,
90 'fullname' => fullname($USER),
91 'lang' => current_language(),
92 'userid' => $USER->id,
93 'userpictureurl' => $profileimageurl->out(false)
96 // Retrieve the service and functions from the web service linked to the token
97 // If you call this function directly from external (not a web service call),
98 // then it will still return site info without information about a service
99 // Note: wsusername/wspassword ws authentication is not supported.
100 $functions = array();
101 if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
102 $token = optional_param('wstoken', '', PARAM_ALPHANUM);
104 if (!empty($token)) { // No need to run if not a ws call.
105 // Retrieve service shortname.
106 $servicesql = 'SELECT s.*
107 FROM {external_services} s, {external_tokens} t
108 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
109 $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
111 $siteinfo['downloadfiles'] = $service->downloadfiles;
112 $siteinfo['uploadfiles'] = $service->uploadfiles;
114 if (!empty($service)) {
115 // Return the release and version number for web service users only.
116 $siteinfo['release'] = $CFG->release;
117 $siteinfo['version'] = $CFG->version;
118 // Retrieve the functions.
119 $functionssql = "SELECT f.*
120 FROM {external_functions} f, {external_services_functions} sf
121 WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
122 $functions = $DB->get_records_sql($functionssql, array($service->id));
123 } else {
124 throw new coding_exception('No service found in get_site_info: something is buggy, \
125 it should have fail at the ws server authentication layer.');
130 // Build up the returned values of the list of functions.
131 $componentversions = array();
132 $availablefunctions = array();
133 foreach ($functions as $function) {
134 $functioninfo = array();
135 $functioninfo['name'] = $function->name;
136 if ($function->component == 'moodle' || $function->component == 'core') {
137 $version = $CFG->version; // Moodle version.
138 } else {
139 $versionpath = core_component::get_component_directory($function->component).'/version.php';
140 if (is_readable($versionpath)) {
141 // We store the component version once retrieved (so we don't load twice the version.php).
142 if (!isset($componentversions[$function->component])) {
143 $plugin = new stdClass();
144 include($versionpath);
145 $componentversions[$function->component] = $plugin->version;
146 $version = $plugin->version;
147 } else {
148 $version = $componentversions[$function->component];
150 } else {
151 // Function component should always have a version.php,
152 // otherwise the function should have been described with component => 'moodle'.
153 throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
156 $functioninfo['version'] = $version;
157 $availablefunctions[] = $functioninfo;
160 $siteinfo['functions'] = $availablefunctions;
162 // Mobile CSS theme and alternative login url.
163 $siteinfo['mobilecssurl'] = $CFG->mobilecssurl;
165 // Retrieve some advanced features. Only enable/disable ones (bool).
166 $advancedfeatures = array("usecomments", "usetags", "enablenotes", "messaging", "enableblogs",
167 "enablecompletion", "enablebadges");
168 foreach ($advancedfeatures as $feature) {
169 if (isset($CFG->{$feature})) {
170 $siteinfo['advancedfeatures'][] = array(
171 'name' => $feature,
172 'value' => (int) $CFG->{$feature}
176 // Special case mnet_dispatcher_mode.
177 $siteinfo['advancedfeatures'][] = array(
178 'name' => 'mnet_dispatcher_mode',
179 'value' => ($CFG->mnet_dispatcher_mode == 'strict') ? 1 : 0
182 // User can manage own files.
183 $siteinfo['usercanmanageownfiles'] = has_capability('moodle/user:manageownfiles', $context);
185 // User quota. 0 means user can ignore the quota.
186 $siteinfo['userquota'] = 0;
187 if (!has_capability('moodle/user:ignoreuserquota', $context)) {
188 $siteinfo['userquota'] = $CFG->userquota;
191 // User max upload file size. -1 means the user can ignore the upload file size.
192 $siteinfo['usermaxuploadfilesize'] = get_user_max_upload_file_size($context, $CFG->maxbytes);
194 return $siteinfo;
198 * Returns description of method result value
200 * @return external_single_structure
201 * @since Moodle 2.2
203 public static function get_site_info_returns() {
204 return new external_single_structure(
205 array(
206 'sitename' => new external_value(PARAM_RAW, 'site name'),
207 'username' => new external_value(PARAM_RAW, 'username'),
208 'firstname' => new external_value(PARAM_TEXT, 'first name'),
209 'lastname' => new external_value(PARAM_TEXT, 'last name'),
210 'fullname' => new external_value(PARAM_TEXT, 'user full name'),
211 'lang' => new external_value(PARAM_LANG, 'user language'),
212 'userid' => new external_value(PARAM_INT, 'user id'),
213 'siteurl' => new external_value(PARAM_RAW, 'site url'),
214 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
215 Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
216 In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
217 "webservice/pluginfile.php?token=WSTOKEN&file="
218 Of course the user can only see profile picture depending
219 on his/her permissions. Moreover it is recommended to use HTTPS too.'),
220 'functions' => new external_multiple_structure(
221 new external_single_structure(
222 array(
223 'name' => new external_value(PARAM_RAW, 'function name'),
224 'version' => new external_value(PARAM_TEXT,
225 'The version number of the component to which the function belongs')
226 ), 'functions that are available')
228 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
229 VALUE_OPTIONAL),
230 'uploadfiles' => new external_value(PARAM_INT, '1 if users are allowed to upload files, 0 if not',
231 VALUE_OPTIONAL),
232 'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
233 'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
234 'mobilecssurl' => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL),
235 'advancedfeatures' => new external_multiple_structure(
236 new external_single_structure(
237 array(
238 'name' => new external_value(PARAM_ALPHANUMEXT, 'feature name'),
239 'value' => new external_value(PARAM_INT, 'feature value. Usually 1 means enabled.')
241 'Advanced features availability'
243 'Advanced features availability',
244 VALUE_OPTIONAL
246 'usercanmanageownfiles' => new external_value(PARAM_BOOL,
247 'true if the user can manage his own files', VALUE_OPTIONAL),
248 'userquota' => new external_value(PARAM_INT,
249 'user quota (bytes). 0 means user can ignore the quota', VALUE_OPTIONAL),
250 'usermaxuploadfilesize' => new external_value(PARAM_INT,
251 'user max upload file size (bytes). -1 means the user can ignore the upload file size',
252 VALUE_OPTIONAL)
259 * Deprecated web service related functions
261 * @package core_webservice
262 * @category external
263 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
264 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
265 * @deprecated Moodle 2.2 MDL-29106 - please do not use this class any more.
266 * @see core_webservice_external
267 * @since Moodle 2.1
269 class moodle_webservice_external extends external_api {
272 * Returns description of method parameters
274 * @return external_function_parameters
275 * @deprecated Moodle 2.2 - please do not use this function any more.
276 * @see core_webservice_external::get_site_info_parameters
277 * @since Moodle 2.1
279 public static function get_siteinfo_parameters() {
280 return core_webservice_external::get_site_info_parameters();
284 * Return user information including profile picture + basic site information
285 * Note:
286 * - no capability checking because we return just known information by logged user
288 * @param array $serviceshortnames of service shortnames - the functions of these services will be returned
289 * @return array
290 * @deprecated Moodle 2.2 - please do not use this function any more.
291 * @see core_webservice_external::get_site_info
292 * @since Moodle 2.1
294 public function get_siteinfo($serviceshortnames = array()) {
295 return core_webservice_external::get_site_info($serviceshortnames);
299 * Returns description of method result value
301 * @return external_single_structure
302 * @deprecated Moodle 2.2 - please do not use this function any more.
303 * @see core_webservice_external::get_site_info_returns
304 * @since Moodle 2.1
306 public static function get_siteinfo_returns() {
307 return core_webservice_external::get_site_info_returns();
311 * Marking the method as deprecated.
313 * @return bool
315 public static function get_siteinfo_is_deprecated() {
316 return true;