Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Contains helper class for the message area.
19
 *
20
 * @package    core_message
21
 * @copyright  2016 Mark Nelson <markn@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_message;
26
use DOMDocument;
1441 ariadna 27
use stdclass;
1 efrain 28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
require_once($CFG->dirroot . '/message/lib.php');
32
 
33
/**
34
 * Helper class for the message area.
35
 *
36
 * @copyright  2016 Mark Nelson <markn@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class helper {
40
 
41
    /**
42
     * @deprecated since 3.6
43
     */
44
    public static function get_messages() {
45
        throw new \coding_exception('\core_message\helper::get_messages has been removed.');
46
    }
47
 
48
    /**
49
     * Helper function to retrieve conversation messages.
50
     *
51
     * @param  int $userid The current user.
52
     * @param  int $convid The conversation identifier.
53
     * @param  int $timedeleted The time the message was deleted
54
     * @param  int $limitfrom Return a subset of records, starting at this point (optional).
55
     * @param  int $limitnum Return a subset comprising this many records in total (optional, required if $limitfrom is set).
56
     * @param  string $sort The column name to order by including optionally direction.
57
     * @param  int $timefrom The time from the message being sent.
58
     * @param  int $timeto The time up until the message being sent.
59
     * @return array of messages
60
     */
61
    public static function get_conversation_messages(int $userid, int $convid, int $timedeleted = 0, int $limitfrom = 0,
62
                                                     int $limitnum = 0, string $sort = 'timecreated ASC', int $timefrom = 0,
63
                                                     int $timeto = 0): array {
64
        global $DB;
65
 
66
        $sql = "SELECT m.id, m.useridfrom, m.subject, m.fullmessage, m.fullmessagehtml,
67
                       m.fullmessageformat, m.fullmessagetrust, m.smallmessage, m.timecreated,
68
                       mc.contextid, muaread.timecreated AS timeread
69
                  FROM {message_conversations} mc
70
            INNER JOIN {messages} m
71
                    ON m.conversationid = mc.id
72
             LEFT JOIN {message_user_actions} muaread
73
                    ON (muaread.messageid = m.id
74
                   AND muaread.userid = :userid1
75
                   AND muaread.action = :readaction)";
76
        $params = ['userid1' => $userid, 'readaction' => api::MESSAGE_ACTION_READ, 'convid' => $convid];
77
 
78
        if (empty($timedeleted)) {
79
            $sql .= " LEFT JOIN {message_user_actions} mua
80
                             ON (mua.messageid = m.id
81
                            AND mua.userid = :userid2
82
                            AND mua.action = :deleteaction
83
                            AND mua.timecreated is NOT NULL)";
84
        } else {
85
            $sql .= " INNER JOIN {message_user_actions} mua
86
                              ON (mua.messageid = m.id
87
                             AND mua.userid = :userid2
88
                             AND mua.action = :deleteaction
89
                             AND mua.timecreated = :timedeleted)";
90
            $params['timedeleted'] = $timedeleted;
91
        }
92
 
93
        $params['userid2'] = $userid;
94
        $params['deleteaction'] = api::MESSAGE_ACTION_DELETED;
95
 
96
        $sql .= " WHERE mc.id = :convid";
97
 
98
        if (!empty($timefrom)) {
99
            $sql .= " AND m.timecreated >= :timefrom";
100
            $params['timefrom'] = $timefrom;
101
        }
102
 
103
        if (!empty($timeto)) {
104
            $sql .= " AND m.timecreated <= :timeto";
105
            $params['timeto'] = $timeto;
106
        }
107
 
108
        if (empty($timedeleted)) {
109
            $sql .= " AND mua.id is NULL";
110
        }
111
 
112
        $sql .= " ORDER BY m.$sort";
113
 
114
        $messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
115
 
116
        return $messages;
117
    }
118
 
119
    /**
120
     * Helper function to return a conversation messages with the involved members (only the ones
121
     * who have sent any of these messages).
122
     *
123
     * @param int $userid The current userid.
124
     * @param int $convid The conversation id.
125
     * @param array $messages The formated array messages.
126
     * @return array A conversation array with the messages and the involved members.
127
     */
