AnswerHandler   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 75
c 0
b 0
f 0
dl 0
loc 199
rs 10
wmc 27

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A create() 0 3 1
A __construct() 0 3 1
B getAnswersDetailsByRegistration() 0 46 11
A getAnswersCompare() 0 25 5
A getCountAnswers() 0 5 1
A getAnswersCriteria() 0 7 1
A getInsertId() 0 3 1
A cleanupAnswers() 0 14 4
A getAllAnswers() 0 5 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Wgevents;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * wgEvents module for xoops
17
 *
18
 * @copyright    2021 XOOPS Project (https://xoops.org)
19
 * @license      GPL 2.0 or later
20
 * @package      wgevents
21
 * @since        1.0.0
22
 * @min_xoops    2.5.11 Beta1
23
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
24
 */
25
26
use XoopsModules\Wgevents;
27
use XoopsModules\Wgevents\Helper;
28
29
/**
30
 * Class Object Handler Answer
31
 */
32
class AnswerHandler extends \XoopsPersistableObjectHandler
33
{
34
    /**
35
     * Constructor
36
     *
37
     * @param \XoopsDatabase $db
38
     */
39
    public function __construct(\XoopsDatabase $db)
40
    {
41
        parent::__construct($db, 'wgevents_answer', Answer::class, 'id', 'evid');
42
    }
43
44
    /**
45
     * @param bool $isNew
46
     *
47
     * @return object
48
     */
49
    public function create($isNew = true)
50
    {
51
        return parent::create($isNew);
52
    }
53
54
    /**
55
     * retrieve a field
56
     *
57
     * @param int $id field id
58
     * @param $fields
59
     * @return \XoopsObject|null reference to the {@link Get} object
60
     */
61
    public function get($id = null, $fields = null)
62
    {
63
        return parent::get($id, $fields);
64
    }
65
66
    /**
67
     * get inserted id
68
     *
69
     * @return int reference to the {@link Get} object
70
     */
71
    public function getInsertId()
72
    {
73
        return $this->db->getInsertId();
74
    }
75
76
    /**
77
     * Get Count Answer in the database
78
     * @param int    $start
79
     * @param int    $limit
80
     * @param string $sort
81
     * @param string $order
82
     * @return int
83
     */
84
    public function getCountAnswers($start = 0, $limit = 0, $sort = 'id ASC, evid', $order = 'ASC')
85
    {
86
        $crCountAnswers = new \CriteriaCompo();
87
        $crCountAnswers = $this->getAnswersCriteria($crCountAnswers, $start, $limit, $sort, $order);
88
        return $this->getCount($crCountAnswers);
89
    }
90
91
    /**
92
     * Get All Answer in the database
93
     * @param int    $start
94
     * @param int    $limit
95
     * @param string $sort
96
     * @param string $order
97
     * @return array
98
     */
99
    public function getAllAnswers($start = 0, $limit = 0, $sort = 'id ASC, evid', $order = 'ASC')
100
    {
101
        $crAllAnswers = new \CriteriaCompo();
102
        $crAllAnswers = $this->getAnswersCriteria($crAllAnswers, $start, $limit, $sort, $order);
103
        return $this->getAll($crAllAnswers);
104
    }
105
106
    /**
107
     * Get Criteria Answer
108
     * @param        $crAnswer
109
     * @param int $start
110
     * @param int $limit
111
     * @param string $sort
112
     * @param string $order
113
     * @return \CriteriaCompo
114
     */
115
    private function getAnswersCriteria($crAnswer, int $start, int $limit, string $sort, string $order)
116
    {
117
        $crAnswer->setStart($start);
118
        $crAnswer->setLimit($limit);
119
        $crAnswer->setSort($sort);
120
        $crAnswer->setOrder($order);
121
        return $crAnswer;
122
    }
123
124
    /**
125
     * Delete all answers for given event
126
     * @param int    $evId
127
     * @param int    $regId
128
     * @return bool
129
     */
130
    public function cleanupAnswers($evId, $regId = 0)
131
    {
132
        if ($evId > 0) {
133
            $crAnswer = new \CriteriaCompo();
134
            $crAnswer->add(new \Criteria('evid', $evId));
135
            if ($regId > 0) {
136
                $crAnswer->add(new \Criteria('regid', $regId));
137
            }
138
            $answersCount = $this->getCount($crAnswer);
139
            if ($answersCount > 0) {
140
                return $this->deleteAll($crAnswer);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->deleteAll($crAnswer) also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
141
            }
142
        }
143
        return true;
144
    }
145
146
    /**
147
     * get all answers of given registration
148
     * @param int    $regId            // id of registration
149
     * @param array  $questionsArr   // array with all questions for this event
150
     * @return array
151
     */
152
    public function getAnswersDetailsByRegistration($regId, $questionsArr)
153
    {
154
        $answers = [];
155
        foreach ($questionsArr as $queId => $queItem) {
156
            // get answers for this questions
157
            $crAnswer = new \CriteriaCompo();
158
            $crAnswer->add(new \Criteria('regid', $regId));
159
            $crAnswer->add(new \Criteria('queid', $queId));
160
            $answersCount = $this->getCount($crAnswer);
161
            if ($answersCount > 0) {
162
                $answersAll = $this->getAll($crAnswer);
163
                foreach (\array_keys($answersAll) as $i) {
164
                    $ansText = $answersAll[$i]->getVar('text', 'n');
165
                    if (Constants::FIELD_RADIOYN == $queItem['type']) {
166
                        if ((bool)$ansText) {
167
                            $ansText = \_YES;
168
                        } else {
169
                            $ansText = \_NO;
170
                        }
171
                    }
172
                    if (Constants::FIELD_CHECKBOX == $queItem['type'] ||
173
                        Constants::FIELD_COMBOBOX == $queItem['type']) {
174
                        $queValues = \unserialize($queItem['values'], ['allowed_classes' => false]);
175
                        $ansItems = \unserialize($ansText, ['allowed_classes' => false]);
176
                        $ansText = '';
177
                        foreach ($ansItems as $ansItem) {
178
                            $ansText .= $queValues[(int)$ansItem] . ' <br>';
179
                        }
180
                    }
181
                    if (Constants::FIELD_SELECTBOX == $queItem['type']) {
182
                        $queValues = \unserialize($queItem['values'], ['allowed_classes' => false]);
183
                        $ansItem = (string)\unserialize($ansText, ['allowed_classes' => false]);
184
                        $ansText = $queValues[(int)$ansItem];
185
                    }
186
                    if (Constants::FIELD_RADIO == $queItem['type']) {
187
                        $queValues = \unserialize($queItem['values']);
188
                        $ansText = $queValues[$ansText];
189
                    }
190
                    $answers[$queId] = $ansText;
191
                }
192
            } else {
193
                $answers[$queId] = '';
194
            }
195
        }
196
197
        return $answers;
198
    }
199
200
    /**
201
     * compare two versions of answers
202
     * @param  $versionOld
203
     * @param  $versionNew
204
     * @return string
205
     */
206
    public function getAnswersCompare($versionOld, $versionNew)
207
    {
208
        $helper  = Helper::getInstance();
209
        $questionHandler = $helper->getHandler('Question');
210
211
        $infotext = '';
212
        foreach(\array_keys($versionNew) as $key) {
213
            $caption = '';
214
            $questionObj = $questionHandler->get($key);
215
            if (\is_object($questionObj)) {
216
                $caption = $questionObj->getVar('caption');
217
            }
218
            $valueOld = (string)$versionOld[$key];
219
            $valueNew = (string)$versionNew[$key];
220
            if ($valueOld !== $valueNew) {
221
                if ('' === $valueNew) {
222
                    $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION_DEL, $caption) . PHP_EOL;
0 ignored issues
show
Bug introduced by
It seems like $caption can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

222
                    $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION_DEL, /** @scrutinizer ignore-type */ $caption) . PHP_EOL;
Loading history...
223
                } else {
224
                    $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION, $caption, $valueOld, $valueNew) . PHP_EOL;
225
                }
226
            }
227
            unset($questionObj);
228
        }
229
230
        return $infotext;
231
    }
232
}
233