forked from lsuits/lsu-block_quickmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.php
377 lines (303 loc) · 13 KB
/
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
//
require_once('../../config.php');
require_once('../../enrol/externallib.php');
require_once('lib.php');
require_once('email_form.php');
require_once('../../lib/weblib.php');
require_login();
$courseid = required_param('courseid', PARAM_INT);
$type = optional_param('type', '', PARAM_ALPHA);
$typeid = optional_param('typeid', 0, PARAM_INT);
$sigid = optional_param('sigid', 0, PARAM_INT);
$messageIDresend = optional_param('fmid', 0, PARAM_INT);
if (!$course = $DB->get_record('course', array('id' => $courseid))) {
print_error('no_course', 'block_quickmail', '', $courseid);
}
if (!empty($type) and !in_array($type, array('log', 'drafts'))) {
print_error('no_type', 'block_quickmail', '', $type);
}
if (!empty($type) and empty($typeid)) {
$string = new stdclass;
$string->tpe = $type;
$string->id = $typeid;
print_error('no_typeid', 'block_quickmail', '', $string);
}
$config = quickmail::load_config($courseid);
$context = context_course::instance($courseid);
$has_permission = (
has_capability('block/quickmail:cansend', $context) or
!empty($config['allowstudents'])
);
if (!$has_permission) {
print_error('no_permission', 'block_quickmail');
}
$sigs = $DB->get_records('block_quickmail_signatures', array('userid' => $USER->id), 'default_flag DESC');
$alt_params = array('courseid' => $course->id, 'valid' => 1);
$alternates = $DB->get_records_menu('block_quickmail_alternate', $alt_params, '', 'id, address');
$blockname = quickmail::_s('pluginname');
$header = quickmail::_s('email');
$PAGE->set_context($context);
$PAGE->set_course($course);
$PAGE->navbar->add($blockname);
$PAGE->navbar->add($header);
$PAGE->set_title($blockname . ': ' . $header);
$PAGE->set_heading($blockname . ': ' . $header);
$PAGE->set_url('/blocks/quickmail/email.php', array('courseid' => $courseid));
$PAGE->set_pagetype(quickmail::PAGE_TYPE);
$PAGE->set_pagelayout('standard');
$PAGE->requires->jquery();
$PAGE->requires->js('/blocks/quickmail/js/selection.js');
$course_roles = get_roles_used_in_context($context);
$filter_roles = $DB->get_records_select('role', sprintf('id IN (%s)', $config['roleselection']));
$roles = quickmail::filter_roles($course_roles, $filter_roles);
$allgroups = groups_get_all_groups($courseid);
$mastercap = true;
$groups = $allgroups;
$restricted_view = (
!has_capability('moodle/site:accessallgroups', $context) and
$config['ferpa'] == 'strictferpa'
);
$respected_view = (
!has_capability('moodle/site:accessallgroups', $context) and
$course->groupmode == 1 and
$config['ferpa'] == 'courseferpa'
);
if ($restricted_view || $respected_view) {
$mastercap = false;
$mygroups = groups_get_user_groups($courseid);
$gids = implode(',', array_values($mygroups['0']));
$groups = empty($gids) ?
array() :
$DB->get_records_select('groups', 'id IN (' . $gids . ')');
}
$globalaccess = empty($allgroups);
// Fill the course users by
$users = array();
$users_to_roles = array();
$users_to_groups = array();
$everyone = quickmail::get_non_suspended_users($context, $courseid);
foreach ($everyone as $userid => $user) {
$usergroups = groups_get_user_groups($courseid, $userid);
$gids = ($globalaccess or $mastercap) ?
array_values($usergroups['0']) :
array_intersect(array_values($mygroups['0']), array_values($usergroups['0']));
$userroles = get_user_roles($context, $userid);
$filterd = quickmail::filter_roles($userroles, $roles);
// Available groups
if ((!$globalaccess and !$mastercap) and
empty($gids) or empty($filterd) or $userid == $USER->id)
continue;
$groupmapper = function($id) use ($allgroups) { return $allgroups[$id]; };
$users_to_groups[$userid] = array_map($groupmapper, $gids);
$users_to_roles[$userid] = $filterd;
if (!$user->suspended) {
$users[$userid] = $user;
}
}
if (empty($users)) {
print_error('no_usergroups', 'block_quickmail');
}
// we are presenting the form with values populated from either the log or drafts table in the db
if (!empty($type)) {
$email = $DB->get_record('block_quickmail_' . $type, array('id' => $typeid));
//$emailmailto = array();
if ($messageIDresend == 1) {
list($email->mailto, $email->additional_emails) = quickmail::clean($email->failuserids);
}
} else {
$email = new stdClass;
$email->id = null;
}
$email->messageformat = editors_get_preferred_format();
$default_sigid = $DB->get_field('block_quickmail_signatures', 'id', array(
'userid' => $USER->id, 'default_flag' => 1
));
$email->sigid = $default_sigid ? $default_sigid : -1;
// Some setters for the form
$email->type = $type;
$email->typeid = $typeid;
$editor_options = array(
'trusttext' => false,
'subdirs' => 1,
'maxfiles' => EDITOR_UNLIMITED_FILES,
'accepted_types' => '*',
'context' => $context
);
$email = file_prepare_standard_editor(
$email, 'message', $editor_options, $context, 'block_quickmail', $type, $email->id
);
$selected = array();
if (!empty($email->mailto)) {
foreach (explode(',', $email->mailto) as $id) {
$selected[$id] = (object) array('id'=>$id,'firstname'=>null,'lastname'=>null,'email'=>$id,'mailformat'=>'1','suspended'=>'0','maildisplay'=>'2','status'=>'0');
if(is_numeric($selected[$id]->id)) {
$selected[$id] = $users[$id];
}
unset($users[$id]);
}
}
$form = new email_form(null, array(
'editor_options' => $editor_options,
'selected' => $selected,
'users' => $users,
'roles' => $roles,
'groups' => $groups,
'users_to_roles' => $users_to_roles,
'users_to_groups' => $users_to_groups,
'sigs' => array_map(function($sig) { return $sig->title; }, $sigs),
'alternates' => $alternates
));
$warnings = array();
//
if ($form->is_cancelled()) {
redirect(new moodle_url('/course/view.php?id=' . $courseid));
// DWE we should check if we have selected users or emails around here.
} else if ($data = $form->get_data()) {
if (empty($data->subject)) {
$warnings[] = get_string('no_subject', 'block_quickmail');
}
if (empty($data->mailto) && empty($data->additional_emails)) {
$warnings[] = get_string('no_users', 'block_quickmail');
}
if (empty($warnings)) {
// Submitted data //////////////////////////////////////////////////////
$data->time = time();
$data->format = $data->message_editor['format'];
$data->message = $data->message_editor['text'];
$data->attachment = quickmail::attachment_names($data->attachments);
$data->messageWithSigAndAttach = "";
// Store data; id is needed for file storage ///////////////////////////
if (isset($data->send)) {
$data->id = $DB->insert_record('block_quickmail_log', $data);
$table = 'log';
} else if (isset($data->draft)) {
$table = 'drafts';
if (!empty($typeid) and $type == 'drafts') {
$data->id = $typeid;
$DB->update_record('block_quickmail_drafts', $data);
} else {
$data->id = $DB->insert_record('block_quickmail_drafts', $data);
}
}
$data = file_postupdate_standard_editor(
$data, 'message', $editor_options, $context, 'block_quickmail', $table, $data->id
);
$DB->update_record('block_quickmail_' . $table, $data);
$prepender = $config['prepend_class'];
if (!empty($prepender) and !empty($course->$prepender)) {
$subject = "[{$course->$prepender}] $data->subject";
} else {
$subject = $data->subject;
}
// An instance id is needed before storing the file repository /////////
file_save_draft_area_files(
$data->attachments, $context->id, 'block_quickmail', 'attachment_' . $table, $data->id, $editor_options
);
// Send emails /////////////////////////////////////////////////////////
if (isset($data->send)) {
if ($type == 'drafts') {
quickmail::draft_cleanup($context->id, $typeid);
}
// deal with possible signature, will be appended to message in a little bit.
if (!empty($sigs) and $data->sigid > -1) {
$sig = $sigs[$data->sigid];
$signaturetext = file_rewrite_pluginfile_urls($sig->signature, 'pluginfile.php', $context->id, 'block_quickmail', 'signature', $sig->id, $editor_options);
}
// Prepare html content of message /////////////////////////////////
//$data->message = file_rewrite_pluginfile_urls($data->message, 'pluginfile.php', $context->id, 'block_quickmail', $table, $data->id, $editor_options);
if(empty($signaturetext)){
$data->messageWithSigAndAttach = $data->message;
}
else{
if($data->format == 0 || $data->format == 2 ){
$data->messageWithSigAndAttach = $data->message . "\n\n" .$signaturetext;
}else{
$data->messageWithSigAndAttach = $data->message . "<br /> <br /> <p></p>" .$signaturetext;
}
}
// Append links to attachments, if any /////////////////////////////
$data->messageWithSigAndAttach .= quickmail::process_attachments(
$context, $data, $table, $data->id
);
// Prepare html content of message
$data->message = file_rewrite_pluginfile_urls($data->message, 'pluginfile.php', $context->id, 'block_quickmail', $table, $data->id, $editor_options);
// Same user, alternate email //////////////////////////////////////
if (!empty($data->alternateid)) {
$user = clone($USER);
$user->email = $alternates[$data->alternateid];
} else {
$user = $USER;
}
$data->failuserids = array();
// DWE -> Begin hopefully new way of dealing with messagetext and messagehtml
// TEXT
// This is where we'll need to factor in the preferences of the receiver.
$messagetext = format_text_email($data->messageWithSigAndAttach, $data->format);
// HTML
$messagehtml = format_text($data->messageWithSigAndAttach, $data->format);
if(!empty($data->mailto)) {
foreach (explode(',', $data->mailto) as $userid) {
// Email gets sent here
$success = email_to_user($everyone[$userid], $user, $subject,$messagetext, $messagehtml);
if (!$success) {
$warnings[] = get_string("no_email", 'block_quickmail', $everyone[$userid]);
$data->failuserids[] = $userid;
}
}
}
if(!empty($data->additional_emails)){
$additional_email_array = preg_split('/[,;]/', $data->additional_emails);
$i = 0;
foreach ($additional_email_array as $additional_email) {
$additional_email = trim($additional_email);
$fakeuser = new object();
$fakeuser->id = 99999900 + $i;
$fakeuser->email = $additional_email;
// TODO make this into a menu option
$fakeuser->mailformat = 1;
$additional_email_success = email_to_user($fakeuser, $user, $subject, $messagetext, $messagehtml);
if (!$additional_email_success) {
$data->failuserids[] = $additional_email;
// will need to notify that an email is incorrect
$warnings[] = get_string("no_email_address", 'block_quickmail', $fakeuser->email);
}
$i++;
}
}
$data->failuserids = implode(',', $data->failuserids);
$DB->update_record('block_quickmail_log', $data);
if ($data->receipt) {
email_to_user($USER, $user, $subject, $messagetext, $messagehtml);
}
}
}
$email = $data;
}
if (empty($email->attachments)) {
if (!empty($type)) {
$attachid = file_get_submitted_draft_itemid('attachment');
file_prepare_draft_area(
$attachid, $context->id, 'block_quickmail', 'attachment_' . $type, $typeid
);
$email->attachments = $attachid;
}
}
$form->set_data($email);
if (empty($warnings)) {
if (isset($email->send)) {
redirect(new moodle_url('/blocks/quickmail/emaillog.php', array('courseid' => $course->id)));
} else if (isset($email->draft)) {
$warnings['success'] = get_string("changessaved");
}
}
echo $OUTPUT->header();
echo $OUTPUT->heading($blockname);
foreach ($warnings as $type => $warning) {
$class = ($type === 'success') ? 'notifysuccess' : 'notifyproblem';
echo $OUTPUT->notification($warning, $class);
}
echo html_writer::start_tag('div', array('class' => 'no-overflow'));
$form->display();
echo html_writer::end_tag('div');
echo $OUTPUT->footer();