Sprankelprachtig aan/afmeldsysteem

activities_controller.rb 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. class ActivitiesController < ApplicationController
  2. include GroupsHelper
  3. include ActivitiesHelper
  4. has_activity_id = [
  5. :show, :edit, :edit_subgroups, :update, :update_subgroups, :destroy,
  6. :presence, :change_organizer, :create_subgroup, :update_subgroup,
  7. :destroy_subgroup, :immediate_subgroups, :clear_subgroups
  8. ]
  9. before_action :set_activity_and_group, only: has_activity_id
  10. before_action :set_group, except: has_activity_id
  11. before_action :set_subgroup, only: [:update_subgroup, :destroy_subgroup]
  12. before_action :require_membership!
  13. before_action :require_leader!, only: [
  14. :mass_new, :mass_create, :new, :create, :destroy
  15. ]
  16. before_action :require_organizer!, only: [
  17. :edit, :update, :change_organizer, :create_subgroup, :update_subgroup,
  18. :destroy_subgroup, :edit_subgroups, :update_subgroups, :immediate_subgroups,
  19. :clear_subgroups
  20. ]
  21. # GET /groups/:id/activities
  22. # GET /activities.json
  23. def index
  24. @activities = if params[:past]
  25. @group.activities
  26. .where('start < ?', Time.zone.now)
  27. .order(start: :desc)
  28. .paginate(page: params[:page], per_page: 25)
  29. else
  30. @group.activities
  31. .where('start > ?', Time.zone.now)
  32. .order(start: :asc)
  33. .paginate(page: params[:page], per_page: 25)
  34. end
  35. end
  36. # GET /activities/1
  37. # GET /activities/1.json
  38. def show
  39. @participants = @activity.participants
  40. .joins(:person)
  41. .order(attending: :desc)
  42. .order('people.first_name ASC')
  43. @organizers = @activity.participants
  44. .joins(:person)
  45. .where(is_organizer: true)
  46. .order('people.first_name ASC')
  47. .map { |p| p.person.full_name }
  48. .join(', ')
  49. @ownparticipant = @activity.participants
  50. .find_by(person: current_person)
  51. @counts = @activity.state_counts
  52. @num_participants = @counts.values.sum
  53. @assignable_subgroups = @activity.subgroups
  54. .where(is_assignable: true)
  55. .order(name: :asc)
  56. .pluck(:name)
  57. @subgroup_ids = @activity.subgroups
  58. .order(name: :asc)
  59. .pluck(:name, :id)
  60. @subgroup_ids.prepend([I18n.t('activities.subgroups.filter_nofilter'), 'all'])
  61. @subgroup_ids.append([I18n.t('activities.subgroups.filter_nogroup'), 'withoutgroup'])
  62. end
  63. # GET /activities/new
  64. def new
  65. @activity = Activity.new
  66. end
  67. # GET /activities/1/edit
  68. def edit
  69. set_edit_parameters!
  70. end
  71. # GET /activities/1/edit_subgroups
  72. def edit_subgroups
  73. @subgroups = @activity.subgroups.order(is_assignable: :desc, name: :asc)
  74. if @subgroups.none?
  75. flash_message(:error, I18n.t('activities.errors.cannot_subgroup_without_subgroups'))
  76. redirect_to group_activity_edit(@group, @activity)
  77. end
  78. @subgroup_options = @subgroups.map { |sg| [sg.name, sg.id] }
  79. @subgroup_options.prepend(['--', 'nil'])
  80. @participants = @activity.participants
  81. .joins(:person)
  82. .where.not(attending: false)
  83. .order(:subgroup_id)
  84. .order('people.first_name', 'people.last_name')
  85. end
  86. # POST /activities/1/update_subgroups
  87. def update_subgroups
  88. Participant.transaction do
  89. # For each key in participant_subgroups:
  90. params[:participant_subgroups].each do |k, v|
  91. # Get Participant, Subgroup
  92. p = Participant.find_by id: k
  93. sg = Subgroup.find_by id: v unless v == 'nil'
  94. # Verify that the Participant and Subgroup belong to this activity
  95. # Edit-capability is enforced by before_filter.
  96. if !p || p.activity != @activity || (!sg && v != 'nil') || (sg && sg.activity != @activity)
  97. flash_message(:danger, I18n.t(:somethingbroke))
  98. redirect_to group_activity_edit_subgroups_path(@group, @activity)
  99. raise ActiveRecord::Rollback
  100. end
  101. p.subgroup = (sg if v != 'nil')
  102. p.save
  103. end
  104. end
  105. flash_message(:success, I18n.t('activities.subgroups.edited'))
  106. redirect_to edit_group_activity_path(@group, @activity, anchor: 'subgroups')
  107. end
  108. # POST /activities/1/immediate_subgroups
  109. def immediate_subgroups
  110. @activity.clear_subgroups! if params[:overwrite]
  111. @activity.assign_subgroups!
  112. if params[:overwrite]
  113. flash_message(:success, I18n.t('activities.subgroups.redistributed'))
  114. else
  115. flash_message(:success, I18n.t('activities.subgroups.remaining_distributed'))
  116. end
  117. redirect_to edit_group_activity_path(@group, @activity)
  118. end
  119. # POST /activities/1/clear_subgroups
  120. def clear_subgroups
  121. @activity.clear_subgroups!
  122. flash_message(:success, I18n.t('activities.subgroups.cleared'))
  123. redirect_to edit_group_activity_path(@group, @activity)
  124. end
  125. # Shared lookups for rendering the edit-view
  126. def set_edit_parameters!
  127. @non_organizers = @activity.participants.where(is_organizer: [false, nil])
  128. @organizers = @activity.organizers
  129. @non_organizers_options = @non_organizers.map { |p| [p.person.full_name, p.id] }
  130. @organizers_options = @organizers.map { |p| [p.person.full_name, p.id] }
  131. @non_organizers_options.sort!
  132. @organizers_options.sort!
  133. @subgroup ||= Subgroup.new
  134. @subgroups = @activity.subgroups.order(is_assignable: :desc, name: :asc)
  135. end
  136. # POST /activities
  137. # POST /activities.json
  138. def create
  139. @activity = Activity.new(activity_params)
  140. @activity.group = @group
  141. respond_to do |format|
  142. if @activity.save
  143. format.html do
  144. redirect_to group_activity_url(@group, @activity)
  145. flash_message(:info, I18n.t('activities.created'))
  146. end
  147. format.json { render :show, status: :created, location: @activity }
  148. else
  149. format.html { render :new }
  150. format.json { render json: @activity.errors, status: :unprocessable_entity }
  151. end
  152. end
  153. end
  154. # Change organizer state for a Participant
  155. def change_organizer
  156. @activity = Activity.find(params[:activity_id])
  157. @participant = @activity.participants.find(params[:participant_id])
  158. @participant.is_organizer = params[:new_state]
  159. @participant.save
  160. message = if params[:new_state] == "true"
  161. I18n.t('activities.organizers.added', name: @participant.person.full_name)
  162. else
  163. I18n.t('activities.organizers.removed', name: @participant.person.full_name)
  164. end
  165. flash_message(:success, message)
  166. redirect_to edit_group_activity_path(@group, @activity, anchor: 'organizers-add')
  167. end
  168. # PATCH/PUT /activities/1
  169. # PATCH/PUT /activities/1.json
  170. def update
  171. respond_to do |format|
  172. if @activity.update(activity_params)
  173. redirect = group_activity_path(@group, @activity)
  174. if params.has_key?('commit-continue')
  175. redirect = edit_group_activity_path(@group, @activity)
  176. end
  177. format.html do
  178. redirect_to redirect
  179. flash_message(:info, I18n.t('activities.updated'))
  180. end
  181. format.json { render :show, status: :ok, location: @activity }
  182. else
  183. set_edit_parameters!
  184. format.html { render :edit }
  185. format.json { render json: @activity.errors, status: :unprocessable_entity }
  186. end
  187. end
  188. end
  189. # DELETE /activities/1
  190. # DELETE /activities/1.json
  191. def destroy
  192. @activity.destroy
  193. respond_to do |format|
  194. format.html do
  195. redirect_to group_activities_url(@group)
  196. flash_message(:info, 'Activity was successfully destroyed.')
  197. end
  198. format.json { head :no_content }
  199. end
  200. end
  201. # POST /activities/1/subgroups
  202. def create_subgroup
  203. @subgroup = Subgroup.new(subgroup_params)
  204. @subgroup.activity = @activity
  205. if @subgroup.save
  206. flash_message :success, I18n.t('activities.subgroups.created')
  207. redirect_to edit_group_activity_path(@group, @activity, anchor: 'subgroups-add')
  208. else
  209. flash_message :danger, I18n.t('activities.subgroups.create_failed')
  210. set_edit_parameters!
  211. render :edit
  212. end
  213. end
  214. # PATCH /activities/1/subgroups/:subgroup_id
  215. def update_subgroup
  216. if @subgroup.update(subgroup_params)
  217. flash_message :success, I18n.t('activities.subgroups.updated')
  218. redirect_to edit_group_activity_path(@group, @activity, anchor: 'subgroups')
  219. else
  220. flash_message :danger, I18n.t('activities.subgroups.update_failed')
  221. set_edit_parameters!
  222. render :edit
  223. end
  224. end
  225. # DELETE /activities/1/subgroups/:subgroup_id
  226. def destroy_subgroup
  227. @subgroup.destroy
  228. flash_message :success, I18n.t('activities.subgroups.destroyed')
  229. redirect_to edit_group_activity_path(@group, @activity, anchor: 'subgroups')
  230. end
  231. # PATCH/PUT /groups/:group_id/activities/:id/presence
  232. # PATCH/PUT /groups/:group_id/activities/:id/presence.json
  233. def presence
  234. participant = Participant.find_by(
  235. person_id: params[:person_id],
  236. activity: @activity
  237. )
  238. if params[:person_id].to_i != current_person.id && !@activity.may_change?(current_person)
  239. head :forbidden
  240. return
  241. end
  242. if @activity.deadline&.past? && !@activity.may_change?(current_person)
  243. head :locked
  244. return
  245. end
  246. params[:notes] = params[:participant][:notes] if params[:participant]
  247. participant.update!(params.permit(:notes, :attending))
  248. head :no_content
  249. end
  250. def mass_new; end
  251. def mass_create
  252. require 'csv'
  253. uploaded_io = params[:spreadsheet]
  254. result = Activity.from_csv(uploaded_io.read, @group)
  255. result.each(&:save!)
  256. flash_message(:success, I18n.t('activities.mass_imported', count: result.count))
  257. redirect_to group_activities_path(@group)
  258. end
  259. private
  260. # The Activity's group takes precedence over whatever's in the URL, set_group not required (and can be mislead)
  261. def set_activity_and_group
  262. @activity = Activity.find(params[:id] || params[:activity_id])
  263. @group = @activity.group
  264. end
  265. def set_group
  266. @group = Group.find(params[:group_id])
  267. end
  268. def set_subgroup
  269. @subgroup = Subgroup.find(params[:subgroup_id])
  270. end
  271. # Never trust parameters from the scary internet, only allow the white list through.
  272. def activity_params
  273. params.require(:activity).permit(:name, :description, :location, :start, :end, :deadline, :reminder_at, :subgroup_division_enabled, :no_response_action)
  274. end
  275. def subgroup_params
  276. params.require(:subgroup).permit(:name, :is_assignable)
  277. end
  278. end