Issues (2128)

main/forum/viewthread.php (10 issues)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CourseBundle\Entity\CForumPost;
6
use ChamiloSession as Session;
7
8
/**
9
 * @author Julio Montoya <[email protected]> UI Improvements + lots of bugfixes
10
 */
11
require_once __DIR__.'/../inc/global.inc.php';
12
$current_course_tool = TOOL_FORUM;
13
14
$this_section = SECTION_COURSES;
15
16
// Notification for unauthorized people.
17
api_protect_course_script(true);
18
19
require_once 'forumfunction.inc.php';
20
21
$nameTools = get_lang('Forum');
22
$forumUrl = api_get_path(WEB_CODE_PATH).'forum/';
23
24
// Are we in a lp ?
25
$origin = api_get_origin();
26
$_user = api_get_user_info();
27
$my_search = null;
28
29
$forumId = isset($_GET['forum']) ? (int) $_GET['forum'] : 0;
30
$threadId = isset($_GET['thread']) ? (int) $_GET['thread'] : 0;
31
32
$current_thread = get_thread_information($forumId, $threadId);
33
$current_forum = get_forum_information($current_thread['forum_id']);
0 ignored issues
show
Deprecated Code introduced by
The function get_forum_information() has been deprecated: this functionality is now moved to get_forums($forum_id) ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

