Add more options for surveys

* Survey period constraints
* Add abilities
* Enforce required questions
* Update survey_submssion updated_at
* Do a full match of possible answer and user reply
This commit is contained in:
Stella Rouzi 2018-02-09 19:07:10 +02:00
parent c278e547ce
commit 683f62a27c
36 changed files with 219 additions and 74 deletions

View file

@ -93,6 +93,8 @@ linters:
- "app/views/admin/sponsors/index.html.haml"
- "app/views/admin/sponsorship_levels/_form.html.haml"
- "app/views/admin/sponsorship_levels/index.html.haml"
- "app/views/admin/surveys/index.html.haml"
- "app/views/admin/surveys/show.html.haml"
- "app/views/admin/targets/_form.html.haml"
- "app/views/admin/targets/index.html.haml"
- "app/views/admin/tickets/_form.html.haml"
@ -161,6 +163,9 @@ linters:
- "app/views/schedules/events.html.haml"
- "app/views/schedules/show.html.haml"
- "app/views/schedules/show.xml.haml"
- "app/views/surveys/*"
- "app/views/admin/surveys/*"
- "app/views/admin/survey_questions/*.html.haml"
- "app/views/shared/_changelog_actions.haml"
- "app/views/shared/_dynamic_association.html.haml"
- "app/views/shared/_media_item.html.haml"
@ -210,6 +215,8 @@ linters:
- "app/views/admin/splashpages/_form.html.haml"
- "app/views/admin/sponsors/_form.html.haml"
- "app/views/admin/sponsorship_levels/_form.html.haml"
- "app/views/admin/survey_questions/*"
- "app/views/admin/surveys/*"
- "app/views/admin/tickets/_form.html.haml"
- "app/views/admin/tracks/_form.html.haml"
- "app/views/admin/users/_form.html.haml"
@ -447,3 +454,5 @@ linters:
- "app/views/conferences/_gallery.html.haml"
- "app/views/layouts/_navigation.html.haml"
- "app/views/schedules/events.html.haml"
- "app/views/admin/survey_questions/*"
- "app/views/admin/surveys/*"

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Admin
class SurveyQuestionsController < Admin::BaseController
load_and_authorize_resource :conference, find_by: :short_title

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module Admin
class SurveysController < Admin::BaseController
load_and_authorize_resource :conference, find_by: :short_title
@ -17,7 +19,7 @@ module Admin
if @survey.save
redirect_to new_admin_conference_survey_survey_question_path(@conference.short_title, @survey), notice: 'Successfully created survey'
else
redirect_to new_admin_conference_survey_survey_question_path(@conference.short_title, @survey), error: 'Could not create survey.' + @survey.errors.full_messates.to_sentence
redirect_to new_admin_conference_survey_survey_question_path(@conference.short_title, @survey), error: 'Could not create survey.' + @survey.errors.full_messages.to_sentence
end
end

View file

@ -1,6 +1,10 @@
# frozen_string_literal: true
class SurveysController < ApplicationController
load_resource :conference, find_by: :short_title
load_and_authorize_resource
load_and_authorize_resource except: :reply
load_resource only: :reply
skip_authorization_check only: :reply
def index
@surveys = @conference.surveys.select(&:active?)
@ -11,6 +15,11 @@ class SurveysController < ApplicationController
end
def reply
unless can? :reply, @survey
redirect_to conference_survey_path(@conference, @survey), alert: 'This survey is currently closed'
return
end
survey_submission = params[:survey_submission]
@survey.survey_questions.each do |survey_question|
@ -22,7 +31,13 @@ class SurveysController < ApplicationController
else
survey_question.survey_replies.create!(text: reply_text, user: current_user)
end
@survey.survey_submissions.create!(user: current_user) unless @survey.survey_submissions.find_by(user: current_user)
user_survey_submission = @survey.survey_submissions.find_by(user: current_user)
if user_survey_submission
user_survey_submission.update_attributes(updated_at: Time.current)
else
@survey.survey_submissions.create!(user: current_user)
end
end
redirect_to :back

View file

