MDL-55623 core_message: create helper function for WS to define 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
MN
26
27use renderable;
28use templatable;
29
30/**
31 * Class to prepare a contact for display.
32 *
33 * @package core_message
34 * @copyright 2016 Mark Nelson <markn@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class contact implements templatable, renderable {
38
39 /**
0dc4a566 40 * @var int Maximum length of message to show in panel.
879e2bef
MN
41 */
42 const MAX_MSG_LENGTH = 60;
43
44 /**
0dc4a566 45 * @var int The userid.
879e2bef 46 */
0dc4a566
MN
47 public $userid;
48
49 /**
50 * @var string The fullname.
51 */
52 public $fullname;
53
54 /**
55 * @var string The profile image url.
56 */
57 public $profileimageurl;
58
59 /**
60 * @var string The small profile image url.
61 */
62 public $profileimageurlsmall;
63
64 /**
65 * @var int The message id.
66 */
67 public $messageid;
68
69 /**
70 * @var bool Are we messaging the user?
71 */
72 public $ismessaging;
73
74 /**
75 * @var string The last message sent.
76 */
77 public $lastmessage;
78
79 /**
80 * @var bool Is the user online?
81 */
82 public $isonline;
83
84 /**
85 * @var bool Is the user blocked?
86 */
87 public $isblocked;
88
89 /**
90 * @var bool Is the message read?
91 */
92 public $isread;
93
94 /**
95 * @var int The number of unread messages.
96 */
97 public $unreadcount;
879e2bef
MN
98
99 /**
100 * Constructor.
101 *
102 * @param \stdClass $contact
103 */
104 public function __construct($contact) {
0dc4a566
MN
105 $this->userid = $contact->userid;
106 $this->fullname = $contact->fullname;
107 $this->profileimageurl = $contact->profileimageurl;
108 $this->profileimageurlsmall = $contact->profileimageurlsmall;
109 $this->messageid = $contact->messageid;
110 $this->ismessaging = $contact->ismessaging;
111 $this->lastmessage = $contact->lastmessage;
112 $this->isonline = $contact->isonline;
113 $this->isblocked = $contact->isblocked;
114 $this->isread = $contact->isread;
115 $this->unreadcount = $contact->unreadcount;
8c250260
RW
116 }
117
879e2bef
MN
118 public function export_for_template(\renderer_base $output) {
119 $contact = new \stdClass();
0dc4a566
MN
120 $contact->userid = $this->userid;
121 $contact->fullname = $this->fullname;
122 $contact->profileimageurl = $this->profileimageurl;
123 $contact->profileimageurlsmall = $this->profileimageurlsmall;
124 $contact->messageid = $this->messageid;
125 $contact->ismessaging = $this->ismessaging;
126 if ($this->lastmessage) {
127 $contact->lastmessage = shorten_text($this->lastmessage, self::MAX_MSG_LENGTH);
49aaadc3
MN
128 } else {
129 $contact->lastmessage = null;
130 }
0dc4a566
MN
131 $contact->isonline = $this->isonline;
132 $contact->isblocked = $this->isblocked;
133 $contact->isread = $this->isread;
134 $contact->unreadcount = $this->unreadcount;
879e2bef
MN
135
136 return $contact;
137 }
138}