1 |
efrain |
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 |
* Contains class used to return information to display for the message popup.
|
|
|
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 |
namespace message_popup;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Class used to return information to display for the message popup.
|
|
|
31 |
*
|
|
|
32 |
* @copyright 2016 Ryan Wyllie <ryan@moodle.com>
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
class api {
|
|
|
36 |
/**
|
|
|
37 |
* Get popup notifications for the specified users. Nothing is returned if notifications are disabled.
|
|
|
38 |
*
|
|
|
39 |
* @param int $useridto the user id who received the notification
|
|
|
40 |
* @param string $sort the column name to order by including optionally direction
|
|
|
41 |
* @param int $limit limit the number of result returned
|
|
|
42 |
* @param int $offset offset the result set by this amount
|
|
|
43 |
* @return array notification records
|
|
|
44 |
* @throws \moodle_exception
|
|
|
45 |
* @since 3.2
|
|
|
46 |
*/
|
|
|
47 |
public static function get_popup_notifications($useridto = 0, $sort = 'DESC', $limit = 0, $offset = 0) {
|
|
|
48 |
global $DB, $USER;
|
|
|
49 |
|
|
|
50 |
$sort = strtoupper($sort);
|
|
|
51 |
if ($sort != 'DESC' && $sort != 'ASC') {
|
|
|
52 |
throw new \moodle_exception('invalid parameter: sort: must be "DESC" or "ASC"');
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
if (empty($useridto)) {
|
|
|
56 |
$useridto = $USER->id;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// Is notification enabled ?
|
|
|
60 |
if ($useridto == $USER->id) {
|
|
|
61 |
$disabled = $USER->emailstop;
|
|
|
62 |
} else {
|
|
|
63 |
$user = \core_user::get_user($useridto, "emailstop", MUST_EXIST);
|
|
|
64 |
$disabled = $user->emailstop;
|
|
|
65 |
}
|
|
|
66 |
if ($disabled) {
|
|
|
67 |
// Notifications are disabled.
|
|
|
68 |
return array();
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
$sql = "SELECT n.id, n.useridfrom, n.useridto,
|
|
|
72 |
n.subject, n.fullmessage, n.fullmessageformat,
|
|
|
73 |
n.fullmessagehtml, n.smallmessage, n.contexturl,
|
|
|
74 |
n.contexturlname, n.timecreated, n.component,
|
|
|
75 |
n.eventtype, n.timeread, n.customdata
|
|
|
76 |
FROM {notifications} n
|
|
|
77 |
WHERE n.id IN (SELECT notificationid FROM {message_popup_notifications})
|
|
|
78 |
AND n.useridto = ?
|
|
|
79 |
ORDER BY timecreated $sort, timeread $sort, id $sort";
|
|
|
80 |
|
|
|
81 |
$notifications = [];
|
|
|
82 |
$records = $DB->get_recordset_sql($sql, [$useridto], $offset, $limit);
|
|
|
83 |
foreach ($records as $record) {
|
|
|
84 |
$notifications[] = (object) $record;
|
|
|
85 |
}
|
|
|
86 |
$records->close();
|
|
|
87 |
|
|
|
88 |
return $notifications;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* Count the unread notifications for a user.
|
|
|
93 |
*
|
|
|
94 |
* @param int $useridto the user id who received the notification
|
|
|
95 |
* @return int count of the unread notifications
|
|
|
96 |
* @since 3.2
|
|
|
97 |
*/
|
|
|
98 |
public static function count_unread_popup_notifications($useridto = 0) {
|
|
|
99 |
global $USER, $DB;
|
|
|
100 |
|
|
|
101 |
if (empty($useridto)) {
|
|
|
102 |
$useridto = $USER->id;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return $DB->count_records_sql(
|
|
|
106 |
"SELECT count(id)
|
|
|
107 |
FROM {notifications}
|
|
|
108 |
WHERE id IN (SELECT notificationid FROM {message_popup_notifications})
|
|
|
109 |
AND useridto = ?
|
|
|
110 |
AND timeread is NULL",
|
|
|
111 |
[$useridto]
|
|
|
112 |
);
|
|
|
113 |
}
|
|
|
114 |
}
|