Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. @non_organizers = @activity.participants.where(is_organizer: [false, nil])
  42. @organizers = @activity.organizers
  43. @non_organizers_options = @non_organizers.map{|p| [p.person.full_name, p.id] }
  44. @organizers_options = @organizers.map{|p| [p.person.full_name, p.id] }
  45. @non_organizers_options.sort!
  46. @organizers_options.sort!
  47. end
  48. # POST /activities
  49. # POST /activities.json
  50. def create
  51. @activity = Activity.new(activity_params)
  52. @activity.group = @group
  53. respond_to do |format|
  54. if @activity.save
  55. format.html {
  56. redirect_to group_activity_url(@group, @activity)
  57. flash_message(:info, I18n.t('activities.created'))
  58. }
  59. format.json { render :show, status: :created, location: @activity }
  60. else
  61. format.html { render :new }
  62. format.json { render json: @activity.errors, status: :unprocessable_entity }
  63. end
  64. end
  65. end
  66. # Change organizer state for a Participant
  67. def change_organizer
  68. @activity = Activity.find(params[:activity_id])
  69. @participant = @activity.participants.find(params[:participant_id])
  70. @participant.is_organizer = params[:new_state]
  71. @participant.save
  72. if params[:new_state] == "true"
  73. message = I18n.t('activities.organizers.added', name: @participant.person.full_name)
  74. else
  75. message = I18n.t('activities.organizers.removed', name: @participant.person.full_name)
  76. end
  77. flash_message(:success, message)
  78. redirect_to edit_group_activity_path(@group, @activity)
  79. end
  80. # PATCH/PUT /activities/1
  81. # PATCH/PUT /activities/1.json
  82. def update
  83. respond_to do |format|
  84. if @activity.update(activity_params)
  85. format.html {
  86. redirect_to group_activity_url(@group, @activity)
  87. flash_message(:info, I18n.t('activities.updated'))
  88. }
  89. format.json { render :show, status: :ok, location: @activity }
  90. else
  91. format.html { render :edit }
  92. format.json { render json: @activity.errors, status: :unprocessable_entity }
  93. end
  94. end
  95. end
  96. # DELETE /activities/1
  97. # DELETE /activities/1.json
  98. def destroy
  99. @activity.destroy
  100. respond_to do |format|
  101. format.html {
  102. redirect_to group_activities_url(@group)
  103. flash_message(:info, 'Activity was successfully destroyed.')
  104. }
  105. format.json { head :no_content }
  106. end
  107. end
  108. # PATCH/PUT /groups/:group_id/activities/:id/presence
  109. # PATCH/PUT /groups/:group_id/activities/:id/presence.json
  110. def presence
  111. participant = Participant.find_by(
  112. person_id: params[:person_id],
  113. activity: @activity
  114. )
  115. if params[:person_id].to_i != current_person.id && !@activity.may_change?(current_person)
  116. head :forbidden
  117. return
  118. end
  119. if @activity.deadline && @activity.deadline < Time.now && !@activity.may_change?(current_person)
  120. head :locked
  121. return
  122. end
  123. if params[:participant]
  124. params[:notes] = params[:participant][:notes]
  125. end
  126. participant.update_attributes(params.permit(:notes, :attending))
  127. head :no_content
  128. end
  129. def mass_new
  130. end
  131. def mass_create
  132. require 'csv'
  133. uploaded_io = params[:spreadsheet]
  134. result = Activity.from_csv(uploaded_io.read, @group)
  135. result.each do |a|
  136. a.save!
  137. end
  138. flash_message(:success, I18n.t('activities.mass_imported', count: result.count))
  139. redirect_to group_activities_path(@group)
  140. end
  141. private
  142. # The Activity's group takes precedence over whatever's in the URL, set_group not required (and can be mislead)
  143. def set_activity_and_group
  144. @activity = Activity.find(params[:id] || params[:activity_id])
  145. @group = @activity.group
  146. end
  147. def set_group
  148. @group = Group.find(params[:group_id])
  149. end
  150. # Never trust parameters from the scary internet, only allow the white list through.
  151. def activity_params
  152. params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline)
  153. end
  154. end