Description:
controllers get available problems from current user
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r288:74436ad04ad0 - - 3 files changed: 42 inserted, 45 deleted

@@ -62,230 +62,189
62 @submission.source_filename = params['file'].original_filename
62 @submission.source_filename = params['file'].original_filename
63 end
63 end
64 @submission.submitted_at = Time.new.gmtime
64 @submission.submitted_at = Time.new.gmtime
65
65
66 if Configuration.time_limit_mode? and user.contest_finished?
66 if Configuration.time_limit_mode? and user.contest_finished?
67 @submission.errors.add_to_base "The contest is over."
67 @submission.errors.add_to_base "The contest is over."
68 prepare_list_information
68 prepare_list_information
69 render :action => 'list' and return
69 render :action => 'list' and return
70 end
70 end
71
71
72 if @submission.valid?
72 if @submission.valid?
73 if @submission.save == false
73 if @submission.save == false
74 flash[:notice] = 'Error saving your submission'
74 flash[:notice] = 'Error saving your submission'
75 elsif Task.create(:submission_id => @submission.id,
75 elsif Task.create(:submission_id => @submission.id,
76 :status => Task::STATUS_INQUEUE) == false
76 :status => Task::STATUS_INQUEUE) == false
77 flash[:notice] = 'Error adding your submission to task queue'
77 flash[:notice] = 'Error adding your submission to task queue'
78 end
78 end
79 else
79 else
80 prepare_list_information
80 prepare_list_information
81 render :action => 'list' and return
81 render :action => 'list' and return
82 end
82 end
83 redirect_to :action => 'list'
83 redirect_to :action => 'list'
84 end
84 end
85
85
86 def source
86 def source
87 submission = Submission.find(params[:id])
87 submission = Submission.find(params[:id])
88 if submission.user_id == session[:user_id]
88 if submission.user_id == session[:user_id]
89 send_data(submission.source,
89 send_data(submission.source,
90 {:filename => submission.download_filename,
90 {:filename => submission.download_filename,
91 :type => 'text/plain'})
91 :type => 'text/plain'})
92 else
92 else
93 flash[:notice] = 'Error viewing source'
93 flash[:notice] = 'Error viewing source'
94 redirect_to :action => 'list'
94 redirect_to :action => 'list'
95 end
95 end
96 end
96 end
97
97
98 def compiler_msg
98 def compiler_msg
99 @submission = Submission.find(params[:id])
99 @submission = Submission.find(params[:id])
100 if @submission.user_id == session[:user_id]
100 if @submission.user_id == session[:user_id]
101 render :action => 'compiler_msg', :layout => 'empty'
101 render :action => 'compiler_msg', :layout => 'empty'
102 else
102 else
103 flash[:notice] = 'Error viewing source'
103 flash[:notice] = 'Error viewing source'
104 redirect_to :action => 'list'
104 redirect_to :action => 'list'
105 end
105 end
106 end
106 end
107
107
108 def submission
108 def submission
109 @user = User.find(session[:user_id])
109 @user = User.find(session[:user_id])
110 - @problems = Problem.find_available_problems
110 + @problems = @user.available_problems
111 if params[:id]==nil
111 if params[:id]==nil
112 @problem = nil
112 @problem = nil
113 @submissions = nil
113 @submissions = nil
114 else
114 else
115 @problem = Problem.find_by_name(params[:id])
115 @problem = Problem.find_by_name(params[:id])
116 if not @problem.available
116 if not @problem.available
117 redirect_to :action => 'list'
117 redirect_to :action => 'list'
118 flash[:notice] = 'Error: submissions for that problem are not viewable.'
118 flash[:notice] = 'Error: submissions for that problem are not viewable.'
119 return
119 return
120 end
120 end
121 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
121 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
122 end
122 end
123 end
123 end
124
124
125 def result
125 def result
126 if !Configuration.show_grading_result
126 if !Configuration.show_grading_result
127 redirect_to :action => 'list' and return
127 redirect_to :action => 'list' and return
128 end
128 end
129 @user = User.find(session[:user_id])
129 @user = User.find(session[:user_id])
130 @submission = Submission.find(params[:id])
130 @submission = Submission.find(params[:id])
131 if @submission.user!=@user
131 if @submission.user!=@user
132 flash[:notice] = 'You are not allowed to view result of other users.'
132 flash[:notice] = 'You are not allowed to view result of other users.'
133 redirect_to :action => 'list' and return
133 redirect_to :action => 'list' and return
134 end
134 end
135 prepare_grading_result(@submission)
135 prepare_grading_result(@submission)
136 end
136 end
137
137
138 def load_output
138 def load_output
139 if !Configuration.show_grading_result or params[:num]==nil
139 if !Configuration.show_grading_result or params[:num]==nil
140 redirect_to :action => 'list' and return
140 redirect_to :action => 'list' and return
141 end
141 end
142 @user = User.find(session[:user_id])
142 @user = User.find(session[:user_id])
143 @submission = Submission.find(params[:id])
143 @submission = Submission.find(params[:id])
144 if @submission.user!=@user
144 if @submission.user!=@user
145 flash[:notice] = 'You are not allowed to view result of other users.'
145 flash[:notice] = 'You are not allowed to view result of other users.'
146 redirect_to :action => 'list' and return
146 redirect_to :action => 'list' and return
147 end
147 end
148 case_num = params[:num].to_i
148 case_num = params[:num].to_i
149 out_filename = output_filename(@user.login,
149 out_filename = output_filename(@user.login,
150 @submission.problem.name,
150 @submission.problem.name,
151 @submission.id,
151 @submission.id,
152 case_num)
152 case_num)
153 if !FileTest.exists?(out_filename)
153 if !FileTest.exists?(out_filename)
154 flash[:notice] = 'Output not found.'
154 flash[:notice] = 'Output not found.'
155 redirect_to :action => 'list' and return
155 redirect_to :action => 'list' and return
156 end
156 end
157
157
158 response.headers['Content-Type'] = "application/force-download"
158 response.headers['Content-Type'] = "application/force-download"
159 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
159 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
160 response.headers["X-Sendfile"] = out_filename
160 response.headers["X-Sendfile"] = out_filename
161 response.headers['Content-length'] = File.size(out_filename)
161 response.headers['Content-length'] = File.size(out_filename)
162 render :nothing => true
162 render :nothing => true
163 end
163 end
164
164
165 def error
165 def error
166 @user = User.find(session[:user_id])
166 @user = User.find(session[:user_id])
167 end
167 end
168
168
169 # announcement refreshing and hiding methods
169 # announcement refreshing and hiding methods
170
170
171 def announcements
171 def announcements
172 if params.has_key? 'recent'
172 if params.has_key? 'recent'
173 prepare_announcements(params[:recent])
173 prepare_announcements(params[:recent])
174 else
174 else
175 prepare_announcements
175 prepare_announcements
176 end
176 end
177 render(:partial => 'announcement',
177 render(:partial => 'announcement',
178 :collection => @announcements,
178 :collection => @announcements,
179 :locals => {:announcement_effect => true})
179 :locals => {:announcement_effect => true})
180 end
180 end
181
181
182 protected
182 protected
183
183
184 def prepare_announcements(recent=nil)
184 def prepare_announcements(recent=nil)
185 if Configuration.show_tasks_to?(@user)
185 if Configuration.show_tasks_to?(@user)
186 @announcements = Announcement.find_published(true)
186 @announcements = Announcement.find_published(true)
187 else
187 else
188 @announcements = Announcement.find_published
188 @announcements = Announcement.find_published
189 end
189 end
190 if recent!=nil
190 if recent!=nil
191 recent_id = recent.to_i
191 recent_id = recent.to_i
192 @announcements = @announcements.find_all { |a| a.id > recent_id }
192 @announcements = @announcements.find_all { |a| a.id > recent_id }
193 end
193 end
194 end
194 end
195
195
196 - def problem_list_by_user_contests(user)
197 - contest_problems = []
198 - pin = {}
199 - user.contests.enabled.each do |contest|
200 - available_problems = contest.problems.available
201 - contest_problems << {
202 - :contest => contest,
203 - :problems => available_problems
204 - }
205 - available_problems.each {|p| pin[p.id] = true}
206 - end
207 - other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
208 - contest_problems << {
209 - :contest => nil,
210 - :problems => other_avaiable_problems
211 - }
212 - return contest_problems
213 - end
214 -
215 - def problem_list_for_user(user, contest_problems=nil)
216 - if not Configuration.multicontests?
217 - return Problem.find_available_problems
218 - else
219 - if contest_problems==nil
220 - contest_problems = problem_list_by_user_contests(user)
221 - end
222 -
223 - problems = []
224 - collected = {}
225 - contest_problems.each do |cp|
226 - cp[:problems].each do |problem|
227 - if not collected[problem.id]
228 - problems << problem
229 - collected[problem.id] = true
230 - end
231 - end
232 - end
233 - return problems
234 - end
235 - end
236 -
237 def prepare_list_information
196 def prepare_list_information
238 @user = User.find(session[:user_id])
197 @user = User.find(session[:user_id])
239 if not Configuration.multicontests?
198 if not Configuration.multicontests?
240 @problems = problem_list_for_user(@user)
199 @problems = problem_list_for_user(@user)
241 else
200 else
242 - @contest_problems = problem_list_by_user_contests(@user)
201 + @contest_problems = @user.available_problems_group_by_contests
243 - @problems = problem_list_for_user(@user, @contest_problems)
202 + @problems = @user.available_problems
244 end
203 end
245 @prob_submissions = {}
204 @prob_submissions = {}
246 @problems.each do |p|
205 @problems.each do |p|
247 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
206 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
248 if sub!=nil
207 if sub!=nil
249 @prob_submissions[p.id] = { :count => sub.number, :submission => sub }
208 @prob_submissions[p.id] = { :count => sub.number, :submission => sub }
250 else
209 else
251 @prob_submissions[p.id] = { :count => 0, :submission => nil }
210 @prob_submissions[p.id] = { :count => 0, :submission => nil }
252 end
211 end
253 end
212 end
254 prepare_announcements
213 prepare_announcements
255 end
214 end
256
215
257 def check_viewability
216 def check_viewability
258 @user = User.find(session[:user_id])
217 @user = User.find(session[:user_id])
259 if (!Configuration.show_tasks_to?(@user)) and
218 if (!Configuration.show_tasks_to?(@user)) and
260 ((action_name=='submission') or (action_name=='submit'))
219 ((action_name=='submission') or (action_name=='submit'))
261 redirect_to :action => 'list' and return
220 redirect_to :action => 'list' and return
262 end
221 end
263 end
222 end
264
223
265 def prepare_grading_result(submission)
224 def prepare_grading_result(submission)
266 if Configuration.task_grading_info.has_key? submission.problem.name
225 if Configuration.task_grading_info.has_key? submission.problem.name
267 grading_info = Configuration.task_grading_info[submission.problem.name]
226 grading_info = Configuration.task_grading_info[submission.problem.name]
268 else
227 else
269 # guess task info from problem.full_score
228 # guess task info from problem.full_score
270 cases = submission.problem.full_score / 10
229 cases = submission.problem.full_score / 10
271 grading_info = {
230 grading_info = {
272 'testruns' => cases,
231 'testruns' => cases,
273 'testcases' => cases
232 'testcases' => cases
274 }
233 }
275 end
234 end
276 @test_runs = []
235 @test_runs = []
277 if grading_info['testruns'].is_a? Integer
236 if grading_info['testruns'].is_a? Integer
278 trun_count = grading_info['testruns']
237 trun_count = grading_info['testruns']
279 trun_count.times do |i|
238 trun_count.times do |i|
280 @test_runs << [ read_grading_result(@user.login,
239 @test_runs << [ read_grading_result(@user.login,
281 submission.problem.name,
240 submission.problem.name,
282 submission.id,
241 submission.id,
283 i+1) ]
242 i+1) ]
284 end
243 end
285 else
244 else
286 grading_info['testruns'].keys.sort.each do |num|
245 grading_info['testruns'].keys.sort.each do |num|
287 run = []
246 run = []
288 testrun = grading_info['testruns'][num]
247 testrun = grading_info['testruns'][num]
289 testrun.each do |c|
248 testrun.each do |c|
290 run << read_grading_result(@user.login,
249 run << read_grading_result(@user.login,
291 submission.problem.name,
250 submission.problem.name,
@@ -1,58 +1,58
1 class TasksController < ApplicationController
1 class TasksController < ApplicationController
2
2
3 before_filter :authenticate, :check_viewability
3 before_filter :authenticate, :check_viewability
4
4
5 def index
5 def index
6 redirect_to :action => 'list'
6 redirect_to :action => 'list'
7 end
7 end
8
8
9 def list
9 def list
10 - @problems = Problem.find_available_problems
10 + @problems = @user.available_problems
11 end
11 end
12
12
13 # this has contest-wide access control
13 # this has contest-wide access control
14 def view
14 def view
15 base_name = params[:file]
15 base_name = params[:file]
16 base_filename = File.basename("#{base_name}.#{params[:ext]}")
16 base_filename = File.basename("#{base_name}.#{params[:ext]}")
17 filename = "#{Problem.download_file_basedir}/#{base_filename}"
17 filename = "#{Problem.download_file_basedir}/#{base_filename}"
18
18
19 if !FileTest.exists?(filename)
19 if !FileTest.exists?(filename)
20 redirect_to :action => 'index' and return
20 redirect_to :action => 'index' and return
21 end
21 end
22
22
23 send_file_to_user(filename, base_filename)
23 send_file_to_user(filename, base_filename)
24 end
24 end
25
25
26 # this has problem-level access control
26 # this has problem-level access control
27 def download
27 def download
28 problem = Problem.find(params[:id])
28 problem = Problem.find(params[:id])
29 if !problem or !problem.available or !@user.can_view_problem? problem
29 if !problem or !problem.available or !@user.can_view_problem? problem
30 redirect_to :action => 'index' and return
30 redirect_to :action => 'index' and return
31 end
31 end
32
32
33 base_name = params[:file]
33 base_name = params[:file]
34 base_filename = File.basename("#{base_name}.#{params[:ext]}")
34 base_filename = File.basename("#{base_name}.#{params[:ext]}")
35 filename = "#{Problem.download_file_basedir}/#{params[:id]}/#{base_filename}"
35 filename = "#{Problem.download_file_basedir}/#{params[:id]}/#{base_filename}"
36 puts "SENDING: #{filename}"
36 puts "SENDING: #{filename}"
37
37
38 if !FileTest.exists?(filename)
38 if !FileTest.exists?(filename)
39 redirect_to :action => 'index' and return
39 redirect_to :action => 'index' and return
40 end
40 end
41
41
42 puts "SENDING: #{filename}"
42 puts "SENDING: #{filename}"
43
43
44 send_file_to_user(filename, base_filename)
44 send_file_to_user(filename, base_filename)
45 end
45 end
46
46
47 protected
47 protected
48
48
49 def send_file_to_user(filename, base_filename)
49 def send_file_to_user(filename, base_filename)
50 if defined?(USE_APACHE_XSENDFILE) and USE_APACHE_XSENDFILE
50 if defined?(USE_APACHE_XSENDFILE) and USE_APACHE_XSENDFILE
51 response.headers['Content-Type'] = "application/force-download"
51 response.headers['Content-Type'] = "application/force-download"
52 response.headers['Content-Disposition'] = "attachment; filename=\"#{File.basename(filename)}\""
52 response.headers['Content-Disposition'] = "attachment; filename=\"#{File.basename(filename)}\""
53 response.headers["X-Sendfile"] = filename
53 response.headers["X-Sendfile"] = filename
54 response.headers['Content-length'] = File.size(filename)
54 response.headers['Content-length'] = File.size(filename)
55 render :nothing => true
55 render :nothing => true
56 else
56 else
57 if params[:ext]=='pdf'
57 if params[:ext]=='pdf'
58 content_type = 'application/pdf'
58 content_type = 'application/pdf'
@@ -152,96 +152,134
152 def contest_finished?
152 def contest_finished?
153 if Configuration.contest_mode?
153 if Configuration.contest_mode?
154 return false if site==nil
154 return false if site==nil
155 return site.finished?
155 return site.finished?
156 elsif Configuration.indv_contest_mode?
156 elsif Configuration.indv_contest_mode?
157 time_limit = Configuration.contest_time_limit
157 time_limit = Configuration.contest_time_limit
158
158
159 return false if contest_stat==nil
159 return false if contest_stat==nil
160
160
161 return contest_time_left == 0
161 return contest_time_left == 0
162 else
162 else
163 return false
163 return false
164 end
164 end
165 end
165 end
166
166
167 def contest_started?
167 def contest_started?
168 if Configuration.contest_mode?
168 if Configuration.contest_mode?
169 return true if site==nil
169 return true if site==nil
170 return site.started
170 return site.started
171 else
171 else
172 return true
172 return true
173 end
173 end
174 end
174 end
175
175
176 def update_start_time
176 def update_start_time
177 stat = self.contest_stat
177 stat = self.contest_stat
178 if stat == nil
178 if stat == nil
179 stat = UserContestStat.new(:user => self,
179 stat = UserContestStat.new(:user => self,
180 :started_at => Time.now.gmtime)
180 :started_at => Time.now.gmtime)
181 stat.save
181 stat.save
182 end
182 end
183 end
183 end
184
184
185 def problem_in_user_contests?(problem)
185 def problem_in_user_contests?(problem)
186 problem_contests = problem.contests.all
186 problem_contests = problem.contests.all
187
187
188 if problem_contests.length == 0 # this is public contest
188 if problem_contests.length == 0 # this is public contest
189 return true
189 return true
190 end
190 end
191
191
192 contests.each do |contest|
192 contests.each do |contest|
193 if problem_contests.find {|c| c.id == contest.id }
193 if problem_contests.find {|c| c.id == contest.id }
194 return true
194 return true
195 end
195 end
196 end
196 end
197 return false
197 return false
198 end
198 end
199
199
200 + def available_problems_group_by_contests
201 + contest_problems = []
202 + pin = {}
203 + contests.enabled.each do |contest|
204 + available_problems = contest.problems.available
205 + contest_problems << {
206 + :contest => contest,
207 + :problems => available_problems
208 + }
209 + available_problems.each {|p| pin[p.id] = true}
210 + end
211 + other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
212 + contest_problems << {
213 + :contest => nil,
214 + :problems => other_avaiable_problems
215 + }
216 + return contest_problems
217 + end
218 +
219 + def available_problems
220 + if not Configuration.multicontests?
221 + return Problem.find_available_problems
222 + else
223 + contest_problems = []
224 + pin = {}
225 + contests.enabled.each do |contest|
226 + contest.problems.available.each do |problem|
227 + if not pin.has_key? problem.id
228 + contest_problems << problem
229 + end
230 + pin[problem.id] = true
231 + end
232 + end
233 + other_avaiable_problems = Problem.available.find_all {|p| pin[p.id]==nil and p.contests.length==0}
234 + return contest_problems + other_avaiable_problems
235 + end
236 + end
237 +
200 def can_view_problem?(problem)
238 def can_view_problem?(problem)
201 if not Configuration.multicontests?
239 if not Configuration.multicontests?
202 return problem.available
240 return problem.available
203 else
241 else
204 return problem_in_user_contests? problem
242 return problem_in_user_contests? problem
205 end
243 end
206 end
244 end
207
245
208 protected
246 protected
209 def encrypt_new_password
247 def encrypt_new_password
210 return if password.blank?
248 return if password.blank?
211 self.salt = (10+rand(90)).to_s
249 self.salt = (10+rand(90)).to_s
212 self.hashed_password = User.encrypt(self.password,self.salt)
250 self.hashed_password = User.encrypt(self.password,self.salt)
213 end
251 end
214
252
215 def assign_default_site
253 def assign_default_site
216 # have to catch error when migrating (because self.site is not available).
254 # have to catch error when migrating (because self.site is not available).
217 begin
255 begin
218 if self.site==nil
256 if self.site==nil
219 self.site = Site.find_by_name('default')
257 self.site = Site.find_by_name('default')
220 if self.site==nil
258 if self.site==nil
221 self.site = Site.find(1) # when 'default has be renamed'
259 self.site = Site.find(1) # when 'default has be renamed'
222 end
260 end
223 end
261 end
224 rescue
262 rescue
225 end
263 end
226 end
264 end
227
265
228 def password_required?
266 def password_required?
229 self.hashed_password.blank? || !self.password.blank?
267 self.hashed_password.blank? || !self.password.blank?
230 end
268 end
231
269
232 def self.encrypt(string,salt)
270 def self.encrypt(string,salt)
233 Digest::SHA1.hexdigest(salt + string)
271 Digest::SHA1.hexdigest(salt + string)
234 end
272 end
235
273
236 def uniqueness_of_email_from_activated_users
274 def uniqueness_of_email_from_activated_users
237 user = User.activated_users.find_by_email(self.email)
275 user = User.activated_users.find_by_email(self.email)
238 if user and (user.login != self.login)
276 if user and (user.login != self.login)
239 self.errors.add_to_base("Email has already been taken")
277 self.errors.add_to_base("Email has already been taken")
240 end
278 end
241 end
279 end
242
280
243 def enough_time_interval_between_same_email_registrations
281 def enough_time_interval_between_same_email_registrations
244 return if !self.new_record?
282 return if !self.new_record?
245 return if self.activated
283 return if self.activated
246 open_user = User.find_by_email(self.email,
284 open_user = User.find_by_email(self.email,
247 :order => 'created_at DESC')
285 :order => 'created_at DESC')
You need to be logged in to leave comments. Login now