Small changes
[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 :check_permission, only: [:index, :show, :resolve,:open,:ignore,:comment]
7   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
8
9   def index
10     # Get user role
11     if @user.administrator?
12       @user_role = "administrator"
13     else
14       @user_role = "moderator"
15     end
16
17     # If search
18     if params[:search_by_user]
19       @find_user = User.find_by_display_name(params[:search_by_user])
20       if @find_user
21         @issues = Issue.where(reported_user_id: @find_user.id, issue_type: @user_role).order(:status)
22       else 
23         @issues = Issue.where(issue_type: @user_role).order(:status)
24         notice = t('issues.index.search.user_not_found') 
25       end
26
27       if @find_user !=nil and @issues.first == nil
28         @issues = Issue.where(issue_type: @user_role).order(:status)
29         notice = t('issues.index.search.issues_not_found')
30       end
31
32       if notice
33         redirect_to issues_path, notice: notice
34       end 
35     
36     else
37       @issues = Issue.where(issue_type: @user_role).order(:status)
38     end
39   end
40
41   def show
42     @read_reports = @issue.read_reports
43     @unread_reports = @issue.unread_reports
44     @comments = @issue.comments
45     @related_issues = @issue.user.issues
46     if @issue.updated_by
47       @updated_by_admin = User.find(@issue.updated_by)
48     end
49   end
50
51   def new
52     unless create_new_issue_params.blank?
53       @issue = Issue.find_or_initialize_by(create_new_issue_params)
54       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
55       @report_strings_yaml = t( path)
56     end
57   end
58
59   def create
60
61     # TODO: Find better place to add these
62     admin_issues = [ 'DiaryEntry', 'DiaryComment', 'User']
63     moderator_issues = []
64
65     
66     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
67     # Check if Issue alrwady exists
68     if !@issue 
69       @issue = Issue.find_or_initialize_by(issue_params)
70       @issue.updated_by = nil
71       @admins = UserRole.where(role: "administrator")
72       @admins.each do |admin|
73         Notifier.new_issue_notification(User.find(admin.user_id)).deliver_now
74       end
75
76       # Reassign to moderators if it is a moderator issue
77       @issue.issue_type = "administrator"
78       if moderator_issues.include? @issue.reportable.class.name
79         reassign_issue
80       end
81     end
82
83     # Check if details provided are sufficient
84     if check_report_params
85       @report = @issue.reports.build(report_params)
86       details =  get_report_details
87       @report.reporter_user_id = @user.id
88       @report.details = details
89
90       # Checking if instance has been updated since last report
91       @last_report = @issue.reports.order(updated_at: :desc).last
92       if check_if_updated
93         if @issue.reopen
94           @issue.save!
95         end
96       end
97
98       if @issue.save!
99         redirect_to root_path, notice: t('issues.create.successful_report')
100       end
101     else
102       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')
103     end
104   end
105
106   def update
107     @issue = Issue.find_by(issue_params)
108     # Check if details provided are sufficient
109     if check_report_params
110       @report = @issue.reports.where(reporter_user_id: @user.id).first
111       
112       if @report == nil
113         @report = @issue.reports.build(report_params)
114         @report.reporter_user_id = @user.id
115         notice = t('issues.update.new_report')
116       end
117
118       details =  get_report_details
119       @report.details = details    
120
121     # Checking if instance has been updated since last report
122       @last_report = @issue.reports.order(updated_at: :desc).last
123       if check_if_updated
124         @issue.reopen
125         @issue.save!
126       end
127
128       if notice == nil
129         notice = t('issues.update.successful_update')
130       end
131
132       if @report.save!
133         redirect_to root_path, notice: notice
134       end
135     else
136       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')
137     end  
138   end
139
140   def comment
141     @issue = Issue.find(params[:id])
142     @issue_comment = @issue.comments.build(issue_comment_params)
143     @issue_comment.commenter_user_id = @user.id
144     if params[:reassign]
145       reassign_issue
146       @issue_comment.reassign = true
147     end
148     @issue_comment.save!
149     @issue.updated_by = @user.id
150     @issue.save!
151     redirect_to @issue
152   end
153
154   # Status Transistions
155   def resolve
156     if @issue.resolve
157       @issue.save!
158       redirect_to @issue, notice: t('issues.resolved')
159     else
160       render :show
161     end
162   end
163
164   def ignore
165     if @issue.ignore
166       @issue.updated_by = @user.id
167       @issue.save!
168       redirect_to @issue, notice: t('issues.ignored')
169     else
170       render :show
171     end
172   end
173
174   def reopen
175     if @issue.reopen
176       @issue.updated_by = @user.id      
177       @issue.save!
178       redirect_to @issue, notice: t('issues.reopened')
179     else
180       render :show
181     end
182   end
183
184   # Reassign Issues between Administrators and Moderators
185   def reassign_issue
186     if @issue.issue_type == "moderator"
187       @issue.issue_type = "administrator"
188     else
189       @issue.issue_type = "moderator"
190     end
191     @issue.save!
192   end
193
194   private
195
196     def check_if_updated
197       if @issue.reportable and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at
198         return true
199       else
200         return false
201       end
202     end
203  
204     def get_report_details
205       details = params[:report][:details] + "--||--"
206       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
207       @report_strings_yaml = t( path)
208       @report_strings_yaml.each do |k,v|
209         if params[k.to_sym]
210           details = details + params[k.to_sym] + "--||--"
211         end
212       end
213       return details
214     end
215
216     def check_report_params
217       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
218       @report_strings_yaml = t( path)
219       if params[:report] and params[:report][:details]
220         @report_strings_yaml.each do |k,v|
221           if params[k.to_sym]
222             return true
223           end
224         end
225       end
226       return false
227     end
228
229     def find_issue
230       @issue = Issue.find(params[:id])
231     end
232
233     def check_permission
234       unless @user.administrator?
235         flash[:error] = t('application.require_admin.not_an_admin')
236         redirect_to root_path
237       end
238     end
239
240     def create_new_issue_params
241       params.permit(:reportable_id, :reportable_type, :reported_user_id)
242     end
243
244     def issue_params
245       params[:issue].permit(:reportable_id, :reportable_type,:reported_user_id)
246     end
247
248     def report_params
249       params[:report].permit(:details)
250     end
251
252     def issue_comment_params
253       params.require(:issue_comment).permit(:body)
254     end
255
256 end