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 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   * This file defines an adhoc task to send notifications.
  19   *
  20   * @package    tool_monitor
  21   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_monitor;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Adhock class, used to send notifications to users.
  31   *
  32   * @since      Moodle 2.8
  33   * @package    tool_monitor
  34   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class notification_task extends \core\task\adhoc_task {
  38  
  39      /**
  40       * Send out messages.
  41       */
  42      public function execute() {
  43          foreach ($this->get_custom_data() as $data) {
  44              $eventobj = $data->event;
  45              $subscriptionids = $data->subscriptionids;
  46              foreach ($subscriptionids as $id) {
  47                  if ($message = $this->generate_message($id, $eventobj)) {
  48                      mtrace("Sending message to the user with id " . $message->userto->id . " for the subscription with id $id...");
  49                      message_send($message);
  50                      mtrace("Sent.");
  51                  }
  52              }
  53          }
  54      }
  55  
  56      /**
  57       * Generates the message object for a give subscription and event.
  58       *
  59       * @param int $subscriptionid Subscription instance
  60       * @param \stdClass $eventobj Event data
  61       *
  62       * @return false|\stdClass message object
  63       */
  64      protected function generate_message($subscriptionid, \stdClass $eventobj) {
  65  
  66          try {
  67              $subscription = subscription_manager::get_subscription($subscriptionid);
  68          } catch (\dml_exception $e) {
  69              // Race condition, someone deleted the subscription.
  70              return false;
  71          }
  72          $user = \core_user::get_user($subscription->userid);
  73          if (empty($user)) {
  74              // User doesn't exist. Should never happen, nothing to do return.
  75              return false;
  76          }
  77          $context = \context_user::instance($user->id, IGNORE_MISSING);
  78          if ($context === false) {
  79              // User context doesn't exist. Should never happen, nothing to do return.
  80              return false;
  81          }
  82  
  83          $template = $subscription->template;
  84          $template = $this->replace_placeholders($template, $subscription, $eventobj, $context);
  85          $htmlmessage = format_text($template, $subscription->templateformat, array('context' => $context));
  86          $msgdata = new \core\message\message();
  87          $msgdata->courseid          = empty($subscription->courseid) ? SITEID : $subscription->courseid;
  88          $msgdata->component         = 'tool_monitor'; // Your component name.
  89          $msgdata->name              = 'notification'; // This is the message name from messages.php.
  90          $msgdata->userfrom          = \core_user::get_noreply_user();
  91          $msgdata->userto            = $user;
  92          $msgdata->subject           = $subscription->get_name($context);
  93          $msgdata->fullmessage       = html_to_text($htmlmessage);
  94          $msgdata->fullmessageformat = FORMAT_PLAIN;
  95          $msgdata->fullmessagehtml   = $htmlmessage;
  96          $msgdata->smallmessage      = '';
  97          $msgdata->notification      = 1; // This is only set to 0 for personal messages between users.
  98  
  99          return $msgdata;
 100      }
 101  
 102      /**
 103       * Replace place holders in the template with respective content.
 104       *
 105       * @param string $template Message template.
 106       * @param subscription $subscription subscription instance
 107       * @param \stdclass $eventobj Event data
 108       * @param \context $context context object
 109       *
 110       * @return mixed final template string.
 111       */
 112      protected function replace_placeholders($template, subscription $subscription, $eventobj, $context) {
 113          $template = str_replace('{link}', $eventobj->link, $template);
 114          if ($eventobj->contextlevel == CONTEXT_MODULE && !empty($eventobj->contextinstanceid)
 115              && (strpos($template, '{modulelink}') !== false)) {
 116              $cm = get_fast_modinfo($eventobj->courseid)->get_cm($eventobj->contextinstanceid);
 117              $modulelink = $cm->url;
 118              $template = str_replace('{modulelink}', $modulelink, $template);
 119          }
 120          $template = str_replace('{rulename}', $subscription->get_name($context), $template);
 121          $template = str_replace('{description}', $subscription->get_description($context), $template);
 122          $template = str_replace('{eventname}', $subscription->get_event_name(), $template);
 123  
 124          return $template;
 125      }
 126  }