MDL-64034 core_message: return conversationid in search_messages()
[moodle.git] / message / classes / output / messagearea / contact.php
CommitLineData
879e2bef
MN
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 class used to prepare a contact for display.
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
1e5f751f 25namespace core_message\output\messagearea;
879e2bef 26
7b55aaa1
MN
27defined('MOODLE_INTERNAL') || die();
28
879e2bef
MN
29use renderable;
30use templatable;
31
32/**
33 * Class to prepare a contact for display.
34 *
35 * @package core_message
36 * @copyright 2016 Mark Nelson <markn@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class contact implements templatable, renderable {
40
41 /**
0dc4a566 42 * @var int Maximum length of message to show in panel.
879e2bef
MN
43 */
44 const MAX_MSG_LENGTH = 60;
45
46 /**
0dc4a566 47 * @var int The userid.
879e2bef 48 */
0dc4a566
MN
49 public $userid;
50
89a70ba1
MN
51 /**
52 * @var int The id of the user who sent the last message.
53 */
54 public $useridfrom;
55
0dc4a566
MN
56 /**
57 * @var string The fullname.
58 */
59 public $fullname;
60
61 /**
62 * @var string The profile image url.
63 */
64 public $profileimageurl;
65
66 /**
67 * @var string The small profile image url.
68 */
69 public $profileimageurlsmall;
70
71 /**
72 * @var int The message id.
73 */
74 public $messageid;
75
76 /**
77 * @var bool Are we messaging the user?
78 */
79 public $ismessaging;
80
81 /**
82 * @var string The last message sent.
83 */
84 public $lastmessage;
85
0802c38a
RW
86 /**
87 * @var int The last message sent timestamp.
88 */
89 public $lastmessagedate;
90
0dc4a566
MN
91 /**
92 * @var bool Is the user online?
93 */
94 public $isonline;
95
96 /**
97 * @var bool Is the user blocked?
98 */
99 public $isblocked;
100
101 /**
102 * @var bool Is the message read?
103 */
104 public $isread;
105
106 /**
107 * @var int The number of unread messages.
108 */
109 public $unreadcount;
879e2bef 110
d2708759
JD
111 /**
112 * @var int The id of the conversation to which to message belongs.
113 */
114 public $conversationid;
115
879e2bef
MN
116 /**
117 * Constructor.
118 *
119 * @param \stdClass $contact
120 */
121 public function __construct($contact) {
0dc4a566 122 $this->userid = $contact->userid;
89a70ba1 123 $this->useridfrom = $contact->useridfrom;
0dc4a566
MN
124 $this->fullname = $contact->fullname;
125 $this->profileimageurl = $contact->profileimageurl;
126 $this->profileimageurlsmall = $contact->profileimageurlsmall;
127 $this->messageid = $contact->messageid;
128 $this->ismessaging = $contact->ismessaging;
129 $this->lastmessage = $contact->lastmessage;
0802c38a 130 $this->lastmessagedate = $contact->lastmessagedate;
0dc4a566
MN
131 $this->isonline = $contact->isonline;
132 $this->isblocked = $contact->isblocked;
133 $this->isread = $contact->isread;
134 $this->unreadcount = $contact->unreadcount;
d2708759 135 $this->conversationid = $contact->conversationid ?? null;
8c250260
RW
136 }
137
879e2bef
MN
138 public function export_for_template(\renderer_base $output) {
139 $contact = new \stdClass();
0dc4a566
MN
140 $contact->userid = $this->userid;
141 $contact->fullname = $this->fullname;
142 $contact->profileimageurl = $this->profileimageurl;
143 $contact->profileimageurlsmall = $this->profileimageurlsmall;
144 $contact->messageid = $this->messageid;
145 $contact->ismessaging = $this->ismessaging;
89a70ba1 146 $contact->sentfromcurrentuser = false;
0dc4a566 147 if ($this->lastmessage) {
89a70ba1
MN
148 if ($this->userid !== $this->useridfrom) {
149 $contact->sentfromcurrentuser = true;
150 }
0dc4a566 151 $contact->lastmessage = shorten_text($this->lastmessage, self::MAX_MSG_LENGTH);
49aaadc3
MN
152 } else {
153 $contact->lastmessage = null;
154 }
0802c38a 155 $contact->lastmessagedate = $this->lastmessagedate;
cb805753 156 $contact->showonlinestatus = is_null($this->isonline) ? false : true;
0dc4a566
MN
157 $contact->isonline = $this->isonline;
158 $contact->isblocked = $this->isblocked;
159 $contact->isread = $this->isread;
160 $contact->unreadcount = $this->unreadcount;
d2708759 161 $contact->conversationid = $this->conversationid;
879e2bef
MN
162
163 return $contact;
164 }
165}