Moodle release 3.6beta
[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
MN
110
111 /**
112 * Constructor.
113 *
114 * @param \stdClass $contact
115 */
116 public function __construct($contact) {
0dc4a566 117 $this->userid = $contact->userid;
89a70ba1 118 $this->useridfrom = $contact->useridfrom;
0dc4a566
MN
119 $this->fullname = $contact->fullname;
120 $this->profileimageurl = $contact->profileimageurl;
121 $this->profileimageurlsmall = $contact->profileimageurlsmall;
122 $this->messageid = $contact->messageid;
123 $this->ismessaging = $contact->ismessaging;
124 $this->lastmessage = $contact->lastmessage;
0802c38a 125 $this->lastmessagedate = $contact->lastmessagedate;
0dc4a566
MN
126 $this->isonline = $contact->isonline;
127 $this->isblocked = $contact->isblocked;
128 $this->isread = $contact->isread;
129 $this->unreadcount = $contact->unreadcount;
8c250260
RW
130 }
131
879e2bef
MN
132 public function export_for_template(\renderer_base $output) {
133 $contact = new \stdClass();
0dc4a566
MN
134 $contact->userid = $this->userid;
135 $contact->fullname = $this->fullname;
136 $contact->profileimageurl = $this->profileimageurl;
137 $contact->profileimageurlsmall = $this->profileimageurlsmall;
138 $contact->messageid = $this->messageid;
139 $contact->ismessaging = $this->ismessaging;
89a70ba1 140 $contact->sentfromcurrentuser = false;
0dc4a566 141 if ($this->lastmessage) {
89a70ba1
MN
142 if ($this->userid !== $this->useridfrom) {
143 $contact->sentfromcurrentuser = true;
144 }
0dc4a566 145 $contact->lastmessage = shorten_text($this->lastmessage, self::MAX_MSG_LENGTH);
49aaadc3
MN
146 } else {
147 $contact->lastmessage = null;
148 }
0802c38a 149 $contact->lastmessagedate = $this->lastmessagedate;
cb805753 150 $contact->showonlinestatus = is_null($this->isonline) ? false : true;
0dc4a566
MN
151 $contact->isonline = $this->isonline;
152 $contact->isblocked = $this->isblocked;
153 $contact->isread = $this->isread;
154 $contact->unreadcount = $this->unreadcount;
879e2bef
MN
155
156 return $contact;
157 }
158}