33
$current_forum = /** @scrutinizer ignore-deprecated */ get_forum_information($current_thread['forum_id']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
34
$current_forum_category = get_forumcategory_information($current_forum['forum_category']);
35
$whatsnew_post_info = isset($_SESSION['whatsnew_post_info']) ? $_SESSION['whatsnew_post_info'] : null;
36
37
if (api_is_in_gradebook()) {
38
    $interbreadcrumb[] = [
39
        'url' => Category::getUrl(),
40
        'name' => get_lang('ToolGradebook'),
41
    ];
42
}
43
44
$groupId = api_get_group_id();
45
$group_properties = GroupManager::get_group_properties($groupId);
46
$sessionId = api_get_session_id();
47
48
$ajaxURL = api_get_path(WEB_AJAX_PATH).'forum.ajax.php?'.api_get_cidreq().'&a=change_post_status';
49
$htmlHeadXtra[] = '<script>
50
$(function() {
51
    $("span").on("click", ".change_post_status", function() {
52
        var updateDiv = $(this).parent();
53
        var postId = updateDiv.attr("id");
54
55
        $.ajax({
56
            url: "'.$ajaxURL.'&post_id="+postId,
57
            type: "GET",
58
            success: function(data) {
59
                updateDiv.html(data);
60
            }
61
        });
62
    });
63
});
64
65
</script>';
66
67
$action = isset($_GET['action']) ? $_GET['action'] : '';
68
$logInfo = [
69
    'tool' => TOOL_FORUM,
70
    'tool_id' => $forumId,
71
    'tool_id_detail' => $threadId,
72
    'action' => !empty($action) ? $action : 'view-thread',
73
    'action_details' => isset($_GET['content']) ? $_GET['content'] : '',
74
];
75
Event::registerLog($logInfo);
0 ignored issues
show
The method registerLog() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
Event::/** @scrutinizer ignore-call */ 
76
       registerLog($logInfo);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
77
$currentUrl = api_get_path(WEB_CODE_PATH).'forum/viewthread.php?forum='.$forumId.'&'.api_get_cidreq().'&thread='.$threadId;
78
79
switch ($action) {
80
    case 'change_view':
81
        $view = isset($_REQUEST['view']) && in_array($_REQUEST['view'], ['nested', 'flat']) ? $_REQUEST['view'] : '';
82
        if (!empty($view)) {
83
            Session::write('thread_view', $view);
84
        }
85
        header('Location: '.$currentUrl);
86
        exit;
87
        break;
88
    case 'delete':
89
        if (
90
            isset($_GET['content']) &&
91
            isset($_GET['id']) &&
92
            (api_is_allowed_to_edit(false, true) ||
93
                (isset($group_properties['iid']) && GroupManager::is_tutor_of_group(api_get_user_id(), $group_properties)))
94
        ) {
95
            $message = delete_post($_GET['id']);
96
            Display::addFlash(Display::return_message(get_lang($message)));
97
        }
98
        header('Location: '.$currentUrl);
99
        exit;
100
        break;
101
    case 'invisible':
102
    case 'visible':
103
        if (isset($_GET['id']) &&
104
            (api_is_allowed_to_edit(false, true) ||
105
                (isset($group_properties['iid']) && GroupManager::is_tutor_of_group(api_get_user_id(), $group_properties)))
106
        ) {
107
            $message = approve_post($_GET['id'], $action);
108
            Display::addFlash(Display::return_message(get_lang($message)));
109
        }
110
        header('Location: '.$currentUrl);
111
        exit;
112
        break;
113
    case 'move':
114
        if (isset($_GET['post'])) {
115
            $message = move_post_form();
116
            Display::addFlash(Display::return_message(get_lang($message), 'normal', false));
117
        }
118
        header('Location: '.$currentUrl);
119
        exit;
120
        break;
121
    case 'report':
122
        $postId = isset($_GET['post_id']) ? $_GET['post_id'] : 0;
123
124
        $result = reportPost($postId, $current_forum, $current_thread);
125
        Display::addFlash(Display::return_message(get_lang('Reported')));
126
        header('Location: '.$currentUrl);
127
        exit;
128
        break;
129
    case 'ask_revision':
130
        if (api_get_configuration_value('allow_forum_post_revisions')) {
131
            $postId = isset($_GET['post_id']) ? $_GET['post_id'] : 0;
132
            $result = savePostRevision($postId);
133
            Display::addFlash(Display::return_message(get_lang('Saved')));
134
        }
135
        header('Location: '.$currentUrl);
136
        exit;
137
        break;
138
}
139
140
if (!empty($groupId)) {
141
    $interbreadcrumb[] = [
142
        'url' => api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq(),
143
        'name' => get_lang('Groups'),
144
    ];
145
    $interbreadcrumb[] = [
146
        'url' => api_get_path(WEB_CODE_PATH).'group/group_space.php?'.api_get_cidreq(),
147
        'name' => get_lang('GroupSpace').' '.$group_properties['name'],
148
    ];
149
    $interbreadcrumb[] = [
150
        'url' => api_get_path(WEB_CODE_PATH).'forum/viewforum.php?forum='.$forumId.'&'.api_get_cidreq()."&search=".Security::remove_XSS(urlencode($my_search)),
151
        'name' => Security::remove_XSS($current_forum['forum_title']),
152
    ];
153
    $interbreadcrumb[] = [
154
        'url' => api_get_path(WEB_CODE_PATH).'forum/viewthread.php?forum='.$forumId.'&'.api_get_cidreq().'&thread='.$threadId,
155
        'name' => Security::remove_XSS($current_thread['thread_title']),
156
    ];
157
} else {
158
    $my_search = isset($_GET['search']) ? $_GET['search'] : '';
159
    if ($origin !== 'learnpath') {
160
        $interbreadcrumb[] = [
161
            'url' => api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq().'&search='.Security::remove_XSS(
162
                    urlencode($my_search)
163
                ),
164
            'name' => $nameTools,
165
        ];
166
        $interbreadcrumb[] = [
167
            'url' => api_get_path(
168
                    WEB_CODE_PATH
169
                ).'forum/viewforumcategory.php?forumcategory='.$current_forum_category['cat_id']."&search=".Security::remove_XSS(
170
                    urlencode($my_search)
171
                ),
172
            'name' => Security::remove_XSS($current_forum_category['cat_title']),
173
        ];
174
        $interbreadcrumb[] = [
175
            'url' => api_get_path(WEB_CODE_PATH).'forum/viewforum.php?'.api_get_cidreq().'&forum='.$forumId."&search=".Security::remove_XSS(urlencode($my_search)),
176
            'name' => Security::remove_XSS($current_forum['forum_title']),
177
        ];
178
        $interbreadcrumb[] = [
179
            'url' => '#',
180
            'name' => Security::remove_XSS($current_thread['thread_title']),
181
        ];
182
    }
183
}
184
185
// If the user is not a course administrator and the forum is hidden
186
// then the user is not allowed here.
187
if (!api_is_allowed_to_edit(false, true) &&
188
    ($current_forum['visibility'] == 0 || $current_thread['visibility'] == 0)
189
) {
190
    api_not_allowed();
191
}
192
// this increases the number of times the thread has been viewed
193
increase_thread_view($threadId);
194
195
if ($origin === 'learnpath') {
196
    $template = new Template('', false, false, true, true, false);
197
} else {
198
    $template = new Template();
199
}
200
201
$actions = '<span style="float:right;">'.search_link().'</span>';
202
if ($origin !== 'learnpath') {
203
    $actions .= '<a href="'.$forumUrl.'viewforum.php?forum='.$forumId.'&'.api_get_cidreq().'">'
204
        .Display::return_icon('back.png', get_lang('BackToForum'), '', ICON_SIZE_MEDIUM).'</a>';
205
}
206
207
// The reply to thread link should only appear when the forum_category is
208
// not locked AND the forum is not locked AND the thread is not locked.
209
// If one of the three levels is locked then the link should not be displayed.
210
if (($current_forum_category &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $current_forum_category of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Consider adding parentheses for clarity. Current Interpretation: ($current_forum_category...ed_to_edit(false, true), Probably Intended Meaning: $current_forum_category ...d_to_edit(false, true))
Loading history...
211
    $current_forum_category['locked'] == 0) &&
212
    $current_forum['locked'] == 0 &&
213
    $current_thread['locked'] == 0 ||
214
    api_is_allowed_to_edit(false, true)
215
) {
216
    // The link should only appear when the user is logged in or when anonymous posts are allowed.
217
    if ($_user['user_id'] || ($current_forum['allow_anonymous'] == 1 && !$_user['user_id'])) {
218
        // reply link
219
        if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
220
            $actions .= '<a href="'.$forumUrl.'reply.php?'.api_get_cidreq().'&forum='.$forumId.'&thread='
221
                .$threadId.'&action=replythread">'
222
                .Display::return_icon('reply_thread.png', get_lang('ReplyToThread'), '', ICON_SIZE_MEDIUM)
223
                .'</a>';
224
        }
225
        // new thread link
226
        if ((
227
            api_is_allowed_to_edit(false, true) &&
228
            !(api_is_session_general_coach() && $current_forum['session_id'] != $sessionId)) ||
229
            ($current_forum['allow_new_threads'] == 1 && isset($_user['user_id'])) ||
230
            ($current_forum['allow_new_threads'] == 1 && !isset($_user['user_id']) && $current_forum['allow_anonymous'] == 1)
231
        ) {
232
            if ($current_forum['locked'] != 1 && $current_forum['locked'] != 1) {
233
                $actions .= '&nbsp;&nbsp;';
234
            } else {
235
                $actions .= get_lang('ForumLocked');
236
            }
237
        }
238
    }
239
}
240
241
$actions .= Display::url(
242
    Display::return_icon('forum_nestedview.png', get_lang('NestedView'), [], ICON_SIZE_MEDIUM),
243
    $currentUrl.'&action=change_view&view=nested'
244
);
245
246
$actions .= Display::url(
247
    Display::return_icon('forum_listview.png', get_lang('FlatView'), [], ICON_SIZE_MEDIUM),
248
    $currentUrl.'&action=change_view&view=flat'
249
);
250
251
$template->assign('forum_actions', $actions);
252
$template->assign('origin', api_get_origin());
253
254
$viewMode = $current_forum['default_view'];
255
256
//$whiteList = ['flat', 'threaded', 'nested'];
257
if ($viewMode !== 'flat') {
258
    $viewMode = 'nested';
259
}
260
261
$userView = Session::read('thread_view');
262
if (!empty($userView)) {
263
    $viewMode = $userView;
264
}
265
266
if ($current_thread['thread_peer_qualify'] == 1) {
267
    Display::addFlash(Display::return_message(get_lang('ForumThreadPeerScoringStudentComment'), 'info'));
268
}
269
270
$allowReport = reportAvailable();
271
272
// Are we in a lp ?
273
$origin = api_get_origin();
274
//delete attachment file
275
if ($action === 'delete_attach' && isset($_GET['id_attach'])
276
) {
277
    delete_attachment(0, $_GET['id_attach']);
278
}
279
280
$origin = api_get_origin();
281
$sessionId = api_get_session_id();
282
$_user = api_get_user_info();
283
$userId = api_get_user_id();
284
$groupId = api_get_group_id();
285
286
// Decide whether we show the latest post first
287
$sortDirection = isset($_GET['posts_order']) && $_GET['posts_order'] === 'desc' ? 'DESC' : ($origin !== 'learnpath' ? 'ASC' : 'DESC');
288
$posts = getPosts($current_forum, $threadId, $sortDirection, true);
289
$count = 0;
290
$group_id = api_get_group_id();
291
$locked = api_resource_is_locked_by_gradebook($threadId, LINK_FORUM_THREAD);
292
$sessionId = api_get_session_id();
293
$currentThread = get_thread_information($forumId, $threadId);
294
$userId = api_get_user_id();
295
$groupInfo = GroupManager::get_group_properties($group_id);
296
$postCount = 1;
297
$allowUserImageForum = api_get_course_setting('allow_user_image_forum');
298
299
// The user who posted it can edit his thread only if the course admin allowed this in the properties
300
// of the forum
301
// The course admin him/herself can do this off course always
302
$tutorGroup = GroupManager::is_tutor_of_group(api_get_user_id(), $groupInfo);
303
304
$postList = [];
305
foreach ($posts as $post) {
306
    $posterId = isset($post['user_id']) ? $post['user_id'] : 0;
307
    $username = '';
308
    if (isset($post['username'])) {
309
        $username = sprintf(get_lang('LoginX'), $post['username']);
310
    }
311
312
    $name = $post['complete_name'];
313
    if (empty($posterId)) {
314
        $name = $post['poster_name'];
315
    }
316
317
    $post['user_data'] = '';
318
    if ($origin !== 'learnpath') {
319
        if ($allowUserImageForum) {
320
            $post['user_data'] = '<div class="thumbnail">'.
321
                display_user_image($posterId, $name, $origin).'</div>';
322
        }
323
324
        $post['user_data'] .= Display::tag(
325
            'h4',
326
            display_user_link($posterId, $name, $origin, $username),
327
            ['class' => 'title-username']
328
        );
329
330
        $_user = api_get_user_info($posterId);
331
        $iconStatus = $_user['icon_status'];
332
        $post['user_data'] .= '<div class="user-type text-center">'.$iconStatus.'</div>';
333
    } else {
334
        if ($allowUserImageForum) {
335
            $post['user_data'] .= '<div class="thumbnail">'.
336
                display_user_image($posterId, $name, $origin).'</div>';
337
        }
338
339
        $post['user_data'] .= Display::tag(
340
            'p',
341
            $name,
342
            [
343
                'title' => api_htmlentities($username, ENT_QUOTES),
344
                'class' => 'lead',
345
            ]
346
        );
347
    }
348
349
    if ($origin !== 'learnpath') {
350
        $post['user_data'] .= Display::tag(
351
            'p',
352
            Display::dateToStringAgoAndLongDate($post['post_date']),
353
            ['class' => 'post-date']
354
        );
355
    } else {
356
        $post['user_data'] .= Display::tag(
357
            'p',
358
            Display::dateToStringAgoAndLongDate($post['post_date']),
359
            ['class' => 'text-muted']
360
        );
361
    }
362
363
    // get attach id
364
    $attachment_list = get_attachment($post['post_id']);
365
    $id_attach = !empty($attachment_list) ? $attachment_list['iid'] : '';
366
367
    $iconEdit = '';
368
    $editButton = '';
369
    $askForRevision = '';
370
    if ((isset($groupInfo['iid']) && $tutorGroup) ||
371
        ($current_forum['allow_edit'] == 1 && $posterId == $userId) ||
372
        (api_is_allowed_to_edit(false, true) &&
373
        !(api_is_session_general_coach() && $current_forum['session_id'] != $sessionId))
374
    ) {
375
        if ($locked == false && postIsEditableByStudent($current_forum, $post)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
376
            $editUrl = api_get_path(WEB_CODE_PATH).'forum/editpost.php?'.api_get_cidreq();
377
            $editUrl .= "&forum=$forumId&thread=$threadId&post={$post['post_id']}&id_attach=$id_attach";
378
            $iconEdit .= "<a href='".$editUrl."'>"
379
                .Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL)
380
                ."</a>";
381
382
            $editButton = Display::toolbarButton(
383
                get_lang('Edit'),
384
                $editUrl,
385
                'pencil',
386
                'default'
387
            );
388
        }
389
    }