@ -55,6 +55,8 @@ class Ability
can [:show, :events], Schedule do |schedule|
schedule.program.schedule_public
end
can [:index, :show], Survey, surveyable_type: 'Conference'
end
end
@ -104,8 +106,18 @@ class Ability
# can manage the commercials of their own events
can :manage, Commercial, commercialable_type: 'Event', commercialable_id: user.events.pluck(:id)
# can view and reply a survey
can [:show, :reply], Survey, surveyable_type: 'Conference', surveyable_id: user.registrations.pluck(:conference_id)
# can view and reply to a survey
can [:index, :show, :reply], Survey, surveyable_type: 'Conference'
can [:index, :show, :reply], Survey, surveyable_type: 'Registration', surveyable_id: user.registrations.pluck(:conference_id)
# TODO: this needs to check for more, eg.
# if survey target is after_conference, check whether or not the conference is over
# if not, do not allow replies.
# do not allow replies before the start_date or after the end_date of survey
cannot :reply, Survey do |survey|
survey.start_date > Time.current || survey.end_date < Time.current
end
can [:destroy], Openid

View file

@ -1235,4 +1235,3 @@ class Conference < ApplicationRecord
result
end
end
# rubocop:enable Metrics/ClassLength

View file

@ -268,6 +268,18 @@ class Event < ApplicationRecord
event_schedules.find_by(schedule_id: selected_schedule_id).try(:start_time)
end
##
# Returns true or false, if the event is already over or not
#
# ====Returns
# * +true+ -> If the event is over
# * +false+ -> If the event is not over yet
def ended?
event_schedule = event_schedules.find_by(schedule_id: selected_schedule_id)
return false unless event_schedule
event_schedule.end_time < Time.current
end
def conference
program.conference
end

View file

@ -1,12 +1,15 @@
# frozen_string_literal: true
class Survey < ActiveRecord::Base
belongs_to :surveyable, polymorphic: true
has_many :survey_questions
has_many :survey_submissions
has_many :survey_questions, dependent: :destroy
has_many :survey_submissions, dependent: :destroy
enum target: [:after_conference, :during_registration]
validates :title, presence: true
def active?
return false unless start_date && end_date
now = Time.now.in_time_zone(surveyable.timezone)
now >= start_date && now <= end_date
end

View file

@ -1,6 +1,8 @@
# frozen_string_literal: true
class SurveyQuestion < ActiveRecord::Base
belongs_to :survey
has_many :survey_replies
has_many :survey_replies, dependent: :destroy
# Order of this list should not be changed without proper action!
enum kind: [:boolean, :choice, :string, :text, :datetime, :numeric]
@ -23,15 +25,15 @@ class SurveyQuestion < ActiveRecord::Base
end
def possible_answers=(value)
self[:possible_answers] = choice? ? value : nil
self[:possible_answers] = value if choice?
end
def min_choices=(value)
self[:min_choices] = choice? ? value : nil
self[:min_choices] = value if choice?
end
def max_choices=(value)
self[:max_choices] = choice? ? value : nil
self[:max_choices] = value if choice?
end
private

View file

@ -1,7 +1,10 @@
# frozen_string_literal: true
class SurveyReply < ActiveRecord::Base
belongs_to :user
belongs_to :survey_question
serialize :text
validates :user_id, :survey_question_id, presence: true
validates :survey_question_id, uniqueness: { scope: :user_id }
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class SurveySubmission < ActiveRecord::Base
belongs_to :user
belongs_to :survey

View file

@ -61,3 +61,8 @@
= link_to "#{event.comments_count}",
admin_conference_program_event_path(conference_id, event),
anchor: 'comments-div'
%td.text-center
= link_to new_admin_conference_survey_path(@conference.short_title,
survey: { surveyable_type: 'Event', surveyable_id: event.id }),
class: 'btn btn-success' do
.fa.fa-plus

View file

