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
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class Object Handler Question |
31
|
|
|
*/ |
32
|
|
|
class QuestionHandler extends \XoopsPersistableObjectHandler |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
|
|
* |
37
|
|
|
* @param \XoopsDatabase $db |
38
|
|
|
*/ |
39
|
|
|
public function __construct(\XoopsDatabase $db) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($db, 'wgevents_question', Question::class, 'id', 'id'); |
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 Question 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 getCountQuestions($start = 0, $limit = 0, $sort = 'weight ASC, id', $order = 'ASC') |
85
|
|
|
{ |
86
|
|
|
$crCountQuestions = new \CriteriaCompo(); |
87
|
|
|
$crCountQuestions = $this->getQuestionsCriteria($crCountQuestions, $start, $limit, $sort, $order); |
88
|
|
|
return $this->getCount($crCountQuestions); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get All Question 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 getAllQuestions($start = 0, $limit = 0, $sort = 'weight ASC, id', $order = 'ASC') |
100
|
|
|
{ |
101
|
|
|
$crAllQuestions = new \CriteriaCompo(); |
102
|
|
|
$crAllQuestions = $this->getQuestionsCriteria($crAllQuestions, $start, $limit, $sort, $order); |
103
|
|
|
return $this->getAll($crAllQuestions); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get Criteria Question |
108
|
|
|
* @param $crQuestion |
109
|
|
|
* @param int $start |
110
|
|
|
* @param int $limit |
111
|
|
|
* @param string $sort |
112
|
|
|
* @param string $order |
113
|
|
|
* @return \CriteriaCompo |
114
|
|
|
*/ |
115
|
|
|
private function getQuestionsCriteria($crQuestion, int $start, int $limit, string $sort, string $order) |
116
|
|
|
{ |
117
|
|
|
$crQuestion->setStart($start); |
118
|
|
|
$crQuestion->setLimit($limit); |
119
|
|
|
$crQuestion->setSort($sort); |
120
|
|
|
$crQuestion->setOrder($order); |
121
|
|
|
return $crQuestion; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Function create Question Defaultset |
126
|
|
|
* @param $addEvid |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function createQuestionsDefaultset ($addEvid) { |
130
|
|
|
|
131
|
|
|
$helper = \XoopsModules\Wgevents\Helper::getInstance(); |
132
|
|
|
|
133
|
|
|
$uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0; |
134
|
|
|
$fieldHandler = $helper->getHandler('Field'); |
135
|
|
|
$questionHandler = $helper->getHandler('Question'); |
136
|
|
|
|
137
|
|
|
$crAddTypes = new \CriteriaCompo(); |
138
|
|
|
$crAddTypes->add(new \Criteria('default', 1)); |
139
|
|
|
$fieldsCount = $fieldHandler->getCount($crAddTypes); |
140
|
|
|
if ($fieldsCount > 0) { |
141
|
|
|
$crAddTypes->setSort('weight asc, id'); |
142
|
|
|
$crAddTypes->setOrder('ASC'); |
143
|
|
|
$fieldsAll = $fieldHandler->getAll($crAddTypes); |
144
|
|
|
// Get All AddTypes |
145
|
|
|
foreach (\array_keys($fieldsAll) as $i) { |
146
|
|
|
$questionObj = $questionHandler->create(); |
147
|
|
|
$questionObj->setVar('evid', $addEvid); |
148
|
|
|
$questionObj->setVar('type', $fieldsAll[$i]->getVar('type')); |
149
|
|
|
$questionObj->setVar('caption', $fieldsAll[$i]->getVar('caption')); |
150
|
|
|
$questionObj->setVar('desc', $fieldsAll[$i]->getVar('desc')); |
151
|
|
|
$questionObj->setVar('values', (string)$fieldsAll[$i]->getVar('values')); |
152
|
|
|
$questionObj->setVar('placeholder', $fieldsAll[$i]->getVar('placeholder')); |
153
|
|
|
$questionObj->setVar('required', $fieldsAll[$i]->getVar('required')); |
154
|
|
|
$questionObj->setVar('print', $fieldsAll[$i]->getVar('print')); |
155
|
|
|
$questionObj->setVar('weight', $i); |
156
|
|
|
$questionObj->setVar('datecreated', \time()); |
157
|
|
|
$questionObj->setVar('submitter', $uidCurrent); |
158
|
|
|
// Insert Data |
159
|
|
|
$questionHandler->insert($questionObj); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Delete all questions for given event |
168
|
|
|
* @param int $evId |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function cleanupQuestions(int $evId) |
172
|
|
|
{ |
173
|
|
|
if ($evId > 0) { |
174
|
|
|
$crQuestion = new \CriteriaCompo(); |
175
|
|
|
$crQuestion->add(new \Criteria('evid', $evId)); |
176
|
|
|
$questionsCount = $this->getCount($crQuestion); |
177
|
|
|
if ($questionsCount > 0) { |
178
|
|
|
return $this->deleteAll($crQuestion); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* get all questions for given event |
186
|
|
|
* @param int $evId |
187
|
|
|
* @param bool $onlyPrintable |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
public function getQuestionsByEvent(int $evId, $onlyPrintable = true) |
191
|
|
|
{ |
192
|
|
|
$questionsArr = []; |
193
|
|
|
if ($evId > 0) { |
194
|
|
|
$crQuestion = new \CriteriaCompo(); |
195
|
|
|
$crQuestion->add(new \Criteria('evid', $evId)); |
196
|
|
|
$crQuestion->setSort('weight ASC, id'); |
197
|
|
|
$crQuestion->setOrder('DESC'); |
198
|
|
|
if ($onlyPrintable) { |
199
|
|
|
$crQuestion->add(new \Criteria('print', 1)); |
200
|
|
|
} |
201
|
|
|
$questionsCount = $this->getCount($crQuestion); |
202
|
|
|
if ($questionsCount > 0) { |
203
|
|
|
$questionsAll = $this->getAll($crQuestion); |
204
|
|
|
foreach (\array_keys($questionsAll) as $queId) { |
205
|
|
|
$questionsArr[$queId] = [ |
206
|
|
|
'caption' => $questionsAll[$queId]->getVar('caption'), |
207
|
|
|
'type' => $questionsAll[$queId]->getVar('type'), |
208
|
|
|
'values' => $questionsAll[$queId]->getVar('values') |
209
|
|
|
]; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
return $questionsArr; |
215
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @public function to get next value for sorting |
220
|
|
|
* @param int $evId |
221
|
|
|
* @return int |
222
|
|
|
*/ |
223
|
|
|
public function getNextWeight(int $evId) |
224
|
|
|
{ |
225
|
|
|
$nextValue = 0; |
226
|
|
|
|
227
|
|
|
$crQuestion = new \CriteriaCompo(); |
228
|
|
|
$crQuestion->add(new \Criteria('evid', $evId)); |
229
|
|
|
$crQuestion->setSort('weight'); |
230
|
|
|
$crQuestion->setOrder('DESC'); |
231
|
|
|
$crQuestion->setLimit(1); |
232
|
|
|
$questionsCount = $this->getCount($crQuestion); |
233
|
|
|
if ($questionsCount > 0) { |
234
|
|
|
$questionsAll = $this->getAll($crQuestion); |
235
|
|
|
foreach (\array_keys($questionsAll) as $queId) { |
236
|
|
|
$nextValue = $questionsAll[$queId]->getVar('weight'); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $nextValue + 1; |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
} |
245
|
|
|
|