Description:
fix problem edit page
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r590:0ef720704122 - - 2 files changed: 4 inserted, 4 deleted

@@ -1,282 +1,282
1 class ProblemsController < ApplicationController
1 class ProblemsController < ApplicationController
2
2
3 before_filter :authenticate, :authorization
3 before_filter :authenticate, :authorization
4
4
5 in_place_edit_for :problem, :name
5 in_place_edit_for :problem, :name
6 in_place_edit_for :problem, :full_name
6 in_place_edit_for :problem, :full_name
7 in_place_edit_for :problem, :full_score
7 in_place_edit_for :problem, :full_score
8
8
9 def index
9 def index
10 @problems = Problem.find(:all, :order => 'date_added DESC')
10 @problems = Problem.find(:all, :order => 'date_added DESC')
11 end
11 end
12
12
13 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
13 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
14 verify :method => :post, :only => [ :destroy,
14 verify :method => :post, :only => [ :destroy,
15 :create, :quick_create,
15 :create, :quick_create,
16 :do_manage,
16 :do_manage,
17 :do_import,
17 :do_import,
18 - :update ],
18 + ],
19 :redirect_to => { :action => :index }
19 :redirect_to => { :action => :index }
20
20
21 def show
21 def show
22 @problem = Problem.find(params[:id])
22 @problem = Problem.find(params[:id])
23 end
23 end
24
24
25 def new
25 def new
26 @problem = Problem.new
26 @problem = Problem.new
27 @description = nil
27 @description = nil
28 end
28 end
29
29
30 def create
30 def create
31 @problem = Problem.new(params[:problem])
31 @problem = Problem.new(params[:problem])
32 @description = Description.new(params[:description])
32 @description = Description.new(params[:description])
33 if @description.body!=''
33 if @description.body!=''
34 if !@description.save
34 if !@description.save
35 render :action => new and return
35 render :action => new and return
36 end
36 end
37 else
37 else
38 @description = nil
38 @description = nil
39 end
39 end
40 @problem.description = @description
40 @problem.description = @description
41 if @problem.save
41 if @problem.save
42 flash[:notice] = 'Problem was successfully created.'
42 flash[:notice] = 'Problem was successfully created.'
43 redirect_to action: :index
43 redirect_to action: :index
44 else
44 else
45 render :action => 'new'
45 render :action => 'new'
46 end
46 end
47 end
47 end
48
48
49 def quick_create
49 def quick_create
50 @problem = Problem.new(params[:problem])
50 @problem = Problem.new(params[:problem])
51 @problem.full_name = @problem.name if @problem.full_name == ''
51 @problem.full_name = @problem.name if @problem.full_name == ''
52 @problem.full_score = 100
52 @problem.full_score = 100
53 @problem.available = false
53 @problem.available = false
54 @problem.test_allowed = true
54 @problem.test_allowed = true
55 @problem.output_only = false
55 @problem.output_only = false
56 @problem.date_added = Time.new
56 @problem.date_added = Time.new
57 if @problem.save
57 if @problem.save
58 flash[:notice] = 'Problem was successfully created.'
58 flash[:notice] = 'Problem was successfully created.'
59 redirect_to action: :index
59 redirect_to action: :index
60 else
60 else
61 flash[:notice] = 'Error saving problem'
61 flash[:notice] = 'Error saving problem'
62 redirect_to action: :index
62 redirect_to action: :index
63 end
63 end
64 end
64 end
65
65
66 def edit
66 def edit
67 @problem = Problem.find(params[:id])
67 @problem = Problem.find(params[:id])
68 @description = @problem.description
68 @description = @problem.description
69 end
69 end
70
70
71 def update
71 def update
72 @problem = Problem.find(params[:id])
72 @problem = Problem.find(params[:id])
73 @description = @problem.description
73 @description = @problem.description
74 - if @description == nil and params[:description][:body]!=''
74 + if @description.nil? and params[:description][:body]!=''
75 @description = Description.new(params[:description])
75 @description = Description.new(params[:description])
76 if !@description.save
76 if !@description.save
77 flash[:notice] = 'Error saving description'
77 flash[:notice] = 'Error saving description'
78 render :action => 'edit' and return
78 render :action => 'edit' and return
79 end
79 end
80 @problem.description = @description
80 @problem.description = @description
81 - elsif @description!=nil
81 + elsif @description
82 if !@description.update_attributes(params[:description])
82 if !@description.update_attributes(params[:description])
83 flash[:notice] = 'Error saving description'
83 flash[:notice] = 'Error saving description'
84 render :action => 'edit' and return
84 render :action => 'edit' and return
85 end
85 end
86 end
86 end
87 if params[:file] and params[:file].content_type != 'application/pdf'
87 if params[:file] and params[:file].content_type != 'application/pdf'
88 flash[:notice] = 'Error: Uploaded file is not PDF'
88 flash[:notice] = 'Error: Uploaded file is not PDF'
89 render :action => 'edit' and return
89 render :action => 'edit' and return
90 end
90 end
91 if @problem.update_attributes(params[:problem])
91 if @problem.update_attributes(params[:problem])
92 flash[:notice] = 'Problem was successfully updated.'
92 flash[:notice] = 'Problem was successfully updated.'
93 unless params[:file] == nil or params[:file] == ''
93 unless params[:file] == nil or params[:file] == ''
94 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
94 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
95 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
95 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
96 if not FileTest.exists? out_dirname
96 if not FileTest.exists? out_dirname
97 Dir.mkdir out_dirname
97 Dir.mkdir out_dirname
98 end
98 end
99
99
100 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
100 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
101 if FileTest.exists? out_filename
101 if FileTest.exists? out_filename
102 File.delete out_filename
102 File.delete out_filename
103 end
103 end
104
104
105 File.open(out_filename,"wb") do |file|
105 File.open(out_filename,"wb") do |file|
106 file.write(params[:file].read)
106 file.write(params[:file].read)
107 end
107 end
108 @problem.description_filename = "#{@problem.name}.pdf"
108 @problem.description_filename = "#{@problem.name}.pdf"
109 @problem.save
109 @problem.save
110 end
110 end
111 redirect_to :action => 'show', :id => @problem
111 redirect_to :action => 'show', :id => @problem
112 else
112 else
113 render :action => 'edit'
113 render :action => 'edit'
114 end
114 end
115 end
115 end
116
116
117 def destroy
117 def destroy
118 Problem.find(params[:id]).destroy
118 Problem.find(params[:id]).destroy
119 redirect_to action: :index
119 redirect_to action: :index
120 end
120 end
121
121
122 def toggle
122 def toggle
123 @problem = Problem.find(params[:id])
123 @problem = Problem.find(params[:id])
124 @problem.update_attributes(available: !(@problem.available) )
124 @problem.update_attributes(available: !(@problem.available) )
125 respond_to do |format|
125 respond_to do |format|
126 format.js { }
126 format.js { }
127 end
127 end
128 end
128 end
129
129
130 def toggle_test
130 def toggle_test
131 @problem = Problem.find(params[:id])
131 @problem = Problem.find(params[:id])
132 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
132 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
133 respond_to do |format|
133 respond_to do |format|
134 format.js { }
134 format.js { }
135 end
135 end
136 end
136 end
137
137
138 def turn_all_off
138 def turn_all_off
139 Problem.find(:all,
139 Problem.find(:all,
140 :conditions => "available = 1").each do |problem|
140 :conditions => "available = 1").each do |problem|
141 problem.available = false
141 problem.available = false
142 problem.save
142 problem.save
143 end
143 end
144 redirect_to action: :index
144 redirect_to action: :index
145 end
145 end
146
146
147 def turn_all_on
147 def turn_all_on
148 Problem.find(:all,
148 Problem.find(:all,
149 :conditions => "available = 0").each do |problem|
149 :conditions => "available = 0").each do |problem|
150 problem.available = true
150 problem.available = true
151 problem.save
151 problem.save
152 end
152 end
153 redirect_to action: :index
153 redirect_to action: :index
154 end
154 end
155
155
156 def stat
156 def stat
157 @problem = Problem.find(params[:id])
157 @problem = Problem.find(params[:id])
158 unless @problem.available or session[:admin]
158 unless @problem.available or session[:admin]
159 redirect_to :controller => 'main', :action => 'list'
159 redirect_to :controller => 'main', :action => 'list'
160 return
160 return
161 end
161 end
162 @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
162 @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
163
163
164 #stat summary
164 #stat summary
165 range =65
165 range =65
166 @histogram = { data: Array.new(range,0), summary: {} }
166 @histogram = { data: Array.new(range,0), summary: {} }
167 user = Hash.new(0)
167 user = Hash.new(0)
168 @submissions.find_each do |sub|
168 @submissions.find_each do |sub|
169 d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60
169 d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60
170 @histogram[:data][d.to_i] += 1 if d < range
170 @histogram[:data][d.to_i] += 1 if d < range
171 user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max
171 user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max
172 end
172 end
173 @histogram[:summary][:max] = [@histogram[:data].max,1].max
173 @histogram[:summary][:max] = [@histogram[:data].max,1].max
174
174
175 @summary = { attempt: user.count, solve: 0 }
175 @summary = { attempt: user.count, solve: 0 }
176 user.each_value { |v| @summary[:solve] += 1 if v == 1 }
176 user.each_value { |v| @summary[:solve] += 1 if v == 1 }
177 end
177 end
178
178
179 def manage
179 def manage
180 @problems = Problem.find(:all, :order => 'date_added DESC')
180 @problems = Problem.find(:all, :order => 'date_added DESC')
181 end
181 end
182
182
183 def do_manage
183 def do_manage
184 if params.has_key? 'change_date_added'
184 if params.has_key? 'change_date_added'
185 change_date_added
185 change_date_added
186 elsif params.has_key? 'add_to_contest'
186 elsif params.has_key? 'add_to_contest'
187 add_to_contest
187 add_to_contest
188 elsif params.has_key? 'enable_problem'
188 elsif params.has_key? 'enable_problem'
189 set_available(true)
189 set_available(true)
190 elsif params.has_key? 'disable_problem'
190 elsif params.has_key? 'disable_problem'
191 set_available(false)
191 set_available(false)
192 end
192 end
193 redirect_to :action => 'manage'
193 redirect_to :action => 'manage'
194 end
194 end
195
195
196 def import
196 def import
197 @allow_test_pair_import = allow_test_pair_import?
197 @allow_test_pair_import = allow_test_pair_import?
198 end
198 end
199
199
200 def do_import
200 def do_import
201 old_problem = Problem.find_by_name(params[:name])
201 old_problem = Problem.find_by_name(params[:name])
202 if !allow_test_pair_import? and params.has_key? :import_to_db
202 if !allow_test_pair_import? and params.has_key? :import_to_db
203 params.delete :import_to_db
203 params.delete :import_to_db
204 end
204 end
205 @problem, import_log = Problem.create_from_import_form_params(params,
205 @problem, import_log = Problem.create_from_import_form_params(params,
206 old_problem)
206 old_problem)
207
207
208 if !@problem.errors.empty?
208 if !@problem.errors.empty?
209 render :action => 'import' and return
209 render :action => 'import' and return
210 end
210 end
211
211
212 if old_problem!=nil
212 if old_problem!=nil
213 flash[:notice] = "The test data has been replaced for problem #{@problem.name}"
213 flash[:notice] = "The test data has been replaced for problem #{@problem.name}"
214 end
214 end
215 @log = import_log
215 @log = import_log
216 end
216 end
217
217
218 def remove_contest
218 def remove_contest
219 problem = Problem.find(params[:id])
219 problem = Problem.find(params[:id])
220 contest = Contest.find(params[:contest_id])
220 contest = Contest.find(params[:contest_id])
221 if problem!=nil and contest!=nil
221 if problem!=nil and contest!=nil
222 problem.contests.delete(contest)
222 problem.contests.delete(contest)
223 end
223 end
224 redirect_to :action => 'manage'
224 redirect_to :action => 'manage'
225 end
225 end
226
226
227 ##################################
227 ##################################
228 protected
228 protected
229
229
230 def allow_test_pair_import?
230 def allow_test_pair_import?
231 if defined? ALLOW_TEST_PAIR_IMPORT
231 if defined? ALLOW_TEST_PAIR_IMPORT
232 return ALLOW_TEST_PAIR_IMPORT
232 return ALLOW_TEST_PAIR_IMPORT
233 else
233 else
234 return false
234 return false
235 end
235 end
236 end
236 end
237
237
238 def change_date_added
238 def change_date_added
239 problems = get_problems_from_params
239 problems = get_problems_from_params
240 year = params[:date_added][:year].to_i
240 year = params[:date_added][:year].to_i
241 month = params[:date_added][:month].to_i
241 month = params[:date_added][:month].to_i
242 day = params[:date_added][:day].to_i
242 day = params[:date_added][:day].to_i
243 date = Date.new(year,month,day)
243 date = Date.new(year,month,day)
244 problems.each do |p|
244 problems.each do |p|
245 p.date_added = date
245 p.date_added = date
246 p.save
246 p.save
247 end
247 end
248 end
248 end
249
249
250 def add_to_contest
250 def add_to_contest
251 problems = get_problems_from_params
251 problems = get_problems_from_params
252 contest = Contest.find(params[:contest][:id])
252 contest = Contest.find(params[:contest][:id])
253 if contest!=nil and contest.enabled
253 if contest!=nil and contest.enabled
254 problems.each do |p|
254 problems.each do |p|
255 p.contests << contest
255 p.contests << contest
256 end
256 end
257 end
257 end
258 end
258 end
259
259
260 def set_available(avail)
260 def set_available(avail)
261 problems = get_problems_from_params
261 problems = get_problems_from_params
262 problems.each do |p|
262 problems.each do |p|
263 p.available = avail
263 p.available = avail
264 p.save
264 p.save
265 end
265 end
266 end
266 end
267
267
268 def get_problems_from_params
268 def get_problems_from_params
269 problems = []
269 problems = []
270 params.keys.each do |k|
270 params.keys.each do |k|
271 if k.index('prob-')==0
271 if k.index('prob-')==0
272 name, id, order = k.split('-')
272 name, id, order = k.split('-')
273 problems << Problem.find(id)
273 problems << Problem.find(id)
274 end
274 end
275 end
275 end
276 problems
276 problems
277 end
277 end
278
278
279 def get_problems_stat
279 def get_problems_stat
280 end
280 end
281
281
282 end
282 end
@@ -1,9 +1,9
1 <h1>Editing problem</h1>
1 <h1>Editing problem</h1>
2
2
3 - <%= form_tag({action: 'update', id: @problem},multipart: true) do %>
3 + <%= form_for @problem,url:{action: 'update'},html: {multipart: true} do %>
4 <%= render :partial => 'form' %>
4 <%= render :partial => 'form' %>
5 <%= submit_tag 'Edit' %>
5 <%= submit_tag 'Edit' %>
6 <% end %>
6 <% end %>
7
7
8 <%= link_to 'Show', :action => 'show', :id => @problem %> |
8 <%= link_to 'Show', :action => 'show', :id => @problem %> |
9 <%= link_to 'Back', :action => 'list' %>
9 <%= link_to 'Back', :action => 'list' %>
You need to be logged in to leave comments. Login now