1 # Copyright (C) 2007, 2008, 2009, 2010, 2016 Heiko Bernloehr (FreeIT.de).
3 # This file is part of ECS.
5 # ECS is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of
8 # the License, or (at your option) any later version.
10 # ECS is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
15 # You should have received a copy of the GNU Affero General Public
16 # License along with ECS. If not, see <http://www.gnu.org/licenses/>.
19 class Admin::ParticipantsController < ApplicationController
25 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
26 verify :method => [ :post, :put, :delete ], :only => [ :destroy, :create, :update, :destroy_participant, :reset ],
27 :add_flash => { :notice => "Failed to execute last action" },
28 :redirect_to => :admin_participants_path
31 redirect_to admin_participants_path
36 render :action => 'list'
40 @list_participants_anonymous_count = Participant.all.count - Participant.without_anonymous.count
41 @participants = case params[:anonymous]
44 @list_participants_count = Participant.all.count
45 Participant.find(:all).uniq
48 @list_participants_count = Participant.all.count - @list_participants_anonymous_count
49 Participant.without_anonymous.uniq
52 @list_participants_count = Participant.all.count - @list_participants_anonymous_count
53 Participant.without_anonymous.uniq
55 @action_buttons = case params[:actionbuttons]
66 @participant = Participant.find(params[:id])
70 @participant = Participant.new
71 @organizations = Organization.find(:all, :order => :id)
72 @participant.identities.build
76 @participant = Participant.new(params[:participant])
77 @participant.ptype = Participant::TYPE[:main]
79 flash[:notice] = "Participant \"#{CGI.escapeHTML @participant.name}\" was successfully created."
80 redirect_to admin_participants_path
82 @organizations = Organization.find(:all, :order => :id)
83 render :action => 'new'
88 @participant = Participant.find(params[:id])
89 @organizations = Organization.find(:all, :order => :id)
93 @participant = Participant.find(params[:id])
94 @participant.destroy_receiver_messages
95 @participant.destroy_sender_messages
96 @participant.destroy_events
97 flash[:notice] = "Successfully cleared all sent and received messages and events of participant \"#{CGI.escapeHTML @participant.name}\"."
98 redirect_to_admin_participants_path
102 params[:participant][:community_ids] ||= []
103 @organizations = Organization.find(:all, :order => :id)
104 @participant = Participant.find(params[:id])
105 lmsgs= leaved_messages(@participant, params[:participant][:community_ids])
106 if @participant.update_attributes(params[:participant])
107 generate_destroyed_events_by_leaving_a_community(@participant,lmsgs) unless lmsgs.blank?
108 flash[:notice] = 'Participant was successfully updated.'
109 redirect_to admin_participant_path(:id => @participant)
111 render :action => 'edit'
116 p = Participant.find(params[:id])
118 flash[:notice] = "Participant \"#{CGI.escapeHTML p.name}\" was successfully destroyed."
119 redirect_to_admin_participants_path
122 def index_communities
123 @participant = Participant.find(params[:id])
124 @communities=Participant.find(params[:id]).memberships.collect {|i| i.community }.uniq.sort{|x,y| x.id <=> y.id }
127 # lists all those communities which the participant has not yet joined
128 def index_noncommunities
130 @communities=(Community.find(:all) - @communities).sort{|x,y| x.id <=> y.id }
133 def destroy_community
134 destroy_membership(params[:c_id], params[:id])
135 redirect_to admin_participant_communities_path(params[:id])
139 # join to a community
141 create_membership(params[:c_id], params[:id])
142 redirect_to admin_participant_communities_path(params[:id])
147 def redirect_to_admin_participants_path
149 if params[:anonymous]
150 queryparams[:anonymous]=true
152 if params[:actionbuttons]
153 queryparams[:actionbuttons]=true
155 redirect_to admin_participants_path(queryparams)
158 # Generate destroyed events for all messages unconnected in respect to the
159 # leaving communities.
160 def generate_destroyed_events_by_leaving_a_community(participant, messages )
161 messages.each do |msg|
162 Event.make(:event_type_name => EvType.find_by_name("destroyed").name, :participant => participant, :message => msg)
163 logger.info "destroyed event for message.id=#{msg.id}, participant:#{participant.name} (pid:#{participant.id})"
167 def leaved_messages(participant, community_ids)
168 leaved_community_ids= participant.communities.map{|c| c.id} - community_ids.map{|p| p.to_i}
170 leaved_community_ids.each do |cid|
171 leaved_messages << Membership.find_by_participant_id_and_community_id(participant.id, cid).messages
172 leaved_messages << Community.find(cid).messages
174 leaved_messages.flatten.compact.uniq