128
    public static function format_conversation_messages(int $userid, int $convid, array $messages): array {
129
        global $USER;
130
 
131
        // Create the conversation array.
132
        $conversation = array(
133
            'id' => $convid,
134
        );
135
 
136
        // Store the messages.
137
        $arrmessages = array();
138
 
139
        foreach ($messages as $message) {
140
            // Store the message information.
1441 ariadna 141
            $msg = new stdClass();
1 efrain 142
            $msg->id = $message->id;
143
            $msg->useridfrom = $message->useridfrom;
144
            $msg->text = message_format_message_text($message);
145
            $msg->timecreated = $message->timecreated;
146
            $arrmessages[] = $msg;
147
        }
148
        // Add the messages to the conversation.
149
        $conversation['messages'] = $arrmessages;
150
 
151
        // Get the users who have sent any of the $messages.
152
        $memberids = array_unique(array_map(function($message) {
153
            return $message->useridfrom;
154
        }, $messages));
155
 
156
        if (!empty($memberids)) {
157
            // Get members information.
158
            $conversation['members'] = self::get_member_info($userid, $memberids);
159
        } else {
160
            $conversation['members'] = array();
161
        }
162
 
163
        return $conversation;
164
    }
165
 
166
    /**
167
     * @deprecated since 3.6
168
     */
169
    public static function create_messages() {
170
        throw new \coding_exception('\core_message\helper::create_messages has been removed.');
171
    }
172
 
173
    /**
174
     * Helper function for creating a contact object.
175
     *
1441 ariadna 176
     * @param stdClass $contact
1 efrain 177
     * @param string $prefix
1441 ariadna 178
     * @return stdClass
1 efrain 179
     */
180
    public static function create_contact($contact, $prefix = '') {
181
        global $PAGE;
182
 
183
        // Create the data we are going to pass to the renderable.
184
        $userfields = \user_picture::unalias($contact, array('lastaccess'), $prefix . 'id', $prefix);
1441 ariadna 185
        $data = new stdClass();
1 efrain 186
        $data->userid = $userfields->id;
187
        $data->useridfrom = null;
188
        $data->fullname = fullname($userfields);
189
        // Get the user picture data.
190
        $userpicture = new \user_picture($userfields);
191
        $userpicture->size = 1; // Size f1.
192
        $data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
193
        $userpicture->size = 0; // Size f2.
194
        $data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
195
        // Store the message if we have it.
196
        $data->ismessaging = false;
197
        $data->lastmessage = null;
198
        $data->lastmessagedate = null;
199
        $data->messageid = null;
200
        if (isset($contact->smallmessage)) {
201
            $data->ismessaging = true;
202
            // Strip the HTML tags from the message for displaying in the contact area.
203
            $data->lastmessage = clean_param($contact->smallmessage, PARAM_NOTAGS);
204
            $data->lastmessagedate = $contact->timecreated;
205
            $data->useridfrom = $contact->useridfrom;
206
            if (isset($contact->messageid)) {
207
                $data->messageid = $contact->messageid;
208
            }
209
        }
210
        $data->isonline = null;
211
        $user = \core_user::get_user($data->userid);
212
        if (self::show_online_status($user)) {
213
            $data->isonline = self::is_online($userfields->lastaccess);
214
        }
215
        $data->isblocked = isset($contact->blocked) ? (bool) $contact->blocked : false;
216
        $data->isread = isset($contact->isread) ? (bool) $contact->isread : false;
217
        $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;
218
        $data->conversationid = $contact->conversationid ?? null;
219
 
220
        return $data;
221
    }
222
 
223
    /**
224
     * Helper function for checking if we should show the user's online status.
225
     *
1441 ariadna 226
     * @param stdClass $user
1 efrain 227
     * @return boolean
228
     */
229
    public static function show_online_status($user) {
230
        global $CFG;
231
 
232
        require_once($CFG->dirroot . '/user/lib.php');
233
 
234
        if ($lastaccess = user_get_user_details($user, null, array('lastaccess'))) {
235
            if (isset($lastaccess['lastaccess'])) {
236
                return true;
237
            }
238
        }
239
 
240
        return false;
241
    }
