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 the message area 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 the message area 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 message_area implements templatable, renderable {
43
44    /**
45     * @var int The user id.
46     */
47    public $userid;
48
49    /**
50     * @var int The other user id.
51     */
52    public $otheruserid;
53
54    /**
55     * @var array The contacts for the users.
56     */
57    public $contacts;
58
59    /**
60     * @var array The messages for the user.
61     */
62    public $messages;
63
64    /**
65     * @var bool Was a specific conversation requested.
66     */
67    public $requestedconversation;
68
69    /**
70     * @var int The minimum time to poll for messages.
71     */
72    public $pollmin;
73
74    /**
75     * @var int The maximum time to poll for messages.
76     */
77    public $pollmax;
78
79    /**
80     * @var int The time used once we have reached the maximum polling time.
81     */
82    public $polltimeout;
83
84    /**
85     * @var bool Are we creating a new message and show the contacts section first?
86     */
87    public $contactsfirst;
88
89    /**
90     * Constructor.
91     *
92     * @param int $userid The ID of the user whose contacts and messages we are viewing
93     * @param int|null $otheruserid The id of the user we are viewing, null if none
94     * @param array $contacts
95     * @param array|null $messages
96     * @param bool $requestedconversation
97     * @param bool $contactsfirst Whether we are viewing the contacts first.
98     * @param int $pollmin
99     * @param int $pollmax
100     * @param int $polltimeout
101     */
102    public function __construct($userid, $otheruserid, $contacts, $messages, $requestedconversation, $contactsfirst, $pollmin,
103            $pollmax, $polltimeout) {
104        $this->userid = $userid;
105        // Setting the other user to null when showing contacts will remove any contact from being selected.
106        $this->otheruserid = (!$contactsfirst) ? $otheruserid : null;
107        $this->contacts = $contacts;
108        $this->messages = $messages;
109        $this->requestedconversation = $requestedconversation;
110        $this->pollmin = $pollmin;
111        $this->pollmax = $pollmax;
112        $this->polltimeout = $polltimeout;
113        $this->contactsfirst = $contactsfirst;
114    }
115
116    public function export_for_template(\renderer_base $output) {
117        $data = new \stdClass();
118        $data->userid = $this->userid;
119        $contacts = new contacts($this->otheruserid, $this->contacts);
120        $data->contacts = $contacts->export_for_template($output);
121        if ($this->contactsfirst) {
122            // Don't show any messages if we are creating a new message.
123            $messages = new messages($this->userid, null, array());
124        } else {
125            $messages = new messages($this->userid, $this->otheruserid, $this->messages);
126        }
127        $data->messages = $messages->export_for_template($output);
128        $data->isconversation = ($this->contactsfirst) ? false : true;
129        $data->requestedconversation = $this->requestedconversation;
130        $data->pollmin = $this->pollmin;
131        $data->pollmax = $this->pollmax;
132        $data->polltimeout = $this->polltimeout;
133        $data->contactsfirst = $this->contactsfirst;
134
135        return $data;
136    }
137}
138