surveyor_gui 0.0.3

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 (283) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +44 -0
  3. data/.travis.yml +17 -0
  4. data/Gemfile +7 -0
  5. data/LICENSE +21 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +159 -0
  8. data/Rakefile +66 -0
  9. data/app/controllers/answers_controller.rb +3 -0
  10. data/app/controllers/application_controller.rb +3 -0
  11. data/app/controllers/dependencys_controller.rb +286 -0
  12. data/app/controllers/question_groups_controller.rb +84 -0
  13. data/app/controllers/questions_controller.rb +187 -0
  14. data/app/controllers/survey_sections_controller.rb +80 -0
  15. data/app/controllers/surveyforms_controller.rb +4 -0
  16. data/app/controllers/surveyor_controller.rb +89 -0
  17. data/app/controllers/surveyor_gui/reports_controller.rb +339 -0
  18. data/app/controllers/surveyor_gui/responses_controller.rb +39 -0
  19. data/app/controllers/surveyor_gui/survey_controller.rb +16 -0
  20. data/app/facades/report_formatter.rb +44 -0
  21. data/app/facades/report_preview_wrapper.rb +11 -0
  22. data/app/facades/report_response_generator.rb +147 -0
  23. data/app/helpers/application_helper.rb +2 -0
  24. data/app/helpers/dependency_helper.rb +5 -0
  25. data/app/helpers/question_helper.rb +22 -0
  26. data/app/helpers/surveyform_helper.rb +179 -0
  27. data/app/helpers/surveyor_gui/report_helper.rb +19 -0
  28. data/app/helpers/surveyor_helper.rb +4 -0
  29. data/app/inputs/currency_input.rb +5 -0
  30. data/app/inputs/date_picker_input.rb +7 -0
  31. data/app/inputs/datetime_picker_input.rb +5 -0
  32. data/app/inputs/percent_input.rb +5 -0
  33. data/app/inputs/time_picker_input.rb +5 -0
  34. data/app/mailers/.gitkeep +0 -0
  35. data/app/models/.gitkeep +0 -0
  36. data/app/models/answer.rb +4 -0
  37. data/app/models/column.rb +3 -0
  38. data/app/models/dependency.rb +4 -0
  39. data/app/models/dependency_condition.rb +4 -0
  40. data/app/models/permitted_params_decorator.rb +80 -0
  41. data/app/models/question.rb +4 -0
  42. data/app/models/question_group.rb +4 -0
  43. data/app/models/question_type.rb +7 -0
  44. data/app/models/response.rb +4 -0
  45. data/app/models/response_set.rb +4 -0
  46. data/app/models/row.rb +3 -0
  47. data/app/models/survey.rb +4 -0
  48. data/app/models/survey_section.rb +4 -0
  49. data/app/models/surveyform.rb +103 -0
  50. data/app/uploaders/blob_uploader.rb +48 -0
  51. data/app/views/dependencys/_column.html.erb +1 -0
  52. data/app/views/dependencys/_dependency.html.erb +22 -0
  53. data/app/views/dependencys/_dependency_condition_fields.html.erb +48 -0
  54. data/app/views/dependencys/_form.html.erb +20 -0
  55. data/app/views/dependencys/blank.html.erb +0 -0
  56. data/app/views/dependencys/edit.html.erb +1 -0
  57. data/app/views/dependencys/new.html.erb +1 -0
  58. data/app/views/layouts/application.html.erb +14 -0
  59. data/app/views/layouts/surveyor_gui_blank.html.erb +12 -0
  60. data/app/views/layouts/surveyor_gui_default.html.erb +16 -0
  61. data/app/views/layouts/surveyor_modified.html.erb +14 -0
  62. data/app/views/partials/_answer.html.haml +33 -0
  63. data/app/views/partials/_question.html.haml +33 -0
  64. data/app/views/partials/_question_group.html.haml +73 -0
  65. data/app/views/partials/_section.html.haml +13 -0
  66. data/app/views/question_groups/_form.html.erb +56 -0
  67. data/app/views/question_groups/_group_inline_field.html.erb +21 -0
  68. data/app/views/question_groups/_group_inline_fields.html.erb +25 -0
  69. data/app/views/question_groups/blank.html.erb +0 -0
  70. data/app/views/question_groups/edit.html.erb +1 -0
  71. data/app/views/question_groups/new.html.erb +1 -0
  72. data/app/views/questions/_answer_fields.html.erb +23 -0
  73. data/app/views/questions/_answer_options.html.erb +28 -0
  74. data/app/views/questions/_form.html.erb +65 -0
  75. data/app/views/questions/_grid_dropdown_columns.html.erb +10 -0
  76. data/app/views/questions/_grid_dropdown_fields.html.erb +42 -0
  77. data/app/views/questions/_grid_fields.html.erb +9 -0
  78. data/app/views/questions/_no_picks.html.erb +29 -0
  79. data/app/views/questions/_pick.html +21 -0
  80. data/app/views/questions/_picks.html.erb +12 -0
  81. data/app/views/questions/blank.html.erb +0 -0
  82. data/app/views/questions/edit.html.erb +1 -0
  83. data/app/views/questions/new.html.erb +1 -0
  84. data/app/views/survey_sections/_form.html.erb +13 -0
  85. data/app/views/survey_sections/blank.html.erb +0 -0
  86. data/app/views/survey_sections/edit.html.erb +1 -0
  87. data/app/views/survey_sections/new.html.erb +1 -0
  88. data/app/views/surveyforms/_form.html.erb +50 -0
  89. data/app/views/surveyforms/_question_field.html.erb +148 -0
  90. data/app/views/surveyforms/_question_group.html.erb +116 -0
  91. data/app/views/surveyforms/_question_group_fields.html.erb +3 -0
  92. data/app/views/surveyforms/_question_name_and_number.html.erb +6 -0
  93. data/app/views/surveyforms/_question_section.html.erb +15 -0
  94. data/app/views/surveyforms/_question_wrapper.html.erb +109 -0
  95. data/app/views/surveyforms/_survey_section_fields.html.erb +138 -0
  96. data/app/views/surveyforms/clone_survey.html.erb +13 -0
  97. data/app/views/surveyforms/edit.html.erb +5 -0
  98. data/app/views/surveyforms/index.html.erb +40 -0
  99. data/app/views/surveyforms/new.html.erb +1 -0
  100. data/app/views/surveyforms/show.html.erb +5 -0
  101. data/app/views/surveyor_gui/reports/_graphs.html.haml +21 -0
  102. data/app/views/surveyor_gui/reports/_grid.html.haml +42 -0
  103. data/app/views/surveyor_gui/reports/_grid_dropdown.html.haml +56 -0
  104. data/app/views/surveyor_gui/reports/_repeater.html.haml +28 -0
  105. data/app/views/surveyor_gui/reports/_show_report.html.haml +33 -0
  106. data/app/views/surveyor_gui/reports/_single_question.html.haml +70 -0
  107. data/app/views/surveyor_gui/reports/show.html.erb +14 -0
  108. data/app/views/surveyor_gui/responses/_grid.html.haml +20 -0
  109. data/app/views/surveyor_gui/responses/_grid_dropdown.html.haml +20 -0
  110. data/app/views/surveyor_gui/responses/_repeater.html.haml +22 -0
  111. data/app/views/surveyor_gui/responses/_survey_results.html.haml +40 -0
  112. data/app/views/surveyor_gui/responses/index.html.haml +24 -0
  113. data/app/views/surveyor_gui/responses/show.html.haml +42 -0
  114. data/app/views/surveyor_gui/shared/_grid_comments.html.haml +10 -0
  115. data/app/views/surveyor_gui/shared/_new_line.html.haml +2 -0
  116. data/app/views/surveyor_gui/shared/_pick_comments.html.haml +15 -0
  117. data/app/views/surveyor_gui/shared/_question_number.html.haml +9 -0
  118. data/app/views/surveyor_gui/shared/_report_data.html.haml +24 -0
  119. data/app/views/surveyor_gui/shared/_stars_report_data.html.haml +14 -0
  120. data/config.ru +4 -0
  121. data/config/environment.rb +0 -0
  122. data/config/routes.rb +71 -0
  123. data/db/migrate/20140307204049_add_template_to_surveys.rb +5 -0
  124. data/db/migrate/20140307235607_add_test_data_to_response_sets.rb +5 -0
  125. data/db/migrate/20140308171947_add_original_choice_to_answers.rb +5 -0
  126. data/db/migrate/20140308172118_add_blob_to_responses.rb +5 -0
  127. data/db/migrate/20140308172417_add_modifiable_to_survey_section.rb +5 -0
  128. data/db/migrate/20140308174532_add_modifiable_to_question.rb +5 -0
  129. data/db/migrate/20140308175305_add_dynamically_generate_to_questions.rb +5 -0
  130. data/db/migrate/20140311032923_add_dummy_blob_to_questions.rb +5 -0
  131. data/db/migrate/20140311160609_add_dynamic_source_to_questions.rb +5 -0
  132. data/db/migrate/20140311161714_add_report_code_to_questions.rb +5 -0
  133. data/db/migrate/20140530181134_add_is_comment_to_questions.rb +5 -0
  134. data/db/migrate/20140531012006_add_is_comment_to_answers.rb +5 -0
  135. data/db/migrate/20140531225529_create_rows.rb +9 -0
  136. data/db/migrate/20140601011151_create_columns.rb +11 -0
  137. data/db/migrate/20140602030330_add_column_id_to_answers.rb +5 -0
  138. data/db/migrate/20140603155606_add_column_id_to_responses.rb +5 -0
  139. data/db/migrate/20140606023527_add_column_id_to_dependency_conditions.rb +5 -0
  140. data/db/migrate/20140815165307_add_user_id_to_survey.rb +5 -0
  141. data/lib/assets/.gitkeep +0 -0
  142. data/lib/assets/images/addicon.png +0 -0
  143. data/lib/assets/images/datepicker.gif +0 -0
  144. data/lib/assets/images/delete.gif +0 -0
  145. data/lib/assets/images/delete.png +0 -0
  146. data/lib/assets/images/images/border.png +0 -0
  147. data/lib/assets/images/images/controls.png +0 -0
  148. data/lib/assets/images/images/loading.gif +0 -0
  149. data/lib/assets/images/images/loading_background.png +0 -0
  150. data/lib/assets/images/images/overlay.png +0 -0
  151. data/lib/assets/images/rails.png +0 -0
  152. data/lib/assets/images/star.gif +0 -0
  153. data/lib/assets/javascripts/.gitkeep +0 -0
  154. data/lib/assets/javascripts/surveyor_gui/jquery.MetaData.js +121 -0
  155. data/lib/assets/javascripts/surveyor_gui/jquery.blockUI.js +619 -0
  156. data/lib/assets/javascripts/surveyor_gui/jquery.rating.js +377 -0
  157. data/lib/assets/javascripts/surveyor_gui/jquery.validate.js +1188 -0
  158. data/lib/assets/javascripts/surveyor_gui/surveyor_add_ons.js +10 -0
  159. data/lib/assets/javascripts/surveyor_gui/surveyor_gui.js +1417 -0
  160. data/lib/assets/stylesheets/.gitkeep +0 -0
  161. data/lib/assets/stylesheets/surveyor_gui/jquery.rating.css +12 -0
  162. data/lib/assets/stylesheets/surveyor_gui/surveyor_add_ons.css +3 -0
  163. data/lib/assets/stylesheets/surveyor_gui/surveyor_gui.sass +650 -0
  164. data/lib/enumerable_extenders.rb +31 -0
  165. data/lib/generators/surveyor_gui/install_generator.rb +57 -0
  166. data/lib/generators/surveyor_gui/templates/app/assets/javascripts/surveyor_add_ons.js +2 -0
  167. data/lib/generators/surveyor_gui/templates/app/assets/javascripts/surveyor_gui_all.js +24 -0
  168. data/lib/generators/surveyor_gui/templates/app/assets/stylesheets/surveyor_add_ons.css.sass +1 -0
  169. data/lib/generators/surveyor_gui/templates/app/assets/stylesheets/surveyor_gui_all.css.sass +8 -0
  170. data/lib/generators/surveyor_gui/templates/app/helpers/surveyor_helper.rb +4 -0
  171. data/lib/generators/surveyor_gui/templates/app/models/response_set_user.rb +13 -0
  172. data/lib/generators/surveyor_gui/templates/app/views/layouts/surveyor_gui_default.html.erb +16 -0
  173. data/lib/generators/surveyor_gui/templates/config/initializers/simple_form.rb +140 -0
  174. data/lib/generators/surveyor_gui/templates/config/locales/en.yml +5 -0
  175. data/lib/surveyor_gui.rb +17 -0
  176. data/lib/surveyor_gui/engine.rb +30 -0
  177. data/lib/surveyor_gui/helpers/surveyor_helper_methods.rb +19 -0
  178. data/lib/surveyor_gui/models/answer_methods.rb +24 -0
  179. data/lib/surveyor_gui/models/column_methods.rb +43 -0
  180. data/lib/surveyor_gui/models/dependency_condition_methods.rb +53 -0
  181. data/lib/surveyor_gui/models/dependency_methods.rb +83 -0
  182. data/lib/surveyor_gui/models/question_and_group_shared_methods.rb +11 -0
  183. data/lib/surveyor_gui/models/question_group_methods.rb +55 -0
  184. data/lib/surveyor_gui/models/question_methods.rb +435 -0
  185. data/lib/surveyor_gui/models/question_type_methods.rb +493 -0
  186. data/lib/surveyor_gui/models/response_methods.rb +67 -0
  187. data/lib/surveyor_gui/models/response_set_methods.rb +54 -0
  188. data/lib/surveyor_gui/models/row_methods.rb +11 -0
  189. data/lib/surveyor_gui/models/survey_methods.rb +32 -0
  190. data/lib/surveyor_gui/models/survey_section_methods.rb +32 -0
  191. data/lib/surveyor_gui/surveyforms_controller_methods.rb +258 -0
  192. data/lib/surveyor_gui/version.rb +3 -0
  193. data/lib/tasks/.gitkeep +0 -0
  194. data/lib/tasks/surveyor_gui_tasks.rake +4 -0
  195. data/lib/templates/erb/scaffold/_form.html.erb +13 -0
  196. data/spec/controllers/surveyforms_controller_spec.rb +361 -0
  197. data/spec/controllers/surveyor_controller_spec.rb +303 -0
  198. data/spec/factories.rb +181 -0
  199. data/spec/features/create_survey_spec.rb +418 -0
  200. data/spec/features/dependencies_spec.rb +61 -0
  201. data/spec/features/rearrange_survey_spec.rb +118 -0
  202. data/spec/features/ui_spec.rb +469 -0
  203. data/spec/fixtures/REDCapDemoDatabase_DataDictionary.csv +127 -0
  204. data/spec/fixtures/chinese_survey.rb +14 -0
  205. data/spec/fixtures/everything.rb +215 -0
  206. data/spec/fixtures/favorites-ish.rb +22 -0
  207. data/spec/fixtures/favorites.rb +22 -0
  208. data/spec/fixtures/feelings.rb +38 -0
  209. data/spec/fixtures/lifestyle.rb +55 -0
  210. data/spec/fixtures/numbers.rb +21 -0
  211. data/spec/fixtures/redcap_new_headers.csv +1 -0
  212. data/spec/fixtures/redcap_siblings.csv +1 -0
  213. data/spec/fixtures/redcap_whitespace.csv +1 -0
  214. data/spec/helpers/formtastic_custom_input_spec.rb +15 -0
  215. data/spec/helpers/surveyor_helper_spec.rb +116 -0
  216. data/spec/lib/common_spec.rb +37 -0
  217. data/spec/lib/parser_spec.rb +393 -0
  218. data/spec/lib/rake_kitchen_sink.rb +42 -0
  219. data/spec/lib/redcap_parser_spec.rb +129 -0
  220. data/spec/lib/unparser_spec.rb +126 -0
  221. data/spec/models/answer_spec.rb +144 -0
  222. data/spec/models/dependency_condition_spec.rb +428 -0
  223. data/spec/models/dependency_spec.rb +90 -0
  224. data/spec/models/question_group_spec.rb +66 -0
  225. data/spec/models/question_spec.rb +176 -0
  226. data/spec/models/response_set_spec.rb +452 -0
  227. data/spec/models/response_spec.rb +208 -0
  228. data/spec/models/survey_section_spec.rb +58 -0
  229. data/spec/models/survey_spec.rb +155 -0
  230. data/spec/models/surveyor_gui/question_spec.rb +60 -0
  231. data/spec/models/surveyor_gui/question_type_spec.rb +268 -0
  232. data/spec/models/validation_condition_spec.rb +98 -0
  233. data/spec/models/validation_spec.rb +64 -0
  234. data/spec/spec.opts +4 -0
  235. data/spec/spec_helper.rb +119 -0
  236. data/spec/support/scenario_outline_helper.rb +39 -0
  237. data/spec/support/shared.rb +10 -0
  238. data/spec/support/surveyforms_creation_helpers.rb +312 -0
  239. data/spec/support/surveyforms_rearrangement_helpers.rb +170 -0
  240. data/spec/support/surveyor_api_helpers.rb +15 -0
  241. data/spec/support/surveyor_ui_helpers.rb +108 -0
  242. data/spec/support/wait_for_ajax.rb +11 -0
  243. data/spec/views/questions/edit.html.erb_spec.rb +73 -0
  244. data/spec/views/surveyforms/edit.html.erb_spec.rb +126 -0
  245. data/surveyor_gui.gemspec +52 -0
  246. data/test/dummy/README.rdoc +261 -0
  247. data/test/dummy/Rakefile +7 -0
  248. data/test/dummy/app/assets/javascripts/application.js +15 -0
  249. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  250. data/test/dummy/app/controllers/application_controller.rb +3 -0
  251. data/test/dummy/app/helpers/application_helper.rb +2 -0
  252. data/test/dummy/app/mailers/.gitkeep +0 -0
  253. data/test/dummy/app/models/.gitkeep +0 -0
  254. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  255. data/test/dummy/config.ru +4 -0
  256. data/test/dummy/config/application.rb +59 -0
  257. data/test/dummy/config/boot.rb +10 -0
  258. data/test/dummy/config/database.yml +25 -0
  259. data/test/dummy/config/environment.rb +5 -0
  260. data/test/dummy/config/environments/development.rb +37 -0
  261. data/test/dummy/config/environments/production.rb +67 -0
  262. data/test/dummy/config/environments/test.rb +37 -0
  263. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  264. data/test/dummy/config/initializers/inflections.rb +15 -0
  265. data/test/dummy/config/initializers/mime_types.rb +5 -0
  266. data/test/dummy/config/initializers/secret_token.rb +7 -0
  267. data/test/dummy/config/initializers/session_store.rb +8 -0
  268. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  269. data/test/dummy/config/locales/en.yml +5 -0
  270. data/test/dummy/config/routes.rb +58 -0
  271. data/test/dummy/lib/assets/.gitkeep +0 -0
  272. data/test/dummy/log/.gitkeep +0 -0
  273. data/test/dummy/public/404.html +26 -0
  274. data/test/dummy/public/422.html +26 -0
  275. data/test/dummy/public/500.html +25 -0
  276. data/test/dummy/public/favicon.ico +0 -0
  277. data/test/dummy/script/rails +6 -0
  278. data/test/surveyor-gui_test.rb +7 -0
  279. data/test/test_helper.rb +15 -0
  280. data/vendor/assets/stylesheets/.gitkeep +0 -0
  281. data/vendor/assets/stylesheets/custom.sass +5 -0
  282. data/vendor/plugins/.gitkeep +0 -0
  283. metadata +664 -0
