Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Lib functions, mostly callbacks.
  19   *
  20   * @package    tool_mobile
  21   * @copyright  2017 Juan Leyva
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Callback to add head elements.
  29   *
  30   * @return str valid html head content
  31   * @since  Moodle 3.3
  32   */
  33  function tool_mobile_before_standard_html_head() {
  34      global $CFG, $PAGE;
  35      $output = '';
  36      // Smart App Banners meta tag is only displayed if mobile services are enabled and configured.
  37      if (!empty($CFG->enablemobilewebservice)) {
  38          $mobilesettings = get_config('tool_mobile');
  39          if (!empty($mobilesettings->enablesmartappbanners)) {
  40              if (!empty($mobilesettings->iosappid)) {
  41                  $output .= '<meta name="apple-itunes-app" content="app-id=' . s($mobilesettings->iosappid) . ', ';
  42                  $output .= 'app-argument=' . $PAGE->url->out() . '"/>';
  43              }
  44  
  45              if (!empty($mobilesettings->androidappid)) {
  46                  $mobilemanifesturl = "$CFG->wwwroot/$CFG->admin/tool/mobile/mobile.webmanifest.php";
  47                  $output .= '<link rel="manifest" href="'.$mobilemanifesturl.'" />';
  48              }
  49          }
  50      }
  51      return $output;
  52  }
  53  
  54  /**
  55   * Generate the app download url to promote moodle mobile.
  56   *
  57   * @return moodle_url|void App download moodle_url object or return if setuplink is not set.
  58   */
  59  function tool_mobile_create_app_download_url() {
  60      global $CFG;
  61  
  62      $mobilesettings = get_config('tool_mobile');
  63  
  64      if (empty($mobilesettings->setuplink)) {
  65          return;
  66      }
  67  
  68      $downloadurl = new moodle_url($mobilesettings->setuplink);
  69  
  70      // Do not update the URL if it is a custom one (we may break it completely).
  71      if ($mobilesettings->setuplink != 'https://download.moodle.org/mobile') {
  72          return $downloadurl;
  73      }
  74  
  75      $downloadurl->param('version', $CFG->version);
  76      $downloadurl->param('lang', current_language());
  77  
  78      if (!empty($mobilesettings->iosappid)) {
  79          $downloadurl->param('iosappid', $mobilesettings->iosappid);
  80      }
  81  
  82      if (!empty($mobilesettings->androidappid)) {
  83          $downloadurl->param('androidappid', $mobilesettings->androidappid);
  84      }
  85  
  86      return $downloadurl;
  87  }
  88  
  89  /**
  90   * Checks if the given user has a mobile token (has used recently the app).
  91   *
  92   * @param  int $userid the user to check
  93   * @return bool        true if the user has a token, false otherwise.
  94   */
  95  function tool_mobile_user_has_token($userid) {
  96      global $DB;
  97  
  98      $sql = "SELECT 1
  99                FROM {external_tokens} t, {external_services} s
 100               WHERE t.externalserviceid = s.id
 101                 AND s.enabled = 1
 102                 AND s.shortname IN ('moodle_mobile_app', 'local_mobile')
 103                 AND t.userid = ?";
 104  
 105      return $DB->record_exists_sql($sql, [$userid]);
 106  }
 107  
 108  /**
 109   * User profile page callback.
 110   *
 111   * Used add a section about the moodle mobile app.
 112   *
 113   * @param \core_user\output\myprofile\tree $tree My profile tree where the setting will be added.
 114   * @param stdClass $user The user object.
 115   * @param bool $iscurrentuser Is this the current user viewing
 116   * @return void Return if the mobile web services setting is disabled or if not the current user.
 117   */
 118  function tool_mobile_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser) {
 119      global $CFG;
 120  
 121      if (empty($CFG->enablemobilewebservice)) {
 122          return;
 123      }
 124  
 125      if (!$iscurrentuser) {
 126          return;
 127      }
 128  
 129      $newnodes = [];
 130      $mobilesettings = get_config('tool_mobile');
 131  
 132      // Check if we should display a QR code.
 133      if (!empty($mobilesettings->qrcodetype)) {
 134          $mobileqr = null;
 135          $qrcodeforappstr = get_string('qrcodeformobileappaccess', 'tool_mobile');
 136  
 137          if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_LOGIN && is_https()) {
 138  
 139              if (is_siteadmin() || \core\session\manager::is_loggedinas()) {
 140                  $mobileqr = get_string('qrsiteadminsnotallowed', 'tool_mobile');
 141              } else {
 142                  $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
 143  
 144                  $minutes = tool_mobile\api::LOGIN_QR_KEY_TTL / MINSECS;
 145                  $mobileqr = html_writer::tag('p', get_string('qrcodeformobileapploginabout', 'tool_mobile', $minutes));
 146                  $mobileqr .= html_writer::link('#qrcode', get_string('viewqrcode', 'tool_mobile'),
 147                      ['class' => 'btn btn-primary mt-2', 'data-toggle' => 'collapse',
 148                      'role' => 'button', 'aria-expanded' => 'false']);
 149                  $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr), 'collapse mt-4', ['id' => 'qrcode']);
 150              }
 151  
 152          } else if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_URL) {
 153              $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
 154  
 155              $mobileqr = get_string('qrcodeformobileappurlabout', 'tool_mobile');
 156              $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr));
 157          }
 158  
 159          if ($mobileqr) {
 160              $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappqr', $qrcodeforappstr, null, null, $mobileqr);
 161          }
 162      }
 163  
 164      // Check if the user is using the app, encouraging him to use it otherwise.
 165      $userhastoken = tool_mobile_user_has_token($user->id);
 166      $mobilestrconnected = null;
 167  
 168      if ($userhastoken) {
 169          $mobilestrconnected = get_string('mobileappconnected', 'tool_mobile');
 170      } else if ($url = tool_mobile_create_app_download_url()) {
 171           $mobilestrconnected = get_string('mobileappenabled', 'tool_mobile', $url->out());
 172      }
 173  
 174      if ($mobilestrconnected) {
 175          $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null);
 176      }
 177  
 178      // Add nodes, if any.
 179      if (!empty($newnodes)) {
 180          $mobilecat = new core_user\output\myprofile\category('mobile', get_string('mobileapp', 'tool_mobile'), 'loginactivity');
 181          $tree->add_category($mobilecat);
 182  
 183          foreach ($newnodes as $node) {
 184              $tree->add_node($node);
 185          }
 186      }
 187  }
 188  
 189  /**
 190   * Callback to add footer elements.
 191   *
 192   * @return str valid html footer content
 193   * @since  Moodle 3.4
 194   */
 195  function tool_mobile_standard_footer_html() {
 196      global $CFG;
 197      $output = '';
 198      if (!empty($CFG->enablemobilewebservice) && $url = tool_mobile_create_app_download_url()) {
 199          $output .= html_writer::link($url, get_string('getmoodleonyourmobile', 'tool_mobile'));
 200      }
 201      return $output;
 202  }
 203  
 204  /**
 205   * Callback to be able to change a message/notification data per processor.
 206   *
 207   * @param  str $procname    processor name
 208   * @param  stdClass $data   message or notification data
 209   */
 210  function tool_mobile_pre_processor_message_send($procname, $data) {
 211      global $CFG;
 212  
 213      if (empty($CFG->enablemobilewebservice)) {
 214          return;
 215      }
 216  
 217      if (empty($data->userto)) {
 218          return;
 219      }
 220  
 221      // Only hack email.
 222      if ($procname == 'email') {
 223  
 224          // Send a message only when there is an HTML version of the email, mobile services are enabled,
 225          // the user receiving the message has not used the app and there is an app download URL set.
 226          if (empty($data->fullmessagehtml)) {
 227              return;
 228          }
 229  
 230          if (!$url = tool_mobile_create_app_download_url()) {
 231              return;
 232          }
 233  
 234          $userto = is_object($data->userto) ? $data->userto->id : $data->userto;
 235          if (tool_mobile_user_has_token($userto)) {
 236              return;
 237          }
 238  
 239          $data->fullmessagehtml .= html_writer::tag('p', get_string('readingthisemailgettheapp', 'tool_mobile', $url->out()));
 240      }
 241  }