242
 
243
    /**
244
     * Helper function for checking the time meets the 'online' condition.
245
     *
246
     * @param int $lastaccess
247
     * @return boolean
248
     */
249
    public static function is_online($lastaccess) {
250
        global $CFG;
251
 
252
        // Variable to check if we consider this user online or not.
253
        $timetoshowusers = 300; // Seconds default.
254
        if (isset($CFG->block_online_users_timetosee)) {
255
            $timetoshowusers = $CFG->block_online_users_timetosee * 60;
256
        }
257
        $time = time() - $timetoshowusers;
258
 
259
        return $lastaccess >= $time;
260
    }
261
 
262
    /**
263
     * Get providers preferences.
264
     *
265
     * @param array $providers
266
     * @param int $userid
1441 ariadna 267
     * @return stdClass
1 efrain 268
     */
269
    public static function get_providers_preferences($providers, $userid) {
1441 ariadna 270
        $preferences = new stdClass();
1 efrain 271
 
272
        // Get providers preferences.
273
        foreach ($providers as $provider) {
274
            $linepref = get_user_preferences('message_provider_' . $provider->component . '_' . $provider->name
275
                . '_enabled', '', $userid);
276
            if ($linepref == '') {
277
                continue;
278
            }
279
            $lineprefarray = explode(',', $linepref);
280
            $preferences->{$provider->component.'_'.$provider->name.'_enabled'} = [];
281
            foreach ($lineprefarray as $pref) {
282
                $preferences->{$provider->component.'_'.$provider->name.'_enabled'}[$pref] = 1;
283
            }
284
 
285
        }
286
 
287
        return $preferences;
288
    }
289
 
290
    /**
291
     * Requires the JS libraries for the toggle contact button.
292
     *
293
     * @return void
294
     */
295
    public static function togglecontact_requirejs() {
296
        global $PAGE;
297
 
298
        static $done = false;
299
        if ($done) {
300
            return;
301
        }
302
 
303
        $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button'));
304
        $done = true;
305
    }
306
 
307
    /**
308
     * Returns the attributes to place on a contact button.
309
     *
310
     * @param object $user User object.
311
     * @param bool $iscontact
312
     * @param bool $displaytextlabel Instructs whether to display a text label.
11 efrain 313
     * @param bool $isrequested Whether the contact request is sent or not.
1 efrain 314
     * @return array
315
     */
11 efrain 316
    public static function togglecontact_link_params(
317
        $user,
318
        $iscontact = false,
319
        bool $displaytextlabel = true,
320
        bool $isrequested = false,
321
    ) {
1 efrain 322
        global $USER;
323
        $params = array(
324
            'data-currentuserid' => $USER->id,
325
            'data-userid' => $user->id,
326
            'data-is-contact' => $iscontact,
11 efrain 327
            'data-is-requested' => $isrequested,
1 efrain 328
            'data-display-text-label' => $displaytextlabel,
329
            'id' => 'toggle-contact-button',
330
            'role' => 'button',
331
            'class' => 'ajax-contact-button',
332
        );
333
 
334
        return $params;
335
    }
336
 
337
    /**
338
     * Requires the JS libraries for the message user button.
339
     *
340
     * @return void
341
     */
342
    public static function messageuser_requirejs() {
343
        global $PAGE;
344
 
345
        static $done = false;
346
        if ($done) {
347
            return;
348
        }
349
 
350
        $PAGE->requires->js_call_amd('core_message/message_user_button', 'send', array('#message-user-button'));
351
        $done = true;
352
    }
353
 
354
    /**
355
     * Returns the attributes to place on the message user button.
356
     *
357
     * @param int $useridto
358
     * @return array
359
     */
360
    public static function messageuser_link_params(int $useridto): array {
361
        global $USER;
362
 
363
        return [
364
            'id' => 'message-user-button',
365
            'role' => 'button',
366
            'data-conversationid' => api::get_conversation_between_users([$USER->id, $useridto]),
367
            'data-userid' => $useridto,
368
        ];
369
    }