@@ -0,0 +1,39 @@
1
+ class SurveyorGui::ResponsesController < ApplicationController
2
+ include ReportPreviewWrapper
3
+ # ReportPreviewWrapper wraps preview in a database transaction so test data is not permanently saved.
4
+ around_action :wrap_in_transaction, only: :preview
5
+ layout 'surveyor_gui_default'
6
+
7
+ def index
8
+ @title = "Survey Responses"
9
+ @response_sets = Survey.find_by_id(params[:id]).response_sets
10
+ end
11
+
12
+ def preview
13
+ user_id = defined?(current_user) && current_user ? current_user.id : nil
14
+ @title = "Show Response"
15
+ @survey = Survey.find(params[:survey_id])
16
+ @response_set = ResponseSet.create(:survey => @survey, :user_id => user_id, :test_data => true)
17
+ ReportResponseGenerator.new(@survey).generate_1_result_set(@response_set)
18
+ @responses = @response_set.responses
19
+ if (!@survey)
20
+ flash[:notice] = "Survey/Questionnnaire not found."
21
+ redirect_to :back
22
+ end
23
+ render :show
24
+ end
25
+
26
+ def show
27
+ @title = "Show Response"
28
+ @response_set = ResponseSet.find(params[:id])
29
+ @survey = @response_set.survey
30
+ @responses = @response_set.responses
31
+ if (!@response_set)
32
+ flash[:error] = "Response not found"
33
+ redirect_to :back
34
+ elsif (!@survey)
35
+ flash[:error] = "Survey/Questionnnaire not found."
36
+ redirect_to :back
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,16 @@
1
+ class SurveyorGui::SurveyController < ApplicationController
2
+ def show
3
+ @title = "User Will See this Survey"
4
+ @survey = Survey.find(params[:survey_id])
5
+ user_id = defined?(current_user) ? current_user.id : 1
6
+ ResponseSet.where('survey_id = ? and test_data = ? and user_id = ?',params[:survey_id],true, user_id).each {|r| r.destroy}
7
+ @response_set = ResponseSet.create(:survey => @survey, :user_id => user_id, :test_data => true)
8
+ if (@survey)
9
+ flash[:notice] = t('surveyor.survey_started_success')
10
+ redirect_to surveyor.edit_my_survey_path @survey.access_code, @response_set.access_code
11
+ else
12
+ flash[:notice] = "Survey not found."
13
+ redirect :back
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ class ReportFormatter
2
+
3
+ STAT_FUNCTIONS = {
4
+ sum: ->(arr){arr.sum},
5
+ min: ->(arr){arr.min},
6
+ max: ->(arr){arr.max},
7
+ average: ->(arr){arr.average}
8
+ }
9
+
10
+ STAT_FORMATS = {
11
+ number: "%g",
12
+ date: "%m-%d-%y",
13
+ time: "%I:%M:%S %P",
14
+ datetime: "%m-%d-%y %I:%M:%S %P"
15
+ }
16
+
17
+ def initialize(question, responses)
18
+ @question = question
19
+ @responses = responses
20
+ end
21
+
22
+ def stats(stat_function)
23
+ stat = calculate_stats(stat_function)
24
+ format_stats(stat)
25
+ end
26
+
27
+ def calculate_stats(stat_function)
28
+ arr = @responses.where(:question_id => @question.id).map{|r| r.response_value.to_f}
29
+ STAT_FUNCTIONS[stat_function].call(arr)
30
+ end
31
+
32
+ def format_stats(stat)
33
+ if @question.question_type_id == :number
34
+ STAT_FORMATS[@question.question_type_id] % stat
35
+ else
36
+ format_time_stat(stat)
37
+ end
38
+ end
39
+
40
+ def format_time_stat(stat)
41
+ stat = Time.zone.at(stat)
42
+ stat.strftime(STAT_FORMATS[@question.question_type_id])
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ module ReportPreviewWrapper
2
+ def wrap_in_transaction
3
+ ActiveRecord::Base.transaction do
4
+ begin
5
+ yield
6
+ ensure
7
+ raise ActiveRecord::Rollback
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,147 @@
1
+ class ReportResponseGenerator
2
+ RESPONSE_GENERATOR = {
3
+ pick_one: pick_one = ->(response, response_set, q, context){ context.send(:make_pick_one, response_set, q) },
4
+ pick_any: pick_any = ->(response, response_set, q, context){ context.send(:random_anys, response, response_set, q) },
5
+ dropdown: pick_one,
6
+ slider: pick_one,
7
+ number: ->(response, response_set, q, context){ response.integer_value = rand(100); response.save },
8
+ string: ->(response, response_set, q, context){ response.string_value = context.send(:random_string); response.save },
9
+ box: ->(response, response_set, q, context){ response.text_value = context.send(:random_string); response.save },
10
+ date: ->(response, response_set, q, context){ response.datetime_value = context.send(:random_date); response.save },
11
+ datetime: ->(response, response_set, q, context){ response.datetime_value = context.send(:random_date); response.save },
12
+ time: ->(response, response_set, q, context){ response.datetime_value = context.send(:random_date); response.save },
13
+ file: ->(response, response_set, q, context){ context.send(:make_blob, response, false) },
14
+ stars: ->(response, response_set, q, context){ response_set.responses.create(:question_id => q.id, :integer_value => rand(5)+1, :answer_id => q.answers.first.id)},
15
+ grid_one: pick_one,
16
+ grid_any: pick_any,
17
+ grid_dropdown: ->(response, response_set, q, context){ context.send(:grid_dropdown, response, response_set, q) }
18
+ }
19
+
20
+ def initialize(survey)
21
+ @survey = survey
22
+ end
23
+
24
+ def generate_1_result_set(response_set)
25
+ @survey.survey_sections.each do |ss|
26
+ ss.questions.each do |q|
27
+ response = response_set.responses.build(:question_id => q.id, :answer_id => q.answers.first ? q.answers.first.id : nil)
28
+ if q.repeater?
29
+ rand(5).times.each do
30
+ response = response_set.responses.build(:question_id => q.id, :answer_id => q.answers.first.id)
31
+ RESPONSE_GENERATOR[q.question_type_id].call(response, response_set, q, self)
32
+ end
33
+ elsif q.question_type_id != :label
34
+ RESPONSE_GENERATOR[q.question_type_id].call(response, response_set, q, self)
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def random_string
41
+ whichone = rand(5)
42
+ case whichone
43
+ when 0
44
+ 'An answer.'
45
+ when 1
46
+ 'A different answer.'
47
+ when 2
48
+ 'Any answer here.'
49
+ when 3
50
+ 'Some response.'
51
+ when 4
52
+ 'A random response.'
53
+ when 5
54
+ 'A random answer.'
55
+ end
56
+ end
57
+
58
+ def random_date
59
+ Time.now + (rand(100)-50).days
60
+ end
61
+
62
+ def random_pick(question, avoid=[])
63
+ answer = nil
64
+ answers = question.answers.is_not_comment
65
+ while !answer && avoid.count < answers.count
66
+ pick = rand(answers.count)
67
+ if !avoid.include?(answers[pick].id)
68
+ answer=answers[pick].id
69
+ end
70
+ end
71
+ return answer
72
+ end
73
+
74
+ def make_pick_comment(response_set, q)
75
+ comment = q.answers.is_comment
76
+ if comment && !comment.empty?
77
+ response_set.responses.create(question_id: q.id, answer_id: comment.first.id, string_value: "User added a comment here.")
78
+ end
79
+ end
80
+
81
+ def make_pick_one(response_set, q)
82
+ if q.is_comment?
83
+ response_set.responses.create(question_id: q.id, answer_id: q.answers.first.id, string_value: "User added a comment here.")
84
+ else
85
+ response_set.responses.create(question_id: q.id, answer_id: random_pick(q))
86
+ make_pick_comment(response_set, q)
87
+ end
88
+ end
89
+
90
+ def random_pick_count(question)
91
+ answers = question.answers
92
+ return rand(answers.count)+1
93
+ end
94
+
95
+ def make_blob(response, show_blob)
96
+ response.save!
97
+ response.blob.store!(File.new(Rails.public_path+'/images/regulations.jpg')) if show_blob
98
+ end
99
+
100
+ def random_any(response, response_set, q)
101
+ if !q.answers.empty?
102
+ how_many = random_pick_count(q)
103
+ how_many.times {
104
+ already_checked = response_set.responses.where('question_id=?',q.id).collect(&:answer_id)
105
+ response = response_set.responses.build(:question_id => q.id, :answer_id => random_pick(q,already_checked))
106
+ response.save
107
+ }
108
+ else
109
+ response = nil
110
+ end
111
+ end
112
+
113
+ def random_anys(response, response_set, q)
114
+ if q.is_comment?
115
+ response_set.responses.create(question_id: q.id, answer_id: q.answers.first.id, string_value: "User added a comment here.")
116
+ else
117
+ random_any(response, response_set, q)
118
+ make_pick_comment(response_set, q)
119
+ end
120
+ end
121
+
122
+ def grid_one(response, response_set, q)
123
+ q.question_group.questions.each do |question|
124
+ response = response_set.responses.build(question_id: question.id, answer_id: random_pick(question))
125
+ response.save
126
+ end
127
+ end
128
+
129
+ def random_pick_with_column(question, column)
130
+ answers = Answer.where(question_id: question.id, column_id: column.id)
131
+ pick = rand(answers.count)
132
+ answer=answers[pick].id
133
+ end
134
+
135
+ def grid_dropdown(response, response_set, q)
136
+ q.question_group.questions.is_not_comment.each do |question|
137
+ q.question_group.columns.each do |column|
138
+ response = response_set.responses.build(question_id: q.id, answer_id: random_pick_with_column(question, column), column_id: column.id )
139
+ response.save
140
+ end
141
+ end
142
+ if question = q.question_group.questions.is_comment.first && question && !question.empty?
143
+ response_set.responses.create(question_id: question.id, answer_id: question.answers.first.id, column_id: column.id, string_value: "User added comment here")
144
+ end
145
+ end
146
+
147
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,5 @@
1
+ module DependencyHelper
2
+ def link_to_remove_tbody (name, f)
3
+ f.hidden_field(:_destroy) + link_to(image_tag("delete.png",:border => 0, :margin=>'-1em'), "#", onclick: "remove_dependency_condition(this);")
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ module QuestionHelper
2
+ def link_to_remove_fields (name, f, dom_to_hide=nil)
3
+ f.hidden_field(:_destroy) + link_to(image_tag("delete.png",:border => 0, :margin=>'-1em'), "#", onclick: "remove_fields(this, \"#{dom_to_hide}\");")
4
+ end
5
+
6
+ def adjusted_text
7
+ if @question.part_of_group?
8
+ @question.question_group.text
9
+ else
10
+ @question.text
11
+ end
12
+ end
13
+
14
+ def question_type_subset(args)
15
+ question_types = QuestionType.all.map{|t|[t.text, t.id]}[0..19].uniq
16
+ ordered_types = []
17
+ args.each do |id|
18
+ ordered_types << question_types.select{|t| id == t[1]}.flatten
19
+ end
20
+ ordered_types
21
+ end
22
+ end
@@ -0,0 +1,179 @@
1
+ module SurveyformHelper
2
+ require 'deep_cloneable'
3
+
4
+ def list_dependencies(o)
5
+ controlling_questions = o.controlling_questions
6
+
7
+ controlling_question_ids = controlling_questions.map{|q| q.question_number.to_s+')'}.uniq
8
+ count = controlling_question_ids.count
9
+ retstr ='This question is shown depending on the '
10
+ retstr += 'answer'.pluralize(count)
11
+ retstr += ' to '
12
+ retstr += 'question'.pluralize(count) + ' '
13
+ retstr + list_phrase(controlling_question_ids)
14
+ end
15
+
16
+ def list_phrase(args)
17
+ ## given a list of word parameters, return a syntactically correct phrase
18
+ ## [dog] = "dog"
19
+ ## [dog, cat] = "dog and cat"
20
+ ## [dog, cat, bird] = "dog, cat and bird"
21
+ case args.count
22
+ when 0
23
+ ''
24
+ when 1
25
+ args[0]
26
+ when 2
27
+ args[0] + ' and ' + args[1]
28
+ else
29
+ last = args.count
30
+ args.take(last - 2).join(', ') + ', ' + args[last - 2] + ' and ' + args[last - 1]
31
+ end
32
+ end
33
+
34
+ def render_questions_and_groups_helper(q, ss)
35
+ #this method will render either a question or a complete question group.
36
+ #we always iterate through questions, and if we happen to notice a question
37
+ #belongs to a group, we process the group at that time.
38
+ #note that questions carry a question_group_id, and this is how we know
39
+ #that a question is part of a group, and that it should not be rendered individually,
40
+ #but as part of a group.
41
+ if q.object.part_of_group?
42
+ _render_initial_group(q, ss) || _respond_to_a_change_in_group_id(q, ss)
43
+ else
44
+ render "question_wrapper", f: q
45
+ end
46
+ end
47
+
48
+ def render_one_group(qg)
49
+ qg.simple_fields_for :questions, @current_group.questions do |f|
50
+ if f.object.is_comment != true
51
+ render "question_group_fields", f: f
52
+ elsif f.object.is_comment == true
53
+ "</div>".html_safe+(render "question_field", f: f)+"<div>".html_safe
54
+ end
55
+ end
56
+ end
57
+
58
+ def question_group_heading(f)
59
+ if f.object.question_type_id == :grid_dropdown
60
+ heading = f.object.question_group.columns
61
+ elsif f.object.question_group.display_type == "grid"
62
+ heading = f.object.answers
63
+ else
64
+ heading = []
65
+ end
66
+ heading.map {|a| "<span class=\"question_group_heading #{f.object.question_type_id.to_s}\" >#{a.text}<\/span>"}.join().html_safe
67
+ end
68
+
69
+ def row_label_if_question_group(question)
70
+ if question.part_of_group?
71
+ "<span class=\"row_name\">#{question.text}: </span>".html_safe
72
+ end
73
+ end
74
+
75
+ def question_group_class(question)
76
+ if @current_group.question_group.display_type == "inline"
77
+ "inline"
78
+ elsif @current_group.question_group.display_type == "default"
79
+ "default"
80
+ else
81
+ if question.question_type_id == :grid_dropdown
82
+ "dropdown"
83
+ else
84
+ "grid"
85
+ end
86
+ end
87
+ end
88
+
89
+ private
90
+ def _render_initial_group(q, ss)
91
+ if @current_group.nil?
92
+ @current_group = QuestionGroupTracker.new(q.object.question_group_id)
93
+ render "question_group", :ss => ss, :f => q
94
+ end
95
+ end
96
+
97
+ def _respond_to_a_change_in_group_id(q, ss)
98
+ if @current_group.question_group_id != q.object.question_group_id
99
+ @current_group = QuestionGroupTracker.new(q.object.question_group_id)
100
+ render "question_group", :ss => ss, :f => q
101
+ end
102
+ end
103
+
104
+ end
105
+
106
+ class SurveyCloneFactory
107
+ def initialize(id, as_template=false)
108
+ @survey = Surveyform.find(id.to_i)
109
+ @as_template = as_template
110
+ end
111
+
112
+ def clone
113
+ cloned_survey = _deep_clone
114
+ _set_api_keys(cloned_survey)
115
+ if cloned_survey.save!
116
+ return cloned_survey
117
+ else
118
+ raise cloned_survey.errors.messages.map{|m| m}.join(',')
119
+ return nil
120
+ end
121
+ end
122
+
123
+ private
124
+
125
+ def _initial_clone
126
+ initial_clone = @survey
127
+ initial_clone.api_id = Surveyor::Common.generate_api_id
128
+ initial_clone.survey_version = Survey.where(access_code: @survey.access_code).maximum(:survey_version) + 1
129
+ return initial_clone
130
+ end
131
+
132
+ def _deep_clone
133
+ _initial_clone.deep_clone include: {
134
+ sections:
135
+ {
136
+ questions: [
137
+ :answers,
138
+ {dependency: :dependency_conditions},
139
+ {question_group: :columns}
140
+ ]
141
+ }
142
+ },
143
+ use_dictionary: true
144
+ end
145
+
146
+ def _set_api_keys(cloned_survey)
147
+ cloned_survey.sections.each do |section|
148
+ section.questions.each do |question|
149
+ question.api_id = Surveyor::Common.generate_api_id
150
+ question.question_group.api_id = Surveyor::Common.generate_api_id if question.part_of_group?
151
+ question.answers.each do |answer|
152
+ answer.api_id = Surveyor::Common.generate_api_id
153
+ end
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ class QuestionGroupTracker
160
+ attr_reader :questions, :question_group_id, :question_group
161
+ def initialize(question_group_id)
162
+ @questions = Question.where('question_group_id=?',question_group_id)
163
+ @counter = 0
164
+ @question_group_id = question_group_id
165
+ @question_group = QuestionGroup.find(question_group_id)
166
+ end
167
+
168
+ def check_for_new_group(question)
169
+ if question.question_group_id != @question_group_id || !defined?(@initial_check)
170
+ initialize(question.question_group_id)
171
+ @initial_check = true
172
+ return true
173
+ else
174
+ return false
175
+ end
176
+ end
177
+ end
178
+
179
+