Passed
Branch master (4e30dc)
by Michael
02:20
created

answer.php ➔ editfaq()   C

Complexity

Conditions 8
Paths 42

Size

Total Lines 117
Code Lines 80

Duplication

Lines 8
Ratio 6.84 %

Importance

Changes 0
Metric Value
cc 8
eloc 80
nc 42
nop 1
dl 8
loc 117
rs 5.2676
c 0
b 0
f 0

How to fix   Long Method   

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
$op = '';
15
16
// Getting the operation we are doing
17
if (isset($_GET['op'])) {
18
    $op = $_GET['op'];
19
}
20
if (isset($_POST['op'])) {
21
    $op = $_POST['op'];
22
}
23
24
// Creating the answer handler object
25
/** @var \XoopsModules\Smartfaq\AnswerHandler $answerHandler */
26
$answerHandler = \XoopsModules\Smartfaq\Helper::getInstance()->getHandler('Answer');
27
28
/**
29
 * @param string $faqid
30
 */
31
function editfaq($faqid = '')
32
{
33
    global $answerHandler, $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $modify, $xoopsModuleConfig, $xoopsModule, $XOOPS_URL, $myts, $pathIcon16, $smartModuleConfig;
34
35
    require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
0 ignored issues
show
Bug introduced by
The constant XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
37
    // Creating the FAQ object
38
    $faqObj = new Smartfaq\Faq($faqid);
39
40
    // Creating the category object
41
    $categoryObj = $faqObj->category();
42
43
    if ($faqObj->notLoaded()) {
44
        redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
0 ignored issues
show
Bug introduced by
The function redirect_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

44
        /** @scrutinizer ignore-call */ 
45
        redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
Loading history...
45
    }
46
47
    switch ($faqObj->status()) {
48
49
        case Constants::SF_STATUS_ANSWERED:
50
            $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...
51
            $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...
52
            $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...
53
            $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...
54
            $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...
55
            $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...
56
            break;
57
58
    }
59
60
    $module_id    = $xoopsModule->getVar('mid');
61
    $gpermHandler = xoops_getHandler('groupperm');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

61
    $gpermHandler = /** @scrutinizer ignore-call */ xoops_getHandler('groupperm');
Loading history...
62
    $groups       = $xoopsUser ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
0 ignored issues
show
Bug introduced by
The constant XOOPS_GROUP_ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
63
64
    if (!Smartfaq\Utility::userIsAdmin()
65
        && (!$gpermHandler->checkRight('category_admin', $faqObj->categoryid(), $groups, $module_id))) {
66
        redirect_header('javascript:history.go(-1)', 1, _NOPERM);
0 ignored issues
show
Bug introduced by
The constant _NOPERM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
67
    }
68
    // Retreiving the official answer
69
    $official_answer = $faqObj->answer();
70
71
    Smartfaq\Utility::collapsableBar('bottomtable', 'bottomtableicon');
72
    echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt=''></a>&nbsp;" . _AM_SF_SUBMITTED_ANSWER . '</h3>';
0 ignored issues
show
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
    echo "<div id='bottomtable'>";
74
    echo '<span style="color: #567; margin: 3px 0 12px 0; font-size: small; display: block; ">' . _AM_SF_SUBMITTED_ANSWER_INFO . '</span>';
75
76
    $proposed_answers = $answerHandler->getAllAnswers($faqid, Constants::SF_AN_STATUS_PROPOSED);
77
78
    if (0 == count($proposed_answers)) {
79
        redirect_header('index.php', 1, _AM_SF_NOANSWERS);
80
    }
81
82
    echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>
83
            <tr>
84
              <td class='head' width='100px'>" . _AM_SF_CATEGORY . "</td>
85
              <td class='even'>" . $categoryObj->name() . "</td>
86
            </tr>
87
            <tr>
88
              <td class='head' width='100px'>" . _AM_SF_QUESTION . "</td>
89
              <td class='even'>" . $faqObj->question() . '</td>
90
            </tr>';
91
    if ($official_answer) {
92
        echo "
93
            <tr>
94
              <td class='head' width='100px'>" . _AM_SF_ANSWER_OFFICIAL . "</td>
95
              <td class='even'>" . $official_answer->answer() . '</td>
96
            </tr>';
97
    }
98
    echo "</table><br>\n";
99
100
    echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>";
101
    echo '<tr>';
102
    echo "<th width='40' class='bg3' align='center'><b>" . _AM_SF_ARTID . '</b></td>';
103
    echo "<th class='bg3' class='bg3' align='center'><b>" . _AM_SF_ANSWER . '</b></td>';
104
105
    echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_ANSWERED . '</b></td>';
106
107
    echo "<th width='180' class='bg3' align='center'><b>" . _AM_SF_CREATED . '</b></td>';
108
    echo "<th width='120' class='bg3' align='center'><b>" . _AM_SF_ACTION . '</b></td>';
109
    echo '</tr>';
110
111
    $merge   = '';
112
    $modify  = '';
113
    $approve = '';
114
    foreach ($proposed_answers as $proposed_answer) {
115
        if (Constants::SF_STATUS_NEW_ANSWER == $faqObj->status()) {
116
            $merge   = "<a href='faq.php?op=merge&amp;faqid="
117
                       . $faqObj->faqid()
118
                       . '&amp;answerid='
119
                       . $proposed_answer->answerid()
120
                       . "'><img src='"
121
                       . XOOPS_URL
122
                       . '/modules/'
123
                       . $xoopsModule->dirname()
124
                       . "/assets/images/icon/merge.gif' title='"
125
                       . _AM_SF_FAQ_MERGE
126
                       . "' alt='"
127
                       . _AM_SF_FAQ_MERGE
128
                       . "'></a>&nbsp;";
129
            $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>";
130
        }
131
        $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;";
132
        $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>";
133
        echo '<tr>';
134
        echo "<td class='head' align='center'>" . $proposed_answer->answerid() . '</td>';
135
        echo "<td class='even' align='left'>" . $proposed_answer->answer() . '</td>';
136
137
        //show name of the answer submitter
138
        $submitter = Smartfaq\Utility::getLinkedUnameFromId($proposed_answer->uid(), $smartModuleConfig['userealname']);
139
        echo "<td class='even' align='center'>" . $submitter . '</td>';
140
141
        echo "<td class='even' align='center'>" . $proposed_answer->datesub() . '</td>';
142
        echo "<td class='even' align='center'> $merge $modify $approve $delete </td>";
143
        echo '</tr>';
144
    }
145
146
    echo "</table>\n<br>";
147
}
148
149
/* -- Available operations -- */
150
switch ($op) {
151
    case 'mod':
152
        xoops_cp_header();
0 ignored issues
show
Bug introduced by
The function xoops_cp_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

152
        /** @scrutinizer ignore-call */ 
153
        xoops_cp_header();
Loading history...
153
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
0 ignored issues
show
Bug introduced by
The constant XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
154
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModuleConfig, $xoopsModule, $modify, $myts;
155
        $faqid = isset($_GET['faqid']) ? (int)$_GET['faqid'] : 0;
156
        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

156
        editfaq(/** @scrutinizer ignore-type */ $faqid);
Loading history...
157
        break;
158
159
    case 'selectanswer':
160
        global $xoopsUser, $_GET, $xoopsModuleConfig;
161
162
        $faqid    = isset($_GET['faqid']) ? (int)$_GET['faqid'] : 0;
163
        $answerid = isset($_GET['answerid']) ? (int)$_GET['answerid'] : 0;
164
165
        // Creating the FAQ object
166
        $faqObj = new Smartfaq\Faq($faqid);
167
168
        if ($faqObj->notLoaded()) {
169
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
0 ignored issues
show
Bug introduced by
The function redirect_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

169
            /** @scrutinizer ignore-call */ 
170
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
Loading history...
170
        }
171
172
        // Creating the answer object
173
        $answerObj = new Smartfaq\Answer($answerid);
174
175
        if ($answerObj->notLoaded()) {
176
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
177
        }
178
179
        $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
180
181
        $notifToDo_answer = null;
182
        $notifToDo_faq    = null;
183
184
        switch ($faqObj->status()) {
185
            // This was an Open Question that became a Submitted FAQ
186
            case Constants::SF_STATUS_ANSWERED:
187
                if (1 == $xoopsModuleConfig['autoapprove_submitted_faq']) {
188
                    // We automatically approve Submitted Q&A
189
                    $redirect_msg = _AM_SF_ANSWER_APPROVED_PUBLISHED;
190
                    $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
191
                    $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
192
                    $notifToDo_faq = [Constants::SF_NOT_FAQ_PUBLISHED];
193
                } else {
194
                    // Submitted Q&A need approbation
195
                    $redirect_msg = _AM_SF_ANSWER_APPROVED_NEED_APPROVED;
196
                    $faqObj->setVar('status', Constants::SF_STATUS_SUBMITTED);
197
                    $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
198
                    $notifToDo_faq = [Constants::SF_NOT_FAQ_SUBMITTED];
199
                }
200
                break;
201
202
            // This is a published FAQ for which a user submitted a new answer and we just accepeted one
203
            case Constants::SF_STATUS_NEW_ANSWER:
204
                $redirect_msg = _AM_SF_FAQ_NEW_ANSWER_PUBLISHED;
205
                $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
206
                $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
207
                $notifToDo_answer = [Constants::SF_NOT_ANSWER_APPROVED];
208
                break;
209
        }
210
211
        // Storing the FAQ object in the database
212
        if (!$faqObj->store()) {
213
            redirect_header('javascript:history.go(-1)', 2, _AM_SF_ERROR_FAQ_NOT_SAVED);
214
        }
215
216
        // Storing the answer object in the database
217
        if (!$answerObj->store()) {
218
            redirect_header('javascript:history.go(-1)', 2, _AM_SF_ERROR_ANSWER_NOT_SAVED);
219
        }
220
221
        // Send FAQ notifications
222
        if (!empty($notifToDo_faq)) {
223
            $faqObj->sendNotifications($notifToDo_faq);
224
        }
225
226
        // Send answer notifications
227
        if (!empty($notifToDo_answer)) {
228
            $answerObj->sendNotifications($notifToDo_answer);
229
        }
230
231
        redirect_header('index.php', 2, $redirect_msg);
232
        break;
233
234
    case 'del':
235
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB;
236
237
        $faqid     = isset($_POST['faqid']) ? (int)$_POST['faqid'] : 0;
238
        $faqid     = isset($_GET['faqid']) ? (int)$_GET['faqid'] : $faqid;
239
        $answerid  = isset($_POST['answerid']) ? (int)$_POST['answerid'] : 0;
240
        $answerid  = isset($_GET['answerid']) ? (int)$_GET['answerid'] : $answerid;
241
        $confirm   = isset($_POST['confirm']) ? (int)$_POST['confirm'] : 0;
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
256
                case Constants::SF_STATUS_NEW_ANSWER:
257
                    $proposed_answers = $answerHandler->getAllAnswers($faqid, Constants::SF_AN_STATUS_PROPOSED);
258
                    if (count($proposed_answers) > 0) {
259
                        // This question has other proposed answer
260
                        $redirect_page = 'answer.php?op=mod&faqid=' . $faqid;
261
                        $redirect_msg  = _AM_SF_ANSWER_REJECTED;
262
                    } else {
263
                        // The question has no other proposed answer
264
                        $redirect_page = 'index.php';
265
                        $redirect_msg  = _AM_SF_ANSWER_REJECTED;
266
                        $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
267
                    }
268
                    break;
269
            }
270
            $faqObj->store();
271
            redirect_header($redirect_page, 3, $redirect_msg);
272
        } else {
273
            xoops_cp_header();
274
            xoops_confirm(['op' => 'del', 'answerid' => $answerid, 'confirm' => 1, 'faqid' => $faqid], 'answer.php', _AM_SF_DELETETHISANSWER, _AM_SF_DELETE);
0 ignored issues
show
Bug introduced by
The function xoops_confirm was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

274
            /** @scrutinizer ignore-call */ 
275
            xoops_confirm(['op' => 'del', 'answerid' => $answerid, 'confirm' => 1, 'faqid' => $faqid], 'answer.php', _AM_SF_DELETETHISANSWER, _AM_SF_DELETE);
Loading history...
275
            xoops_cp_footer();
0 ignored issues
show
Bug introduced by
The function xoops_cp_footer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

275
            /** @scrutinizer ignore-call */ 
276
            xoops_cp_footer();
Loading history...
276
        }
277
        exit();
278
        break;
279
280
    case 'default':
281
    default:
282
        xoops_cp_header();
283
284
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
285
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModuleConfig, $xoopsModule;
286
287
        editfaq();
288
        break;
289
}
290
291
require_once __DIR__ . '/admin_footer.php';
292