370
 
371
    /**
372
     * Returns the conversation hash between users for easy look-ups in the DB.
373
     *
374
     * @param array $userids
375
     * @return string
376
     */
377
    public static function get_conversation_hash(array $userids) {
378
        sort($userids);
379
 
380
        return sha1(implode('-', $userids));
381
    }
382
 
383
    /**
384
     * Returns the cache key for the time created value of the last message of this conversation.
385
     *
386
     * @param int $convid The conversation identifier.
387
     * @return string The key.
388
     */
389
    public static function get_last_message_time_created_cache_key(int $convid) {
390
        return $convid;
391
    }
392
 
393
    /**
394
     * Checks if legacy messages exist for a given user.
395
     *
396
     * @param int $userid
397
     * @return bool
398
     */
399
    public static function legacy_messages_exist($userid) {
400
        global $DB;
401
 
402
        $sql = "SELECT id
403
                  FROM {message} m
404
                 WHERE useridfrom = ?
405
                    OR useridto = ?";
406
        $messageexists = $DB->record_exists_sql($sql, [$userid, $userid]);
407
 
408
        $sql = "SELECT id
409
                  FROM {message_read} m
410
                 WHERE useridfrom = ?
411
                    OR useridto = ?";
412
        $messagereadexists = $DB->record_exists_sql($sql, [$userid, $userid]);
413
 
414
        return $messageexists || $messagereadexists;
415
    }
416
 
417
    /**
418
     * Returns conversation member info for the supplied users, relative to the supplied referenceuserid.
419
     *
420
     * This is the basic structure used when returning members, and includes information about the relationship between each member
421
     * and the referenceuser, such as a whether the referenceuser has marked the member as a contact, or has blocked them.
422
     *
423
     * @param int $referenceuserid the id of the user which check contact and blocked status.
424
     * @param array $userids
425
     * @param bool $includecontactrequests Do we want to include contact requests with this data?
426
     * @param bool $includeprivacyinfo Do we want to include whether the user can message another, and if the user
427
     *             requires a contact.
428
     * @return array the array of objects containing member info, indexed by userid.
429
     * @throws \coding_exception
430
     * @throws \dml_exception
431
     */
