Completed
Push — master ( 6c5294...348995 )
by Michael
02:27
created

answer.php ➔ editfaq()   C

Complexity

Conditions 8
Paths 42

Size

Total Lines 104
Code Lines 80

Duplication

Lines 8
Ratio 7.69 %

Importance

Changes 0
Metric Value
cc 8
eloc 80
nc 42
nop 1
dl 8
loc 104
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
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 27 and the first side effect is on line 9.

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
include_once __DIR__ . '/admin_header.php';
10
11
$op = '';
12
13
// Getting the operation we are doing
14
if (isset($_GET['op'])) {
15
    $op = $_GET['op'];
16
}
17
if (isset($_POST['op'])) {
18
    $op = $_POST['op'];
19
}
20
21
// Creating the answer handler object
22
$answerHandler = sf_gethandler('answer');
23
24
/**
25
 * @param string $faqid
26
 */
27
function editfaq($faqid = '')
28
{
29
    global $answerHandler, $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $modify, $xoopsModuleConfig, $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...
30
31
    include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
32
33
    // Creating the FAQ object
34
    $faqObj = new sfFaq($faqid);
35
36
    // Creating the category object
37
    $categoryObj = $faqObj->category();
38
39
    if ($faqObj->notLoaded()) {
40
        redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
41
    }
42
43
    switch ($faqObj->status()) {
44
45 View Code Duplication
        case _SF_STATUS_ANSWERED:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
            $breadcrumb_action1   = _AM_SF_SUBMITTED;
0 ignored issues
show
Unused Code introduced by
$breadcrumb_action1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
47
            $breadcrumb_action2   = _AM_SF_APPROVING;
0 ignored issues
show
Unused Code introduced by
$breadcrumb_action2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
48
            $collapsableBar_title = _AM_SF_SUBMITTED_TITLE;
0 ignored issues
show
Unused Code introduced by
$collapsableBar_title is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
            $collapsableBar_info  = _AM_SF_SUBMITTED_INFO;
0 ignored issues
show
Unused Code introduced by
$collapsableBar_info is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
            $button_caption       = _AM_SF_APPROVE;
0 ignored issues
show
Unused Code introduced by
$button_caption is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
            $an_status            = _SF_AN_STATUS_PROPOSED;
0 ignored issues
show
Unused Code introduced by
$an_status is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
            break;
53
54
    }
55
56
    $module_id    = $xoopsModule->getVar('mid');
57
    $gpermHandler = xoops_getHandler('groupperm');
58
    $groups       = $xoopsUser ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS;
59
60
    if (!sf_userIsAdmin() && (!$gpermHandler->checkRight('category_admin', $faqObj->categoryid(), $groups, $module_id))) {
61
        redirect_header('javascript:history.go(-1)', 1, _NOPERM);
62
    }
63
    // Retreiving the official answer
64
    $official_answer = $faqObj->answer();
65
66
    sf_collapsableBar('bottomtable', 'bottomtableicon');
67
    echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a>&nbsp;" . _AM_SF_SUBMITTED_ANSWER . '</h3>';
68
    echo "<div id='bottomtable'>";
69
    echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . _AM_SF_SUBMITTED_ANSWER_INFO . '</span>';
70
71
    $proposed_answers = $answerHandler->getAllAnswers($faqid, _SF_AN_STATUS_PROPOSED);
72
73
    if (count($proposed_answers) == 0) {
74
        redirect_header('index.php', 1, _AM_SF_NOANSWERS);
75
    }
76
77
    echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>
78
            <tr>
79
              <td class='head' width='100px'>" . _AM_SF_CATEGORY . "</td>
80
              <td class='even'>" . $categoryObj->name() . "</td>
81
            </tr>
82
            <tr>
83
              <td class='head' width='100px'>" . _AM_SF_QUESTION . "</td>
84
              <td class='even'>" . $faqObj->question() . '</td>
85
            </tr>';
86
    if ($official_answer) {
87
        echo "
88
            <tr>
89
              <td class='head' width='100px'>" . _AM_SF_ANSWER_OFFICIAL . "</td>
90
              <td class='even'>" . $official_answer->answer() . '</td>
91
            </tr>';
92
    }
93
    echo "</table><br />\n";
94
95
    echo "<table width='100%' cellspacing=1 cellpadding=3 border=0 class = outer>";
96
    echo '<tr>';
97
    echo "<th width='40' class='bg3' align='center'><b>" . _AM_SF_ARTID . '</b></td>';
98
    echo "<th class='bg3' class='bg3' align='center'><b>" . _AM_SF_ANSWER . '</b></td>';
99
100
    echo "<th width='90' class='bg3' align='center'><b>" . _AM_SF_ANSWERED . '</b></td>';
101
102
    echo "<th width='180' class='bg3' align='center'><b>" . _AM_SF_CREATED . '</b></td>';
103
    echo "<th width='120' class='bg3' align='center'><b>" . _AM_SF_ACTION . '</b></td>';
104
    echo '</tr>';
105
106
    $merge   = '';
107
    $modify  = '';
108
    $approve = '';
109
    foreach ($proposed_answers as $proposed_answer) {
110
        if ($faqObj->status() == _SF_STATUS_NEW_ANSWER) {
111
            $merge   = "<a href='faq.php?op=merge&amp;faqid=" . $faqObj->faqid() . '&amp;answerid=' . $proposed_answer->answerid() . "'><img src='" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/merge.gif' title='" . _AM_SF_FAQ_MERGE . "' alt='" . _AM_SF_FAQ_MERGE . "' /></a>&nbsp;";
112
            $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>";
113
        }
114
        $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;";
115
        $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>";
116
        echo '<tr>';
117
        echo "<td class='head' align='center'>" . $proposed_answer->answerid() . '</td>';
118
        echo "<td class='even' align='left'>" . $proposed_answer->answer() . '</td>';
119
120
        //show name of the answer submitter
121
        $submitter = sf_getLinkedUnameFromId($proposed_answer->uid(), $smartModuleConfig['userealname']);
122
        echo "<td class='even' align='center'>" . $submitter . '</td>';
123
124
        echo "<td class='even' align='center'>" . $proposed_answer->datesub() . '</td>';
125
        echo "<td class='even' align='center'> $merge $modify $approve $delete </td>";
126
        echo '</tr>';
127
    }
128
129
    echo "</table>\n<br />";
130
}
131
132
/* -- Available operations -- */
133
switch ($op) {
134
    case 'mod':
135
        xoops_cp_header();
136
        include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
137
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModuleConfig, $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...
138
        $faqid = isset($_GET['faqid']) ? (int)$_GET['faqid'] : 0;
139
        editfaq($faqid);
140
        break;
141
142
    case 'selectanswer':
143
        global $xoopsUser, $_GET, $xoopsModuleConfig;
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...
144
145
        $faqid    = isset($_GET['faqid']) ? (int)$_GET['faqid'] : 0;
146
        $answerid = isset($_GET['answerid']) ? (int)$_GET['answerid'] : 0;
147
148
        // Creating the FAQ object
149
        $faqObj = new sfFaq($faqid);
150
151
        if ($faqObj->notLoaded()) {
152
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
153
        }
154
155
        // Creating the answer object
156
        $answerObj = new sfAnswer($answerid);
157
158
        if ($answerObj->notLoaded()) {
159
            redirect_header('index.php', 1, _AM_SF_NOFAQSELECTED);
160
        }
161
162
        $answerObj->setVar('status', _SF_AN_STATUS_APPROVED);
163
164
        $notifToDo_answer = null;
165
        $notifToDo_faq    = null;
166
167
        switch ($faqObj->status()) {
168
            // This was an Open Question that became a Submitted FAQ
169
            case _SF_STATUS_ANSWERED:
170
                if ($xoopsModuleConfig['autoapprove_submitted_faq'] == 1) {
171
                    // We automatically approve Submitted Q&A
172
                    $redirect_msg = _AM_SF_ANSWER_APPROVED_PUBLISHED;
173
                    $faqObj->setVar('status', _SF_STATUS_PUBLISHED);
174
                    $answerObj->setVar('status', _SF_AN_STATUS_APPROVED);
175
                    $notifToDo_faq = array(_SF_NOT_FAQ_PUBLISHED);
176
                } else {
177
                    // Submitted Q&A need approbation
178
                    $redirect_msg = _AM_SF_ANSWER_APPROVED_NEED_APPROVED;
179
                    $faqObj->setVar('status', _SF_STATUS_SUBMITTED);
180
                    $answerObj->setVar('status', _SF_AN_STATUS_APPROVED);
181
                    $notifToDo_faq = array(_SF_NOT_FAQ_SUBMITTED);
182
                }
183
                break;
184
185
            // This is a published FAQ for which a user submitted a new answer and we just accepeted one
186
            case _SF_STATUS_NEW_ANSWER:
187
                $redirect_msg = _AM_SF_FAQ_NEW_ANSWER_PUBLISHED;
188
                $faqObj->setVar('status', _SF_STATUS_PUBLISHED);
189
                $answerObj->setVar('status', _SF_AN_STATUS_APPROVED);
190
                $notifToDo_answer = array(_SF_NOT_ANSWER_APPROVED);
191
                break;
192
        }
193
194
        // Storing the FAQ object in the database
195
        if (!$faqObj->store()) {
196
            redirect_header('javascript:history.go(-1)', 2, _AM_SF_ERROR_FAQ_NOT_SAVED);
197
        }
198
199
        // Storing the answer object in the database
200
        if (!$answerObj->store()) {
201
            redirect_header('javascript:history.go(-1)', 2, _AM_SF_ERROR_ANSWER_NOT_SAVED);
202
        }
203
204
        // Send FAQ notifications
205
        if (!empty($notifToDo_faq)) {
206
            $faqObj->sendNotifications($notifToDo_faq);
207
        }
208
209
        // Send answer notifications
210
        if (!empty($notifToDo_answer)) {
211
            $answerObj->sendNotifications($notifToDo_answer);
212
        }
213
214
        redirect_header('index.php', 2, $redirect_msg);
215
        break;
216
217
    case 'del':
218
        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...
219
220
        $faqid     = isset($_POST['faqid']) ? (int)$_POST['faqid'] : 0;
221
        $faqid     = isset($_GET['faqid']) ? (int)$_GET['faqid'] : $faqid;
222
        $answerid  = isset($_POST['answerid']) ? (int)$_POST['answerid'] : 0;
223
        $answerid  = isset($_GET['answerid']) ? (int)$_GET['answerid'] : $answerid;
224
        $confirm   = isset($_POST['confirm']) ? (int)$_POST['confirm'] : 0;
225
        $faqObj    = new sfFaq($faqid);
226
        $answerObj = new sfAnswer($answerid);
227
        if ($confirm) {
228
            $answerObj->setVar('status', _SF_AN_STATUS_REJECTED);
229
            $answerObj->store();
230
231
            switch ($faqObj->status()) {
232
                // Open Question for which we are rejecting an answer
233
                case _SF_STATUS_ANSWERED:
234
                    $redirect_page = 'index.php';
235
                    $redirect_msg  = _AM_SF_ANSWER_REJECTED_OPEN_QUESTION;
236
                    $faqObj->setVar('status', _SF_STATUS_OPENED);
237
                    break;
238
239
                case _SF_STATUS_NEW_ANSWER:
240
                    $proposed_answers = $answerHandler->getAllAnswers($faqid, _SF_AN_STATUS_PROPOSED);
241
                    if (count($proposed_answers) > 0) {
242
                        // This question has other proposed answer
243
                        $redirect_page = 'answer.php?op=mod&faqid=' . $faqid;
244
                        $redirect_msg  = _AM_SF_ANSWER_REJECTED;
245
                    } else {
246
                        // The question has no other proposed answer
247
                        $redirect_page = 'index.php';
248
                        $redirect_msg  = _AM_SF_ANSWER_REJECTED;
249
                        $faqObj->setVar('status', _SF_STATUS_PUBLISHED);
250
                    }
251
                    break;
252
            }
253
            $faqObj->store();
254
            redirect_header($redirect_page, 3, $redirect_msg);
255
        } else {
256
            xoops_cp_header();
257
            xoops_confirm(array('op' => 'del', 'answerid' => $answerid, 'confirm' => 1, 'faqid' => $faqid), 'answer.php', _AM_SF_DELETETHISANSWER, _AM_SF_DELETE);
258
            xoops_cp_footer();
259
        }
260
        exit();
261
        break;
262
263
    case 'default':
264
    default:
265
        xoops_cp_header();
266
267
        include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
268
        global $xoopsUser, $xoopsUser, $xoopsConfig, $xoopsDB, $xoopsModuleConfig, $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...
269
270
        editfaq();
271
        break;
272
}
273
274
include_once __DIR__ . '/admin_footer.php';
275