-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathblock_jmail_message.class.php
585 lines (496 loc) · 20.5 KB
/
block_jmail_message.class.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<?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/>.
/**
* Mailbox message class
* Manages a single message
*
* @package blocks
* @subpackage jmail
* @copyright 2011 Juan Leyva <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/blocks/jmail/locallib.php');
class block_jmail_message {
/** @var integer */
public $id = 0;
/** @var integer Receiver*/
public $courseid = 0;
/** @var integer */
public $sentid = 0;
/** @var integer */
public $subject = '';
/** @var integer */
public $body = '';
/** @var integer */
public $timesent = 0;
/** @var integer */
public $sender = 0;
/** @var integer */
public $timecreated = 0;
/** @var integer Receiver*/
public $userid = 0;
/** @var integer */
public $approved = 0;
/** @var integer */
public $deleted = 0;
/** @var integer */
public $read = 1;
/** @var integer */
public $deletedsender = 0;
/**
* Class constructor
* @param object $message A message
*/
function __construct($message) {
if (!empty($message->id)) {
$this->id = $message->id;
}
if (!empty($message->courseid)) {
$this->courseid = $message->courseid;
}
if (!empty($message->subject)) {
$this->subject = $message->subject;
}
if (!empty($message->body)) {
$this->body = $message->body;
}
if (!empty($message->timesent)) {
$this->timesent = $message->timesent;
}
if (!empty($message->timecreated)) {
$this->timecreated = $message->timecreated;
}
if (!empty($message->sender)) {
$this->sender = $message->sender;
}
if (!empty($message->approved)) {
$this->approved = $message->approved;
}
if (isset($message->deletedsender)) {
$this->deletedsender = $message->deletedsender;
}
// Reference to message sent
if (!empty($message->sentid)) {
$this->sentid = $message->sentid;
}
if (!empty($message->userid)) {
$this->userid = $message->userid;
}
if (isset($message->deleted)) {
$this->deleted = $message->deleted;
}
if (isset($message->mread)) {
$this->read = $message->mread;
}
}
/**
* Return message headers
* @return object Message header
*/
public function headers() {
global $DB, $USER;
if (isset($SESSION->jmailcache->courses[$this->courseid])) {
$course = $SESSION->jmailcache->courses[$this->courseid];
} else {
$course = $DB->get_record('course', array('id'=>$this->courseid),'id, fullname, shortname', MUST_EXIST);
$SESSION->jmailcache->courses[$this->courseid] = $course;
}
$header = new stdClass;
$user = $DB->get_record('user', array('id' => $this->sender, 'deleted' => 0));
$header->id = $this->id;
$header->from = fullname($user);
$header->subject = format_string($this->subject);
$time = (!empty($this->timesent)) ? $this->timesent : $this->timecreated;
$header->date = userdate($time, get_string('strftimedatetimeshort', 'langconfig'));
$header->read = ($this->sender == $USER->id)? 1 : $this->read;
$header->approved = $this->approved;
$header->courseid = $course->id;
$header->courseshortname = $course->shortname;
$header->userid = $this->userid;
$header->userto = fullname($DB->get_record('user', array('id' => $this->userid, 'deleted' => 0)));
return $header;
}
/**
* Return full message
* @return object Message
*/
public function full() {
global $DB, $USER, $OUTPUT, $CFG;
require_once($CFG->libdir.'/filelib.php');
$user = $DB->get_record('user', array('id' => $this->sender, 'deleted' => 0));
$message = new stdClass;
$message->id = $this->id;
$message->from = fullname($user);
$message->sender = $this->sender;
$message->subject = format_string($this->subject);
if ($this->timesent) {
$message->date = userdate($this->timesent);
} else {
$message->date = userdate($this->timecreated);
}
$message->body = $this->body;
$message->approved = $this->approved;
$message->deleted = $this->deleted;
$message->destinataries = array();
$message->attachments = array();
$message->labels = array();
if (isset($SESSION->jmailcache->courses[$this->courseid])) {
$course = $SESSION->jmailcache->courses[$this->courseid];
} else {
$course = $DB->get_record('course', array('id'=>$this->courseid),'id, fullname, shortname', MUST_EXIST);
$SESSION->jmailcache->courses[$this->courseid] = $course;
}
$message->courseid = $course->id;
$message->courseshortname = $course->shortname;
// Destinataries
if ($destinataries = $DB->get_records('block_jmail_sent', array('messageid' => $this->id))) {
foreach ($destinataries as $dest) {
if ($dest->type == 'bcc' and $this->sender != $USER->id) {
continue;
}
if ($user = $DB->get_record('user', array('id'=>$dest->userid, 'deleted'=>0))) {
$dest->fullname = fullname($user);
$message->destinataries[$dest->type][] = $dest;
}
}
}
// Attachments
$context = block_jmail_get_context(CONTEXT_COURSE, $this->courseid);
// PATCH - Fix print attachments when block context display throughout the entire site.
// We need the block instance for getting the attachments
if (!$instance = $DB->get_record('block_instances', array('blockname'=>'jmail', 'parentcontextid'=>$context->id))) {
// attachments
$system = block_jmail_get_context(CONTEXT_SYSTEM);
$instances = $DB->get_records('block_instances', array('blockname'=>'jmail', 'parentcontextid' => $system->id));
// A block can be present at multiple sites for a course.
$instance = array_shift($instances);
}
if ($instance) {
$context = block_jmail_get_context(CONTEXT_BLOCK, $instance->id, MUST_EXIST);
$fs = get_file_storage();
if ($files = $fs->get_area_files($context->id, 'block_jmail', 'attachment', $this->id, "timemodified", false)) {
foreach ($files as $file) {
$mimetype = $file->get_mimetype();
$attachment = new stdClass;
$attachment->filename = $file->get_filename();
$attachment->iconimage = '<img src="'.$OUTPUT->pix_url(file_mimetype_icon($mimetype)).'" class="icon" alt="'.$mimetype.'" />';
$attachment->path = file_encode_url($CFG->wwwroot.'/pluginfile.php', '/'.$context->id.'/block_jmail/attachment/'.$this->id.'/'.$attachment->filename);
$message->attachments[] = $attachment;
}
$message->body = file_rewrite_pluginfile_urls($message->body, 'pluginfile.php', $context->id, 'block_jmail', 'body', $this->id);
$message->body = format_text($message->body);
}
}
// END PATCH.
// Labels
$sql = "SELECT l.id, l.name
FROM
{block_jmail_label} l
JOIN {block_jmail_m_label} m
ON l.id = m.labelid
WHERE
m.messagesentid = ?
";
if ($labels = $DB->get_records_sql($sql, array($this->sentid))) {
foreach ($labels as $label) {
$message->labels[] = $label;
}
}
if ($this->timesent and $this->sender == $USER->id) {
$message->labels[] = 'sent';
}
if (!$this->timesent and $this->sender == $USER->id) {
$message->labels[] = 'draft';
}
if ($message->deleted) {
$message->labels[] = 'trash';
}
if (!$message->approved) {
$message->labels[] = 'toapprove';
}
if (empty($message->labels)) {
$message->labels[] = 'inbox';
}
return $message;
}
/**
* Update a message and destinataries
* @param array $destinataries Destinataries for the message
* @param array $attachments File attachments
* @return mixed Message id or false if something fails
*/
public function update($destinataries, $attachments, $editoritemid) {
global $USER, $DB, $CFG;
if (!$message = $DB->get_record('block_jmail', array('id'=>$this->id))) {
return false;
}
$message->sender = $USER->id;
$message->courseid = $this->courseid;
$message->subject = $this->subject;
$message->body = $this->body;
$message->timesent = $this->timesent;
$message->timecreated = $this->timecreated;
if ($DB->update_record('block_jmail', $message)) {
$DB->delete_records('block_jmail_sent', array('messageid'=>$this->id));
foreach ($destinataries as $d) {
if (!$d->userid) {
continue;
}
$to = new stdClass;
$to->userid = $d->userid;
$to->messageid = $this->id;
$to->type = $d->type;
$to->mread = 0;
$to->answered = 0;
$to->deleted = 0;
$to->labeled = 0;
$DB->insert_record('block_jmail_sent', $to);
}
$context = block_jmail_get_context(CONTEXT_COURSE, $this->courseid);
// PATCH - Fix print attachments when block context display throughout the entire site.
// We need the block instance for saving the attachments
if (!$instance = $DB->get_record('block_instances', array('blockname'=>'jmail', 'parentcontextid'=>$context->id))) {
// attachments
$system = block_jmail_get_context(CONTEXT_SYSTEM);
$instances = $DB->get_records('block_instances', array('blockname'=>'jmail', 'parentcontextid' => $system->id));
// A block can be present at multiple sites for a course.
$instance = array_shift($instances);
}
if ($instance) {
$context = block_jmail_get_context(CONTEXT_BLOCK, $instance->id, MUST_EXIST);
require_once($CFG->libdir.'/filelib.php');
$message->body = file_save_draft_area_files($editoritemid, $context->id, 'block_jmail', 'body', $this->id, array('subdirs'=>true), $message->body);
$DB->set_field('block_jmail', 'body', $message->body, array('id'=>$this->id));
$info = file_get_draft_area_info($attachments);
$present = ($info['filecount']>0) ? '1' : '';
file_save_draft_area_files($attachments, $context->id, 'block_jmail', 'attachment', $this->id);
$DB->set_field('block_jmail', 'attachment', $present, array('id'=>$this->id));
}
// END PATCH.
return $this->id;
}
return false;
}
/**
* Save a message and destinataries
* @param array $destinataries Destinataries for the message
* @param array $attachments File attachments
* @return mixed Message id or false if something fails
*/
public function save($destinataries, $attachments, $editoritemid) {
global $USER, $DB, $CFG;
$message = new stdClass;
$message->sender = $USER->id;
$message->courseid = $this->courseid;
$message->subject = $this->subject;
$message->body = $this->body;
//$post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message);
$message->attachment = 0;
$message->approved = $this->approved;
$message->timesent = $this->timesent;
$message->timecreated = $this->timecreated;
if ($messageid = $DB->insert_record('block_jmail', $message)) {
foreach ($destinataries as $d) {
if (!$d->userid) {
continue;
}
$to = new stdClass;
$to->userid = $d->userid;
$to->messageid = $messageid;
$to->type = $d->type;
$to->mread = 0;
$to->answered = 0;
$to->deleted = 0;
$to->labeled = 0;
$DB->insert_record('block_jmail_sent', $to);
}
$this->id = $messageid;
$context = block_jmail_get_context(CONTEXT_COURSE, $this->courseid);
// PATCH - Fix print attachments when block context display throughout the entire site.
// We need the block instance for saving the attachments
if (!$instance = $DB->get_record('block_instances', array('blockname'=>'jmail', 'parentcontextid'=>$context->id))) {
// attachments
$system = block_jmail_get_context(CONTEXT_SYSTEM);
$instances = $DB->get_records('block_instances', array('blockname'=>'jmail', 'parentcontextid' => $system->id));
// A block can be present at multiple sites for a course.
$instance = array_shift($instances);
}
if ($instance) {
$context = block_jmail_get_context(CONTEXT_BLOCK, $instance->id, MUST_EXIST);
require_once($CFG->libdir.'/filelib.php');
$message->body = file_save_draft_area_files($editoritemid, $context->id, 'block_jmail', 'body', $messageid, array('subdirs'=>true), $message->body);
$DB->set_field('block_jmail', 'body', $message->body, array('id'=>$messageid));
$info = file_get_draft_area_info($attachments);
$present = ($info['filecount']>0) ? '1' : '';
file_save_draft_area_files($attachments, $context->id, 'block_jmail', 'attachment', $messageid);
$DB->set_field('block_jmail', 'attachment', $present, array('id'=>$messageid));
}
// END PATCH.
return $messageid;
}
return false;
}
/**
* Mark as deleted the current message in database
* @return boolean True if the message have been deleted succesfully
*/
public function delete() {
global $DB, $USER;
// Maybe the sender was a destinatary to, this cover this case.
if ($this->sender == $USER->id) {
$DB->set_field('block_jmail', 'deleted', 1, array('id' => $this->id));
}
// TODO, there is no way for deleting drafts right now
if (!$this->sentid) {
return false;
}
return $DB->set_field('block_jmail_sent', 'deleted', 1, array('id' => $this->sentid));
}
/**
* Mark as deleted the current message sent by the current user in database
* @return boolean True if the message have been deleted succesfully
*/
public function delete_sent() {
global $DB;
return $DB->set_field('block_jmail', 'deleted', 1, array('id' => $this->id));
}
/**
* Mark as undeleted the current message in database
* @return boolean True if the message have been undeleted succesfully
*/
public function undelete() {
global $DB, $USER;
if ($this->sender == $USER->id and $this->deletedsender) {
return $DB->set_field('block_jmail', 'deleted', 0, array('id' => $this->id));
}
// TODO, there is no way for deleting drafts right now
if (!$this->sentid) {
return false;
}
return $DB->set_field('block_jmail_sent', 'deleted', 0, array('id' => $this->sentid));
}
/**
* Mark as sent the current message in database
* @return boolean True if the message have been sent succesfully
*/
public function mark_sent() {
global $DB;
if (!$this->sentid) {
return false;
}
return $DB->set_field('block_jmail_sent', 'timesent', time(), array('id' => $this->sentid));
}
/**
* Mark as read the current message in database
* @return boolean True if the message have been sent succesfully
*/
public function mark_read() {
global $DB;
if (!$this->sentid) {
return false;
}
return $DB->set_field('block_jmail_sent', 'mread', 1, array('id' => $this->sentid));
}
/**
* Mark as unread the current message in database
* @return boolean True if the message have been sent succesfully
*/
public function mark_unread() {
global $DB;
if (!$this->sentid) {
return false;
}
return $DB->set_field('block_jmail_sent', 'mread', 0, array('id' => $this->sentid));
}
/**
* Mark as labeled the current message in database
* @return boolean True if the message have been sent succesfully
*/
public function mark_labeled() {
global $DB;
return $DB->set_field('block_jmail_sent', 'labeled', 1, array('id' => $this->sentid));
}
/**
* Mark as unlabeled the current message in database
* @return boolean True if the message have been sent succesfully
*/
public function mark_unlabeled() {
global $DB;
if ($DB->count_records('block_jmail_m_label', array('messagesentid' => $this->sentid)) == 0) {
return $DB->set_field('block_jmail_sent', 'labeled', 0, array('id' => $this->sentid));
}
return true;
}
/**
* Approve a message
* @return boolean True if the message have been approved succesfully
*/
public function approve() {
global $DB;
return $DB->set_field('block_jmail', 'approved', 1, array('id' => $this->id));
}
/**
* Checks if the message has been created for the current user or has been sent to the current user
* @return boolean True if the message is from the current user
*/
public function is_mine() {
global $USER;
if ($this->sender == $USER->id) {
return true;
}
if ($this->userid == $USER->id and $this->timesent > 0 and $this->approved) {
return true;
}
}
/**
* Returns an object from id
* @param integer $id Message id
* @return block_jmail_message Message object
*/
public static function get_from_id($id) {
global $DB, $USER;
if ($message = $DB->get_record('block_jmail', array('id' => $id))) {
$message->deletedsender = $message->deleted;
if ($messagesent = $DB->get_record('block_jmail_sent', array('messageid' => $id, 'userid' => $USER->id))) {
$message->userid = $messagesent->userid;
$message->sentid = $messagesent->id;
$message->deleted = $messagesent->deleted;
}
return new block_jmail_message($message);
} else {
return false;
}
}
/**
* Returns an object from the id in sent messages
* @param integer $id Message id
* @return block_jmail_message Message object
*/
public static function get_from_sentid($id) {
global $DB, $USER;
if (! $messagesent = $DB->get_record('block_jmail_sent', array('id'=>$id))) {
return false;
}
if ($message = $DB->get_record('block_jmail', array('id'=>$messagesent->messageid))) {
$message->sentid = $messagesent->id;
$message->userid = $messagesent->userid;
$message->deleted = $messagesent->deleted;
return new block_jmail_message($message);
} else {
return false;
}
}
}