MDL-45639 webservice: Support private tokens
[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;
b2478ed0
JL
31
32/**
33 * API exposed by tool_mobile
34 *
35 * @copyright 2016 Juan Leyva
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 * @since Moodle 3.1
38 */
39class api {
40
c951f1fe
JL
41 /** @var int to identify the login via app. */
42 const LOGIN_VIA_APP = 1;
43 /** @var int to identify the login via browser. */
44 const LOGIN_VIA_BROWSER = 2;
45 /** @var int to identify the login via an embedded browser. */
46 const LOGIN_VIA_EMBEDDED_BROWSER = 3;
47
b2478ed0
JL
48 /**
49 * Returns a list of Moodle plugins supporting the mobile app.
50 *
51 * @return array an array of objects containing the plugin information
52 */
53 public static function get_plugins_supporting_mobile() {
54 global $CFG;
55 require_once($CFG->libdir . '/adminlib.php');
56
57 $pluginsinfo = [];
58 $plugintypes = core_component::get_plugin_types();
59
60 foreach ($plugintypes as $plugintype => $unused) {
61 // We need to include files here.
62 $pluginswithfile = core_component::get_plugin_list_with_file($plugintype, 'db' . DIRECTORY_SEPARATOR . 'mobile.php');
63 foreach ($pluginswithfile as $plugin => $notused) {
64 $path = core_component::get_plugin_directory($plugintype, $plugin);
65 $component = $plugintype . '_' . $plugin;
66 $version = get_component_version($component);
67
68 require_once("$path/db/mobile.php");
69 foreach ($addons as $addonname => $addoninfo) {
70 $plugininfo = array(
71 'component' => $component,
72 'version' => $version,
73 'addon' => $addonname,
b4baee42 74 'dependencies' => !empty($addoninfo['dependencies']) ? $addoninfo['dependencies'] : array(),
b2478ed0
JL
75 'fileurl' => '',
76 'filehash' => '',
77 'filesize' => 0
78 );
79
80 // All the mobile packages must be under the plugin mobile directory.
81 $package = $path . DIRECTORY_SEPARATOR . 'mobile' . DIRECTORY_SEPARATOR . $addonname . '.zip';
82 if (file_exists($package)) {
83 $plugininfo['fileurl'] = $CFG->wwwroot . '' . str_replace($CFG->dirroot, '', $package);
84 $plugininfo['filehash'] = sha1_file($package);
85 $plugininfo['filesize'] = filesize($package);
86 }
87 $pluginsinfo[] = $plugininfo;
88 }
89 }
90 }
91 return $pluginsinfo;
92 }
93
0002056f
JL
94 /**
95 * Returns a list of the site public settings, those not requiring authentication.
96 *
97 * @return array with the settings and warnings
98 */
7c1cb3bf 99 public static function get_public_config() {
0002056f
JL
100 global $CFG, $SITE, $PAGE;
101
102 $context = context_system::instance();
103 // We need this to make work the format text functions.
104 $PAGE->set_context($context);
105
106 $settings = array(
107 'wwwroot' => $CFG->wwwroot,
108 'httpswwwroot' => $CFG->httpswwwroot,
109 'sitename' => external_format_string($SITE->fullname, $context->id, true),
110 'guestlogin' => $CFG->guestloginbutton,
111 'rememberusername' => $CFG->rememberusername,
112 'authloginviaemail' => $CFG->authloginviaemail,
113 'registerauth' => $CFG->registerauth,
114 'forgottenpasswordurl' => $CFG->forgottenpasswordurl,
115 'authinstructions' => format_text($CFG->auth_instructions),
116 'authnoneenabled' => (int) is_enabled_auth('none'),
117 'enablewebservices' => $CFG->enablewebservices,
118 'enablemobilewebservice' => $CFG->enablemobilewebservice,
119 'maintenanceenabled' => $CFG->maintenance_enabled,
120 'maintenancemessage' => format_text($CFG->maintenance_message),
121 );
c951f1fe
JL
122
123 $typeoflogin = get_config('tool_mobile', 'typeoflogin');
124 // Not found, edge case.
125 if ($typeoflogin === false) {
126 $typeoflogin = self::LOGIN_VIA_APP; // Defaults to via app.
127 }
128 $settings['typeoflogin'] = $typeoflogin;
129
130 if ($typeoflogin == self::LOGIN_VIA_BROWSER or
131 $typeoflogin == self::LOGIN_VIA_EMBEDDED_BROWSER) {
132 $url = new moodle_url("/$CFG->admin/tool/mobile/launch.php");
133 $settings['launchurl'] = $url->out(false);
134 }
0002056f
JL
135 return $settings;
136 }
137
6b492628
JL
138 /**
139 * Returns a list of site configurations, filtering by section.
140 *
141 * @param string $section section name
142 * @return stdClass object containing the settings
143 */
144 public static function get_config($section) {
145 global $CFG, $SITE;
146
147 $settings = new \stdClass;
148 $context = context_system::instance();
149 $isadmin = has_capability('moodle/site:config', $context);
150
151 if (empty($section) or $section == 'frontpagesettings') {
152 require_once($CFG->dirroot . '/course/format/lib.php');
153 // First settings that anyone can deduce.
154 $settings->fullname = $SITE->fullname;
155 $settings->shortname = $SITE->shortname;
156 $settings->summary = $SITE->summary;
157 $settings->frontpage = $CFG->frontpage;
158 $settings->frontpageloggedin = $CFG->frontpageloggedin;
159 $settings->maxcategorydepth = $CFG->maxcategorydepth;
160 $settings->frontpagecourselimit = $CFG->frontpagecourselimit;
161 $settings->numsections = course_get_format($SITE)->get_course()->numsections;
162 $settings->newsitems = $SITE->newsitems;
163 $settings->commentsperpage = $CFG->commentsperpage;
164
165 // Now, admin settings.
166 if ($isadmin) {
167 $settings->defaultfrontpageroleid = $CFG->defaultfrontpageroleid;
168 }
169 }
170
171 if (empty($section) or $section == 'sitepolicies') {
172 $settings->disableuserimages = $CFG->disableuserimages;
173 }
174
175 if (empty($section) or $section == 'gradessettings') {
176 require_once($CFG->dirroot . '/user/lib.php');
177 $settings->mygradesurl = user_mygrades_url()->out(false);
178 }
179
180 return $settings;
181 }
182
b2478ed0 183}