@ -17,7 +17,7 @@
.col-md-12
= f.input :title
= f.input :mandatory
%div.survey-possible-answers{ class: @survey_question.choice? ? '' : 'hidden'}
.survey-possible-answers{ class: @survey_question.choice? ? '' : 'hidden' }
= f.input :possible_answers, hint: 'Comma separated', input_html: { rows: 3 }
.row
.col-md-6
@ -38,16 +38,16 @@
%label
Preview:
.panel.panel-default
.panel-body{id: 'survey_question_preview'}
%p{id: 'title', style: 'word-wrap: break-word'}
.panel-body{ id: 'survey_question_preview' }
%p{ id: 'title', style: 'word-wrap: break-word' }
= @survey_question.title.blank? ? 'What is your answer?' : @survey_question.title
%div.kinds.boolean{ class: @survey_question.boolean? ? '' : 'hidden'}
%input{type: 'radio', name: 'radio'} Yes
.kinds.boolean{ class: @survey_question.boolean? ? '' : 'hidden' }
%input{ type: 'radio', name: 'radio' } Yes
%br
%input{type: 'radio', name: 'radio'} No
%input{ type: 'radio', name: 'radio' } No
%div.kinds.choice{ id: '', class: @survey_question.choice? ? '' : 'hidden'}
.kinds.choice{ class: @survey_question.choice? ? '' : 'hidden' }
- if @survey_question.possible_answers.blank?
- if @survey_question.single_choice?
%input{ type: 'radio', name: 'radio' } Choice 1
@ -72,21 +72,21 @@
= option
%br
%div.kinds.string{ class: @survey_question.string? ? '' : 'hidden'}
.kinds.string{ class: @survey_question.string? ? '' : 'hidden' }
%input.form-control
%div.kinds.text{ class: @survey_question.text? ? '' : 'hidden'}
.kinds.text{ class: @survey_question.text? ? '' : 'hidden' }
%textarea.form-control{ rows: 4 }
%div.kinds.datetime{ class: @survey_question.datetime? ? '' : 'hidden'}
.form-group{class: 'datetimepicker'}
.kinds.datetime{ class: @survey_question.datetime? ? '' : 'hidden' }
.form-group{ class: 'datetimepicker' }
.input-group
.input-group-addon
%span.fa.fa-calendar
%input.form-control{readonly: 'readonly'}
%input.form-control{ readonly: 'readonly' }
.kinds.numeric{ class: @survey_question.numeric? ? '' : 'hidden' }
%div.kinds.numeric{ class: @survey_question.numeric? ? '' : 'hidden'}
%input.form-control{ type: 'number' }
= f.submit 'Save', class: 'btn btn-primary'

View file

