Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is 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 2 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   * Popup message processor
  19   *
  20   * @package   message_popup
  21   * @copyright 2008 Luis Rodrigues
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v2 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php'); //included from messagelib (how to fix?)
  26  require_once($CFG->dirroot.'/message/output/lib.php');
  27  
  28  /**
  29   * The popup message processor
  30   *
  31   * @package   message_popup
  32   * @copyright 2008 Luis Rodrigues and Martin Dougiamas
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class message_output_popup extends message_output {
  36  
  37      /**
  38       * Adds notifications to the 'message_popup_notifications' table if applicable.
  39       *
  40       * The reason for this is because we may not want to show all notifications in the notification popover. This
  41       * can happen if the popup processor was disabled when the notification was sent. If the processor is disabled this
  42       * function is never called so the notification will never be added to the 'message_popup_notifications' table.
  43       * Essentially this table is used to filter what notifications to display from the 'notifications' table.
  44       *
  45       * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
  46       * @return true if ok, false if error
  47       */
  48      public function send_message($eventdata) {
  49          global $DB;
  50  
  51          // Prevent users from getting popup notifications from themselves (happens with forum notifications).
  52          if ($eventdata->notification) {
  53              if (($eventdata->userfrom->id != $eventdata->userto->id) ||
  54                  (isset($eventdata->anonymous) && $eventdata->anonymous)) {
  55                  if (!$DB->record_exists('message_popup_notifications', ['notificationid' => $eventdata->savedmessageid])) {
  56                      $record = new stdClass();
  57                      $record->notificationid = $eventdata->savedmessageid;
  58  
  59                      $DB->insert_record('message_popup_notifications', $record);
  60                  }
  61              }
  62          }
  63  
  64          return true;
  65      }
  66  
  67      /**
  68       * Creates necessary fields in the messaging config form.
  69       *
  70       * @param array $preferences An array of user preferences
  71       */
  72      function config_form($preferences) {
  73          return null;
  74      }
  75  
  76      /**
  77       * Parses the submitted form data and saves it into preferences array.
  78       *
  79       * @param stdClass $form preferences form class
  80       * @param array $preferences preferences array
  81       */
  82      public function process_form($form, &$preferences) {
  83          return true;
  84      }
  85  
  86      /**
  87       * Loads the config data from database to put on the form during initial form display
  88       *
  89       * @param array $preferences preferences array
  90       * @param int $userid the user id
  91       */
  92      public function load_data(&$preferences, $userid) {
  93          global $USER;
  94          return true;
  95      }
  96  
  97      /**
  98       * Don't show this processor on the message preferences page. The user can't disable
  99       * the notifications for user-to-user messaging.
 100       *
 101       * @return bool
 102       */
 103      public function has_message_preferences() {
 104          return false;
 105      }
 106  
 107      /**
 108       * Determines if this processor should process a message regardless of user preferences or site settings.
 109       *
 110       * @return bool
 111       */
 112      public function force_process_messages() {
 113          global $CFG;
 114  
 115          return !empty($CFG->messaging);
 116      }
 117  
 118      /**
 119       * Remove all popup notifications up to specified time
 120       *
 121       * @param int $notificationdeletetime
 122       * @return void
 123       */
 124      public function cleanup_all_notifications(int $notificationdeletetime): void {
 125          global $DB;
 126  
 127          $DB->delete_records_select('message_popup_notifications',
 128              'notificationid IN (SELECT id FROM {notifications} WHERE timecreated < ?)', [$notificationdeletetime]);
 129      }
 130  
 131      /**
 132       * Remove read popup notifications up to specified time
 133       *
 134       * @param int $notificationdeletetime
 135       * @return void
 136       */
 137      public function cleanup_read_notifications(int $notificationdeletetime): void {
 138          global $DB;
 139  
 140          $DB->delete_records_select('message_popup_notifications',
 141              'notificationid IN (SELECT id FROM {notifications} WHERE timeread < ?)', [$notificationdeletetime]);
 142      }
 143  }