MDL-69089 core_contentbank: Tests for empty content names
[moodle.git] / admin / tool / mobile / classes / output / subscription.php
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/>.
17 /**
18  * Subscription page.
19  *
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
24  */
26 namespace tool_mobile\output;
28 /**
29  * Subscription page.
30  *
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
35  */
36 class subscription implements \renderable, \templatable {
38     /**
39      * Subscription data.
40      *
41      * @var array subscription data
42      */
43     protected $subscriptiondata;
45     /**
46      * Constructor for the class, sets the subscription data.
47      *
48      * @param array $subscriptiondata subscription data
49      * @return void
50      */
51     public function __construct(array $subscriptiondata) {
52         $this->subscriptiondata = $subscriptiondata;
53     }
55     /**
56      * Exports the data.
57      *
58      * @param \renderer_base $output
59      * @return array with the subscription information
60      */
61     public function export_for_template(\renderer_base $output): array {
62         global $CFG;
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']];
73             }
74         }
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'])
92                     ];
93                 }
94             }
95         }
97         // Review features.
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;
106                     continue;
107                 }
109                 switch ($feature['name']) {
110                     // Check active devices.
111                     case 'pushnotificationsdevices':
112                         if (isset($data['notifications']['currentactivedevices'])) {
113                             $feature['status'] = $data['notifications']['currentactivedevices'];
114                         }
115                         break;
116                     // Check menu items.
117                     case 'custommenuitems':
118                         $custommenuitems = [];
119                         $els = rtrim($ms->custommenuitems, "\n");
120                         if (!empty($els)) {
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];
126                                 }, $custommenuitems)
127                             );
128                         }
129                         $feature['status'] = count($custommenuitems);
130                         break;
131                     // Check language strings.
132                     case 'customlanguagestrings':
133                         $langstrings = [];
134                         $els = rtrim($ms->customlangstrings, "\n");
135                         if (!empty($els)) {
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];
141                                 }, $langstrings)
142                             );
143                         }
144                         $feature['status'] = count($langstrings);
145                         break;
146                     // Check disabled features strings.
147                     case 'disabledfeatures':
148                         $feature['status'] = empty($ms->disabledfeatures) ? 0 : count(explode(',', $ms->disabledfeatures));
149                         break;
150                 }
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';
160                     }
162                     if ($feature['status'] > $feature['limit']) {
163                         $feature['barclass'] = 'bg-danger';
164                         $feature['humanstatus'] .= ' - ' . get_string('subscriptionlimitsurpassed', 'tool_mobile');
165                     }
166                 }
168             } else {
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.
174                         case 'remotethemes':
175                             if (!empty($CFG->mobilecssurl)) {
176                                 $feature['message'] = [
177                                     'type' => 'danger', 'message' => get_string('subscriptionfeaturenotapplied', 'tool_mobile')];
178                             }
179                             break;
180                         // Check site logo.
181                         case 'sitelogo':
182                             if ($output->get_logo_url() || $output->get_compact_logo_url()) {
183                                 $feature['message'] = [
184                                     'type' => 'danger', 'message' => get_string('subscriptionfeaturenotapplied', 'tool_mobile')];
185                             }
186                             break;
187                     }
188                 }
189             }
190         }
192         usort($data['subscription']['features'],
193             function (array $featurea, array $featureb) {
194                 $isfeaturea = !array_key_exists('limit', $featurea);
195                 $isfeatureb = !array_key_exists('limit', $featureb);
197                 if (!$isfeaturea && $isfeatureb) {
198                     return 1;
199                 }
200                 return 0;
201             }
202         );
204         return $data;
205     }