/
survey.py
1173 lines (1044 loc) · 43.1 KB
/
survey.py
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
survey - Assessment Data Analysis Tool
For more details see the blueprint at:
http://eden.sahanafoundation.org/wiki/BluePrint/SurveyTool/ADAT
@todo: open template from the dataTables into the section tab not update
@todo: in the pages that add a link to a template make the combobox display the label not the numbers
The model for this is in templates.IFRC.survey.py
"""
if not settings.has_module(c):
raise HTTP(404, body="Module disabled: %s" % c)
# -----------------------------------------------------------------------------
def index():
""" Module's Home Page """
module_name = settings.modules[c].get("name_nice")
response.title = module_name
return {"module_name": module_name,
}
# -----------------------------------------------------------------------------
def create():
"""
Enter a new assessment.
- provides a simpler URL to access from mobile devices...
"""
redirect(URL(f="new_assessment.iframe",
vars={"viewing": "survey_series.%s" % request.args[0]}))
# -----------------------------------------------------------------------------
def template():
""" RESTful CRUD controller """
# Load Model
#table = s3db.survey_template
def prep(r):
if r.component:
if r.component_name == "translate":
table = s3db.survey_translate
if r.component_id == None:
# list existing translations and allow the addition of a new translation
table.file.readable = False
table.file.writable = False
else:
# edit the selected translation
table.language.writable = False
table.code.writable = False
# remove CRUD generated buttons in the tabs
s3db.configure("survey_translate",
deletable=False)
else:
table = r.table
s3_action_buttons(r)
# Status of Pending
rows = db(table.status == 1).select(table.id)
try:
s3.actions[1]["restrict"].extend(str(row.id) for row in rows)
except KeyError: # the restrict key doesn't exist
s3.actions[1]["restrict"] = [str(row.id) for row in rows]
except IndexError: # the delete buttons doesn't exist
pass
# Add some highlighting to the rows
# Status of Pending
s3.dataTableStyleAlert = [str(row.id) for row in rows]
# Status of closed
rows = db(table.status == 3).select(table.id)
s3.dataTableStyleDisabled = [str(row.id) for row in rows]
s3.dataTableStyleWarning = [str(row.id) for row in rows]
# Status of Master
rows = db(table.status == 4).select(table.id)
s3.dataTableStyleWarning.extend(str(row.id) for row in rows)
s3db.configure("survey_template",
orderby = "survey_template.status",
create_next = URL(c="survey", f="template"),
update_next = URL(c="survey", f="template"),
)
return True
s3.prep = prep
# Post-processor
def postp(r, output):
if r.component:
template_id = r.id
if r.component_name == "translate":
s3_action_buttons(r)
s3.actions.extend([dict(label=str(T("Download")),
_class="action-btn",
url=r.url(method = "translate_download",
component = "translate",
component_id = "[id]",
representation = "xls",
),
),
dict(label=str(T("Upload")),
_class="action-btn",
url=URL(c = "survey",
f = "template",
args = [template_id,
"translate",
"[id]",
]),
),
])
#elif r.component_name == "section":
# # Add the section select widget to the form
# # undefined
# sectionSelect = s3.survey_section_select_widget(template_id)
# output.update(form = sectionSelect)
# Add a button to show what the questionnaire looks like
#s3_action_buttons(r)
#s3.actions = s3.actions + [
# dict(label=str(T("Display")),
# _class="action-btn",
# url=URL(c="survey",
# f="template_read",
# args=["[id]"])
# ),
# ]
return output
s3.postp = postp
if request.ajax:
post = request.post_vars
action = post.get("action")
template_id = post.get("parent_id")
section_id = post.get("section_id")
section_text = post.get("section_text")
if action == "section" and template_id != None:
id = db.survey_section.insert(name=section_text,
template_id=template_id,
cloned_section_id=section_id)
if id is None:
current.log.error("Failed to insert record")
return
# Remove CRUD generated buttons in the tabs
s3db.configure("survey_template",
listadd=False,
#deletable=False,
)
output = s3_rest_controller(rheader=s3db.survey_template_rheader)
return output
# -----------------------------------------------------------------------------
def template_read():
"""
Show the details of all the questions of a particular template
"""
if len(get_vars) > 0:
dummy, template_id = get_vars.viewing.split(".")
else:
template_id = request.args[0]
def postp(r, output):
if r.interactive:
template_id = r.id
form = s3db.survey_buildQuestionnaireFromTemplate(template_id)
output["items"] = None
output["form"] = None
output["item"] = form
output["title"] = s3.crud_strings["survey_template"].title_question_details
return output
s3.postp = postp
# remove CRUD generated buttons in the tabs
s3db.configure("survey_template",
listadd = False,
editable = False,
deletable = False,
)
from s3 import s3_request
r = s3_request("survey", "template",
args = [template_id],
)
return r(method = "read",
rheader = s3db.survey_template_rheader,
)
# -----------------------------------------------------------------------------
def template_summary():
"""
Show section-wise summary of questions of a template
"""
# Load Model
tablename = "survey_template"
s3db[tablename]
s3db.survey_complete
crud_strings = s3.crud_strings[tablename]
def postp(r, output):
if r.interactive:
if len(get_vars) > 0:
dummy, template_id = get_vars.viewing.split(".")
else:
template_id = r.id
form = s3db.survey_build_template_summary(template_id)
output["items"] = form
output["sortby"] = [[0, "asc"]]
output["title"] = crud_strings.title_analysis_summary
output["subtitle"] = crud_strings.subtitle_analysis_summary
return output
s3.postp = postp
# remove CRUD generated buttons in the tabs
s3db.configure(tablename,
listadd=False,
deletable=False,
)
output = s3_rest_controller("survey", "template",
method = "list",
rheader = s3db.survey_template_rheader,
)
s3.actions = None
return output
# -----------------------------------------------------------------------------
def series():
""" RESTful CRUD controller """
# Load Model
table = s3db.survey_series
s3db.survey_answerlist_dataTable_pre()
def prep(r):
if r.interactive:
if r.method == "create":
ttable = s3db.survey_template
if not db(ttable.deleted == False).select(ttable.id,
limitby=(0, 1)
):
session.warning = T("You need to create a template before you can create a series")
redirect(URL(c="survey", f="template", args=[], vars={}))
if r.id and (r.method == "update"):
table.template_id.writable = False
return True
s3.prep = prep
def postp(r, output):
method = r.method
if request.ajax == True and method == "read":
return output["item"]
if not r.component and method != "summary":
# Replace the Action buttons
s3.actions = [{"label": s3_str(messages.UPDATE),
"_class": "action-btn edit",
"url": URL(c="survey",
f="series",
args=["[id]",
"summary",
],
),
},
]
elif r.component_name == "complete":
if method == "update":
if r.http == "GET":
form = s3db.survey_buildQuestionnaireFromSeries(r.id,
r.component_id)
output["form"] = form
elif r.http == "POST":
if len(request.post_vars) > 0:
id = s3db.survey_save_answers_for_series(r.id,
r.component_id, # Update
request.post_vars)
response.confirmation = \
s3.crud_strings["survey_complete"].msg_record_modified
else:
s3db.survey_answerlist_dataTable_post(r)
return output
s3.postp = postp
# Remove CRUD generated buttons in the tabs
s3db.configure("survey_series",
deletable = False,
)
s3db.configure("survey_complete",
listadd = False,
deletable = False,
)
output = s3_rest_controller(rheader = s3db.survey_series_rheader)
return output
# -----------------------------------------------------------------------------
def series_export_formatted():
"""
Download a Spreadsheet which can be filled-in offline & uploaded
@ToDo: rewrite as S3Method handler
"""
try:
series_id = request.args[0]
except:
output = s3_rest_controller("survey", "series",
rheader = s3db.survey_series_rheader)
return output
# Load Model
table = s3db.survey_series
s3db.table("survey_complete")
vars = request.post_vars
series = db(table.id == series_id).select(table.name,
table.logo,
limitby = (0, 1)
).first()
if not series.logo:
logo = None
else:
if "Export_Spreadsheet" in vars:
ext = "bmp"
else:
ext = "png"
logo = os.path.join(request.folder,
"uploads",
"survey",
"logo",
"%s.%s" % (series.logo, ext)
)
if not os.path.exists(logo) or not os.path.isfile(logo):
logo = None
# Get the translation dictionary
lang_dict = {}
lang = request.post_vars.get("translation_language", None)
if lang:
if lang == "Default":
lang_dict = {}
else:
try:
from gluon.languages import read_dict
lang_filename = "applications/%s/uploads/survey/translations/%s.py" % \
(appname, lang)
lang_dict = read_dict(lang_filename)
except:
lang_dict = {}
if "Export_Spreadsheet" in vars:
(matrix, matrix_answers) = series_prepare_matrix(series_id,
series,
logo,
lang_dict,
justified = True
)
output = series_export_spreadsheet(matrix,
matrix_answers,
logo,
)
filename = "%s.xls" % series.name
content_type = ".xls"
elif "Export_Word" in vars:
template = s3db.survey_getTemplateFromSeries(series_id)
template_id = template.id
title = "%s (%s)" % (series.name, template.name)
title = s3db.survey_T(title, lang_dict)
widget_list = s3db.survey_getAllWidgetsForTemplate(template_id)
output = series_export_word(widget_list, lang_dict, title, logo)
filename = "%s.rtf" % series.name
content_type = ".rtf"
else:
output = s3_rest_controller("survey", "series",
rheader = s3db.survey_series_rheader)
return output
from gluon.contenttype import contenttype
output.seek(0)
response.headers["Content-Type"] = contenttype(content_type)
response.headers["Content-disposition"] = "attachment; filename=\"%s\"" % filename
return output.read()
# -----------------------------------------------------------------------------
def series_prepare_matrix(series_id, series, logo, lang_dict, justified=False):
"""
Helper function for series_export_formatted()
"""
# Get the data
# ============
# * The sections within the template
# * The layout rules for each question
template = s3db.survey_getTemplateFromSeries(series_id)
template_id = template.id
section_list = s3db.survey_getAllSectionsForSeries(series_id)
survey_T = s3db.survey_T
title = "%s (%s)" % (series.name, template.name)
title = survey_T(title, lang_dict)
layout = []
survey_getQstnLayoutRules = s3db.survey_getQstnLayoutRules
for section in section_list:
section_name = survey_T(section["name"], lang_dict)
rules = survey_getQstnLayoutRules(template_id,
section["section_id"])
layout_rules = [section_name, rules]
layout.append(layout_rules)
widget_list = s3db.survey_getAllWidgetsForTemplate(template_id)
layout_blocks = s3db.survey_LayoutBlocks()
# Store the questions into a matrix based on the layout and the space
# required for each question - for example an option question might
# need one row for each possible option, and if this is in a layout
# then the position needs to be recorded carefully...
preliminary_matrix = s3db.survey_getMatrix(title,
logo,
layout,
widget_list,
False,
lang_dict,
showSectionLabels = False,
layoutBlocks = layout_blocks,
)
if not justified:
return preliminary_matrix
# Align the questions so that each row takes up the same space.
# This is done by storing resize and margin instructions with
# each widget that is being printed
layout_blocks.align()
# Now rebuild the matrix with the spacing for each widget set up so
# that the document will be fully justified
layout_blocks = s3db.survey_LayoutBlocks()
(matrix1, matrix2) = s3db.survey_getMatrix(title,
logo,
layout,
widget_list,
True,
lang_dict,
showSectionLabels = False,
)
return (matrix1, matrix2)
# -----------------------------------------------------------------------------
def series_export_word(widget_list, lang_dict, title, logo):
"""
Export a Series in RTF Format
@ToDo: rewrite as S3Method handler
"""
import gluon.contrib.pyrtf as pyrtf
from io import BytesIO
output = BytesIO()
doc = pyrtf.Document(default_language=pyrtf.Languages.EnglishUK)
section = pyrtf.Section()
ss = doc.StyleSheet
ps = ss.ParagraphStyles.Normal.Copy()
ps.SetName("NormalGrey")
ps.SetShadingPropertySet(pyrtf.ShadingPropertySet(pattern=1,
background=pyrtf.Colour("grey light", 224, 224, 224)))
ss.ParagraphStyles.append(ps)
ps = ss.ParagraphStyles.Normal.Copy()
ps.SetName("NormalCentre")
ps.SetParagraphPropertySet(pyrtf.ParagraphPropertySet(alignment=3))
ss.ParagraphStyles.append(ps)
doc.Sections.append(section)
heading = pyrtf.Paragraph(ss.ParagraphStyles.Heading1)
if logo:
image = pyrtf.Image(logo)
heading.append(image)
heading.append(title)
section.append(heading)
col = [2800, 6500]
table = pyrtf.Table(*col)
AddRow = table.AddRow
sorted_widget_list = sorted(widget_list.values(),
key=lambda widget: widget.question.posn)
for widget in sorted_widget_list:
line = widget.writeQuestionToRTF(ss, lang_dict)
try:
AddRow(*line)
except:
if settings.base.debug:
raise
pass
section.append(table)
renderer = pyrtf.Renderer()
renderer.Write(doc, output)
return output
# -----------------------------------------------------------------------------
def series_export_spreadsheet(matrix, matrix_answers, logo):
"""
Now take the matrix data type and generate a spreadsheet from it
"""
try:
import xlwt
except ImportError:
response.error = T("xlwt not installed, so cannot export as a Spreadsheet")
output = s3_rest_controller("survey", "survey_series",
rheader = s3db.survey_series_rheader)
return output
import math
from io import BytesIO
# -------------------------------------------------------------------------
def wrap_text(sheet, cell, style):
row = cell.row
col = cell.col
try:
text = s3_str(cell.text)
except:
text = cell.text
width = 16
# Wrap text and calculate the row width and height
characters_in_cell = float(width-2)
twips_per_row = 255 #default row height for 10 point font
if cell.merged():
try:
sheet.write_merge(cell.row,
cell.row + cell.mergeV,
cell.col,
cell.col + cell.mergeH,
text,
style,
)
except Exception as msg:
log = current.log
log.error(msg)
log.debug("row: %s + vert: %s, col: %s + horiz %s" % \
(cell.row, cell.mergeV, cell.col, cell.mergeH))
posn = "%s,%s" % (cell.row, cell.col)
if matrix.matrix[posn]:
log.debug(matrix.matrix[posn])
rows = math.ceil((len(text) / characters_in_cell) / (1 + cell.mergeH))
else:
sheet.write(cell.row,
cell.col,
text,
style,
)
rows = math.ceil(len(text) / characters_in_cell)
new_row_height = int(rows * twips_per_row)
new_col_width = width * COL_WIDTH_MULTIPLIER
if sheet.row(row).height < new_row_height:
sheet.row(row).height = new_row_height
if sheet.col(col).width < new_col_width:
sheet.col(col).width = new_col_width
# -------------------------------------------------------------------------
def merge_styles(list_template, style_list):
"""
Take a list of styles and return a single style object with
all the differences from a newly created object added to the
resultant style.
"""
if len(style_list) == 0:
final_style = xlwt.XFStyle()
elif len(style_list) == 1:
final_style = list_template[style_list[0]]
else:
zero_style = xlwt.XFStyle()
final_style = xlwt.XFStyle()
for i in range(0, len(style_list)):
final_style = merge_object_diff(final_style,
list_template[style_list[i]],
zero_style)
return final_style
# -------------------------------------------------------------------------
def merge_object_diff(base_obj, new_obj, zero_obj):
"""
Function to copy all the elements in new_obj that are different from
the zero_obj and place them in the base_obj
"""
element_list = new_obj.__dict__
for (element, value) in element_list.items():
try:
base_obj.__dict__[element] = merge_object_diff(base_obj.__dict__[element],
value,
zero_obj.__dict__[element])
except:
if zero_obj.__dict__[element] != value:
base_obj.__dict__[element] = value
return base_obj
COL_WIDTH_MULTIPLIER = 240
book = xlwt.Workbook(encoding="utf-8")
output = BytesIO()
protection = xlwt.Protection()
protection.cell_locked = 1
no_protection = xlwt.Protection()
no_protection.cell_locked = 0
borders = xlwt.Borders()
borders.left = xlwt.Borders.DOTTED
borders.right = xlwt.Borders.DOTTED
borders.top = xlwt.Borders.DOTTED
borders.bottom = xlwt.Borders.DOTTED
border_t1 = xlwt.Borders()
border_t1.top = xlwt.Borders.THIN
border_t2 = xlwt.Borders()
border_t2.top = xlwt.Borders.MEDIUM
border_l1 = xlwt.Borders()
border_l1.left = xlwt.Borders.THIN
border_l2 = xlwt.Borders()
border_l2.left = xlwt.Borders.MEDIUM
border_r1 = xlwt.Borders()
border_r1.right = xlwt.Borders.THIN
border_r2 = xlwt.Borders()
border_r2.right = xlwt.Borders.MEDIUM
border_b1 = xlwt.Borders()
border_b1.bottom = xlwt.Borders.THIN
border_b2 = xlwt.Borders()
border_b2.bottom = xlwt.Borders.MEDIUM
align_base = xlwt.Alignment()
align_base.horz = xlwt.Alignment.HORZ_LEFT
align_base.vert = xlwt.Alignment.VERT_TOP
align_wrap = xlwt.Alignment()
align_wrap.horz = xlwt.Alignment.HORZ_LEFT
align_wrap.vert = xlwt.Alignment.VERT_TOP
align_wrap.wrap = xlwt.Alignment.WRAP_AT_RIGHT
shaded_fill = xlwt.Pattern()
shaded_fill.pattern = xlwt.Pattern.SOLID_PATTERN
shaded_fill.pattern_fore_colour = 0x16 # 25% Grey
shaded_fill.pattern_back_colour = 0x08 # Black
heading_fill = xlwt.Pattern()
heading_fill.pattern = xlwt.Pattern.SOLID_PATTERN
heading_fill.pattern_fore_colour = 0x1F # ice_blue
heading_fill.pattern_back_colour = 0x08 # Black
style_title = xlwt.XFStyle()
style_title.font.height = 0x0140 # 320 twips, 16 points
style_title.font.bold = True
style_title.alignment = align_base
style_header = xlwt.XFStyle()
style_header.font.height = 0x00F0 # 240 twips, 12 points
style_header.font.bold = True
style_header.alignment = align_base
style_sub_header = xlwt.XFStyle()
style_sub_header.font.bold = True
style_sub_header.alignment = align_wrap
style_section_heading = xlwt.XFStyle()
style_section_heading.font.bold = True
style_section_heading.alignment = align_wrap
style_section_heading.pattern = heading_fill
style_hint = xlwt.XFStyle()
style_hint.protection = protection
style_hint.font.height = 160 # 160 twips, 8 points
style_hint.font.italic = True
style_hint.alignment = align_wrap
style_text = xlwt.XFStyle()
style_text.protection = protection
style_text.alignment = align_wrap
style_instructions = xlwt.XFStyle()
style_instructions.font.height = 0x00B4 # 180 twips, 9 points
style_instructions.font.italic = True
style_instructions.protection = protection
style_instructions.alignment = align_wrap
style_box = xlwt.XFStyle()
style_box.borders = borders
style_box.protection = no_protection
style_input = xlwt.XFStyle()
style_input.borders = borders
style_input.protection = no_protection
style_input.pattern = shaded_fill
box_l1 = xlwt.XFStyle()
box_l1.borders = border_l1
box_l2 = xlwt.XFStyle()
box_l2.borders = border_l2
box_t1 = xlwt.XFStyle()
box_t1.borders = border_t1
box_t2 = xlwt.XFStyle()
box_t2.borders = border_t2
box_r1 = xlwt.XFStyle()
box_r1.borders = border_r1
box_r2 = xlwt.XFStyle()
box_r2.borders = border_r2
box_b1 = xlwt.XFStyle()
box_b1.borders = border_b1
box_b2 = xlwt.XFStyle()
box_b2.borders = border_b2
style_list = {}
style_list["styleTitle"] = style_title
style_list["styleHeader"] = style_header
style_list["styleSubHeader"] = style_sub_header
style_list["styleSectionHeading"] = style_section_heading
style_list["styleHint"] = style_hint
style_list["styleText"] = style_text
style_list["styleInstructions"] = style_instructions
style_list["styleInput"] = style_input
style_list["boxL1"] = box_l1
style_list["boxL2"] = box_l2
style_list["boxT1"] = box_t1
style_list["boxT2"] = box_t2
style_list["boxR1"] = box_r1
style_list["boxR2"] = box_r2
style_list["boxB1"] = box_b1
style_list["boxB2"] = box_b2
sheet1 = book.add_sheet(T("Assessment"))
sheet2 = book.add_sheet(T("Metadata"))
max_col = 0
for cell in matrix.matrix.values():
if cell.col + cell.mergeH > 255:
current.log.warning("Cell (%s,%s) - (%s,%s) ignored" % \
(cell.col, cell.row, cell.col + cell.mergeH, cell.row + cell.mergeV))
continue
if cell.col + cell.mergeH > max_col:
max_col = cell.col + cell.mergeH
if cell.joined():
continue
style = merge_styles(style_list, cell.styleList)
if (style.alignment.wrap == style.alignment.WRAP_AT_RIGHT):
# get all the styles from the joined cells
# and merge these styles in.
joined_styles = matrix.joinedElementStyles(cell)
joined_style = merge_styles(style_list, joined_styles)
try:
wrap_text(sheet1, cell, joined_style)
except:
pass
else:
if cell.merged():
# get all the styles from the joined cells
# and merge these styles in.
joined_styles = matrix.joinedElementStyles(cell)
joined_style = merge_styles(style_list, joined_styles)
try:
sheet1.write_merge(cell.row,
cell.row + cell.mergeV,
cell.col,
cell.col + cell.mergeH,
s3_str(cell.text),
joined_style,
)
except Exception as msg:
log = current.log
log.error(msg)
log.debug("row: %s + vert: %s, col: %s + horiz %s" % \
(cell.row, cell.mergeV, cell.col, cell.mergeH))
posn = "%s,%s" % (cell.row, cell.col)
if matrix.matrix[posn]:
log.debug(matrix.matrix[posn])
else:
sheet1.write(cell.row,
cell.col,
s3_str(cell.text),
style,
)
CELL_WIDTH = 480 # approximately 2 characters
if max_col > 255:
max_col = 255
for col in range(max_col + 1):
sheet1.col(col).width = CELL_WIDTH
sheet2.write(0, 0, "Question Code")
sheet2.write(0, 1, "Response Count")
sheet2.write(0, 2, "Values")
sheet2.write(0, 3, "Cell Address")
for cell in matrix_answers.matrix.values():
style = merge_styles(style_list, cell.styleList)
sheet2.write(cell.row,
cell.col,
s3_str(cell.text),
style,
)
if logo != None:
sheet1.insert_bitmap(logo, 0, 0)
sheet1.protect = True
sheet2.protect = True
for i in range(26):
sheet2.col(i).width = 0
sheet2.write(0,
26,
s3_str(T("Please do not remove this sheet")),
style_header,
)
sheet2.col(26).width = 12000
book.save(output)
return output
# -----------------------------------------------------------------------------
def completed_chart():
"""
Allows the user to display all the data from the selected question
in a simple chart. If the data is numeric then a histogram will be
drawn if it is an option type then a pie chart, although the type of
chart drawn is managed by the analysis widget.
"""
series_id = get_vars.get("series_id")
if not series_id:
return "Programming Error: Series ID missing"
question_id = get_vars.get("question_id")
if not question_id:
return "Programming Error: Question ID missing"
q_type = get_vars.get("type")
if not q_type:
return "Programming Error: Question Type missing"
getAnswers = s3db.survey_getAllAnswersForQuestionInSeries
answers = getAnswers(question_id, series_id)
analysisTool = s3db.survey_analysis_type[q_type](question_id, answers)
qstnName = analysisTool.qstnWidget.question.name
image = analysisTool.drawChart(series_id, output="png")
return image
# -----------------------------------------------------------------------------
def section():
"""
RESTful CRUD controller
- unused
"""
# Load Model
#table = s3db.survey_section
def prep(r):
s3db.configure(r.tablename,
deletable = False,
orderby = "%s.posn" % r.tablename,
)
return True
s3.prep = prep
# Post-processor
def postp(r, output):
""" Add the section select widget to the form """
try:
template_id = int(request.args[0])
except:
template_id = None
# Undefined?
sectionSelect = s3.survey_section_select_widget(template_id)
output["sectionSelect"] = sectionSelect
return output
#s3.postp = postp
output = s3_rest_controller(# Undefined
#rheader=s3db.survey_section_rheader
)
return output
# -----------------------------------------------------------------------------
def question():
""" RESTful CRUD controller """
def prep(r):
s3db.configure(r.tablename,
orderby = r.tablename + ".posn",
)
return True
s3.prep = prep
output = s3_rest_controller(# Undefined
#rheader=s3db.survey_section_rheader
)
return output
# -----------------------------------------------------------------------------
def question_list():
""" RESTful CRUD controller """
output = s3_rest_controller()
return output
# -----------------------------------------------------------------------------
def formatter():
""" RESTful CRUD controller """
output = s3_rest_controller()
return output
# -----------------------------------------------------------------------------
def question_metadata():
""" RESTful CRUD controller """
output = s3_rest_controller()
return output
# -----------------------------------------------------------------------------
def new_assessment():
"""
RESTful CRUD controller to create a new 'complete' survey
- although the created form is a fully custom one
"""
# Load Model
table = s3db.survey_complete
s3db.table("survey_series")
def prep(r):
if r.interactive:
viewing = get_vars.get("viewing", None)
if viewing:
dummy, series_id = viewing.split(".")
else:
series_id = get_vars.get("series", None)
if not series_id:
series_id = r.id
if series_id is None:
# The URL is bad, without a series id we're lost so list all series
redirect(URL(c="survey", f="series", args=[], vars={}))
if len(request.post_vars) > 0:
id = s3db.survey_save_answers_for_series(series_id,
None, # Insert
request.post_vars)
response.confirmation = \
s3.crud_strings["survey_complete"].msg_record_created
r.method = "create"
return True
s3.prep = prep
def postp(r, output):
if r.interactive:
# Not sure why we need to repeat this & can't do it outside the prep/postp
viewing = get_vars.get("viewing", None)
if viewing:
dummy, series_id = viewing.split(".")
else:
series_id = get_vars.get("series", None)
if not series_id:
series_id = r.id
if output["form"] is None:
# The user is not authorised to create so switch to read
redirect(URL(c="survey", f="series",
args=[series_id, "read"],
vars={}))
# This is a bespoke form which confuses CRUD, which displays an
# error "Invalid form (re-opened in another window?)"
# So so long as we don't have an error in the form we can
# delete this error.
elif response.error and not output["form"]["error"]:
response.error = None
s3db.survey_answerlist_dataTable_post(r)
form = s3db.survey_buildQuestionnaireFromSeries(series_id, None)
urlimport = URL(c="survey", f="complete", args=["import"],
vars={"viewing":"%s.%s" % ("survey_series", series_id),
"single_pass":True}
)
buttons = DIV(A(T("Upload Completed Assessment Form"),
_href=urlimport,
_id="Excel-import",
_class="action-btn"
),
)
output["form"] = TAG[""](buttons, form)
return output
s3.postp = postp
output = s3_rest_controller("survey", "complete",
method = "create",
rheader = s3db.survey_series_rheader
)
return output
# -----------------------------------------------------------------------------
def complete():
""" RESTful CRUD controller """
# Load Model
table = s3db.survey_complete
stable = s3db.survey_series
s3db.survey_answerlist_dataTable_pre()