Passed
Push — master ( 2744eb...81ba93 )
by Michael
02:46
created

editfaq()   D

Complexity

Conditions 10
Paths 192

Size

Total Lines 118
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 118
rs 4.606
c 1
b 0
f 0
cc 10
eloc 78
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 34 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
/** @var Smartfaq\Helper $helper */
13
$helper = Smartfaq\Helper::getInstance();
14
15
require_once __DIR__ . '/admin_header.php';
16
17
$op = '';
18
19
// Getting the operation we are doing
20
if (isset($_GET['op'])) {
21
    $op = $_GET['op'];
22
}
23
if (isset($_POST['op'])) {
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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';
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...
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);
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

49
        /** @scrutinizer ignore-call */ 
50
        redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
Loading history...
50
    }
51
52
    switch ($faqObj->status()) {
53
54
        case Constants::SF_STATUS_ANSWERED:
55
            $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...
56
            $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...
57
            $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...
58
            $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...
59
            $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...
60
            $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...
61
            break;
62
63
    }
64
65
    $module_id    = $xoopsModule->getVar('mid');
66
    $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

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

157
        /** @scrutinizer ignore-call */ 
158
        xoops_cp_header();
Loading history...
158
        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...
159
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModule, $modify, $myts;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
160
        $faqid = \Xmf\Request::getInt('faqid', 0, 'GET');
0 ignored issues
show
Bug introduced by
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
161
        editfaq($faqid);
162
        break;
163
164
    case 'selectanswer':
165
        global $xoopsUser, $_GET;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
166
167
        $faqid    = \Xmf\Request::getInt('faqid', 0, 'GET');
168
        $answerid = \Xmf\Request::getInt('answerid', 0, 'GET');
169
170
        // Creating the FAQ object
171
        $faqObj = new Smartfaq\Faq($faqid);
172
173
        if ($faqObj->notLoaded()) {
174
            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

174
            /** @scrutinizer ignore-call */ 
175
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
Loading history...
175
        }
176
177
        // Creating the answer object
178
        $answerObj = new Smartfaq\Answer($answerid);
179
180
        if ($answerObj->notLoaded()) {
181
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
182
        }
183
184
        $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
185
186
        $notifToDo_answer = null;
187
        $notifToDo_faq    = null;
188
189
        switch ($faqObj->status()) {
190
            // This was an Open Question that became a Submitted FAQ
191
            case Constants::SF_STATUS_ANSWERED:
192
                if (1 == $helper->getConfig('autoapprove_submitted_faq')) {
193
                    // We automatically approve Submitted Q&A
194
                    $redirect_msg = _AM_SF_ANSWER_APPROVED_PUBLISHED;
195
                    $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
196
                    $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
197
                    $notifToDo_faq = [Constants::SF_NOT_FAQ_PUBLISHED];
198
                } else {
199
                    // Submitted Q&A need approbation
200
                    $redirect_msg = _AM_SF_ANSWER_APPROVED_NEED_APPROVED;
201
                    $faqObj->setVar('status', Constants::SF_STATUS_SUBMITTED);
202
                    $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
203
                    $notifToDo_faq = [Constants::SF_NOT_FAQ_SUBMITTED];
204
                }
205
                break;
206
207
            // This is a published FAQ for which a user submitted a new answer and we just accepeted one
208
            case Constants::SF_STATUS_NEW_ANSWER:
209
                $redirect_msg = _AM_SF_FAQ_NEW_ANSWER_PUBLISHED;
210
                $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
211
                $answerObj->setVar('status', Constants::SF_AN_STATUS_APPROVED);
212
                $notifToDo_answer = [Constants::SF_NOT_ANSWER_APPROVED];
213
                break;
214
        }
215
216
        // Storing the FAQ object in the database
217
        if (!$faqObj->store()) {
218
            redirect_header('javascript:history.go(-1)', 2, _AM_SF_ERROR_FAQ_NOT_SAVED);
219
        }
220
221
        // Storing the answer object in the database
222
        if (!$answerObj->store()) {
223
            redirect_header('javascript:history.go(-1)', 2, _AM_SF_ERROR_ANSWER_NOT_SAVED);
224
        }
225
226
        // Send FAQ notifications
227
        if (!empty($notifToDo_faq)) {
228
            $faqObj->sendNotifications($notifToDo_faq);
229
        }
230
231
        // Send answer notifications
232
        if (!empty($notifToDo_answer)) {
233
            $answerObj->sendNotifications($notifToDo_answer);
234
        }
235
236
        redirect_header('index.php', 2, $redirect_msg);
237
        break;
238
239
    case 'del':
240
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
241
242
        $faqid     = \Xmf\Request::getInt('faqid', 0, 'POST');
243
        $faqid     = \Xmf\Request::getInt('faqid', $faqid, 'GET');
244
        $answerid  = \Xmf\Request::getInt('answerid', 0, 'POST');
245
        $answerid  = \Xmf\Request::getInt('answerid', $answerid, 'GET');
246
        $confirm   = \Xmf\Request::getInt('confirm', 0, 'POST');
247
        $faqObj    = new Smartfaq\Faq($faqid);
248
        $answerObj = new Smartfaq\Answer($answerid);
249
        if ($confirm) {
250
            $answerObj->setVar('status', Constants::SF_AN_STATUS_REJECTED);
251
            $answerObj->store();
252
253
            switch ($faqObj->status()) {
254
                // Open Question for which we are rejecting an answer
255
                case Constants::SF_STATUS_ANSWERED:
256
                    $redirect_page = 'index.php';
257
                    $redirect_msg  = _AM_SF_ANSWER_REJECTED_OPEN_QUESTION;
258
                    $faqObj->setVar('status', Constants::SF_STATUS_OPENED);
259
                    break;
260
261
                case Constants::SF_STATUS_NEW_ANSWER:
262
                    $proposed_answers = $answerHandler->getAllAnswers($faqid, Constants::SF_AN_STATUS_PROPOSED);
263
                    if (count($proposed_answers) > 0) {
264
                        // This question has other proposed answer
265
                        $redirect_page = 'answer.php?op=mod&faqid=' . $faqid;
266
                        $redirect_msg  = _AM_SF_ANSWER_REJECTED;
267
                    } else {
268
                        // The question has no other proposed answer
269
                        $redirect_page = 'index.php';
270
                        $redirect_msg  = _AM_SF_ANSWER_REJECTED;
271
                        $faqObj->setVar('status', Constants::SF_STATUS_PUBLISHED);
272
                    }
273
                    break;
274
            }
275
            $faqObj->store();
276
            redirect_header($redirect_page, 3, $redirect_msg);
277
        } else {
278
            xoops_cp_header();
279
            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

279
            /** @scrutinizer ignore-call */ 
280
            xoops_confirm(['op' => 'del', 'answerid' => $answerid, 'confirm' => 1, 'faqid' => $faqid], 'answer.php', _AM_SF_DELETETHISANSWER, _AM_SF_DELETE);
Loading history...
280
            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

280
            /** @scrutinizer ignore-call */ 
281
            xoops_cp_footer();
Loading history...
281
        }
282
        exit();
283
        break;
284
285
    case 'default':
286
    default:
287
        xoops_cp_header();
288
289
        require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
290
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModule;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
291
        /** @var Smartfaq\Helper $helper */
292
        $helper = Smartfaq\Helper::getInstance();
293
294
        editfaq();
295
        break;
296
}
297
298
require_once __DIR__ . '/admin_footer.php';
299