-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
422 lines (354 loc) · 14.6 KB
/
controller.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
414
415
416
417
418
419
420
421
422
<?
class FormBlockController extends BlockController {
protected $btDescription = "Build simple forms and surveys.";
public $btName = "Form";
public $btTable = 'btForm';
public $btQuestionsTablename = 'btFormQuestions';
public $btAnswerSetTablename = 'btFormAnswerSet';
public $btAnswersTablename = 'btFormAnswers';
public $btInterfaceWidth = '420';
public $btInterfaceHeight = '430';
public function __construct($b = null){
parent::__construct($b);
//$this->bID = intval($this->_bID);
}
//form add or edit submit
function save( $data=array() ) {
if( !$data || count($data)==0 ) $data=$_POST;
$db = Loader::db();
if(intval($this->bID)>0){
$q = "select count(*) as total from {$this->btTable} where bID = ".intval($this->bID);
$total = $db->getOne($q);
}else $total = 0;
$v = array( $data['qsID'], $data['surveyName'], intval($data['notifyMeOnSubmission']), $data['recipientEmail'], intval($this->bID) );
$q = ($total > 0) ? "update {$this->btTable} set questionSetId = ?, surveyName=?, notifyMeOnSubmission=?, recipientEmail=? where bID = ?"
: "insert into {$this->btTable} (questionSetId,surveyName, notifyMeOnSubmission, recipientEmail, bID) values (?, ?, ?, ?, ?)";
$rs = $db->query($q,$v);
//Add Questions (for programmatically creating forms, such as during the site install)
if( count($data['questions'])>0 ){
$miniSurvey = new MiniSurvey();
foreach( $data['questions'] as $questionData )
$miniSurvey->addEditQuestion($questionData,0);
}
return true;
}
function duplicate($newBID) {
$db = Loader::db();
$v = array($this->bID);
$q = "select * from {$this->btTable} where bID = ? LIMIT 1";
$r = $db->query($q, $v);
$row = $r->fetchRow();
if(count($row)>0){
$oldQuestionSetId=$row['questionSetId'];
$newQuestionSetId=time();
//duplicate survey block record
$v = array($newQuestionSetId,$row['surveyName'],$newBID);
$q = "insert into {$this->btTable} (questionSetId,surveyName, , bID) values (?, ?, ?)";
//duplicate questions records
$rs=$db->query("SELECT * FROM {$this->btQuestionsTablename} WHERE questionSetId=$oldQuestionSetId");
while( $row=$rs->fetchRow() ){
$v=array($newQuestionSetId,$row['question'],$row['inputType'],$row['options'],$row['position']);
$sql='INSERT INTO {$this->btQuestionsTablename} (questionSetId,question,inputType,options,position) VALUES (!,?,?,?,!)';
}
}
}
//users submits the completed survey
function action_submit_form() {
$db = Loader::db();
//question set id
$qsID=intval($_POST['qsID']);
if($qsID==0)
throw new Exception("Oops, something is wrong with the form you posted (it doesn't have a question set id).");
//save main survey record
$q="insert into {$this->btAnswerSetTablename} (questionSetId) values (?)";
$db->query($q,array($qsID));
$answerSetID=$db->Insert_ID();
//get all questions for this question set
$rs=$db->query("SELECT * FROM {$this->btQuestionsTablename} WHERE questionSetId=$qsID");
$questionAnswerPairs=array();
//loop through each question and get the answers
$nl_name = "";
$nl_email = "";
while( $row=$rs->fetchRow() ){
//save each answer
if($row['inputType']=='text'){
$answerLong=$_POST['Question'.$row['msqID']];
$answer='';
}else{
$answerLong="";
$answer=$_POST['Question'.$row['msqID']];
}
$questionAnswerPairs[$row['msqID']]['question']=$row['question'];
$questionAnswerPairs[$row['msqID']]['answer']=$_POST['Question'.$row['msqID']];
if(stristr($row['question'],"name"))
{
$nl_name = $_POST['Question'.$row['msqID']];
}
if(stristr($row['question'],"email"))
{
$nl_email = $_POST['Question'.$row['msqID']];
}
if($nl_name != "" && $nl_email != "")
{
$v = array($nl_email);
$q = "SELECT * FROM btNLetter WHERE email = ?";
$f = $db->query($q, $v);
if(!$f->FetchRow())
{
$v = array($nl_name, $nl_email, 0, 0, $_SERVER['REMOTE_ADDR']);
$q = "insert into btNLetter (name, email, bID, uID, ipAddress) values (?, ?, ?, ?, ?)";
$db->query($q, $v);
}
}
if( is_array($answer) )
$answer=join(',',$answer);
$v=array($row['msqID'],$answerSetID,$answer,$answerLong);
$q="insert into {$this->btAnswersTablename} (msqID,asID,answer,answerLong) values (?,?,?,?)";
$db->query($q,$v);
}
$refer_uri=$_POST['pURI'];
if(!strstr($refer_uri,'?')) $refer_uri.='?';
if(intval($this->notifyMeOnSubmission)>0){
$mh = Loader::helper('mail');
$mh->to( $this->recipientEmail );
$mh->addParameter('formName', $this->surveyName);
$mh->addParameter('questionSetId', $this->questionSetId);
$mh->addParameter('questionAnswerPairs', $questionAnswerPairs);
ob_start();
$mh->load('block_form_submission');
$mh->setBody(ob_get_contents());
ob_end_clean();
$mh->setSubject($this->surveyName.' Form Submission');
//echo $mh->body.'<br>';
@$mh->sendMail();
}
header("Location: ".$refer_uri."&surveySuccess=1&qsid=".$this->questionSetId);
die;
}
function delete() {
$db = Loader::db();
$miniSurvey=new MiniSurvey();
$info=$miniSurvey->getMiniSurveyBlockInfo($this->bID);
//get all answer sets
$q = "SELECT asID FROM {$this->btAnswerSetTablename} WHERE questionSetId = ".intval($info['questionSetId']);
$answerSetsRS = $db->query($q);
//delete the answers
while( $answerSet=$answerSetsRS->fetchRow() ){
$q = "delete from {$this->btAnswersTablename} where asID = ".intval( $answerSet['asID'] );
$r = $db->query($q);
}
//delete the answer sets
$q = "delete from {$this->btAnswerSetTablename} where questionSetId = ".intval($info['questionSetId']);
$r = $db->query($q);
//delete the questions
$q = "delete from {$this->btQuestionsTablename} where questionSetId = ".intval($info['questionSetId']);
$r = $db->query($q);
//delete the form block
$q = "delete from {$this->btTable} where bID = '{$this->bID}'";
$r = $db->query($q);
}
}
class FormBlockStatistics {
public static function getTotalSubmissions($date = null) {
$db = Loader::db();
if ($date != null) {
return $db->GetOne("select count(asID) from btFormAnswerSet where DATE_FORMAT(created, '%Y-%m-%d') = ?", array($date));
} else {
return $db->GetOne("select count(asID) from btFormAnswerSet");
}
}
public static function loadSurveys($MiniSurvey){
$db = Loader::db();
return $db->query('SELECT * FROM '.$MiniSurvey->btTable.' AS s');
}
public static function buildAnswerSetsArray( $questionSet, $limit='' ){
$db = Loader::db();
if( strlen(trim($limit))>0 && !strstr(strtolower($limit),'limit') )
$limit=' LIMIT '.$limit;
//get answers sets
$sql='SELECT * FROM btFormAnswerSet AS aSet '.
'WHERE aSet.questionSetId='.$questionSet.' ORDER BY created DESC '.$limit;
$answerSetsRS=$db->query($sql);
//load answers into a nicer multi-dimensional array
$answerSets=array();
$answerSetIds=array(0);
while( $answer = $answerSetsRS->fetchRow() ){
//answer set id - question id
$answerSets[$answer['asID']]=$answer;
$answerSetIds[]=$answer['asID'];
}
//get answers
$sql='SELECT * FROM btFormAnswers AS a WHERE a.asID IN ('.join(',',$answerSetIds).')';
$answersRS=$db->query($sql);
//load answers into a nicer multi-dimensional array
while( $answer = $answersRS->fetchRow() ){
//answer set id - question id
$answerSets[$answer['asID']]['answers'][$answer['msqID']]=$answer;
}
return $answerSets;
}
}
class MiniSurvey{
public $btTable = 'btForm';
public $btQuestionsTablename = 'btFormQuestions';
public $btAnswerSetTablename = 'btFormAnswerSet';
public $btAnswersTablename = 'btFormAnswers';
function MiniSurvey(){
$db = Loader::db();
$this->db=$db;
}
function addEditQuestion($values,$withOutput=1){
$jsonVals=array();
$values['options']=str_replace(array("\r","\n"),'%%',$values['options']);
//set question set id, or create a new one if none exists
if(intval($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(intval($values['msqID'])>0){
$dataValues=array(intval($values['qsID']), trim($values['question']), $values['inputType'],
$values['options'], intval($values['position']), intval($values['msqID']) );
$sql='UPDATE btFormQuestions SET questionSetId=?, question=?, inputType=?, options=?, position=? WHERE msqID=?';
$jsonVals['mode']='"Edit"';
}else{
$dataValues=array( intval($values['qsID']), trim($values['question']), $values['inputType'],
$values['options'], 1000 );
$sql='INSERT INTO btFormQuestions (questionSetId,question,inputType,options,position) VALUES (?,?,?,?,?)';
$jsonVals['mode']='"Add"';
}
$result=$this->db->query($sql,$dataValues);
$jsonVals['success']=1;
}
$jsonVals['qsID']=$values['qsID'];
//create json response object
$jsonPairs=array();
foreach($jsonVals as $key=>$val) $jsonPairs[]=$key.':'.$val;
if($withOutput) echo '{'.join(',',$jsonPairs).'}';
}
function getQuestionInfo($qsID,$msqID){
$questionRS=$this->db->query('SELECT * FROM btFormQuestions WHERE questionSetId='.intval($qsID).' AND msqID='.intval($msqID).' LIMIT 1' );
$questionRow=$questionRS->fetchRow();
$jsonPairs=array();
foreach($questionRow as $key=>$val){
if($key=='options') $key='optionVals';
$jsonPairs[]=$key.':"'.str_replace(array("\r","\n"),'%%',addslashes($val)).'"';
}
echo '{'.join(',',$jsonPairs).'}';
}
function deleteQuestion($qsID,$msqID){
$sql='DELETE FROM btFormQuestions WHERE questionSetId='.intval($qsID).' AND msqID='.intval($msqID);
$this->db->query($sql,$dataValues);
}
static function loadQuestions($qsID){
$db = Loader::db();
return $db->query('SELECT * FROM btFormQuestions WHERE questionSetId='.intval($qsID).' ORDER BY position');
}
static function getAnswerCount($qsID){
$db = Loader::db();
return $db->getOne( 'SELECT count(*) FROM btFormAnswerSet WHERE questionSetId='.intval($qsID) );
}
function loadSurvey($qsID,$showEdit=false){
//loading questions
$questionsRS=self::loadQuestions($qsID);
if(!$showEdit){
echo '<table class="formBlockSurveyTable">';
while( $questionRow=$questionsRS->fetchRow() ){
echo '<tr>';
if ($questionRow['inputType']=="title")
{echo '<td colspan=2 valign="top" class="question"><h3>'.$questionRow['question'].'</h3></td>';}
elseif ($questionRow['inputType']=="spacer")
{echo '<td colspan=2 valign="top" class="question"><hr></td>';}
elseif ($questionRow['inputType']=="normal")
{echo '<td colspan=2 valign="top" class="question">'.$questionRow['question'].'</td>';}
else
{echo '<td valign="top" class="question">'.$questionRow['question'].'</td>';
echo '<td valign="top">'.$this->loadInputType($questionRow,$showEdit).'</td>';}
echo '</tr>';
}
echo '<tr><td> </td><td><input class="formBlockSubmitButton" name="Submit" type="submit" value="Submit" /></td></tr>';
echo '</table>';
}else{
echo '<div id="miniSurveyTableWrap"><div id="miniSurveyPreviewTable" class="miniSurveyTable">';
while( $questionRow=$questionsRS->fetchRow() ){ ?>
<div id="miniSurveyQuestionRow<?=$questionRow['msqID']?>" class="miniSurveyQuestionRow">
<div class="miniSurveyQuestion"><?=$questionRow['question']?></div>
<? /* <div class="miniSurveyResponse"><?=$this->loadInputType($questionRow,$showEdit)?></div> */ ?>
<div class="miniSurveyOptions">
<div style="float:right">
<a href="#" onclick="miniSurvey.moveUp(this,<?=$questionRow['msqID']?>);return false" class="moveUpLink"></a>
<a href="#" onclick="miniSurvey.moveDown(this,<?=$questionRow['msqID']?>);return false" class="moveDownLink"></a>
</div>
<a href="#" onclick="miniSurvey.reloadQuestion(<?=$questionRow['msqID']?>);return false">edit</a>
<a href="#" onclick="miniSurvey.deleteQuestion(this,<?=$questionRow['msqID']?>);return false">remove</a>
</div>
<div class="miniSurveySpacer"></div>
</div>
<? }
echo '</div></div>';
}
}
function loadInputType($questionData,$showEdit){
$options=explode('%%',$questionData['options']);
switch($questionData['inputType']){
case 'list':
// return 'what to do with lists?';
foreach($options as $option)
$html.= '<option >'.trim($option).'</option>';
return '<select name="Question'.$questionData['msqID'].'[]" size="4" multiple >'.$html.'</select>';
case 'select':
if($this->frontEndMode)
$html.= '<option value="">----</option>';
foreach($options as $option)
$html.= '<option >'.trim($option).'</option>';
return '<select name="Question'.$questionData['msqID'].'" >'.$html.'</select>';
case 'radios':
$X = 0;
foreach($options as $option){
if(strlen(trim($option))==0) continue;
$html.= '<div class="radioPair"><input name="Question'.$questionData['msqID'].'" type="radio" value="'.trim($option).'"';
if($X == 0) $html.= ' checked';
$X += 1;
$html.= ' /> '.$option.'</div>';
}
return $html;
case 'check':
$X = 0;
foreach($options as $option){
if(strlen(trim($option))==0) continue;
$html.= '<div class="radioPair"><input name="Question'.$questionData['msqID'].'[]" type="checkbox" value="'.trim($option).'"';
if($X == 0) $html.= ' checked';
$X += 1;
$html.= ' /> '.$option.'</div>';
}
return $html;
case 'text':
return '<textarea name="Question'.$questionData['msqID'].'" cols="50" rows="4" style="width:95%"></textarea>';
case 'title':
return '<input name="Question'.$questionData['msqID'].'" type="hidden" />';
case 'normal':
return '<input name="Question'.$questionData['msqID'].'" type="hidden" />';
case 'field':
default:
return '<input name="Question'.$questionData['msqID'].'" type="field" />';
}
}
function getMiniSurveyBlockInfo($bID){
$rs=$this->db->query('SELECT * FROM btForm WHERE bID='.intval($bID).' LIMIT 1' );
return $rs->fetchRow();
}
function reorderQuestions($qsID=0,$qIDs){
$qIDs=explode(',',$qIDs);
if(!is_array($qIDs)) $qIDs=array($qIDs);
$positionNum=0;
foreach($qIDs as $qID){
$vals=array( $positionNum,intval($qID), intval($qsID) );
$sql='UPDATE btFormQuestions SET position=? WHERE msqID=? AND questionSetId=?';
$rs=$this->db->query($sql,$vals);
$positionNum++;
}
}
}
?>