UI changes + Redirection after creating report fixed
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :require_user
6   before_action :set_issues
7   before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore,:comment]
8   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
9   before_action :get_user_role, only: [:show, :index]
10
11   helper_method :sort_column, :sort_direction
12
13   def index
14     if @user.moderator?
15       @issue_types = @moderator_issues
16       @users = User.joins(:roles).where(user_roles: {role: 'moderator'})
17     else
18       @issue_types = @admin_issues
19       @users = User.joins(:roles).where(user_roles: {role: 'administrator'})
20     end
21
22     @issues = Issue.where(issue_type: @user_role).order(sort_column + " " + sort_direction)
23
24     # If search
25     if params[:search_by_user] and !params[:search_by_user].blank?
26       @find_user = User.find_by_display_name(params[:search_by_user])
27       if @find_user
28         @issues = @issues.where(reported_user_id: @find_user.id)
29       else 
30         notice = t('issues.index.search.user_not_found') 
31       end
32     end
33
34
35     if params[:status] and !params[:status][0].blank?
36       @issues = @issues.where(status: params[:status][0].to_i)
37     end
38
39     if params[:issue_type] and !params[:issue_type][0].blank?
40       @issues = @issues.where(reportable_type: params[:issue_type][0])
41     end
42
43     # If last_updated_by
44     if params[:last_updated_by] and !params[:last_updated_by][0].blank?
45       last_reported_by = params[:last_updated_by][0].to_s == "nil" ? nil : params[:last_updated_by][0].to_i
46       @issues = @issues.where(updated_by: last_updated_by)
47     end
48
49     if @issues.first == nil
50         notice = t('issues.index.search.issues_not_found')      
51     end
52
53     if params[:last_reported_by] and !params[:last_reported_by][0].blank?
54       last_reported_by = params[:last_reported_by][0].to_s == "nil" ? nil : params[:last_reported_by][0].to_i
55       @issues = @issues.where(updated_by: last_reported_by)
56     end
57
58     if notice
59       redirect_to issues_path, notice: notice
60     end 
61   end
62
63   def show
64     @read_reports = @issue.read_reports
65     @unread_reports = @issue.unread_reports
66     @comments = @issue.comments
67     @related_issues = @issue.user.issues.where(issue_type: @user_role)
68     if @issue.updated_by
69       @updated_by_admin = User.find(@issue.updated_by)
70     end
71   end
72
73   def new
74     unless create_new_issue_params.blank?
75       @issue = Issue.find_or_initialize_by(create_new_issue_params)
76       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
77       @report_strings_yaml = t(path)
78       flash[:referer] = params[:referer]
79     end
80   end
81
82   def create
83     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
84     # Check if Issue already exists
85     if !@issue 
86       @issue = Issue.find_or_initialize_by(issue_params)
87       @issue.updated_by = nil
88
89       # Reassign to moderators if it is a moderator issue
90       @issue.issue_type = "administrator"
91       if @moderator_issues.include? @issue.reportable.class.name
92         reassign_issue
93       end
94     end
95
96     # Check if details provided are sufficient
97     if check_report_params
98       @report = @issue.reports.build(report_params)
99       details =  get_report_details
100       @report.reporter_user_id = @user.id
101       @report.details = details
102       # Checking if instance has been updated since last report
103       @last_report = @issue.reports.order(updated_at: :desc).last
104       if check_if_updated
105         if @issue.reopen
106           @issue.save!
107         end
108       end
109
110       if @issue.save!
111         @issue.report_count = @issue.reports.count
112         @issue.save!
113         
114         @admins_or_mods = UserRole.where(role: @issue.issue_type)
115         @admins_or_mods.each do |user|
116           Notifier.new_issue_notification(@issue.id, User.find(user.user_id)).deliver_now
117         end
118
119         redirect_to flash[:referer], notice: t('issues.create.successful_report')
120       end
121     else
122       redirect_to new_issue_path(reportable_type: @issue.reportable_type,reportable_id: @issue.reportable_id, reported_user_id: @issue.reported_user_id), notice: t('issues.create.provide_details')
123     end
124   end
125
126   def update
127     @issue = Issue.find_by(issue_params)
128     # Check if details provided are sufficient
129     if check_report_params
130       @report = @issue.reports.where(reporter_user_id: @user.id).first
131       
132       if @report == nil
133         @report = @issue.reports.build(report_params)
134         @report.reporter_user_id = @user.id
135         notice = t('issues.update.new_report')
136       end
137
138       details =  get_report_details
139       @report.details = details    
140
141     # Checking if instance has been updated since last report
142       @last_report = @issue.reports.order(updated_at: :desc).last
143       if check_if_updated
144         @issue.reopen
145         @issue.save!
146       end
147
148       if notice == nil
149         notice = t('issues.update.successful_update')
150       end
151
152
153       if @report.save!
154         @issue.report_count = @issue.reports.count
155         @issue.save!
156         redirect_to flash[:referer], notice: notice
157       end
158     else
159       redirect_to new_issue_path(reportable_type: @issue.reportable_type,reportable_id: @issue.reportable_id, reported_user_id: @issue.reported_user_id), notice: t('issues.update.provide_details')
160     end  
161   end
162
163   def comment
164     @issue = Issue.find(params[:id])
165     if issue_comment_params.blank?
166       notice = t('issues.comment.provide_details')
167     else
168       @issue_comment = @issue.comments.build(issue_comment_params)
169       @issue_comment.commenter_user_id = @user.id
170       if params[:reassign]
171         reassign_issue
172         @issue_comment.reassign = true
173       end
174       @issue_comment.save!
175       @issue.updated_by = @user.id
176       @issue.save!
177       notice = t('issues.comment.comment_created')
178     end
179     redirect_to @issue, notice: notice
180   end
181
182   # Status Transistions
183   def resolve
184     if @issue.resolve
185       @issue.save!
186       redirect_to @issue, notice: t('issues.resolved')
187     else
188       render :show
189     end
190   end
191
192   def ignore
193     if @issue.ignore
194       @issue.updated_by = @user.id
195       @issue.save!
196       redirect_to @issue, notice: t('issues.ignored')
197     else
198       render :show
199     end
200   end
201
202   def reopen
203     if @issue.reopen
204       @issue.updated_by = @user.id      
205       @issue.save!
206       redirect_to @issue, notice: t('issues.reopened')
207     else
208       render :show
209     end
210   end
211
212   # Reassign Issues between Administrators and Moderators
213   def reassign_issue
214     if @issue.issue_type == "moderator"
215       @issue.issue_type = "administrator"
216     else
217       @issue.issue_type = "moderator"
218     end
219     @issue.save!
220   end
221
222   private
223
224     def set_issues
225       @admin_issues = [ 'DiaryEntry', 'DiaryComment', 'User']
226       @moderator_issues = [ 'Changeset', 'Note' ]
227     end
228
229     def get_user_role
230       # Get user role
231       if @user.administrator?
232         @user_role = "administrator"
233       else
234         @user_role = "moderator"
235       end      
236     end
237     
238     def check_if_updated
239       if @issue.reportable and (@issue.ignored? or @issue.resolved?) and @issue.reportable.has_attribute?(:updated_by) and @issue.reportable.updated_at > @last_report.updated_at
240         return true
241       else
242         return false
243       end
244     end
245  
246     def get_report_details
247       details = params[:report][:details] + "--||--"
248       details = details + params[:report_type].to_s + "--||--"
249       return details
250     end
251
252     def check_report_params
253       if params[:report] and params[:report][:details] and params[:report_type]
254         return true
255       end
256       return false
257     end
258
259     def find_issue
260       @issue = Issue.find(params[:id])
261     end
262
263     def check_permission
264       unless @user.administrator? or @user.moderator?
265         flash[:error] = t('application.require_admin.not_an_admin')
266         redirect_to root_path
267       end
268     end
269
270     def create_new_issue_params
271       params.permit(:reportable_id, :reportable_type, :reported_user_id)
272     end
273
274     def issue_params
275       params[:issue].permit(:reportable_id, :reportable_type,:reported_user_id)
276     end
277
278     def report_params
279       params[:report].permit(:details)
280     end
281
282     def issue_comment_params
283       params.require(:issue_comment).permit(:body)
284     end
285
286     def sort_column
287       Issue.column_names.include?(params[:sort]) ? params[:sort] : "status"
288     end
289
290     def sort_direction
291       %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
292     end
293 end