Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Provides read-only access to Activities.
  2. class Api::ActivitiesController < ApiController
  3. has_no_activity = [:index]
  4. # Session-based authentication/authorization
  5. before_action :set_activity, except: has_no_activity
  6. before_action :require_membership!, except: has_no_activity
  7. before_action :api_require_admin!, only: has_no_activity
  8. skip_before_action :api_require_authentication!, :set_activity, :require_membership!, if: 'request.authorization'
  9. # Group API-key-based authentication/authorization
  10. before_action :api_auth_group_token, if: 'request.authorization'
  11. before_action :set_activity_with_group, if: 'request.authorization'
  12. # GET /api/activities
  13. def index
  14. @activities = Activity.all
  15. end
  16. # GET /api/activities/1
  17. def show; end
  18. # GET /api/activities/1/response_summary
  19. def response_summary
  20. as = @activity
  21. .participants
  22. .joins(:person)
  23. .order('people.first_name ASC')
  24. present = as.where(attending: true)
  25. unknown = as.where(attending: nil)
  26. absent = as.where(attending: false)
  27. presentnames = present.map { |p| p.person.first_name }
  28. unknownnames = unknown.map { |p| p.person.first_name }
  29. absentnames = absent.map { |p| p.person.first_name }
  30. if presentnames.positive?
  31. present_mess = I18n.t('activities.participant.these_present', count: present.count, names: presentnames.join(', '))
  32. else
  33. present_mess = I18n.t('activities.participant.none_present')
  34. end
  35. if unknownnames.positive?
  36. unknown_mess = I18n.t('activities.participant.these_unknown', count: unknown.count, names: unknownnames.join(', '))
  37. else
  38. unknown_mess = I18n.t('activities.participant.none_unknown')
  39. end
  40. if absentnames.positive?
  41. absent_mess = I18n.t('activities.participant.these_absent', count: absent.count, names: absentnames.join(', '))
  42. else
  43. absent_mess = I18n.t('activities.participant.none_absent')
  44. end
  45. @summary = {
  46. present: {
  47. count: present.count,
  48. names: presentnames,
  49. message: present_mess
  50. },
  51. unknown: {
  52. count: unknown.count,
  53. names: unknownnames,
  54. message: unknown_mess
  55. },
  56. absent: {
  57. count: absent.count,
  58. names: absentnames,
  59. message: absent_mess
  60. }
  61. }
  62. end
  63. def presence
  64. participant = Participant.find_by(
  65. person_id: params[:person_id],
  66. activity: @activity
  67. )
  68. participant.update_attributes(params.permit(:attending))
  69. head :no_content
  70. end
  71. private
  72. # Set activity from the :id-parameter
  73. def set_activity
  74. @activity = Activity.find(params[:id])
  75. @group = @activity.group
  76. end
  77. # Set activity from the :id-parameter, and assert that it belongs to the set @group.
  78. def set_activity_with_group
  79. @activity = Activity.find_by id: params[:id]
  80. unless @activity
  81. head :not_found
  82. return
  83. end
  84. head :unauthorized unless @activity.group == @group
  85. end
  86. end