Passed
Push — master ( aa9c38...5241a6 )
by Vitalii
01:23 queued 21s
created

forum_page()   F

Complexity

Conditions 17
Paths 2016

Size

Total Lines 122
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 75
nc 2016
nop 3
dl 0
loc 122
rs 1.0499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2019 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
// forum page: display 'New thread' button and list of threads
20
21
require_once('../inc/util.inc');
22
require_once('../inc/time.inc');
23
require_once('../inc/forum.inc');
24
require_once('../inc/pm.inc');
25
26
function forum_page($forum, $user, $msg=null) {
27
    global $forum_sort_styles;
28
    $sort_style = get_int("sort", true);
29
    $start = get_int("start", true);
30
    if (!$start) $start = 0;
31
    $subs = null;
32
    if ($user) {
33
        if (DISABLE_FORUMS && !is_admin($user)) {
34
            error_page("Forums are disabled");
35
        }
36
        BoincForumPrefs::lookup($user);
37
        $subs = BoincSubscription::enum("userid=$user->id");
38
    }
39
40
    if (!is_forum_visible_to_user($forum, $user)) {
41
        if ($user) {
42
            remove_subscriptions_forum($user->id, $forum->id);
43
        }
44
        error_page(tra("Not visible to you"));
45
    }
46
47
    if (!$sort_style) {
48
        // get the sort style either from the logged in user or a cookie
49
        if ($user){
50
            $sort_style = $user->prefs->forum_sorting;
51
        } else {
52
            list($sort_style, $thread_style) = parse_forum_cookie();
53
        }
54
    } else {
55
        // set the sort style
56
        if ($user){
57
            $user->prefs->forum_sorting = $sort_style;
58
            $user->prefs->update("forum_sorting=$sort_style");
59
        } else {
60
            list($old_style, $thread_style) = parse_forum_cookie();
61
            send_cookie(
62
                'sorting', implode("|", array($sort_style, $thread_style)), true
63
            );
64
        }
65
    }
66
67
    switch ($forum->parent_type) {
68
    case 0:
69
        $category = BoincCategory::lookup_id($forum->category);
70
        if ($category->is_helpdesk) {
71
            page_head(tra("Questions and Answers").' : '.$forum->title);
72
        } else {
73
            page_head(tra("Message boards").' : '.$forum->title);
74
        }
75
        if ($msg) echo "<p>$msg</p>\n";
76
        show_forum_header($user);
77
        show_forum_title($category, $forum, NULL);
78
        break;
79
    case 1:
80
        $team = BoincTeam::lookup_id($forum->category);
81
        page_head(tra("Team message board for %1", $team->name));
82
        if ($msg) echo "<p>$msg</p>\n";
83
        show_forum_header($user);
84
        show_team_forum_title($forum);
85
        break;
86
    }
87
88
    echo '
89
        <p>
90
        <form action="forum_forum.php" method="get" class="form-inline">
91
        <input type="hidden" name="id" value="'.$forum->id.'">
92
        <table width="100%" cellspacing="0" cellpadding="0">
93
        <tr valign="top">
94
        <td colspan=2>
95
    ';
96
97
    if (user_can_create_thread($user, $forum)) {
98
        show_button(
99
            "forum_post.php?id=$forum->id",
100
            tra("New thread"),
101
            tra("Add a new thread to this forum")
102
        );
103
    }
104
105
    if (is_subscribed(-$forum->id, $subs)) {
106
        BoincNotify::delete_aux(sprintf(
107
            'userid=%d and type=%d and opaque=%d',
108
            $logged_in_user->id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $logged_in_user seems to be never defined.
Loading history...
109
            NOTIFY_SUBSCRIBED_FORUM,
110
            $forum->id
111
        ));
112
        show_button_small(
113
            "forum_forum.php?id=$forum->id&action=unsubscribe",
114
            'Unsubscribe',
115
            'Unsubscribe from this forum'
116
        );
117
    } else {
118
        show_button_small(
119
            "forum_forum.php?id=$forum->id&action=subscribe",
120
            'Subscribe',
121
            'Click to get notified when there are new threads in this forum'
122
        );
123
    }
124
125
    echo '</td>
126
        <td valign=top align="right">
127
        <div class="form-group">
128
    ';
129
    echo select_from_array("sort", $forum_sort_styles, $sort_style);
130
    echo sprintf('
131
        <input class="btn btn-sm" %s type="submit" value="Sort">
132
        </div>
133
        </td>
134
        </tr>
135
        </table>
136
        </form>
137
        <p></p> ',
138
        button_style()
139
    );
140
141
    show_forum_threads($forum, $start, $sort_style, $user, $subs);
142
143
    echo "
144
        <p>".
145
        tra("This message board is available as an %1 RSS feed %2", "<a href=forum_rss.php?forumid=$forum->id&setup=1>", "<img src=img/feed_logo.png></a>");
146
147
    page_tail();
148
}
149
150
// Show the threads for the given forum
151
// starting from $start,
152
// using the given $sort_style (as defined in forum.php)
153
// and using the features for the logged in user in $user.
154
//
155
function show_forum_threads($forum, $start, $sort_style, $user, $subs) {
156
    $page_nav = page_links(
157
        "forum_forum.php?id=$forum->id&amp;sort=$sort_style",
158
        $forum->threads,
159
        THREADS_PER_PAGE,
160
        $start
161
    );
162
    echo $page_nav;
163
    start_table('table-striped');
164
    row_heading_array(
165
        array(
166
            "",
167
            tra("Threads"),
168
            tra("Posts"),
169
            tra("Author"),
170
            tra("Views"),
171
            "<nobr>".tra("Last post")."</nobr>"
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
172
        ),
173
        array("", "width=35%", "", "", "", "")
174
175
    );
176
177
    $sticky_first = !$user || !$user->prefs->ignore_sticky_posts;
178
179
    // Show hidden threads if logged in user is a moderator
180
    //
181
    $show_hidden = is_moderator($user, $forum);
182
    $threads = get_forum_threads(
183
        $forum->id, $start, THREADS_PER_PAGE,
184
        $sort_style, $show_hidden, $sticky_first
185
    );
186
187
    // Run through the list of threads, displaying each of them
188
    //
189
    foreach ($threads as $thread) {
190
        $owner = BoincUser::lookup_id($thread->owner);
191
        if (!$owner) continue;
192
        $unread = thread_is_unread($user, $thread);
193
194
        //if ($thread->status==1){
195
            // This is an answered helpdesk thread
196
        if ($user && is_subscribed($thread->id, $subs)) {
197
            echo '<tr class="bg-info">';
198
        } else {
199
            // Just a standard thread.
200
            echo '<tr>';
201
        }
202
203
        echo "<td width=\"1%\"><nobr>";
204
        if ($thread->hidden) {
205
            show_image(IMAGE_HIDDEN, tra("This thread is hidden"), tra("hidden"));
206
        } else if ($unread) {
207
            if ($thread->sticky) {
208
                if ($thread->locked) {
209
                    show_image(NEW_IMAGE_STICKY_LOCKED, tra("This thread is sticky and locked, and you haven't read it yet"), tra("sticky/locked/unread"));
210
                } else {
211
                    show_image(NEW_IMAGE_STICKY, tra("This thread is sticky and you haven't read it yet"), tra("sticky/unread"));
212
                }
213
            } else {
214
                if ($thread->locked) {
215
                    show_image(NEW_IMAGE_LOCKED, tra("You haven't read this thread yet, and it's locked"), tra("unread/locked"));
216
                } else {
217
                    show_image(NEW_IMAGE, tra("You haven't read this thread yet"), tra("unread"));
218
                }
219
            }
220
        } else {
221
            if ($thread->sticky) {
222
                if ($thread->locked) {
223
                    show_image(IMAGE_STICKY_LOCKED, tra("This thread is sticky and locked"), tra("sticky/locked"));
224
                } else {
225
                    show_image(IMAGE_STICKY, tra("This thread is sticky"), tra("sticky"));
226
                }
227
            } else {
228
                if ($thread->locked) {
229
                    show_image(IMAGE_LOCKED, tra("This thread is locked"), tra("locked"));
230
                } else {
231
                    show_image(IMAGE_POST, tra("You read this thread"), tra("read"));
232
                }
233
            }
234
        }
235
        echo "</nobr></td>";
236
237
        $title = cleanup_title($thread->title);
238
        //$titlelength = 9999;
239
        //if (strlen($title) > $titlelength) {
240
        //    $title = substr($title, 0, $titlelength)."...";
241
        //}
242
        echo "<td><a href=\"forum_thread.php?id=$thread->id\">$title</a><br></td>";
243
244
        echo '
245
            <td>'.($thread->replies+1).'</td>
246
            <td>'.user_links($owner, BADGE_HEIGHT_SMALL).'</td>
247
            <td>'.$thread->views.'</td>
248
            <td>'.time_diff_str($thread->timestamp, time()).'</td>
249
            </tr>
250
        ';
251
        flush();
252
    }
253
    end_table();
254
    echo "<br>$page_nav";    // show page links
255
}
256
257
$id = get_int("id");
258
$forum = BoincForum::lookup_id($id);
259
if (!$forum) error_page("forum ID not found");
260
$user = get_logged_in_user(false);
261
$action = get_str('action', true);
262
263
if ($action == 'subscribe') {
264
    BoincSubscription::replace($user->id, -$id);
265
    forum_page($forum, $user, 'You are now subscribed to this forum.');
266
} else if ($action == 'unsubscribe') {
267
    BoincSubscription::delete($user->id, -$id);
268
    forum_page($forum, $user, 'You are now unsubscribed from this forum.');
269
} else {
270
    forum_page($forum, $user);
271
}
272
273
?>
274