Browse Source

Add manual subgroup (re)division

Maarten van den Berg 7 years ago
parent
commit
2bfd77df46

+ 33 - 17
app/controllers/activities_controller.rb

@@ -5,7 +5,7 @@ class ActivitiesController < ApplicationController
5 5
   has_activity_id = [
6 6
     :show, :edit, :edit_subgroups, :update, :update_subgroups, :destroy,
7 7
     :presence, :change_organizer, :create_subgroup, :update_subgroup,
8
-    :destroy_subgroup
8
+    :destroy_subgroup, :immediate_subgroups, :clear_subgroups
9 9
   ]
10 10
   before_action :set_activity_and_group, only: has_activity_id
11 11
   before_action :set_group,            except: has_activity_id
@@ -17,7 +17,8 @@ class ActivitiesController < ApplicationController
17 17
   ]
18 18
   before_action :require_organizer!, only: [
19 19
     :edit, :update, :change_organizer, :create_subgroup, :update_subgroup,
20
-    :destroy_subgroup, :edit_subgroups, :update_subgroups
20
+    :destroy_subgroup, :edit_subgroups, :update_subgroups, :immediate_subgroups,
21
+    :clear_subgroups
21 22
   ]
22 23
 
23 24
   # GET /groups/:id/activities
@@ -88,26 +89,18 @@ class ActivitiesController < ApplicationController
88 89
 
89 90
   # POST /activities/1/update_subgroups
90 91
   def update_subgroups
91
-    # TODO:
92
-    # voor elke key in participant_subgroups
93
-    # pak participant, subgroup
94
-    # verifieer participant hoort bij activiteit
95
-    # verifieer subgroup hoort bij activiteit
96
-    # (impl dat editen mogen al gecheckt is, check of dit zo is!)
97
-    # doe veranderen
98
-    # knikker alles in een transactie want dan atomisch enzo cool en leuk
99
-    # S: on error netjes bleren maar wat er mis kan gaan is bijna alleen gekut dus meh
100
-    kappen = false
101
-
102 92
     Participant.transaction do
93
+      # For each key in participant_subgroups:
103 94
       params[:participant_subgroups].each do |k, v|
95
+        # Get Participant, Subgroup
104 96
         p = Participant.find_by id: k
105 97
         sg = Subgroup.find_by id: v unless v == 'nil'
106 98
 
99
+        # Verify that the Participant and Subgroup belong to this activity
100
+        # Edit-capability is enforced by before_filter.
107 101
         if !p || p.activity != @activity || (!sg && v != 'nil') || (sg && sg.activity != @activity)
108 102
           flash_message(:danger, I18n.t(:somethingbroke))
109 103
           redirect_to group_activity_edit_subgroups_path(@group, @activity)
110
-          kappen = true
111 104
           raise ActiveRecord::Rollback
112 105
         end
113 106
 
@@ -121,10 +114,33 @@ class ActivitiesController < ApplicationController
121 114
       end
122 115
     end
123 116
 
124
-    unless kappen
125
-      flash_message(:success, I18n.t('activities.subgroups.edited'))
126
-      redirect_to edit_group_activity_path(@group, @activity)
117
+    flash_message(:success, I18n.t('activities.subgroups.edited'))
118
+    redirect_to edit_group_activity_path(@group, @activity)
119
+  end
120
+
121
+  # POST /activities/1/immediate_subgroups
122
+  def immediate_subgroups
123
+    if params[:overwrite]
124
+      @activity.clear_subgroups!
127 125
     end
126
+
127
+    @activity.assign_subgroups!
128
+
129
+    if params[:overwrite]
130
+      flash_message(:success, I18n.t('activities.subgroups.redistributed'))
131
+    else
132
+      flash_message(:success, I18n.t('activities.subgroups.remaining_distributed'))
133
+    end
134
+
135
+    redirect_to edit_group_activity_path(@group, @activity)
136
+  end
137
+
138
+  # POST /activities/1/clear_subgroups
139
+  def clear_subgroups
140
+    @activity.clear_subgroups!
141
+
142
+    flash_message(:success, I18n.t('activities.subgroups.cleared'))
143
+    redirect_to edit_group_activity_path(@group, @activity)
128 144
   end
129 145
 
130 146
   # Shared lookups for rendering the edit-view

+ 19 - 0
app/models/activity.rb

@@ -250,6 +250,25 @@ class Activity < ApplicationRecord
250 250
     end
251 251
   end
252 252
 
253
+  def clear_subgroups!(only_assignable = true)
254
+    sgs = self
255
+      .subgroups
256
+
257
+    if only_assignable
258
+    sgs = sgs
259
+      .where(is_assignable: true)
260
+    end
261
+
262
+    ps = self
263
+      .participants
264
+      .where(subgroup: sgs)
265
+
266
+    ps.each do |p|
267
+      p.subgroup = nil
268
+      p.save
269
+    end
270
+  end
271
+
253 272
   # Notify participants of the current subgroups, if any.