390
391
    if ((isset($groupInfo['iid']) && $tutorGroup) ||
392
        api_is_allowed_to_edit(false, true) &&
393
        !(api_is_session_general_coach() && $current_forum['session_id'] != $sessionId)
394
    ) {
395
        if ($locked == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
396
            $deleteUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query(
397
                [
398
                    'forum' => $forumId,
399
                    'thread' => $threadId,
400
                    'action' => 'delete',
401
                    'content' => 'post',
402
                    'id' => $post['post_id'],
403
                ]
404
            );
405
            $iconEdit .= Display::url(
406
                Display::return_icon('delete.png', get_lang('Delete'), [], ICON_SIZE_SMALL),
407
                $deleteUrl,
408
                [
409
                    'onclick' => "javascript:if(!confirm('"
410
                        .addslashes(api_htmlentities(get_lang('DeletePost'), ENT_QUOTES))
411
                        ."')) return false;",
412
                    'id' => "delete-post-{$post['post_id']}",
413
                ]
414
            );
415
        }
416
    }
417
418
    if (api_is_allowed_to_edit(false, true) &&
419
        !(
420
            api_is_session_general_coach() &&
421
            $current_forum['session_id'] != $sessionId
422
        )
423
    ) {
424
        $iconEdit .= return_visible_invisible_icon(
425
            'post',
426
            $post['post_id'],
427
            $post['visible'],
428
            [
429
                'forum' => $forumId,
430
                'thread' => $threadId,
431
            ]
432
        );
433
434
        if ($count > 0) {
435
            $iconEdit .= "<a href=\"viewthread.php?".api_get_cidreq()
436
                ."&forum=$forumId&thread=$threadId&action=move&post={$post['post_id']}"
437
                ."\">".Display::return_icon('move.png', get_lang('MovePost'), [], ICON_SIZE_SMALL)."</a>";
438
        }
439
    }