432
    public static function get_member_info(int $referenceuserid, array $userids, bool $includecontactrequests = false,
433
                                           bool $includeprivacyinfo = false): array {
434
        global $DB, $PAGE;
435
 
436
        // Prevent exception being thrown when array is empty.
437
        if (empty($userids)) {
438
            return [];
439
        }
440
 
441
        list($useridsql, $usersparams) = $DB->get_in_or_equal($userids);
442
        $userfieldsapi = \core_user\fields::for_userpic()->including('lastaccess');
443
        $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
444
        $userssql = "SELECT $userfields, u.deleted, mc.id AS contactid, mub.id AS blockedid
445
                       FROM {user} u
446
                  LEFT JOIN {message_contacts} mc
447
                         ON ((mc.userid = ? AND mc.contactid = u.id) OR (mc.userid = u.id AND mc.contactid = ?))
448
                  LEFT JOIN {message_users_blocked} mub
449
                         ON (mub.userid = ? AND mub.blockeduserid = u.id)
450
                      WHERE u.id $useridsql";
451
        $usersparams = array_merge([$referenceuserid, $referenceuserid, $referenceuserid], $usersparams);
452
        $otherusers = $DB->get_records_sql($userssql, $usersparams);
453
 
454
        $members = [];
455
        foreach ($otherusers as $member) {
456
            // Set basic data.
1441 ariadna 457
            $data = new stdClass();
1 efrain 458
            $data->id = $member->id;
459
            $data->fullname = fullname($member);
460
 
461
            // Create the URL for their profile.
462
            $profileurl = new \moodle_url('/user/profile.php', ['id' => $member->id]);
463
            $data->profileurl = $profileurl->out(false);
464
 
465
            // Set the user picture data.
466
            $userpicture = new \user_picture($member);
467
            $userpicture->size = 1; // Size f1.
468
            $data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
469
            $userpicture->size = 0; // Size f2.
470
            $data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
471
 
472
            // Set online status indicators.
473
            $data->isonline = false;
474
            $data->showonlinestatus = false;
475
            if (!$member->deleted) {
476
                $data->isonline = self::show_online_status($member) ? self::is_online($member->lastaccess) : null;
477
                $data->showonlinestatus = is_null($data->isonline) ? false : true;
478
            }
479
 
480
            // Set contact and blocked status indicators.
481
            $data->iscontact = ($member->contactid) ? true : false;
482
 
1441 ariadna 483
            // Set permission to create a contact request.
484
            $data->cancreatecontact = api::can_create_contact($referenceuserid, $member->id);
485
 
1 efrain 486
            // We don't want that a user has been blocked if they can message the user anyways.
487
            $canmessageifblocked = api::can_send_message($referenceuserid, $member->id, true);
488
            $data->isblocked = ($member->blockedid && !$canmessageifblocked) ? true : false;
489
 
490
            $data->isdeleted = ($member->deleted) ? true : false;
491
 
492
            $data->requirescontact = null;
493
            $data->canmessage = null;
494
            $data->canmessageevenifblocked = null;
495
            if ($includeprivacyinfo) {
496
                $privacysetting = api::get_user_privacy_messaging_preference($member->id);
497
                $data->requirescontact = $privacysetting == api::MESSAGE_PRIVACY_ONLYCONTACTS;
498
 
499
                // Here we check that if the sender wanted to block the recipient, the
500
                // recipient would still be able to message them regardless.
501
                $data->canmessageevenifblocked = !$data->isdeleted && $canmessageifblocked;
502
                $data->canmessage = !$data->isdeleted && api::can_send_message($member->id, $referenceuserid);
503
            }
504
 
505
            // Populate the contact requests, even if we don't need them.
506
            $data->contactrequests = [];
507
 
508
            $members[$data->id] = $data;
509
        }
510
 
511
        // Check if we want to include contact requests as well.
512
        if (!empty($members) && $includecontactrequests) {
513
            list($useridsql, $usersparams) = $DB->get_in_or_equal($userids);
514
 
515
            $wheresql = "(userid $useridsql AND requesteduserid = ?) OR (userid = ? AND requesteduserid $useridsql)";
516
            $params = array_merge($usersparams, [$referenceuserid, $referenceuserid], $usersparams);
517
            if ($contactrequests = $DB->get_records_select('message_contact_requests', $wheresql, $params,
518
                    'timecreated ASC, id ASC')) {
519
                foreach ($contactrequests as $contactrequest) {
520
                    if (isset($members[$contactrequest->userid])) {
521
                        $members[$contactrequest->userid]->contactrequests[] = $contactrequest;
522
                    }
523
                    if (isset($members[$contactrequest->requesteduserid])) {
524
                        $members[$contactrequest->requesteduserid]->contactrequests[] = $contactrequest;
525
                    }
526
                }
527
            }
528
        }
529
 
530
        // Remove any userids not in $members. This can happen in the case of a user who has been deleted
531
        // from the Moodle database table (which can happen in earlier versions of Moodle).
532
        $userids = array_filter($userids, function($userid) use ($members) {
533
            return isset($members[$userid]);
534
        });
535
 
536
        // Return member information in the same order as the userids originally provided.
537
        $members = array_replace(array_flip($userids), $members);
538
 
539
        return $members;
540
    }
541
    /**
542
     * @deprecated since 3.6
543
     */
544
    public static function get_conversations_legacy_formatter() {
545
        throw new \coding_exception('\core_message\helper::get_conversations_legacy_formatter has been removed.');
546
    }
547
 
548
    /**
549
     * Renders the messaging widget.
550
     *
551
     * @param bool $isdrawer Are we are rendering the drawer or is this on a full page?
552
     * @param int|null $sendtouser The ID of the user we want to send a message to
553
     * @param int|null $conversationid The ID of the conversation we want to load
554
     * @param string|null $view The first view to load in the message widget
555
     * @return string The HTML.
556
     */
