Passed
Push — master ( 81ba93...c6c854 )
by Michael
03:30
created

editfaq()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 116
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
c 1
b 0
f 0
dl 0
loc 116
rs 6
cc 10
nc 192
nop 1

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
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
use XoopsModules\Smartfaq;
10
use XoopsModules\Smartfaq\Constants;
11
12
require_once __DIR__ . '/admin_header.php';
13
14
/** @var Smartfaq\Helper $helper */
15
$helper = Smartfaq\Helper::getInstance();
16
17
$op = '';
18
19
// Getting the operation we are doing
20
if (\Xmf\Request::hasVar('op', 'GET')) {
21
    $op = $_GET['op'];
22
}
23
if (\Xmf\Request::hasVar('op', 'POST')) {
24
    $op = $_POST['op'];
25
}
26
27
// Creating the answer handler object
28
/** @var \XoopsModules\Smartfaq\AnswerHandler $answerHandler */
29
$answerHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Answer');
30
31
/**
32
 * @param string $faqid
33
 */
34
function editfaq($faqid = '')
35
{
36
    global $answerHandler, $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $modify, $xoopsModule, $XOOPS_URL, $myts, $pathIcon16, $smartModuleConfig;
37
    /** @var Smartfaq\Helper $helper */
38
    $helper = Smartfaq\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
39
40
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
41
42
    // Creating the FAQ object
43
    $faqObj = new Smartfaq\Faq($faqid);
44
45
    // Creating the category object
46
    $categoryObj = $faqObj->category();
47
48
    if ($faqObj->notLoaded()) {
49
        redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
50
    }
51
52
    switch ($faqObj->status()) {
53
        case Constants::SF_STATUS_ANSWERED:
54
            $breadcrumb_action1   = _AM_SF_SUBMITTED;
0 ignored issues
show
Unused Code introduced by
The assignment to $breadcrumb_action1 is dead and can be removed.
Loading history...
55
            $breadcrumb_action2   = _AM_SF_APPROVING;
0 ignored issues
show
Unused Code introduced by
The assignment to $breadcrumb_action2 is dead and can be removed.
Loading history...
56
            $collapsableBar_title = _AM_SF_SUBMITTED_TITLE;
0 ignored issues
show
Unused Code introduced by
The assignment to $collapsableBar_title is dead and can be removed.
Loading history...
57
            $collapsableBar_info  = _AM_SF_SUBMITTED_INFO;
0 ignored issues
show
Unused Code introduced by
The assignment to $collapsableBar_info is dead and can be removed.
Loading history...
58
            $button_caption       = _AM_SF_APPROVE;
0 ignored issues
show
Unused Code introduced by
The assignment to $button_caption is dead and can be removed.
Loading history...
59
            $an_status            = Constants::SF_AN_STATUS_PROPOSED;
0 ignored issues
show
Unused Code introduced by
The assignment to $an_status is dead and can be removed.
Loading history...
60
            break;
61
    }
62
63
    $module_id        = $xoopsModule->getVar('mid');
64
    $grouppermHandler = xoops_getHandler('groupperm');
65
    $groups           = $xoopsUser ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
66
67
    if (!Smartfaq\Utility::userIsAdmin()
68
        && (!$grouppermHandler->checkRight('category_admin', $faqObj->categoryid(), $groups, $module_id))) {
0 ignored issues
show
Bug introduced by
The method checkRight() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsGroupPermHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

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

68
        && (!$grouppermHandler->/** @scrutinizer ignore-call */ checkRight('category_admin', $faqObj->categoryid(), $groups, $module_id))) {
Loading history...
69
        redirect_header('<script>javascript:history.go(-1)</script>', 1, _NOPERM);
70
    }
71
    // Retreiving the official answer
72
    $official_answer = $faqObj->answer();
73
74
    Smartfaq\Utility::collapsableBar('bottomtable', 'bottomtableicon');
75
    echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt=''></a>&nbsp;" . _AM_SF_SUBMITTED_ANSWER . '</h3>';
76
    echo "<div id='bottomtable'>";
77
    echo '<span style="color: #567; margin: 3px 0 12px 0; font-size: small; display: block; ">' . _AM_SF_SUBMITTED_ANSWER_INFO . '</span>';
78
79
    $proposed_answers = $answerHandler->getAllAnswers($faqid, Constants::SF_AN_STATUS_PROPOSED);
80
81
    if (0 == count($proposed_answers)) {
82
        redirect_header('index.php', 1, _AM_SF_NOANSWERS);
83
    }
84
85
    echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>
86
            <tr>
87
              <td class='head' width='100px'>" . _AM_SF_CATEGORY . "</td>
88
              <td class='even'>" . $categoryObj->name() . "</td>
89
            </tr>
90
            <tr>
91
              <td class='head' width='100px'>" . _AM_SF_QUESTION . "</td>
92
              <td class='even'>" . $faqObj->question() . '</td>
93
            </tr>';
94
    if ($official_answer) {
0 ignored issues
show
introduced by
$official_answer is of type XoopsModules\Smartfaq\Answer, thus it always evaluated to true.
Loading history...
95
        echo "
96
            <tr>
97
              <td class='head' width='100px'>" . _AM_SF_ANSWER_OFFICIAL . "</td>
98
              <td class='even'>" . $official_answer->answer() . '</td>
99
            </tr>';
100
    }
101
    echo "</table><br>\n";
102
103
    echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>";
104
    echo '<tr>';
105
    echo "<th width='40' class='bg3' align='center'><b>" . _AM_SF_ARTID . '</b></td>';
106
    echo "<th class='bg3' class='bg3' align='center'><b>" . _AM_SF_ANSWER . '</b></td>';
107
108
    echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_ANSWERED . '</b></td>';
109
110
    echo "<th width='180' class='bg3' align='center'><b>" . _AM_SF_CREATED . '</b></td>';
111
    echo "<th width='120' class='bg3' align='center'><b>" . _AM_SF_ACTION . '</b></td>';
112
    echo '</tr>';
113
114
    $merge   = '';
115
    $modify  = '';
116
    $approve = '';
117
    foreach ($proposed_answers as $proposed_answer) {
118
        if (Constants::SF_STATUS_NEW_ANSWER == $faqObj->status()) {
119
            $merge   = "<a href='faq.php?op=merge&amp;faqid="
120
                       . $faqObj->faqid()
121
                       . '&amp;answerid='
122
                       . $proposed_answer->answerid()
123
                       . "'><img src='"
124
                       . XOOPS_URL
125
                       . '/modules/'
126
                       . $xoopsModule->dirname()
127
                       . "/assets/images/icon/merge.gif' title='"
128
                       . _AM_SF_FAQ_MERGE
129
                       . "' alt='"
130
                       . _AM_SF_FAQ_MERGE
131
                       . "'></a>&nbsp;";
132
            $approve = "<a href='answer.php?op=selectanswer&amp;faqid=" . $faqid . '&amp;answerid=' . $proposed_answer->answerid() . "'><img src='" . $pathIcon16 . '/on.png' . "' title='" . _AM_SF_FAQ_APPROVE_NEW_ANSWER . "' alt='" . _AM_SF_APPROVESUB . "'></a>";
133
        }
134
        $modify = "<a href='faq.php?op=mod&amp;faqid=" . $faqObj->faqid() . '&amp;answerid=' . $proposed_answer->answerid() . "'><img src='" . $pathIcon16 . '/edit.png' . "' title='" . _AM_SF_FAQ_REVIEW . "' alt='" . _AM_SF_FAQ_REVIEW . "'></a>&nbsp;";
135
        $delete = "<a href='answer.php?op=del&amp;faqid=" . $faqObj->faqid() . '&amp;answerid=' . $proposed_answer->answerid() . "'><img src='" . $pathIcon16 . '/delete.png' . "' title='" . _AM_SF_DELETESUBM . "' alt='" . _AM_SF_DELETESUBM . "'></a>";
136
        echo '<tr>';
137
        echo "<td class='head' align='center'>" . $proposed_answer->answerid() . '</td>';
138
        echo "<td class='even' align='left'>" . $proposed_answer->answer() . '</td>';
139
140
        //show name of the answer submitter
141
        $submitter = Smartfaq\Utility::getLinkedUnameFromId($proposed_answer->uid(), $smartModuleConfig['userealname']);
142
        echo "<td class='even' align='center'>" . $submitter . '</td>';
143
144
        echo "<td class='even' align='center'>" . $proposed_answer->datesub() . '</td>';
145
        echo "<td class='even' align='center'> $merge $modify $approve $delete </td>";
146
        echo '</tr>';
147
    }
148
149
    echo "</table>\n<br>";
150
}
151
152
/* -- Available operations -- */
153
switch ($op) {
154
    case 'mod':
155
        xoops_cp_header();
156
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
157
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModule, $modify, $myts;
158
        $faqid = \Xmf\Request::getInt('faqid', 0, 'GET');
159
        editfaq($faqid);
0 ignored issues
show
Bug introduced by
$faqid of type integer is incompatible with the type boolean expected by parameter $showmenu of editfaq(). ( Ignorable by Annotation )

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

159
        editfaq(/** @scrutinizer ignore-type */ $faqid);
Loading history...
160
        break;
161
    case 'selectanswer':
162
        global $xoopsUser, $_GET;
163
164
        $faqid    = \Xmf\Request::getInt('faqid', 0, 'GET');
165
        $answerid = \Xmf\Request::getInt('answerid', 0, 'GET');
166
167
        // Creating the FAQ object
168
        $faqObj = new Smartfaq\Faq($faqid);
169
170
        if ($faqObj->notLoaded()) {
171
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
172
        }
173
174
        // Creating the answer object
175
        $answerObj = new Smartfaq\Answer($answerid);
176
177
        if ($answerObj->notLoaded()) {
178
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
179
        }
180
181
        $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
182
183
        $notifToDo_answer = null;
184
        $notifToDo_faq    = null;
185
186
        switch ($faqObj->status()) {
187
            // This was an Open Question that became a Submitted FAQ
188
            case Constants::SF_STATUS_ANSWERED:
189
                if (1 == $helper->getConfig('autoapprove_submitted_faq')) {
190
                    // We automatically approve Submitted Q&A
191
                    $redirect_msg = _AM_SF_ANSWER_APPROVED_PUBLISHED;
192
                    $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
193
                    $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
194
                    $notifToDo_faq = [Constants::SF_NOT_FAQ_PUBLISHED];
195
                } else {
196
                    // Submitted Q&A need approbation
197
                    $redirect_msg = _AM_SF_ANSWER_APPROVED_NEED_APPROVED;
198
                    $faqObj->setVar('status', Constants::SF_STATUS_SUBMITTED);
199
                    $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
200
                    $notifToDo_faq = [Constants::SF_NOT_FAQ_SUBMITTED];
201
                }
202
                break;
203
            // This is a published FAQ for which a user submitted a new answer and we just accepeted one
204
            case Constants::SF_STATUS_NEW_ANSWER:
205
                $redirect_msg = _AM_SF_FAQ_NEW_ANSWER_PUBLISHED;
206
                $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
207
                $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
208
                $notifToDo_answer = [Constants::SF_NOT_ANSWER_APPROVED];
209
                break;
210
        }
211
212
        // Storing the FAQ object in the database
213
        if (!$faqObj->store()) {
214
            redirect_header('<script>javascript:history.go(-1)</script>', 2, _AM_SF_ERROR_FAQ_NOT_SAVED);
215
        }
216
217
        // Storing the answer object in the database
218
        if (!$answerObj->store()) {
219
            redirect_header('<script>javascript:history.go(-1)</script>', 2, _AM_SF_ERROR_ANSWER_NOT_SAVED);
220
        }
221
222
        // Send FAQ notifications
223
        if (!empty($notifToDo_faq)) {
224
            $faqObj->sendNotifications($notifToDo_faq);
225
        }
226
227
        // Send answer notifications
228
        if (!empty($notifToDo_answer)) {
229
            $answerObj->sendNotifications($notifToDo_answer);
230
        }
231
232
        redirect_header('index.php', 2, $redirect_msg);
233
        break;
234
    case 'del':
235
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB;
236
237
        $faqid     = \Xmf\Request::getInt('faqid', 0, 'POST');
238
        $faqid     = \Xmf\Request::getInt('faqid', $faqid, 'GET');
239
        $answerid  = \Xmf\Request::getInt('answerid', 0, 'POST');
240
        $answerid  = \Xmf\Request::getInt('answerid', $answerid, 'GET');
241
        $confirm   = \Xmf\Request::getInt('confirm', 0, 'POST');
242
        $faqObj    = new Smartfaq\Faq($faqid);
243
        $answerObj = new Smartfaq\Answer($answerid);
244
        if ($confirm) {
245
            $answerObj->setVar('status', Constants::SF_AN_STATUS_REJECTED);
246
            $answerObj->store();
247
248
            switch ($faqObj->status()) {
249
                // Open Question for which we are rejecting an answer
250
                case Constants::SF_STATUS_ANSWERED:
251
                    $redirect_page = 'index.php';
252
                    $redirect_msg  = _AM_SF_ANSWER_REJECTED_OPEN_QUESTION;
253
                    $faqObj->setVar('status', Constants::SF_STATUS_OPENED);
254
                    break;
255
                case Constants::SF_STATUS_NEW_ANSWER:
256
                    $proposed_answers = $answerHandler->getAllAnswers($faqid, Constants::SF_AN_STATUS_PROPOSED);
257
                    if (count($proposed_answers) > 0) {
258
                        // This question has other proposed answer
259
                        $redirect_page = 'answer.php?op=mod&faqid=' . $faqid;
260
                        $redirect_msg  = _AM_SF_ANSWER_REJECTED;
261
                    } else {
262
                        // The question has no other proposed answer
263
                        $redirect_page = 'index.php';
264
                        $redirect_msg  = _AM_SF_ANSWER_REJECTED;
265
                        $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
266
                    }
267
                    break;
268
            }
269
            $faqObj->store();
270
            redirect_header($redirect_page, 3, $redirect_msg);
271
        } else {
272
            xoops_cp_header();
273
            xoops_confirm(['op' => 'del', 'answerid' => $answerid, 'confirm' => 1, 'faqid' => $faqid], 'answer.php', _AM_SF_DELETETHISANSWER, _AM_SF_DELETE);
274
            xoops_cp_footer();
275
        }
276
        exit();
277
        break;
278
    case 'default':
279
    default:
280
        xoops_cp_header();
281
282
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
283
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModule;
284
        /** @var Smartfaq\Helper $helper */
285
        $helper = Smartfaq\Helper::getInstance();
286
287
        editfaq();
288
        break;
289
}
290
291
require_once __DIR__ . '/admin_footer.php';
292