Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 4.7KB

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