@ -1,31 +1,11 @@
.panel.panel-default
.panel-heading
%span.badge
= question_index
= survey_question.title
- if survey_question.multiple_choice?
( Select
- if survey_question.min_choices != survey_question.max_choices
a minimum of
= survey_question.min_choices
- if survey_question.min_choices != survey_question.max_choices
and a maximum of
= survey_question.max_choices
choices )
- if survey_question.mandatory
%span.fa.fa-asterisk.text-danger
- if can? :edit, @survey
%p.pull-right
= link_to edit_admin_conference_survey_survey_question_path(@conference.short_title, @survey, survey_question) do
%span.fa.fa-edit
= link_to admin_conference_survey_survey_question_path(@conference.short_title, @survey, survey_question), method: :delete, data: { confirm: 'Are you sure you want to delete this question?' } do
%span.fa.fa-times
= render partial: 'admin/surveys/survey_question_title', locals: { survey_question: survey_question, question_index: question_index }
.panel-body
- if survey_question.boolean?
%input{ type: 'hidden', name: "survey_submission[#{survey_question.id}][]" }
%label
%input{ type: 'radio', name: "survey_submission[#{survey_question.id}][]", value: 'Yes', checked: survey_reply.text == 'Yes' }
%input{ type: 'radio', name: "survey_submission[#{survey_question.id}][]", value: 'Yes', checked: survey_reply.text == 'Yes',required: survey_question.mandatory }
Yes
%br
%label
@ -39,24 +19,24 @@
- possible_answers.each.with_index(1) do |answer, answer_index|
- if survey_question.single_choice?
%label
%input{ type: 'radio', name: "survey_submission[#{survey_question.id}][]", value: answer, checked: (survey_reply.text.include? answer if survey_reply.text) }
%input{ type: 'radio', name: "survey_submission[#{survey_question.id}][]", value: answer, checked: (survey_reply.text == answer if survey_reply.text), required: survey_question.mandatory }
= answer
%br
- else
%label
= check_box_tag "survey_submission[#{survey_question.id}][]", answer, (survey_reply.text.include? answer if survey_reply.text), id: dom_id(survey_reply)
= check_box_tag "survey_submission[#{survey_question.id}][]", answer, (survey_reply.text.include? answer if survey_reply.text), id: dom_id(survey_reply), required: survey_question.mandatory
= answer
%br
- elsif survey_question.string?
%input.form-control{ name: "survey_submission[#{survey_question.id}][]", value: survey_reply.text }
%input.form-control{ name: "survey_submission[#{survey_question.id}][]", value: survey_reply.text, required: survey_question.mandatory }
- elsif survey_question.text?
= text_area_tag "survey_submission[#{survey_question.id}][]", survey_reply.text, rows: 4, class: 'form-control'
= text_area_tag "survey_submission[#{survey_question.id}][]", survey_reply.text, rows: 4, class: 'form-control', required: survey_question.mandatory
- elsif survey_question.datetime?
.form-group{ class: 'datetimepicker' }
.input-group
.input-group-addon
%span.fa.fa-calendar
%input.form-control{ readonly: 'readonly', name: "survey_submission[#{survey_question.id}][]", value: survey_reply.text }
%input.form-control{ readonly: 'readonly', name: "survey_submission[#{survey_question.id}][]", value: survey_reply.text, required: survey_question.mandatory }
- elsif survey_question.numeric?
%input.form-control{ type: 'number', name: "survey_submission[#{survey_question.id}][]", value: survey_reply.text }
%input.form-control{ type: 'number', name: "survey_submission[#{survey_question.id}][]", value: survey_reply.text, required: survey_question.mandatory }

View file

@ -0,0 +1,22 @@
%span.badge
= question_index
= survey_question.title
- if survey_question.multiple_choice?
( Select
- if survey_question.min_choices != survey_question.max_choices
a minimum of
= survey_question.min_choices
- if survey_question.min_choices != survey_question.max_choices
and a maximum of
= survey_question.max_choices
choices )
- if survey_question.mandatory
%span.fa.fa-asterisk.text-danger
- if can? :edit, @survey
%p.pull-right
= "(#{survey_question.survey_replies.count} replies)"
= link_to edit_admin_conference_survey_survey_question_path(@conference.short_title, @survey, survey_question) do
%span.fa.fa-edit
= link_to admin_conference_survey_survey_question_path(@conference.short_title, @survey, survey_question), method: :delete, data: { confirm: 'Are you sure you want to delete this question?' } do
%span.fa.fa-times

View file

@ -0,0 +1,16 @@
- @survey.survey_questions.each.with_index(1) do |survey_question, question_index|
.panel.panel-default
.panel-heading{ type: 'button', 'data-toggle': 'collapse', 'data-target': "#question-#{survey_question.id}", 'aria-expanded': 'true', 'aria-controls': 'collapse' }
= render partial: 'survey_question_title', locals: { survey_question: survey_question, question_index: question_index }
.panel-body.collapse{ id: "question-#{survey_question.id}" }
- if survey_question.survey_replies.any?
%table.table
%thead
%th User Email
%th Reply
%tbody
- survey_question.survey_replies.reload.each do |reply|
%tr
%td= reply.user.email
%td= reply.text

View file

