upgrade_surveyor_gui 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (291) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +44 -0
  3. data/.travis.yml +17 -0
  4. data/CHANGELOG.md +46 -0
  5. data/Gemfile +8 -0
  6. data/LICENSE +21 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +244 -0
  9. data/Rakefile +66 -0
  10. data/app/controllers/surveyor_controller.rb +89 -0
  11. data/app/controllers/surveyor_gui/answers_controller.rb +3 -0
  12. data/app/controllers/surveyor_gui/application_controller.rb +3 -0
  13. data/app/controllers/surveyor_gui/dependencys_controller.rb +286 -0
  14. data/app/controllers/surveyor_gui/question_groups_controller.rb +90 -0
  15. data/app/controllers/surveyor_gui/questions_controller.rb +187 -0
  16. data/app/controllers/surveyor_gui/reports_controller.rb +339 -0
  17. data/app/controllers/surveyor_gui/responses_controller.rb +41 -0
  18. data/app/controllers/surveyor_gui/survey_controller.rb +16 -0
  19. data/app/controllers/surveyor_gui/survey_sections_controller.rb +80 -0
  20. data/app/controllers/surveyor_gui/surveyforms_controller.rb +4 -0
  21. data/app/facades/report_formatter.rb +46 -0
  22. data/app/facades/report_preview_wrapper.rb +11 -0
  23. data/app/facades/report_response_generator.rb +147 -0
  24. data/app/helpers/surveyor_gui/application_helper.rb +2 -0
  25. data/app/helpers/surveyor_gui/dependencys_helper.rb +5 -0
  26. data/app/helpers/surveyor_gui/question_groups_helper.rb +3 -0
  27. data/app/helpers/surveyor_gui/questions_helper.rb +22 -0
  28. data/app/helpers/surveyor_gui/reports_helper.rb +19 -0
  29. data/app/helpers/surveyor_gui/surveyforms_helper.rb +164 -0
  30. data/app/helpers/surveyor_helper.rb +4 -0
  31. data/app/inputs/currency_input.rb +5 -0
  32. data/app/inputs/date_picker_input.rb +7 -0
  33. data/app/inputs/datetime_picker_input.rb +5 -0
  34. data/app/inputs/percent_input.rb +5 -0
  35. data/app/inputs/time_picker_input.rb +5 -0
  36. data/app/mailers/.gitkeep +0 -0
  37. data/app/models/.gitkeep +0 -0
  38. data/app/models/answer.rb +4 -0
  39. data/app/models/column.rb +3 -0
  40. data/app/models/dependency.rb +4 -0
  41. data/app/models/dependency_condition.rb +4 -0
  42. data/app/models/permitted_params_decorator.rb +80 -0
  43. data/app/models/question.rb +4 -0
  44. data/app/models/question_group.rb +4 -0
  45. data/app/models/question_group_tracker.rb +19 -0
  46. data/app/models/question_type.rb +7 -0
  47. data/app/models/response.rb +4 -0
  48. data/app/models/response_set.rb +4 -0
  49. data/app/models/row.rb +3 -0
  50. data/app/models/survey.rb +4 -0
  51. data/app/models/survey_section.rb +4 -0
  52. data/app/models/surveyform.rb +103 -0
  53. data/app/uploaders/blob_uploader.rb +48 -0
  54. data/app/views/layouts/surveyor_gui/application.html.erb +14 -0
  55. data/app/views/layouts/surveyor_gui/surveyor_gui_blank.html.erb +13 -0
  56. data/app/views/layouts/surveyor_gui/surveyor_gui_default.html.erb +17 -0
  57. data/app/views/layouts/surveyor_gui/surveyor_modified.html.erb +14 -0
  58. data/app/views/partials/_answer.html.haml +33 -0
  59. data/app/views/partials/_question.html.haml +33 -0
  60. data/app/views/partials/_question_group.html.haml +73 -0
  61. data/app/views/partials/_section.html.haml +13 -0
  62. data/app/views/surveyor/edit.html.haml +21 -0
  63. data/app/views/surveyor_gui/dependencys/_column.html.erb +1 -0
  64. data/app/views/surveyor_gui/dependencys/_dependency.html.erb +22 -0
  65. data/app/views/surveyor_gui/dependencys/_dependency_condition_fields.html.erb +48 -0
  66. data/app/views/surveyor_gui/dependencys/_form.html.erb +20 -0
  67. data/app/views/surveyor_gui/dependencys/blank.html.erb +0 -0
  68. data/app/views/surveyor_gui/dependencys/edit.html.erb +1 -0
  69. data/app/views/surveyor_gui/dependencys/new.html.erb +1 -0
  70. data/app/views/surveyor_gui/partials/_url_helpers.html.erb +5 -0
  71. data/app/views/surveyor_gui/question_groups/_form.html.erb +57 -0
  72. data/app/views/surveyor_gui/question_groups/_group_inline_field.html.erb +21 -0
  73. data/app/views/surveyor_gui/question_groups/_group_inline_fields.html.erb +25 -0
  74. data/app/views/surveyor_gui/question_groups/blank.html.erb +0 -0
  75. data/app/views/surveyor_gui/question_groups/edit.html.erb +1 -0
  76. data/app/views/surveyor_gui/question_groups/new.html.erb +1 -0
  77. data/app/views/surveyor_gui/questions/_answer_fields.html.erb +23 -0
  78. data/app/views/surveyor_gui/questions/_answer_options.html.erb +28 -0
  79. data/app/views/surveyor_gui/questions/_form.html.erb +66 -0
  80. data/app/views/surveyor_gui/questions/_grid_dropdown_columns.html.erb +10 -0
  81. data/app/views/surveyor_gui/questions/_grid_dropdown_fields.html.erb +42 -0
  82. data/app/views/surveyor_gui/questions/_grid_fields.html.erb +9 -0
  83. data/app/views/surveyor_gui/questions/_no_picks.html.erb +29 -0
  84. data/app/views/surveyor_gui/questions/_pick.html +21 -0
  85. data/app/views/surveyor_gui/questions/_picks.html.erb +12 -0
  86. data/app/views/surveyor_gui/questions/blank.html.erb +0 -0
  87. data/app/views/surveyor_gui/questions/edit.html.erb +1 -0
  88. data/app/views/surveyor_gui/questions/new.html.erb +1 -0
  89. data/app/views/surveyor_gui/reports/_graphs.html.haml +21 -0
  90. data/app/views/surveyor_gui/reports/_grid.html.haml +42 -0
  91. data/app/views/surveyor_gui/reports/_grid_dropdown.html.haml +56 -0
  92. data/app/views/surveyor_gui/reports/_repeater.html.haml +27 -0
  93. data/app/views/surveyor_gui/reports/_show_report.html.haml +33 -0
  94. data/app/views/surveyor_gui/reports/_single_question.html.haml +70 -0
  95. data/app/views/surveyor_gui/reports/show.html.erb +14 -0
  96. data/app/views/surveyor_gui/responses/_grid.html.haml +20 -0
  97. data/app/views/surveyor_gui/responses/_grid_dropdown.html.haml +20 -0
  98. data/app/views/surveyor_gui/responses/_repeater.html.haml +22 -0
  99. data/app/views/surveyor_gui/responses/_survey_results.html.haml +40 -0
  100. data/app/views/surveyor_gui/responses/index.html.haml +24 -0
  101. data/app/views/surveyor_gui/responses/show.html.haml +42 -0
  102. data/app/views/surveyor_gui/shared/_grid_comments.html.haml +10 -0
  103. data/app/views/surveyor_gui/shared/_new_line.html.haml +2 -0
  104. data/app/views/surveyor_gui/shared/_pick_comments.html.haml +15 -0
  105. data/app/views/surveyor_gui/shared/_question_number.html.haml +11 -0
  106. data/app/views/surveyor_gui/shared/_report_data.html.haml +27 -0
  107. data/app/views/surveyor_gui/shared/_stars_report_data.html.haml +14 -0
  108. data/app/views/surveyor_gui/survey_sections/_form.html.erb +13 -0
  109. data/app/views/surveyor_gui/survey_sections/blank.html.erb +0 -0
  110. data/app/views/surveyor_gui/survey_sections/edit.html.erb +1 -0
  111. data/app/views/surveyor_gui/survey_sections/new.html.erb +1 -0
  112. data/app/views/surveyor_gui/surveyforms/_form.html.erb +50 -0
  113. data/app/views/surveyor_gui/surveyforms/_question_field.html.erb +148 -0
  114. data/app/views/surveyor_gui/surveyforms/_question_group.html.erb +116 -0
  115. data/app/views/surveyor_gui/surveyforms/_question_group_fields.html.erb +3 -0
  116. data/app/views/surveyor_gui/surveyforms/_question_name_and_number.html.erb +6 -0
  117. data/app/views/surveyor_gui/surveyforms/_question_section.html.erb +15 -0
  118. data/app/views/surveyor_gui/surveyforms/_question_wrapper.html.erb +108 -0
  119. data/app/views/surveyor_gui/surveyforms/_survey_section_fields.html.erb +137 -0
  120. data/app/views/surveyor_gui/surveyforms/clone_survey.html.erb +13 -0
  121. data/app/views/surveyor_gui/surveyforms/edit.html.erb +5 -0
  122. data/app/views/surveyor_gui/surveyforms/index.html.erb +40 -0
  123. data/app/views/surveyor_gui/surveyforms/new.html.erb +1 -0
  124. data/app/views/surveyor_gui/surveyforms/show.html.erb +5 -0
  125. data/config/environment.rb +0 -0
  126. data/config/routes.rb +71 -0
  127. data/config.ru +4 -0
  128. data/db/migrate/20140307204049_add_template_to_surveys.rb +5 -0
  129. data/db/migrate/20140307235607_add_test_data_to_response_sets.rb +5 -0
  130. data/db/migrate/20140308171947_add_original_choice_to_answers.rb +5 -0
  131. data/db/migrate/20140308172118_add_blob_to_responses.rb +5 -0
  132. data/db/migrate/20140308172417_add_modifiable_to_survey_section.rb +5 -0
  133. data/db/migrate/20140308174532_add_modifiable_to_question.rb +5 -0
  134. data/db/migrate/20140308175305_add_dynamically_generate_to_questions.rb +5 -0
  135. data/db/migrate/20140311032923_add_dummy_blob_to_questions.rb +5 -0
  136. data/db/migrate/20140311160609_add_dynamic_source_to_questions.rb +5 -0
  137. data/db/migrate/20140311161714_add_report_code_to_questions.rb +5 -0
  138. data/db/migrate/20140530181134_add_is_comment_to_questions.rb +5 -0
  139. data/db/migrate/20140531012006_add_is_comment_to_answers.rb +5 -0
  140. data/db/migrate/20140531225529_create_rows.rb +9 -0
  141. data/db/migrate/20140601011151_create_columns.rb +11 -0
  142. data/db/migrate/20140602030330_add_column_id_to_answers.rb +5 -0
  143. data/db/migrate/20140603155606_add_column_id_to_responses.rb +5 -0
  144. data/db/migrate/20140606023527_add_column_id_to_dependency_conditions.rb +5 -0
  145. data/db/migrate/20140815165307_add_user_id_to_survey.rb +5 -0
  146. data/lib/assets/.gitkeep +0 -0
  147. data/lib/assets/images/addicon.png +0 -0
  148. data/lib/assets/images/datepicker.gif +0 -0
  149. data/lib/assets/images/delete.gif +0 -0
  150. data/lib/assets/images/delete.png +0 -0
  151. data/lib/assets/images/images/border.png +0 -0
  152. data/lib/assets/images/images/controls.png +0 -0
  153. data/lib/assets/images/images/loading.gif +0 -0
  154. data/lib/assets/images/images/loading_background.png +0 -0
  155. data/lib/assets/images/images/overlay.png +0 -0
  156. data/lib/assets/images/rails.png +0 -0
  157. data/lib/assets/images/star.gif +0 -0
  158. data/lib/assets/javascripts/.gitkeep +0 -0
  159. data/lib/assets/javascripts/surveyor_gui/jquery.MetaData.js +121 -0
  160. data/lib/assets/javascripts/surveyor_gui/jquery.blockUI.js +619 -0
  161. data/lib/assets/javascripts/surveyor_gui/jquery.rating.js +377 -0
  162. data/lib/assets/javascripts/surveyor_gui/jquery.validate.js +1188 -0
  163. data/lib/assets/javascripts/surveyor_gui/surveyor_add_ons.js +10 -0
  164. data/lib/assets/javascripts/surveyor_gui/surveyor_gui.js +1428 -0
  165. data/lib/assets/stylesheets/.gitkeep +0 -0
  166. data/lib/assets/stylesheets/surveyor_gui/jquery.rating.css +12 -0
  167. data/lib/assets/stylesheets/surveyor_gui/surveyor_add_ons.css +3 -0
  168. data/lib/assets/stylesheets/surveyor_gui/surveyor_gui.sass +652 -0
  169. data/lib/enumerable_extenders.rb +31 -0
  170. data/lib/generators/surveyor_gui/install_generator.rb +57 -0
  171. data/lib/generators/surveyor_gui/templates/app/assets/javascripts/surveyor_add_ons.js +2 -0
  172. data/lib/generators/surveyor_gui/templates/app/assets/javascripts/surveyor_gui_all.js +24 -0
  173. data/lib/generators/surveyor_gui/templates/app/assets/stylesheets/surveyor_add_ons.css.sass +1 -0
  174. data/lib/generators/surveyor_gui/templates/app/assets/stylesheets/surveyor_gui_all.css.sass +8 -0
  175. data/lib/generators/surveyor_gui/templates/app/helpers/surveyor_helper.rb +4 -0
  176. data/lib/generators/surveyor_gui/templates/app/models/response_set_user.rb +13 -0
  177. data/lib/generators/surveyor_gui/templates/app/views/layouts/surveyor_gui/surveyor_gui_default.html.erb +17 -0
  178. data/lib/generators/surveyor_gui/templates/config/initializers/simple_form.rb +140 -0
  179. data/lib/generators/surveyor_gui/templates/config/locales/en.yml +5 -0
  180. data/lib/surveyor_gui/engine.rb +31 -0
  181. data/lib/surveyor_gui/helpers/surveyor_helper_methods.rb +19 -0
  182. data/lib/surveyor_gui/models/answer_methods.rb +24 -0
  183. data/lib/surveyor_gui/models/column_methods.rb +43 -0
  184. data/lib/surveyor_gui/models/dependency_condition_methods.rb +53 -0
  185. data/lib/surveyor_gui/models/dependency_methods.rb +83 -0
  186. data/lib/surveyor_gui/models/question_and_group_shared_methods.rb +11 -0
  187. data/lib/surveyor_gui/models/question_group_methods.rb +55 -0
  188. data/lib/surveyor_gui/models/question_methods.rb +435 -0
  189. data/lib/surveyor_gui/models/question_type_methods.rb +493 -0
  190. data/lib/surveyor_gui/models/response_methods.rb +71 -0
  191. data/lib/surveyor_gui/models/response_set_methods.rb +56 -0
  192. data/lib/surveyor_gui/models/row_methods.rb +11 -0
  193. data/lib/surveyor_gui/models/survey_methods.rb +32 -0
  194. data/lib/surveyor_gui/models/survey_section_methods.rb +32 -0
  195. data/lib/surveyor_gui/surveyforms_controller_methods.rb +258 -0
  196. data/lib/surveyor_gui/version.rb +3 -0
  197. data/lib/surveyor_gui.rb +17 -0
  198. data/lib/tasks/.gitkeep +0 -0
  199. data/lib/tasks/surveyor_gui_tasks.rake +4 -0
  200. data/lib/templates/erb/scaffold/_form.html.erb +13 -0
  201. data/spec/controllers/surveyforms_controller_spec.rb +362 -0
  202. data/spec/controllers/surveyor_controller_spec.rb +303 -0
  203. data/spec/factories.rb +181 -0
  204. data/spec/features/create_survey_spec.rb +417 -0
  205. data/spec/features/dependencies_spec.rb +65 -0
  206. data/spec/features/make_and_take_spec.rb +187 -0
  207. data/spec/features/rearrange_survey_spec.rb +118 -0
  208. data/spec/features/ui_spec.rb +469 -0
  209. data/spec/fixtures/REDCapDemoDatabase_DataDictionary.csv +127 -0
  210. data/spec/fixtures/chinese_survey.rb +14 -0
  211. data/spec/fixtures/everything.rb +215 -0
  212. data/spec/fixtures/favorites-ish.rb +22 -0
  213. data/spec/fixtures/favorites.rb +22 -0
  214. data/spec/fixtures/feelings.rb +38 -0
  215. data/spec/fixtures/lifestyle.rb +55 -0
  216. data/spec/fixtures/numbers.rb +21 -0
  217. data/spec/fixtures/redcap_new_headers.csv +1 -0
  218. data/spec/fixtures/redcap_siblings.csv +1 -0
  219. data/spec/fixtures/redcap_whitespace.csv +1 -0
  220. data/spec/helpers/formtastic_custom_input_spec.rb +15 -0
  221. data/spec/helpers/surveyor_helper_spec.rb +116 -0
  222. data/spec/lib/common_spec.rb +37 -0
  223. data/spec/lib/parser_spec.rb +393 -0
  224. data/spec/lib/rake_kitchen_sink.rb +42 -0
  225. data/spec/lib/redcap_parser_spec.rb +129 -0
  226. data/spec/lib/unparser_spec.rb +126 -0
  227. data/spec/models/answer_spec.rb +144 -0
  228. data/spec/models/dependency_condition_spec.rb +428 -0
  229. data/spec/models/dependency_spec.rb +90 -0
  230. data/spec/models/question_group_spec.rb +66 -0
  231. data/spec/models/question_spec.rb +176 -0
  232. data/spec/models/response_set_spec.rb +452 -0
  233. data/spec/models/response_spec.rb +208 -0
  234. data/spec/models/survey_section_spec.rb +58 -0
  235. data/spec/models/survey_spec.rb +155 -0
  236. data/spec/models/surveyor_gui/question_spec.rb +60 -0
  237. data/spec/models/surveyor_gui/question_type_spec.rb +268 -0
  238. data/spec/models/validation_condition_spec.rb +98 -0
  239. data/spec/models/validation_spec.rb +64 -0
  240. data/spec/spec.opts +4 -0
  241. data/spec/spec_helper.rb +119 -0
  242. data/spec/support/scenario_outline_helper.rb +39 -0
  243. data/spec/support/shared.rb +10 -0
  244. data/spec/support/surveyforms_creation_helpers.rb +313 -0
  245. data/spec/support/surveyforms_rearrangement_helpers.rb +170 -0
  246. data/spec/support/surveyor_api_helpers.rb +15 -0
  247. data/spec/support/surveyor_ui_helpers.rb +108 -0
  248. data/spec/support/wait_for_ajax.rb +11 -0
  249. data/spec/views/questions/edit.html.erb_spec.rb +74 -0
  250. data/spec/views/surveyforms/edit.html.erb_spec.rb +126 -0
  251. data/surveyor_gui.gemspec +52 -0
  252. data/test/dummy/README.rdoc +261 -0
  253. data/test/dummy/Rakefile +7 -0
  254. data/test/dummy/app/assets/javascripts/application.js +15 -0
  255. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  256. data/test/dummy/app/controllers/application_controller.rb +3 -0
  257. data/test/dummy/app/helpers/application_helper.rb +2 -0
  258. data/test/dummy/app/mailers/.gitkeep +0 -0
  259. data/test/dummy/app/models/.gitkeep +0 -0
  260. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  261. data/test/dummy/config/application.rb +59 -0
  262. data/test/dummy/config/boot.rb +10 -0
  263. data/test/dummy/config/database.yml +25 -0
  264. data/test/dummy/config/environment.rb +5 -0
  265. data/test/dummy/config/environments/development.rb +37 -0
  266. data/test/dummy/config/environments/production.rb +67 -0
  267. data/test/dummy/config/environments/test.rb +37 -0
  268. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  269. data/test/dummy/config/initializers/inflections.rb +15 -0
  270. data/test/dummy/config/initializers/mime_types.rb +5 -0
  271. data/test/dummy/config/initializers/secret_token.rb +7 -0
  272. data/test/dummy/config/initializers/session_store.rb +8 -0
  273. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  274. data/test/dummy/config/locales/en.yml +5 -0
  275. data/test/dummy/config/routes.rb +58 -0
  276. data/test/dummy/config.ru +4 -0
  277. data/test/dummy/lib/assets/.gitkeep +0 -0
  278. data/test/dummy/log/.gitkeep +0 -0
  279. data/test/dummy/public/404.html +26 -0
  280. data/test/dummy/public/422.html +26 -0
  281. data/test/dummy/public/500.html +25 -0
  282. data/test/dummy/public/favicon.ico +0 -0
  283. data/test/dummy/script/rails +6 -0
  284. data/test/surveyor-gui_test.rb +7 -0
  285. data/test/test_helper.rb +15 -0
  286. data/tmp/cache/assets/CF0/DA0/sprockets%2Fd7d5b37686831d37c4dd75e645f5e016 +0 -0
  287. data/tmp/cache/assets/E25/4C0/sprockets%2Fde2fd9fd11c04a582cdbbe3d84a35ae6 +0 -0
  288. data/vendor/assets/stylesheets/.gitkeep +0 -0
  289. data/vendor/assets/stylesheets/custom.sass +5 -0
  290. data/vendor/plugins/.gitkeep +0 -0
  291. metadata +600 -0
