MDL-54708 message: notification popover respects message preferences
authorRyan Wyllie <ryan@moodle.com>
Fri, 29 Jul 2016 02:10:32 +0000 (02:10 +0000)
committerMark Nelson <markn@moodle.com>
Fri, 7 Oct 2016 08:26:40 +0000 (16:26 +0800)
lib/db/services.php
message/amd/src/notification_repository.js
message/externallib.php
message/lib.php
message/output/popup/db/events.php [new file with mode: 0644]
message/output/popup/db/install.xml [new file with mode: 0644]
message/output/popup/db/upgrade.php
message/output/popup/message_output_popup.php
message/output/popup/version.php
message/tests/externallib_test.php
message/tests/messagelib_test.php

index 3c815ec..1a461c1 100644 (file)
@@ -708,19 +708,19 @@ $functions = array(
         'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
         'ajax' => true,
     ),
-    'core_message_get_notifications' => array(
+    'core_message_get_popup_notifications' => array(
         'classname' => 'core_message_external',
-        'methodname' => 'get_notifications',
+        'methodname' => 'get_popup_notifications',
         'classpath' => 'message/externallib.php',
-        'description' => 'Retrieve a list of notifications sent and received by a user',
+        'description' => 'Retrieve a list of popup notifications for a user',
         'type' => 'read',
         'ajax' => true,
     ),
-    'core_message_get_unread_notification_count' => array(
+    'core_message_get_unread_popup_notification_count' => array(
         'classname' => 'core_message_external',
-        'methodname' => 'get_unread_notification_count',
+        'methodname' => 'get_unread_popup_notification_count',
         'classpath' => 'message/externallib.php',
-        'description' => 'Retrieve the count of unread notifications for a given user',
+        'description' => 'Retrieve the count of unread popup notifications for a given user',
         'type' => 'read',
         'ajax' => true,
     ),
index eb7f33c..0cf97ad 100644 (file)
@@ -40,7 +40,7 @@ define(['core/ajax', 'core/notification'], function(ajax, notification) {
         }
 
         var request = {
-            methodname: 'core_message_get_notifications',
+            methodname: 'core_message_get_popup_notifications',
             args: args
         };
 
@@ -59,7 +59,7 @@ define(['core/ajax', 'core/notification'], function(ajax, notification) {
      */
     var countUnread = function(args) {
         var request = {
-            methodname: 'core_message_get_unread_notification_count',
+            methodname: 'core_message_get_unread_popup_notification_count',
             args: args
         };
 
index 6e91751..35e9fb2 100644 (file)
@@ -1178,18 +1178,15 @@ class core_message_external extends external_api {
     }
 
     /**
-     * Get notifications parameters description.
+     * Get popup notifications parameters description.
      *
      * @return external_function_parameters
      * @since 3.2
      */
-    public static function get_notifications_parameters() {
+    public static function get_popup_notifications_parameters() {
         return new external_function_parameters(
             array(
                 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
-                'useridfrom' => new external_value(
-                    PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user',
-                    VALUE_DEFAULT, 0),
                 'status' => new external_value(
                     PARAM_ALPHA, 'filter the results to just "read" or "unread" notifications',
                     VALUE_DEFAULT, ''),
@@ -1221,7 +1218,6 @@ class core_message_external extends external_api {
      * @throws invalid_parameter_exception
      * @throws moodle_exception
      * @param  int      $useridto           the user id who received the message
-     * @param  int      $useridfrom         the user id who send the message. -10 or -20 for no-reply or support user
      * @param  string   $status             filter the results to only read or unread notifications
      * @param  bool     $embedpreference    true to embed the recipient user details in the record for each notification
      * @param  bool     $embeduserto        true to embed the recipient user details in the record for each notification
@@ -1232,15 +1228,14 @@ class core_message_external extends external_api {
      * @param  int      $offset             offset the result set by a given amount
      * @return external_description
      */
-    public static function get_notifications($useridto, $useridfrom, $status, $embedpreference,
+    public static function get_popup_notifications($useridto, $status, $embedpreference,
         $embeduserto, $embeduserfrom, $newestfirst, $markasread, $limit, $offset) {
         global $CFG, $USER, $OUTPUT;
 
         $params = self::validate_parameters(
-            self::get_notifications_parameters(),
+            self::get_popup_notifications_parameters(),
             array(
                 'useridto' => $useridto,
-                'useridfrom' => $useridfrom,
                 'status' => $status,
                 'embedpreference' => $embedpreference,
                 'embeduserto' => $embeduserto,
@@ -1256,7 +1251,6 @@ class core_message_external extends external_api {
         self::validate_context($context);
 
         $useridto = $params['useridto'];
-        $useridfrom = $params['useridfrom'];
         $status = $params['status'];
         $embedpreference = $params['embedpreference'];
         $embeduserto = $params['embeduserto'];
@@ -1277,41 +1271,26 @@ class core_message_external extends external_api {
             }
         }
 
-        if (!empty($useridfrom) && $embeduserfrom) {
-            // We use get_user here because the from user can be the noreply or support user.
-            $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST);
-        }
-
         // Check if the current user is the sender/receiver or just a privileged user.
-        if ($useridto != $USER->id and $useridfrom != $USER->id and !$issuperuser) {
+        if ($useridto != $USER->id and !$issuperuser) {
             throw new moodle_exception('accessdenied', 'admin');
         }
 
         $sort = $newestfirst ? 'DESC' : 'ASC';
-        $notifications = message_get_notifications($useridto, $useridfrom, $status, $embeduserto, $embeduserfrom, $sort, $limit, $offset);
+        $notifications = message_get_popup_notifications($useridto, $status, $embeduserto, $embeduserfrom, $sort, $limit, $offset);
 
         if ($notifications) {
-            // In some cases, we don't need to get the to/from user objects from the sql query.
-            $userfromfullname = '';
+            // In some cases, we don't need to get the to user objects from the sql query.
             $usertofullname = '';
 
             // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there.
             if (!empty($useridto) && $embeduserto) {
                 $usertofullname = fullname($userto);
-                // The user from may or may not be filled.
-                if (!empty($useridfrom) && $embeduserfrom) {
-                    $userfromfullname = fullname($userfrom);
-                }
-            } else if (!empty($useridfrom) && $embeduserfrom) {
-                // If the useridto field is empty, the useridfrom must be filled.
-                $userfromfullname = fullname($userfrom);
             }
 
             foreach ($notifications as $notification) {
 
-                if (($useridto == $USER->id and $notification->timeusertodeleted) or
-                        ($useridfrom == $USER->id and $notification->timeuserfromdeleted)) {
-
+                if ($useridto == $USER->id and $notification->timeusertodeleted) {
                     $notification->deleted = true;
                 } else {
                     $notification->deleted = false;
@@ -1319,19 +1298,15 @@ class core_message_external extends external_api {
 
                 // We need to get the user from the query.
                 if ($embeduserfrom) {
-                    if (empty($userfromfullname)) {
-                        // Check for non-reply and support users.
-                        if (core_user::is_real_user($notification->useridfrom)) {
-                            $user = new stdClass();
-                            $user = username_load_fields_from_object($user, $notification, 'userfrom');
-                            $profileurl = new moodle_url('/user/profile.php', array('id' => $notification->useridfrom));
-                            $notification->userfromfullname = fullname($user);
-                            $notification->userfromprofileurl = $profileurl->out();
-                        } else {
-                            $notification->userfromfullname = get_string('coresystem');
-                        }
+                    // Check for non-reply and support users.
+                    if (core_user::is_real_user($notification->useridfrom)) {
+                        $user = new stdClass();
+                        $user = username_load_fields_from_object($user, $notification, 'userfrom');
+                        $profileurl = new moodle_url('/user/profile.php', array('id' => $notification->useridfrom));
+                        $notification->userfromfullname = fullname($user);
+                        $notification->userfromprofileurl = $profileurl->out();
                     } else {
-                        $notification->userfromfullname = $userfromfullname;
+                        $notification->userfromfullname = get_string('coresystem');
                     }
                 }
 
@@ -1378,7 +1353,7 @@ class core_message_external extends external_api {
 
         return array(
             'notifications' => $notifications,
-            'unreadcount' => message_count_unread_notifications($useridto, $useridfrom),
+            'unreadcount' => message_count_unread_popup_notifications($useridto),
         );
     }
 
@@ -1388,7 +1363,7 @@ class core_message_external extends external_api {
      * @return external_single_structure
      * @since 3.2
      */
-    public static function get_notifications_returns() {
+    public static function get_popup_notifications_returns() {
         return new external_single_structure(
             array(
                 'notifications' => new external_multiple_structure(
@@ -1518,13 +1493,10 @@ class core_message_external extends external_api {
      * @return external_function_parameters
      * @since 3.2
      */
-    public static function get_unread_notification_count_parameters() {
+    public static function get_unread_popup_notification_count_parameters() {
         return new external_function_parameters(
             array(
                 'useridto' => new external_value(PARAM_INT, 'the user id who received the message, 0 for any user', VALUE_REQUIRED),
-                'useridfrom' => new external_value(
-                    PARAM_INT, 'the user id who send the message, 0 for any user. -10 or -20 for no-reply or support user',
-                    VALUE_DEFAULT, 0),
             )
         );
     }
@@ -1536,25 +1508,20 @@ class core_message_external extends external_api {
      * @throws invalid_parameter_exception
      * @throws moodle_exception
      * @param  int      $useridto       the user id who received the message
-     * @param  int      $useridfrom     the user id who send the message. -10 or -20 for no-reply or support user
      * @return external_description
      */
-    public static function get_unread_notification_count($useridto, $useridfrom) {
+    public static function get_unread_popup_notification_count($useridto) {
         global $CFG, $USER;
 
         $params = self::validate_parameters(
-            self::get_unread_notification_count_parameters(),
-            array(
-                'useridto' => $useridto,
-                'useridfrom' => $useridfrom,
-            )
+            self::get_unread_popup_notification_count_parameters(),
+            array('useridto' => $useridto)
         );
 
         $context = context_system::instance();
         self::validate_context($context);
 
         $useridto = $params['useridto'];
-        $useridfrom = $params['useridfrom'];
 
         if (!empty($useridto)) {
             if (core_user::is_real_user($useridto)) {
@@ -1564,27 +1531,21 @@ class core_message_external extends external_api {
             }
         }
 
-        if (!empty($useridfrom)) {
-            // We use get_user here because the from user can be the noreply or support user.
-            $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST);
-        }
-
         // Check if the current user is the sender/receiver or just a privileged user.
-        if ($useridto != $USER->id and $useridfrom != $USER->id and
-             !has_capability('moodle/site:readallmessages', $context)) {
+        if ($useridto != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
             throw new moodle_exception('accessdenied', 'admin');
         }
 
-        return message_count_unread_notifications($useridto, $useridfrom);
+        return message_count_unread_popup_notifications($useridto);
     }
 
     /**
-     * Get unread notification count return description.
+     * Get unread popup notification count return description.
      *
      * @return external_single_structure
      * @since 3.2
      */
-    public static function get_unread_notification_count_returns() {
+    public static function get_unread_popup_notification_count_returns() {
         return new external_value(PARAM_INT, 'the user whose blocked users we want to retrieve');
     }
 
index 3470f90..ea7fda6 100644 (file)
@@ -1810,20 +1810,19 @@ function message_get_messages($useridto, $useridfrom = 0, $notifications = -1, $
 }
 
 /**
- * Get notifications to and from the specified users.
+ * Get popup notifications for the specified users.
  *
  * @param  int      $useridto       the user id who received the notification
- * @param  int      $useridfrom     the user id who sent the notification. -10 or -20 for no-reply or support user
  * @param  bool     $status         MESSAGE_READ for retrieving read notifications, MESSAGE_UNREAD for unread, empty for both
- * @param  string   $sort           the column name to order by including optionally direction
  * @param  bool     $embeduserto    embed the to user details in the notification response
  * @param  bool     $embeduserfrom  embed the from user details in the notification response
+ * @param  string   $sort           the column name to order by including optionally direction
  * @param  int      $limit          limit the number of result returned
  * @param  int      $offset         offset the result set by this amount
  * @return array                    array of notification records
- * @since  3.1
+ * @since  3.2
  */
-function message_get_notifications($useridto = 0, $useridfrom = 0, $status = '',
+function message_get_popup_notifications($useridto = 0, $status = '',
     $embeduserto = false, $embeduserfrom = false, $sort = 'DESC', $limit = 0, $offset = 0) {
     global $DB;
 
@@ -1837,30 +1836,23 @@ function message_get_notifications($useridto = 0, $useridfrom = 0, $status = '',
         throw new moodle_exception('invalid parameter: sort: must be "DESC" or "ASC"');
     }
 
+    if (empty($useridto)) {
+        $useridto = $USER->id;
+    }
+
     $params = array();
 
-    $buildtablesql = function($table, $prefix, $additionalfields) use ($useridto, $useridfrom, $embeduserto, $embeduserfrom) {
-        $params = array();
+    $buildtablesql = function($table, $prefix, $additionalfields, $messagestatus)
+            use ($status, $useridto, $embeduserto, $embeduserfrom) {
+
+        $joinsql = '';
         $fields = "concat('$prefix', $prefix.id) as uniqueid, $prefix.id, $prefix.useridfrom, $prefix.useridto,
             $prefix.subject, $prefix.fullmessage, $prefix.fullmessageformat,
             $prefix.fullmessagehtml, $prefix.smallmessage, $prefix.notification, $prefix.contexturl,
             $prefix.contexturlname, $prefix.timecreated, $prefix.timeuserfromdeleted, $prefix.timeusertodeleted,
             $prefix.component, $prefix.eventtype, $additionalfields";
-        $where = '';
-        $joinsql = '';
-
-        if (empty($useridto)) {
-            $where .= " AND $prefix.useridfrom = :{$prefix}useridfrom";
-            $params["{$prefix}useridfrom"] = $useridfrom;
-        } else {
-            $where .= " AND $prefix.useridto = :{$prefix}useridto";
-            $params["{$prefix}useridto"] = $useridto;
-
-            if (!empty($useridfrom)) {
-                $where .= " AND $prefix.useridfrom = :{$prefix}useridfrom";
-                $params["{$prefix}useridfrom"] = $useridfrom;
-            }
-        }
+        $where = " AND $prefix.useridto = :{$prefix}useridto";
+        $params = ["{$prefix}useridto" => $useridto];
 
         if ($embeduserto) {
             $embedprefix = "{$prefix}ut";
@@ -1874,22 +1866,38 @@ function message_get_notifications($useridto = 0, $useridfrom = 0, $status = '',
             $joinsql .= " LEFT JOIN {user} $embedprefix ON $embedprefix.id = $prefix.useridfrom";
         }
 
-        return array(sprintf("SELECT %s FROM %s %s %s WHERE %s.notification = 1 %s", $fields, $table, $prefix, $joinsql, $prefix, $where), $params);
+        if ($messagestatus == MESSAGE_READ) {
+            $isread = '1';
+        } else {
+            $isread = '0';
+        }
+
+        return array(
+            sprintf(
+                "SELECT %s
+                FROM %s %s %s
+                WHERE %s.notification = 1
+                AND %s.id IN (SELECT messageid FROM {message_popup} WHERE isread = %s)
+                %s",
+                $fields, $table, $prefix, $joinsql, $prefix, $prefix, $isread, $where
+            ),
+            $params
+        );
     };
 
     $sql = '';
     switch ($status) {
         case MESSAGE_READ:
-            list($sql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread');
+            list($sql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ);
             $params = array_merge($params, $readparams);
             break;
         case MESSAGE_UNREAD:
-            list($sql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread');
+            list($sql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD);
             $params = array_merge($params, $unreadparams);
             break;
         default:
-            list($readsql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread');
-            list($unreadsql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread');
+            list($readsql, $readparams) = $buildtablesql('{message_read}', 'r', 'r.timeread', MESSAGE_READ);
+            list($unreadsql, $unreadparams) = $buildtablesql('{message}', 'u', '0 as timeread', MESSAGE_UNREAD);
             $sql = sprintf("SELECT * FROM (%s UNION %s) f", $readsql, $unreadsql);
             $params = array_merge($params, $readparams, $unreadparams);
     }
@@ -1903,24 +1911,23 @@ function message_get_notifications($useridto = 0, $useridfrom = 0, $status = '',
  * Count the unread notifications for a user.
  *
  * @param  int      $useridto       the user id who received the notification
- * @param  int      $useridfrom     the user id who sent the notification. -10 or -20 for no-reply or support user
  * @return int                      count of the unread notifications
- * @since  3.1
+ * @since  3.2
  */
-function message_count_unread_notifications($useridto = 0, $useridfrom = 0) {
+function message_count_unread_popup_notifications($useridto = 0) {
     global $USER, $DB;
 
     if (empty($useridto)) {
         $useridto = $USER->id;
     }
 
-    if (!empty($useridfrom)) {
-        return $DB->count_records_select('message', "useridto = ? AND useridfrom = ? AND notification = 1",
-            array($useridto, $useridfrom), "COUNT('id')");
-    } else {
-        return $DB->count_records_select('message', "useridto = ? AND notification = 1",
-            array($useridto), "COUNT('id')");
-    }
+    return $DB->count_records_sql(
+        "SELECT count(id)
+        FROM {message}
+        WHERE id IN (SELECT messageid FROM {message_popup} WHERE isread = 0)
+        AND useridto = ?",
+        [$useridto]
+    );
 }
 
 /**
diff --git a/message/output/popup/db/events.php b/message/output/popup/db/events.php
new file mode 100644 (file)
index 0000000..617b8ad
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Event observers definition.
+ *
+ * @package message_output_popup
+ * @category event
+ * @copyright 2016 Ryan Wyllie <ryan@moodle.com>
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+$observers = array(
+
+    // Message viewed.
+    array(
+        'eventname' => '\core\event\message_viewed',
+        'callback' => 'message_output_popup::message_viewed',
+        'includefile' => '/message/output/popup/message_output_popup.php'
+    )
+);
diff --git a/message/output/popup/db/install.xml b/message/output/popup/db/install.xml
new file mode 100644 (file)
index 0000000..e915f8f
--- /dev/null
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<XMLDB PATH="message/output/popup/db" VERSION="20160729" COMMENT="XMLDB file for Moodle message/output/popup"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
+>
+  <TABLES>
+    <TABLE NAME="message_popup" COMMENT="Keep state of notifications for the popup message processor">
+      <FIELDS>
+        <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
+        <FIELD NAME="messageid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
+        <FIELD NAME="isread" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
+      </FIELDS>
+      <KEYS>
+        <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+      </KEYS>
+      <INDEXES>
+        <INDEX NAME="messageid-isread" UNIQUE="true" FIELDS="messageid, isread"/>
+      </INDEXES>
+    </TABLE>
+  </TABLES>
+</XMLDB>
\ No newline at end of file
index c1834c9..f3f2442 100644 (file)
@@ -30,7 +30,7 @@ defined('MOODLE_INTERNAL') || die();
  * @param int $oldversion The version that we are upgrading from
  */
 function xmldb_message_popup_upgrade($oldversion) {
-    global $CFG;
+    global $CFG, $DB;
 
     // Moodle v2.8.0 release upgrade line.
     // Put any upgrade step following this.
@@ -44,5 +44,32 @@ function xmldb_message_popup_upgrade($oldversion) {
     // Moodle v3.1.0 release upgrade line.
     // Put any upgrade step following this.
 
+    $dbman = $DB->get_manager();
+
+    if ($oldversion < 2016052309) {
+
+        // Define table message_popup to be created.
+        $table = new xmldb_table('message_popup');
+
+        // Adding fields to table message_popup.
+        $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
+        $table->add_field('messageid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
+        $table->add_field('isread', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
+
+        // Adding keys to table message_popup.
+        $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
+
+        // Adding indexes to table message_popup.
+        $table->add_index('messageid-isread', XMLDB_INDEX_UNIQUE, array('messageid', 'isread'));
+
+        // Conditionally launch create table for message_popup.
+        if (!$dbman->table_exists($table)) {
+            $dbman->create_table($table);
+        }
+
+        // Popup savepoint reached.
+        upgrade_plugin_savepoint(true, 2016052309, 'message', 'popup');
+    }
+
     return true;
 }
index f504ad1..8e2b77d 100644 (file)
@@ -32,7 +32,7 @@ require_once($CFG->dirroot.'/message/output/lib.php');
  * @copyright 2008 Luis Rodrigues and Martin Dougiamas
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class message_output_popup extends message_output{
+class message_output_popup extends message_output {
 
     /**
      * Do nothing on send_message.
@@ -41,6 +41,35 @@ class message_output_popup extends message_output{
      * @return true if ok, false if error
      */
     public function send_message($eventdata) {
+        global $DB;
+
+        //hold onto the popup processor id because /admin/cron.php sends a lot of messages at once
+        static $processorid = null;
+
+        //prevent users from getting popup notifications of messages to themselves (happens with forum notifications)
+        if ($eventdata->userfrom->id != $eventdata->userto->id) {
+            if (empty($processorid)) {
+                $processor = $DB->get_record('message_processors', array('name'=>'popup'));
+                $processorid = $processor->id;
+            }
+            $procmessage = new stdClass();
+            $procmessage->unreadmessageid = $eventdata->savedmessageid;
+            $procmessage->processorid     = $processorid;
+
+            //save this message for later delivery
+            $DB->insert_record('message_working', $procmessage);
+
+            if ($eventdata->notification) {
+                if (!$DB->record_exists('message_popup', ['messageid' => $eventdata->savedmessageid, 'isread' => 0])) {
+                    $record = new StdClass();
+                    $record->messageid = $eventdata->savedmessageid;
+                    $record->isread = 0;
+
+                    $DB->insert_record('message_popup', $record);
+                }
+            }
+        }
+
         return true;
     }
 
@@ -73,4 +102,20 @@ class message_output_popup extends message_output{
         global $USER;
         return true;
     }
+
+    /**
+     * Handles the message_viewed event to keep data in sync.
+     *
+     * @param \core\event\base $event The event data
+     */
+    public static function message_viewed(\core\event\base $event) {
+        global $DB;
+
+        if ($record = $DB->get_record('message_popup', ['messageid' => $event->other['messageid']])) {
+            // The id can change when the moving to the message_read table.
+            $record->messageid = $event->objectid;
+            $record->isread = 1;
+            $DB->update_record('message_popup', $record);
+        }
+    }
 }
index 4865f73..483ffd5 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2016052300;        // The current plugin version (Date: YYYYMMDDXX)
+$plugin->version   = 2016052309;        // The current plugin version (Date: YYYYMMDDXX)
 $plugin->requires  = 2016051900;        // Requires this Moodle version
 $plugin->component = 'message_popup';  // Full name of the plugin (used for diagnostics)
index 7454ae3..8827f01 100644 (file)
@@ -63,8 +63,8 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $insert = $DB->insert_record('message', $record);
     }
 
-        /**
-     * Send a fake unread notification.
+    /**
+     * Send a fake unread popup notification.
      *
      * {@link message_send()} does not support transaction, this function will simulate a message
      * sent from a user to another. We should stop using it once {@link message_send()} will support
@@ -76,7 +76,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
      * @param int $timecreated time the message was created.
      * @return int the id of the message
      */
-    protected function send_fake_unread_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
+    protected function send_fake_unread_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
         global $DB;
 
         $record = new stdClass();
@@ -88,11 +88,19 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $record->smallmessage = $message;
         $record->timecreated = $timecreated ? $timecreated : time();
 
-        return $DB->insert_record('message', $record);
+        $id = $DB->insert_record('message', $record);
+
+        $popup = new stdClass();
+        $popup->messageid = $id;
+        $popup->isread = 0;
+
+        $DB->insert_record('message_popup', $popup);
+
+        return $id;
     }
 
     /**
-     * Send a fake read notification.
+     * Send a fake read popup notification.
      *
      * {@link message_send()} does not support transaction, this function will simulate a message
      * sent from a user to another. We should stop using it once {@link message_send()} will support
@@ -104,7 +112,7 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
      * @param int $timecreated time the message was created.
      * @return int the id of the message
      */
-    protected function send_fake_read_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0, $timeread = 0) {
+    protected function send_fake_read_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0, $timeread = 0) {
         global $DB;
 
         $record = new stdClass();
@@ -117,7 +125,15 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $record->timecreated = $timecreated ? $timecreated : time();
         $record->timeread = $timeread ? $timeread : time();
 
-        return $DB->insert_record('message_read', $record);
+        $id = $DB->insert_record('message_read', $record);
+
+        $popup = new stdClass();
+        $popup->messageid = $id;
+        $popup->isread = 1;
+
+        $DB->insert_record('message_popup', $popup);
+
+        return $id;
     }
 
     /**
@@ -878,14 +894,14 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
 
     }
 
-    public function test_get_notifications_no_user_exception() {
+    public function test_get_popup_notifications_no_user_exception() {
         $this->resetAfterTest(true);
 
         $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_notifications(-2132131, 0, '', false, false, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications(-2132131, '', false, false, false, true, false, 0, 0);
     }
 
-    public function test_get_notifications_access_denied_exception() {
+    public function test_get_popup_notifications_access_denied_exception() {
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user();
@@ -893,57 +909,57 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
 
         $this->setUser($user);
         $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_notifications($sender->id, 0, '', false, false, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($sender->id, '', false, false, false, true, false, 0, 0);
     }
 
-    public function test_get_notifications_as_recipient() {
+    public function test_get_popup_notifications_as_recipient() {
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Recipy', 'lastname' => 'Recipient'));
 
         $notificationids = array(
-            $this->send_fake_unread_notification($sender, $recipient),
-            $this->send_fake_unread_notification($sender, $recipient),
-            $this->send_fake_read_notification($sender, $recipient),
-            $this->send_fake_read_notification($sender, $recipient),
+            $this->send_fake_unread_popup_notification($sender, $recipient),
+            $this->send_fake_unread_popup_notification($sender, $recipient),
+            $this->send_fake_read_popup_notification($sender, $recipient),
+            $this->send_fake_read_popup_notification($sender, $recipient),
         );
 
         // Confirm that admin has super powers to retrieve any notifications.
         $this->setAdminUser();
-        $result = core_message_external::get_notifications($recipient->id, 0, '', false, false, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, '', false, false, false, true, false, 0, 0);
         $this->assertCount(4, $result['notifications']);
 
         $this->setUser($recipient);
-        $result = core_message_external::get_notifications($recipient->id, 0, '', false, false, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, '', false, false, false, true, false, 0, 0);
         $this->assertCount(4, $result['notifications']);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_UNREAD, false, true, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_UNREAD, false, false, true, true, false, 0, 0);
         $this->assertCount(2, $result['notifications']);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][0]);
         $this->assertObjectNotHasAttribute('usertofullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][1]);
         $this->assertObjectNotHasAttribute('usertofullname', $result['notifications'][1]);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_UNREAD, true, true, true, false, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_UNREAD, false, true, true, true, false, 0, 0);
         $this->assertCount(2, $result['notifications']);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('usertofullname', $result['notifications'][0]);
         $this->assertObjectHasAttribute('userfromfullname', $result['notifications'][1]);
         $this->assertObjectHasAttribute('usertofullname', $result['notifications'][1]);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_UNREAD, true, true, true, true, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_UNREAD, false, true, true, true, true, 0, 0);
         $this->assertCount(2, $result['notifications']);
         $this->assertEquals(0, $result['unreadcount']);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_UNREAD, true, true, true, true, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_UNREAD, false, true, true, true, true, 0, 0);
         $this->assertCount(0, $result['notifications']);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, MESSAGE_READ, true, true, true, true, 0, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, MESSAGE_READ, false, true, true, true, true, 0, 0);
         $this->assertCount(4, $result['notifications']);
     }
 
-    public function test_get_notification_limit_offset() {
+    public function test_get_popup_notification_limit_offset() {
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Sendy', 'lastname' => 'Sender'));
@@ -952,22 +968,22 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->setUser($recipient);
 
         $notificationids = array(
-            $this->send_fake_unread_notification($sender, $recipient, 'Notification', 1),
-            $this->send_fake_unread_notification($sender, $recipient, 'Notification', 2),
-            $this->send_fake_unread_notification($sender, $recipient, 'Notification', 3),
-            $this->send_fake_unread_notification($sender, $recipient, 'Notification', 4),
-            $this->send_fake_read_notification($sender, $recipient, 'Notification', 5),
-            $this->send_fake_read_notification($sender, $recipient, 'Notification', 6),
-            $this->send_fake_read_notification($sender, $recipient, 'Notification', 7),
-            $this->send_fake_read_notification($sender, $recipient, 'Notification', 8),
+            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 1', 1),
+            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 2', 2),
+            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 3', 3),
+            $this->send_fake_unread_popup_notification($sender, $recipient, 'Notification 4', 4),
+            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 5', 5),
+            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 6', 6),
+            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 7', 7),
+            $this->send_fake_read_popup_notification($sender, $recipient, 'Notification 8', 8),
         );
 
-        $result = core_message_external::get_notifications($recipient->id, 0, '', false, false, true, false, 2, 0);
+        $result = core_message_external::get_popup_notifications($recipient->id, '', false, false, false, true, false, 2, 0);
 
         $this->assertEquals($result['notifications'][0]->id, $notificationids[7]);
         $this->assertEquals($result['notifications'][1]->id, $notificationids[6]);
 
-        $result = core_message_external::get_notifications($recipient->id, 0, '', false, false, true, false, 2, 2);
+        $result = core_message_external::get_popup_notifications($recipient->id, '', false, false, false, true, false, 2, 2);
 
         $this->assertEquals($result['notifications'][0]->id, $notificationids[5]);
         $this->assertEquals($result['notifications'][1]->id, $notificationids[4]);
@@ -1012,37 +1028,37 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->setUser($recipient);
 
         $notificationids = array(
-            $this->send_fake_unread_notification($sender1, $recipient, 'Notification', 1),
-            $this->send_fake_unread_notification($sender1, $recipient, 'Notification', 2),
-            $this->send_fake_unread_notification($sender2, $recipient, 'Notification', 3),
-            $this->send_fake_unread_notification($sender2, $recipient, 'Notification', 4),
-            $this->send_fake_unread_notification($sender3, $recipient, 'Notification', 5),
-            $this->send_fake_unread_notification($sender3, $recipient, 'Notification', 6),
+            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
+            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
+            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
+            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
+            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
+            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
         );
 
         core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id);
-        $readresult = core_message_external::get_notifications($recipient->id, 0, 'read', false, false, true, false, 0, 0);
-        $unreadresult = core_message_external::get_notifications($recipient->id, 0, 'unread', false, false, true, false, 0, 0);
+        $readresult = core_message_external::get_popup_notifications($recipient->id, 'read', false, false, false, true, false, 0, 0);
+        $unreadresult = core_message_external::get_popup_notifications($recipient->id, 'unread', false, false, false, true, false, 0, 0);
 
         $this->assertCount(2, $readresult['notifications']);
         $this->assertCount(4, $unreadresult['notifications']);
 
         core_message_external::mark_all_notifications_as_read($recipient->id, 0);
-        $readresult = core_message_external::get_notifications($recipient->id, 0, 'read', false, false, true, false, 0, 0);
-        $unreadresult = core_message_external::get_notifications($recipient->id, 0, 'unread', false, false, true, false, 0, 0);
+        $readresult = core_message_external::get_popup_notifications($recipient->id, 'read', false, false, false, true, false, 0, 0);
+        $unreadresult = core_message_external::get_popup_notifications($recipient->id, 'unread', false, false, false, true, false, 0, 0);
 
         $this->assertCount(6, $readresult['notifications']);
         $this->assertCount(0, $unreadresult['notifications']);
     }
 
-    public function test_get_unread_notification_count_invalid_user_exception() {
+    public function test_get_unread_popup_notification_count_invalid_user_exception() {
         $this->resetAfterTest(true);
 
         $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_unread_notification_count(-2132131, 0);
+        $result = core_message_external::get_unread_popup_notification_count(-2132131, 0);
     }
 
-    public function test_get_unread_notification_count_access_denied_exception() {
+    public function test_get_unread_popup_notification_count_access_denied_exception() {
         $this->resetAfterTest(true);
 
         $sender = $this->getDataGenerator()->create_user();
@@ -1050,20 +1066,10 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
 
         $this->setUser($user);
         $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_unread_notification_count($sender->id, 0);
-    }
-
-    public function test_get_unread_notification_count_missing_from_user_exception() {
-        $this->resetAfterTest(true);
-
-        $sender = $this->getDataGenerator()->create_user();
-
-        $this->setUser($sender);
-        $this->setExpectedException('moodle_exception');
-        $result = core_message_external::get_unread_notification_count($sender->id, 99999);
+        $result = core_message_external::get_unread_popup_notification_count($sender->id, 0);
     }
 
-    public function test_get_unread_notification_count() {
+    public function test_get_unread_popup_notification_count() {
         $this->resetAfterTest(true);
 
         $sender1 = $this->getDataGenerator()->create_user();
@@ -1074,18 +1080,15 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
         $this->setUser($recipient);
 
         $notificationids = array(
-            $this->send_fake_unread_notification($sender1, $recipient, 'Notification', 1),
-            $this->send_fake_unread_notification($sender1, $recipient, 'Notification', 2),
-            $this->send_fake_unread_notification($sender2, $recipient, 'Notification', 3),
-            $this->send_fake_unread_notification($sender2, $recipient, 'Notification', 4),
-            $this->send_fake_unread_notification($sender3, $recipient, 'Notification', 5),
-            $this->send_fake_unread_notification($sender3, $recipient, 'Notification', 6),
+            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 1),
+            $this->send_fake_unread_popup_notification($sender1, $recipient, 'Notification', 2),
+            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 3),
+            $this->send_fake_unread_popup_notification($sender2, $recipient, 'Notification', 4),
+            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 5),
+            $this->send_fake_unread_popup_notification($sender3, $recipient, 'Notification', 6),
         );
 
-        $count = core_message_external::get_unread_notification_count($recipient->id, $sender1->id);
-        $this->assertEquals($count, 2);
-
-        $count = core_message_external::get_unread_notification_count($recipient->id, 0);
+        $count = core_message_external::get_unread_popup_notification_count($recipient->id);
         $this->assertEquals($count, 6);
     }
 }
index 726bf30..906942f 100644 (file)
@@ -79,7 +79,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Send a fake unread notification.
+     * Send a fake unread popup notification.
      *
      * {@link message_send()} does not support transaction, this function will simulate a message
      * sent from a user to another. We should stop using it once {@link message_send()} will support
@@ -91,7 +91,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
      * @param int $timecreated time the message was created.
      * @return int the id of the message
      */
-    protected function send_fake_unread_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
+    protected function send_fake_unread_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0) {
         global $DB;
 
         $record = new stdClass();
@@ -103,11 +103,19 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $record->smallmessage = $message;
         $record->timecreated = $timecreated ? $timecreated : time();
 
-        return $DB->insert_record('message', $record);
+        $id = $DB->insert_record('message', $record);
+
+        $popup = new stdClass();
+        $popup->messageid = $id;
+        $popup->isread = 0;
+
+        $DB->insert_record('message_popup', $popup);
+
+        return $id;
     }
 
     /**
-     * Send a fake read notification.
+     * Send a fake read popup notification.
      *
      * {@link message_send()} does not support transaction, this function will simulate a message
      * sent from a user to another. We should stop using it once {@link message_send()} will support
@@ -119,7 +127,7 @@ class core_message_messagelib_testcase extends advanced_testcase {
      * @param int $timecreated time the message was created.
      * @return int the id of the message
      */
-    protected function send_fake_read_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0, $timeread = 0) {
+    protected function send_fake_read_popup_notification($userfrom, $userto, $message = 'Hello world!', $timecreated = 0, $timeread = 0) {
         global $DB;
 
         $record = new stdClass();
@@ -132,7 +140,15 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $record->timecreated = $timecreated ? $timecreated : time();
         $record->timeread = $timeread ? $timeread : time();
 
-        return $DB->insert_record('message_read', $record);
+        $id = $DB->insert_record('message_read', $record);
+
+        $popup = new stdClass();
+        $popup->messageid = $id;
+        $popup->isread = 1;
+
+        $DB->insert_record('message_popup', $popup);
+
+        return $id;
     }
 
     /**
@@ -929,66 +945,66 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that the message_get_notifications function will return only read notifications if requested.
+     * Test that the message_get_popup_notifications function will return only read notifications if requested.
      */
-    public function test_message_get_notifications_read_only() {
+    public function test_message_get_popup_notifications_read_only() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 2);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 2', 4);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 2', 4);
 
-        $notifications = message_get_notifications($recipient->id, 0, MESSAGE_READ);
+        $notifications = message_get_popup_notifications($recipient->id, MESSAGE_READ);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
 
         // Check if we request read and unread but there are only read messages, it should
         // still return those correctly.
-        $notifications = message_get_notifications($recipient->id, 0, '');
+        $notifications = message_get_popup_notifications($recipient->id, '');
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
     }
 
     /**
-     * Test that the message_get_notifications function will return only unread notifications if requested.
+     * Test that the message_get_popup_notifications function will return only unread notifications if requested.
      */
-    public function test_message_get_notifications_unread_only() {
+    public function test_message_get_popup_notifications_unread_only() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 1', 2);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 4);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 1', 2);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 4);
 
-        $notifications = message_get_notifications($recipient->id, 0, MESSAGE_UNREAD);
+        $notifications = message_get_popup_notifications($recipient->id, MESSAGE_UNREAD);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
 
         // Check if we request read and unread but there are only read messages, it should
         // still return those correctly.
-        $notifications = message_get_notifications($recipient->id, 0, '');
+        $notifications = message_get_popup_notifications($recipient->id, '');
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
     }
 
     /**
-     * Test that the message_get_notifications function will return the correct notifications when both
+     * Test that the message_get_popup_notifications function will return the correct notifications when both
      * read and unread notifications are included.
      */
-    public function test_message_get_notifications_mixed() {
+    public function test_message_get_popup_notifications_mixed() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 3', 3, 1);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 4', 3, 2);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 5', 4);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
 
-        $notifications = message_get_notifications($recipient->id, 0);
+        $notifications = message_get_popup_notifications($recipient->id);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 4');
@@ -996,44 +1012,44 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $this->assertEquals($notifications[3]->fullmessage, 'Message 2');
         $this->assertEquals($notifications[4]->fullmessage, 'Message 1');
 
-        $notifications = message_get_notifications($recipient->id, 0, MESSAGE_READ);
+        $notifications = message_get_popup_notifications($recipient->id, MESSAGE_READ);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
         $this->assertEquals($notifications[2]->fullmessage, 'Message 1');
 
-        $notifications = message_get_notifications($recipient->id, 0, MESSAGE_UNREAD);
+        $notifications = message_get_popup_notifications($recipient->id, MESSAGE_UNREAD);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 5');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
     }
 
     /**
-     * Test that the message_get_notifications function works correctly with limiting and offsetting
+     * Test that the message_get_popup_notifications function works correctly with limiting and offsetting
      * the result set if requested.
      */
-    public function test_message_get_notifications_all_with_limit_and_offset() {
+    public function test_message_get_popup_notifications_all_with_limit_and_offset() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 3', 3, 1);
-        $this->send_fake_read_notification($sender, $recipient, 'Message 4', 3, 2);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 5', 4);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 6', 5);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 3', 3, 1);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 4', 3, 2);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 5', 4);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 6', 5);
 
-        $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 2, 0);
+        $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 0);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 6');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 5');
 
-        $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 2, 2);
+        $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 2, 2);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
 
-        $notifications = message_get_notifications($recipient->id, 0, '', false, false, 'DESC', 0, 3);
+        $notifications = message_get_popup_notifications($recipient->id, '', false, false, 'DESC', 0, 3);
 
         $this->assertEquals($notifications[0]->fullmessage, 'Message 3');
         $this->assertEquals($notifications[1]->fullmessage, 'Message 2');
@@ -1041,54 +1057,17 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that the message_get_notifications function returns correct values if specifying
-     * a sender.
-     */
-    public function test_message_get_notifications_multiple_senders() {
-        $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
-        $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
-        $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
-        $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
-
-        $this->send_fake_read_notification($sender1, $recipient1, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender1, $recipient1, 'Message 2', 2);
-        $this->send_fake_read_notification($sender1, $recipient2, 'Message 3', 3);
-        $this->send_fake_unread_notification($sender1, $recipient2, 'Message 4', 4);
-        $this->send_fake_read_notification($sender2, $recipient1, 'Message 5', 5);
-        $this->send_fake_unread_notification($sender2, $recipient1, 'Message 6', 6);
-        $this->send_fake_read_notification($sender2, $recipient2, 'Message 7', 7);
-        $this->send_fake_unread_notification($sender2, $recipient2, 'Message 8', 8);
-
-        $notifications = message_get_notifications(0, $sender1->id, '', false, false, 'DESC');
-
-        $this->assertEquals($notifications[0]->fullmessage, 'Message 4');
-        $this->assertEquals($notifications[1]->fullmessage, 'Message 3');
-        $this->assertEquals($notifications[2]->fullmessage, 'Message 2');
-        $this->assertEquals($notifications[3]->fullmessage, 'Message 1');
-
-        $notifications = message_get_notifications(0, $sender1->id, '', false, false, 'DESC', 2, 2);
-
-        $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
-        $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
-
-        $notifications = message_get_notifications($recipient1->id, $sender1->id, '', false, false, 'DESC');
-
-        $this->assertEquals($notifications[0]->fullmessage, 'Message 2');
-        $this->assertEquals($notifications[1]->fullmessage, 'Message 1');
-    }
-
-    /**
-     * Test that the message_get_notifications function returns embedded user details for the
+     * Test that the message_get_popup_notifications function returns embedded user details for the
      * sender if requested.
      */
-    public function test_message_get_notifications_embed_sender() {
+    public function test_message_get_popup_notifications_embed_sender() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
 
-        $notifications = message_get_notifications(0, $sender->id, '', false, true, 'DESC');
+        $notifications = message_get_popup_notifications($recipient->id, '', false, true, 'DESC');
 
         $func = function($type) {
             return function($notification) use ($type) {
@@ -1113,17 +1092,17 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that the message_get_notifications function returns embedded user details for the
+     * Test that the message_get_popup_notifications function returns embedded user details for the
      * recipient if requested.
      */
-    public function test_message_get_notifications_embed_recipient() {
+    public function test_message_get_popup_notifications_embed_recipient() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
 
-        $notifications = message_get_notifications(0, $sender->id, '', true, false, 'DESC');
+        $notifications = message_get_popup_notifications($recipient->id, '', true, false, 'DESC');
 
         $func = function($type) {
             return function($notification) use ($type) {
@@ -1148,16 +1127,16 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that the message_get_notifications function returns embedded all user details.
+     * Test that the message_get_popup_notifications function returns embedded all user details.
      */
-    public function test_message_get_notifications_embed_both() {
+    public function test_message_get_popup_notifications_embed_both() {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_read_notification($sender, $recipient, 'Message 1', 1);
-        $this->send_fake_unread_notification($sender, $recipient, 'Message 2', 2);
+        $this->send_fake_read_popup_notification($sender, $recipient, 'Message 1', 1);
+        $this->send_fake_unread_popup_notification($sender, $recipient, 'Message 2', 2);
 
-        $notifications = message_get_notifications(0, $sender->id, '', true, true, 'DESC');
+        $notifications = message_get_popup_notifications($recipient->id, '', true, true, 'DESC');
 
         $func = function($type) {
             return function($notification) use ($type) {
@@ -1182,27 +1161,25 @@ class core_message_messagelib_testcase extends advanced_testcase {
     }
 
     /**
-     * Test message_count_unread_notifications.
+     * Test message_count_unread_popup_notifications.
      */
-    public function test_message_count_unread_notifications() {
+    public function test_message_count_unread_popup_notifications() {
         $sender1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
         $recipient1 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
         $recipient2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test4', 'lastname' => 'User4'));
 
-        $this->send_fake_unread_notification($sender1, $recipient1);
-        $this->send_fake_unread_notification($sender1, $recipient1);
-        $this->send_fake_unread_notification($sender1, $recipient2);
-        $this->send_fake_unread_notification($sender2, $recipient1);
-        $this->send_fake_unread_notification($sender2, $recipient2);
-        $this->send_fake_unread_notification($sender2, $recipient2);
-
-        $this->assertEquals(message_count_unread_notifications($recipient1->id, $sender1->id), 2);
-        $this->assertEquals(message_count_unread_notifications($recipient2->id, $sender1->id), 1);
-        $this->assertEquals(message_count_unread_notifications($recipient1->id, $sender2->id), 1);
-        $this->assertEquals(message_count_unread_notifications($recipient2->id, $sender2->id), 2);
-        $this->assertEquals(message_count_unread_notifications($recipient1->id, 0), 3);
-        $this->assertEquals(message_count_unread_notifications($recipient2->id, 0), 3);
+        $this->send_fake_unread_popup_notification($sender1, $recipient1);
+        $this->send_fake_unread_popup_notification($sender1, $recipient1);
+        $this->send_fake_unread_popup_notification($sender2, $recipient1);
+        $this->send_fake_unread_popup_notification($sender1, $recipient2);
+        $this->send_fake_unread_popup_notification($sender2, $recipient2);
+        $this->send_fake_unread_popup_notification($sender2, $recipient2);
+        $this->send_fake_unread_popup_notification($sender2, $recipient2);
+        $this->send_fake_unread_popup_notification($sender2, $recipient2);
+
+        $this->assertEquals(message_count_unread_popup_notifications($recipient1->id), 3);
+        $this->assertEquals(message_count_unread_popup_notifications($recipient2->id), 5);
     }
 
 
@@ -1210,9 +1187,9 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_unread_notification($sender, $recipient);
-        $this->send_fake_unread_notification($sender, $recipient);
-        $this->send_fake_unread_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
@@ -1226,15 +1203,15 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $sender2 = $this->getDataGenerator()->create_user(array('firstname' => 'Test3', 'lastname' => 'User3'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_unread_notification($sender1, $recipient);
-        $this->send_fake_unread_notification($sender1, $recipient);
-        $this->send_fake_unread_notification($sender1, $recipient);
+        $this->send_fake_unread_popup_notification($sender1, $recipient);
+        $this->send_fake_unread_popup_notification($sender1, $recipient);
+        $this->send_fake_unread_popup_notification($sender1, $recipient);
         $this->send_fake_message($sender1, $recipient);
         $this->send_fake_message($sender1, $recipient);
         $this->send_fake_message($sender1, $recipient);
-        $this->send_fake_unread_notification($sender2, $recipient);
-        $this->send_fake_unread_notification($sender2, $recipient);
-        $this->send_fake_unread_notification($sender2, $recipient);
+        $this->send_fake_unread_popup_notification($sender2, $recipient);
+        $this->send_fake_unread_popup_notification($sender2, $recipient);
+        $this->send_fake_unread_popup_notification($sender2, $recipient);
         $this->send_fake_message($sender2, $recipient);
         $this->send_fake_message($sender2, $recipient);
         $this->send_fake_message($sender2, $recipient);
@@ -1247,9 +1224,9 @@ class core_message_messagelib_testcase extends advanced_testcase {
         $sender = $this->getDataGenerator()->create_user(array('firstname' => 'Test1', 'lastname' => 'User1'));
         $recipient = $this->getDataGenerator()->create_user(array('firstname' => 'Test2', 'lastname' => 'User2'));
 
-        $this->send_fake_unread_notification($sender, $recipient);
-        $this->send_fake_unread_notification($sender, $recipient);
-        $this->send_fake_unread_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
+        $this->send_fake_unread_popup_notification($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
         $this->send_fake_message($sender, $recipient);
         $this->send_fake_message($sender, $recipient);