557
    public static function render_messaging_widget(
558
        bool $isdrawer,
1441 ariadna 559
        ?int $sendtouser = null,
560
        ?int $conversationid = null,
561
        ?string $view = null
1 efrain 562
    ) {
563
        global $USER, $CFG, $PAGE;
564
 
565
        // Early bail out conditions.
566
        if (empty($CFG->messaging) || !isloggedin() || isguestuser() || \core_user::awaiting_action()) {
567
            return '';
568
        }
569
 
570
        $renderer = $PAGE->get_renderer('core');
571
        $requestcount = \core_message\api::get_received_contact_requests_count($USER->id);
572
        $contactscount = \core_message\api::count_contacts($USER->id);
573
 
574
        $choices = [];
575
        $choices[] = [
576
            'value' => \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS,
577
            'text' => get_string('contactableprivacy_onlycontacts', 'message')
578
        ];
579
        $choices[] = [
580
            'value' => \core_message\api::MESSAGE_PRIVACY_COURSEMEMBER,
581
            'text' => get_string('contactableprivacy_coursemember', 'message')
582
        ];
583
        if (!empty($CFG->messagingallusers)) {
584
            // Add the MESSAGE_PRIVACY_SITE option when site-wide messaging between users is enabled.
585
            $choices[] = [
586
                'value' => \core_message\api::MESSAGE_PRIVACY_SITE,
587
                'text' => get_string('contactableprivacy_site', 'message')
588
            ];
589
        }
590
 
591
        // Enter to send.
592
        $entertosend = get_user_preferences('message_entertosend', $CFG->messagingdefaultpressenter, $USER);
593
 
594
        $notification = '';
595
        if (!get_user_preferences('core_message_migrate_data', false)) {
596
            $notification = get_string('messagingdatahasnotbeenmigrated', 'message');
597
        }
598
 
599
        if ($isdrawer) {
600
            $template = 'core_message/message_drawer';
601
            $messageurl = new \moodle_url('/message/index.php');
602
        } else {
603
            $template = 'core_message/message_index';
604
            $messageurl = null;
605
        }
606
 
607
        $templatecontext = [
608
            'contactrequestcount' => $requestcount,
609
            'loggedinuser' => [
610
                'id' => $USER->id,
611
                'midnight' => usergetmidnight(time())
612
            ],
613
            // The starting timeout value for message polling.
614
            'messagepollmin' => $CFG->messagingminpoll ?? MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS,
615
            // The maximum value that message polling timeout can reach.
616
            'messagepollmax' => $CFG->messagingmaxpoll ?? MESSAGE_DEFAULT_MAX_POLL_IN_SECONDS,
617
            // The timeout to reset back to after the max polling time has been reached.
618
            'messagepollaftermax' => $CFG->messagingtimeoutpoll ?? MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS,
619
            'contacts' => [
620
                'sectioncontacts' => [
621
                    'placeholders' => array_fill(0, $contactscount > 50 ? 50 : $contactscount, true)
622
                ],
623
                'sectionrequests' => [
624
                    'placeholders' => array_fill(0, $requestcount > 50 ? 50 : $requestcount, true)
625
                ],
626
            ],
627
            'settings' => [
628
                'privacy' => $choices,
629
                'entertosend' => $entertosend
630
            ],
631
            'overview' => [
632
                'messageurl' => $messageurl,
633
                'notification' => $notification
634
            ],
635
            'isdrawer' => $isdrawer,
636
            'showemojipicker' => !empty($CFG->allowemojipicker),
637
            'messagemaxlength' => api::MESSAGE_MAX_LENGTH,
638
            'caneditownmessageprofile' => has_capability('moodle/user:editownmessageprofile', \context_system::instance())
639
        ];
640
 
641
        if ($sendtouser || $conversationid) {
642
            $route = [
643
                'path' => 'view-conversation',
644
                'params' => $conversationid ? [$conversationid] : [null, 'create', $sendtouser]
645
            ];
646
        } else if ($view === 'contactrequests') {
647
            $route = [
648
                'path' => 'view-contacts',
649
                'params' => ['requests']
650
            ];
651
        } else {
652
            $route = null;
653
        }
654
 
655
        $templatecontext['route'] = json_encode($route);
656
 
657
        return $renderer->render_from_template($template, $templatecontext);
658
    }
