Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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
  18. end
  19. # GET /api/activities/1/response_summary
  20. def response_summary
  21. as = @activity
  22. .participants
  23. .joins(:person)
  24. .order('people.first_name ASC')
  25. present = as
  26. .where(attending: true)
  27. unknown = as
  28. .where(attending: nil)
  29. absent = as
  30. .where(attending: false)
  31. presentnames = present
  32. .map{|p| p.person.first_name }
  33. unknownnames = unknown
  34. .map{|p| p.person.first_name }
  35. absentnames = absent
  36. .map{|p| p.person.first_name }
  37. if presentnames.count > 0
  38. present_mess = I18n.t('activities.participant.these_present', count: present.count, names: presentnames.join(', '))
  39. else
  40. present_mess = I18n.t('activities.participant.none_present')
  41. end
  42. if unknownnames.count > 0
  43. unknown_mess = I18n.t('activities.participant.these_unknown', count: unknown.count, names: unknownnames.join(', '))
  44. else
  45. unknown_mess = I18n.t('activities.participant.none_unknown')
  46. end
  47. if absentnames.count > 0
  48. absent_mess = I18n.t('activities.participant.these_absent', count: absent.count, names: absentnames.join(', '))
  49. else
  50. absent_mess = I18n.t('activities.participant.none_absent')
  51. end
  52. @summary = {
  53. present: {
  54. count: present.count,
  55. names: presentnames,
  56. message: present_mess
  57. },
  58. unknown: {
  59. count: unknown.count,
  60. names: unknownnames,
  61. message: unknown_mess
  62. },
  63. absent: {
  64. count: absent.count,
  65. names: absentnames,
  66. message: absent_mess
  67. }
  68. }
  69. end
  70. private
  71. # Set activity from the :id-parameter
  72. def set_activity
  73. @activity = Activity.find(params[:id])
  74. @group = @activity.group
  75. end
  76. # Set activity from the :id-parameter, and assert that it belongs to the set @group.
  77. def set_activity_with_group
  78. @activity = Activity.find_by id: params[:id]
  79. unless @activity
  80. head :not_found
  81. return
  82. end
  83. head :unauthorized unless @activity.group == @group
  84. end
  85. end