sendmessage.php 9.84 KB
Newer Older
1
2
3
4
<?php
/**
 *
 * @package    mahara
5
 * @subpackage module-multirecipientnotification
6
7
8
9
10
11
12
13
14
15
16
 * @author     David Ballhausen, Tobias Zeuch
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
 *
 */

define('INTERNAL', 1);
define('MENUITEM', 'inbox');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once(get_config('docroot') . '/lib/searchlib.php');
safe_require('search', 'internal');
17
safe_require('module', 'multirecipientnotification');
18
19

$id = param_integer('id', null);
20
$oldreplytoid = param_integer('oldreplyto', null);
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
$replytoid = param_integer('replyto', null);
$messages = null;
$users = array();
$user = null;

global $USER;
global $THEME;
global $SESSION;

$subject = '';

if (null !== $id) {
    $user = get_record('usr', 'id', $id);

    if (!$user) {
        throw new UserNotFoundException(get_string('cantmessageuser', 'group'));
    }
    else if ($user->deleted != 0) {
        throw new AccessDeniedException(get_string('cantmessageuserdeleted', 'group'));
    }
    else if (!can_send_message($USER->to_stdclass(), $id)) {
        throw new AccessDeniedException(get_string('cantmessageuser', 'group'));
    }

    $users[] = $id;
}
47
48
49
50
51
52
53
54
55
56

if (!is_null($oldreplytoid)) {
    $message = get_message_thread($oldreplytoid);
    if (null === $message) {
        throw new AccessDeniedException(get_string('cantviewmessage', 'group'));
    }
    if ($message[0]->usr != $USER->id) {
        throw new AccessDeniedException(get_string('cantviewmessage', 'group'));
    }
    $subject = $message[0]->subject;
57
    $prefix = trim(get_string('replysubjectprefix', 'module.multirecipientnotification'));
58
59
60
61
62
    if (strpos($subject, $prefix) !== 0) {
        $subject = $prefix . ' ' . $subject;
    }
}

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
if (!is_null($replytoid)) {
    // Let us validate what we are going to reply first. The message should exist,
    // addressed to us and originated from the user we are replying to.
    $message = get_message_mr($USER->id, $replytoid);
    if (null === $message) {
        throw new AccessDeniedException(get_string('cantviewmessage', 'group'));
    }

    if (0 === count($users)) {
        foreach ($message->userids as $userrelid) {
            if ($USER->get('id') === $userrelid) {
                continue;
            }
            $deleted = get_field('usr', 'deleted', 'id', $userrelid);
            if (($deleted === '0') && can_send_message($USER->to_stdclass(), $userrelid) &&
                    $USER->id != $userrelid) {
                $users[] = $userrelid;
            }
            else {
82
                $SESSION->add_info_msg(get_string('removeduserfromlist', 'module.multirecipientnotification'));
83
84
85
86
87
88
89
90
91
92
            }
        }

        if ($USER->get('id') !== $message->fromid) {
            $deleted = get_field('usr', 'deleted', 'id', $message->fromid);
            if (($deleted === '0') && can_send_message($USER->to_stdclass(), $message->fromid) &&
                    $USER->id != $message->fromid) {
                $users[] = $message->fromid;
            }
            else {
93
                $SESSION->add_info_msg(get_string('removeduserfromlist', 'module.multirecipientnotification'));
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
            }
        }
    }

    // OK, now it is safe to fetch the whole thread.
    $messages = get_message_thread_mr($replytoid);

    if (!is_array($messages) || count($messages) <= 0) {
        throw new AccessDeniedException();
    }

    // there may be deleted users as sender or other recipients, so we format
    // all users here to not link to deleted users or the logged in user. Also
    // count deleted users and wrap them up in one span at the end
    foreach ($messages as $oldmessage) {
        $fromusr = get_user($oldmessage->fromid);
        if ($USER->get('id') === $oldmessage->fromid || $fromusr->deleted) {
            $oldmessage->fromusrlink = null;
        }
        else {
            $oldmessage->fromusrlink = profile_url($oldmessage->fromid);
        }
        if ($fromusr->deleted) {
            $oldmessage->fromusrname = get_string('deleteduser');
        }
        else {
            $oldmessage->fromusrname = display_name($oldmessage->fromid);
        }

        $countdeleted = 0;
        foreach ($oldmessage->userids as $tousrid) {
            if (get_user($tousrid)->deleted) {
                $countdeleted++;
            }
            else {
                $tousrarray = array(
                    'display' => display_name($tousrid),
                    'link' => null,
                );
                if ($tousrid !== $USER->get('id')) {
                    $tousrarray['link'] = profile_url($tousrid);
                }
                $oldmessage->tousrs[] = $tousrarray;
            }
        }
        if ($countdeleted > 0) {
            $oldmessage->tousrs[] = array(
141
                'display' => $countdeleted . ' ' . get_string('deleteduser', 'module.multirecipientnotification'),
142
143
144
145
146
147
                'link' => null,
            );
        }
    }

    $subject = $message->subject;
148
    $prefix = trim(get_string('replysubjectprefix', 'module.multirecipientnotification'));
149
150
151
152
153
154
155
156
157
    if (strpos($subject, $prefix) !== 0) {
        $subject = $prefix . ' ' . $subject;
    }
    // just in case, someone calls with replyto and returnto=view, which shouldn't
    // happen anyway. But in that case, proceed to first user in recipient-list
    if (sizeof($users) > 1) {
        $user = $users[0];
    }
}
158
define('TITLE', get_string('sendmessageto', 'module.multirecipientnotification'));
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

