2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * @package tool_mobile
21 * @copyright 2020 Moodle Pty Ltd
22 * @author <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace tool_mobile\output;
31 * @package tool_mobile
32 * @copyright 2020 Moodle Pty Ltd
33 * @author <juan@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class subscription implements \renderable, \templatable {
41 * @var array subscription data
43 protected $subscriptiondata;
46 * Constructor for the class, sets the subscription data.
48 * @param array $subscriptiondata subscription data
51 public function __construct(array $subscriptiondata) {
52 $this->subscriptiondata = $subscriptiondata;
58 * @param \renderer_base $output
59 * @return array with the subscription information
61 public function export_for_template(\renderer_base $output): array {
64 $ms = get_config('tool_mobile'); // Get mobile settings.
66 $data = $this->subscriptiondata;
67 $data['appsportalurl'] = \tool_mobile\api::MOODLE_APPS_PORTAL_URL;
69 // First prepare messages that may come from the WS.
70 if (!empty($data['messages'])) {
71 foreach ($data['messages'] as $msg) {
72 $data['messages' . $msg['type']][] = ['message' => $msg['message']];
75 unset($data['messages']);
77 // Now prepare statistics information.
78 if (isset($data['statistics']['notifications'])) {
79 $data['notifications'] = $data['statistics']['notifications'];
80 unset($data['statistics']['notifications']);
82 // Find current month data.
83 $data['notifications']['currentactivedevices'] = 0;
85 if (isset($data['notifications']['monthly'][0])) {
86 $currentmonth = $data['notifications']['monthly'][0];
87 $data['notifications']['currentactivedevices'] = $currentmonth['activedevices'];
88 if (!empty($currentmonth['limitreachedtime'])) {
89 $data['notifications']['limitreachedtime'] = $currentmonth['limitreachedtime'];
90 $data['notifications']['ignorednotificationswarning'] = [
91 'message' => get_string('notificationslimitreached', 'tool_mobile', $data['appsportalurl'])
98 foreach ($data['subscription']['features'] as &$feature) {
100 // Check the type of features, if it is a limitation or functionality feature.
101 if (array_key_exists('limit', $feature)) {
103 if (empty($feature['limit'])) { // Unlimited, no need to calculate current values.
104 $feature['humanstatus'] = get_string('unlimited');
105 $feature['showbar'] = 0;
109 switch ($feature['name']) {
110 // Check active devices.
111 case 'pushnotificationsdevices':
112 if (isset($data['notifications']['currentactivedevices'])) {
113 $feature['status'] = $data['notifications']['currentactivedevices'];
117 case 'custommenuitems':
118 $custommenuitems = [];
119 $els = rtrim($ms->custommenuitems, "\n");
121 $custommenuitems = explode("\n", $els);
122 // Get unique custom menu urls.
123 $custommenuitems = array_flip(
124 array_map(function($val) {
125 return explode('|', $val)[1];
129 $feature['status'] = count($custommenuitems);
131 // Check language strings.
132 case 'customlanguagestrings':
134 $els = rtrim($ms->customlangstrings, "\n");
136 $langstrings = explode("\n", $els);
137 // Get unique language string ids.
138 $langstrings = array_flip(
139 array_map(function($val) {
140 return explode('|', $val)[0];
144 $feature['status'] = count($langstrings);
146 // Check disabled features strings.
147 case 'disabledfeatures':
148 $feature['status'] = empty($ms->disabledfeatures) ? 0 : count(explode(',', $ms->disabledfeatures));
152 $feature['humanstatus'] = '?/' . $feature['limit'];
153 // Check if we should display the bar and how.
154 if (isset($feature['status']) && is_int($feature['status'])) {
155 $feature['humanstatus'] = $feature['status'] . '/' . $feature['limit'];
156 $feature['showbar'] = 1;
158 if ($feature['status'] == $feature['limit']) {
159 $feature['barclass'] = 'bg-warning';
162 if ($feature['status'] > $feature['limit']) {
163 $feature['barclass'] = 'bg-danger';
164 $feature['humanstatus'] .= ' - ' . get_string('subscriptionlimitsurpassed', 'tool_mobile');
169 $feature['humanstatus'] = empty($feature['enabled']) ? get_string('notincluded') : get_string('included');
171 if (empty($feature['enabled'])) {
172 switch ($feature['name']) {
173 // Check remote themes.
175 if (!empty($CFG->mobilecssurl)) {
176 $feature['message'] = [
177 'type' => 'danger', 'message' => get_string('subscriptionfeaturenotapplied', 'tool_mobile')];
182 if ($output->get_logo_url() || $output->get_compact_logo_url()) {
183 $feature['message'] = [
184 'type' => 'danger', 'message' => get_string('subscriptionfeaturenotapplied', 'tool_mobile')];
187 // Check QR automatic login.
188 case 'qrautomaticlogin':
189 if ($ms->qrcodetype == \tool_mobile\api::QR_CODE_LOGIN) {
190 $feature['message'] = [
191 'type' => 'danger', 'message' => get_string('subscriptionfeaturenotapplied', 'tool_mobile')];
199 usort($data['subscription']['features'],
200 function (array $featurea, array $featureb) {
201 $isfeaturea = !array_key_exists('limit', $featurea);
202 $isfeatureb = !array_key_exists('limit', $featureb);
204 if (!$isfeaturea && $isfeatureb) {