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 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  namespace core\task;
  18  
  19  /**
  20   * Adhoc task that send login notifications.
  21   *
  22   * @package    core
  23   * @copyright  2021 Moodle Pty Ltd.
  24   * @author     Juan Leyva <juan@moodle.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class send_login_notifications extends adhoc_task {
  28  
  29      use \core\task\logging_trait;
  30  
  31      /**
  32       * Run the adhoc task and preform the backup.
  33       */
  34      public function execute() {
  35          global $CFG, $DB, $SITE, $USER, $PAGE;
  36  
  37          $customdata = $this->get_custom_data();
  38  
  39          // First check the mobile app special case, to detect if the user is not using a new device after login from a different IP.
  40          if (!empty($customdata->ismoodleapp)) {
  41              $where = 'userid = ? AND timecreated >= ?';
  42              if (!$DB->count_records_select('user_devices', $where, [$USER->id, $customdata->logintime])) {
  43                  // Do nothing, seems to be the same person doing login from a new IP using a known device.
  44                  return;
  45              }
  46          }
  47  
  48          $this->log_start("Sending login notification to {$USER->username}");
  49          $sitename = format_string($SITE->fullname);
  50          $siteurl = $CFG->wwwroot;
  51          $userfullname = fullname($USER);
  52          $username = $USER->username;
  53          $useremail = ($USER->username != $USER->email) ? $USER->email : '';
  54          $logindevice = $customdata->ismoodleapp ? get_string('mobileapp', 'tool_mobile') : '';
  55          $logindevice .= ' ' . $customdata->useragent;
  56          $loginip = $customdata->loginip;
  57          $logintime = userdate($customdata->logintime);
  58  
  59          $changepasswordlink = (new \moodle_url('/user/preferences.php', ['userid' => $USER->id]))->out(false);
  60          // Find a better final URL for changing password.
  61          $userauth = get_auth_plugin($USER->auth);
  62          if ($userauth->can_change_password()) {
  63              if ($changepwurl = $userauth->change_password_url()) {
  64                  $changepasswordlink = (string) $changepwurl;
  65              } else {
  66                  $changepasswordlink = (new \moodle_url('/login/change_password.php'))->out(false);
  67              }
  68          }
  69  
  70          $eventdata = new \core\message\message();
  71          $eventdata->courseid          = SITEID;
  72          $eventdata->component         = 'moodle';
  73          $eventdata->name              = 'newlogin';
  74          $eventdata->userfrom          = \core_user::get_noreply_user();
  75          $eventdata->userto            = $USER;
  76          $eventdata->notification      = 1;
  77          $eventdata->subject           = get_string('newloginnotificationtitle', 'moodle', $sitename);
  78          $eventdata->fullmessageformat = FORMAT_HTML;
  79          $info = compact('sitename', 'siteurl', 'userfullname', 'username', 'useremail',
  80              'logindevice', 'logintime', 'loginip', 'changepasswordlink');
  81          $eventdata->fullmessagehtml   = get_string('newloginnotificationbodyfull', 'moodle', $info);
  82          $eventdata->fullmessage       = html_to_text($eventdata->fullmessagehtml);
  83          $eventdata->smallmessage      = get_string('newloginnotificationbodysmall', 'moodle', $username);
  84  
  85          $userpicture = new \user_picture($USER);
  86          $userpicture->size = 1; // Use f1 size.
  87          $userpicture->includetoken = $USER->id; // Generate an out-of-session token for the user receiving the message.
  88          $eventdata->customdata = ['notificationiconurl' => $userpicture->get_url($PAGE)->out(false)];
  89  
  90          if (message_send($eventdata)) {
  91              $this->log_finish("Notification successfully sent");
  92          } else {
  93              $this->log_finish("Failed to send notification");
  94          }
  95      }
  96  }