Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. class ActivitiesController < ApplicationController
  2. include GroupsHelper
  3. include ActivitiesHelper
  4. before_action :set_activity_and_group, only: [:show, :edit, :update, :destroy, :presence, :change_organizer]
  5. before_action :set_group, except: [:show, :edit, :update, :destroy, :presence, :change_organizer]
  6. before_action :require_membership!
  7. before_action :require_leader!, only: [:mass_new, :mass_create, :new, :create, :destroy]
  8. before_action :require_organizer!, only: [:edit, :update, :change_organizer]
  9. # GET /groups/:id/activities
  10. # GET /activities.json
  11. def index
  12. @activities = @group.activities
  13. .where('start > ?', Time.now)
  14. .order(start: :asc)
  15. .paginate(page: params[:page], per_page: 25)
  16. end
  17. # GET /activities/1
  18. # GET /activities/1.json
  19. def show
  20. @participants = @activity.participants
  21. .joins(:person)
  22. .order(attending: :desc)
  23. .order('people.first_name ASC')
  24. @organizers = @activity.participants
  25. .joins(:person)
  26. .where(is_organizer: true)
  27. .order('people.first_name ASC')
  28. .map{|p| p.person.full_name}
  29. .join(', ')
  30. @ownparticipant = @activity.participants
  31. .find_by(person: current_person)
  32. @counts = @activity.state_counts
  33. @num_participants = @counts.values.sum
  34. end
  35. # GET /activities/new
  36. def new
  37. @activity = Activity.new
  38. end
  39. # GET /activities/1/edit
  40. def edit
  41. set_edit_parameters!
  42. end
  43. # Shared lookups for rendering the edit-view
  44. def set_edit_parameters!
  45. @non_organizers = @activity.participants.where(is_organizer: [false, nil])
  46. @organizers = @activity.organizers
  47. @non_organizers_options = @non_organizers.map{|p| [p.person.full_name, p.id] }
  48. @organizers_options = @organizers.map{|p| [p.person.full_name, p.id] }
  49. @non_organizers_options.sort!
  50. @organizers_options.sort!
  51. end
  52. # POST /activities
  53. # POST /activities.json
  54. def create
  55. @activity = Activity.new(activity_params)
  56. @activity.group = @group
  57. respond_to do |format|
  58. if @activity.save
  59. format.html {
  60. redirect_to group_activity_url(@group, @activity)
  61. flash_message(:info, I18n.t('activities.created'))
  62. }
  63. format.json { render :show, status: :created, location: @activity }
  64. else
  65. format.html { render :new }
  66. format.json { render json: @activity.errors, status: :unprocessable_entity }
  67. end
  68. end
  69. end
  70. # Change organizer state for a Participant
  71. def change_organizer
  72. @activity = Activity.find(params[:activity_id])
  73. @participant = @activity.participants.find(params[:participant_id])
  74. @participant.is_organizer = params[:new_state]
  75. @participant.save
  76. if params[:new_state] == "true"
  77. message = I18n.t('activities.organizers.added', name: @participant.person.full_name)
  78. else
  79. message = I18n.t('activities.organizers.removed', name: @participant.person.full_name)
  80. end
  81. flash_message(:success, message)
  82. redirect_to edit_group_activity_path(@group, @activity)
  83. end
  84. # PATCH/PUT /activities/1
  85. # PATCH/PUT /activities/1.json
  86. def update
  87. respond_to do |format|
  88. if @activity.update(activity_params)
  89. format.html {
  90. redirect_to group_activity_url(@group, @activity)
  91. flash_message(:info, I18n.t('activities.updated'))
  92. }
  93. format.json { render :show, status: :ok, location: @activity }
  94. else
  95. set_edit_parameters!
  96. format.html { render :edit }
  97. format.json { render json: @activity.errors, status: :unprocessable_entity }
  98. end
  99. end
  100. end
  101. # DELETE /activities/1
  102. # DELETE /activities/1.json
  103. def destroy
  104. @activity.destroy
  105. respond_to do |format|
  106. format.html {
  107. redirect_to group_activities_url(@group)
  108. flash_message(:info, 'Activity was successfully destroyed.')
  109. }
  110. format.json { head :no_content }
  111. end
  112. end
  113. # PATCH/PUT /groups/:group_id/activities/:id/presence
  114. # PATCH/PUT /groups/:group_id/activities/:id/presence.json
  115. def presence
  116. participant = Participant.find_by(
  117. person_id: params[:person_id],
  118. activity: @activity
  119. )
  120. if params[:person_id].to_i != current_person.id && !@activity.may_change?(current_person)
  121. head :forbidden
  122. return
  123. end
  124. if @activity.deadline && @activity.deadline < Time.now && !@activity.may_change?(current_person)
  125. head :locked
  126. return
  127. end
  128. if params[:participant]
  129. params[:notes] = params[:participant][:notes]
  130. end
  131. participant.update_attributes(params.permit(:notes, :attending))
  132. head :no_content
  133. end
  134. def mass_new
  135. end
  136. def mass_create
  137. require 'csv'
  138. uploaded_io = params[:spreadsheet]
  139. result = Activity.from_csv(uploaded_io.read, @group)
  140. result.each do |a|
  141. a.save!
  142. end
  143. flash_message(:success, I18n.t('activities.mass_imported', count: result.count))
  144. redirect_to group_activities_path(@group)
  145. end
  146. private
  147. # The Activity's group takes precedence over whatever's in the URL, set_group not required (and can be mislead)
  148. def set_activity_and_group
  149. @activity = Activity.find(params[:id] || params[:activity_id])
  150. @group = @activity.group
  151. end
  152. def set_group
  153. @group = Group.find(params[:group_id])
  154. end
  155. # Never trust parameters from the scary internet, only allow the white list through.
  156. def activity_params
  157. params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline, :reminder_at)
  158. end
  159. end