440
441
    $userCanQualify = $currentThread['thread_peer_qualify'] == 1 && $post['poster_id'] != $userId;
442
    if (api_is_allowed_to_edit(null, true)) {
443
        $userCanQualify = true;
444
    }
445
446
    $postIsARevision = false;
447
    $flagRevision = '';
448
449
    if ($post['poster_id'] == $userId) {
450
        $revision = getPostRevision($post['post_id']);
451
        if (empty($revision)) {
452
            $askForRevision = getAskRevisionButton($post['post_id'], $current_thread);
453
        } else {
454
            $postIsARevision = true;
455
            $languageId = api_get_language_id(strtolower($revision));
456
            $languageInfo = api_get_language_info($languageId);
457
            if ($languageInfo) {
458
                $languages = api_get_language_list_for_flag();
459
                $flagRevision = '<span class="flag-icon flag-icon-'.$languages[$languageInfo['english_name']].'"></span> ';
460
            }
461
        }
462
    } else {
463
        if (postNeedsRevision($post['post_id'])) {
464
            $askForRevision = giveRevisionButton($post['post_id'], $current_thread);
465
        } else {
466
            $revision = getPostRevision($post['post_id']);
467
            if (!empty($revision)) {
468
                $postIsARevision = true;
469
                $languageId = api_get_language_id(strtolower($revision));
470
                $languageInfo = api_get_language_info($languageId);
471
                if ($languageInfo) {
472
                    $languages = api_get_language_list_for_flag();
473
                    $flagRevision = '<span
474
                        class="flag-icon flag-icon-'.$languages[$languageInfo['english_name']].'"></span> ';
475
                }
476
            }
477
        }
478
    }
