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