@ -0,0 +1,13 @@
= javascript_include_tag "//www.google.com/jsapi", "chartkick"
= javascript_include_tag 'chartkick'
= bar_chart @survey.survey_questions.map{ |q| [q.title, q.survey_replies.count] }
- @survey.survey_questions.each.with_index(1) do |survey_question, question_index|
.panel.panel-default
.panel-heading
= render partial: 'survey_question_title', locals: { survey_question: survey_question, question_index: question_index }
.panel-body
- question_replies = survey_question.survey_replies
- if question_replies.any?
= pie_chart question_replies.group(:text).count, library: { legend: 'bottom', plotOptions: { pie: { dataLabels: { enabled: false }, showInLegend: true } } }

View file

@ -14,6 +14,8 @@
%table.table.table-hover.datatable#surveys
%thead
%th Title
%th # of questions
%th # of submissions
%th When
%th Start Date
%th End Date
@ -23,6 +25,10 @@
%tr
%td
= link_to survey.title, admin_conference_survey_path(@conference.short_title, survey)
%td
= survey.survey_questions.length
%td
= survey.survey_submissions.length
%td
= survey.target
%td

View file

@ -11,10 +11,24 @@
= @survey.end_date
.row
.col-md-12
= semantic_form_for 'survey_submission', url: '#' do |f|
- @survey.survey_questions.each.with_index(1) do |survey_question, question_index|
.row
.col-md-12
- survey_reply = survey_question.survey_replies.new(survey_question_id: survey_question.id, user_id: current_user.id)
= render partial: 'survey_question', locals: { survey_question: survey_question, question_index: question_index, survey_reply: survey_reply }
= link_to 'Add Question', new_admin_conference_survey_survey_question_path(@conference.short_title, @survey), class: 'btn btn-success pull-right'
.tabbable
%ul.nav.nav-tabs
%li.active
= link_to 'Questions' , '#questions-content', 'data-toggle' => 'tab'
%li
= link_to 'Replies' , '#replies-content', 'data-toggle' => 'tab'
%li
= link_to 'Stats' , '#stats-content', 'data-toggle' => 'tab'
.tab-content
.tab-pane.active#questions-content
= semantic_form_for 'survey_submission', url: '#' do |f|
- @survey.survey_questions.each.with_index(1) do |survey_question, question_index|
.row
.col-md-12
- survey_reply = survey_question.survey_replies.new(survey_question_id: survey_question.id, user_id: current_user.id)
= render partial: 'survey_question', locals: { survey_question: survey_question, question_index: question_index, survey_reply: survey_reply }
= link_to 'Add Question', new_admin_conference_survey_survey_question_path(@conference.short_title, @survey), class: 'btn btn-success pull-right'
.tab-pane#replies-content
= render partial: 'survey_replies'
.tab-pane#stats-content
= render partial: 'survey_stats'

View file

@ -10,15 +10,19 @@
= @survey.start_date
to
= @survey.end_date
- unless @survey.active?
%label
%i.fa.fa-exclamation-circle
This survey does not accept replies at this monent.
.row
.col-md-12
= semantic_form_for @survey_submission, url: reply_conference_survey_path(@conference.short_title, @survey) do |f|
- @survey.survey_questions.each.with_index(1) do |survey_question, question_index|
.row
.col-md-10.col-md-offset-1
- survey_reply = survey_question.survey_replies.find_by(user: current_user) || SurveyReply.new(survey_question_id: survey_question.id, user_id: current_user.id)
- survey_reply = survey_question.survey_replies.find_by(user: current_user) || SurveyReply.new(survey_question_id: survey_question.id, user: current_user)
= render partial: 'admin/surveys/survey_question', locals: { survey_question: survey_question, question_index: question_index, survey_reply: survey_reply }
.row
.col-md-10.col-md-offset-1
= f.submit 'Submit', class: 'btn btn-primary pull-right'
= f.submit 'Submit', class: 'btn btn-primary pull-right', disabled: !(can? :reply, @survey)

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class CreateSurveys < ActiveRecord::Migration
def change
create_table :surveys do |t|

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class CreateSurveyQuestions < ActiveRecord::Migration
def change
create_table :survey_questions do |t|

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AddTargetToSurveys < ActiveRecord::Migration
def change
add_column :surveys, :target, :integer, default: 0

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class CreateSurveyReplies < ActiveRecord::Migration
def change
create_table :survey_replies do |t|

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class CreateSurveySubmissions < ActiveRecord::Migration
def change
create_table :survey_submissions do |t|