479
480
    $post['is_a_revision'] = $postIsARevision;
481
    $post['flag_revision'] = $flagRevision;
482
483
    if (empty($currentThread['thread_qualify_max'])) {
484
        $userCanQualify = false;
485
    }
486
487
    if ($userCanQualify) {
488
        if ($count > 0) {
489
            $current_qualify_thread = showQualify(
490
                '1',
491
                $posterId,
492
                $threadId
493
            );
494
            if ($locked == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
495
                $iconEdit .= "<a href=\"forumqualify.php?".api_get_cidreq()
496
                    ."&forum=$forumId&thread=$threadId&action=list&post={$post['post_id']}"
497
                    ."&user={$post['user_id']}&user_id={$post['user_id']}"
498
                    ."&idtextqualify=$current_qualify_thread"
499
                    ."\" >".Display::return_icon('quiz.png', get_lang('Qualify'))."</a>";
500
            }
501
        }
502
    }
503
504
    $reportButton = '';
505
    if ($allowReport) {
506
        $reportButton = getReportButton($post['post_id'], $current_thread);
507
    }
508
509
    $statusIcon = getPostStatus($current_forum, $post);
510
    if (!empty($iconEdit)) {
511
        $post['user_data'] .= "<div class='tools-icons'> $iconEdit $statusIcon </div>";
512
    } else {
513
        if (!empty(strip_tags($statusIcon))) {
514
            $post['user_data'] .= "<div class='tools-icons'> $statusIcon </div>";
515
        }
516
    }
