Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * Base trait for message popup tests.
  19   *
  20   * @package    message_popup
  21   * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  trait message_popup_test_helper {
  28      /**
  29       * Send a fake unread popup notification.
  30       *
  31       * {@link message_send()} does not support transaction, this function will simulate a message
  32       * sent from a user to another. We should stop using it once {@link message_send()} will support
  33       * transactions. This is not clean at all, this is just used to add rows to the table.
  34       *
  35       * @param stdClass $userfrom user object of the one sending the message.
  36       * @param stdClass $userto user object of the one receiving the message.
  37       * @param string $message message to send.
  38       * @param int $timecreated time the message was created.
  39       * @return int the id of the message
  40       */
  41      protected function send_fake_unread_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
  42          global $DB;
  43  
  44          $record = new stdClass();
  45          $record->useridfrom = $userfrom->id;
  46          $record->useridto = $userto->id;
  47          $record->notification = 1;
  48          $record->subject = 'No subject';
  49          $record->fullmessage = $message;
  50          $record->smallmessage = $message;
  51          $record->timecreated = $timecreated ? $timecreated : time();
  52          $record->customdata  = json_encode(['datakey' => 'data']);
  53  
  54          $id = $DB->insert_record('notifications', $record);
  55  
  56          $popup = new stdClass();
  57          $popup->notificationid = $id;
  58  
  59          $DB->insert_record('message_popup_notifications', $popup);
  60  
  61          return $id;
  62      }
  63  
  64      /**
  65       * Send a fake read popup notification.
  66       *
  67       * {@link message_send()} does not support transaction, this function will simulate a message
  68       * sent from a user to another. We should stop using it once {@link message_send()} will support
  69       * transactions. This is not clean at all, this is just used to add rows to the table.
  70       *
  71       * @param stdClass $userfrom user object of the one sending the message.
  72       * @param stdClass $userto user object of the one receiving the message.
  73       * @param string $message message to send.
  74       * @param int $timecreated time the message was created.
  75       * @param int $timeread the the message was read
  76       * @return int the id of the message
  77       */
  78      protected function send_fake_read_popup_notification($userfrom, $userto, $message = 'Hello world!',
  79                                                           $timecreated = 0, $timeread = 0) {
  80          global $DB;
  81  
  82          $record = new stdClass();
  83          $record->useridfrom = $userfrom->id;
  84          $record->useridto = $userto->id;
  85          $record->notification = 1;
  86          $record->subject = 'No subject';
  87          $record->fullmessage = $message;
  88          $record->smallmessage = $message;
  89          $record->timecreated = $timecreated ? $timecreated : time();
  90          $record->timeread = $timeread ? $timeread : time();
  91  
  92          $record->id = $DB->insert_record('notifications', $record);
  93  
  94          // Mark it as read.
  95          \core_message\api::mark_notification_as_read($record);
  96  
  97          $popup = new stdClass();
  98          $popup->notificationid = $record->id;
  99          $DB->insert_record('message_popup_notifications', $popup);
 100  
 101          return $record->id;
 102      }
 103  }