diff --git a/app/models/answer_set.rb b/app/models/answer_set.rb index e61e7f3..42d4acd 100644 --- a/app/models/answer_set.rb +++ b/app/models/answer_set.rb @@ -3,7 +3,7 @@ class AnswerSet < ActiveRecord::Base belongs_to :question_set belongs_to :survey_item_answer - attr_accessible :content, :survey_item_answer_id, :is_checked + attr_accessible :content, :survey_item_answer_id, :is_checked, :position before_validation do diff --git a/app/models/question_set.rb b/app/models/question_set.rb index 9d4042b..3b637f6 100644 --- a/app/models/question_set.rb +++ b/app/models/question_set.rb @@ -6,8 +6,8 @@ class QuestionSet < ActiveRecord::Base has_many :answer_sets accepts_nested_attributes_for :answer_sets - - scope :dont_answered, includes(:answer_sets).where("answer_sets.is_checked == ?", false) + + end diff --git a/app/models/survey_item.rb b/app/models/survey_item.rb index 68d710f..d034ea2 100644 --- a/app/models/survey_item.rb +++ b/app/models/survey_item.rb @@ -4,7 +4,7 @@ class SurveyItem < ActiveRecord::Base attr_accessible :description, :display_type, :help, :item_type, :questions_type, :string_type, :title, :archived, :locked, :text_presentation, :max_checkboxes ITEM_TYPES = ["question", "note"] - QUESTIONS_TYPES = ["boolean", "string", "check_box", "radio", "little_string"] + QUESTIONS_TYPES = ["boolean", "string", "check_box", "radio", "order"] DISPLAY_TYPES = ["inline", "grid", "dropdown"] has_many :answers, :class_name => "SurveyItemAnswer", :dependent => :destroy diff --git a/app/models/survey_type.rb b/app/models/survey_type.rb index 3ffb835..2d0564a 100644 --- a/app/models/survey_type.rb +++ b/app/models/survey_type.rb @@ -17,7 +17,7 @@ class SurveyType < ActiveRecord::Base question_set = @survey_set.question_sets.build(:survey_item_id => item.id) - if item.questions_type == 3 + if item.questions_type == 3 || item.questions_type == 5 item.answers.each do |answer| diff --git a/app/views/admin/survey_types/show.html.haml b/app/views/admin/survey_types/show.html.haml index 8bf5768..f78f8d1 100644 --- a/app/views/admin/survey_types/show.html.haml +++ b/app/views/admin/survey_types/show.html.haml @@ -3,217 +3,235 @@ #container -@survey.items.each do |item| - %h3=item.title - - - -if item.questions_type == 1 - %table - %tr - %th oui - %th non - %th nr - %th non + nr - %tr - %td - =oui = item.question_sets.where(:boolean_content => true).count - %td - =non = item.question_sets.where(:boolean_content => false).count - %td - =nr = item.question_sets.count - non - oui - %td - =nuls = nr + non - %tr - %td - =per_oui = number_to_percentage (oui*100.00)/(oui+nuls), :precision => 1 - - %td - =per_non = number_to_percentage (non*100.00)/(oui+nuls), :precision => 1 - %td - =per_nr = number_to_percentage (nr*100.00)/(oui+nuls), :precision => 1 - %td - =number_to_percentage (nuls*100.00)/(oui+nuls), :precision => 1 - - - -r = rand 99999 - + %h3=item.title + + + -if item.questions_type == 1 + %table + %tr + %th oui + %th non + %th nr + %th non + nr + %tr + %td + =oui = item.question_sets.where(:boolean_content => true).count + %td + =non = item.question_sets.where(:boolean_content => false).count + %td + =nr = item.question_sets.count - non - oui + %td + =nuls = nr + non + %tr + %td + =per_oui = number_to_percentage (oui*100.00)/(oui+nuls), :precision => 1 + + %td + =per_non = number_to_percentage (non*100.00)/(oui+nuls), :precision => 1 + %td + =per_nr = number_to_percentage (nr*100.00)/(oui+nuls), :precision => 1 + %td + =number_to_percentage (nuls*100.00)/(oui+nuls), :precision => 1 + + + -r = rand 99999 + - -chart_data = [{:name => "oui", :y => oui, :per => per_oui},{:name => "non", :y => non, :per => per_non} ,{:name => "non répondu", :y => nr, :per => per_nr} ] + -chart_data = [{:name => "oui", :y => oui, :per => per_oui},{:name => "non", :y => non, :per => per_non} ,{:name => "non répondu", :y => nr, :per => per_nr} ] - %script - $(document).ready(function() { - charts.push(new Highcharts.Chart( - =raw 'getChartConfig("'+r.to_s+'", "'+item.title+'", '+chart_data.to_json+')' - )) - }); - + %script + $(document).ready(function() { + charts.push(new Highcharts.Chart( + =raw 'getChartConfig("'+r.to_s+'", "'+item.title+'", '+chart_data.to_json+')' + )) + }); + - - %div.chart{:id => r.to_s} - - -if item.questions_type == 2 - %table.large - -count = 0 - -item.question_sets.each do |question| - -if question.content? - %tr - %td=simple_format question.content - -else - -count = count+1 - %tr - %td - non répondu : - =count - ( - =per_nr = number_to_percentage (count*100.00)/(@survey.nbr_responses), :precision => 1 - ) - - -r = rand 99999 - -chart_data = "['répondu',#{(((@survey.nbr_responses-count)*100.00)/(@survey.nbr_responses)).round(1)}],['non répondu', #{((count*100.00)/(@survey.nbr_responses)).round(1)}]" + + %div.chart{:id => r.to_s} + + -if item.questions_type == 2 + %table.large + -count = 0 + -item.question_sets.each do |question| + -if question.content? + %tr + %td=simple_format question.content + -else + -count = count+1 + %tr + %td + non répondu : + =count + ( + =per_nr = number_to_percentage (count*100.00)/(@survey.nbr_responses), :precision => 1 + ) + + -r = rand 99999 + -chart_data = "['répondu',#{(((@survey.nbr_responses-count)*100.00)/(@survey.nbr_responses)).round(1)}],['non répondu', #{((count*100.00)/(@survey.nbr_responses)).round(1)}]" - -chart_data = [{:name => "répondu", :y => (@survey.nbr_responses-count), :per => number_to_percentage(((@survey.nbr_responses-count)*100.00)/(@survey.nbr_responses), :precision => 1)} ,{:name => "non répondu", :y => count, :per => per_nr} ] + -chart_data = [{:name => "répondu", :y => (@survey.nbr_responses-count), :per => number_to_percentage(((@survey.nbr_responses-count)*100.00)/(@survey.nbr_responses), :precision => 1)} ,{:name => "non répondu", :y => count, :per => per_nr} ] - %script - $(document).ready(function() { - charts.push(new Highcharts.Chart( - =raw 'getChartConfig("'+r.to_s+'", "'+item.title+'", '+chart_data.to_json+')' - )) - }); - + %script + $(document).ready(function() { + charts.push(new Highcharts.Chart( + =raw 'getChartConfig("'+r.to_s+'", "'+item.title+'", '+chart_data.to_json+')' + )) + }); + - - %div.chart{:id => r.to_s} - - -if item.questions_type == 3 - - -#chart_data = "['oui',#{oui}],['non',#{non}],['non répondu', #{nr}]" - -chart_data = [] - -categories = [] - - %table - -item.answers.each do |answer| - %tr - %td=answer.label_text - %td - - =tot = answer.answer_sets.where(:is_checked => true).count - %td=per = number_to_percentage (tot*100.00)/(@survey.nbr_responses), :precision => 1 - - -categories << answer.label_text - -chart_data << {:name => answer.label_text, :y => tot, :per => per.to_s} - - - -count = 0 - -item.question_sets.each do |qs| - - -if qs.answer_sets.where(:is_checked => false).count == item.answers.count - -count = count+1 - - %tr - %td non répondu - %td=count - %td=per = number_to_percentage (count*100.00)/(@survey.nbr_responses), :precision => 1 - - -categories << "non répondu" - -chart_data << {:name => "non répondu", :y => count, :per => per.to_s} - - -if item.answers.where(:field_type => [2,3]).count > 0 - -np = 0 - -item.answers.where(:field_type => [2,3]).each do |a| - %h4 - Réponses personnalisées pour " - =a.label_text - " - %table.large - -a.answer_sets.each do |a| - -if a.content? - %tr - %td{:colspan => 3}=a.content - -else - -np = np +1 - %tr - %td - Non précisé - %td=np - %td=number_to_percentage (np*100.00)/(a.answer_sets.where(:is_checked => true).count), :precision => 1 - - -r = rand 99999 + + %div.chart{:id => r.to_s} + + -if item.questions_type == 3 + + -#chart_data = "['oui',#{oui}],['non',#{non}],['non répondu', #{nr}]" + -chart_data = [] + -categories = [] + + %table + -item.answers.each do |answer| + %tr + %td=answer.label_text + %td + + =tot = answer.answer_sets.where(:is_checked => true).count + %td=per = number_to_percentage (tot*100.00)/(@survey.nbr_responses), :precision => 1 + + -categories << answer.label_text + -chart_data << {:name => answer.label_text, :y => tot, :per => per.to_s} + + + -count = 0 + -item.question_sets.each do |qs| + + -if qs.answer_sets.where(:is_checked => false).count == item.answers.count + -count = count+1 + + %tr + %td non répondu + %td=count + %td=per = number_to_percentage (count*100.00)/(@survey.nbr_responses), :precision => 1 + + -categories << "non répondu" + -chart_data << {:name => "non répondu", :y => count, :per => per.to_s} + + -if item.answers.where(:field_type => [2,3]).count > 0 + -np = 0 + -item.answers.where(:field_type => [2,3]).each do |a| + %h4 + Réponses personnalisées pour " + =a.label_text + " + %table.large + -a.answer_sets.each do |a| + -if a.content? + %tr + %td{:colspan => 3}=a.content + -else + -np = np +1 + %tr + %td + Non précisé + %td=np + %td=number_to_percentage (np*100.00)/(a.answer_sets.where(:is_checked => true).count), :precision => 1 + + -r = rand 99999 - - %script - $(document).ready(function() { + + %script + $(document).ready(function() { - - =raw 'chartbar("'+r.to_s+'", '+chart_data.to_json+', '+categories.to_s+', "'+item.title+'");' - + + =raw 'chartbar("'+r.to_s+'", '+chart_data.to_json+', '+categories.to_s+', "'+item.title+'");' + - }); - - .bar_chart{:id => r} - -if item.questions_type == 4 - - -#chart_data = "['oui',#{oui}],['non',#{non}],['non répondu', #{nr}]" - -chart_data = [] - -categories = [] - - %table - -item.answers.each do |answer| - %tr - %td=answer.label_text - %td - - =tot = item.question_sets.where(:content => answer.label_text).count - %td=per = number_to_percentage (tot*100.00)/(@survey.nbr_responses), :precision => 1 - - -categories << answer.label_text - - - -chart_data << {:name => answer.label_text, :y => tot, :per => per.to_s} - - - - %tr - %td non répondu - %td=count = item.question_sets.where(:content => nil).count - %td=per = number_to_percentage (count*100.00)/(@survey.nbr_responses), :precision => 1 - - -categories << "non répondu" - -chart_data << {:name => "non répondu", :y => count, :per => per.to_s} - - -if item.answers.where(:field_type => 2).count > 0 - -np = 0 - -item.answers.where(:field_type => 2).each do |a| - %h4 - Réponses personnalisées pour " - =a.label_text - " - %table.large - -a.answer_sets.where(:is_checked => true).each do |a| - -if a.content? - %tr - %td{:colspan => 3}=a.content - -else - -np = np +1 - %tr - %td - Non précisé - %td=np - %td=number_to_percentage (np*100.00)/(a.answer_sets.where(:is_checked => true).count), :precision => 1 - - -r = rand 99999 + }); + + .bar_chart{:id => r} + -if item.questions_type == 4 + + -#chart_data = "['oui',#{oui}],['non',#{non}],['non répondu', #{nr}]" + -chart_data = [] + -categories = [] + + %table + -item.answers.each do |answer| + %tr + %td=answer.label_text + %td + + =tot = item.question_sets.where(:content => answer.label_text).count + %td=per = number_to_percentage (tot*100.00)/(@survey.nbr_responses), :precision => 1 + + -categories << answer.label_text + + + -chart_data << {:name => answer.label_text, :y => tot, :per => per.to_s} + + + + %tr + %td non répondu + %td=count = item.question_sets.where(:content => nil).count + %td=per = number_to_percentage (count*100.00)/(@survey.nbr_responses), :precision => 1 + + -categories << "non répondu" + -chart_data << {:name => "non répondu", :y => count, :per => per.to_s} + + -if item.answers.where(:field_type => 2).count > 0 + -np = 0 + -item.answers.where(:field_type => 2).each do |a| + %h4 + Réponses personnalisées pour " + =a.label_text + " + %table.large + -a.answer_sets.where(:is_checked => true).each do |a| + -if a.content? + %tr + %td{:colspan => 3}=a.content + -else + -np = np +1 + %tr + %td + Non précisé + %td=np + %td=number_to_percentage (np*100.00)/(a.answer_sets.where(:is_checked => true).count), :precision => 1 + + -r = rand 99999 - - %script - $(document).ready(function() { + + %script + $(document).ready(function() { - -if item.answers.count > 4 - =raw 'chartbar("'+r.to_s+'", '+chart_data.to_json+', '+categories.to_s+', "'+item.title+'");' - -else - charts.push(new Highcharts.Chart( - =raw 'getChartConfig("'+r.to_s+'", "'+item.title+'", '+chart_data.to_json+')' - )); - - }); - - .bar_chart{:id => r} - .break - \ No newline at end of file + -if item.answers.count > 4 + =raw 'chartbar("'+r.to_s+'", '+chart_data.to_json+', '+categories.to_s+', "'+item.title+'");' + -else + charts.push(new Highcharts.Chart( + =raw 'getChartConfig("'+r.to_s+'", "'+item.title+'", '+chart_data.to_json+')' + )); + + }); + + .bar_chart{:id => r} + .break + + -if item.questions_type == 5 + + -#chart_data = "['oui',#{oui}],['non',#{non}],['non répondu', #{nr}]" + -chart_data = [] + -categories = [] + + + -item.question_sets.each do |question_set| + %table + -question_set.answer_sets.order("position ASC").each do |answer_set| + + %tr + + %td=answer_set.position + %td=answer_set.survey_item_answer.label_text + %td=answer_set.content + %br + \ No newline at end of file diff --git a/app/views/survey_sets/_answer_set.html.haml b/app/views/survey_sets/_answer_set.html.haml index 527bae7..1576c68 100644 --- a/app/views/survey_sets/_answer_set.html.haml +++ b/app/views/survey_sets/_answer_set.html.haml @@ -1,21 +1,33 @@ +-if @item.questions_type == 3 - -.answer_p + .answer_p - =f.check_box :is_checked, :class => "checkboxe_#{answer.survey_item.id}" - -checkboxe_class = "checkboxe_#{answer.survey_item.id}" - =f.label :is_checked, answer.label_text + =f.check_box :is_checked, :class => "checkboxe_#{answer.survey_item.id}" + -checkboxe_class = "checkboxe_#{answer.survey_item.id}" + =f.label :is_checked, answer.label_text - =f.text_field :content if answer.field_type == 2 - %br - =f.text_area :content, :class => "large" if answer.field_type == 3 + =f.text_field :content if answer.field_type == 2 + %br + =f.text_area :content, :class => "large" if answer.field_type == 3 +-if @item.questions_type == 5 + .answer_p + + + =f.text_field :position, :style => "width:1.5em;" + =f.label :position, answer.label_text + + =f.text_field :content if answer.field_type == 2 + %br + =f.text_area :content, :class => "large" if answer.field_type == 3 + + -if 1 == 2 %script diff --git a/app/views/survey_sets/_question_set.html.haml b/app/views/survey_sets/_question_set.html.haml index bf7dd64..d60f37e 100644 --- a/app/views/survey_sets/_question_set.html.haml +++ b/app/views/survey_sets/_question_set.html.haml @@ -1,5 +1,5 @@ - .question=item.title +-@item = item -if item.questions_type == 1 .answer_p @@ -26,6 +26,12 @@ .answer_p{:style =>( "display:inline;" if item.display_type == 1)} =f.radio_button :content, answer.label_text =f.label :content, answer.label_text, :value => answer.label_text + +-elsif item.questions_type == 5 + + = f.fields_for(:answer_sets) do |f| + =f.hidden_field :survey_item_answer_id + =render :partial => "survey_sets/answer_set", :locals => {:f => f, :answer => f.object.survey_item_answer} diff --git a/db/migrate/20130717000000_sondage_frapna.rb b/db/migrate/20130717000000_sondage_frapna.rb index 9192fb3..7f3a2d4 100644 --- a/db/migrate/20130717000000_sondage_frapna.rb +++ b/db/migrate/20130717000000_sondage_frapna.rb @@ -49,14 +49,14 @@ item.answers.build(:label_text => "L’usage agricole, irrigation des cultures", item.answers.build(:label_text => "L’usage touristique (neige artificielle, arrosage espaces verts, piscine, spa, aménagement d’étangs de loisir)", :field_type => 1) -item = @survey.items.build(:questions_type => 3, :title => "Parmi les solutions proposées pour diminuer les problèmes de quantité de la ressource en eau, quelle priorisation souhaiteriez-vous appliquer dans votre département ? (merci d’indiquer l’ordre de priorité par des chiffres, de 1 à 6, devant les carrés)", :item_type => 1) +item = @survey.items.build(:questions_type => 5, :title => "Parmi les solutions proposées pour diminuer les problèmes de quantité de la ressource en eau, quelle priorisation souhaiteriez-vous appliquer dans votre département ? (merci d’indiquer l’ordre de priorité par des chiffres, de 1 à 6, devant les carrés)", :item_type => 1) item.answers.build(:label_text => "Adaptation des cultures (plantes et méthodes) et des techniques d’irrigation", :field_type => 1) item.answers.build(:label_text => "Sensibilisation des ménages/collectivités et restrictions obligatoires plus sévères en cas de sécheresse, aide à la mise en place de dispositifs/bonnes pratiques pour limiter les prélèvements.", :field_type => 1) item.answers.build(:label_text => "Changement et adaptation des procédés et des productions industriels", :field_type => 1) item.answers.build(:label_text => "Adaptation de l’urbanisation en fonction de la disponibilité de la ressource en eau", :field_type => 1) item.answers.build(:label_text => "Adaptation de l’usage touristique de la ressource en eau en cas de sécheresse", :field_type => 1) -item.answers.build(:label_text => "Suspension ou interdiction de certains usages, précisez lesquels : ", :field_type => 3) -item.answers.build(:label_text => "Autres > Précisez ", :field_type => 3) +item.answers.build(:label_text => "Suspension ou interdiction de certains usages, précisez lesquels : ", :field_type => 2) +item.answers.build(:label_text => "Autres > Précisez ", :field_type => 2) @@ -66,11 +66,13 @@ item.answers.build(:label_text => "Autres > Précisez ", :field_type => 3) @survey.save + add_column :answer_sets, :position, :integer end def down + remove_column :answer_sets, :position @survey = SurveyType.find_by_slug("07-13-ressource-en-eau").destroy end end diff --git a/db/schema.rb b/db/schema.rb index cc72f84..927c92a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -42,6 +42,7 @@ ActiveRecord::Schema.define(:version => 20130717000000) do t.boolean "is_checked" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.integer "position" end add_index "answer_sets", ["question_set_id"], :name => "index_answer_sets_on_question_set_id"