1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
<?php
/**
* messages.php - contains Resources_MessagesController
*
* This program 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 2 of
* the License, or (at your option) any later version.
*
* @author Moritz Strohm <strohm@data-quest.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @copyright 2018-2019
* @category Stud.IP
* @since 4.5
*/
/**
* Resources_MessagesController contains actions
* for the message sending functions related to the resource
* management system.
*/
class Resources_MessagesController extends AuthenticatedController
{
public function index_action()
{
if (Navigation::hasItem('/resources/messages/index')) {
Navigation::activateItem('/resources/messages/index');
}
PageLayout::setTitle(_('Rundmails senden'));
$this->current_user = User::findCurrent();
if (!ResourceManager::userHasGlobalPermission($this->current_user, 'admin')) {
throw new AccessDeniedException();
}
$this->room_selection = 'search';
$this->recipient_selection = 'permission';
$this->clipboard_id = '';
$this->min_permission = '';
$this->selected_rooms = [];
$this->room_search = new QuickSearch(
'room_name',
new RoomSearch()
);
$this->room_search->fireJSFunctionOnSelect(
'STUDIP.Resources.Messages.selectRoom'
);
$this->clipboards = Clipboard::getClipboardsForUser(
$GLOBALS['user']->id,
['Room']
);
//STUB
$this->begin = new DateTime();
$this->end = clone $this->begin;
$this->end->add(new DateInterval('P14D'));
if (Request::submitted('write_mail')) {
CSRFProtection::verifyUnsafeRequest();
$this->recipient_selection = Request::get('recipient_selection');
$this->room_selection = Request::get('room_selection');
$this->room_ids = Request::getArray('room_ids');
$this->selected_rooms = Room::findMany($this->room_ids);
$this->clipboard_id = Request::int('clipboard_id');
//First validation:
if (!in_array($this->room_selection, ['search', 'clipboard'])) {
PageLayout::postError(
_('Die Raumauswahl ist ungültig!')
);
return;
}
if (!in_array($this->recipient_selection, ['permission', 'booking'])) {
PageLayout::postError(
_('Der Empfängerkreis ist ungültig!')
);
return;
}
//Load fields depending on the selection method
//selected for rooms and recipients.
if ($this->recipient_selection == 'permission') {
$this->min_permission = Request::get('min_permission');
//Second validation:
if (!in_array($this->min_permission, ['user', 'autor', 'tutor', 'admin'])) {
PageLayout::postError(
_('Die Rechtestufe ist ungültig!')
);
return;
}
} else {
$this->begin = Request::getDateTime(
'begin_date',
'd.m.Y',
'begin_time',
'H:i'
);
$this->end = Request::getDateTime(
'end_date',
'd.m.Y',
'end_time',
'H:i'
);
//Second validation:
if (!$this->begin) {
PageLayout::postError(
_('Der Startzeitpunkt konnte nicht verarbeitet werden!')
);
$this->relocate('resources/messages/index');
return;
}
if (!$this->end) {
PageLayout::postError(
_('Der Endzeitpunkt konnte nicht verarbeitet werden!')
);
$this->relocate('resources/messages/index');
return;
}
if ($this->begin > $this->end) {
PageLayout::postError(
_('Der Startzeitpunkt darf nicht hinter dem Endzeitpunkt liegen!')
);
$this->relocate('resources/messages/index');
return;
}
}
//Validation complete. We can collect the rooms and then the
//recipients and send the mails.
//First we collect all room-IDs, if they are not already there
//from the search selection method:
if ($this->room_selection == 'clipboard') {
$selected_clipboard = Clipboard::find($this->clipboard_id);
if ($selected_clipboard) {
$this->room_ids = $selected_clipboard->getAllRangeIds('Room');
}
}
//If we haven't found any rooms here we must stop:
if (!$this->room_ids) {
PageLayout::postError(
_('Es konnte keine Raumliste erstellt werden!')
);
return;
}
//Now we get the recipients:
$recipients = [];
if ($this->recipient_selection == 'permission') {
$perm_levels = ResourceManager::getHigherPermissionLevels(
$this->min_permission
);
array_push($perm_levels, $this->min_permission);
$now = time();
$recipients = User::findBySql(
"user_id IN (
SELECT user_id
FROM resource_permissions
WHERE
resource_id IN ( :room_ids )
AND
perms IN ( :perms )
UNION
SELECT user_id
FROM resource_temporary_permissions
WHERE
resource_id IN ( :room_ids )
AND
perms in ( :perms )
AND
begin <= :now
AND
end >= :now
)",
[
'room_ids' => $this->room_ids,
'perms' => $perm_levels,
'now' => $now
]
);
} elseif ($this->recipient_selection == 'booking') {
foreach ($this->room_ids as $room_id) {
$resource = Resource::find($room_id);
if (!$resource) {
continue;
}
$relevant_room_bookings = ResourceBooking::findByResourceAndTimeRanges(
$resource,
[
[
'begin' => $this->begin,
'end' => $this->end
]
]
);
foreach ($relevant_room_bookings as $booking) {
$users = $booking->getAssignedUsers(false);
foreach ($users as $user) {
$recipients[$user->id] = $user;
}
}
}
}
if (!$recipients) {
PageLayout::postInfo(
_('Für die gewählten Räume gibt es keine Empfänger!')
);
$this->relocate('resources/messages/index');
return;
}
//Send the mail to each recipient in the recipient's language:
$recipient_array = [];
foreach ($recipients as $recipient) {
$recipient_array[] = $recipient->username;
}
$_SESSION['sms_data']['p_rec'] = array_unique($recipient_array);
$this->redirect(
'messages/write'
);
}
}
}
|