forked from lsuits/lsu-block_quickmail
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadmin_email.php
282 lines (252 loc) · 9.07 KB
/
admin_email.php
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?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/>.
/**
* Page for the admin email form.
*
* @package block_quickmail
* @copyright 2008-2017 Louisiana State University
* @copyright 2008-2017 Adam Zapletal, Chad Mazilly, Philip Cali, Robert Russo
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
global $CFG, $USER, $SESSION, $PAGE, $SITE, $OUTPUT, $DB;
require_once("$CFG->dirroot/course/lib.php");
require_once("$CFG->libdir/adminlib.php");
require_once("$CFG->dirroot/user/filters/lib.php");
require_once('lib.php');
require_once('classes/message.php');
require_once('admin_email_form.php');
require_login();
// Get page params.
$page = optional_param('page', 0, PARAM_INT);
$perpage = optional_param('perpage', 20, PARAM_INT);
$sort = optional_param('sort', 'lastname', PARAM_ACTION);
$direction = optional_param('dir', 'ASC', PARAM_ACTION);
$courseid = optional_param('courseid', '', PARAM_INT);
$type = optional_param('type', '', PARAM_ALPHA);
$typeid = optional_param('typeid', 0, PARAM_INT);
$fmid = optional_param('fmid', 0, PARAM_INT);
$blockname = get_string('pluginname', 'block_quickmail');
$header = get_string('sendadmin', 'block_quickmail');
$context = context_system::instance();
if (has_capability('block/quickmail:myaddinstance', $context) || is_siteadmin($USER)) {
// Page params for ui filter.
$filterparams = $typeid > 0 ? ['courseid' => $courseid, 'type' => $type, 'typeid' => $typeid] : null;
$PAGE->set_context($context);
$PAGE->set_url($CFG->wwwroot . '/blocks/quickmail/admin_email.php');
$PAGE->navbar->add($blockname);
$PAGE->navbar->add($header);
$PAGE->set_heading($SITE->shortname . ': ' . $blockname);
$PAGE->set_pagetype(quickmail::PAGE_TYPE);
$PAGE->set_pagelayout('standard');
if ($type == 'log') {
$logmessage = $DB->get_record('block_quickmail_' . $type, ['id' => $typeid]);
// Try to get the saved, serialized filters from mailto.
if (isset($logmessage->mailto)) {
// Will give a Notice if content of mailto in not unserializable.
$filters = @unserialize($logmessage->mailto);
if ($filters !== false && is_array($filters) && ( empty($_POST['addfilter']) && empty($_POST['removeselected']) )) {
$SESSION->user_filtering = $filters;
}
}
}
// Get Our users.
$fields = [
'realname' => 1,
'lastname' => 1,
'firstname' => 1,
'email' => 1,
'city' => 1,
'country' => 1,
'confirmed' => 1,
'suspended' => 1,
'profile' => 1,
'courserole' => 0,
'systemrole' => 0,
'username' => 0,
'cohort' => 1,
'firstaccess' => 1,
'lastaccess' => 0,
'neveraccessed' => 1,
'timemodified' => 1,
'nevermodified' => 1,
'auth' => 1,
'mnethostid' => 1,
'language' => 1,
'firstnamephonetic' => 1,
'lastnamephonetic' => 1,
'middlename' => 1,
'alternatename' => 1,
];
$ufiltering = new user_filtering($fields, null, $filterparams);
[$sql, $params] = $ufiltering->get_sql_filter();
$usersearchcount = get_users(
false,
'',
true,
null,
'',
'',
'',
'',
'',
'*',
$sql,
$params
);
if ($fmid == 1) {
$sql = 'id IN (' . $logmessage->failuserids . ')';
}
$displayusers = empty($sql) ? [] :
get_users_listing(
$sort,
$direction,
$page * $perpage,
$perpage,
'',
'',
'',
$sql,
$params
);
$users = empty($sql) ? [] :
get_users_listing(
$sort,
$direction,
0,
0,
'',
'',
'',
$sql,
$params
);
$editoroptions = [
'trusttext' => true,
'subdirs' => 1,
'maxfiles' => EDITOR_UNLIMITED_FILES,
'accepted_types' => '*',
'context' => $context,
];
$form = new admin_email_form(null, [
'editor_options' => $editoroptions,
]);
// Process data submission.
if ($form->is_cancelled()) {
unset($SESSION->user_filtering);
redirect(new moodle_url('/blocks/quickmail/admin_email.php'));
} else if ($data = $form->get_data()) {
$message = new Message($data, array_keys($users));
// Todo: Refactor so that we're not building two similar structures, namely: $data and $message.
$data->courseid = SITEID;
$data->userid = $USER->id;
$data->mailto = isset($SESSION->user_filtering) ? serialize($SESSION->user_filtering) : "unknown filter";
$data->format = $data->message_editor['format'];
$data->message = $data->message_editor['text'];
$data->attachment = '';
$data->time = time();
// Save record of the message, regardless of errors.
$data->id = $DB->insert_record('block_quickmail_log', $data);
// Send the messages and save the failed users if there are any.
$data->failuserids = implode(',', $message->send());
$message->sendAdminReceipt();
// Finished processing.
// Empty errors mean that you can go back home.
if (empty($message->warnings)) {
unset($SESSION->user_filtering);
if (is_siteadmin($USER->id)) {
redirect(new moodle_url('/blocks/quickmail/emaillog.php', ['courseid' => $COURSE->id]));
} else {
redirect(new moodle_url('/my', null));
}
} else {
// Update DB to reflect fail status.
$data->status = quickmail::_s('failed_to_send_to') + count($message->warnings) + quickmail::_s('users');
$DB->update_record('block_quickmail_log', $data);
}
}
// Get data for form.
if (!empty($type)) {
$data = $logmessage;
$logmessage->messageformat = $USER->mailformat;
$logmessage = file_prepare_standard_editor(
$logmessage,
'message',
$editoroptions,
$context,
'block_quickmail',
$type,
$logmessage->id
);
} else {
$logmessage = new stdClass();
}
// Begin output.
echo $OUTPUT->header();
echo $OUTPUT->heading($header);
// Notify the admin.
if (!empty($message->warnings)) {
foreach ($message->warnings as $warning) {
echo $OUTPUT->notification($warning);
}
}
// Start work.
if ($fmid != 1) {
$ufiltering->display_add();
$ufiltering->display_active();
}
$pagingbar = !$sql ? '' :
$OUTPUT->paging_bar(
$usersearchcount,
$page,
$perpage,
new moodle_url('/blocks/quickmail/admin_email.php', [
'sort' => $sort,
'dir' => $direction,
'perpage' => $perpage,
])
);
if (!empty($sql)) {
echo $OUTPUT->heading("Found $usersearchcount User(s)");
}
echo $pagingbar;
if (!empty($displayusers)) {
$columns = ['firstname', 'lastname', 'email', 'city', 'lastaccess'];
foreach ($columns as $column) {
$direction = ($sort == $column && $direction == "ASC") ? "DESC" : "ASC";
$$column = html_writer::link('admin_email.php?sort=' . $column . '&dir=' .
$direction, get_string($column));
}
$table = new html_table();
$table->head = ["$firstname / $lastname", $email, $city, $lastaccess];
$table->data = array_map(function ($user) {
$fullname = fullname($user);
$email = $user->email;
$city = $user->city;
$lastaccesstime = isset($user->lastaccess) ?
format_time(time() - $user->lastaccess) : get_string('never');
return [$fullname, $email, $city, $lastaccesstime];
}, $displayusers);
echo html_writer::table($table);
}
// Need no-reply in both cases.
$logmessage->noreply = $CFG->noreplyaddress;
// Display form and done.
$form->set_data($logmessage);
echo $form->display();
echo $pagingbar;
echo $OUTPUT->footer();
}