Description:
allow ta to view problem stat
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r803:30dd5b343f6b - - 2 files changed: 5 inserted, 3 deleted
@@ -1,239 +1,238 | |||||
|
1 | require 'ipaddr' |
|
1 | require 'ipaddr' |
|
2 |
|
2 | ||
|
3 | class ApplicationController < ActionController::Base |
|
3 | class ApplicationController < ActionController::Base |
|
4 | protect_from_forgery |
|
4 | protect_from_forgery |
|
5 |
|
5 | ||
|
6 | before_action :current_user |
|
6 | before_action :current_user |
|
7 |
|
7 | ||
|
8 | SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode' |
|
8 | SINGLE_USER_MODE_CONF_KEY = 'system.single_user_mode' |
|
9 | MULTIPLE_IP_LOGIN_CONF_KEY = 'right.multiple_ip_login' |
|
9 | MULTIPLE_IP_LOGIN_CONF_KEY = 'right.multiple_ip_login' |
|
10 | WHITELIST_IGNORE_CONF_KEY = 'right.whitelist_ignore' |
|
10 | WHITELIST_IGNORE_CONF_KEY = 'right.whitelist_ignore' |
|
11 | WHITELIST_IP_CONF_KEY = 'right.whitelist_ip' |
|
11 | WHITELIST_IP_CONF_KEY = 'right.whitelist_ip' |
|
12 |
|
12 | ||
|
13 | #report and redirect for unauthorized activities |
|
13 | #report and redirect for unauthorized activities |
|
14 | def unauthorized_redirect(notice = 'You are not authorized to view the page you requested') |
|
14 | def unauthorized_redirect(notice = 'You are not authorized to view the page you requested') |
|
15 | flash[:notice] = notice |
|
15 | flash[:notice] = notice |
|
16 | redirect_to login_main_path |
|
16 | redirect_to login_main_path |
|
17 | end |
|
17 | end |
|
18 |
|
18 | ||
|
19 | # Returns the current logged-in user (if any). |
|
19 | # Returns the current logged-in user (if any). |
|
20 | def current_user |
|
20 | def current_user |
|
21 | return nil unless session[:user_id] |
|
21 | return nil unless session[:user_id] |
|
22 | @current_user ||= User.find(session[:user_id]) |
|
22 | @current_user ||= User.find(session[:user_id]) |
|
23 | end |
|
23 | end |
|
24 |
|
24 | ||
|
25 | def admin_authorization |
|
25 | def admin_authorization |
|
26 | return false unless check_valid_login |
|
26 | return false unless check_valid_login |
|
27 | user = User.includes(:roles).find(session[:user_id]) |
|
27 | user = User.includes(:roles).find(session[:user_id]) |
|
28 | unless user.admin? |
|
28 | unless user.admin? |
|
29 | unauthorized_redirect |
|
29 | unauthorized_redirect |
|
30 | return false |
|
30 | return false |
|
31 | end |
|
31 | end |
|
32 | return true |
|
32 | return true |
|
33 | end |
|
33 | end |
|
34 |
|
34 | ||
|
35 | def authorization_by_roles(allowed_roles) |
|
35 | def authorization_by_roles(allowed_roles) |
|
36 | return false unless check_valid_login |
|
36 | return false unless check_valid_login |
|
37 | - user = User.find(session[:user_id]) |
|
37 | + unless @current_user.roles.detect { |role| allowed_roles.member?(role.name) } |
|
38 | - unless user.roles.detect { |role| allowed_roles.member?(role.name) } |
|
||
|
39 | unauthorized_redirect |
|
38 | unauthorized_redirect |
|
40 | return false |
|
39 | return false |
|
41 | end |
|
40 | end |
|
42 | end |
|
41 | end |
|
43 |
|
42 | ||
|
44 | def testcase_authorization |
|
43 | def testcase_authorization |
|
45 | #admin always has privileged |
|
44 | #admin always has privileged |
|
46 | if @current_user.admin? |
|
45 | if @current_user.admin? |
|
47 | return true |
|
46 | return true |
|
48 | end |
|
47 | end |
|
49 |
|
48 | ||
|
50 | unauthorized_redirect unless GraderConfiguration["right.view_testcase"] |
|
49 | unauthorized_redirect unless GraderConfiguration["right.view_testcase"] |
|
51 | end |
|
50 | end |
|
52 |
|
51 | ||
|
53 |
|
52 | ||
|
54 | protected |
|
53 | protected |
|
55 |
|
54 | ||
|
56 | #redirect to root (and also force logout) |
|
55 | #redirect to root (and also force logout) |
|
57 | #if the user is not logged_in or the system is in "ADMIN ONLY" mode |
|
56 | #if the user is not logged_in or the system is in "ADMIN ONLY" mode |
|
58 | def check_valid_login |
|
57 | def check_valid_login |
|
59 | #check if logged in |
|
58 | #check if logged in |
|
60 | unless session[:user_id] |
|
59 | unless session[:user_id] |
|
61 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
60 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
62 | unauthorized_redirect('You need to login but you cannot log in at this time') |
|
61 | unauthorized_redirect('You need to login but you cannot log in at this time') |
|
63 | else |
|
62 | else |
|
64 | unauthorized_redirect('You need to login') |
|
63 | unauthorized_redirect('You need to login') |
|
65 | end |
|
64 | end |
|
66 | return false |
|
65 | return false |
|
67 | end |
|
66 | end |
|
68 |
|
67 | ||
|
69 | # check if run in single user mode |
|
68 | # check if run in single user mode |
|
70 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
69 | if GraderConfiguration[SINGLE_USER_MODE_CONF_KEY] |
|
71 | if @current_user==nil || (!@current_user.admin?) |
|
70 | if @current_user==nil || (!@current_user.admin?) |
|
72 | unauthorized_redirect('You cannot log in at this time') |
|
71 | unauthorized_redirect('You cannot log in at this time') |
|
73 | return false |
|
72 | return false |
|
74 | end |
|
73 | end |
|
75 | end |
|
74 | end |
|
76 |
|
75 | ||
|
77 | # check if the user is enabled |
|
76 | # check if the user is enabled |
|
78 | unless @current_user.enabled? || @current_user.admin? |
|
77 | unless @current_user.enabled? || @current_user.admin? |
|
79 | unauthorized_redirect 'Your account is disabled' |
|
78 | unauthorized_redirect 'Your account is disabled' |
|
80 | return false |
|
79 | return false |
|
81 | end |
|
80 | end |
|
82 |
|
81 | ||
|
83 | # check if user ip is allowed |
|
82 | # check if user ip is allowed |
|
84 | unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
83 | unless @current_user.admin? || GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
85 | unless is_request_ip_allowed? |
|
84 | unless is_request_ip_allowed? |
|
86 | unauthorized_redirect 'Your IP is not allowed to login at this time.' |
|
85 | unauthorized_redirect 'Your IP is not allowed to login at this time.' |
|
87 | return false |
|
86 | return false |
|
88 | end |
|
87 | end |
|
89 | end |
|
88 | end |
|
90 |
|
89 | ||
|
91 | if GraderConfiguration.multicontests? |
|
90 | if GraderConfiguration.multicontests? |
|
92 | return true if @current_user.admin? |
|
91 | return true if @current_user.admin? |
|
93 | begin |
|
92 | begin |
|
94 | if @current_user.contest_stat(true).forced_logout |
|
93 | if @current_user.contest_stat(true).forced_logout |
|
95 | flash[:notice] = 'You have been automatically logged out.' |
|
94 | flash[:notice] = 'You have been automatically logged out.' |
|
96 | redirect_to :controller => 'main', :action => 'index' |
|
95 | redirect_to :controller => 'main', :action => 'index' |
|
97 | end |
|
96 | end |
|
98 | rescue |
|
97 | rescue |
|
99 | end |
|
98 | end |
|
100 | end |
|
99 | end |
|
101 | return true |
|
100 | return true |
|
102 | end |
|
101 | end |
|
103 |
|
102 | ||
|
104 | #redirect to root (and also force logout) |
|
103 | #redirect to root (and also force logout) |
|
105 | #if the user use different ip from the previous connection |
|
104 | #if the user use different ip from the previous connection |
|
106 | # only applicable when MULTIPLE_IP_LOGIN options is false only |
|
105 | # only applicable when MULTIPLE_IP_LOGIN options is false only |
|
107 | def authenticate_by_ip_address |
|
106 | def authenticate_by_ip_address |
|
108 | #this assume that we have already authenticate normally |
|
107 | #this assume that we have already authenticate normally |
|
109 | unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY] |
|
108 | unless GraderConfiguration[MULTIPLE_IP_LOGIN_CONF_KEY] |
|
110 | user = User.find(session[:user_id]) |
|
109 | user = User.find(session[:user_id]) |
|
111 | if (!user.admin? && user.last_ip && user.last_ip != request.remote_ip) |
|
110 | if (!user.admin? && user.last_ip && user.last_ip != request.remote_ip) |
|
112 | flash[:notice] = "You cannot use the system from #{request.remote_ip}. Your last ip is #{user.last_ip}" |
|
111 | flash[:notice] = "You cannot use the system from #{request.remote_ip}. Your last ip is #{user.last_ip}" |
|
113 | redirect_to :controller => 'main', :action => 'login' |
|
112 | redirect_to :controller => 'main', :action => 'login' |
|
114 | return false |
|
113 | return false |
|
115 | end |
|
114 | end |
|
116 | unless user.last_ip |
|
115 | unless user.last_ip |
|
117 | user.last_ip = request.remote_ip |
|
116 | user.last_ip = request.remote_ip |
|
118 | user.save |
|
117 | user.save |
|
119 | end |
|
118 | end |
|
120 | end |
|
119 | end |
|
121 | return true |
|
120 | return true |
|
122 | end |
|
121 | end |
|
123 |
|
122 | ||
|
124 | def authorization |
|
123 | def authorization |
|
125 | return false unless check_valid_login |
|
124 | return false unless check_valid_login |
|
126 | user = User.find(session[:user_id]) |
|
125 | user = User.find(session[:user_id]) |
|
127 | unless user.roles.detect { |role| |
|
126 | unless user.roles.detect { |role| |
|
128 | role.rights.detect{ |right| |
|
127 | role.rights.detect{ |right| |
|
129 | right.controller == self.class.controller_name and |
|
128 | right.controller == self.class.controller_name and |
|
130 | (right.action == 'all' || right.action == action_name) |
|
129 | (right.action == 'all' || right.action == action_name) |
|
131 | } |
|
130 | } |
|
132 | } |
|
131 | } |
|
133 | flash[:notice] = 'You are not authorized to view the page you requested' |
|
132 | flash[:notice] = 'You are not authorized to view the page you requested' |
|
134 | #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login') |
|
133 | #request.env['HTTP_REFERER'] ? (redirect_to :back) : (redirect_to :controller => 'login') |
|
135 | redirect_to :controller => 'main', :action => 'login' |
|
134 | redirect_to :controller => 'main', :action => 'login' |
|
136 | return false |
|
135 | return false |
|
137 | end |
|
136 | end |
|
138 | end |
|
137 | end |
|
139 |
|
138 | ||
|
140 | def verify_time_limit |
|
139 | def verify_time_limit |
|
141 | return true if session[:user_id]==nil |
|
140 | return true if session[:user_id]==nil |
|
142 | user = User.find(session[:user_id], :include => :site) |
|
141 | user = User.find(session[:user_id], :include => :site) |
|
143 | return true if user==nil || user.site == nil |
|
142 | return true if user==nil || user.site == nil |
|
144 | if user.contest_finished? |
|
143 | if user.contest_finished? |
|
145 | flash[:notice] = 'Error: the contest you are participating is over.' |
|
144 | flash[:notice] = 'Error: the contest you are participating is over.' |
|
146 | redirect_to :back |
|
145 | redirect_to :back |
|
147 | return false |
|
146 | return false |
|
148 | end |
|
147 | end |
|
149 | return true |
|
148 | return true |
|
150 | end |
|
149 | end |
|
151 |
|
150 | ||
|
152 | def is_request_ip_allowed? |
|
151 | def is_request_ip_allowed? |
|
153 | unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
152 | unless GraderConfiguration[WHITELIST_IGNORE_CONF_KEY] |
|
154 | user_ip = IPAddr.new(request.remote_ip) |
|
153 | user_ip = IPAddr.new(request.remote_ip) |
|
155 | allowed = GraderConfiguration[WHITELIST_IP_CONF_KEY] || '' |
|
154 | allowed = GraderConfiguration[WHITELIST_IP_CONF_KEY] || '' |
|
156 |
|
155 | ||
|
157 | allowed.delete(' ').split(',').each do |ips| |
|
156 | allowed.delete(' ').split(',').each do |ips| |
|
158 | allow_ips = IPAddr.new(ips) |
|
157 | allow_ips = IPAddr.new(ips) |
|
159 | if allow_ips.include?(user_ip) |
|
158 | if allow_ips.include?(user_ip) |
|
160 | return true |
|
159 | return true |
|
161 | end |
|
160 | end |
|
162 | end |
|
161 | end |
|
163 | return false |
|
162 | return false |
|
164 | end |
|
163 | end |
|
165 | return true |
|
164 | return true |
|
166 | end |
|
165 | end |
|
167 |
|
166 | ||
|
168 | #function for datatable ajax query |
|
167 | #function for datatable ajax query |
|
169 | #return record,total_count,filter_count |
|
168 | #return record,total_count,filter_count |
|
170 | def process_query_record(record, |
|
169 | def process_query_record(record, |
|
171 | total_count: nil, |
|
170 | total_count: nil, |
|
172 | select: '', |
|
171 | select: '', |
|
173 | global_search: [], |
|
172 | global_search: [], |
|
174 | no_search: false, |
|
173 | no_search: false, |
|
175 | force_order: '', |
|
174 | force_order: '', |
|
176 | date_filter: '', date_param_since: 'date_since',date_param_until: 'date_until', |
|
175 | date_filter: '', date_param_since: 'date_since',date_param_until: 'date_until', |
|
177 | hard_limit: nil) |
|
176 | hard_limit: nil) |
|
178 | arel_table = record.model.arel_table |
|
177 | arel_table = record.model.arel_table |
|
179 |
|
178 | ||
|
180 | if !no_search && params['search'] |
|
179 | if !no_search && params['search'] |
|
181 | global_value = record.model.sanitize_sql(params['search']['value'].strip.downcase) |
|
180 | global_value = record.model.sanitize_sql(params['search']['value'].strip.downcase) |
|
182 | if !global_value.blank? |
|
181 | if !global_value.blank? |
|
183 | global_value.split.each do |value| |
|
182 | global_value.split.each do |value| |
|
184 | global_where = global_search.map{|f| "LOWER(#{f}) like '%#{value}%'"}.join(' OR ') |
|
183 | global_where = global_search.map{|f| "LOWER(#{f}) like '%#{value}%'"}.join(' OR ') |
|
185 | record = record.where(global_where) |
|
184 | record = record.where(global_where) |
|
186 | end |
|
185 | end |
|
187 | end |
|
186 | end |
|
188 |
|
187 | ||
|
189 | params['columns'].each do |i, col| |
|
188 | params['columns'].each do |i, col| |
|
190 | if !col['search']['value'].blank? |
|
189 | if !col['search']['value'].blank? |
|
191 | record = record.where(arel_table[col['name']].lower.matches("%#{col['search']['value'].strip.downcase}%")) |
|
190 | record = record.where(arel_table[col['name']].lower.matches("%#{col['search']['value'].strip.downcase}%")) |
|
192 | end |
|
191 | end |
|
193 | end |
|
192 | end |
|
194 | end |
|
193 | end |
|
195 |
|
194 | ||
|
196 | if !date_filter.blank? |
|
195 | if !date_filter.blank? |
|
197 | param_since = params[date_param_since] |
|
196 | param_since = params[date_param_since] |
|
198 | param_until = params[date_param_until] |
|
197 | param_until = params[date_param_until] |
|
199 | date_since = Time.zone.parse( param_since ) || Time.new(1,1,1) rescue Time.new(1,1,1) |
|
198 | date_since = Time.zone.parse( param_since ) || Time.new(1,1,1) rescue Time.new(1,1,1) |
|
200 | date_until = Time.zone.parse( param_until ) || Time.zone.now() rescue Time.zone.now() |
|
199 | date_until = Time.zone.parse( param_until ) || Time.zone.now() rescue Time.zone.now() |
|
201 | date_range = date_since..(date_until.end_of_day) |
|
200 | date_range = date_since..(date_until.end_of_day) |
|
202 | record = record.where(date_filter.to_sym => date_range) |
|
201 | record = record.where(date_filter.to_sym => date_range) |
|
203 | end |
|
202 | end |
|
204 |
|
203 | ||
|
205 | if force_order.blank? |
|
204 | if force_order.blank? |
|
206 | if params['order'] |
|
205 | if params['order'] |
|
207 | params['order'].each do |i, o| |
|
206 | params['order'].each do |i, o| |
|
208 | colName = params['columns'][o['column']]['name'] |
|
207 | colName = params['columns'][o['column']]['name'] |
|
209 | colName = "#{record.model.table_name}.#{colName}" if colName.upcase == 'ID' |
|
208 | colName = "#{record.model.table_name}.#{colName}" if colName.upcase == 'ID' |
|
210 | record = record.order("#{colName} #{o['dir'].casecmp('desc') != 0 ? 'ASC' : 'DESC'}") unless colName.blank? |
|
209 | record = record.order("#{colName} #{o['dir'].casecmp('desc') != 0 ? 'ASC' : 'DESC'}") unless colName.blank? |
|
211 | end |
|
210 | end |
|
212 | end |
|
211 | end |
|
213 | else |
|
212 | else |
|
214 | record = record.order(force_order) |
|
213 | record = record.order(force_order) |
|
215 | end |
|
214 | end |
|
216 |
|
215 | ||
|
217 | filterCount = record.count(record.model.primary_key) |
|
216 | filterCount = record.count(record.model.primary_key) |
|
218 | # if .group() is used, filterCount might be like {id_1: count_1, id_2: count_2, ...} |
|
217 | # if .group() is used, filterCount might be like {id_1: count_1, id_2: count_2, ...} |
|
219 | # so we should count the result again.. |
|
218 | # so we should count the result again.. |
|
220 | if filterCount.is_a? Hash |
|
219 | if filterCount.is_a? Hash |
|
221 | filterCount = filterCount.count |
|
220 | filterCount = filterCount.count |
|
222 | end |
|
221 | end |
|
223 |
|
222 | ||
|
224 |
|
223 | ||
|
225 | record = record.offset(params['start'] || 0) |
|
224 | record = record.offset(params['start'] || 0) |
|
226 | record = record.limit(hard_limit) |
|
225 | record = record.limit(hard_limit) |
|
227 | if (params['length']) |
|
226 | if (params['length']) |
|
228 | limit = params['length'].to_i |
|
227 | limit = params['length'].to_i |
|
229 | limit == hard_limit if (hard_limit && hard_limit < limit) |
|
228 | limit == hard_limit if (hard_limit && hard_limit < limit) |
|
230 | record = record.limit(limit) |
|
229 | record = record.limit(limit) |
|
231 | end |
|
230 | end |
|
232 | if (!select.blank?) |
|
231 | if (!select.blank?) |
|
233 | record = record.select(select) |
|
232 | record = record.select(select) |
|
234 | end |
|
233 | end |
|
235 |
|
234 | ||
|
236 | return record, total_count || record.model.count, filterCount |
|
235 | return record, total_count || record.model.count, filterCount |
|
237 | end |
|
236 | end |
|
238 |
|
237 | ||
|
239 | end |
|
238 | end |
@@ -1,307 +1,310 | |||||
|
1 | class ProblemsController < ApplicationController |
|
1 | class ProblemsController < ApplicationController |
|
2 |
|
2 | ||
|
3 | - before_action :admin_authorization |
|
3 | + before_action :admin_authorization, except: [:stat] |
|
|
4 | + before_action only: [:stat] do | ||
|
|
5 | + authorization_by_roles(['admin','ta']) | ||
|
|
6 | + end | ||
|
4 |
|
7 | ||
|
5 | in_place_edit_for :problem, :name |
|
8 | in_place_edit_for :problem, :name |
|
6 | in_place_edit_for :problem, :full_name |
|
9 | in_place_edit_for :problem, :full_name |
|
7 | in_place_edit_for :problem, :full_score |
|
10 | in_place_edit_for :problem, :full_score |
|
8 |
|
11 | ||
|
9 | def index |
|
12 | def index |
|
10 | @problems = Problem.order(date_added: :desc) |
|
13 | @problems = Problem.order(date_added: :desc) |
|
11 | end |
|
14 | end |
|
12 |
|
15 | ||
|
13 |
|
16 | ||
|
14 | def show |
|
17 | def show |
|
15 | @problem = Problem.find(params[:id]) |
|
18 | @problem = Problem.find(params[:id]) |
|
16 | end |
|
19 | end |
|
17 |
|
20 | ||
|
18 | def new |
|
21 | def new |
|
19 | @problem = Problem.new |
|
22 | @problem = Problem.new |
|
20 | @description = nil |
|
23 | @description = nil |
|
21 | end |
|
24 | end |
|
22 |
|
25 | ||
|
23 | def create |
|
26 | def create |
|
24 | @problem = Problem.new(problem_params) |
|
27 | @problem = Problem.new(problem_params) |
|
25 | @description = Description.new(description_params) |
|
28 | @description = Description.new(description_params) |
|
26 | if @description.body!='' |
|
29 | if @description.body!='' |
|
27 | if !@description.save |
|
30 | if !@description.save |
|
28 | render :action => new and return |
|
31 | render :action => new and return |
|
29 | end |
|
32 | end |
|
30 | else |
|
33 | else |
|
31 | @description = nil |
|
34 | @description = nil |
|
32 | end |
|
35 | end |
|
33 | @problem.description = @description |
|
36 | @problem.description = @description |
|
34 | if @problem.save |
|
37 | if @problem.save |
|
35 | flash[:notice] = 'Problem was successfully created.' |
|
38 | flash[:notice] = 'Problem was successfully created.' |
|
36 | redirect_to action: :index |
|
39 | redirect_to action: :index |
|
37 | else |
|
40 | else |
|
38 | render :action => 'new' |
|
41 | render :action => 'new' |
|
39 | end |
|
42 | end |
|
40 | end |
|
43 | end |
|
41 |
|
44 | ||
|
42 | def quick_create |
|
45 | def quick_create |
|
43 | @problem = Problem.new(problem_params) |
|
46 | @problem = Problem.new(problem_params) |
|
44 | @problem.full_name = @problem.name if @problem.full_name == '' |
|
47 | @problem.full_name = @problem.name if @problem.full_name == '' |
|
45 | @problem.full_score = 100 |
|
48 | @problem.full_score = 100 |
|
46 | @problem.available = false |
|
49 | @problem.available = false |
|
47 | @problem.test_allowed = true |
|
50 | @problem.test_allowed = true |
|
48 | @problem.output_only = false |
|
51 | @problem.output_only = false |
|
49 | @problem.date_added = Time.new |
|
52 | @problem.date_added = Time.new |
|
50 | if @problem.save |
|
53 | if @problem.save |
|
51 | flash[:notice] = 'Problem was successfully created.' |
|
54 | flash[:notice] = 'Problem was successfully created.' |
|
52 | redirect_to action: :index |
|
55 | redirect_to action: :index |
|
53 | else |
|
56 | else |
|
54 | flash[:notice] = 'Error saving problem' |
|
57 | flash[:notice] = 'Error saving problem' |
|
55 | redirect_to action: :index |
|
58 | redirect_to action: :index |
|
56 | end |
|
59 | end |
|
57 | end |
|
60 | end |
|
58 |
|
61 | ||
|
59 | def edit |
|
62 | def edit |
|
60 | @problem = Problem.find(params[:id]) |
|
63 | @problem = Problem.find(params[:id]) |
|
61 | @description = @problem.description |
|
64 | @description = @problem.description |
|
62 | end |
|
65 | end |
|
63 |
|
66 | ||
|
64 | def update |
|
67 | def update |
|
65 | @problem = Problem.find(params[:id]) |
|
68 | @problem = Problem.find(params[:id]) |
|
66 | @description = @problem.description |
|
69 | @description = @problem.description |
|
67 | if @description.nil? and params[:description][:body]!='' |
|
70 | if @description.nil? and params[:description][:body]!='' |
|
68 | @description = Description.new(description_params) |
|
71 | @description = Description.new(description_params) |
|
69 | if !@description.save |
|
72 | if !@description.save |
|
70 | flash[:notice] = 'Error saving description' |
|
73 | flash[:notice] = 'Error saving description' |
|
71 | render :action => 'edit' and return |
|
74 | render :action => 'edit' and return |
|
72 | end |
|
75 | end |
|
73 | @problem.description = @description |
|
76 | @problem.description = @description |
|
74 | elsif @description |
|
77 | elsif @description |
|
75 | if !@description.update_attributes(description_params) |
|
78 | if !@description.update_attributes(description_params) |
|
76 | flash[:notice] = 'Error saving description' |
|
79 | flash[:notice] = 'Error saving description' |
|
77 | render :action => 'edit' and return |
|
80 | render :action => 'edit' and return |
|
78 | end |
|
81 | end |
|
79 | end |
|
82 | end |
|
80 | if params[:file] and params[:file].content_type != 'application/pdf' |
|
83 | if params[:file] and params[:file].content_type != 'application/pdf' |
|
81 | flash[:notice] = 'Error: Uploaded file is not PDF' |
|
84 | flash[:notice] = 'Error: Uploaded file is not PDF' |
|
82 | render :action => 'edit' and return |
|
85 | render :action => 'edit' and return |
|
83 | end |
|
86 | end |
|
84 | if @problem.update_attributes(problem_params) |
|
87 | if @problem.update_attributes(problem_params) |
|
85 | flash[:notice] = 'Problem was successfully updated.' |
|
88 | flash[:notice] = 'Problem was successfully updated.' |
|
86 | unless params[:file] == nil or params[:file] == '' |
|
89 | unless params[:file] == nil or params[:file] == '' |
|
87 | flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.' |
|
90 | flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.' |
|
88 | out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" |
|
91 | out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" |
|
89 | if not FileTest.exists? out_dirname |
|
92 | if not FileTest.exists? out_dirname |
|
90 | Dir.mkdir out_dirname |
|
93 | Dir.mkdir out_dirname |
|
91 | end |
|
94 | end |
|
92 |
|
95 | ||
|
93 | out_filename = "#{out_dirname}/#{@problem.name}.pdf" |
|
96 | out_filename = "#{out_dirname}/#{@problem.name}.pdf" |
|
94 | if FileTest.exists? out_filename |
|
97 | if FileTest.exists? out_filename |
|
95 | File.delete out_filename |
|
98 | File.delete out_filename |
|
96 | end |
|
99 | end |
|
97 |
|
100 | ||
|
98 | File.open(out_filename,"wb") do |file| |
|
101 | File.open(out_filename,"wb") do |file| |
|
99 | file.write(params[:file].read) |
|
102 | file.write(params[:file].read) |
|
100 | end |
|
103 | end |
|
101 | @problem.description_filename = "#{@problem.name}.pdf" |
|
104 | @problem.description_filename = "#{@problem.name}.pdf" |
|
102 | @problem.save |
|
105 | @problem.save |
|
103 | end |
|
106 | end |
|
104 | redirect_to :action => 'show', :id => @problem |
|
107 | redirect_to :action => 'show', :id => @problem |
|
105 | else |
|
108 | else |
|
106 | render :action => 'edit' |
|
109 | render :action => 'edit' |
|
107 | end |
|
110 | end |
|
108 | end |
|
111 | end |
|
109 |
|
112 | ||
|
110 | def destroy |
|
113 | def destroy |
|
111 | p = Problem.find(params[:id]).destroy |
|
114 | p = Problem.find(params[:id]).destroy |
|
112 | redirect_to action: :index |
|
115 | redirect_to action: :index |
|
113 | end |
|
116 | end |
|
114 |
|
117 | ||
|
115 | def toggle |
|
118 | def toggle |
|
116 | @problem = Problem.find(params[:id]) |
|
119 | @problem = Problem.find(params[:id]) |
|
117 | @problem.update_attributes(available: !(@problem.available) ) |
|
120 | @problem.update_attributes(available: !(@problem.available) ) |
|
118 | respond_to do |format| |
|
121 | respond_to do |format| |
|
119 | format.js { } |
|
122 | format.js { } |
|
120 | end |
|
123 | end |
|
121 | end |
|
124 | end |
|
122 |
|
125 | ||
|
123 | def toggle_test |
|
126 | def toggle_test |
|
124 | @problem = Problem.find(params[:id]) |
|
127 | @problem = Problem.find(params[:id]) |
|
125 | @problem.update_attributes(test_allowed: !(@problem.test_allowed?) ) |
|
128 | @problem.update_attributes(test_allowed: !(@problem.test_allowed?) ) |
|
126 | respond_to do |format| |
|
129 | respond_to do |format| |
|
127 | format.js { } |
|
130 | format.js { } |
|
128 | end |
|
131 | end |
|
129 | end |
|
132 | end |
|
130 |
|
133 | ||
|
131 | def toggle_view_testcase |
|
134 | def toggle_view_testcase |
|
132 | @problem = Problem.find(params[:id]) |
|
135 | @problem = Problem.find(params[:id]) |
|
133 | @problem.update_attributes(view_testcase: !(@problem.view_testcase?) ) |
|
136 | @problem.update_attributes(view_testcase: !(@problem.view_testcase?) ) |
|
134 | respond_to do |format| |
|
137 | respond_to do |format| |
|
135 | format.js { } |
|
138 | format.js { } |
|
136 | end |
|
139 | end |
|
137 | end |
|
140 | end |
|
138 |
|
141 | ||
|
139 | def turn_all_off |
|
142 | def turn_all_off |
|
140 | Problem.available.all.each do |problem| |
|
143 | Problem.available.all.each do |problem| |
|
141 | problem.available = false |
|
144 | problem.available = false |
|
142 | problem.save |
|
145 | problem.save |
|
143 | end |
|
146 | end |
|
144 | redirect_to action: :index |
|
147 | redirect_to action: :index |
|
145 | end |
|
148 | end |
|
146 |
|
149 | ||
|
147 | def turn_all_on |
|
150 | def turn_all_on |
|
148 | Problem.where.not(available: true).each do |problem| |
|
151 | Problem.where.not(available: true).each do |problem| |
|
149 | problem.available = true |
|
152 | problem.available = true |
|
150 | problem.save |
|
153 | problem.save |
|
151 | end |
|
154 | end |
|
152 | redirect_to action: :index |
|
155 | redirect_to action: :index |
|
153 | end |
|
156 | end |
|
154 |
|
157 | ||
|
155 | def stat |
|
158 | def stat |
|
156 | @problem = Problem.find(params[:id]) |
|
159 | @problem = Problem.find(params[:id]) |
|
157 | unless @problem.available or session[:admin] |
|
160 | unless @problem.available or session[:admin] |
|
158 | redirect_to :controller => 'main', :action => 'list' |
|
161 | redirect_to :controller => 'main', :action => 'list' |
|
159 | return |
|
162 | return |
|
160 | end |
|
163 | end |
|
161 | @submissions = Submission.includes(:user).includes(:language).where(problem_id: params[:id]).order(:user_id,:id) |
|
164 | @submissions = Submission.includes(:user).includes(:language).where(problem_id: params[:id]).order(:user_id,:id) |
|
162 |
|
165 | ||
|
163 | #stat summary |
|
166 | #stat summary |
|
164 | range =65 |
|
167 | range =65 |
|
165 | @histogram = { data: Array.new(range,0), summary: {} } |
|
168 | @histogram = { data: Array.new(range,0), summary: {} } |
|
166 | user = Hash.new(0) |
|
169 | user = Hash.new(0) |
|
167 | @submissions.find_each do |sub| |
|
170 | @submissions.find_each do |sub| |
|
168 | d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60 |
|
171 | d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60 |
|
169 | @histogram[:data][d.to_i] += 1 if d < range |
|
172 | @histogram[:data][d.to_i] += 1 if d < range |
|
170 | user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max |
|
173 | user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max |
|
171 | end |
|
174 | end |
|
172 | @histogram[:summary][:max] = [@histogram[:data].max,1].max |
|
175 | @histogram[:summary][:max] = [@histogram[:data].max,1].max |
|
173 |
|
176 | ||
|
174 | @summary = { attempt: user.count, solve: 0 } |
|
177 | @summary = { attempt: user.count, solve: 0 } |
|
175 | user.each_value { |v| @summary[:solve] += 1 if v == 1 } |
|
178 | user.each_value { |v| @summary[:solve] += 1 if v == 1 } |
|
176 | end |
|
179 | end |
|
177 |
|
180 | ||
|
178 | def manage |
|
181 | def manage |
|
179 | @problems = Problem.order(date_added: :desc) |
|
182 | @problems = Problem.order(date_added: :desc) |
|
180 | end |
|
183 | end |
|
181 |
|
184 | ||
|
182 | def do_manage |
|
185 | def do_manage |
|
183 | if params.has_key? 'change_date_added' and params[:date_added].strip.empty? == false |
|
186 | if params.has_key? 'change_date_added' and params[:date_added].strip.empty? == false |
|
184 | change_date_added |
|
187 | change_date_added |
|
185 | elsif params.has_key? 'add_to_contest' |
|
188 | elsif params.has_key? 'add_to_contest' |
|
186 | add_to_contest |
|
189 | add_to_contest |
|
187 | elsif params.has_key? 'enable_problem' |
|
190 | elsif params.has_key? 'enable_problem' |
|
188 | set_available(true) |
|
191 | set_available(true) |
|
189 | elsif params.has_key? 'disable_problem' |
|
192 | elsif params.has_key? 'disable_problem' |
|
190 | set_available(false) |
|
193 | set_available(false) |
|
191 | elsif params.has_key? 'add_group' |
|
194 | elsif params.has_key? 'add_group' |
|
192 | group = Group.find(params[:group_id]) |
|
195 | group = Group.find(params[:group_id]) |
|
193 | ok = [] |
|
196 | ok = [] |
|
194 | failed = [] |
|
197 | failed = [] |
|
195 | get_problems_from_params.each do |p| |
|
198 | get_problems_from_params.each do |p| |
|
196 | begin |
|
199 | begin |
|
197 | group.problems << p |
|
200 | group.problems << p |
|
198 | ok << p.full_name |
|
201 | ok << p.full_name |
|
199 | rescue => e |
|
202 | rescue => e |
|
200 | failed << p.full_name |
|
203 | failed << p.full_name |
|
201 | end |
|
204 | end |
|
202 | end |
|
205 | end |
|
203 | flash[:success] = "The following problems are added to the group #{group.name}: " + ok.join(', ') if ok.count > 0 |
|
206 | flash[:success] = "The following problems are added to the group #{group.name}: " + ok.join(', ') if ok.count > 0 |
|
204 | flash[:alert] = "The following problems are already in the group #{group.name}: " + failed.join(', ') if failed.count > 0 |
|
207 | flash[:alert] = "The following problems are already in the group #{group.name}: " + failed.join(', ') if failed.count > 0 |
|
205 | elsif params.has_key? 'add_tags' |
|
208 | elsif params.has_key? 'add_tags' |
|
206 | get_problems_from_params.each do |p| |
|
209 | get_problems_from_params.each do |p| |
|
207 | p.tag_ids += params[:tag_ids] |
|
210 | p.tag_ids += params[:tag_ids] |
|
208 | end |
|
211 | end |
|
209 | end |
|
212 | end |
|
210 |
|
213 | ||
|
211 | redirect_to :action => 'manage' |
|
214 | redirect_to :action => 'manage' |
|
212 | end |
|
215 | end |
|
213 |
|
216 | ||
|
214 | def import |
|
217 | def import |
|
215 | @allow_test_pair_import = allow_test_pair_import? |
|
218 | @allow_test_pair_import = allow_test_pair_import? |
|
216 | end |
|
219 | end |
|
217 |
|
220 | ||
|
218 | def do_import |
|
221 | def do_import |
|
219 | old_problem = Problem.find_by_name(params[:name]) |
|
222 | old_problem = Problem.find_by_name(params[:name]) |
|
220 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
223 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
221 | params.delete :import_to_db |
|
224 | params.delete :import_to_db |
|
222 | end |
|
225 | end |
|
223 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
226 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
224 | old_problem) |
|
227 | old_problem) |
|
225 |
|
228 | ||
|
226 | if !@problem.errors.empty? |
|
229 | if !@problem.errors.empty? |
|
227 | render :action => 'import' and return |
|
230 | render :action => 'import' and return |
|
228 | end |
|
231 | end |
|
229 |
|
232 | ||
|
230 | if old_problem!=nil |
|
233 | if old_problem!=nil |
|
231 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
234 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
232 | end |
|
235 | end |
|
233 | @log = import_log |
|
236 | @log = import_log |
|
234 | end |
|
237 | end |
|
235 |
|
238 | ||
|
236 | def remove_contest |
|
239 | def remove_contest |
|
237 | problem = Problem.find(params[:id]) |
|
240 | problem = Problem.find(params[:id]) |
|
238 | contest = Contest.find(params[:contest_id]) |
|
241 | contest = Contest.find(params[:contest_id]) |
|
239 | if problem!=nil and contest!=nil |
|
242 | if problem!=nil and contest!=nil |
|
240 | problem.contests.delete(contest) |
|
243 | problem.contests.delete(contest) |
|
241 | end |
|
244 | end |
|
242 | redirect_to :action => 'manage' |
|
245 | redirect_to :action => 'manage' |
|
243 | end |
|
246 | end |
|
244 |
|
247 | ||
|
245 | ################################## |
|
248 | ################################## |
|
246 | protected |
|
249 | protected |
|
247 |
|
250 | ||
|
248 | def allow_test_pair_import? |
|
251 | def allow_test_pair_import? |
|
249 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
252 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
250 | return ALLOW_TEST_PAIR_IMPORT |
|
253 | return ALLOW_TEST_PAIR_IMPORT |
|
251 | else |
|
254 | else |
|
252 | return false |
|
255 | return false |
|
253 | end |
|
256 | end |
|
254 | end |
|
257 | end |
|
255 |
|
258 | ||
|
256 | def change_date_added |
|
259 | def change_date_added |
|
257 | problems = get_problems_from_params |
|
260 | problems = get_problems_from_params |
|
258 | date = Date.parse(params[:date_added]) |
|
261 | date = Date.parse(params[:date_added]) |
|
259 | problems.each do |p| |
|
262 | problems.each do |p| |
|
260 | p.date_added = date |
|
263 | p.date_added = date |
|
261 | p.save |
|
264 | p.save |
|
262 | end |
|
265 | end |
|
263 | end |
|
266 | end |
|
264 |
|
267 | ||
|
265 | def add_to_contest |
|
268 | def add_to_contest |
|
266 | problems = get_problems_from_params |
|
269 | problems = get_problems_from_params |
|
267 | contest = Contest.find(params[:contest][:id]) |
|
270 | contest = Contest.find(params[:contest][:id]) |
|
268 | if contest!=nil and contest.enabled |
|
271 | if contest!=nil and contest.enabled |
|
269 | problems.each do |p| |
|
272 | problems.each do |p| |
|
270 | p.contests << contest |
|
273 | p.contests << contest |
|
271 | end |
|
274 | end |
|
272 | end |
|
275 | end |
|
273 | end |
|
276 | end |
|
274 |
|
277 | ||
|
275 | def set_available(avail) |
|
278 | def set_available(avail) |
|
276 | problems = get_problems_from_params |
|
279 | problems = get_problems_from_params |
|
277 | problems.each do |p| |
|
280 | problems.each do |p| |
|
278 | p.available = avail |
|
281 | p.available = avail |
|
279 | p.save |
|
282 | p.save |
|
280 | end |
|
283 | end |
|
281 | end |
|
284 | end |
|
282 |
|
285 | ||
|
283 | def get_problems_from_params |
|
286 | def get_problems_from_params |
|
284 | problems = [] |
|
287 | problems = [] |
|
285 | params.keys.each do |k| |
|
288 | params.keys.each do |k| |
|
286 | if k.index('prob-')==0 |
|
289 | if k.index('prob-')==0 |
|
287 | name, id, order = k.split('-') |
|
290 | name, id, order = k.split('-') |
|
288 | problems << Problem.find(id) |
|
291 | problems << Problem.find(id) |
|
289 | end |
|
292 | end |
|
290 | end |
|
293 | end |
|
291 | problems |
|
294 | problems |
|
292 | end |
|
295 | end |
|
293 |
|
296 | ||
|
294 | def get_problems_stat |
|
297 | def get_problems_stat |
|
295 | end |
|
298 | end |
|
296 |
|
299 | ||
|
297 | private |
|
300 | private |
|
298 |
|
301 | ||
|
299 | def problem_params |
|
302 | def problem_params |
|
300 | params.require(:problem).permit(:name, :full_name, :full_score, :change_date_added, :date_added, :available, :test_allowed,:output_only, :url, :description, tag_ids:[]) |
|
303 | params.require(:problem).permit(:name, :full_name, :full_score, :change_date_added, :date_added, :available, :test_allowed,:output_only, :url, :description, tag_ids:[]) |
|
301 | end |
|
304 | end |
|
302 |
|
305 | ||
|
303 | def description_params |
|
306 | def description_params |
|
304 | params.require(:description).permit(:body, :markdowned) |
|
307 | params.require(:description).permit(:body, :markdowned) |
|
305 | end |
|
308 | end |
|
306 |
|
309 | ||
|
307 | end |
|
310 | end |
You need to be logged in to leave comments.
Login now