/** * Get notifications function. * * @since 3.2 * @throws invalid_parameter_exception * @throws moodle_exception * @param int $useridto the user id who received the message * @param bool $newestfirst true for ordering by newest first, false for oldest first * @param int $limit the number of results to return * @param int $offset offset the result set by a given amount * @return external_description */ public static function get_popup_notifications($useridto, $newestfirst, $limit, $offset) { global $USER, $PAGE; $params = self::validate_parameters(self::get_popup_notifications_parameters(), array('useridto' => $useridto, 'newestfirst' => $newestfirst, 'limit' => $limit, 'offset' => $offset)); $context = context_system::instance(); self::validate_context($context); $useridto = $params['useridto']; $newestfirst = $params['newestfirst']; $limit = $params['limit']; $offset = $params['offset']; $issuperuser = has_capability('moodle/site:readallmessages', $context); $renderer = $PAGE->get_renderer('core_message'); if (empty($useridto)) { $useridto = $USER->id; } // Check if the current user is the sender/receiver or just a privileged user. if ($useridto != $USER->id and !$issuperuser) { throw new moodle_exception('accessdenied', 'admin'); } if (!empty($useridto)) { if (!core_user::is_real_user($useridto)) { throw new moodle_exception('invaliduser'); } } $sort = $newestfirst ? 'DESC' : 'ASC'; $notifications = \message_popup\api::get_popup_notifications($useridto, $sort, $limit, $offset); $notificationcontexts = []; if ($notifications) { foreach ($notifications as $notification) { $notificationoutput = new \message_popup\output\popup_notification($notification); $notificationcontext = $notificationoutput->export_for_template($renderer); $notificationcontexts[] = $notificationcontext; } } return array('notifications' => $notificationcontexts, 'unreadcount' => \message_popup\api::count_unread_popup_notifications($useridto)); }
/** * Test that the get_popup_notifications function works correctly with limiting and offsetting * the result set if requested. */ public function test_message_get_popup_notifications_all_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_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_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 0); $this->assertEquals($notifications[0]->fullmessage, 'Message 6'); $this->assertEquals($notifications[1]->fullmessage, 'Message 5'); $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 2, 2); $this->assertEquals($notifications[0]->fullmessage, 'Message 4'); $this->assertEquals($notifications[1]->fullmessage, 'Message 3'); $notifications = \message_popup\api::get_popup_notifications($recipient->id, 'DESC', 0, 3); $this->assertEquals($notifications[0]->fullmessage, 'Message 3'); $this->assertEquals($notifications[1]->fullmessage, 'Message 2'); $this->assertEquals($notifications[2]->fullmessage, 'Message 1'); }