MDL-63303 message: add lastmessagedate to messagearea contact
[moodle.git] / message / classes / output / messagearea / contact.php
blob3a138c1250bd74c36dbbd58a51452dc9c7e4ea71
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.
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/>.
17 /**
18 * Contains class used to prepare a contact for display.
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
25 namespace core_message\output\messagearea;
27 defined('MOODLE_INTERNAL') || die();
29 use renderable;
30 use templatable;
32 /**
33 * Class to prepare a contact for display.
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
39 class contact implements templatable, renderable {
41 /**
42 * @var int Maximum length of message to show in panel.
44 const MAX_MSG_LENGTH = 60;
46 /**
47 * @var int The userid.
49 public $userid;
51 /**
52 * @var int The id of the user who sent the last message.
54 public $useridfrom;
56 /**
57 * @var string The fullname.
59 public $fullname;
61 /**
62 * @var string The profile image url.
64 public $profileimageurl;
66 /**
67 * @var string The small profile image url.
69 public $profileimageurlsmall;
71 /**
72 * @var int The message id.
74 public $messageid;
76 /**
77 * @var bool Are we messaging the user?
79 public $ismessaging;
81 /**
82 * @var string The last message sent.
84 public $lastmessage;
86 /**
87 * @var int The last message sent timestamp.
89 public $lastmessagedate;
91 /**
92 * @var bool Is the user online?
94 public $isonline;
96 /**
97 * @var bool Is the user blocked?
99 public $isblocked;
102 * @var bool Is the message read?
104 public $isread;
107 * @var int The number of unread messages.
109 public $unreadcount;
112 * Constructor.
114 * @param \stdClass $contact
116 public function __construct($contact) {
117 $this->userid = $contact->userid;
118 $this->useridfrom = $contact->useridfrom;
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;
125 $this->lastmessagedate = $contact->lastmessagedate;
126 $this->isonline = $contact->isonline;
127 $this->isblocked = $contact->isblocked;
128 $this->isread = $contact->isread;
129 $this->unreadcount = $contact->unreadcount;
132 public function export_for_template(\renderer_base $output) {
133 $contact = new \stdClass();
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;
140 $contact->sentfromcurrentuser = false;
141 if ($this->lastmessage) {
142 if ($this->userid !== $this->useridfrom) {
143 $contact->sentfromcurrentuser = true;
145 $contact->lastmessage = shorten_text($this->lastmessage, self::MAX_MSG_LENGTH);
146 } else {
147 $contact->lastmessage = null;
149 $contact->lastmessagedate = $this->lastmessagedate;
150 $contact->showonlinestatus = is_null($this->isonline) ? false : true;
151 $contact->isonline = $this->isonline;
152 $contact->isblocked = $this->isblocked;
153 $contact->isread = $this->isread;
154 $contact->unreadcount = $this->unreadcount;
156 return $contact;