/
mini_survey.php
413 lines (362 loc) · 18.8 KB
/
mini_survey.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
namespace Concrete\Block\Form;
use Core;
use Database;
use Request;
class MiniSurvey
{
public $btTable = 'btForm';
public $btQuestionsTablename = 'btFormQuestions';
public $btAnswerSetTablename = 'btFormAnswerSet';
public $btAnswersTablename = 'btFormAnswers';
public $lastSavedMsqID = 0;
public $lastSavedqID = 0;
public function __construct()
{
$this->db = Database::connection();
}
public function addEditQuestion($values, $withOutput = 1)
{
$jsonVals = [];
$values['options'] = str_replace(["\r", "\n"], '%%', $values['options']);
if (strtolower($values['inputType']) == 'undefined') {
$values['inputType'] = 'field';
}
//set question set id, or create a new one if none exists
if ((int) ($values['qsID']) == 0) {
$values['qsID'] = time();
}
//validation
if (strlen($values['question']) == 0 || strlen($values['inputType']) == 0 || $values['inputType'] == 'null') {
//complete required fields
$jsonVals['success'] = 0;
$jsonVals['noRequired'] = 1;
} else {
if ((int) ($values['msqID'])) {
$jsonVals['mode'] = '"Edit"';
//questions that are edited are given a placeholder row in btFormQuestions with bID=0, until a bID is assign on block update
$pendingEditExists = $this->db->fetchColumn('select count(*) as total from btFormQuestions where bID=0 AND msqID=' . (int) ($values['msqID']));
//hideQID tells the interface to hide the old version of the question in the meantime
$vals = [(int) ($values['msqID'])];
$jsonVals['hideQID'] = (int) ($this->db->fetchColumn('SELECT MAX(qID) FROM btFormQuestions WHERE bID!=0 AND msqID=?', $vals));
} else {
$jsonVals['mode'] = '"Add"';
$pendingEditExists = false;
}
//see if the 'send notification from' checkbox is checked and save this to the options field
if ($values['inputType'] == 'email') {
$options = [];
if (array_key_exists('send_notification_from', $values) && $values['send_notification_from'] == 1) {
$options['send_notification_from'] = 1;
} else {
$options['send_notification_from'] = 0;
}
$values['options'] = serialize($options);
}
if ($pendingEditExists) {
$width = $height = 0;
if ($values['inputType'] == 'text') {
$width = $this->limitRange((int) ($values['width']), 20, 500);
$height = $this->limitRange((int) ($values['height']), 1, 100);
}
$dataValues = [
(int) ($values['qsID']),
trim($values['question']),
$values['inputType'],
$values['options'],
(int) ($values['position']),
$width,
$height,
(int) ($values['required']),
$values['defaultDate'],
(int) ($values['msqID']),
];
$sql = 'UPDATE btFormQuestions SET questionSetId=?, question=?, inputType=?, options=?, position=?, width=?, height=?, required=?, defaultDate=? WHERE msqID=? AND bID=0';
} else {
if (!isset($values['position'])) {
$values['position'] = 1000;
}
if (!(int) ($values['msqID'])) {
$values['msqID'] = (int) ($this->db->fetchColumn('SELECT MAX(msqID) FROM btFormQuestions') + 1);
}
$dataValues = [
$values['msqID'],
(int) ($values['qsID']),
trim($values['question']),
$values['inputType'],
$values['options'],
(int) ($values['position']),
(int) ($values['width']),
(int) ($values['height']),
(int) ($values['required']),
$values['defaultDate'],
];
$sql = 'INSERT INTO btFormQuestions (msqID,questionSetId,question,inputType,options,position,width,height,required,defaultDate) VALUES (?,?,?,?,?,?,?,?,?,?)';
}
$result = $this->db->executeQuery($sql, $dataValues);
$this->lastSavedMsqID = (int) ($values['msqID']);
$this->lastSavedqID = (int) ($this->db->fetchColumn('SELECT MAX(qID) FROM btFormQuestions WHERE bID=0 AND msqID=?', [$values['msqID']]));
$jsonVals['qID'] = $this->lastSavedqID;
$jsonVals['success'] = 1;
}
$jsonVals['qsID'] = $values['qsID'];
$jsonVals['msqID'] = (int) ($values['msqID']);
//create json response object
$jsonPairs = [];
foreach ($jsonVals as $key => $val) {
$jsonPairs[] = $key . ':' . $val;
}
if ($withOutput) {
echo '{' . implode(',', $jsonPairs) . '}';
}
}
public function getQuestionInfo($qsID, $qID)
{
$questionRS = $this->db->executeQuery('SELECT * FROM btFormQuestions WHERE questionSetId=' . (int) $qsID . ' AND qID=' . (int) $qID . ' LIMIT 1');
$questionRow = $questionRS->fetch();
$jsonPairs = [];
foreach ($questionRow as $key => $val) {
if ($key == 'options') {
$key = 'optionVals';
if ($questionRow['inputType'] == 'email') {
$options = unserialize($val);
if (is_array($options)) {
foreach ($options as $o_key => $o_val) {
$val = $o_key . '::' . $o_val . ';';
}
}
}
}
$jsonPairs[] = $key . ':"' . str_replace(["\r", "\n"], '%%', addslashes($val)) . '"';
}
echo '{' . implode(',', $jsonPairs) . '}';
}
public function deleteQuestion($qsID, $msqID)
{
$sql = 'DELETE FROM btFormQuestions WHERE questionSetId=' . (int) $qsID . ' AND msqID=' . (int) $msqID . ' AND bID=0';
$this->db->executeQuery($sql);
}
public function loadQuestions($qsID, $bID = 0, $showPending = 0)
{
$db = Database::connection();
if ((int) $bID) {
$bIDClause = ' AND ( bID=' . (int) $bID . ' ';
if ($showPending) {
$bIDClause .= ' OR bID=0) ';
} else {
$bIDClause .= ' ) ';
}
} else {
$bIDClause = '';
}
return $db->executeQuery('SELECT * FROM btFormQuestions WHERE questionSetId=' . (int) $qsID . ' ' . $bIDClause . ' ORDER BY position, msqID');
}
public static function getAnswerCount($qsID)
{
$db = Database::connection();
return $db->fetchColumn('SELECT count(*) FROM btFormAnswerSet WHERE questionSetId=' . (int) $qsID);
}
public function loadSurvey($qsID, $showEdit = false, $bID = 0, $hideQIDs = [], $showPending = 0, $editmode = 0)
{
//loading questions
$questionsRS = $this->loadQuestions($qsID, $bID, $showPending);
if (!$showEdit) {
echo '<div>';
while ($questionRow = $questionsRS->fetch()) {
if (in_array($questionRow['qID'], $hideQIDs)) {
continue;
}
// this special view logic for the checkbox list isn't doing it for me
/*
if ($questionRow['inputType'] == 'checkboxlist' && strpos($questionRow['options'], '%%') === false) {
echo '<tr>
<td valign="top" colspan="2" class="question">
<div class="checkboxItem">
<div class="checkboxPair">'.$this->loadInputType($questionRow,$showEdit).$questionRow['question'].'</div>
</div>
</td>
</tr>';
} else {
*/
$requiredSymbol = ($questionRow['required']) ? ' <span class="required">*</span>' : '';
echo '<div class="form-group">
<label class="control-label" for="Question' . (int) ($questionRow['msqID']) . '">' . $questionRow['question'] . '' . $requiredSymbol . '</label></td>
<div>' . $this->loadInputType($questionRow, $showEdit) . '</div>
</div>';
//}
}
$surveyBlockInfo = $this->getMiniSurveyBlockInfoByQuestionId($qsID, (int) $bID);
if (!strlen($surveyBlockInfo['submitText'])) {
$surveyBlockInfo['submitText'] = 'Submit';
}
if (!empty($surveyBlockInfo['displayCaptcha'])) {
echo '<div class="ccm-edit-mode-disabled-item">' . t('Form Captcha') . '</div><br/>';
}
if ($editmode) {
echo '<div class="form-group"><input class="btn btn-primary" name="Submit" type="button" value="' . t($surveyBlockInfo['submitText']) . '" /></div>'; //make this a button
} else {
echo '<div class="form-group"><input class="btn btn-primary" name="Submit" type="submit" value="' . t($surveyBlockInfo['submitText']) . '" /></div>';
}
echo '</div>';
} else {
echo '<div id="miniSurveyTableWrap"><ul id="miniSurveyPreviewTable" class="list-group">';
while ($questionRow = $questionsRS->fetch()) {
if (in_array($questionRow['qID'], $hideQIDs)) {
continue;
}
$requiredSymbol = ($questionRow['required']) ? '<span class="required">*</span>' : '';
?>
<li id="miniSurveyQuestionRow<?php echo $questionRow['msqID']?>" class="miniSurveyQuestionRow list-group-item">
<div class="miniSurveyQuestion"><?php echo $questionRow['question'] . ' ' . $requiredSymbol?></div>
<?php /* <div class="miniSurveyResponse"><?php echo $this->loadInputType($questionRow,$showEdit)?></div> */ ?>
<div class="miniSurveyOptions">
<a href="javascript:void(0)" class="ccm-icon-wrapper" onclick="miniSurvey.moveUp(this,<?php echo $questionRow['msqID']?>);return false"><i class="fa fa-chevron-up"></i></a>
<a href="javascript:void(0)" class="ccm-icon-wrapper" onclick="miniSurvey.moveDown(this,<?php echo $questionRow['msqID']?>);return false"><i class="fa fa-chevron-down"></i></a>
<a href="javascript:void(0)" class="ccm-icon-wrapper" onclick="miniSurvey.reloadQuestion(<?=(int) ($questionRow['qID']) ?>);return false"><i class="fa fa-pencil"></i></a>
<a href="javascript:void(0)" class="ccm-icon-wrapper" onclick="miniSurvey.deleteQuestion(this,<?=(int) ($questionRow['msqID']) ?>,<?=(int) ($questionRow['qID'])?>);return false"><i class="fa fa-trash"></i></a>
</div>
<div class="clearfix"></div>
</li>
<?php
}
echo '</div></div>';
}
}
public function loadInputType($questionData, $showEdit)
{
$options = explode('%%', $questionData['options']);
$defaultDate = $questionData['defaultDate'];
$msqID = (int) ($questionData['msqID']);
$datetime = Core::make('helper/form/date_time');
$html = '';
switch ($questionData['inputType']) {
case 'checkboxlist':
// this is looking really crappy so i'm going to make it behave the same way all the time - andrew
/*
if (count($options) == 1) {
if(strlen(trim($options[0]))==0) continue;
$checked=(Request::request('Question'.$msqID.'_0')==trim($options[0]))?'checked':'';
$html.= '<input name="Question'.$msqID.'_0" type="checkbox" value="'.trim($options[0]).'" '.$checked.' />';
} else {
*/
$html .= '<div class="checkboxList">' . "\r\n";
for ($i = 0; $i < count($options); ++$i) {
if (strlen(trim($options[$i])) == 0) {
continue;
}
$checked = (Request::request('Question' . $msqID . '_' . $i) == trim($options[$i])) ? 'checked' : '';
$html .= ' <div class="checkbox"><label><input name="Question' . $msqID . '_' . $i . '" type="checkbox" value="' . trim($options[$i]) . '" ' . $checked . ' /> <span>' . $options[$i] . '</span></label></div>' . "\r\n";
}
$html .= '</div>';
//}
return $html;
case 'select':
if ($this->frontEndMode) {
$selected = (!Request::request('Question' . $msqID)) ? 'selected="selected"' : '';
$html .= '<option value="" ' . $selected . '>----</option>';
}
foreach ($options as $option) {
$checked = (Request::request('Question' . $msqID) == trim($option)) ? 'selected="selected"' : '';
$html .= '<option ' . $checked . '>' . trim($option) . '</option>';
}
return '<select class="form-control" name="Question' . $msqID . '" id="Question' . $msqID . '" >' . $html . '</select>';
case 'radios':
foreach ($options as $option) {
if (strlen(trim($option)) == 0) {
continue;
}
$checked = (Request::request('Question' . $msqID) == trim($option)) ? 'checked' : '';
$html .= '<div class="radio"><label><input name="Question' . $msqID . '" type="radio" value="' . trim($option) . '" ' . $checked . ' /> <span>' . $option . '</span></label></div>';
}
return $html;
case 'fileupload':
$html = '<input type="file" name="Question' . $msqID . '" class="form-control" id="Question' . $msqID . '" />';
return $html;
case 'text':
$val = (Request::request('Question' . $msqID)) ? Core::make('helper/text')->entities(Request::request('Question' . $msqID)) : '';
return '<textarea name="Question' . $msqID . '" class="form-control" id="Question' . $msqID . '" cols="' . $questionData['width'] . '" rows="' . $questionData['height'] . '">' . $val . '</textarea>';
case 'url':
$val = (Request::request('Question' . $msqID)) ? Request::request('Question' . $msqID) : '';
return '<input name="Question' . $msqID . '" id="Question' . $msqID . '" class="form-control" type="url" value="' . stripslashes(htmlspecialchars($val)) . '" />';
case 'telephone':
$val = (Request::request('Question' . $msqID)) ? Request::request('Question' . $msqID) : '';
return '<input name="Question' . $msqID . '" id="Question' . $msqID . '" class="form-control" type="tel" value="' . stripslashes(htmlspecialchars($val)) . '" />';
case 'email':
$val = (Request::request('Question' . $msqID)) ? Request::request('Question' . $msqID) : '';
return '<input name="Question' . $msqID . '" id="Question' . $msqID . '" class="form-control" type="email" value="' . stripslashes(htmlspecialchars($val)) . '" />';
case 'date':
$val = (Request::request('Question' . $msqID)) ? Request::request('Question' . $msqID) : $defaultDate;
return $datetime->date('Question' . $msqID, $val);
case 'datetime':
$val = Request::request('Question' . $msqID);
if (!isset($val)) {
if (
Request::request('Question' . $msqID . '_dt') && Request::request('Question' . $msqID . '_h')
&& Request::request('Question' . $msqID . '_m') && Request::request('Question' . $msqID . '_a')
) {
$val = Request::request('Question' . $msqID . '_dt') . ' ' . Request::request('Question' . $msqID . '_h')
. ':' . Request::request('Question' . $msqID . '_m') . ' ' . Request::request('Question' . $msqID . '_a');
} else {
$val = $defaultDate;
}
}
return $datetime->datetime('Question' . $msqID, $val);
case 'field':
default:
$val = (Request::request('Question' . $msqID)) ? Request::request('Question' . $msqID) : '';
return '<input name="Question' . $msqID . '" id="Question' . $msqID . '" class="form-control" type="text" value="' . stripslashes(htmlspecialchars($val)) . '" />';
}
}
public function getMiniSurveyBlockInfo($bID)
{
$rs = $this->db->executeQuery('SELECT * FROM btForm WHERE bID=' . (int) $bID . ' LIMIT 1');
return $rs->fetch();
}
public function getMiniSurveyBlockInfoByQuestionId($qsID, $bID = 0)
{
$sql = 'SELECT * FROM btForm WHERE questionSetId=' . (int) $qsID;
if ((int) $bID > 0) {
$sql .= ' AND bID=' . $bID;
}
$sql .= ' LIMIT 1';
$rs = $this->db->executeQuery($sql);
return $rs->fetch();
}
public function reorderQuestions($qsID = 0, $qIDs)
{
$qIDs = explode(',', $qIDs);
if (!is_array($qIDs)) {
$qIDs = [$qIDs];
}
$positionNum = 0;
foreach ($qIDs as $qID) {
$vals = [$positionNum, (int) $qID, (int) $qsID];
$sql = 'UPDATE btFormQuestions SET position=? WHERE msqID=? AND questionSetId=?';
$rs = $this->db->executeQuery($sql, $vals);
++$positionNum;
}
}
public function limitRange($val, $min, $max)
{
$val = ($val < $min) ? $min : $val;
$val = ($val > $max) ? $max : $val;
return $val;
}
//Run on Form block edit
public static function questionCleanup($qsID = 0, $bID = 0)
{
$db = Database::connection();
//First make sure that the bID column has been set for this questionSetId (for backwards compatibility)
$vals = [(int) $qsID];
$questionsWithBIDs = $db->fetchColumn('SELECT count(*) FROM btFormQuestions WHERE bID!=0 AND questionSetId=? ', $vals);
//form block was just upgraded, so set the bID column
if (!$questionsWithBIDs) {
$vals = [(int) $bID, (int) $qsID];
$rs = $db->executeQuery('UPDATE btFormQuestions SET bID=? WHERE bID=0 AND questionSetId=?', $vals);
return;
}
//Then remove all temp/placeholder questions for this questionSetId that haven't been assigned to a block
$vals = [(int) $qsID];
$rs = $db->executeQuery('DELETE FROM btFormQuestions WHERE bID=0 AND questionSetId=?', $vals);
}
}