Completed
Push — master ( a7fe76...1b2f30 )
by Michael
04:20
created

answer.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Module: SmartFAQ
5
 * Author: The SmartFactory <www.smartfactory.ca>
6
 * Licence: GNU
7
 */
8
9
include_once __DIR__ . '/header.php';
10
11
global $xoopsUser, $xoopsConfig, $xoopsModuleConfig, $xoopsModule;
12
13
// If user is anonymous and we don't allow anonymous posting, exit; else, get the uid
14
if (!$xoopsUser && ($xoopsModuleConfig['anonpost'] != 1)) {
15
    redirect_header('index.php', 3, _NOPERM);
16
}
17
18
$op = 'form';
19
20
// Getting the operation we are doing
21
if (isset($_GET['op'])) {
22
    $op = $_GET['op'];
23
}
24
if (isset($_POST['op'])) {
25
    $op = $_POST['op'];
26
}
27
28
// Getting the faqid
29
$faqid = isset($_GET['faqid']) ? (int)$_GET['faqid'] : 0;
30
$faqid = isset($_POST['faqid']) ? (int)$_POST['faqid'] : $faqid;
31
32
// If no FAQ is selected, exit
33
if ($faqid == 0) {
34
    redirect_header('javascript:history.go(-1)', 1, _MD_SF_NOFAQSELECTED);
35
}
36
37
// Creating the FAQ handler object
38
$faqHandler = sf_gethandler('faq');
39
40
// Creating the answer handler object
41
$answerHandler = sf_gethandler('answer');
42
43
switch ($op) {
44
    // The answer is posted
45
    case 'post':
0 ignored issues
show
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
46
47
        global $faqObj, $xoopsUser, $xoopsConfig, $xoopsModule, $xoopsModuleConfig, $xoopsDB;
48
49
        // If user is anonymous and we don't allow anonymous posting, exit; else, get the uid
50 View Code Duplication
        if (!$xoopsUser) {
51
            if ($xoopsModuleConfig['anonpost'] == 1) {
52
                $uid = 0;
53
            } else {
54
                redirect_header('index.php', 3, _NOPERM);
55
            }
56
        } else {
57
            $uid = $xoopsUser->uid();
58
        }
59
60
        // Creating the FAQ object for the selected FAQ
61
        $faqObj = new sfFaq($faqid);
62
63
        // If the selected FAQ was not found, exit
64
        if ($faqObj->notLoaded()) {
65
            redirect_header('javascript:history.go(-2)', 1, _MD_SF_NOFAQSELECTED);
66
        }
67
68
        // Get the category object related to that FAQ
69
        $categoryObj = $faqObj->category();
70
71
        // Create the answer object
72
        $newAnswerObj = $answerHandler->create();
73
74
        // Putting the values in the answer object
75
        $newAnswerObj->setVar('faqid', $faqObj->faqid());
76
        $newAnswerObj->setVar('answer', $_POST['answer']);
77
        $newAnswerObj->setVar('uid', $uid);
78
79
        // Depending of the status of the FAQ, some values need to be set
80
        $original_status = $faqObj->status();
81
        switch ($original_status) {
82
            // This is an Open Question
83
            case _SF_STATUS_OPENED:
84
                if ($xoopsModuleConfig['autoapprove_answer'] == 1) {
85
                    // We automatically approve submitted answer for Open Question, so the question become a Submitted Q&A
86 View Code Duplication
                    if ($xoopsModuleConfig['autoapprove_submitted_faq'] == 1) {
87
                        // We automatically approve Submitted Q&A
88
                        $redirect_msg = _MD_SF_QNA_RECEIVED_AND_PUBLISHED;
89
                        $faqObj->setVar('status', _SF_STATUS_PUBLISHED);
90
                        $newAnswerObj->setVar('status', _SF_AN_STATUS_APPROVED);
91
                        $notifCase = 1;
92
                    } else {
93
                        // Submitted Q&A need approbation
94
                        $redirect_msg = _MD_SF_QNA_RECEIVED_NEED_APPROVAL;
95
                        $faqObj->setVar('status', _SF_STATUS_SUBMITTED);
96
                        $newAnswerObj->setVar('status', _SF_AN_STATUS_PROPOSED);
97
                        $notifCase = 2;
98
                    }
99
                } else {
100
                    // Submitted answer need approbation
101
                    $redirect_msg = _MD_SF_OPEN_ANSWER_NEED_APPROBATION;
102
                    $faqObj->setVar('status', _SF_STATUS_ANSWERED);
103
                    $newAnswerObj->setVar('status', _SF_AN_STATUS_PROPOSED);
104
105
                    $notifCase = 3;
106
                }
107
                break;
108
109
            // This is a published FAQ for which a user submitted a new answer
110
            case _SF_STATUS_PUBLISHED:
111
            case _SF_STATUS_NEW_ANSWER:
112 View Code Duplication
                if ($xoopsModuleConfig['autoapprove_answer_new'] == 1) {
113
                    // We automatically approve new submitted answer for already published FAQ
114
                    $redirect_msg = '4';
115
                    $faqObj->setVar('status', _SF_STATUS_SUBMITTED);
116
                    $newAnswerObj->setVar('status', _SF_AN_STATUS_APPROVED);
117
                    $notifCase = 4;
118
                } else {
119
                    // New submitted answer need approbation
120
                    $redirect_msg = _MD_SF_FAQ_NEW_ANSWER_NEED_APPROBATION;
121
                    $faqObj->setVar('status', _SF_STATUS_NEW_ANSWER);
122
                    $newAnswerObj->setVar('status', _SF_AN_STATUS_PROPOSED);
123
                    $notifCase = 5;
124
                }
125
                break;
126
        }
127
128
        // Storing the FAQ object in the database
129
        if (!$faqObj->store()) {
130
            redirect_header('javascript:history.go(-1)', 3, _MD_SF_SUBMIT_ERROR . sf_formatErrors($faqObj->getErrors()));
131
        }
132
133
        // Storing the answer object in the database
134
        if (!$newAnswerObj->store()) {
135
            redirect_header('javascript:history.go(-1)', 3, _MD_SF_SUBMIT_ERROR . sf_formatErrors($newAnswerObj->getErrors()));
136
        }
137
138
        $notificationHandler = xoops_getHandler('notification');
139
        switch ($notifCase) {
140
            case 1:
141
                // Question submitted, auto-approved; became Q&A, auto-approved
142
                // We do not not subscribe user to notification on publish since we publish it right away
143
144
                // Send notifications
145
                $faqObj->sendNotifications(array(_SF_NOT_FAQ_PUBLISHED));
146
                break;
147
148 View Code Duplication
            case 2:
149
                // Answer for an open question submitted, auto-approved; became Q&A, need approbation
150
                if (isset($_POST['notifypub']) && $_POST['notifypub'] == 1) {
151
                    include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
152
                    $notificationHandler->subscribe('faq', $faqObj->faqid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
153
                }
154
                // Send notifications
155
                $faqObj->sendNotifications(array(_SF_NOT_FAQ_SUBMITTED));
156
                break;
157
158
            case 3:
159
                // Answer submitted, needs approbation
160
                if (isset($_POST['notifypub']) && $_POST['notifypub'] == 1) {
161
                    include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
162
                    $notificationHandler->subscribe('question', $newAnswerObj->answerid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
163
                }
164
                // Send notifications
165
                $faqObj->sendNotifications(array(_SF_NOT_QUESTION_SUBMITTED));
166
                break;
167
            case 4:
168
                // New answer submitted for a published Q&A, auto-approved
169
                // TODO...
170
                break;
171
172 View Code Duplication
            case 5:
173
                // New answer submitted for a published Q&A, need approbation
174
                // Send notifications
175
                if (isset($_POST['notifypub']) && $_POST['notifypub'] == 1) {
176
                    include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
177
                    $notificationHandler->subscribe('faq', $newAnswerObj->answerid(), 'answer_approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
178
                }
179
180
                $faqObj->sendNotifications(array(_SF_NOT_NEW_ANSWER_PROPOSED));
181
                break;
182
        }
183
184
        //redirect_header("javascript:history.go(-1)", 3, $redirect_msg);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
185
        redirect_header('index.php', 3, $redirect_msg);
186
        break;
187
188
    case 'form':
189
    default:
0 ignored issues
show
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
190
191
        global $xoopsUser, $xoopsModule, $HTTP_SERVER_VARS;
192
193
        // Creating the FAQ object for the selected FAQ
194
        $faqObj = new sfFaq($faqid);
195
196
        // If the selected FAQ was not found, exit
197
        if ($faqObj->notLoaded()) {
198
            redirect_header('javascript:history.go(-1)', 1, _MD_SF_NOFAQSELECTED);
199
        }
200
201
        // Creating the category object that holds the selected FAQ
202
        $categoryObj = $faqObj->category();
203
204
        // Creating the answer object
205
        $answerObj = $faqObj->answer();
206
207
        // Check user permissions to access that category of the selected FAQ
208
        if (faqAccessGranted($faqObj) < 0) {
209
            redirect_header('javascript:history.go(-1)', 1, _NOPERM);
210
        }
211
212
        $xoopsOption['template_main'] = 'smartfaq_submit.tpl';
213
        include_once(XOOPS_ROOT_PATH . '/header.php');
214
        include_once __DIR__ . '/footer.php';
215
216
        $name = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
217
218
        $moduleName =& $myts->displayTarea($xoopsModule->getVar('name'));
219
        $xoopsTpl->assign('whereInSection', $moduleName);
220
        $xoopsTpl->assign('lang_submit', _MD_SF_SUBMITANSWER);
221
222
        $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUBMITANSWERTO, ucwords($xoopsModule->name())));
223
        $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUBMITANSWER_INTRO);
224
225
        include_once 'include/answer.inc.php';
226
227
        include_once XOOPS_ROOT_PATH . '/footer.php';
228
        break;
229
}
230