surveyor_gui 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +44 -0
- data/.travis.yml +17 -0
- data/Gemfile +7 -0
- data/LICENSE +21 -0
- data/MIT-LICENSE +20 -0
- data/README.md +159 -0
- data/Rakefile +66 -0
- data/app/controllers/answers_controller.rb +3 -0
- data/app/controllers/application_controller.rb +3 -0
- data/app/controllers/dependencys_controller.rb +286 -0
- data/app/controllers/question_groups_controller.rb +84 -0
- data/app/controllers/questions_controller.rb +187 -0
- data/app/controllers/survey_sections_controller.rb +80 -0
- data/app/controllers/surveyforms_controller.rb +4 -0
- data/app/controllers/surveyor_controller.rb +89 -0
- data/app/controllers/surveyor_gui/reports_controller.rb +339 -0
- data/app/controllers/surveyor_gui/responses_controller.rb +39 -0
- data/app/controllers/surveyor_gui/survey_controller.rb +16 -0
- data/app/facades/report_formatter.rb +44 -0
- data/app/facades/report_preview_wrapper.rb +11 -0
- data/app/facades/report_response_generator.rb +147 -0
- data/app/helpers/application_helper.rb +2 -0
- data/app/helpers/dependency_helper.rb +5 -0
- data/app/helpers/question_helper.rb +22 -0
- data/app/helpers/surveyform_helper.rb +179 -0
- data/app/helpers/surveyor_gui/report_helper.rb +19 -0
- data/app/helpers/surveyor_helper.rb +4 -0
- data/app/inputs/currency_input.rb +5 -0
- data/app/inputs/date_picker_input.rb +7 -0
- data/app/inputs/datetime_picker_input.rb +5 -0
- data/app/inputs/percent_input.rb +5 -0
- data/app/inputs/time_picker_input.rb +5 -0
- data/app/mailers/.gitkeep +0 -0
- data/app/models/.gitkeep +0 -0
- data/app/models/answer.rb +4 -0
- data/app/models/column.rb +3 -0
- data/app/models/dependency.rb +4 -0
- data/app/models/dependency_condition.rb +4 -0
- data/app/models/permitted_params_decorator.rb +80 -0
- data/app/models/question.rb +4 -0
- data/app/models/question_group.rb +4 -0
- data/app/models/question_type.rb +7 -0
- data/app/models/response.rb +4 -0
- data/app/models/response_set.rb +4 -0
- data/app/models/row.rb +3 -0
- data/app/models/survey.rb +4 -0
- data/app/models/survey_section.rb +4 -0
- data/app/models/surveyform.rb +103 -0
- data/app/uploaders/blob_uploader.rb +48 -0
- data/app/views/dependencys/_column.html.erb +1 -0
- data/app/views/dependencys/_dependency.html.erb +22 -0
- data/app/views/dependencys/_dependency_condition_fields.html.erb +48 -0
- data/app/views/dependencys/_form.html.erb +20 -0
- data/app/views/dependencys/blank.html.erb +0 -0
- data/app/views/dependencys/edit.html.erb +1 -0
- data/app/views/dependencys/new.html.erb +1 -0
- data/app/views/layouts/application.html.erb +14 -0
- data/app/views/layouts/surveyor_gui_blank.html.erb +12 -0
- data/app/views/layouts/surveyor_gui_default.html.erb +16 -0
- data/app/views/layouts/surveyor_modified.html.erb +14 -0
- data/app/views/partials/_answer.html.haml +33 -0
- data/app/views/partials/_question.html.haml +33 -0
- data/app/views/partials/_question_group.html.haml +73 -0
- data/app/views/partials/_section.html.haml +13 -0
- data/app/views/question_groups/_form.html.erb +56 -0
- data/app/views/question_groups/_group_inline_field.html.erb +21 -0
- data/app/views/question_groups/_group_inline_fields.html.erb +25 -0
- data/app/views/question_groups/blank.html.erb +0 -0
- data/app/views/question_groups/edit.html.erb +1 -0
- data/app/views/question_groups/new.html.erb +1 -0
- data/app/views/questions/_answer_fields.html.erb +23 -0
- data/app/views/questions/_answer_options.html.erb +28 -0
- data/app/views/questions/_form.html.erb +65 -0
- data/app/views/questions/_grid_dropdown_columns.html.erb +10 -0
- data/app/views/questions/_grid_dropdown_fields.html.erb +42 -0
- data/app/views/questions/_grid_fields.html.erb +9 -0
- data/app/views/questions/_no_picks.html.erb +29 -0
- data/app/views/questions/_pick.html +21 -0
- data/app/views/questions/_picks.html.erb +12 -0
- data/app/views/questions/blank.html.erb +0 -0
- data/app/views/questions/edit.html.erb +1 -0
- data/app/views/questions/new.html.erb +1 -0
- data/app/views/survey_sections/_form.html.erb +13 -0
- data/app/views/survey_sections/blank.html.erb +0 -0
- data/app/views/survey_sections/edit.html.erb +1 -0
- data/app/views/survey_sections/new.html.erb +1 -0
- data/app/views/surveyforms/_form.html.erb +50 -0
- data/app/views/surveyforms/_question_field.html.erb +148 -0
- data/app/views/surveyforms/_question_group.html.erb +116 -0
- data/app/views/surveyforms/_question_group_fields.html.erb +3 -0
- data/app/views/surveyforms/_question_name_and_number.html.erb +6 -0
- data/app/views/surveyforms/_question_section.html.erb +15 -0
- data/app/views/surveyforms/_question_wrapper.html.erb +109 -0
- data/app/views/surveyforms/_survey_section_fields.html.erb +138 -0
- data/app/views/surveyforms/clone_survey.html.erb +13 -0
- data/app/views/surveyforms/edit.html.erb +5 -0
- data/app/views/surveyforms/index.html.erb +40 -0
- data/app/views/surveyforms/new.html.erb +1 -0
- data/app/views/surveyforms/show.html.erb +5 -0
- data/app/views/surveyor_gui/reports/_graphs.html.haml +21 -0
- data/app/views/surveyor_gui/reports/_grid.html.haml +42 -0
- data/app/views/surveyor_gui/reports/_grid_dropdown.html.haml +56 -0
- data/app/views/surveyor_gui/reports/_repeater.html.haml +28 -0
- data/app/views/surveyor_gui/reports/_show_report.html.haml +33 -0
- data/app/views/surveyor_gui/reports/_single_question.html.haml +70 -0
- data/app/views/surveyor_gui/reports/show.html.erb +14 -0
- data/app/views/surveyor_gui/responses/_grid.html.haml +20 -0
- data/app/views/surveyor_gui/responses/_grid_dropdown.html.haml +20 -0
- data/app/views/surveyor_gui/responses/_repeater.html.haml +22 -0
- data/app/views/surveyor_gui/responses/_survey_results.html.haml +40 -0
- data/app/views/surveyor_gui/responses/index.html.haml +24 -0
- data/app/views/surveyor_gui/responses/show.html.haml +42 -0
- data/app/views/surveyor_gui/shared/_grid_comments.html.haml +10 -0
- data/app/views/surveyor_gui/shared/_new_line.html.haml +2 -0
- data/app/views/surveyor_gui/shared/_pick_comments.html.haml +15 -0
- data/app/views/surveyor_gui/shared/_question_number.html.haml +9 -0
- data/app/views/surveyor_gui/shared/_report_data.html.haml +24 -0
- data/app/views/surveyor_gui/shared/_stars_report_data.html.haml +14 -0
- data/config.ru +4 -0
- data/config/environment.rb +0 -0
- data/config/routes.rb +71 -0
- data/db/migrate/20140307204049_add_template_to_surveys.rb +5 -0
- data/db/migrate/20140307235607_add_test_data_to_response_sets.rb +5 -0
- data/db/migrate/20140308171947_add_original_choice_to_answers.rb +5 -0
- data/db/migrate/20140308172118_add_blob_to_responses.rb +5 -0
- data/db/migrate/20140308172417_add_modifiable_to_survey_section.rb +5 -0
- data/db/migrate/20140308174532_add_modifiable_to_question.rb +5 -0
- data/db/migrate/20140308175305_add_dynamically_generate_to_questions.rb +5 -0
- data/db/migrate/20140311032923_add_dummy_blob_to_questions.rb +5 -0
- data/db/migrate/20140311160609_add_dynamic_source_to_questions.rb +5 -0
- data/db/migrate/20140311161714_add_report_code_to_questions.rb +5 -0
- data/db/migrate/20140530181134_add_is_comment_to_questions.rb +5 -0
- data/db/migrate/20140531012006_add_is_comment_to_answers.rb +5 -0
- data/db/migrate/20140531225529_create_rows.rb +9 -0
- data/db/migrate/20140601011151_create_columns.rb +11 -0
- data/db/migrate/20140602030330_add_column_id_to_answers.rb +5 -0
- data/db/migrate/20140603155606_add_column_id_to_responses.rb +5 -0
- data/db/migrate/20140606023527_add_column_id_to_dependency_conditions.rb +5 -0
- data/db/migrate/20140815165307_add_user_id_to_survey.rb +5 -0
- data/lib/assets/.gitkeep +0 -0
- data/lib/assets/images/addicon.png +0 -0
- data/lib/assets/images/datepicker.gif +0 -0
- data/lib/assets/images/delete.gif +0 -0
- data/lib/assets/images/delete.png +0 -0
- data/lib/assets/images/images/border.png +0 -0
- data/lib/assets/images/images/controls.png +0 -0
- data/lib/assets/images/images/loading.gif +0 -0
- data/lib/assets/images/images/loading_background.png +0 -0
- data/lib/assets/images/images/overlay.png +0 -0
- data/lib/assets/images/rails.png +0 -0
- data/lib/assets/images/star.gif +0 -0
- data/lib/assets/javascripts/.gitkeep +0 -0
- data/lib/assets/javascripts/surveyor_gui/jquery.MetaData.js +121 -0
- data/lib/assets/javascripts/surveyor_gui/jquery.blockUI.js +619 -0
- data/lib/assets/javascripts/surveyor_gui/jquery.rating.js +377 -0
- data/lib/assets/javascripts/surveyor_gui/jquery.validate.js +1188 -0
- data/lib/assets/javascripts/surveyor_gui/surveyor_add_ons.js +10 -0
- data/lib/assets/javascripts/surveyor_gui/surveyor_gui.js +1417 -0
- data/lib/assets/stylesheets/.gitkeep +0 -0
- data/lib/assets/stylesheets/surveyor_gui/jquery.rating.css +12 -0
- data/lib/assets/stylesheets/surveyor_gui/surveyor_add_ons.css +3 -0
- data/lib/assets/stylesheets/surveyor_gui/surveyor_gui.sass +650 -0
- data/lib/enumerable_extenders.rb +31 -0
- data/lib/generators/surveyor_gui/install_generator.rb +57 -0
- data/lib/generators/surveyor_gui/templates/app/assets/javascripts/surveyor_add_ons.js +2 -0
- data/lib/generators/surveyor_gui/templates/app/assets/javascripts/surveyor_gui_all.js +24 -0
- data/lib/generators/surveyor_gui/templates/app/assets/stylesheets/surveyor_add_ons.css.sass +1 -0
- data/lib/generators/surveyor_gui/templates/app/assets/stylesheets/surveyor_gui_all.css.sass +8 -0
- data/lib/generators/surveyor_gui/templates/app/helpers/surveyor_helper.rb +4 -0
- data/lib/generators/surveyor_gui/templates/app/models/response_set_user.rb +13 -0
- data/lib/generators/surveyor_gui/templates/app/views/layouts/surveyor_gui_default.html.erb +16 -0
- data/lib/generators/surveyor_gui/templates/config/initializers/simple_form.rb +140 -0
- data/lib/generators/surveyor_gui/templates/config/locales/en.yml +5 -0
- data/lib/surveyor_gui.rb +17 -0
- data/lib/surveyor_gui/engine.rb +30 -0
- data/lib/surveyor_gui/helpers/surveyor_helper_methods.rb +19 -0
- data/lib/surveyor_gui/models/answer_methods.rb +24 -0
- data/lib/surveyor_gui/models/column_methods.rb +43 -0
- data/lib/surveyor_gui/models/dependency_condition_methods.rb +53 -0
- data/lib/surveyor_gui/models/dependency_methods.rb +83 -0
- data/lib/surveyor_gui/models/question_and_group_shared_methods.rb +11 -0
- data/lib/surveyor_gui/models/question_group_methods.rb +55 -0
- data/lib/surveyor_gui/models/question_methods.rb +435 -0
- data/lib/surveyor_gui/models/question_type_methods.rb +493 -0
- data/lib/surveyor_gui/models/response_methods.rb +67 -0
- data/lib/surveyor_gui/models/response_set_methods.rb +54 -0
- data/lib/surveyor_gui/models/row_methods.rb +11 -0
- data/lib/surveyor_gui/models/survey_methods.rb +32 -0
- data/lib/surveyor_gui/models/survey_section_methods.rb +32 -0
- data/lib/surveyor_gui/surveyforms_controller_methods.rb +258 -0
- data/lib/surveyor_gui/version.rb +3 -0
- data/lib/tasks/.gitkeep +0 -0
- data/lib/tasks/surveyor_gui_tasks.rake +4 -0
- data/lib/templates/erb/scaffold/_form.html.erb +13 -0
- data/spec/controllers/surveyforms_controller_spec.rb +361 -0
- data/spec/controllers/surveyor_controller_spec.rb +303 -0
- data/spec/factories.rb +181 -0
- data/spec/features/create_survey_spec.rb +418 -0
- data/spec/features/dependencies_spec.rb +61 -0
- data/spec/features/rearrange_survey_spec.rb +118 -0
- data/spec/features/ui_spec.rb +469 -0
- data/spec/fixtures/REDCapDemoDatabase_DataDictionary.csv +127 -0
- data/spec/fixtures/chinese_survey.rb +14 -0
- data/spec/fixtures/everything.rb +215 -0
- data/spec/fixtures/favorites-ish.rb +22 -0
- data/spec/fixtures/favorites.rb +22 -0
- data/spec/fixtures/feelings.rb +38 -0
- data/spec/fixtures/lifestyle.rb +55 -0
- data/spec/fixtures/numbers.rb +21 -0
- data/spec/fixtures/redcap_new_headers.csv +1 -0
- data/spec/fixtures/redcap_siblings.csv +1 -0
- data/spec/fixtures/redcap_whitespace.csv +1 -0
- data/spec/helpers/formtastic_custom_input_spec.rb +15 -0
- data/spec/helpers/surveyor_helper_spec.rb +116 -0
- data/spec/lib/common_spec.rb +37 -0
- data/spec/lib/parser_spec.rb +393 -0
- data/spec/lib/rake_kitchen_sink.rb +42 -0
- data/spec/lib/redcap_parser_spec.rb +129 -0
- data/spec/lib/unparser_spec.rb +126 -0
- data/spec/models/answer_spec.rb +144 -0
- data/spec/models/dependency_condition_spec.rb +428 -0
- data/spec/models/dependency_spec.rb +90 -0
- data/spec/models/question_group_spec.rb +66 -0
- data/spec/models/question_spec.rb +176 -0
- data/spec/models/response_set_spec.rb +452 -0
- data/spec/models/response_spec.rb +208 -0
- data/spec/models/survey_section_spec.rb +58 -0
- data/spec/models/survey_spec.rb +155 -0
- data/spec/models/surveyor_gui/question_spec.rb +60 -0
- data/spec/models/surveyor_gui/question_type_spec.rb +268 -0
- data/spec/models/validation_condition_spec.rb +98 -0
- data/spec/models/validation_spec.rb +64 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +119 -0
- data/spec/support/scenario_outline_helper.rb +39 -0
- data/spec/support/shared.rb +10 -0
- data/spec/support/surveyforms_creation_helpers.rb +312 -0
- data/spec/support/surveyforms_rearrangement_helpers.rb +170 -0
- data/spec/support/surveyor_api_helpers.rb +15 -0
- data/spec/support/surveyor_ui_helpers.rb +108 -0
- data/spec/support/wait_for_ajax.rb +11 -0
- data/spec/views/questions/edit.html.erb_spec.rb +73 -0
- data/spec/views/surveyforms/edit.html.erb_spec.rb +126 -0
- data/surveyor_gui.gemspec +52 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/controllers/application_controller.rb +3 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.gitkeep +0 -0
- data/test/dummy/app/models/.gitkeep +0 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +59 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +58 -0
- data/test/dummy/lib/assets/.gitkeep +0 -0
- data/test/dummy/log/.gitkeep +0 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/surveyor-gui_test.rb +7 -0
- data/test/test_helper.rb +15 -0
- data/vendor/assets/stylesheets/.gitkeep +0 -0
- data/vendor/assets/stylesheets/custom.sass +5 -0
- data/vendor/plugins/.gitkeep +0 -0
- 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,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,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
|
+
|