-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsurvey-edit.links
435 lines (373 loc) · 11.4 KB
/
survey-edit.links
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
423
424
425
426
427
428
429
430
431
432
433
434
435
# This module is responsible for editing the answers given by respondents to a survey questions. To work properly two parameters must be given: ID of an respondent and ID of the survey. Then the information about the survey and current respondent answers are downloaded from database and displayed in a form.
# Variables used in database operations.
var db = database "surveybuilder";
var readysurveys =
table "surveys"
with (id : Int,
name : String,
textareasno : Int
) from db;
var textareas =
table "textareas"
with (
id : Int,
surveyid : Int,
questioncontent : String
) from db;
var textareaentries =
table "textareaentries"
with
(
id : Int,
textareaid : Int,
respondendid : Int,
answer : String
) from db;
var readyrespondents =
table "surveyrespondents"
with
(
id : Int,
surveyid : Int,
name : String
) from db;
var readycheckboxquestions =
table "checkboxquestions"
with (
id : Int,
surveyid : Int,
checkboxcontent : String
) from db;
var readycheckboxoptions =
table "checkboxoptions"
with (
id : Int,
questionid : Int,
optioncontent : String
) from db;
var readycheckboxdbentries =
table "checkboxentries"
with (
id : Int,
optionid : Int,
respondendid : Int,
optionvalue : Int
) from db;
var checkboxdbentries =
table "checkboxentries"
with (
optionid : Int,
respondendid : Int,
optionvalue : Int
) from db;
# This function id called after user clicks on 'Next' button and submits his/her answers. It prints out his/her decisions and enables asks if he/she is 100% sure of this answers. It is kind of bridge between writing/selecting answers to all the questions and storing them in a database.
fun editSummarize(inputData, surveyId, surveyName, respondentId)
{
var editFormlet = formlet
<#>
{
for(cbentry <- inputData.cbvalues)
{
<p><b>{stringToXml(cbentry.questiontxt)}</b>
<table>
{for(singleOption <- cbentry.answers)
{
if(singleOption.selv)
{
<tr><td><font color="green">{stringToXml(singleOption.otxt)}</font></td></tr>
}
else
{
<tr><td><font color="red">{stringToXml(singleOption.otxt)}</font></td></tr>
}
}
}
</table>
</p>
}
}
{
for(entry <- inputData.listofvalues)
{
<p><b>{stringToXml(entry.question)}</b><br></br><em>{stringToXml(entry.answer)}</em></p>
}
}
<p>If they are in line with your opinion please submit them by clicking on 'confirm' button. Otherwise go back.</p>
<p>{submitButton("confirm") -> submit}</p>
</#>
yields {
(id = surveyId , data = inputData)
};
page
<html>
<title>SingleSurveyEditor</title>
<body bgcolor="#ffffff">
<h1>{stringToXml(surveyName)}</h1>
<p>That is how do your answers look like now:</p>
{
editFormlet => fun (par) { confirmEdit(par, surveyName, respondentId) }
}
<hr />
</body>
</html>
}
# Updates an answer for a given question for a given respondent.
fun updatesinglerowTA(r, respondentId)
{
update (x <-- textareaentries)
where (x.textareaid == r.id && x.respondendid == respondentId)
set (
answer = r.answer
);
()
}
# Updates an answer for a given checkbox question option for a given respondent.
fun updateSingleCbEntry(optionId, respondentIt, checkedornot)
{
var asd = if(checkedornot)
{
update (x <-- checkboxdbentries)
where (x.optionid == optionId && x.respondendid == respondentIt)
set (
optionvalue = 1
)
}
else
{
update (x <-- checkboxdbentries)
where (x.optionid == optionId && x.respondendid == respondentIt)
set (
optionvalue = 0
)
};
()
}
# Calls all the update functions and in the end displays information about the success.
fun confirmEdit(inputValues, sName, respondentId)
{
var data = inputValues.data.listofvalues;
var someMagic = for(r <- data)
{
updatesinglerowTA(r, respondentId);
[]
};
var cbdata = inputValues.data.cbvalues;
var nameforfor = for (singleQuestion <- cbdata)
{
for (singleOpt <- singleQuestion.answers)
{
updateSingleCbEntry(singleOpt.oid,respondentId,singleOpt.selv);
[]
}
};
page
<html>
<title>SingleSurveyEditor</title>
<body bgcolor="#ffffff">
<h1>{stringToXml(sName)}</h1>
<p>Your answers have been successfully updated in our database !</p>
<br></br>
<img src="success.jpg" alt="" width="620" height="349"></img>
</body>
</html>
}
# Recursive function responsible for presenting answer options for the questions of a checkbox type.
fun checkboxesFormlet(cboptions)
{
if (cboptions == [])
formlet <#></#> yields []
else {
var cbopt = hd(cboptions).qco;
formlet <#>
<tr>
<td>{ stringToXml(hd(cboptions).qotxt) }</td>
<td> { checkboxDefault(cbopt) -> c } </td>
</tr>
{ checkboxesFormlet(tl(cboptions)) -> rest }
</#>
yields
("otxt"=hd(cboptions).qotxt,"oid"=hd(cboptions).qoid,"selv"=c)::rest
}
}
# Recursive function generating form to display survey questions and then gather information from user input.
fun surveyFormletCb(opt, questions, answers, checkboxes)
{
if (opt == 1)
{
if(checkboxes == [])
{
formlet <#>
{ surveyFormletCb(2,questions, answers,[]) -> rest }
</#>
yields {
rest
}
}
else
{
formlet <#>
<tr><td><b>{stringToXml(hd(checkboxes).qname)}</b></td></tr>
<tr class="blank_row"><td colspan="5"></td></tr>
{ checkboxesFormlet(hd(checkboxes).qo) -> useranswers}
<tr class="blank_row"><td colspan="5"></td></tr>
<tr class="blank_row"><td colspan="5"></td></tr>
<tr class="blank_row"><td colspan="5"></td></tr>
{ surveyFormletCb(opt,questions,answers,tl(checkboxes)) -> rest }
</#>
yields {
(listofvalues = rest.listofvalues, cbvalues = (questiontxt = hd(checkboxes).qname, answers = useranswers)::rest.cbvalues)
}
}
}
else
{
if (questions == [])
formlet <#>
<tr class="blank_row"><td colspan="5"></td></tr>
<tr class="blank_row"><td colspan="5"></td></tr>
<tr><td></td></tr>
<tr><td>{submitButton("Next") -> submit}</td></tr>
</#>
yields {
(listofvalues = [], cbvalues = [])
}
else {
formlet <#>
<tr>
<td> <b> {stringToXml(hd(questions).questioncontent)} </b> </td>
</tr>
<tr>
<td> {textareacss(hd(answers).answer,"width: 340px; height: 85px;") -> lanswer} </td>
</tr>
{ surveyFormletCb(opt,tl(questions),tl(answers),checkboxes) -> rest }
</#>
yields
(listofvalues = (id = hd(questions).id, answer = lanswer, question = hd(questions).questioncontent)::rest.listofvalues, cbvalues = [])
}
}
}
fun getSurvey(surveyselected)
{
if (length(surveyselected) > 0){
var selectedsurveyid = hd(surveyselected);
for (s <-- readysurveys)
where (s.id == stringToInt(selectedsurveyid))
[("id"=s.id, "name"=s.name, "textareasno"=s.textareasno)]
}
else
{
[]
}
}
fun getRespondent(respondentselected, selsurveyId)
{
if (length(respondentselected) > 0)
{
var selectedrespondentid = hd(respondentselected);
for (r <-- readyrespondents)
where (r.id == stringToInt(selectedrespondentid) && r.surveyid == selsurveyId)
[("id"=r.id, "surveyid"=r.surveyid, "name"=r.name)]
}
else
{
[]
}
}
fun getCbQuestions(surveyId)
{
for(cbquestion <- asList(readycheckboxquestions))
where(cbquestion.surveyid == surveyId)
[cbquestion]
}
fun getOptionsForQuestion(questionId, respondentId)
{
for(cboption <- asList(readycheckboxoptions))
where(cboption.questionid == questionId)
[("qoid"=cboption.id, "qotxt"=cboption.optioncontent, "qco"=getCurrentAnswerForOption(cboption.id, respondentId))]
}
fun getCurrentAnswerForOption(optionId, respondentId)
{
var listWithOneElement = for (cbentry <- asList(readycheckboxdbentries))
where(cbentry.optionid == optionId && cbentry.respondendid == respondentId)
[("optvalue" = cbentry.optionvalue)];
var currentOptionEntry = hd(listWithOneElement).optvalue;
if(currentOptionEntry == 1)
{
true
}
else
{
false
}
}
fun prepareCheckboxes(surveyId, respondentId)
{
if(surveyId <> 0)
{
var cbQuestions = getCbQuestions(surveyId);
var outArray = for (singleQuestion <- cbQuestions)
{
var qo = getOptionsForQuestion(singleQuestion.id, respondentId);
[("qname"=singleQuestion.checkboxcontent,"qo"=qo)]
};
outArray
}
else
{
[]
}
}
fun main() {
var env = environment();
var srvopt = for ((x,y) <- env) where (x == "surveyId") [y];
var rspndntopt = for ((x,y) <- env) where (x == "respondentId") [y];
var srvnLst = getSurvey(srvopt);
var selectedsrv = if (length(srvnLst) > 0)
{
hd(srvnLst)
}
else
{
("id"=0, "name"="takiegoimienianiktnienada", "textareasno"=0)
};
var rspndntLst = getRespondent(rspndntopt,selectedsrv.id);
var selectedrspndnt = if (length(rspndntLst) > 0)
{
hd(rspndntLst)
}
else
{
("id"=0, "surveyid"=0, "name"="atakiegotojuznapewnonikt")
};
if (selectedrspndnt.id <> 0)
{
var surveyquestions = query { for (rquestion <-- textareas) where (rquestion.surveyid == selectedsrv.id) [rquestion] };
var surveyanswers = query { for (ranswer <-- textareaentries) where (ranswer.respondendid == selectedrspndnt.id) [ranswer] };
var checkboxes = prepareCheckboxes(selectedsrv.id, selectedrspndnt.id);
var startValue = 1;
var ep = page <table> { ( surveyFormletCb(startValue,surveyquestions, surveyanswers, checkboxes) ) => fun (r) {editSummarize(r,selectedsrv.id,selectedsrv.name,selectedrspndnt.id)} } </table>;
page
<html>
<title>SingleSurveyEditor</title>
<body bgcolor="#ffffff">
<h1>{stringToXml(selectedsrv.name)}</h1>
<p>Below there is placed a form of the survey so that you can improve your answers. We are glad of your engagement.</p>
{|ep|}
<hr />
</body>
</html>
}
else
{
page
<html>
<title>SingleSurveyEditor</title>
<body bgcolor="#ffffff">
<h1>Welcome to the single survey editor of the SurveyBuilder application !</h1>
<p>Please provide a proper both survey ID and respondent ID as an HTML parameters. To do that you should have in your URL something like that: "survey-edit.links?surveyId=<em>yourSurveyId</em>&respondentId=<em>yourRespondentId</em>".</p>
<p> If you don't know what I'm talking about, then you haven't been asked to edit this survey.</p>
</body>
</html>
}
}
main()