mirrored from git://git.moodle.org/moodle.git
/
discussion.php
439 lines (396 loc) · 17.4 KB
/
discussion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Discussion renderer.
*
* @package mod_forum
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_forum\local\renderers;
defined('MOODLE_INTERNAL') || die();
use mod_forum\local\entities\discussion as discussion_entity;
use mod_forum\local\entities\forum as forum_entity;
use mod_forum\local\entities\post as post_entity;
use mod_forum\local\entities\sorter as sorter_entity;
use mod_forum\local\factories\entity as entity_factory;
use mod_forum\local\factories\legacy_data_mapper as legacy_data_mapper_factory;
use mod_forum\local\factories\exporter as exporter_factory;
use mod_forum\local\factories\url as url_factory;
use mod_forum\local\factories\vault as vault_factory;
use mod_forum\local\managers\capability as capability_manager;
use mod_forum\local\renderers\posts as posts_renderer;
use core\output\notification;
use context;
use context_module;
use html_writer;
use moodle_exception;
use moodle_page;
use moodle_url;
use rating_manager;
use renderer_base;
use single_button;
use single_select;
use stdClass;
use url_select;
require_once($CFG->dirroot . '/mod/forum/lib.php');
/**
* Discussion renderer class.
*
* @copyright 2019 Ryan Wyllie <ryan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class discussion {
/** @var forum_entity $forum The forum that the discussion belongs to */
private $forum;
/** @var discussion_entity $discussion The discussion entity */
private $discussion;
/** @var stdClass $discussionrecord Legacy discussion record */
private $discussionrecord;
/** @var stdClass $forumrecord Legacy forum record */
private $forumrecord;
/** @var int $displaymode The display mode to render the discussion in */
private $displaymode;
/** @var renderer_base $renderer Renderer base */
private $renderer;
/** @var posts_renderer $postsrenderer A posts renderer */
private $postsrenderer;
/** @var moodle_page $page The page this discussion is being rendered for */
private $page;
/** @var legacy_data_mapper_factory $legacydatamapperfactory Legacy data mapper factory */
private $legacydatamapperfactory;
/** @var exporter_factory $exporterfactory Exporter factory */
private $exporterfactory;
/** @var vault_factory $vaultfactory Vault factory */
private $vaultfactory;
/** @var url_factory $urlfactory URL factory */
private $urlfactory;
/** @var entity_factory $entityfactory Entity factory */
private $entityfactory;
/** @var capability_manager $capabilitymanager Capability manager */
private $capabilitymanager;
/** @var rating_manager $ratingmanager Rating manager */
private $ratingmanager;
/** @var moodle_url $baseurl The base URL for the discussion */
private $baseurl;
/** @var array $notifications List of HTML notifications to display */
private $notifications;
/** @var sorter_entity $exportedpostsorter Sorter for the exported posts */
private $exportedpostsorter;
/** @var callable $postprocessfortemplate Function to process exported posts before template rendering */
private $postprocessfortemplate;
/**
* Constructor.
*
* @param forum_entity $forum The forum that the discussion belongs to
* @param discussion_entity $discussion The discussion entity
* @param int $displaymode The display mode to render the discussion in
* @param renderer_base $renderer Renderer base
* @param posts_renderer $postsrenderer A posts renderer
* @param moodle_page $page The page this discussion is being rendered for
* @param legacy_data_mapper_factory $legacydatamapperfactory Legacy data mapper factory
* @param exporter_factory $exporterfactory Exporter factory
* @param vault_factory $vaultfactory Vault factory
* @param url_factory $urlfactory URL factory
* @param entity_factory $entityfactory Entity factory
* @param capability_manager $capabilitymanager Capability manager
* @param rating_manager $ratingmanager Rating manager
* @param sorter_entity $exportedpostsorter Sorter for the exported posts
* @param moodle_url $baseurl The base URL for the discussion
* @param array $notifications List of HTML notifications to display
* @param callable|null $postprocessfortemplate Post processing for template callback
*/
public function __construct(
forum_entity $forum,
discussion_entity $discussion,
int $displaymode,
renderer_base $renderer,
posts_renderer $postsrenderer,
moodle_page $page,
legacy_data_mapper_factory $legacydatamapperfactory,
exporter_factory $exporterfactory,
vault_factory $vaultfactory,
url_factory $urlfactory,
entity_factory $entityfactory,
capability_manager $capabilitymanager,
rating_manager $ratingmanager,
sorter_entity $exportedpostsorter,
moodle_url $baseurl,
array $notifications = [],
callable $postprocessfortemplate = null
) {
$this->forum = $forum;
$this->discussion = $discussion;
$this->displaymode = $displaymode;
$this->renderer = $renderer;
$this->postsrenderer = $postsrenderer;
$this->page = $page;
$this->baseurl = $baseurl;
$this->legacydatamapperfactory = $legacydatamapperfactory;
$this->exporterfactory = $exporterfactory;
$this->vaultfactory = $vaultfactory;
$this->urlfactory = $urlfactory;
$this->entityfactory = $entityfactory;
$this->capabilitymanager = $capabilitymanager;
$this->ratingmanager = $ratingmanager;
$this->notifications = $notifications;
$this->exportedpostsorter = $exportedpostsorter;
$this->postprocessfortemplate = $postprocessfortemplate;
$forumdatamapper = $this->legacydatamapperfactory->get_forum_data_mapper();
$this->forumrecord = $forumdatamapper->to_legacy_object($forum);
$discussiondatamapper = $this->legacydatamapperfactory->get_discussion_data_mapper();
$this->discussionrecord = $discussiondatamapper->to_legacy_object($discussion);
}
/**
* Render the discussion for the given user in the specified display mode.
*
* @param stdClass $user The user viewing the discussion
* @param post_entity $firstpost The first post in the discussion
* @param array $replies List of replies to the first post
* @return string HTML for the discussion
*/
public function render(
stdClass $user,
post_entity $firstpost,
array $replies
) : string {
global $CFG;
$displaymode = $this->displaymode;
$capabilitymanager = $this->capabilitymanager;
$urlfactory = $this->urlfactory;
$entityfactory = $this->entityfactory;
// Make sure we can render.
if (!$capabilitymanager->can_view_discussions($user)) {
throw new moodle_exception('noviewdiscussionspermission', 'mod_forum');
}
$posts = array_merge([$firstpost], array_values($replies));
if ($this->postprocessfortemplate !== null) {
$exporteddiscussion = ($this->postprocessfortemplate) ($this->discussion, $user, $this->forum);
} else {
$exporteddiscussion = $this->get_exported_discussion($user);
}
$hasanyactions = false;
$hasanyactions = $hasanyactions || $capabilitymanager->can_favourite_discussion($user);
$hasanyactions = $hasanyactions || $capabilitymanager->can_pin_discussions($user);
$hasanyactions = $hasanyactions || $capabilitymanager->can_manage_forum($user);
$exporteddiscussion = array_merge($exporteddiscussion, [
'notifications' => $this->get_notifications($user),
'html' => [
'hasanyactions' => $hasanyactions,
'posts' => $this->postsrenderer->render($user, [$this->forum], [$this->discussion], $posts),
'modeselectorform' => $this->get_display_mode_selector_html($displaymode, $user),
'subscribe' => null,
'movediscussion' => null,
'pindiscussion' => null,
'neighbourlinks' => $this->get_neighbour_links_html(),
'exportdiscussion' => !empty($CFG->enableportfolios) ? $this->get_export_discussion_html() : null
]
]);
$capabilities = (array) $exporteddiscussion['capabilities'];
if ($capabilities['move']) {
$exporteddiscussion['html']['movediscussion'] = $this->get_move_discussion_html();
}
if (!empty($user->id)) {
$loggedinuser = $entityfactory->get_author_from_stdClass($user);
$exporteddiscussion['loggedinuser'] = [
'firstname' => $loggedinuser->get_first_name(),
'fullname' => $loggedinuser->get_full_name(),
'profileimageurl' => ($urlfactory->get_author_profile_image_url($loggedinuser, null))->out(false)
];
}
if ($this->displaymode === FORUM_MODE_NESTED_V2) {
$template = 'mod_forum/forum_discussion_nested_v2';
} else {
$template = 'mod_forum/forum_discussion';
}
return $this->renderer->render_from_template($template, $exporteddiscussion);
}
/**
* Get the groups details for all groups available to the forum.
*
* @return stdClass[]
*/
private function get_groups_available_in_forum() : array {
$course = $this->forum->get_course_record();
$coursemodule = $this->forum->get_course_module_record();
return groups_get_all_groups($course->id, 0, $coursemodule->groupingid);
}
/**
* Get the exported discussion.
*
* @param stdClass $user The user viewing the discussion
* @return array
*/
private function get_exported_discussion(stdClass $user) : array {
$discussionexporter = $this->exporterfactory->get_discussion_exporter(
$user,
$this->forum,
$this->discussion,
$this->get_groups_available_in_forum()
);
return (array) $discussionexporter->export($this->renderer);
}
/**
* Get the HTML for the display mode selector.
*
* @param int $displaymode The current display mode
* @param stdClass $user The current user
* @return string
*/
private function get_display_mode_selector_html(int $displaymode, stdClass $user) : string {
$baseurl = $this->baseurl;
$select = new single_select(
$baseurl,
'mode',
forum_get_layout_modes(get_user_preferences('forum_useexperimentalui', false, $user)),
$displaymode,
null,
'mode'
);
$select->set_label(get_string('displaymode', 'forum'), ['class' => 'accesshide']);
return $this->renderer->render($select);
}
/**
* Get the HTML to render the move discussion selector and button.
*
* @return string
*/
private function get_move_discussion_html() : ?string {
global $DB;
$forum = $this->forum;
$discussion = $this->discussion;
$courseid = $forum->get_course_id();
// Popup menu to move discussions to other forums. The discussion in a
// single discussion forum can't be moved.
$modinfo = get_fast_modinfo($courseid);
if (isset($modinfo->instances['forum'])) {
$forummenu = [];
// Check forum types and eliminate simple discussions.
$forumcheck = $DB->get_records('forum', ['course' => $courseid], '', 'id, type');
foreach ($modinfo->instances['forum'] as $forumcm) {
if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
context_module::instance($forumcm->id))) {
continue;
}
$section = $forumcm->sectionnum;
$sectionname = get_section_name($courseid, $section);
if (empty($forummenu[$section])) {
$forummenu[$section] = [$sectionname => []];
}
$forumidcompare = $forumcm->instance != $forum->get_id();
$forumtypecheck = $forumcheck[$forumcm->instance]->type !== 'single';
if ($forumidcompare and $forumtypecheck) {
$url = "/mod/forum/discuss.php?d={$discussion->get_id()}&move=$forumcm->instance&sesskey=".sesskey();
$forummenu[$section][$sectionname][$url] = format_string($forumcm->name);
}
}
if (!empty($forummenu)) {
$html = '<div class="movediscussionoption">';
$movebutton = get_string('move');
if ($this->displaymode === FORUM_MODE_NESTED_V2) {
// Move discussion selector will be rendered on the settings drawer. We won't output the button in this mode.
$movebutton = null;
}
$select = new url_select($forummenu, '',
['/mod/forum/discuss.php?d=' . $discussion->get_id() => get_string("movethisdiscussionto", "forum")],
'forummenu', $movebutton);
$html .= $this->renderer->render($select);
$html .= "</div>";
return $html;
}
}
return null;
}
/**
* Get the HTML to render the export discussion button.
*
* @return string|null
*/
private function get_export_discussion_html() : ?string {
global $CFG;
require_once($CFG->libdir . '/portfoliolib.php');
$discussion = $this->discussion;
$button = new \portfolio_add_button();
$button->set_callback_options('forum_portfolio_caller', ['discussionid' => $discussion->get_id()], 'mod_forum');
$button = $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportdiscussion', 'mod_forum'));
return $button ?: null;
}
/**
* Get a list of notification HTML to render in the page.
*
* @param stdClass $user The user viewing the discussion
* @return string[]
*/
private function get_notifications($user) : array {
$notifications = $this->notifications;
$discussion = $this->discussion;
$forum = $this->forum;
$renderer = $this->renderer;
if ($forum->is_cutoff_date_reached()) {
$notifications[] = (new notification(
get_string('cutoffdatereached', 'forum'),
notification::NOTIFY_INFO
))->set_show_closebutton();
} else if ($forum->is_due_date_reached()) {
$notifications[] = (new notification(
get_string('thisforumisdue', 'forum', userdate($forum->get_due_date())),
notification::NOTIFY_INFO
))->set_show_closebutton();
} else if ($forum->has_due_date()) {
$notifications[] = (new notification(
get_string('thisforumhasduedate', 'forum', userdate($forum->get_due_date())),
notification::NOTIFY_INFO
))->set_show_closebutton();
}
if ($forum->is_discussion_locked($discussion)) {
$notifications[] = (new notification(
get_string('discussionlocked', 'forum'),
notification::NOTIFY_INFO
))
->set_extra_classes(['discussionlocked'])
->set_show_closebutton();
}
if ($forum->get_type() == 'qanda') {
if ($this->capabilitymanager->must_post_before_viewing_discussion($user, $discussion)) {
$notifications[] = (new notification(
get_string('qandanotify', 'forum')
))->set_show_closebutton(true);
}
}
if ($forum->has_blocking_enabled()) {
$notifications[] = (new notification(
get_string('thisforumisthrottled', 'forum', [
'blockafter' => $forum->get_block_after(),
'blockperiod' => get_string('secondstotime' . $forum->get_block_period())
])
))->set_show_closebutton();
}
return array_map(function($notification) {
return $notification->export_for_template($this->renderer);
}, $notifications);
}
/**
* Get HTML to display the neighbour links.
*
* @return string
*/
private function get_neighbour_links_html() : string {
$forum = $this->forum;
$coursemodule = $forum->get_course_module_record();
$neighbours = forum_get_discussion_neighbours($coursemodule, $this->discussionrecord, $this->forumrecord);
return $this->renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']);
}
}