MDL-63854 competencies, themes: misplaced dropdown arrows
[moodle.git] / webservice / externallib.php
blob94e3858c0a0579b4af80d9ca80d0f09bba1e4c08
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' => clean_param(current_language(), PARAM_LANG),
94 'userid' => $USER->id,
95 'userpictureurl' => $profileimageurl->out(false),
96 'siteid' => SITEID
99 // Retrieve the service and functions from the web service linked to the token
100 // If you call this function directly from external (not a web service call),
101 // then it will still return site info without information about a service
102 // Note: wsusername/wspassword ws authentication is not supported.
103 $functions = array();
104 if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call.
105 $token = optional_param('wstoken', '', PARAM_ALPHANUM);
107 if (!empty($token)) { // No need to run if not a ws call.
108 // Retrieve service shortname.
109 $servicesql = 'SELECT s.*
110 FROM {external_services} s, {external_tokens} t
111 WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1';
112 $service = $DB->get_record_sql($servicesql, array($token, $USER->id));
114 $siteinfo['downloadfiles'] = $service->downloadfiles;
115 $siteinfo['uploadfiles'] = $service->uploadfiles;
117 if (!empty($service)) {
118 // Return the release and version number for web service users only.
119 $siteinfo['release'] = $CFG->release;
120 $siteinfo['version'] = $CFG->version;
121 // Retrieve the functions.
122 $functionssql = "SELECT f.*
123 FROM {external_functions} f, {external_services_functions} sf
124 WHERE f.name = sf.functionname AND sf.externalserviceid = ?";
125 $functions = $DB->get_records_sql($functionssql, array($service->id));
126 } else {
127 throw new coding_exception('No service found in get_site_info: something is buggy, \
128 it should have fail at the ws server authentication layer.');
133 // Build up the returned values of the list of functions.
134 $componentversions = array();
135 $availablefunctions = array();
136 foreach ($functions as $function) {
137 $functioninfo = array();
138 $functioninfo['name'] = $function->name;
139 if ($function->component == 'moodle' || $function->component == 'core') {
140 $version = $CFG->version; // Moodle version.
141 } else {
142 $versionpath = core_component::get_component_directory($function->component).'/version.php';
143 if (is_readable($versionpath)) {
144 // We store the component version once retrieved (so we don't load twice the version.php).
145 if (!isset($componentversions[$function->component])) {
146 $plugin = new stdClass();
147 include($versionpath);
148 $componentversions[$function->component] = $plugin->version;
149 $version = $plugin->version;
150 } else {
151 $version = $componentversions[$function->component];
153 } else {
154 // Function component should always have a version.php,
155 // otherwise the function should have been described with component => 'moodle'.
156 throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
159 $functioninfo['version'] = $version;
160 $availablefunctions[] = $functioninfo;
163 $siteinfo['functions'] = $availablefunctions;
165 // Mobile CSS theme and alternative login url.
166 $siteinfo['mobilecssurl'] = !empty($CFG->mobilecssurl) ? $CFG->mobilecssurl : '';
168 // Retrieve some advanced features. Only enable/disable ones (bool).
169 $advancedfeatures = array("usecomments", "usetags", "enablenotes", "messaging", "enableblogs",
170 "enablecompletion", "enablebadges", "messagingallusers");
171 foreach ($advancedfeatures as $feature) {
172 if (isset($CFG->{$feature})) {
173 $siteinfo['advancedfeatures'][] = array(
174 'name' => $feature,
175 'value' => (int) $CFG->{$feature}
179 // Special case mnet_dispatcher_mode.
180 $siteinfo['advancedfeatures'][] = array(
181 'name' => 'mnet_dispatcher_mode',
182 'value' => ($CFG->mnet_dispatcher_mode == 'strict') ? 1 : 0
185 // User can manage own files.
186 $siteinfo['usercanmanageownfiles'] = has_capability('moodle/user:manageownfiles', $context);
188 // User quota. 0 means user can ignore the quota.
189 $siteinfo['userquota'] = 0;
190 if (!has_capability('moodle/user:ignoreuserquota', $context)) {
191 $siteinfo['userquota'] = (int) $CFG->userquota; // Cast to int to ensure value is not higher than PHP_INT_MAX.
194 // User max upload file size. -1 means the user can ignore the upload file size.
195 // Cast to int to ensure value is not higher than PHP_INT_MAX.
196 $siteinfo['usermaxuploadfilesize'] = (int) get_user_max_upload_file_size($context, $CFG->maxbytes);
198 // User home page.
199 $siteinfo['userhomepage'] = get_home_page();
201 // Calendar.
202 $siteinfo['sitecalendartype'] = $CFG->calendartype;
203 if (empty($USER->calendartype)) {
204 $siteinfo['usercalendartype'] = $CFG->calendartype;
205 } else {
206 $siteinfo['usercalendartype'] = $USER->calendartype;
209 return $siteinfo;
213 * Returns description of method result value
215 * @return external_single_structure
216 * @since Moodle 2.2
218 public static function get_site_info_returns() {
219 return new external_single_structure(
220 array(
221 'sitename' => new external_value(PARAM_RAW, 'site name'),
222 'username' => new external_value(PARAM_RAW, 'username'),
223 'firstname' => new external_value(PARAM_TEXT, 'first name'),
224 'lastname' => new external_value(PARAM_TEXT, 'last name'),
225 'fullname' => new external_value(PARAM_TEXT, 'user full name'),
226 'lang' => new external_value(PARAM_LANG, 'Current language.'),
227 'userid' => new external_value(PARAM_INT, 'user id'),
228 'siteurl' => new external_value(PARAM_RAW, 'site url'),
229 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture.
230 Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES.
231 In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by
232 "webservice/pluginfile.php?token=WSTOKEN&file="
233 Of course the user can only see profile picture depending
234 on his/her permissions. Moreover it is recommended to use HTTPS too.'),
235 'functions' => new external_multiple_structure(
236 new external_single_structure(
237 array(
238 'name' => new external_value(PARAM_RAW, 'function name'),
239 'version' => new external_value(PARAM_TEXT,
240 'The version number of the component to which the function belongs')
241 ), 'functions that are available')
243 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not',
244 VALUE_OPTIONAL),
245 'uploadfiles' => new external_value(PARAM_INT, '1 if users are allowed to upload files, 0 if not',
246 VALUE_OPTIONAL),
247 'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL),
248 'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL),
249 'mobilecssurl' => new external_value(PARAM_URL, 'Mobile custom CSS theme', VALUE_OPTIONAL),
250 'advancedfeatures' => new external_multiple_structure(
251 new external_single_structure(
252 array(
253 'name' => new external_value(PARAM_ALPHANUMEXT, 'feature name'),
254 'value' => new external_value(PARAM_INT, 'feature value. Usually 1 means enabled.')
256 'Advanced features availability'
258 'Advanced features availability',
259 VALUE_OPTIONAL
261 'usercanmanageownfiles' => new external_value(PARAM_BOOL,
262 'true if the user can manage his own files', VALUE_OPTIONAL),
263 'userquota' => new external_value(PARAM_INT,
264 'user quota (bytes). 0 means user can ignore the quota', VALUE_OPTIONAL),
265 'usermaxuploadfilesize' => new external_value(PARAM_INT,
266 'user max upload file size (bytes). -1 means the user can ignore the upload file size',
267 VALUE_OPTIONAL),
268 'userhomepage' => new external_value(PARAM_INT,
269 'the default home page for the user: 0 for the site home, 1 for dashboard',
270 VALUE_OPTIONAL),
271 'siteid' => new external_value(PARAM_INT, 'Site course ID', VALUE_OPTIONAL),
272 'sitecalendartype' => new external_value(PARAM_PLUGIN, 'Calendar type set in the site.', VALUE_OPTIONAL),
273 'usercalendartype' => new external_value(PARAM_PLUGIN, 'Calendar typed used by the user.', VALUE_OPTIONAL),