Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Discussion summary exporter class.
19
 *
20
 * @package    mod_forum
21
 * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace mod_forum\local\exporters;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use mod_forum\local\entities\discussion_summary as discussion_summary_entity;
30
use core\external\exporter;
31
use renderer_base;
32
 
33
/**
34
 * Discussion summary exporter class.
35
 *
36
 * @copyright  2019 Andrew Nicols <andrew@nicols.co.uk>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class discussion_summary extends exporter {
40
    /** @var discussion_summary_entity The discussion summary information */
41
    private $summary;
42
 
43
    /** @var stdClass[] The group information for each author */
44
    private $groupsbyid;
45
 
46
    /** @var stdClass[] The group information for each author */
47
    private $groupsbyauthorid;
48
 
49
    /** @var int The number of replies to the discussion */
50
    private $replycount;
51
 
52
    /** @var int number of unread posts if the user is tracking these */
53
    private $unreadcount;
54
 
55
    /** @var int The latest post id in the discussion */
56
    private $latestpostid;
57
 
58
    /** @var int|null The context id for the author of the first post */
59
    private $firstpostauthorcontextid;
60
 
61
    /** @var int|null The context id for the author of the latest post */
62
    private $latestpostauthorcontextid;
63
 
64
    /**
65
     * Constructor.
66
     *
67
     * @param discussion_summary_entity $summary The discussion summary information
68
     * @param stdClass[] $groupsbyid The group information for each author
69
     * @param stdClass[] $groupsbyauthorid The group information for each author
70
     * @param int $replycount The number of replies to the discussion
71
     * @param int $unreadcount number of unread posts if the user is tracking these
72
     * @param int $latestpostid The latest post id in the discussion
73
     * @param int|null $firstpostauthorcontextid The context id for the author of the first post
74
     * @param int|null $latestpostauthorcontextid The context id for the author of the latest post
75
     * @param array $related The related objects
76
     */
77
    public function __construct(
78
        discussion_summary_entity $summary,
79
        array $groupsbyid,
80
        array $groupsbyauthorid,
81
        int $replycount,
82
        int $unreadcount,
83
        int $latestpostid,
84
        ?int $firstpostauthorcontextid,
85
        ?int $latestpostauthorcontextid,
86
        array $related = []
87
    ) {
88
        $this->summary = $summary;
89
        $this->groupsbyid = $groupsbyid;
90
        $this->groupsbyauthorid = $groupsbyauthorid;
91
        $this->replycount = $replycount;
92
        $this->unreadcount = $unreadcount;
93
        $this->latestpostid = $latestpostid;
94
        $this->firstpostauthorcontextid = $firstpostauthorcontextid;
95
        $this->latestpostauthorcontextid = $latestpostauthorcontextid;
96
        return parent::__construct([], $related);
97
    }
98
 
99
    /**
100
     * Return the list of additional properties.
101
     *
102
     * @return array
103
     */
104
    protected static function define_other_properties() {
105
        return [
106
            'id' => ['type' => PARAM_INT],
107
            'discussion' => [
108
                'type' => discussion::read_properties_definition(),
109
            ],
110
            'replies' => ['type' => PARAM_INT],
111
            'unread' => ['type' => PARAM_INT],
112
            'firstpostauthor' => [
113
                'type' => author::read_properties_definition(),
114
            ],
115
            'latestpostauthor' => [
116
                'type' => author::read_properties_definition(),
117
            ],
118
            'latestpostid' => ['type' => PARAM_INT],
119
        ];
120
    }
121
 
122
    /**
123
     * Get the additional values to inject while exporting.
124
     *
125
     * @param renderer_base $output The renderer.
126
     * @return array Keys are the property names, values are their values.
127
     */
128
    protected function get_other_values(renderer_base $output) {
129
        $capabilitymanager = $this->related['capabilitymanager'];
130
        $forum = $this->related['forum'];
131
        $user = $this->related['user'];
132
        $latestpostauthor = $this->related['latestauthor'];
133
        $discussion = $this->summary->get_discussion();
134
 
135
        $related = (array) (object) $this->related;
136
        $related['latestpostid'] = $this->latestpostid;
137
        $related['groupsbyid'] = $this->groupsbyid;
138
        $discussionexporter = new discussion($discussion, $related);
139
 
140
        $related = [
141
            'urlfactory' => $this->related['urlfactory'],
142
            'context' => $this->related['forum']->get_context(),
143
            'forum' => $forum,
144
        ];
145
 
146
        $firstpostauthor = new author(
147
            $this->summary->get_first_post_author(),
148
            $this->firstpostauthorcontextid,
149
            $this->groupsbyauthorid[$this->summary->get_first_post_author()->get_id()],
150
            $capabilitymanager->can_view_post(
151
                $user,
152
                $discussion,
153
                $this->summary->get_first_post()
154
            ),
155
            $related
156
        );
157
 
158
        $latestpostauthor = new author(
159
            $latestpostauthor ?? $this->summary->get_latest_post_author(),
160
            $this->latestpostauthorcontextid,
161
            [],
162
            $capabilitymanager->can_view_post(
163
                $user,
164
                $discussion,
165
                $this->summary->get_first_post()
166
            ),
167
            $related
168
        );
169
 
170
        return [
171
            'id' => $discussion->get_id(),
172
            'discussion' => $discussionexporter->export($output),
173
            'replies' => $this->replycount,
174
            'unread' => $this->unreadcount,
175
            'firstpostauthor' => $firstpostauthor->export($output),
176
            'latestpostauthor' => $latestpostauthor->export($output),
177
            'latestpostid' => $this->latestpostid,
178
        ];
179
    }
180
 
181
    /**
182
     * Returns a list of objects that are related.
183
     *
184
     * @return array
185
     */
186
    protected static function define_related() {
187
        return [
188
            'legacydatamapperfactory' => 'mod_forum\local\factories\legacy_data_mapper',
189
            'context' => 'context',
190
            'forum' => 'mod_forum\local\entities\forum',
191
            'capabilitymanager' => 'mod_forum\local\managers\capability',
192
            'urlfactory' => 'mod_forum\local\factories\url',
193
            'user' => 'stdClass',
194
            'favouriteids' => 'int[]?',
195
            'latestauthor' => 'mod_forum\local\entities\author?'
196
        ];
197
    }
198
}