Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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   * 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   */
  25  
  26  namespace tool_mobile\output;
  27  
  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 {
  37  
  38      /**
  39       * Subscription data.
  40       *
  41       * @var array subscription data
  42       */
  43      protected $subscriptiondata;
  44  
  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      }
  54  
  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;
  63  
  64          $ms = get_config('tool_mobile');    // Get mobile settings.
  65  
  66          $data = $this->subscriptiondata;
  67          $data['appsportalurl'] = \tool_mobile\api::MOODLE_APPS_PORTAL_URL;
  68  
  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']);
  76  
  77          // Now prepare statistics information.
  78          if (isset($data['statistics']['notifications'])) {
  79              $data['notifications'] = $data['statistics']['notifications'];
  80              unset($data['statistics']['notifications']);
  81  
  82              // Find current month data.
  83              $data['notifications']['currentactivedevices'] = 0;
  84  
  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          }
  96  
  97          // Review features.
  98          foreach ($data['subscription']['features'] as &$feature) {
  99  
 100              // Check the type of features, if it is a limitation or functionality feature.
 101              if (array_key_exists('limit', $feature)) {
 102  
 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                  }
 108  
 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                  }
 151  
 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;
 157  
 158                      if ($feature['status'] == $feature['limit']) {
 159                          $feature['barclass'] = 'bg-warning';
 160                      }
 161  
 162                      if ($feature['status'] > $feature['limit']) {
 163                          $feature['barclass'] = 'bg-danger';
 164                          $feature['humanstatus'] .= ' - ' . get_string('subscriptionlimitsurpassed', 'tool_mobile');
 165                      }
 166                  }
 167  
 168              } else {
 169                  $feature['humanstatus'] = empty($feature['enabled']) ? get_string('notincluded') : get_string('included');
 170  
 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                          // 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')];
 192                              }
 193                              break;
 194                      }
 195                  }
 196              }
 197          }
 198  
 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);
 203  
 204                  if (!$isfeaturea && $isfeatureb) {
 205                      return 1;
 206                  }
 207                  return 0;
 208              }
 209          );
 210  
 211          return $data;
 212      }
 213  }