Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 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   * Return the user mobile app WebService access token.
  91   *
  92   * @param  int $userid the user to return the token from
  93   * @return stdClass|false the token or false if the token doesn't exists
  94   * @since  3.10
  95   */
  96  function tool_mobile_get_token($userid) {
  97      global $DB;
  98  
  99      $sql = "SELECT t.*
 100                FROM {external_tokens} t, {external_services} s
 101               WHERE t.externalserviceid = s.id
 102                 AND s.enabled = 1
 103                 AND s.shortname IN ('moodle_mobile_app', 'local_mobile')
 104                 AND t.userid = ?";
 105  
 106      return $DB->get_record_sql($sql, [$userid], IGNORE_MULTIPLE);
 107  }
 108  
 109  /**
 110   * Checks if the given user has a mobile token (has used recently the app).
 111   *
 112   * @param  int $userid the user to check
 113   * @return bool true if the user has a token, false otherwise.
 114   */
 115  function tool_mobile_user_has_token($userid) {
 116  
 117      return !empty(tool_mobile_get_token($userid));
 118  }
 119  
 120  /**
 121   * User profile page callback.
 122   *
 123   * Used add a section about the moodle mobile app.
 124   *
 125   * @param \core_user\output\myprofile\tree $tree My profile tree where the setting will be added.
 126   * @param stdClass $user The user object.
 127   * @param bool $iscurrentuser Is this the current user viewing
 128   * @return void Return if the mobile web services setting is disabled or if not the current user.
 129   */
 130  function tool_mobile_myprofile_navigation(\core_user\output\myprofile\tree $tree, $user, $iscurrentuser) {
 131      global $CFG;
 132  
 133      if (empty($CFG->enablemobilewebservice)) {
 134          return;
 135      }
 136  
 137      $newnodes = [];
 138      $mobilesettings = get_config('tool_mobile');
 139  
 140      // Check if we should display a QR code.
 141      if ($iscurrentuser && !empty($mobilesettings->qrcodetype)) {
 142          $mobileqr = null;
 143          $qrcodeforappstr = get_string('qrcodeformobileappaccess', 'tool_mobile');
 144  
 145          if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_LOGIN && is_https()) {
 146  
 147              if (is_siteadmin() || \core\session\manager::is_loggedinas()) {
 148                  $mobileqr = get_string('qrsiteadminsnotallowed', 'tool_mobile');
 149              } else {
 150                  $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
 151  
 152                  $qrkeyttl = !empty($mobilesettings->qrkeyttl) ? $mobilesettings->qrkeyttl : tool_mobile\api::LOGIN_QR_KEY_TTL;
 153                  $mobileqr = html_writer::tag('p', get_string('qrcodeformobileapploginabout', 'tool_mobile',
 154                      format_time($qrkeyttl)));
 155                  $mobileqr .= html_writer::link('#qrcode', get_string('viewqrcode', 'tool_mobile'),
 156                      ['class' => 'btn btn-primary mt-2', 'data-toggle' => 'collapse',
 157                      'role' => 'button', 'aria-expanded' => 'false']);
 158                  $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr), 'collapse mt-4', ['id' => 'qrcode']);
 159              }
 160  
 161          } else if ($mobilesettings->qrcodetype == tool_mobile\api::QR_CODE_URL) {
 162              $qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
 163  
 164              $mobileqr = get_string('qrcodeformobileappurlabout', 'tool_mobile');
 165              $mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr));
 166          }
 167  
 168          if ($mobileqr) {
 169              $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappqr', $qrcodeforappstr, null, null, $mobileqr);
 170          }
 171      }
 172  
 173      // Check if the user is using the app, encouraging him to use it otherwise.
 174      if ($iscurrentuser || is_siteadmin()) {
 175          $usertoken = tool_mobile_get_token($user->id);
 176          $mobilestrconnected = null;
 177          $mobilelastaccess = null;
 178  
 179          if ($usertoken) {
 180              $mobilestrconnected = get_string('lastsiteaccess');
 181              if ($usertoken->lastaccess) {
 182                  $mobilelastaccess = userdate($usertoken->lastaccess) . "&nbsp; (" . format_time(time() - $usertoken->lastaccess) . ")";
 183                  // Logout link.
 184                  $validtoken = empty($usertoken->validuntil) || time() < $usertoken->validuntil;
 185                  if ($iscurrentuser && $validtoken) {
 186                      $url = new moodle_url('/'.$CFG->admin.'/tool/mobile/logout.php', ['sesskey' => sesskey()]);
 187                      $logoutlink = html_writer::link($url, get_string('logout'));
 188                      $mobilelastaccess .= "&nbsp; ($logoutlink)";
 189                  }
 190              } else {
 191                  // We should not reach this point.
 192                  $mobilelastaccess = get_string("never");
 193              }
 194          } else if ($url = tool_mobile_create_app_download_url()) {
 195               $mobilestrconnected = get_string('mobileappenabled', 'tool_mobile', $url->out());
 196          }
 197  
 198          if ($mobilestrconnected) {
 199              $newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null, null,
 200                  $mobilelastaccess);
 201          }
 202      }
 203  
 204      // Add nodes, if any.
 205      if (!empty($newnodes)) {
 206          $mobilecat = new core_user\output\myprofile\category('mobile', get_string('mobileapp', 'tool_mobile'), 'loginactivity');
 207          $tree->add_category($mobilecat);
 208  
 209          foreach ($newnodes as $node) {
 210              $tree->add_node($node);
 211          }
 212      }
 213  }
 214  
 215  /**
 216   * Callback to add footer elements.
 217   *
 218   * @return str valid html footer content
 219   * @since  Moodle 3.4
 220   */
 221  function tool_mobile_standard_footer_html() {
 222      global $CFG;
 223      $output = '';
 224      if (!empty($CFG->enablemobilewebservice) && $url = tool_mobile_create_app_download_url()) {
 225          $output .= html_writer::link($url, get_string('getmoodleonyourmobile', 'tool_mobile'), ['class' => 'mobilelink']);
 226      }
 227      return $output;
 228  }
 229  
 230  /**
 231   * Callback to be able to change a message/notification data per processor.
 232   *
 233   * @param  str $procname    processor name
 234   * @param  stdClass $data   message or notification data
 235   */
 236  function tool_mobile_pre_processor_message_send($procname, $data) {
 237      global $CFG;
 238  
 239      if (empty($CFG->enablemobilewebservice)) {
 240          return;
 241      }
 242  
 243      if (empty($data->userto)) {
 244          return;
 245      }
 246  
 247      // Only hack email.
 248      if ($procname == 'email') {
 249  
 250          // Send a message only when there is an HTML version of the email, mobile services are enabled,
 251          // the user receiving the message has not used the app and there is an app download URL set.
 252          if (empty($data->fullmessagehtml)) {
 253              return;
 254          }
 255  
 256          if (!$url = tool_mobile_create_app_download_url()) {
 257              return;
 258          }
 259  
 260          $userto = is_object($data->userto) ? $data->userto->id : $data->userto;
 261          if (tool_mobile_user_has_token($userto)) {
 262              return;
 263          }
 264  
 265          $data->fullmessagehtml .= html_writer::tag('p', get_string('readingthisemailgettheapp', 'tool_mobile', $url->out()));
 266      }
 267  }