MDL-63303 message: add count_contacts api function
[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
86 /**
87 * @var bool Is the user online?
88 */
89 public $isonline;
90
91 /**
92 * @var bool Is the user blocked?
93 */
94 public $isblocked;
95
96 /**
97 * @var bool Is the message read?
98 */
99 public $isread;
100
101 /**
102 * @var int The number of unread messages.
103 */
104 public $unreadcount;
879e2bef
MN
105
106 /**
107 * Constructor.
108 *
109 * @param \stdClass $contact
110 */
111 public function __construct($contact) {
0dc4a566 112 $this->userid = $contact->userid;
89a70ba1 113 $this->useridfrom = $contact->useridfrom;
0dc4a566
MN
114 $this->fullname = $contact->fullname;
115 $this->profileimageurl = $contact->profileimageurl;
116 $this->profileimageurlsmall = $contact->profileimageurlsmall;
117 $this->messageid = $contact->messageid;
118 $this->ismessaging = $contact->ismessaging;
119 $this->lastmessage = $contact->lastmessage;
120 $this->isonline = $contact->isonline;
121 $this->isblocked = $contact->isblocked;
122 $this->isread = $contact->isread;
123 $this->unreadcount = $contact->unreadcount;
8c250260
RW
124 }
125
879e2bef
MN
126 public function export_for_template(\renderer_base $output) {
127 $contact = new \stdClass();
0dc4a566
MN
128 $contact->userid = $this->userid;
129 $contact->fullname = $this->fullname;
130 $contact->profileimageurl = $this->profileimageurl;
131 $contact->profileimageurlsmall = $this->profileimageurlsmall;
132 $contact->messageid = $this->messageid;
133 $contact->ismessaging = $this->ismessaging;
89a70ba1 134 $contact->sentfromcurrentuser = false;
0dc4a566 135 if ($this->lastmessage) {
89a70ba1
MN
136 if ($this->userid !== $this->useridfrom) {
137 $contact->sentfromcurrentuser = true;
138 }
0dc4a566 139 $contact->lastmessage = shorten_text($this->lastmessage, self::MAX_MSG_LENGTH);
49aaadc3
MN
140 } else {
141 $contact->lastmessage = null;
142 }
cb805753 143 $contact->showonlinestatus = is_null($this->isonline) ? false : true;
0dc4a566
MN
144 $contact->isonline = $this->isonline;
145 $contact->isblocked = $this->isblocked;
146 $contact->isread = $this->isread;
147 $contact->unreadcount = $this->unreadcount;
879e2bef
MN
148
149 return $contact;
150 }
151}