weekly release 3.10.1+
[moodle.git] / webservice / externallib.php
CommitLineData
0bf486a6
JM
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.
13//
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/>.
16
a0a07014 17
0bf486a6
JM
18/**
19 * external API for mobile web services
20 *
a0a07014
JM
21 * @package core_webservice
22 * @category external
23 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
0bf486a6
JM
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
639bc8b2
PC
27defined('MOODLE_INTERNAL') || die;
28
29require_once("$CFG->libdir/externallib.php");
30
5d1017e1
JM
31/**
32 * Web service related functions
a0a07014
JM
33 *
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
5d1017e1
JM
39 */
40class core_webservice_external extends external_api {
0bf486a6 41
5d1017e1
JM
42 /**
43 * Returns description of method parameters
a0a07014 44 *
e6acc551 45 * @return external_function_parameters
a0a07014 46 * @since Moodle 2.2
5d1017e1
JM
47 */
48 public static function get_site_info_parameters() {
0bf486a6
JM
49 return new external_function_parameters(
50 array('serviceshortnames' => new external_multiple_structure (
00a33dcb
SH
51 new external_value(
52 PARAM_ALPHANUMEXT,
53 'service shortname'),
639bc8b2
PC
54 'DEPRECATED PARAMETER - it was a design error in the original implementation. \
55 It is ignored now. (parameter kept for backward compatibility)',
00a33dcb
SH
56 VALUE_DEFAULT,
57 array()
58 ),
59 )
0bf486a6
JM
60 );
61 }
62
63 /**
00a33dcb 64 * Return user information including profile picture + basic site information
0bf486a6 65 * Note:
a0a07014
JM
66 * - no capability checking because we return only known information about logged user
67 *
97f6be20 68 * @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored -
639bc8b2 69 * it was an original design error, we keep for backward compatibility.
a0a07014
JM
70 * @return array site info
71 * @since Moodle 2.2
0bf486a6 72 */
639bc8b2 73 public static function get_site_info($serviceshortnames = array()) {
d85bedf7 74 global $USER, $SITE, $CFG, $DB, $PAGE;
0bf486a6 75
5d1017e1 76 $params = self::validate_parameters(self::get_site_info_parameters(),
00a33dcb 77 array('serviceshortnames'=>$serviceshortnames));
0bf486a6 78
11c78c11 79 $context = context_user::instance($USER->id);
591ece30 80 $systemcontext = context_system::instance();
d85bedf7
JL
81
82 $userpicture = new user_picture($USER);
83 $userpicture->size = 1; // Size f1.
84 $profileimageurl = $userpicture->get_url($PAGE);
0bf486a6 85
639bc8b2 86 // Site information.
af03513f 87 $siteinfo = array(
591ece30 88 'sitename' => external_format_string($SITE->fullname, $systemcontext),
af03513f
JM
89 'siteurl' => $CFG->wwwroot,
90 'username' => $USER->username,
91 'firstname' => $USER->firstname,
92 'lastname' => $USER->lastname,
93 'fullname' => fullname($USER),
6db24235 94 'lang' => clean_param(current_language(), PARAM_LANG),
af03513f 95 'userid' => $USER->id,
43695b6a
DP
96 'userpictureurl' => $profileimageurl->out(false),
97 'siteid' => SITEID
af03513f 98 );
0bf486a6 99
639bc8b2
PC
100 // Retrieve the service and functions from the web service linked to the token
101 // If you call this function directly from external (not a web service call),
102 // then it will still return site info without information about a service
103 // Note: wsusername/wspassword ws authentication is not supported.
af03513f 104 $functions = array();
639bc8b2 105 if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
af03513f 106 $token = optional_param('wstoken', '', PARAM_ALPHANUM);
0bf486a6 107
639bc8b2
PC
108 if (!empty($token)) { // No need to run if not a ws call.
109 // Retrieve service shortname.
af03513f
JM
110 $servicesql = 'SELECT s.*
111 FROM {external_services} s, {external_tokens} t
112 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
113 $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
114
115 $siteinfo['downloadfiles'] = $service->downloadfiles;
106c55fb 116 $siteinfo['uploadfiles'] = $service->uploadfiles;
af03513f
JM
117
118 if (!empty($service)) {
639bc8b2
PC
119 // Return the release and version number for web service users only.
120 $siteinfo['release'] = $CFG->release;
121 $siteinfo['version'] = $CFG->version;
122 // Retrieve the functions.
af03513f
JM
123 $functionssql = "SELECT f.*
124 FROM {external_functions} f, {external_services_functions} sf
125 WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
126 $functions = $DB->get_records_sql($functionssql, array($service->id));
127 } else {
639bc8b2
PC
128 throw new coding_exception('No service found in get_site_info: something is buggy, \
129 it should have fail at the ws server authentication layer.');
af03513f
JM
130 }
131 }
132 }
0bf486a6 133
639bc8b2 134 // Build up the returned values of the list of functions.
0bf486a6 135 $componentversions = array();
639bc8b2 136 $availablefunctions = array();
0bf486a6
JM
137 foreach ($functions as $function) {
138 $functioninfo = array();
139 $functioninfo['name'] = $function->name;
639bc8b2
PC
140 if ($function->component == 'moodle' || $function->component == 'core') {
141 $version = $CFG->version; // Moodle version.
0bf486a6 142 } else {
b0d1d941 143 $versionpath = core_component::get_component_directory($function->component).'/version.php';
0bf486a6 144 if (is_readable($versionpath)) {
639bc8b2 145 // We store the component version once retrieved (so we don't load twice the version.php).
0bf486a6 146 if (!isset($componentversions[$function->component])) {
5978773d 147 $plugin = new stdClass();
0bf486a6
JM
148 include($versionpath);
149 $componentversions[$function->component] = $plugin->version;
150 $version = $plugin->version;
151 } else {
152 $version = $componentversions[$function->component];
153 }
154 } else {
639bc8b2
PC
155 // Function component should always have a version.php,
156 // otherwise the function should have been described with component => 'moodle'.
0bf486a6
JM
157 throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
158 }
159 }
160 $functioninfo['version'] = $version;
639bc8b2 161 $availablefunctions[] = $functioninfo;
0bf486a6
JM
162 }
163
639bc8b2 164 $siteinfo['functions'] = $availablefunctions;
af03513f 165
9184d44d 166 // Mobile CSS theme and alternative login url.
0c6c8664 167 $siteinfo['mobilecssurl'] = !empty($CFG->mobilecssurl) ? $CFG->mobilecssurl : '';
33af7882 168
cc90f74b
JL
169 // Retrieve some advanced features. Only enable/disable ones (bool).
170 $advancedfeatures = array("usecomments", "usetags", "enablenotes", "messaging", "enableblogs",
2b6a0c6a 171 "enablecompletion", "enablebadges", "messagingallusers");
cc90f74b
JL
172 foreach ($advancedfeatures as $feature) {
173 if (isset($CFG->{$feature})) {
174 $siteinfo['advancedfeatures'][] = array(
175 'name' => $feature,
176 'value' => (int) $CFG->{$feature}
177 );
178 }
179 }
180 // Special case mnet_dispatcher_mode.
181 $siteinfo['advancedfeatures'][] = array(
182 'name' => 'mnet_dispatcher_mode',
183 'value' => ($CFG->mnet_dispatcher_mode == 'strict') ? 1 : 0
184 );
185
11c78c11
JL
186 // User can manage own files.
187 $siteinfo['usercanmanageownfiles'] = has_capability('moodle/user:manageownfiles', $context);
188
189 // User quota. 0 means user can ignore the quota.
190 $siteinfo['userquota'] = 0;
191 if (!has_capability('moodle/user:ignoreuserquota', $context)) {
c851ee5d 192 $siteinfo['userquota'] = (int) $CFG->userquota; // Cast to int to ensure value is not higher than PHP_INT_MAX.
11c78c11
JL
193 }
194
195 // User max upload file size. -1 means the user can ignore the upload file size.
c851ee5d
JL
196 // Cast to int to ensure value is not higher than PHP_INT_MAX.
197 $siteinfo['usermaxuploadfilesize'] = (int) get_user_max_upload_file_size($context, $CFG->maxbytes);
11c78c11 198
333e7770
JL
199 // User home page.
200 $siteinfo['userhomepage'] = get_home_page();
201
bef5777f
JL
202 // Calendar.
203 $siteinfo['sitecalendartype'] = $CFG->calendartype;
204 if (empty($USER->calendartype)) {
205 $siteinfo['usercalendartype'] = $CFG->calendartype;
206 } else {
207 $siteinfo['usercalendartype'] = $USER->calendartype;
208 }
20570e5d 209 $siteinfo['userissiteadmin'] = is_siteadmin();
bef5777f 210
cd43240d
JL
211 // User key, to avoid using the WS token for fetching assets.
212 $siteinfo['userprivateaccesskey'] = get_user_key('core_files', $USER->id);
213
355c37b8
JL
214 // Current theme.
215 $siteinfo['theme'] = clean_param($PAGE->theme->name, PARAM_THEME); // We always clean to avoid problem with old sites.
216
af03513f 217 return $siteinfo;
0bf486a6
JM
218 }
219
5d1017e1
JM
220 /**
221 * Returns description of method result value
a0a07014 222 *
e6acc551 223 * @return external_single_structure
a0a07014 224 * @since Moodle 2.2
5d1017e1
JM
225 */
226 public static function get_site_info_returns() {
0bf486a6
JM
227 return new external_single_structure(
228 array(
229 'sitename' => new external_value(PARAM_RAW, 'site name'),
230 'username' => new external_value(PARAM_RAW, 'username'),
231 'firstname' => new external_value(PARAM_TEXT, 'first name'),
232 'lastname' => new external_value(PARAM_TEXT, 'last name'),
233 'fullname' => new external_value(PARAM_TEXT, 'user full name'),
6db24235 234 'lang' => new external_value(PARAM_LANG, 'Current language.'),
0bf486a6
JM
235 'userid' => new external_value(PARAM_INT, 'user id'),
236 'siteurl' => new external_value(PARAM_RAW, 'site url'),
e1f97020
JM
237 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
238 Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
239 In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
639bc8b2
PC
240 "webservice/pluginfile.php?token=WSTOKEN&file="
241 Of course the user can only see profile picture depending
242 on his/her permissions. Moreover it is recommended to use HTTPS too.'),
0bf486a6
JM
243 'functions' => new external_multiple_structure(
244 new external_single_structure(
245 array(
246 'name' => new external_value(PARAM_RAW, 'function name'),
87ede25c 247 'version' => new external_value(PARAM_TEXT,
639bc8b2 248 'The version number of the component to which the function belongs')
00a33dcb 249 ), 'functions that are available')
0bf486a6 250 ),
639bc8b2
PC
251 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
252 VALUE_OPTIONAL),
106c55fb
DW
253 'uploadfiles' => new external_value(PARAM_INT, '1 if users are allowed to upload files, 0 if not',
254 VALUE_OPTIONAL),
639bc8b2 255 'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
33af7882 256 'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
cc90f74b
JL
257 'mobilecssurl' => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL),
258 'advancedfeatures' => new external_multiple_structure(
259 new external_single_structure(
260 array(
261 'name' => new external_value(PARAM_ALPHANUMEXT, 'feature name'),
262 'value' => new external_value(PARAM_INT, 'feature value. Usually 1 means enabled.')
263 ),
264 'Advanced features availability'
265 ),
266 'Advanced features availability',
267 VALUE_OPTIONAL
11c78c11
JL
268 ),
269 'usercanmanageownfiles' => new external_value(PARAM_BOOL,
270 'true if the user can manage his own files', VALUE_OPTIONAL),
271 'userquota' => new external_value(PARAM_INT,
272 'user quota (bytes). 0 means user can ignore the quota', VALUE_OPTIONAL),
273 'usermaxuploadfilesize' => new external_value(PARAM_INT,
274 'user max upload file size (bytes). -1 means the user can ignore the upload file size',
333e7770
JL
275 VALUE_OPTIONAL),
276 'userhomepage' => new external_value(PARAM_INT,
277 'the default home page for the user: 0 for the site home, 1 for dashboard',
43695b6a 278 VALUE_OPTIONAL),
cd43240d
JL
279 'userprivateaccesskey' => new external_value(PARAM_ALPHANUM, 'Private user access key for fetching files.',
280 VALUE_OPTIONAL),
bef5777f
JL
281 'siteid' => new external_value(PARAM_INT, 'Site course ID', VALUE_OPTIONAL),
282 'sitecalendartype' => new external_value(PARAM_PLUGIN, 'Calendar type set in the site.', VALUE_OPTIONAL),
283 'usercalendartype' => new external_value(PARAM_PLUGIN, 'Calendar typed used by the user.', VALUE_OPTIONAL),
20570e5d 284 'userissiteadmin' => new external_value(PARAM_BOOL, 'Whether the user is a site admin or not.', VALUE_OPTIONAL),
355c37b8 285 'theme' => new external_value(PARAM_THEME, 'Current theme for the user.', VALUE_OPTIONAL),
0bf486a6
JM
286 )
287 );
288 }
5d1017e1 289}