forked from danmarsden/moodle-mod_dialogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.php
385 lines (318 loc) · 13.1 KB
/
message.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
<?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/>.
namespace mod_dialogue;
defined('MOODLE_INTERNAL') || die();
class message implements \renderable {
protected $_dialogue = null;
protected $_conversation = null;
protected $_conversationindex = 0;
protected $_messageid = null;
protected $_authorid = null;
protected $_body = '';
protected $_bodyformat = null;
protected $_bodydraftid = null;
protected $_attachmentsdraftid = null;
protected $_attachments = null;
protected $_state = dialogue::STATE_DRAFT;
protected $_timecreated = null;
protected $_timemodified = null;
protected $_form = null;
protected $_formdatasaved = false;
public function __construct(dialogue $dialogue = null, conversation $conversation = null) {
global $USER;
$this->_dialogue = $dialogue;
$this->_conversation = $conversation;
$this->_authorid = $USER->id;
$this->_bodyformat = editors_get_preferred_format();
$this->_timecreated = time();
$this->_timemodified = time();
}
/**
* PHP overloading magic to make the $dialogue->course syntax work by redirecting
* it to the corresponding $dialogue->magic_get_course() method if there is one, and
* throwing an exception if not. Taken from pagelib.php
*
* @param string $name property name
* @return mixed
*/
public function __get($name) {
$getmethod = 'magic_get_' . $name;
if (method_exists($this, $getmethod)) {
return $this->$getmethod();
} else {
throw new \coding_exception('Unknown property: ' . $name);
}
}
/**
* Returns true/false if current user is the author
* of this message;
*
* @global type $USER
* @return boolean
*/
public function is_author() {
global $USER;
return ($USER->id == $this->_authorid);
}
public function is_participant() {
global $USER;
$participants = $this->conversation->participants;
return in_array($USER->id, array_keys($participants));
}
public function delete() {
global $DB, $USER;
$context = $this->dialogue->context;
$fs = get_file_storage();
// hasn't been saved yet
if (is_null($this->_messageid)) {
return true;
}
// permission to delete conversation
$candelete = ((has_capability('mod/dialogue:delete', $context) and $USER->id == $this->_authorid) or
has_capability('mod/dialogue:deleteany', $context));
if (!$candelete) {
throw new \moodle_exception('nopermissiontodelete', 'dialogue');
}
// delete message and attachment files for message
$fs->delete_area_files($context->id, false, false, $this->_messageid);
// delete message
$DB->delete_records('dialogue_messages', array('id' => $this->_messageid));
return true;
}
protected function magic_get_author() {
$dialogue = $this->dialogue;
//return $dialogue->get_user_brief_details($this->_authorid);
return dialogue_get_user_details($dialogue, $this->_authorid);
}
protected function magic_get_attachments() {
$fs = get_file_storage();
$contextid = $this->dialogue->context->id;
if ($this->_attachments) {
return $fs->get_area_files($contextid, 'mod_dialogue', 'attachment', $this->messageid, "timemodified", false);
}
return array();
}
protected function magic_get_messageid() {
return $this->_messageid;
}
protected function magic_get_conversation() {
if (is_null($this->_conversation)) {
throw new \coding_exception('Parent conversation is not set');
}
return $this->_conversation;
}
protected function magic_get_dialogue() {
if (is_null($this->_dialogue)) {
throw new \coding_exception('Parent dialogue is not set');
}
return $this->_dialogue;
}
protected function magic_get_body() {
return $this->_body;
}
protected function magic_get_bodydraftid() {
return $this->_bodydraftid;
}
protected function magic_get_bodyformat() {
return $this->_bodyformat;
}
protected function magic_get_bodyhtml() {
$contextid = $this->dialogue->context->id;
$ret = file_rewrite_pluginfile_urls($this->_body, 'pluginfile.php', $contextid, 'mod_dialogue', 'message', $this->_messageid);
return format_text($ret, $this->bodyformat);
}
protected function magic_get_state() {
return $this->_state;
}
protected function magic_get_timemodified() {
return $this->_timemodified;
}
public function set_flag($flag, $user = null) {
global $DB, $USER;
if (is_null($this->_messageid)) {
throw new \coding_exception('message must be saved before a user flag can be set');
}
if (is_null($user)) {
$user = $USER;
}
$dialogueid = $this->dialogue->dialogueid;
$conversationid = $this->conversation->conversationid;
$params = array('messageid' => $this->_messageid, 'userid' => $user->id, 'flag' => $flag);
if (!$DB->record_exists('dialogue_flags', $params)) {
$messageflag = new \stdClass();
$messageflag->dialogueid = $dialogueid;
$messageflag->conversationid = $conversationid;
$messageflag->messageid = $this->_messageid;
$messageflag->userid = $user->id;
$messageflag->flag = $flag;
$messageflag->timemodified = time();
$DB->insert_record('dialogue_flags', $messageflag);
}
return true;
}
public function mark_read($user = null) {
// only mark read if in a open or closed state
return $this->set_flag(dialogue::FLAG_READ, $user);
}
public function mark_sent($user = null) {
return $this->set_flag(dialogue::FLAG_SENT, $user);
}
public function set_body($body, $format, $itemid = null) {
$this->_body = $body;
$this->_bodyformat = $format;
if ($format == FORMAT_HTML and isset($itemid)) {
$this->_bodydraftid = $itemid;
$this->_body = file_rewrite_urls_to_pluginfile($this->_body, $this->_bodydraftid);
}
}
public function set_attachmentsdraftid($attachmentsdraftitemid) {
$fileareainfo = file_get_draft_area_info($attachmentsdraftitemid);
if ($fileareainfo['filecount']) {
$this->_attachmentsdraftid = $attachmentsdraftitemid;
}
}
public function set_author($authorid) {
if (is_object($authorid)) {
$authorid = $authorid->id;
}
$this->_authorid = $authorid;
}
public function set_state($state) {
$this->_state = $state; //@todo check actual state
}
public function save() {
global $DB, $USER;
$admin = get_admin(); // possible cronjob
if ($USER->id != $admin->id and $USER->id != $this->_authorid) {
throw new \moodle_exception("This doesn't belong to you!");
}
$context = $this->dialogue->context; // needed for filelib functions
$dialogueid = $this->dialogue->dialogueid;
$conversationid = $this->conversation->conversationid;
$record = new \stdClass();
$record->id = $this->_messageid;
$record->dialogueid = $dialogueid;
$record->conversationid = $conversationid;
$record->conversationindex = $this->_conversationindex;
$record->authorid = $this->_authorid;
// rewrite body now if has embedded files
if (dialogue_contains_draft_files($this->_bodydraftid)) {
$record->body = file_rewrite_urls_to_pluginfile($this->_body, $this->_bodydraftid);
} else {
$record->body = $this->_body;
}
$record->bodyformat = $this->_bodyformat;
// mark atttachments now if has them
if (dialogue_contains_draft_files($this->_attachmentsdraftid)) {
$record->attachments = 1;
} else {
$record->attachments = 0;
}
$record->state = $this->_state;
$record->timecreated = $this->_timecreated;
$record->timemodified = $this->_timemodified;
if (is_null($this->_messageid)) {
// create new record
$this->_messageid = $DB->insert_record('dialogue_messages', $record);
} else {
$record->timemodified = time();
// update existing record
$DB->update_record('dialogue_messages', $record);
}
// deal with embedded files
if ($this->_bodydraftid) {
file_save_draft_area_files($this->_bodydraftid, $context->id, 'mod_dialogue', 'message', $this->_messageid);
}
// deal with attached files
if ($this->_attachmentsdraftid) {
file_save_draft_area_files($this->_attachmentsdraftid, $context->id, 'mod_dialogue', 'attachment', $this->_messageid);
}
return true;
}
public function send() {
global $DB;
// add author to participants and save
$this->conversation->add_participant($this->_authorid);
$this->conversation->save_participants();
// update state to open
$this->_state = dialogue::STATE_OPEN;
$DB->set_field('dialogue_messages', 'state', $this->_state, array('id' => $this->_messageid));
// setup information for messageapi object
$cm = $this->dialogue->cm;
$conversationid = $this->conversation->conversationid;
$course = $this->dialogue->course;
$context = $this->dialogue->context;
$userfrom = $DB->get_record('user', array('id' => $this->_authorid), '*', MUST_EXIST);
$subject = format_string($this->conversation->subject, true, array('context' => $context));
$a = new \stdClass();
$a->userfrom = fullname($userfrom);
$a->subject = $subject;
$url = new \moodle_url('/mod/dialogue/view.php', array('id' => $cm->id));
$a->url = $url->out(false);
$posthtml = get_string('messageapibasicmessage', 'dialogue', $a);
$posttext = html_to_text($posthtml);
$smallmessage = get_string('messageapismallmessage', 'dialogue', fullname($userfrom));
$contexturlparams = array('id' => $cm->id, 'conversationid' => $conversationid);
$contexturl = new \moodle_url('/mod/dialogue/conversation.php', $contexturlparams);
$contexturl->set_anchor('m' . $this->_messageid);
// flags and messaging
$participants = $this->conversation->participants;
foreach ($participants as $participant) {
if ($participant->id == $this->_authorid) {
// so unread flag count displays properly for author, they wrote it, they should of read it.
$this->set_flag(dialogue::FLAG_READ, $this->author);
continue;
}
// give participant a sent flag
$this->set_flag(dialogue::FLAG_SENT, $participant);
$userto = $DB->get_record('user', array('id' => $participant->id), '*', MUST_EXIST);
$eventdata = new \stdClass();
$eventdata->component = 'mod_dialogue';
$eventdata->name = 'post';
$eventdata->userfrom = $userfrom;
$eventdata->userto = $userto;
$eventdata->subject = $subject;
$eventdata->fullmessage = $posttext;
$eventdata->fullmessageformat = FORMAT_HTML;
$eventdata->fullmessagehtml = $posthtml;
$eventdata->smallmessage = $smallmessage;
$eventdata->notification = 1;
$eventdata->contexturl = $contexturl->out(false);
$eventdata->contexturlname = $subject;
$result = message_send($eventdata);
if (!$result) {
//throw new moodle_exception('message not saved');
}
}
return true;
}
/**
* Message is marked as trash so can be deleted at a later time.
*
* @global stdClass $DB
* @throws moodle_exception
*/
public function trash() {
global $DB;
// can only only trash drafts
if ($this->state != dialogue::STATE_DRAFT) {
throw new \moodle_exception('onlydraftscanbetrashed', 'dialogue');
}
// update state to trashed
$this->_state = dialogue::STATE_TRASHED;
$DB->set_field('dialogue_messages', 'state', $this->_state, array('id' => $this->_messageid));
}
}