sendmessage.php 9.66 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
17
 * @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/pieforms/pieform.php');
require_once(get_config('docroot') . '/lib/searchlib.php');
safe_require('search', 'internal');
18
safe_require('module', 'multirecipientnotification');
19
20

$id = param_integer('id', null);
21
$oldreplytoid = param_integer('oldreplyto', null);
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
$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;
}
48
49
50
51
52
53
54
55
56
57

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;
58
    $prefix = trim(get_string('replysubjectprefix', 'module.multirecipientnotification'));
59
60
61
62
63
    if (strpos($subject, $prefix) !== 0) {
        $subject = $prefix . ' ' . $subject;
    }
}

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 {
83
                $SESSION->add_info_msg(get_string('removeduserfromlist', 'module.multirecipientnotification'));
84
85
86
87
88
89
90
91
92
93
            }
        }

        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 {
94
                $SESSION->add_info_msg(get_string('removeduserfromlist', 'module.multirecipientnotification'));
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
            }
        }
    }

    // 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(
142
                'display' => $countdeleted . ' ' . get_string('deleteduser', 'module.multirecipientnotification'),
143
144
145
146
147
148
                'link' => null,
            );
        }
    }

    $subject = $message->subject;
149
    $prefix = trim(get_string('replysubjectprefix', 'module.multirecipientnotification'));
150
151
152
153
154
155
156
157
158
    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];
    }
}
159
define('TITLE', get_string('sendmessageto', 'module.multirecipientnotification'));
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

$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:
179
        $goto = 'module/multirecipientnotification/outbox.php';
180
        break;
181
182
183
184
185
186
187
188
189
190
191
192
}
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',
193
            'title' => get_string('titlerecipient', 'module.multirecipientnotification'),
194
            'defaultvalue' => $users,
195
            'ajaxurl' => get_config('wwwroot') . 'module/multirecipientnotification/sendmessage.json.php',
196
197
198
            'initfunction' => 'translate_ids_to_names',
            'multiple' => true,
            'ajaxextraparams' => array(),
199
            'rules' => array('required' => true),
200
201
        ),
        'subject' => array(
202
            'title' => get_string('titlesubject', 'module.multirecipientnotification'),
203
204
            'type' => 'text',
            'name' => 'subject',
205
            'size' => '40',
206
            'defaultvalue' => $subject,
207
            'rules' => array('required' => true),
208
209
210
211
212
213
        ),
        'message' => array(
            'type'  => 'textarea',
            'title' => $messages ? get_string('Reply', 'group') : get_string('message'),
            'cols'  => 80,
            'rows'  => 10,
214
            'rules' => array('maxlength' => 65536, 'required' => true),
215
216
217
218
219
220
221
        ),
        'goto' => array(
            'type' => 'hidden',
            'value' => $goto,
        ),
        'submit' => array(
            'type' => 'submitcancel',
222
            'class' => 'btn btn-success',
223
224
225
226
227
228
229
            'value' => array($messages ? get_string('Reply', 'group') : get_string('sendmessage', 'group'), get_string('cancel')),
            'goto' => get_config('wwwroot') . $goto,
        )
    )
));

$javascripts = array(
230
    'module/multirecipientnotification/js/sendmessage.js',
231
);
232
233

$smarty = smarty($javascripts);
234
235
236
237
$smarty->assign('PAGEHEADING', TITLE);
$smarty->assign('form', $form);
$smarty->assign('user', $USER);
$smarty->assign('messages', $messages);
238
$smarty->assign('link', get_config('wwwroot') . '/module/multirecipientnotification/sendmessage.php');
239
$smarty->assign('returnto', $returnto);
240
$smarty->display('module:multirecipientnotification:sendmessage.tpl');
241
242
243
244
245
246
247
248
249
250
251

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'])) {
252
        $form->set_error('subject', get_string('cantsendemptysubject', 'module.multirecipientnotification'));
253
254
    }
    if (empty($values['message'])) {
255
        $form->set_error('message', get_string('cantsendemptytext', 'module.multirecipientnotification'));
256
257
258
    }
    $recipients = array_diff($values['recipients'], array(''));
    if (empty($recipients)) {
259
        $form->set_error('recipients', get_string('cantsendnorecipients', 'module.multirecipientnotification'));
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    }
}

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;
275
}