659
 
660
    /**
661
     * Returns user details for a user, if they are visible to the current user in the message search.
662
     *
663
     * This method checks the visibility of a user specifically for the purpose of inclusion in the message search results.
664
     * Visibility depends on the site-wide messaging setting 'messagingallusers':
665
     * If enabled, visibility depends only on the core notion of visibility; a visible site or course profile.
666
     * If disabled, visibility requires that the user be sharing a course with the searching user, and have a visible profile there.
667
     * The current user is always returned.
668
     *
669
     * You can use the $userfields parameter to reduce the amount of a user record that is required by the method.
670
     * The minimum user fields are:
671
     *  * id
672
     *  * deleted
673
     *  * all potential fullname fields
674
     *
1441 ariadna 675
     * @param stdClass $user
1 efrain 676
     * @param array $userfields An array of userfields to be returned, the values must be a
677
     *                          subset of user_get_default_fields (optional)
678
     * @return array the array of userdetails, if visible, or an empty array otherwise.
679
     */
1441 ariadna 680
    public static function search_get_user_details(stdClass $user, array $userfields = []): array {
1 efrain 681
        global $CFG, $USER;
682
        require_once($CFG->dirroot . '/user/lib.php');
683
 
684
        if ($CFG->messagingallusers || $user->id == $USER->id) {
685
            return \user_get_user_details_courses($user, $userfields) ?? []; // This checks visibility of site and course profiles.
686
        } else {
687
            // Messaging specific: user must share a course with the searching user AND have a visible profile there.
688
            $sharedcourses = enrol_get_shared_courses($USER, $user);
689
            foreach ($sharedcourses as $course) {
690
                if (user_can_view_profile($user, $course)) {
691
                    $userdetails = user_get_user_details($user, $course, $userfields);
692
                    if (!is_null($userdetails)) {
693
                        return $userdetails;
694
                    }
695
                }
696
            }
697
        }
698
        return [];
699
    }
700
 
701
    /**
702
     * Prevent unclosed HTML elements in a message.
703
     *
704
     * @param string $message The html message.
705
     * @param bool $removebody True if we want to remove tag body.
706
     * @return string The html properly structured.
707
     */
708
    public static function prevent_unclosed_html_tags(
709
        string $message,
710
        bool $removebody = false
711
    ): string {
712
        $html = '';
713
        if (!empty($message)) {
714
            $doc = new DOMDocument();
715
            $olderror = libxml_use_internal_errors(true);
716
            $doc->loadHTML('<?xml version="1.0" encoding="UTF-8" ?>' . $message);
717
            libxml_clear_errors();
718
            libxml_use_internal_errors($olderror);
719
            $html = $doc->getElementsByTagName('body')->item(0)->C14N(false, true);
720
            if ($removebody) {
721
                // Remove <body> element added in C14N function.
722
                $html = preg_replace('~<(/?(?:body))[^>]*>\s*~i', '', $html);
723
            }
724
        }
725
 
726
        return $html;
727
    }
1441 ariadna 728
 
729
    /**
730
     * Check the support for SMS in different components.
731
     *
732
     * At the moment, SMS is not supported for all the components.
733
     * Components that supports SMS, can implement the callback to let the notification API support them.
734
     *
735
     * @param stdclass $messagedata The message data
736
     * @return bool
737
     */
738
    public static function supports_sms_notifications(stdclass $messagedata): bool {
739
        // Notification API assigns system ones as component 'moodle'.
740
        // We need a special mechanism to handle that as component callback will throw errors.
741
        if ($messagedata->component === 'moodle') {
742
            return false;
743
        }
744
 
745
        $smssupport = component_callback(
746
            $messagedata->component,
747
            'supports_sms_notifications'
748
        );
749
 
750
        if (empty($smssupport)) {
751
            return false;
752
        }
753
        return true;
754
    }
1 efrain 755
}