Completed
Push — master ( 454ebd...de22fe )
by Michael
03:58
created

answer.php (2 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
use Xmf\Request;
10
11
require_once __DIR__ . '/header.php';
12
13
global $xoopsUser, $xoopsConfig, $xoopsModuleConfig, $xoopsModule;
14
15
// If user is anonymous and we don't allow anonymous posting, exit; else, get the uid
16
if (!$xoopsUser && (1 != $xoopsModuleConfig['anonpost'])) {
17
    redirect_header('index.php', 3, _NOPERM);
18
}
19
20
$op = 'form';
21
22
// Getting the operation we are doing
23
if (isset($_GET['op'])) {
24
    $op = $_GET['op'];
25
}
26
if (isset($_POST['op'])) {
27
    $op = $_POST['op'];
28
}
29
30
// Getting the faqid
31
$faqid = Request::getInt('faqid', 0, 'GET');
32
$faqid = Request::getInt('faqid', $faqid, 'POST');
33
34
// If no FAQ is selected, exit
35
if (0 == $faqid) {
36
    redirect_header('javascript:history.go(-1)', 1, _MD_SF_NOFAQSELECTED);
37
}
38
39
// Creating the FAQ handler object
40
$faqHandler = sf_gethandler('faq');
41
42
// Creating the answer handler object
43
$answerHandler = sf_gethandler('answer');
44
45
switch ($op) {
46
    // The answer is posted
47
    case 'post':
48
49
        global $faqObj, $xoopsUser, $xoopsConfig, $xoopsModule, $xoopsModuleConfig, $xoopsDB;
50
51
        // If user is anonymous and we don't allow anonymous posting, exit; else, get the uid
52 View Code Duplication
        if (!$xoopsUser) {
53
            if (1 == $xoopsModuleConfig['anonpost']) {
54
                $uid = 0;
55
            } else {
56
                redirect_header('index.php', 3, _NOPERM);
57
            }
58
        } else {
59
            $uid = $xoopsUser->uid();
60
        }
61
62
        // Creating the FAQ object for the selected FAQ
63
        $faqObj = new sfFaq($faqid);
64
65
        // If the selected FAQ was not found, exit
66
        if ($faqObj->notLoaded()) {
67
            redirect_header('javascript:history.go(-2)', 1, _MD_SF_NOFAQSELECTED);
68
        }
69
70
        // Get the category object related to that FAQ
71
        $categoryObj = $faqObj->category();
72
73
        // Create the answer object
74
        $newAnswerObj = $answerHandler->create();
75
76
        // Putting the values in the answer object
77
        $newAnswerObj->setVar('faqid', $faqObj->faqid());
78
        $newAnswerObj->setVar('answer', $_POST['answer']);
79
        $newAnswerObj->setVar('uid', $uid);
80
81
        // Depending of the status of the FAQ, some values need to be set
82
        $original_status = $faqObj->status();
83
        switch ($original_status) {
84
            // This is an Open Question
85
            case _SF_STATUS_OPENED:
86
                if (1 == $xoopsModuleConfig['autoapprove_answer']) {
87
                    // We automatically approve submitted answer for Open Question, so the question become a Submitted Q&A
88 View Code Duplication
                    if (1 == $xoopsModuleConfig['autoapprove_submitted_faq']) {
89
                        // We automatically approve Submitted Q&A
90
                        $redirect_msg = _MD_SF_QNA_RECEIVED_AND_PUBLISHED;
91
                        $faqObj->setVar('status', _SF_STATUS_PUBLISHED);
92
                        $newAnswerObj->setVar('status', _SF_AN_STATUS_APPROVED);
93
                        $notifCase = 1;
94
                    } else {
95
                        // Submitted Q&A need approbation
96
                        $redirect_msg = _MD_SF_QNA_RECEIVED_NEED_APPROVAL;
97
                        $faqObj->setVar('status', _SF_STATUS_SUBMITTED);
98
                        $newAnswerObj->setVar('status', _SF_AN_STATUS_PROPOSED);
99
                        $notifCase = 2;
100
                    }
101
                } else {
102
                    // Submitted answer need approbation
103
                    $redirect_msg = _MD_SF_OPEN_ANSWER_NEED_APPROBATION;
104
                    $faqObj->setVar('status', _SF_STATUS_ANSWERED);
105
                    $newAnswerObj->setVar('status', _SF_AN_STATUS_PROPOSED);
106
107
                    $notifCase = 3;
108
                }
109
                break;
110
111
            // This is a published FAQ for which a user submitted a new answer
112
            case _SF_STATUS_PUBLISHED:
113
            case _SF_STATUS_NEW_ANSWER:
114 View Code Duplication
                if (1 == $xoopsModuleConfig['autoapprove_answer_new']) {
115
                    // We automatically approve new submitted answer for already published FAQ
116
                    $redirect_msg = '4';
117
                    $faqObj->setVar('status', _SF_STATUS_SUBMITTED);
118
                    $newAnswerObj->setVar('status', _SF_AN_STATUS_APPROVED);
119
                    $notifCase = 4;
120
                } else {
121
                    // New submitted answer need approbation
122
                    $redirect_msg = _MD_SF_FAQ_NEW_ANSWER_NEED_APPROBATION;
123
                    $faqObj->setVar('status', _SF_STATUS_NEW_ANSWER);
124
                    $newAnswerObj->setVar('status', _SF_AN_STATUS_PROPOSED);
125
                    $notifCase = 5;
126
                }
127
                break;
128
        }
129
130
        // Storing the FAQ object in the database
131
        if (!$faqObj->store()) {
132
            redirect_header('javascript:history.go(-1)', 3, _MD_SF_SUBMIT_ERROR . sf_formatErrors($faqObj->getErrors()));
133
        }
134
135
        // Storing the answer object in the database
136
        if (!$newAnswerObj->store()) {
137
            redirect_header('javascript:history.go(-1)', 3, _MD_SF_SUBMIT_ERROR . sf_formatErrors($newAnswerObj->getErrors()));
138
        }
139
140
        $notificationHandler = xoops_getHandler('notification');
141
        switch ($notifCase) {
142
            case 1:
143
                // Question submitted, auto-approved; became Q&A, auto-approved
144
                // We do not not subscribe user to notification on publish since we publish it right away
145
146
                // Send notifications
147
                $faqObj->sendNotifications([_SF_NOT_FAQ_PUBLISHED]);
148
                break;
149
150 View Code Duplication
            case 2:
151
                // Answer for an open question submitted, auto-approved; became Q&A, need approbation
152
                if (isset($_POST['notifypub']) && 1 == $_POST['notifypub']) {
153
                    require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
154
                    $notificationHandler->subscribe('faq', $faqObj->faqid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
155
                }
156
                // Send notifications
157
                $faqObj->sendNotifications([_SF_NOT_FAQ_SUBMITTED]);
158
                break;
159
160
            case 3:
161
                // Answer submitted, needs approbation
162
                if (isset($_POST['notifypub']) && 1 == $_POST['notifypub']) {
163
                    require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
164
                    $notificationHandler->subscribe('question', $newAnswerObj->answerid(), 'approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
165
                }
166
                // Send notifications
167
                $faqObj->sendNotifications([_SF_NOT_QUESTION_SUBMITTED]);
168
                break;
169
            case 4:
170
                // New answer submitted for a published Q&A, auto-approved
171
                // TODO...
172
                break;
173
174 View Code Duplication
            case 5:
175
                // New answer submitted for a published Q&A, need approbation
176
                // Send notifications
177
                if (isset($_POST['notifypub']) && 1 == $_POST['notifypub']) {
178
                    require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
179
                    $notificationHandler->subscribe('faq', $newAnswerObj->answerid(), 'answer_approved', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE);
180
                }
181
182
                $faqObj->sendNotifications([_SF_NOT_NEW_ANSWER_PROPOSED]);
183
                break;
184
        }
185
186
        //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...
187
        redirect_header('index.php', 3, $redirect_msg);
188
        break;
189
190
    case 'form':
191
    default:
192
193
        global $xoopsUser, $xoopsModule, $HTTP_SERVER_VARS;
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...
194
195
        // Creating the FAQ object for the selected FAQ
196
        $faqObj = new sfFaq($faqid);
197
198
        // If the selected FAQ was not found, exit
199
        if ($faqObj->notLoaded()) {
200
            redirect_header('javascript:history.go(-1)', 1, _MD_SF_NOFAQSELECTED);
201
        }
202
203
        // Creating the category object that holds the selected FAQ
204
        $categoryObj = $faqObj->category();
205
206
        // Creating the answer object
207
        $answerObj = $faqObj->answer();
208
209
        // Check user permissions to access that category of the selected FAQ
210
        if (faqAccessGranted($faqObj) < 0) {
211
            redirect_header('javascript:history.go(-1)', 1, _NOPERM);
212
        }
213
214
        $GLOBALS['xoopsOption']['template_main'] = 'smartfaq_submit.tpl';
215
        require_once XOOPS_ROOT_PATH . '/header.php';
216
        require_once __DIR__ . '/footer.php';
217
218
        $name = $xoopsUser ? ucwords($xoopsUser->getVar('uname')) : 'Anonymous';
219
220
        $moduleName =& $myts->displayTarea($xoopsModule->getVar('name'));
221
        $xoopsTpl->assign('whereInSection', $moduleName);
222
        $xoopsTpl->assign('lang_submit', _MD_SF_SUBMITANSWER);
223
224
        $xoopsTpl->assign('lang_intro_title', sprintf(_MD_SF_SUBMITANSWERTO, ucwords($xoopsModule->name())));
225
        $xoopsTpl->assign('lang_intro_text', _MD_SF_GOODDAY . "<b>$name</b>, " . _MD_SF_SUBMITANSWER_INTRO);
226
227
        require_once __DIR__ . '/include/answer.inc.php';
228
229
        require_once XOOPS_ROOT_PATH . '/footer.php';
230
        break;
231
}
232