Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. # Surveys Controller
  2.  
  3. class Api::V1::SurveysController < Api::V1::BaseController
  4. before_action :set_survey, only: [:show, :questions]
  5.  
  6. # Survey List
  7. def index
  8. @schedules = @account.schedules.includes(:survey, survey_version: :departments).joins(:survey)
  9. .where(surveys: { status: 1 }, completed_at: nil).where(ignore: false)
  10. .where('schedules.start_time <= :time AND schedules.buffer_time >= :time', time: Time.current)
  11. .order('schedules.start_time asc')
  12. end
  13.  
  14. # Surveys for schedule reminder
  15. def reminder
  16. @schedules = @account.schedules.includes(:survey, :survey_version).joins(:survey)
  17. .where(surveys: { status: 1 }, completed_at: nil).where(ignore: false).where.not(survey_versions: { notify_time: 0 })
  18. .where(schedules: { notify_time: Time.current.beginning_of_day..Time.current.end_of_day })
  19. .order('schedules.notify_time asc')
  20. end
  21.  
  22. # History of Survey for current Day
  23. def history
  24. @schedules = @account.schedules.includes(:survey, :survey_version).where(ignore: false)
  25. .where(completed_at: Time.current.beginning_of_day..Time.current.end_of_day)
  26. end
  27.  
  28. # Survey Show on History list
  29. def history_show
  30. schedule = Schedule.find(params[:id])
  31. @survey = schedule.survey
  32. if !schedule.completed_at.nil?
  33. @answers = schedule.answers.group_by(&:question_id)
  34. else
  35. render json: { code: 404, status: 'not_found', payload: {}, errors: ['Survey not found'] }
  36. end
  37. end
  38.  
  39. # Details of Surveys
  40. def survey_details
  41. @schedule = Schedule.includes(:survey_version).find(params[:id])
  42. @questions = (@schedule.survey_version.questions.order('created_at ASC') + Incident.default_questions) unless @schedule.survey_version.disable_default_questions
  43. @questions = @schedule.survey_version.questions.order('created_at ASC') if @schedule.survey_version.disable_default_questions
  44. @staffs = @account.organization.accounts
  45. @departments = @account.organization.departments
  46. end
  47.  
  48. def departments
  49. render json: { departments: @account.organization.departments.pluck(:id, :name) }
  50. end
  51.  
  52. private
  53.  
  54. def set_survey
  55. @survey = Survey.find(params[:id])
  56. end
  57. end
  58.  
  59. # Survey Model
  60.  
  61. class Survey < ActiveRecord::Base
  62. has_many :email_notifications, as: :item
  63. belongs_to :version, class_name: 'SurveyVersion', foreign_key: 'version_id'
  64. belongs_to :department
  65. belongs_to :account
  66. belongs_to :organization
  67.  
  68. has_many :survey_versions, dependent: :destroy
  69. has_many :completed_surveys, dependent: :destroy
  70. has_many :schedules, dependent: :destroy
  71.  
  72. enum status: %w(draft published suspended archived)
  73.  
  74. accepts_nested_attributes_for :survey_versions
  75.  
  76. def publish!
  77. update_attributes(status: 1, published_on: Time.current, suspended_on: nil, archived_on: nil)
  78. version.update_attribute(:unpublished_at, nil)
  79. version.schedule!
  80. end
  81.  
  82. def suspend!
  83. update_attributes(status: 2, suspended_on: Time.current)
  84. version.update_attribute(:unpublished_at, Time.current)
  85. version.remove_future_schedules!
  86. end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement