-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue.php
More file actions
463 lines (415 loc) · 19.5 KB
/
issue.php
File metadata and controls
463 lines (415 loc) · 19.5 KB
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<?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/>.
/**
* @package local_edusupport
* @copyright 2020 Center for Learningmanagement (www.lernmanagement.at)
* @author Robert Schrenk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../../config.php');
// We fake a forum discussion here.
// This code is mainly taken from /mod/forum/discuss.php.
$d = optional_param('d', 0, PARAM_INT); // Discussionid.
$discussion = optional_param('discussion', 0, PARAM_INT); // Discussionid.
$discussionid = $discussion | $d;
$replyto = optional_param('replyto', 0, PARAM_INT); // If set, we reply to this post.
$parent = optional_param('parent', 0, PARAM_INT); // If set, then display this post and all children.
$mode = optional_param('mode', 0, PARAM_INT); // If set, changes the layout of the thread.
$move = optional_param('move', 0, PARAM_INT); // If set, moves this discussion to another forum.
$mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated.
$postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated.
$pin = optional_param('pin', -1, PARAM_INT); // If set, pin or unpin this discussion.
$edit = optional_param('edit', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_INT);
$url = new moodle_url('/local/edusupport/issue.php', ['discussion' => $discussionid,
'replyto' => $replyto, 'delete' => $delete]);
if ($parent !== 0) {
$url->param('parent', $parent);
}
$PAGE->set_url($url);
$context = \context_system::instance();
$PAGE->set_context($context);
require_login();
$issue = \local_edusupport\lib::get_issue($discussionid, false);
$discussion = $DB->get_record('forum_discussions', ['id' => $discussionid], '*', MUST_EXIST);
$PAGE->set_title($discussion->name);
$PAGE->set_heading($discussion->name);
$issueslinkname = get_string('issues', 'local_edusupport');
if (!\local_edusupport\lib::is_supportteam() && !is_siteadmin()) {
echo $OUTPUT->header();
$cm = \get_coursemodule_from_instance('forum', $discussion->forum);
$tocmurl = new moodle_url('/mod/forum/view.php', ['id' => $cm->id]);
echo $OUTPUT->render_from_template('local_edusupport/alert', [
'content' => get_string('missing_permission', 'local_edusupport'),
'type' => 'danger',
'url' => $tocmurl->__toString(),
]);
} else if (empty($issue->id)) {
echo $OUTPUT->header();
$toissuesurl = new moodle_url('/local/edusupport/issues.php', []);
$todiscussionurl = new moodle_url('/mod/forum/discuss.php', ['d' => $discussionid]);
echo $OUTPUT->render_from_template('local_edusupport/alert', [
'content' => get_string('no_such_issue', 'local_edusupport', [
'todiscussionurl' => $todiscussionurl->__toString(),
'toissuesurl' => $toissuesurl->__toString(),
]),
'type' => 'danger',
]);
} else {
$forum = $DB->get_record('forum', ['id' => $discussion->forum], '*', MUST_EXIST);
$course = get_course($forum->course);
$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
$post = $DB->get_record('forum_posts', ['discussion' => $discussionid, 'parent' => 0]);
$coursecontext = \context_course::instance($forum->course);
$modcontext = \context_module::instance($cm->id);
$PAGE->set_title("$course->shortname: " . format_string($discussion->name));
$PAGE->set_heading($course->fullname);
$PAGE->navbar->add($issueslinkname, new moodle_url('/local/edusupport/issues.php'));
$PAGE->navbar->add($course->shortname, new moodle_url('/mod/forum/view.php', ['id' => $cm->id]));
$PAGE->navbar->add(get_string('issue', 'local_edusupport') . ": {$discussion->name}", new moodle_url($url));
$vaultfactory = \mod_forum\local\container::get_vault_factory();
$discussionvault = $vaultfactory->get_discussion_vault();
$vdiscussion = $discussionvault->get_from_id($discussionid);
$discussion = $DB->get_record('forum_discussions', ['id' => $discussionid]);
if (!$vdiscussion) {
throw new \moodle_exception('Unable to find discussion with id ' . $discussionid);
}
$forumvault = $vaultfactory->get_forum_vault();
$vforum = $forumvault->get_from_id($vdiscussion->get_forum_id());
$forum = $DB->get_record('forum', ['id' => $vdiscussion->get_forum_id()]);
if (!$forum) {
throw new \moodle_exception('Unable to find forum with id ' . $vdiscussion->get_forum_id());
}
$cm = get_coursemodule_from_instance('forum', $forum->id, 0, false, MUST_EXIST);
if (!empty($replyto)) {
require_once($CFG->dirroot . '/local/edusupport/classes/post_form.php');
$thresholdwarning = forum_check_throttling($forum->id, $cm);
$mformpost = new \local_edusupport_post_form(
$CFG->wwwroot . '/local/edusupport/issue.php?d=' .
$discussionid . '&replyto=' . $replyto,
[
'course' => $course,
'cm' => $cm,
'coursecontext' => $coursecontext,
'modcontext' => $modcontext,
'forum' => $forum,
'post' => '',
'subscribe' => 0,
'thresholdwarning' => $thresholdwarning,
'edit' => $edit,
],
'post',
'',
['id' => 'mformforum']
);
$formheading = '';
if (!empty($parent)) {
$heading = get_string("yourreply", "forum");
$formheading = get_string('reply', 'forum');
} else {
if ($forum->type == 'qanda') {
$heading = get_string('yournewquestion', 'forum');
} else {
$heading = get_string('yournewtopic', 'forum');
}
}
$postid = empty($post->id) ? null : $post->id;
$draftitemid = \file_get_submitted_draft_itemid('attachments');
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
/* \file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', empty($post->id) ? null : $post->id,
\mod_forum_post_form::attachment_options($forum)); */
\file_prepare_draft_area(
$draftitemid,
$modcontext->id,
'mod_forum',
'attachment',
null,
\local_edusupport_post_form::attachment_options($forum)
);
$draftideditor = file_get_submitted_draft_itemid('message');
$currenttext = file_prepare_draft_area(
$draftideditor,
$modcontext->id,
'mod_forum',
'post',
$postid,
\local_edusupport_post_form::editor_options($modcontext, $postid),
$post->message
);
$mformpost->set_data(
[
'attachments' => $draftitemid,
'general' => $heading,
'subject' => 'Re: ' . $post->subject,
'message' => [
'text' => '',
'format' => editors_get_preferred_format(),
'itemid' => $draftideditor,
],
'discussionsubscribe' => 0,
'mailnow' => 1,
'userid' => $USER->id,
'parent' => $replyto,
'discussion' => $discussionid,
'course' => $course->id,
'forum' => $forum->id,
]
+ (isset($post->format) ? ['format' => $post->format] : [])
+ (isset($discussion->timestart) ? ['timestart' => $discussion->timestart] : [])
+ (isset($discussion->timeend) ? ['timeend' => $discussion->timeend] : [])
+ (isset($discussion->pinned) ? ['pinned' => $discussion->pinned] : [])
+ (isset($post->groupid) ? ['groupid' => $post->groupid] : [])
+ (isset($discussion->id) ? ['discussion' => $discussion->id] : [])
);
if ($mformpost->is_cancelled()) {
redirect('/local/edusupport/issue.php?d=' . $discussion->id);
} else if ($fromform = $mformpost->get_data()) {
$fromform->itemid = $fromform->message['itemid'];
$fromform->messageformat = $fromform->message['format'];
$fromform->message = $fromform->message['text'];
// WARNING: the $fromform->message array has been overwritten, do not use it anymore!
$fromform->messagetrust = trusttext_trusted($modcontext);
// Clean message text.
$fromform = trusttext_pre_edit($fromform, 'message', $modcontext);
if ($fromform->discussion) { // Adding a new post to an existing discussion
// Before we add this we must check that the user will not exceed the blocking threshold.
\forum_check_blocking_threshold($thresholdwarning);
unset($fromform->groupid);
$message = '';
$addpost = $fromform;
$addpost->forum = $forum->id;
if ($fromform->id = \forum_add_new_post($addpost, $mformpost)) {
$fromform->deleted = 0;
$subscribemessage = \forum_post_subscription($fromform, $forum, $discussion);
if (!empty($fromform->mailnow)) {
$message .= get_string("postmailnow", "forum");
} else {
$message .= '<p>' . get_string("postaddedsuccess", "forum") . '</p>';
$message .= '<p>' . get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
}
$discussionurl = $PAGE->url->__toString();
$params = [
'context' => $modcontext,
'objectid' => $fromform->id,
'other' => [
'discussionid' => $discussion->id,
'forumid' => $forum->id,
'forumtype' => $forum->type,
],
];
$event = \mod_forum\event\post_created::create($params);
$event->add_record_snapshot('forum_posts', $fromform);
$event->add_record_snapshot('forum_discussions', $discussion);
$event->trigger();
// Update completion state.
$completion = new \completion_info($course);
if (
$completion->is_enabled($cm) &&
($forum->completionreplies || $forum->completionposts)
) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}
$message = get_string("postaddedsuccess", "forum", fullname($USER));
$discussionurl = $CFG->wwwroot . '/local/edusupport/issue.php?d=' . $discussionid;
redirect(
$discussionurl,
$message,
null,
\core\output\notification::NOTIFY_SUCCESS
);
} else {
$errordestination = $CFG->wwwroot . '/local/edusupport/issue.php?d=' . $discussionid;
throw new moodle_exception("couldnotadd", "forum", $errordestination);
}
}
}
}
echo $OUTPUT->header();
$options = [];
if (!empty($issue->currentsupporter)) {
$supporter = $DB->get_record('local_edusupport_supporters', ['userid' => $issue->currentsupporter]);
$user = $DB->get_record('user', ['id' => $supporter->userid]);
$options[] = [
"title" => \fullname($user) . ' (' . (!empty($supporter->supportlevel) ? $supporter->supportlevel :
get_string('label:2ndlevel', 'local_edusupport')) . ')',
"class" => '',
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
/* "icon" => 'i/checkpermissions', */
"href" => $CFG->wwwroot . '/user/view.php?id' . $supporter->id,
];
}
$status = \local_edusupport\lib::status_to_template($issue->status);
$options[] = [
"title" => $status['status'],
"class" => $status['class'],
];
$options[] = [
"title" => get_string('issue_assign', 'local_edusupport'),
"class" => 'btn-secondary',
"icon" => 'i/assignroles',
"href" => '#',
"onclick" => "require(['local_edusupport/main'], function(MAIN){ MAIN.assignSupporter($discussionid); }); return false;",
];
$options[] = [
"title" => get_string('issue_close', 'local_edusupport'),
"class" => 'btn-primary',
"icon" => 't/approve',
"href" => '#',
"onclick" => "require(['local_edusupport/main'], function(MAIN){ MAIN.closeIssue($discussionid); }); return false;",
];
$changestatus = true;
$id = $issue->id;
echo $OUTPUT->render_from_template(
'local_edusupport/issue_options',
[
'options' => $options,
'changestatus' => $changestatus,
'id' => $id]
);
// We capture the output, as we need to modify links to attachments!
ob_start();
if (!empty($delete)) {
$deletepost = $DB->get_record('forum_posts', ['id' => $delete]);
if (!empty($deletepost->id) && $deletepost->userid == $USER->id) {
$vaultfactory = mod_forum\local\container::get_vault_factory();
$postvault = $vaultfactory->get_post_vault();
$postentity = $postvault->get_from_id($delete);
$managerfactory = mod_forum\local\container::get_manager_factory();
$legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
$forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
$postdatamapper = $legacydatamapperfactory->get_post_data_mapper();
forum_delete_post(
$postdatamapper->to_legacy_object($postentity),
true, // Capability.
$vforum->get_course_record(),
$vforum->get_course_module_record(),
$forumdatamapper->to_legacy_object($vforum)
);
echo $OUTPUT->render_from_template('local_edusupport/alert', [
'content' => get_string('deletedpost', 'mod_forum'),
'type' => 'success',
]);
}
}
$mode = optional_param('mode', 0, PARAM_INT); // If set, changes the layout of the thread.
$saveddisplaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
if ($mode) {
$displaymode = $mode;
} else {
$displaymode = $saveddisplaymode;
}
if (get_user_preferences('forum_useexperimentalui', false)) {
if ($displaymode == FORUM_MODE_NESTED) {
$displaymode = FORUM_MODE_NESTED_V2;
}
} else {
if ($displaymode == FORUM_MODE_NESTED_V2) {
$displaymode = FORUM_MODE_NESTED;
}
}
if ($displaymode != $saveddisplaymode) {
set_user_preference('forum_displaymode', $displaymode);
}
if ($parent) {
// If flat AND parent, then force nested display this time.
if ($displaymode == FORUM_MODE_FLATOLDEST || $displaymode == FORUM_MODE_FLATNEWEST) {
$displaymode = FORUM_MODE_NESTED;
}
} else {
$parent = $vdiscussion->get_first_post_id();
}
$postvault = $vaultfactory->get_post_vault();
if (!$vpost = $postvault->get_from_id($parent)) {
throw new moodle_exception("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f={$vforum->get_id()}");
}
$post = $DB->get_record('forum_posts', ['id' => $parent]);
$rendererfactory = \mod_forum\local\container::get_renderer_factory();
$discussionrenderer = $rendererfactory->get_discussion_renderer($vforum, $vdiscussion, $displaymode);
$orderpostsby = $displaymode == FORUM_MODE_FLATNEWEST ? 'created DESC' : 'created ASC';
$replies = $postvault->get_replies_to_post($USER, $vpost, true, $orderpostsby);
$postids = array_map(function ($vpost) {
return $vpost->get_id();
}, array_merge([$vpost], array_values($replies)));
// We use the first admin account for rendering the forum page.
$admins = explode(',', get_config('core', 'siteadmins'));
$user = $DB->get_record('user', ['id' => $admins[0]]);
echo $discussionrenderer->render($user, $vpost, $replies);
$PAGE->requires->js_call_amd("local_edusupport/main", "injectReplyButtons", [$discussionid]);
// Now catch the output from the renderer and modify some parts.
$out = ob_get_contents();
ob_end_clean();
$out = str_replace("class=\"discussion-settings-menu\"", "class=\"discussion-settings-menu\" style=\"display: none;\"", $out);
$out = str_replace("class=\"next-discussion\"", "class=\"next-discussion\" style=\"display: none;\"", $out);
$out = str_replace("class=\"prev-discussion\"", "class=\"prev-discussion\" style=\"display: none;\"", $out);
$out = str_replace(
$CFG->wwwroot . '/mod/forum/discuss.php',
$CFG->wwwroot . '/local/edusupport/issue.php',
$out
);
$out = str_replace(
$CFG->wwwroot . '/mod/forum/post.php?reply=',
$CFG->wwwroot . '/local/edusupport/issue.php?discussion=' . $discussionid . '&parent=',
$out
);
$out = str_replace(
$CFG->wwwroot . '/mod/forum/post.php?edit=',
$CFG->wwwroot . '/local/edusupport/editpost.php?discussion=' . $discussionid . '&edit=',
$out
);
$out = str_replace(
$CFG->wwwroot . '/mod/forum/post.php?delete=',
$CFG->wwwroot . '/local/edusupport/issue.php?discussion=' . $discussionid . '&delete=',
$out
);
$starts = [
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
/* '<div class="singleselect d-inline-block">',
'<div class="discussion-nav clearfix">', */
'<div class="commands">',
];
$ends = [
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
/* '</div>',
'</div>', */
'</div>',
];
for ($a = 0; $a < count($starts); $a++) {
if (empty($starts[$a] || empty($ends[$a]))) {
continue;
}
while (($cutstart = strpos($out, $starts[$a])) > 0) {
$cutend = strpos($out, $ends[$a], $cutstart);
$out1 = substr($out, 0, $cutstart);
$out2 = substr($out, $cutend + strlen($ends[$a]));
$out = $out1 . $out2;
}
}
// It gets saved at /mod_forum/ Don t know why this line was needed.
/*
$replacements = array(
array($CFG->wwwroot . '/pluginfile.php/' . $modcontext->id . '/mod_forum/',
$CFG->wwwroot . '/pluginfile.php/' . $modcontext->id . '/local_edusupport/'),
);
foreach ($replacements as $replacement) {
$out = str_replace($replacement[0], $replacement[1], $out);
}
*/
echo $out;
if (!empty($replyto)) {
$mformpost->display();
}
}
echo $OUTPUT->footer();