254 273
   def notify_subgroups!
255 274
     ps = self

+ 23 - 1
app/views/activities/_form.html.erb

@@ -49,6 +49,28 @@
49 49
         <%= f.check_box(:subgroup_division_enabled) %>
50 50
         <%= t 'activerecord.attributes.activity.subgroup_division_enabled' %>
51 51
       </div>
52
-    <%= f.submit class: 'btn btn-primary' %>
52
+    <div class="form-group btn-group">
53
+      <%= f.submit class: 'btn btn-primary' %>
54
+      <% unless activity.new_record? %>
55
+        <%= link_to I18n.t('activities.subgroups.distribute_remaining'),
56
+          { action: 'immediate_subgroups', group_id: activity.group_id, activity_id: activity.id },
57
+          class: 'btn btn-warning',
58
+          method: :post,
59
+          data: { confirm: I18n.t('activities.subgroups.distribute_remaining_explanation')}
60
+        %>
61
+        <%= link_to I18n.t('activities.subgroups.redistribute'),
62
+          { action: 'immediate_subgroups', group_id: activity.group_id, activity_id: activity.id, overwrite: true },
63
+          method: :post,
64
+          class: 'btn btn-danger',
65
+          data: { confirm: I18n.t('activities.subgroups.redistribute_explanation')}
66
+        %>
67
+        <%= link_to I18n.t('activities.subgroups.clear'),
68
+          { action: 'clear_subgroups', group_id: activity.group_id, activity_id: activity.id, },
69
+          method: :post,
70
+          class: 'btn btn-danger',
71
+          data: { confirm: I18n.t('activities.subgroups.clear_explanation')}
72
+        %>
73
+      <% end %>
74
+    </div>
53 75
   </div>
54 76
 <% end %>

+ 12 - 0
config/locales/activities/en.yml

@@ -107,3 +107,15 @@ en:
107 107
       filter_nofilter: 'Show everyone'
108 108
 
109 109
       only_present_people: "Only people who will attend are listed, because people who don't cannot be in a subgroup."
110
+
111
+      clear: 'Clear subgroups'
112
+      clear_explanation: 'This removes everyone in a assignable subgroup from their subgroup! Are you sure you want this?'
113
+      cleared: 'Subgroups cleared.'
114
+
115
+      redistribute: 'Redistribute subgroups'
116
+      redistribute_explanation: 'This removes everyone in a assignable subgroup from that subgroup, and then randomly reassigns everyone to their subgroup. Are you sure you want this?'
117
+      redistributed: 'Subgroups redistributed.'
118
+
119
+      distribute_remaining: 'Distribute remaining to subgroups'
120
+      distribute_remaining_explanation: 'This distributes all remaining attending participants not yet in a subgroup to a subgroup. Do you want this?'
121
+      remaining_distributed: 'Remaining participants distributed.'

+ 12 - 0
config/locales/activities/nl.yml

@@ -114,3 +114,15 @@ nl:
114 114
       filter_nofilter: 'Toon iedereen'
115 115
 
116 116
       only_present_people: 'Je ziet alleen maar mensen die zijn aangemeld in dit overzicht, omdat mensen die zijn afgemeld niet in een subgroep kunnen zitten.'
117
+
118
+      clear: 'Subgroepen legen'
119
+      clear_explanation: 'Dit verwijdert iedereen in een indeelbare subgroep uit zijn subgroep! Weet je zeker dat je dit wilt?'
120
+      cleared: 'Subgroepen geleegd.'
121
+
122
+      redistribute: 'Subgroepen opnieuw indelen'
123
+      redistribute_explanation: 'Dit verwijdert iedereen in een indeelbare subgroep uit die subgroep, en deelt daarna iedereen willekeurig opnieuw in. Weet je zeker dat je dit wilt?'
124
+      redistributed: 'Subgroepen opnieuw ingedeeld.'
125
+
126
+      distribute_remaining: 'Overgebleven naar subgroepen'
127
+      distribute_remaining_explanation: 'Dit deelt iedereen die is aangemeld, maar nog niet in een subgroep zit, in in een willekeurige subgroep (volgens normale volgorde van kleinste groep eerst). Wil je dit?'
128
+      remaining_distributed: 'Overgebleven personen ingedeeld in subgroepen.'

+ 2 - 0
config/routes.rb

@@ -59,6 +59,8 @@ Rails.application.routes.draw do
59 59
 
60 60
       get 'edit_subgroups', to: 'activities#edit_subgroups'
61 61
       post 'update_subgroups', to: 'activities#update_subgroups'
62
+      post 'immediate_subgroups', to: 'activities#immediate_subgroups'
63
+      post 'clear_subgroups', to: 'activities#clear_subgroups'
62 64
     end
63 65
   end
64 66
   get 'my_groups', to: 'groups#user_groups', as: :user_groups