$returnto = param_alpha('returnto', 'myfriends');
$offset = param_integer('offset', 0);
switch ($returnto) {
    case 'find':
        $goto = 'user/find.php';
        break;
    case 'view':
        $goto = profile_url($user, false);
        break;
    case 'inbox':
        $goto = 'account/activity';
        break;
    case 'institution':
        $goto = ($inst = param_alpha('inst', null))
            ? 'institution/index.php?institution=' . $inst
            : 'account/activity';
        break;
    default:
178
        $goto = 'module/multirecipientnotification/outbox.php';
179
        break;
180
181
182
183
184
185
186
187
188
189
190
191
}
if ($offset > 0) {
    $goto .= (strpos($goto,'?')) ? '&offset=' . $offset : '?offset=' . $offset;
}

$form = pieform(array(
    'name' => 'sendmessage',
    'autofocus' => false,
    'validatecallback' => 'sendmessage_validate',
    'elements' => array(
        'recipients' => array(
            'type' => 'autocomplete',
192
            'title' => get_string('titlerecipient', 'module.multirecipientnotification'),
193
            'defaultvalue' => $users,
194
            'ajaxurl' => get_config('wwwroot') . 'module/multirecipientnotification/sendmessage.json.php',
195
196
            'initfunction' => 'translate_ids_to_names',
            'multiple' => true,
197
198
199
200
201
202
203
204
205
206
207
            'extraparams' => array(
                    'escapeMarkup' => 'function (markup) { return markup; }',  // let our custom formatter work
                    'templateSelection' =>
'function (data) {
    if (typeof data.name !== "undefined") {
        return data.name;
    }
    else {
        return data.text;
    }
}',
208
                    'renderresult' => true,
209
            ),
210
            'ajaxextraparams' => array(),
211
            'rules' => array('required' => true),
212
213
        ),
        'subject' => array(
214
            'title' => get_string('titlesubject', 'module.multirecipientnotification'),
215
216
            'type' => 'text',
            'name' => 'subject',
217
            'size' => '40',
218
            'defaultvalue' => $subject,
219
            'rules' => array('required' => true),
220
221
222
223
224
225
        ),
        'message' => array(
            'type'  => 'textarea',
            'title' => $messages ? get_string('Reply', 'group') : get_string('message'),
            'cols'  => 80,
            'rows'  => 10,
226
            'rules' => array('maxlength' => 65536, 'required' => true),
227
228
229
230
231
232
233
        ),
        'goto' => array(
            'type' => 'hidden',
            'value' => $goto,
        ),
        'submit' => array(
            'type' => 'submitcancel',
234
            'class' => 'btn-primary',
235
236
237
238
239
240
            'value' => array($messages ? get_string('Reply', 'group') : get_string('sendmessage', 'group'), get_string('cancel')),
            'goto' => get_config('wwwroot') . $goto,
        )
    )
));

241
$smarty = smarty();
242
243
244
$smarty->assign('form', $form);
$smarty->assign('user', $USER);
$smarty->assign('messages', $messages);
245
$smarty->assign('link', get_config('wwwroot') . '/module/multirecipientnotification/sendmessage.php');
246
$smarty->assign('returnto', $returnto);
247
$smarty->display('module:multirecipientnotification:sendmessage.tpl');
248
249
250
251
252
253
254
255
256
257
258

function sendmessage_submit(Pieform $form, $values) {
    global $SESSION;

    send_user_message_mr($values['recipients'], $values['subject'], $values['message'], param_integer('replyto', null));
    $SESSION->add_ok_msg(get_string('messagesent', 'group'));
    redirect(get_config('wwwroot') . $values['goto']);
}

function sendmessage_validate(Pieform $form, $values) {
    if (empty($values['subject'])) {
259
        $form->set_error('subject', get_string('cantsendemptysubject', 'module.multirecipientnotification'));
260
261
    }
    if (empty($values['message'])) {
262
        $form->set_error('message', get_string('cantsendemptytext', 'module.multirecipientnotification'));
263
264
265
    }
    $recipients = array_diff($values['recipients'], array(''));
    if (empty($recipients)) {
266
        $form->set_error('recipients', get_string('cantsendnorecipients', 'module.multirecipientnotification'));
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    }
}

function translate_ids_to_names(array $ids) {
    global $USER;
    // for an empty list, the element '' is transmitted
    $ids = array_diff($ids, array(''));
    $results = array();
    foreach ($ids as $id) {
        $deleted = get_field('usr', 'deleted', 'id', $id);
        if (($deleted === '0') && is_numeric($id) && can_send_message($USER->to_stdclass(), $id)) {
            $results[] = (object) array('id' => $id, 'text' => display_name($id));
        }
    }
    return $results;
282
}