@@ -0,0 +1,187 @@
1
+ class SurveyorGui::QuestionsController < ApplicationController
2
+ layout 'surveyor_gui/surveyor_gui_blank'
3
+
4
+ def new
5
+ @title = "Add Question"
6
+ survey_section = SurveySection.find(params[:survey_section_id])
7
+ survey = Survey.find(survey_section.survey_id)
8
+ @question_group = QuestionGroup.new
9
+ if params[:prev_question_id]
10
+ prev_question_display_order = _get_prev_display_order(params[:prev_question_id])
11
+ @question = Question.new(:survey_section_id => params[:survey_section_id],
12
+ :text => params[:text],
13
+ :display_type => "default",
14
+ :display_order => prev_question_display_order)
15
+ else
16
+ @question = Question.new(:survey_section_id => params[:survey_section_id],
17
+ :text => params[:text],
18
+ :display_type => "default",
19
+ :display_order => params[:display_order] || 0)
20
+ end
21
+ @question.question_type_id = params[:question_type_id] if !params[:question_type_id].blank?
22
+ @question.answers.build(:text => '', :response_class=>"string")
23
+ end
24
+
25
+ def edit
26
+ @title = "Edit Question"
27
+ @question = Question.includes(:answers).find(params[:id])
28
+ @question.question_type_id = params[:question_type_id] if !params[:question_type_id].blank?
29
+ end
30
+
31
+ def adjusted_text
32
+ if @question.part_of_group?
33
+ @question.question_group.text
34
+ else
35
+ @question.text
36
+ end
37
+ end
38
+
39
+ helper_method :adjusted_text
40
+
41
+ def create
42
+ Question.where(:survey_section_id => params[:question][:survey_section_id])
43
+ .where("display_order >= ?", params[:question][:display_order])
44
+ .update_all("display_order = display_order+1")
45
+ if !params[:question][:answers_attributes].blank? && !params[:question][:answers_attributes]['0'].blank?
46
+ params[:question][:answers_attributes]['0'][:original_choice] = params[:question][:answers_attributes]['0'][:text]
47
+ end
48
+ @question = Question.new(question_params)
49
+ if @question.save
50
+ @question.answers.each_with_index {|a, index| a.destroy if index > 0} if @question.pick == 'none'
51
+ #load any page - if it has no flash errors, the colorbox that contains it will be closed immediately after the page loads
52
+ render :inline => '<div id="cboxQuestionId">'+@question.id.to_s+'</div>', :layout => 'surveyor_gui/surveyor_gui_blank'
53
+ else
54
+ @title = "Add Question"
55
+ render :action => 'new', :layout => 'surveyor_gui/surveyor_gui_blank'
56
+ end
57
+ end
58
+
59
+ def update
60
+ @title = "Update Question"
61
+ @question = Question.includes(:answers).find(params[:id])
62
+ if @question.update_attributes(question_params)
63
+ @question.answers.each_with_index {|a, index| a.destroy if index > 0} if @question.pick == 'none'
64
+ #load any page - if it has no flash errors, the colorbox that contains it will be closed immediately after the page loads
65
+ render :blank, :layout => 'surveyor_gui/surveyor_gui_blank'
66
+ else
67
+ render :action => 'edit', :layout => 'surveyor_gui/surveyor_gui_blank'
68
+ end
69
+ end
70
+
71
+ def destroy
72
+ question = Question.find(params[:id])
73
+ if !question.survey_section.survey.template && question.survey_section.survey.response_sets.count > 0
74
+ flash[:error]="Reponses have already been collected for this survey, therefore it cannot be modified. Please create a new survey instead."
75
+ return false
76
+ end
77
+ if !question.dependency_conditions.blank?
78
+ render :text=>"The following questions have logic that depend on this question: \n\n"+question.dependency_conditions.map{|d| " - "+d.dependency.question.text}.join('\n')+"\n\nPlease delete logic before deleting this question.".html_safe
79
+ return
80
+ end
81
+ if question.part_of_group?
82
+ question.question_group.questions.each{|q| q.destroy}
83
+ render :text=>''
84
+ return
85
+ end
86
+ question.destroy
87
+ render :text=>''
88
+ end
89
+
90
+ def sort
91
+ survey = Surveyform.find(params[:survey_id])
92
+ survey.sort_as_per_array(params)
93
+ render :nothing => true
94
+ end
95
+
96
+ def cut_question
97
+ session[:cut_question]=params[:id]
98
+ if q=Question.find(params[:id])
99
+ q.update_attribute(:survey_section_id,nil)
100
+ end
101
+ redirect_to :back
102
+ end
103
+
104
+ def render_answer_fields_partial
105
+ if params[:id].blank?
106
+ @questions = Question.new
107
+ else
108
+ @questions = Question.find(params[:id])
109
+ end
110
+ if @questions.answers.empty?
111
+ @questions.answers.build(:text=>'')
112
+ else
113
+ if !@questions.answers.first.original_choice.blank?
114
+ @questions.answers.first.update_attribute(:text,@questions.answers.first.original_choice)
115
+ end
116
+ if params[:add_row]
117
+ display_order = @questions.answers.maximum(:display_order)
118
+ display_order = display_order ? display_order + 1 : 0
119
+ @questions = Question.new
120
+ @questions.answers.build(:text=>'', :display_order=>display_order)
121
+ end
122
+ end
123
+ render :partial => 'answer_fields'
124
+ end
125
+
126
+ def render_grid_partial
127
+ if params[:id].blank?
128
+ @questions = Question.new
129
+ else
130
+ @questions = Question.find(params[:id])
131
+ end
132
+ if @questions.answers.empty?
133
+ @questions.answers.build(text: "", response_class: "answer")
134
+ else
135
+ if !@questions.answers.first.original_choice.blank?
136
+ @questions.answers.first.update_attribute(:text,@questions.answers.first.original_choice)
137
+ end
138
+ end
139
+ if @questions.question_group
140
+ @question_group=@questions.question_group
141
+ else
142
+ @question_group=QuestionGroup.new
143
+ @question_group.columns.build
144
+ end
145
+ column_count = @question_group.columns.size
146
+ requested_columns = params[:index] == "NaN" ? column_count : params[:index].to_i
147
+ if requested_columns >= column_count
148
+ requested_columns = requested_columns - column_count
149
+ (requested_columns).times.each {@question_group.columns.build}
150
+ else
151
+ @question_group.trim_columns (column_count-requested_columns)
152
+ end
153
+ @questions.dropdown_column_count = requested_columns.to_i+1
154
+ if params[:question_type_id] == "grid_dropdown"
155
+ render :partial => 'grid_dropdown_fields'
156
+ else
157
+ render :partial => 'grid_fields'
158
+ end
159
+ end
160
+
161
+
162
+ def render_no_picks_partial
163
+ if params[:id].blank?
164
+ @questions = Question.new
165
+ else
166
+ @questions = Question.find(params[:id])
167
+ end
168
+ if @questions.answers.empty?
169
+ @questions.answers.build(:text=>'')
170
+ end
171
+ render :partial => 'no_picks'
172
+ end
173
+
174
+ private
175
+ def question_params
176
+ ::PermittedParams.new(params[:question]).question
177
+ end
178
+
179
+ def _get_prev_display_order(prev_question)
180
+ prev_question = Question.find(prev_question)
181
+ if prev_question.part_of_group?
182
+ prev_question.question_group.questions.last.display_order + 1
183
+ else
184
+ prev_question.display_order + 1
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,339 @@
1
+ require 'complex'
2
+ include Math
3
+
4
+ class SurveyorGui::ReportsController < ApplicationController
5
+ include ReportPreviewWrapper
6
+
7
+ # ReportPreviewWrapper wraps preview in a database transaction so test data is not permanently saved.
8
+ around_action :wrap_in_transaction, only: :preview
9
+ layout 'surveyor_gui/surveyor_gui_default'
10
+
11
+ def preview
12
+ response_qty = 5
13
+ user_ids = response_qty.times.map{|i| -1*i}
14
+ @title = "Preview Report for "+response_qty.to_s+" randomized responses"
15
+ @survey = Survey.find(params[:survey_id])
16
+ user_ids.each do |user_id|
17
+ @response_set = ResponseSet.create(survey: @survey, user_id: user_id, test_data: true)
18
+ ReportResponseGenerator.new(@survey).generate_1_result_set(@response_set)
19
+ end
20
+ @response_sets = ResponseSet.where(survey_id: @survey.id, test_data: true).where('user_id in (?)', user_ids)
21
+ @responses = Response.joins(:response_set, :answer).where('user_id in (?) and survey_id = ? and test_data = ? and answers.is_comment = ?',user_ids,params[:survey_id],true, false)
22
+ if (!@survey)
23
+ flash[:notice] = "Survey/Questionnnaire not found."
24
+ redirect_to :back
25
+ end
26
+ generate_report(params[:survey_id], true)
27
+ render :show
28
+ end
29
+
30
+ def show
31
+ @survey = Survey.find(params[:id])
32
+ @response_sets = ResponseSet.where(survey_id: @survey.id, test_data: false)
33
+ @responses = Response.joins(:response_set, :answer).where('survey_id = ? and test_data = ? and answers.is_comment=?',@survey.id,false, false)
34
+ @title = "Show report for #{@survey.title}"
35
+ if @responses.count > 0
36
+ generate_report(@survey.id, false)
37
+ else
38
+ flash[:error] = "No responses have been collected for this survey"
39
+ redirect_to surveyforms_path
40
+ end
41
+ end
42
+
43
+ def generate_report(survey_id, test)
44
+ questions = Question.joins(:survey_section).where('survey_sections.survey_id = ? and is_comment = ?', survey_id, false)
45
+ # multiple_choice_responses = Response.joins(:response_set, :answer).where('survey_id = ? and test_data = ?',survey_id,test).group('responses.question_id','answers.id','answers.text').select('responses.question_id, answers.id, answers.text as text, count(*) as answer_count').order('responses.question_id','answers.id')
46
+
47
+ # multiple_choice_responses = Answer.unscoped.joins(:question=>:survey_section).includes(:responses=>:response_set).where('survey_sections.survey_id=? and (response_sets.test_data=? or response_sets.test_data is null)',survey_id,test).group('answers.question_id','answers.id','answers.text').select('answers.question_id, answers.id, answers.text as text, count(*) as answer_count').order('answers.question_id','answers.id')
48
+
49
+ # multiple_choice_responses = Answer.unscoped.find(:all,
50
+ # :joins => "LEFT JOIN responses ON responses.answer_id = answers.id",
51
+ # :select => "answers.question_id, answers.id, answers.text as text, count(answers.*) as answer_count",
52
+ # :group => "answers.question_id, answers.id, answers.text",
53
+ # :order => "answers.question_id, answers.id")
54
+
55
+ multiple_choice_responses = Answer.unscoped.joins("LEFT OUTER JOIN responses ON responses.answer_id = answers.id
56
+ LEFT OUTER JOIN response_sets ON response_sets.id = responses.response_set_id").
57
+ joins(:question=>:survey_section).
58
+ where('survey_sections.survey_id=? and (response_sets.test_data=? or response_sets.test_data is null)',survey_id,test).
59
+ select("answers.question_id, answers.id, answers.text as text, answers.is_comment, count(responses.id) as answer_count").
60
+ group("answers.question_id, answers.id, answers.text, answers.is_comment").
61
+ order("answers.question_id, answers.id")
62
+
63
+ single_choice_responses = Response.joins(:response_set).where('survey_id = ? and test_data = ?',survey_id,test).select('responses.question_id, responses.answer_id,
64
+ responses.float_value,
65
+ responses.integer_value,
66
+ responses.datetime_value,
67
+ responses.string_value')
68
+ @chart = {}
69
+ colors = ['#4572A7', '#AA4643', '#89A54E', '#80699B', '#3D96AE', '#DB843D', '#92A8CD', '#A47D7C', '#B5CA92']
70
+ questions.each do |q|
71
+ if [:grid_one, :grid_any].include? q.question_type_id
72
+ generate_stacked_bar_chart(q, multiple_choice_responses, colors)
73
+ elsif q.question_type_id == :grid_dropdown
74
+ q.question_group.questions.where(is_comment: false).each do |question|
75
+ generate_grid_dropdown_bar_chart(question, multiple_choice_responses, colors)
76
+ end
77
+ elsif q.pick == 'one'
78
+ generate_pie_chart(q, multiple_choice_responses)
79
+ elsif q.pick == 'any'
80
+ generate_bar_chart(q, multiple_choice_responses, colors)
81
+ elsif [:number,:date,:datetime,:time].include? q.question_type_id
82
+ generate_histogram_chart(q, single_choice_responses)
83
+ end
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def generate_pie_chart(q, responses)
90
+ piearray = []
91
+ responses.where(:question_id => q.id).each_with_index do |a, index|
92
+ piearray[index]= {:y=> a.answer_count.to_i, :name => a.text.to_s} if !a.is_comment?
93
+ end
94
+ @chart[q.id.to_s] = LazyHighCharts::HighChart.new('graph') do |f|
95
+ f.options[:chart][:plotBorderWidth] = nil
96
+ f.options[:chart][:plotBackgroundColor] = nil
97
+ f.options[:title][:text] = q.text
98
+ f.plot_options(:pie=>{
99
+ :allowPointSelect=>true,
100
+ :cursor=>"pointer" ,
101
+ :dataLabels=>{
102
+ :enabled=>true,
103
+ :color=>"#000000",
104
+ :connectorColor=>"#000000"
105
+ },
106
+ :enableMouseTracking => false,
107
+ :shadow => false,
108
+ :animation => false
109
+ })
110
+ f.series( :type => 'pie',
111
+ :name=> q.text,
112
+ :data => piearray
113
+ )
114
+ end
115
+ end
116
+
117
+ def generate_bar_chart(q, responses, colors)
118
+ bararray = []
119
+ responses.where(:question_id => q.id).each_with_index do |a, index|
120
+ bararray[index]= {:y=> a.answer_count.to_i, :color => colors[index].to_s} if !a.is_comment?
121
+ end
122
+ @chart[q.id.to_s] = LazyHighCharts::HighChart.new('graph') do |f|
123
+ f.options[:chart][:defaultSeriesType] = 'column'
124
+ f.options[:title][:text] = q.text
125
+ f.options[:xAxis][:categories] = q.answers.order('answers.id').map{|a| a.text}
126
+ f.options[:xAxis][:labels] = {:rotation=> -45, :align => 'right'}
127
+ f.options[:yAxis][:min] = 0
128
+ f.options[:yAxis][:title] = {:text => 'Count'}
129
+ f.plot_options(
130
+ :pointPadding=>true,
131
+ :borderWidth => 0,
132
+ :enableMouseTracking => false,
133
+ :shadow => false,
134
+ :animation => false,
135
+ :stickyTracking => false
136
+ )
137
+ f.series( :data => bararray,
138
+ :dataLabels => {
139
+ :enabled=>true
140
+ } )
141
+ end
142
+ end
143
+
144
+ def generate_stacked_bar_chart(q, responses, colors)
145
+ @chart[q.id.to_s] = LazyHighCharts::HighChart.new('graph') do |f|
146
+ f.options[:chart][:defaultSeriesType] = 'column'
147
+ f.options[:title][:text] = q.question_group.text
148
+ f.options[:xAxis][:categories] = q.answers.order('answers.id').map{|a| a.text}
149
+ f.options[:xAxis][:labels] = {:rotation=> -45, :align => 'right'}
150
+ f.options[:yAxis][:min] = 0
151
+ f.options[:yAxis][:title] = {:text => 'Count'}
152
+ f.plot_options(
153
+ :column => {
154
+ :stacking => 'normal',
155
+ dataLabels: {
156
+ enabled: true,
157
+ color: 'black',
158
+ style: {
159
+ fontWeight: 'bold',
160
+ fontSize: '12px'
161
+ }
162
+ }
163
+ },
164
+ :pointPadding=>true,
165
+ :borderWidth => 0,
166
+ :enableMouseTracking => false,
167
+ :shadow => false,
168
+ :animation => false,
169
+ :stickyTracking => false
170
+ )
171
+
172
+ q.question_group.questions.where(is_comment: false).each_with_index do |question, question_index|
173
+ bararray = []
174
+ responses.where(:question_id => question.id).each_with_index do |a, answer_index|
175
+ bararray[answer_index]= {:y=> a.answer_count.to_i}
176
+ end
177
+ f.series(
178
+ name: question.text,
179
+ data: bararray,
180
+ color: colors[question_index].to_s
181
+ )
182
+ end
183
+ end
184
+ end
185
+
186
+ def generate_grid_dropdown_bar_chart(q, responses, colors)
187
+ @chart[q.id.to_s] = LazyHighCharts::HighChart.new('graph') do |f|
188
+ f.options[:chart][:defaultSeriesType] = 'column'
189
+ f.options[:title][:text] = q.text
190
+ f.options[:xAxis][:categories] = q.question_group.columns.map{|c| c.text}
191
+ f.options[:xAxis][:labels] = {:rotation=> -45, :align => 'right'}
192
+ f.options[:yAxis][:min] = 0
193
+ f.options[:yAxis][:title] = {:text => 'Count'}
194
+ f.plot_options(
195
+ :column => {
196
+ :stacking => 'normal',
197
+ dataLabels: {
198
+ enabled: true,
199
+ color: 'black',
200
+ style: {
201
+ fontWeight: 'bold',
202
+ fontSize: '12px'
203
+ }
204
+ }
205
+ },
206
+ :pointPadding=>true,
207
+ :borderWidth => 0,
208
+ :enableMouseTracking => false,
209
+ :shadow => false,
210
+ :animation => false,
211
+ :stickyTracking => false
212
+ )
213
+
214
+ series = []
215
+ q.question_group.columns.each_with_index do |column, column_index|
216
+ q.answers.where(column_id: column.id).each_with_index do |answer, answer_index|
217
+ response_count = Response.where(question_id: q.id, answer_id: answer.id, column_id: column.id).count
218
+ series<<{column_id: column.id, answer_id: answer.id, name: answer.text, y: response_count.to_i}
219
+ end
220
+ end
221
+ series.map{|a| a[:name]}.uniq.each_with_index do |answer_name, answer_index|
222
+ bararray=[]
223
+ q.question_group.columns.each do |column|
224
+ match = series.select{|s| s[:name]==answer_name && s[:column_id]==column.id}.first
225
+ bararray << (match ? match[:y] : 0)
226
+ end
227
+ f.series(
228
+ name: answer_name,
229
+ data: bararray,
230
+ color: colors[answer_index].to_s
231
+ )
232
+ end
233
+ end
234
+ end
235
+
236
+ def generate_histogram_chart(q, responses)
237
+ suffix = q.suffix
238
+ histarray = HistogramArray.new(q, responses.where(:question_id => q.id), suffix).calculate
239
+ @chart[q.id.to_s] = LazyHighCharts::HighChart.new('graph') do |f|
240
+ f.options[:chart][:defaultSeriesType] = 'column'
241
+ f.options[:title][:text] = 'Histogram for "'+q.text+'"'
242
+ f.options[:legend][:enabled] = false
243
+ f.options[:xAxis][:categories] = histarray.map{|h| h[:x]}
244
+ f.options[:xAxis][:labels] = {:rotation=> -45, :align => 'right'}
245
+ f.options[:yAxis][:min] = 0
246
+ f.options[:yAxis][:title] = {:text => 'Occurrences'}
247
+ f.plot_options(
248
+ :pointPadding=>true,
249
+ :borderWidth => 0,
250
+ :enableMouseTracking => false,
251
+ :shadow => false,
252
+ :animation => false
253
+ )
254
+ f.series( :data=> histarray.map{|h| h[:y]},
255
+ :dataLabels => {
256
+ :enabled=>true
257
+ }
258
+ )
259
+ end
260
+ end
261
+
262
+ def report_params
263
+ @report_params ||= params.permit(:survey_id, :id)
264
+ end
265
+
266
+ end
267
+
268
+ class HistogramArray
269
+ def initialize(question, in_arr, label=nil)
270
+ @out_arr = []
271
+ p "in arr at init #{in_arr.map{|a| a}}"
272
+ @in_arr = in_arr.map{|a| a.response_value}
273
+ return if in_arr.empty?
274
+ @question = question
275
+ set_min
276
+ set_max
277
+ set_count
278
+ set_distribution
279
+ set_step
280
+ end
281
+
282
+ def calculate
283
+ if !@in_arr.empty?
284
+ @distribution.times do |index|
285
+ refresh_range
286
+ set_x_label
287
+ @out_arr[index]= {
288
+ :x => @x_label,
289
+ :y => @in_arr.select {|v| v.to_f >= @lower_bound && v.to_f < @upper_bound}.count
290
+ }
291
+ end
292
+ end
293
+ return @out_arr
294
+ end
295
+
296
+ private
297
+
298
+ def set_min
299
+ @min = @range = @in_arr.min.to_f
300
+ end
301
+
302
+ def set_max
303
+ @max = @in_arr.max.to_f
304
+ @max = @max + @max.abs*0.00000000001
305
+ end
306
+
307
+ def set_count
308
+ @count = @in_arr.count
309
+ end
310
+
311
+ def set_distribution
312
+ @distribution = sqrt(@count).round
313
+ end
314
+
315
+ def set_step
316
+ @step = ((@max-@min)/@distribution)
317
+ end
318
+
319
+ def refresh_range
320
+ @lower_bound = @range
321
+ @upper_bound = @range+@step
322
+ @range = @upper_bound
323
+ end
324
+
325
+ def trunc_range(num)
326
+ return (num*10000000000).to_i/10000000000
327
+ end
328
+
329
+ def set_x_label
330
+ if @question.question_type_id == :number
331
+ @x_label = trunc_range(@lower_bound).to_s+' to '+trunc_range(@upper_bound).to_s+' '+@label.to_s
332
+ else
333
+ response_formatter = ReportFormatter.new(@question, @in_arr)
334
+ lower_bound = response_formatter.format_stats(@lower_bound)
335
+ upper_bound = response_formatter.format_stats(@upper_bound)
336
+ @x_label = lower_bound+' to '+upper_bound+' '+@label.to_s
337
+ end
338
+ end
339
+ end