MDL-49423 admin: Support optgroup in admin_setting_configmultiselect
[moodle.git] / admin / tool / mobile / classes / api.php
CommitLineData
b2478ed0
JL
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
17/**
18 * Class for Moodle Mobile tools.
19 *
20 * @package tool_mobile
21 * @copyright 2016 Juan Leyva
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 * @since Moodle 3.1
24 */
25namespace tool_mobile;
26
27use core_component;
28use core_plugin_manager;
0002056f 29use context_system;
c951f1fe 30use moodle_url;
961c9549 31use moodle_exception;
b2478ed0
JL
32
33/**
e2fe3bc0 34 * API exposed by tool_mobile, to be used mostly by external functions.
b2478ed0
JL
35 *
36 * @copyright 2016 Juan Leyva
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 3.1
39 */
40class api {
41
c951f1fe
JL
42 /** @var int to identify the login via app. */
43 const LOGIN_VIA_APP = 1;
44 /** @var int to identify the login via browser. */
45 const LOGIN_VIA_BROWSER = 2;
46 /** @var int to identify the login via an embedded browser. */
47 const LOGIN_VIA_EMBEDDED_BROWSER = 3;
961c9549
JL
48 /** @var int seconds an auto-login key will expire. */
49 const LOGIN_KEY_TTL = 60;
af1b6043
JL
50 /** @var str link to the custom strings documentation for the app */
51 const CUSTOM_STRINGS_DOC_URL = 'https://docs.moodle.org/en/Moodle_Mobile_language_strings_customisation';
c951f1fe 52
b2478ed0
JL
53 /**
54 * Returns a list of Moodle plugins supporting the mobile app.
55 *
56 * @return array an array of objects containing the plugin information
57 */
58 public static function get_plugins_supporting_mobile() {
59 global $CFG;
60 require_once($CFG->libdir . '/adminlib.php');
61
62 $pluginsinfo = [];
63 $plugintypes = core_component::get_plugin_types();
64
65 foreach ($plugintypes as $plugintype => $unused) {
66 // We need to include files here.
67 $pluginswithfile = core_component::get_plugin_list_with_file($plugintype, 'db' . DIRECTORY_SEPARATOR . 'mobile.php');
68 foreach ($pluginswithfile as $plugin => $notused) {
69 $path = core_component::get_plugin_directory($plugintype, $plugin);
70 $component = $plugintype . '_' . $plugin;
71 $version = get_component_version($component);
72
73 require_once("$path/db/mobile.php");
74 foreach ($addons as $addonname => $addoninfo) {
75 $plugininfo = array(
76 'component' => $component,
77 'version' => $version,
78 'addon' => $addonname,
b4baee42 79 'dependencies' => !empty($addoninfo['dependencies']) ? $addoninfo['dependencies'] : array(),
b2478ed0
JL
80 'fileurl' => '',
81 'filehash' => '',
82 'filesize' => 0
83 );
84
85 // All the mobile packages must be under the plugin mobile directory.
86 $package = $path . DIRECTORY_SEPARATOR . 'mobile' . DIRECTORY_SEPARATOR . $addonname . '.zip';
87 if (file_exists($package)) {
88 $plugininfo['fileurl'] = $CFG->wwwroot . '' . str_replace($CFG->dirroot, '', $package);
89 $plugininfo['filehash'] = sha1_file($package);
90 $plugininfo['filesize'] = filesize($package);
91 }
92 $pluginsinfo[] = $plugininfo;
93 }
94 }
95 }
96 return $pluginsinfo;
97 }
98
0002056f
JL
99 /**
100 * Returns a list of the site public settings, those not requiring authentication.
101 *
102 * @return array with the settings and warnings
103 */
7c1cb3bf 104 public static function get_public_config() {
5d5e30c6 105 global $CFG, $SITE, $PAGE, $OUTPUT;
851b2919 106 require_once($CFG->libdir . '/authlib.php');
0002056f
JL
107
108 $context = context_system::instance();
109 // We need this to make work the format text functions.
110 $PAGE->set_context($context);
111
e2fe3bc0
JL
112 list($authinstructions, $notusedformat) = external_format_text($CFG->auth_instructions, FORMAT_MOODLE, $context->id);
113 list($maintenancemessage, $notusedformat) = external_format_text($CFG->maintenance_message, FORMAT_MOODLE, $context->id);
0002056f
JL
114 $settings = array(
115 'wwwroot' => $CFG->wwwroot,
116 'httpswwwroot' => $CFG->httpswwwroot,
117 'sitename' => external_format_string($SITE->fullname, $context->id, true),
118 'guestlogin' => $CFG->guestloginbutton,
119 'rememberusername' => $CFG->rememberusername,
120 'authloginviaemail' => $CFG->authloginviaemail,
121 'registerauth' => $CFG->registerauth,
122 'forgottenpasswordurl' => $CFG->forgottenpasswordurl,
e2fe3bc0 123 'authinstructions' => $authinstructions,
0002056f
JL
124 'authnoneenabled' => (int) is_enabled_auth('none'),
125 'enablewebservices' => $CFG->enablewebservices,
126 'enablemobilewebservice' => $CFG->enablemobilewebservice,
127 'maintenanceenabled' => $CFG->maintenance_enabled,
e2fe3bc0 128 'maintenancemessage' => $maintenancemessage,
91fff391 129 'mobilecssurl' => !empty($CFG->mobilecssurl) ? $CFG->mobilecssurl : '',
0002056f 130 );
c951f1fe
JL
131
132 $typeoflogin = get_config('tool_mobile', 'typeoflogin');
133 // Not found, edge case.
134 if ($typeoflogin === false) {
135 $typeoflogin = self::LOGIN_VIA_APP; // Defaults to via app.
136 }
137 $settings['typeoflogin'] = $typeoflogin;
138
851b2919
JL
139 // Check if the user can sign-up to return the launch URL in that case.
140 $cansignup = signup_is_enabled();
141
c951f1fe 142 if ($typeoflogin == self::LOGIN_VIA_BROWSER or
851b2919
JL
143 $typeoflogin == self::LOGIN_VIA_EMBEDDED_BROWSER or
144 $cansignup) {
c951f1fe
JL
145 $url = new moodle_url("/$CFG->admin/tool/mobile/launch.php");
146 $settings['launchurl'] = $url->out(false);
147 }
5d5e30c6
JL
148
149 if ($logourl = $OUTPUT->get_logo_url()) {
150 $settings['logourl'] = $logourl->out(false);
151 }
152 if ($compactlogourl = $OUTPUT->get_compact_logo_url()) {
153 $settings['compactlogourl'] = $compactlogourl->out(false);
154 }
155
0002056f
JL
156 return $settings;
157 }
158
6b492628
JL
159 /**
160 * Returns a list of site configurations, filtering by section.
161 *
162 * @param string $section section name
163 * @return stdClass object containing the settings
164 */
165 public static function get_config($section) {
166 global $CFG, $SITE;
167
168 $settings = new \stdClass;
169 $context = context_system::instance();
170 $isadmin = has_capability('moodle/site:config', $context);
171
172 if (empty($section) or $section == 'frontpagesettings') {
173 require_once($CFG->dirroot . '/course/format/lib.php');
174 // First settings that anyone can deduce.
e2fe3bc0
JL
175 $settings->fullname = external_format_string($SITE->fullname, $context->id);
176 $settings->shortname = external_format_string($SITE->shortname, $context->id);
b14a04e0
DM
177
178 // Return to a var instead of directly to $settings object because of differences between
179 // list() in php5 and php7. {@link http://php.net/manual/en/function.list.php}
180 $formattedsummary = external_format_text($SITE->summary, $SITE->summaryformat,
e2fe3bc0 181 $context->id);
b14a04e0
DM
182 $settings->summary = $formattedsummary[0];
183 $settings->summaryformat = $formattedsummary[1];
6b492628
JL
184 $settings->frontpage = $CFG->frontpage;
185 $settings->frontpageloggedin = $CFG->frontpageloggedin;
186 $settings->maxcategorydepth = $CFG->maxcategorydepth;
187 $settings->frontpagecourselimit = $CFG->frontpagecourselimit;
188 $settings->numsections = course_get_format($SITE)->get_course()->numsections;
189 $settings->newsitems = $SITE->newsitems;
190 $settings->commentsperpage = $CFG->commentsperpage;
191
192 // Now, admin settings.
193 if ($isadmin) {
194 $settings->defaultfrontpageroleid = $CFG->defaultfrontpageroleid;
195 }
196 }
197
198 if (empty($section) or $section == 'sitepolicies') {
199 $settings->disableuserimages = $CFG->disableuserimages;
200 }
201
202 if (empty($section) or $section == 'gradessettings') {
203 require_once($CFG->dirroot . '/user/lib.php');
204 $settings->mygradesurl = user_mygrades_url()->out(false);
205 }
206
7bdcf970
JL
207 if (empty($section) or $section == 'mobileapp') {
208 $settings->tool_mobile_forcelogout = get_config('tool_mobile', 'forcelogout');
af1b6043 209 $settings->tool_mobile_customlangstrings = get_config('tool_mobile', 'customlangstrings');
7bdcf970
JL
210 }
211
6b492628
JL
212 return $settings;
213 }
214
961c9549
JL
215 /*
216 * Check if all the required conditions are met to allow the auto-login process continue.
217 *
218 * @param int $userid current user id
219 * @since Moodle 3.2
220 * @throws moodle_exception
221 */
222 public static function check_autologin_prerequisites($userid) {
223 global $CFG;
224
225 if (!$CFG->enablewebservices or !$CFG->enablemobilewebservice) {
226 throw new moodle_exception('enablewsdescription', 'webservice');
227 }
228
229 if (!is_https()) {
230 throw new moodle_exception('httpsrequired', 'tool_mobile');
231 }
232
233 if (has_capability('moodle/site:config', context_system::instance(), $userid) or is_siteadmin($userid)) {
234 throw new moodle_exception('autologinnotallowedtoadmins', 'tool_mobile');
235 }
236 }
237
238 /**
239 * Creates an auto-login key for the current user, this key is restricted by time and ip address.
240 *
241 * @return string the key
242 * @since Moodle 3.2
243 */
244 public static function get_autologin_key() {
245 global $USER;
246 // Delete previous keys.
247 delete_user_key('tool_mobile', $USER->id);
248
249 // Create a new key.
250 $iprestriction = getremoteaddr();
251 $validuntil = time() + self::LOGIN_KEY_TTL;
252 return create_user_key('tool_mobile', $USER->id, null, $iprestriction, $validuntil);
253 }
b2478ed0 254}