View file

@ -7,8 +7,8 @@ namespace :data do
task surveys: :environment do
conference = create(:full_conference, start_date: Date.current, end_date: Date.current + 1.day)
survey_after_conference_active = create(:survey, surveyable: conference, target: :after_conference, title: 'Survey about the conference', start_date: conference.start_date - 1.day, end_date: conference.end_date + 5.days, description: 'Survey about the conference. You can already see it.')
survey_after_conference_inactive = create(:survey, surveyable: conference, target: :after_conference, title: 'Survey about the conference', start_date: conference.start_date + 1.day, end_date: conference.end_date + 5.days, description: 'Survey abou the conference. Not available yet!')
survey_on_registration = create(:survey, surveyable: conference, target: :during_registration, title: 'Survey during registation', start_date: conference.registration_period.start_date, end_date: conference.registration_period.end_date, description: 'Survey during registration.')
# survey_after_conference_inactive = create(:survey, surveyable: conference, target: :after_conference, title: 'Survey about the conference', start_date: conference.start_date + 1.day, end_date: conference.end_date + 5.days, description: 'Survey abou the conference. Not available yet!')
# survey_on_registration = create(:survey, surveyable: conference, target: :during_registration, title: 'Survey during registation', start_date: conference.registration_period.start_date, end_date: conference.registration_period.end_date, description: 'Survey during registration.')
create(:boolean_non_mandatory, survey: survey_after_conference_active)
create(:boolean_mandatory, survey: survey_after_conference_active)

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'spec_helper'
describe SurveysController do

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryGirl.define do
factory :survey_question do
survey

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryGirl.define do
factory :survey_reply do
user

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryGirl.define do
factory :survey_submission do
user

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
FactoryGirl.define do
factory :survey do
title 'This is my survey'

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'spec_helper'
describe SurveyQuestion do
@ -7,7 +9,7 @@ describe SurveyQuestion do
let(:single_choice_question) { create(:choice_mandatory_1_reply) }
let(:multiple_choice_question) { create(:choice_mandatory_2_replies) }
let(:boolean_question) { create(:boolean_question, min_choices: 3)}
let(:boolean_question) { create(:boolean_question, min_choices: 3) }
describe 'association' do
it { is_expected.to belong_to(:survey) }
@ -88,7 +90,7 @@ describe SurveyQuestion do
end
end
fields = ['min_choices', 'max_choices', 'possible_answers']
fields = %w[min_choices max_choices possible_answers]
(SurveyQuestion.kinds.keys - ['choice']).each do |kind|
fields.each do |field|
it_behaves_like 'is nil', kind, field

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'spec_helper'
describe SurveyReply do

View file

@ -1,9 +1,11 @@
# frozen_string_literal: true
require 'spec_helper'
describe Survey do
subject { create(:survey) }
let(:survey_active) { create(:conference_survey, start_date: Date.current - 1.day, end_date: Date.current + 1.day) }
let(:survey_inactive) { create(:conference_survey, start_date: Date.current - 2.day, end_date: Date.current - 1.day) }
let(:survey_inactive) { create(:conference_survey, start_date: Date.current - 2.days, end_date: Date.current - 1.day) }
describe 'association' do
it { is_expected.to have_many(:survey_questions) }
@ -17,12 +19,8 @@ describe Survey do
describe '#active?' do
it { expect(survey_active.active?).to eq true }
it { expect(survey_inactive.active?).to eq false }
it 'returns false, if start_date is not set' do
expect(create(:survey, start_date: nil, end_date: Date.current + 1.day).active?).to eq false
end
it 'returns false, if end_date is not set' do
expect(create(:survey, start_date: Date.current + 1.day, end_date: nil).active?).to eq false
it 'returns true, if both start_date and end_date are not set' do
expect(create(:survey, start_date: nil, end_date: nil, surveyable: create(:conference)).active?).to eq true
end
end
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'spec_helper'
describe SurveySubmission do