517
518
    $buttonReply = '';
519
    $buttonQuote = '';
520
    $waitingValidation = '';
521
522
    if (($current_forum_category && $current_forum_category['locked'] == 0) &&
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: ($current_forum_category...ed_to_edit(false, true), Probably Intended Meaning: $current_forum_category ...d_to_edit(false, true))
Loading history...
Bug Best Practice introduced by
The expression $current_forum_category of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
523
        $current_forum['locked'] == 0 && $current_thread['locked'] == 0 || api_is_allowed_to_edit(false, true)
524
    ) {
525
        if ($userId || ($current_forum['allow_anonymous'] == 1 && !$userId)) {
526
            if (!api_is_anonymous() && api_is_allowed_to_session_edit(false, true)) {
527
                $buttonReply = Display::toolbarButton(
528
                    get_lang('ReplyToMessage'),
529
                    'reply.php?'.api_get_cidreq().'&'.http_build_query([
530
                        'forum' => $forumId,
531
                        'thread' => $threadId,
532
                        'post' => $post['post_id'],
533
                        'action' => 'replymessage',
534
                    ]),
535
                    'reply',
536
                    'primary',
537
                    ['id' => "reply-to-post-{$post['post_id']}"]
538
                );
539
540
                $buttonQuote = Display::toolbarButton(
541
                    get_lang('QuoteMessage'),
542
                    'reply.php?'.api_get_cidreq().'&'.http_build_query([
543
                        'forum' => $forumId,
544
                        'thread' => $threadId,
545
                        'post' => $post['post_id'],
546
                        'action' => 'quote',
547
                    ]),
548
                    'quote-left',
549
                    'success',
550
                    ['id' => "quote-post-{$post['post_id']}"]
551
                );
552
553
                if ($current_forum['moderated'] && !api_is_allowed_to_edit(false, true)) {
554
                    if (empty($post['status']) || $post['status'] == CForumPost::STATUS_WAITING_MODERATION) {
555
                        $buttonReply = '';
556
                        $buttonQuote = '';
557
                    }
558
                }
559
            }
560
        }
561
    } else {
562
        $closedPost = '';
563
        if ($current_forum_category && $current_forum_category['locked'] == 1) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $current_forum_category of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
564
            $closedPost = Display::tag(
565
                'div',
566
                '<em class="fa fa-exclamation-triangle"></em> '.get_lang('ForumcategoryLocked'),
567
                ['class' => 'alert alert-warning post-closed']
568
            );
569
        }
570
        if ($current_forum['locked'] == 1) {
571
            $closedPost = Display::tag(
572
                'div',
573
                '<em class="fa fa-exclamation-triangle"></em> '.get_lang('ForumLocked'),
574
                ['class' => 'alert alert-warning post-closed']
575
            );
576
        }
577
        if ($current_thread['locked'] == 1) {
578
            $closedPost = Display::tag(
579
                'div',
580
                '<em class="fa fa-exclamation-triangle"></em> '.get_lang('ThreadLocked'),
581
                ['class' => 'alert alert-warning post-closed']
582
            );
583
        }
584
585
        $post['user_data'] .= $closedPost;
586
    }
587
588
    // note: this can be removed here because it will be displayed in the tree
589
    if (isset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$post['post_id']]) &&
590
        !empty($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$post['post_id']]) &&
591
        !empty($whatsnew_post_info[$forumId][$post['thread_id']])
592
    ) {
593
        $post_image = Display::return_icon('forumpostnew.gif');
594
    } else {
595
        $post_image = Display::return_icon('forumpost.gif');
596
    }
597
598
    if ($post['post_notification'] == '1' && $post['poster_id'] == $userId) {
599
        $post_image .= Display::return_icon(
600
            'forumnotification.gif',
601
            get_lang('YouWillBeNotified')
602
        );
603
    }
604
605
    $post['current'] = false;
606
    if (isset($_GET['post_id']) && $_GET['post_id'] == $post['post_id']) {
607
        $post['current'] = true;
608
    }
609
610
    // Replace Re: with an icon
611
    $search = [
612
        get_lang('ReplyShort'),
613
        'Re:',
614
        'RE:',
615
        'AW:',
616
        'Aw:',
617
    ];
618
    $replace = '<span>'.Display::returnFontAwesomeIcon('mail-reply').'</span>';
