Merge branch 'MDL-53010-master' of git://github.com/lameze/moodle
[moodle.git] / webservice / externallib.php
blobb3dc59a1cb1c78f1200b1b9c95dab503744e21be
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, $PAGE;
76 $params = self::validate_parameters(self::get_site_info_parameters(),
77 array('serviceshortnames'=>$serviceshortnames));
79 $context = context_user::instance($USER->id);
81 $userpicture = new user_picture($USER);
82 $userpicture->size = 1; // Size f1.
83 $profileimageurl = $userpicture->get_url($PAGE);
85 // Site information.
86 $siteinfo = array(
87 'sitename' => $SITE->fullname,
88 'siteurl' => $CFG->wwwroot,
89 'username' => $USER->username,
90 'firstname' => $USER->firstname,
91 'lastname' => $USER->lastname,
92 'fullname' => fullname($USER),
93 'lang' => current_language(),
94 'userid' => $USER->id,
95 'userpictureurl' => $profileimageurl->out(false)
98 // Retrieve the service and functions from the web service linked to the token
99 // If you call this function directly from external (not a web service call),
100 // then it will still return site info without information about a service
101 // Note: wsusername/wspassword ws authentication is not supported.
102 $functions = array();
103 if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
104 $token = optional_param('wstoken', '', PARAM_ALPHANUM);
106 if (!empty($token)) { // No need to run if not a ws call.
107 // Retrieve service shortname.
108 $servicesql = 'SELECT s.*
109 FROM {external_services} s, {external_tokens} t
110 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
111 $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
113 $siteinfo['downloadfiles'] = $service->downloadfiles;
114 $siteinfo['uploadfiles'] = $service->uploadfiles;
116 if (!empty($service)) {
117 // Return the release and version number for web service users only.
118 $siteinfo['release'] = $CFG->release;
119 $siteinfo['version'] = $CFG->version;
120 // Retrieve the functions.
121 $functionssql = "SELECT f.*
122 FROM {external_functions} f, {external_services_functions} sf
123 WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
124 $functions = $DB->get_records_sql($functionssql, array($service->id));
125 } else {
126 throw new coding_exception('No service found in get_site_info: something is buggy, \
127 it should have fail at the ws server authentication layer.');
132 // Build up the returned values of the list of functions.
133 $componentversions = array();
134 $availablefunctions = array();
135 foreach ($functions as $function) {
136 $functioninfo = array();
137 $functioninfo['name'] = $function->name;
138 if ($function->component == 'moodle' || $function->component == 'core') {
139 $version = $CFG->version; // Moodle version.
140 } else {
141 $versionpath = core_component::get_component_directory($function->component).'/version.php';
142 if (is_readable($versionpath)) {
143 // We store the component version once retrieved (so we don't load twice the version.php).
144 if (!isset($componentversions[$function->component])) {
145 $plugin = new stdClass();
146 include($versionpath);
147 $componentversions[$function->component] = $plugin->version;
148 $version = $plugin->version;
149 } else {
150 $version = $componentversions[$function->component];
152 } else {
153 // Function component should always have a version.php,
154 // otherwise the function should have been described with component => 'moodle'.
155 throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
158 $functioninfo['version'] = $version;
159 $availablefunctions[] = $functioninfo;
162 $siteinfo['functions'] = $availablefunctions;
164 // Mobile CSS theme and alternative login url.
165 $siteinfo['mobilecssurl'] = $CFG->mobilecssurl;
167 // Retrieve some advanced features. Only enable/disable ones (bool).
168 $advancedfeatures = array("usecomments", "usetags", "enablenotes", "messaging", "enableblogs",
169 "enablecompletion", "enablebadges");
170 foreach ($advancedfeatures as $feature) {
171 if (isset($CFG->{$feature})) {
172 $siteinfo['advancedfeatures'][] = array(
173 'name' => $feature,
174 'value' => (int) $CFG->{$feature}
178 // Special case mnet_dispatcher_mode.
179 $siteinfo['advancedfeatures'][] = array(
180 'name' => 'mnet_dispatcher_mode',
181 'value' => ($CFG->mnet_dispatcher_mode == 'strict') ? 1 : 0
184 // User can manage own files.
185 $siteinfo['usercanmanageownfiles'] = has_capability('moodle/user:manageownfiles', $context);
187 // User quota. 0 means user can ignore the quota.
188 $siteinfo['userquota'] = 0;
189 if (!has_capability('moodle/user:ignoreuserquota', $context)) {
190 $siteinfo['userquota'] = $CFG->userquota;
193 // User max upload file size. -1 means the user can ignore the upload file size.
194 $siteinfo['usermaxuploadfilesize'] = get_user_max_upload_file_size($context, $CFG->maxbytes);
196 // User home page.
197 $siteinfo['userhomepage'] = get_home_page();
199 return $siteinfo;
203 * Returns description of method result value
205 * @return external_single_structure
206 * @since Moodle 2.2
208 public static function get_site_info_returns() {
209 return new external_single_structure(
210 array(
211 'sitename' => new external_value(PARAM_RAW, 'site name'),
212 'username' => new external_value(PARAM_RAW, 'username'),
213 'firstname' => new external_value(PARAM_TEXT, 'first name'),
214 'lastname' => new external_value(PARAM_TEXT, 'last name'),
215 'fullname' => new external_value(PARAM_TEXT, 'user full name'),
216 'lang' => new external_value(PARAM_LANG, 'user language'),
217 'userid' => new external_value(PARAM_INT, 'user id'),
218 'siteurl' => new external_value(PARAM_RAW, 'site url'),
219 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
220 Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
221 In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
222 "webservice/pluginfile.php?token=WSTOKEN&file="
223 Of course the user can only see profile picture depending
224 on his/her permissions. Moreover it is recommended to use HTTPS too.'),
225 'functions' => new external_multiple_structure(
226 new external_single_structure(
227 array(
228 'name' => new external_value(PARAM_RAW, 'function name'),
229 'version' => new external_value(PARAM_TEXT,
230 'The version number of the component to which the function belongs')
231 ), 'functions that are available')
233 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
234 VALUE_OPTIONAL),
235 'uploadfiles' => new external_value(PARAM_INT, '1 if users are allowed to upload files, 0 if not',
236 VALUE_OPTIONAL),
237 'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
238 'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
239 'mobilecssurl' => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL),
240 'advancedfeatures' => new external_multiple_structure(
241 new external_single_structure(
242 array(
243 'name' => new external_value(PARAM_ALPHANUMEXT, 'feature name'),
244 'value' => new external_value(PARAM_INT, 'feature value. Usually 1 means enabled.')
246 'Advanced features availability'
248 'Advanced features availability',
249 VALUE_OPTIONAL
251 'usercanmanageownfiles' => new external_value(PARAM_BOOL,
252 'true if the user can manage his own files', VALUE_OPTIONAL),
253 'userquota' => new external_value(PARAM_INT,
254 'user quota (bytes). 0 means user can ignore the quota', VALUE_OPTIONAL),
255 'usermaxuploadfilesize' => new external_value(PARAM_INT,
256 'user max upload file size (bytes). -1 means the user can ignore the upload file size',
257 VALUE_OPTIONAL),
258 'userhomepage' => new external_value(PARAM_INT,
259 'the default home page for the user: 0 for the site home, 1 for dashboard',
260 VALUE_OPTIONAL)
267 * Deprecated web service related functions
269 * @package core_webservice
270 * @category external
271 * @copyright 2011 Jerome Mouneyrac <jerome@moodle.com>
272 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
273 * @deprecated Moodle 2.2 MDL-29106 - please do not use this class any more.
274 * @see core_webservice_external
275 * @since Moodle 2.1
277 class moodle_webservice_external extends external_api {
280 * Returns description of method parameters
282 * @return external_function_parameters
283 * @deprecated Moodle 2.2 - please do not use this function any more.
284 * @see core_webservice_external::get_site_info_parameters
285 * @since Moodle 2.1
287 public static function get_siteinfo_parameters() {
288 return core_webservice_external::get_site_info_parameters();
292 * Return user information including profile picture + basic site information
293 * Note:
294 * - no capability checking because we return just known information by logged user
296 * @param array $serviceshortnames of service shortnames - the functions of these services will be returned
297 * @return array
298 * @deprecated Moodle 2.2 - please do not use this function any more.
299 * @see core_webservice_external::get_site_info
300 * @since Moodle 2.1
302 public function get_siteinfo($serviceshortnames = array()) {
303 return core_webservice_external::get_site_info($serviceshortnames);
307 * Returns description of method result value
309 * @return external_single_structure
310 * @deprecated Moodle 2.2 - please do not use this function any more.
311 * @see core_webservice_external::get_site_info_returns
312 * @since Moodle 2.1
314 public static function get_siteinfo_returns() {
315 return core_webservice_external::get_site_info_returns();
319 * Marking the method as deprecated.
321 * @return bool
323 public static function get_siteinfo_is_deprecated() {
324 return true;