619
    $post['post_title'] = str_replace($search, $replace, Security::remove_XSS($post['post_title']));
620
621
    // The post title
622
    $titlePost = Display::tag('h3', $post['post_title'], ['class' => 'forum_post_title']);
623
    $post['post_title'] = '<a name="post_id_'.$post['post_id'].'"></a>';
624
    $post['post_title'] .= Display::tag('div', $titlePost, ['class' => 'post-header']);
625
626
    // the post body
627
    $post['post_text'] = Security::remove_XSS($post['post_text']);
628
    $post['post_data'] = Display::tag('div', $post['post_text'], ['class' => 'post-body']);
629
630
    // The check if there is an attachment
631
    $post['post_attachments'] = '';
632
    $attachment_list = getAllAttachment($post['post_id']);
633
    if (!empty($attachment_list) && is_array($attachment_list)) {
634
        foreach ($attachment_list as $attachment) {
635
            $user_filename = $attachment['filename'];
636
            $post['post_attachments'] .= Display::return_icon('attachment.gif', get_lang('Attachment'));
637
            $post['post_attachments'] .= '<a href="download.php?file=';
638
            $post['post_attachments'] .= $attachment['path'];
639
            $post['post_attachments'] .= ' "> '.$user_filename.' </a>';
640
            $post['post_attachments'] .= '<span class="forum_attach_comment" >'.$attachment['comment'].'</span>';
641
            if (($current_forum['allow_edit'] == 1 && $post['user_id'] == $userId) ||
642
                (api_is_allowed_to_edit(false, true) && !(api_is_session_general_coach() && $current_forum['session_id'] != $sessionId))
643
            ) {
644
                $post['post_attachments'] .= '&nbsp;&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&action=delete_attach&id_attach='
645
                    .$attachment['iid'].'&forum='.$forumId.'&thread='.$threadId
646
                    .'" onclick="javascript:if(!confirm(\''
647
                    .addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)).'\')) return false;">'
648
                    .Display::return_icon('delete.png', get_lang('Delete')).'</a><br />';
649
            }
650
        }
651
    }
652
653
    $post['post_buttons'] = "$askForRevision $editButton $reportButton $buttonReply $buttonQuote $waitingValidation";
654
    $postList[] = $post;
655
656
    // The post has been displayed => it can be removed from the what's new array
657
    unset($whatsnew_post_info[$current_forum['forum_id']][$current_thread['thread_id']][$post['post_id']]);
658
    unset($_SESSION['whatsnew_post_info'][$current_forum['forum_id']][$current_thread['thread_id']][$post['post_id']]);
659
    $count++;
660
}
661
662
$template->assign('posts', $postList);
663
664
$formToString = '';
665
$showForm = true;
666
if (!api_is_allowed_to_edit(false, true) &&
667
    (($current_forum_category && $current_forum_category['visibility'] == 0) || $current_forum['visibility'] == 0)
668
) {
669
    $showForm = false;
670
}
671
672
if (!api_is_allowed_to_session_edit(false, true) ||
673
    (
674
        ($current_forum_category && $current_forum_category['locked'] != 0) ||
675
            $current_forum['locked'] != 0 || $current_thread['locked'] != 0
676
    )
677
) {
678
    $showForm = false;
679
}
680
681
if (!$_user['user_id'] && $current_forum['allow_anonymous'] == 0) {
682
    $showForm = false;
683
}
684
685
if ($current_forum['forum_of_group'] != 0) {
686
    $show_forum = GroupManager::user_has_access(
687
        api_get_user_id(),
688
        $current_forum['forum_of_group'],
689
        GroupManager::GROUP_TOOL_FORUM
690
    );
691
    if (!$show_forum) {
692
        $showForm = false;
693
    }
694
}
695
696
if ($showForm) {
697
    $values = [
698
        'post_title' => Security::remove_XSS($current_thread['thread_title']),
699
        'post_text' => '',
700
        'post_notification' => '',
701
        'thread_sticky' => '',
702
        'thread_peer_qualify' => '',
703
    ];
704
    $form = show_add_post_form(
705
        $current_forum,
706
        'replythread',
707
        $values,
708
        false
709
    );
710
    $formToString = $form->returnForm();
711
}
712
713
$template->assign('form', $formToString);
714
$template->assign('view_mode', $viewMode);
715
$template->display($template->get_template('forum/posts.tpl'));
716