Description:
update problem stat to show all submissions change view to haml
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r448:f7ee3d7bf71a - - 4 files changed: 36 inserted, 31 deleted

@@ -0,0 +1,34
1 + :css
2 + .fix-width {
3 + font-family: "Consolas, Monaco, Droid Sans Mono,Mono, Monospace,Courier"
4 + }
5 +
6 + %h1 Problem stat: #{@problem.name}
7 + %h2 Overview
8 +
9 + %h2 Submissions
10 + - if @submissions and @submissions.count > 0
11 + %table.info#main_table
12 + %thead
13 + %tr.info-head
14 + %th ID
15 + %th Login
16 + %th Name
17 + %th Submitted_at
18 + %th Points
19 + %th comment
20 + %tbody
21 + - row_odd,curr = true,''
22 + - @submissions.each do |sub|
23 + - next unless sub.user
24 + - row_odd,curr = !row_odd, sub.user if curr != sub.user
25 + %tr{class: row_odd ? "info-odd" : "info-even"}
26 + %td= link_to sub.id, controller: 'graders', action: 'submission', id: sub.id
27 + %td= link_to sub.user.login, controller: :users, action: :profile, id: sub.user.id
28 + %td= sub.user.full_name
29 + %td= time_ago_in_words(sub.submitted_at) + " ago"
30 + %td= sub.points
31 + %td.fix-width= sub.grader_comment
32 + - else
33 + No submission
34 +
@@ -1,226 +1,226
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 list
10 list
11 render :action => 'list'
11 render :action => 'list'
12 end
12 end
13
13
14 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
14 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
15 verify :method => :post, :only => [ :destroy,
15 verify :method => :post, :only => [ :destroy,
16 :create, :quick_create,
16 :create, :quick_create,
17 :do_manage,
17 :do_manage,
18 :do_import,
18 :do_import,
19 :update ],
19 :update ],
20 :redirect_to => { :action => :list }
20 :redirect_to => { :action => :list }
21
21
22 def list
22 def list
23 @problems = Problem.find(:all, :order => 'date_added DESC')
23 @problems = Problem.find(:all, :order => 'date_added DESC')
24 end
24 end
25
25
26 def show
26 def show
27 @problem = Problem.find(params[:id])
27 @problem = Problem.find(params[:id])
28 end
28 end
29
29
30 def new
30 def new
31 @problem = Problem.new
31 @problem = Problem.new
32 @description = nil
32 @description = nil
33 end
33 end
34
34
35 def create
35 def create
36 @problem = Problem.new(params[:problem])
36 @problem = Problem.new(params[:problem])
37 @description = Description.new(params[:description])
37 @description = Description.new(params[:description])
38 if @description.body!=''
38 if @description.body!=''
39 if !@description.save
39 if !@description.save
40 render :action => new and return
40 render :action => new and return
41 end
41 end
42 else
42 else
43 @description = nil
43 @description = nil
44 end
44 end
45 @problem.description = @description
45 @problem.description = @description
46 if @problem.save
46 if @problem.save
47 flash[:notice] = 'Problem was successfully created.'
47 flash[:notice] = 'Problem was successfully created.'
48 redirect_to :action => 'list'
48 redirect_to :action => 'list'
49 else
49 else
50 render :action => 'new'
50 render :action => 'new'
51 end
51 end
52 end
52 end
53
53
54 def quick_create
54 def quick_create
55 @problem = Problem.new(params[:problem])
55 @problem = Problem.new(params[:problem])
56 @problem.full_name = @problem.name if @problem.full_name == ''
56 @problem.full_name = @problem.name if @problem.full_name == ''
57 @problem.full_score = 100
57 @problem.full_score = 100
58 @problem.available = false
58 @problem.available = false
59 @problem.test_allowed = true
59 @problem.test_allowed = true
60 @problem.output_only = false
60 @problem.output_only = false
61 @problem.date_added = Time.new
61 @problem.date_added = Time.new
62 if @problem.save
62 if @problem.save
63 flash[:notice] = 'Problem was successfully created.'
63 flash[:notice] = 'Problem was successfully created.'
64 redirect_to :action => 'list'
64 redirect_to :action => 'list'
65 else
65 else
66 flash[:notice] = 'Error saving problem'
66 flash[:notice] = 'Error saving problem'
67 redirect_to :action => 'list'
67 redirect_to :action => 'list'
68 end
68 end
69 end
69 end
70
70
71 def edit
71 def edit
72 @problem = Problem.find(params[:id])
72 @problem = Problem.find(params[:id])
73 @description = @problem.description
73 @description = @problem.description
74 end
74 end
75
75
76 def update
76 def update
77 @problem = Problem.find(params[:id])
77 @problem = Problem.find(params[:id])
78 @description = @problem.description
78 @description = @problem.description
79 if @description == nil and params[:description][:body]!=''
79 if @description == nil and params[:description][:body]!=''
80 @description = Description.new(params[:description])
80 @description = Description.new(params[:description])
81 if !@description.save
81 if !@description.save
82 flash[:notice] = 'Error saving description'
82 flash[:notice] = 'Error saving description'
83 render :action => 'edit' and return
83 render :action => 'edit' and return
84 end
84 end
85 @problem.description = @description
85 @problem.description = @description
86 elsif @description!=nil
86 elsif @description!=nil
87 if !@description.update_attributes(params[:description])
87 if !@description.update_attributes(params[:description])
88 flash[:notice] = 'Error saving description'
88 flash[:notice] = 'Error saving description'
89 render :action => 'edit' and return
89 render :action => 'edit' and return
90 end
90 end
91 end
91 end
92 if @problem.update_attributes(params[:problem])
92 if @problem.update_attributes(params[:problem])
93 flash[:notice] = 'Problem was successfully updated.'
93 flash[:notice] = 'Problem was successfully updated.'
94 redirect_to :action => 'show', :id => @problem
94 redirect_to :action => 'show', :id => @problem
95 else
95 else
96 render :action => 'edit'
96 render :action => 'edit'
97 end
97 end
98 end
98 end
99
99
100 def destroy
100 def destroy
101 Problem.find(params[:id]).destroy
101 Problem.find(params[:id]).destroy
102 redirect_to :action => 'list'
102 redirect_to :action => 'list'
103 end
103 end
104
104
105 def toggle
105 def toggle
106 @problem = Problem.find(params[:id])
106 @problem = Problem.find(params[:id])
107 @problem.available = !(@problem.available)
107 @problem.available = !(@problem.available)
108 @problem.save
108 @problem.save
109 end
109 end
110
110
111 def turn_all_off
111 def turn_all_off
112 Problem.find(:all,
112 Problem.find(:all,
113 :conditions => "available = 1").each do |problem|
113 :conditions => "available = 1").each do |problem|
114 problem.available = false
114 problem.available = false
115 problem.save
115 problem.save
116 end
116 end
117 redirect_to :action => 'list'
117 redirect_to :action => 'list'
118 end
118 end
119
119
120 def turn_all_on
120 def turn_all_on
121 Problem.find(:all,
121 Problem.find(:all,
122 :conditions => "available = 0").each do |problem|
122 :conditions => "available = 0").each do |problem|
123 problem.available = true
123 problem.available = true
124 problem.save
124 problem.save
125 end
125 end
126 redirect_to :action => 'list'
126 redirect_to :action => 'list'
127 end
127 end
128
128
129 def stat
129 def stat
130 @problem = Problem.find(params[:id])
130 @problem = Problem.find(params[:id])
131 if !@problem.available
131 if !@problem.available
132 redirect_to :controller => 'main', :action => 'list'
132 redirect_to :controller => 'main', :action => 'list'
133 else
133 else
134 - @submissions = Submission.find_all_last_by_problem(params[:id])
134 + @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
135 end
135 end
136 end
136 end
137
137
138 def manage
138 def manage
139 @problems = Problem.find(:all, :order => 'date_added DESC')
139 @problems = Problem.find(:all, :order => 'date_added DESC')
140 end
140 end
141
141
142 def do_manage
142 def do_manage
143 if params.has_key? 'change_date_added'
143 if params.has_key? 'change_date_added'
144 change_date_added
144 change_date_added
145 else params.has_key? 'add_to_contest'
145 else params.has_key? 'add_to_contest'
146 add_to_contest
146 add_to_contest
147 end
147 end
148 redirect_to :action => 'manage'
148 redirect_to :action => 'manage'
149 end
149 end
150
150
151 def import
151 def import
152 @allow_test_pair_import = allow_test_pair_import?
152 @allow_test_pair_import = allow_test_pair_import?
153 end
153 end
154
154
155 def do_import
155 def do_import
156 old_problem = Problem.find_by_name(params[:name])
156 old_problem = Problem.find_by_name(params[:name])
157 if !allow_test_pair_import? and params.has_key? :import_to_db
157 if !allow_test_pair_import? and params.has_key? :import_to_db
158 params.delete :import_to_db
158 params.delete :import_to_db
159 end
159 end
160 @problem, import_log = Problem.create_from_import_form_params(params,
160 @problem, import_log = Problem.create_from_import_form_params(params,
161 old_problem)
161 old_problem)
162
162
163 if !@problem.errors.empty?
163 if !@problem.errors.empty?
164 render :action => 'import' and return
164 render :action => 'import' and return
165 end
165 end
166
166
167 if old_problem!=nil
167 if old_problem!=nil
168 flash[:notice] = "The test data has been replaced for problem #{@problem.name}"
168 flash[:notice] = "The test data has been replaced for problem #{@problem.name}"
169 end
169 end
170 @log = import_log
170 @log = import_log
171 end
171 end
172
172
173 def remove_contest
173 def remove_contest
174 problem = Problem.find(params[:id])
174 problem = Problem.find(params[:id])
175 contest = Contest.find(params[:contest_id])
175 contest = Contest.find(params[:contest_id])
176 if problem!=nil and contest!=nil
176 if problem!=nil and contest!=nil
177 problem.contests.delete(contest)
177 problem.contests.delete(contest)
178 end
178 end
179 redirect_to :action => 'manage'
179 redirect_to :action => 'manage'
180 end
180 end
181
181
182 ##################################
182 ##################################
183 protected
183 protected
184
184
185 def allow_test_pair_import?
185 def allow_test_pair_import?
186 if defined? ALLOW_TEST_PAIR_IMPORT
186 if defined? ALLOW_TEST_PAIR_IMPORT
187 return ALLOW_TEST_PAIR_IMPORT
187 return ALLOW_TEST_PAIR_IMPORT
188 else
188 else
189 return false
189 return false
190 end
190 end
191 end
191 end
192
192
193 def change_date_added
193 def change_date_added
194 problems = get_problems_from_params
194 problems = get_problems_from_params
195 year = params[:date_added][:year].to_i
195 year = params[:date_added][:year].to_i
196 month = params[:date_added][:month].to_i
196 month = params[:date_added][:month].to_i
197 day = params[:date_added][:day].to_i
197 day = params[:date_added][:day].to_i
198 date = Date.new(year,month,day)
198 date = Date.new(year,month,day)
199 problems.each do |p|
199 problems.each do |p|
200 p.date_added = date
200 p.date_added = date
201 p.save
201 p.save
202 end
202 end
203 end
203 end
204
204
205 def add_to_contest
205 def add_to_contest
206 problems = get_problems_from_params
206 problems = get_problems_from_params
207 contest = Contest.find(params[:contest][:id])
207 contest = Contest.find(params[:contest][:id])
208 if contest!=nil and contest.enabled
208 if contest!=nil and contest.enabled
209 problems.each do |p|
209 problems.each do |p|
210 p.contests << contest
210 p.contests << contest
211 end
211 end
212 end
212 end
213 end
213 end
214
214
215 def get_problems_from_params
215 def get_problems_from_params
216 problems = []
216 problems = []
217 params.keys.each do |k|
217 params.keys.each do |k|
218 if k.index('prob-')==0
218 if k.index('prob-')==0
219 name, id = k.split('-')
219 name, id = k.split('-')
220 problems << Problem.find(id)
220 problems << Problem.find(id)
221 end
221 end
222 end
222 end
223 problems
223 problems
224 end
224 end
225
225
226 end
226 end
@@ -1,173 +1,173
1 class Submission < ActiveRecord::Base
1 class Submission < ActiveRecord::Base
2
2
3 belongs_to :language
3 belongs_to :language
4 belongs_to :problem
4 belongs_to :problem
5 belongs_to :user
5 belongs_to :user
6
6
7 before_validation :assign_problem
7 before_validation :assign_problem
8 before_validation :assign_language
8 before_validation :assign_language
9
9
10 validates_presence_of :source
10 validates_presence_of :source
11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
13 validate :must_have_valid_problem
13 validate :must_have_valid_problem
14 validate :must_specify_language
14 validate :must_specify_language
15
15
16 before_save :assign_latest_number_if_new_recond
16 before_save :assign_latest_number_if_new_recond
17
17
18 def self.find_last_by_user_and_problem(user_id, problem_id)
18 def self.find_last_by_user_and_problem(user_id, problem_id)
19 last_sub = find(:first,
19 last_sub = find(:first,
20 :conditions => {:user_id => user_id,
20 :conditions => {:user_id => user_id,
21 :problem_id => problem_id},
21 :problem_id => problem_id},
22 :order => 'number DESC')
22 :order => 'number DESC')
23 return last_sub
23 return last_sub
24 end
24 end
25
25
26 def self.find_all_last_by_problem(problem_id)
26 def self.find_all_last_by_problem(problem_id)
27 # need to put in SQL command, maybe there's a better way
27 # need to put in SQL command, maybe there's a better way
28 - Submission.find_by_sql("SELECT * FROM submissions " +
28 + Submission.includes(:user).find_by_sql("SELECT * FROM submissions " +
29 "WHERE id = " +
29 "WHERE id = " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
31 "WHERE subs.user_id = submissions.user_id AND " +
31 "WHERE subs.user_id = submissions.user_id AND " +
32 "problem_id = " + problem_id.to_s + " " +
32 "problem_id = " + problem_id.to_s + " " +
33 "GROUP BY user_id) " +
33 "GROUP BY user_id) " +
34 "ORDER BY user_id")
34 "ORDER BY user_id")
35 end
35 end
36
36
37 def self.find_in_range_by_user_and_problem(user_id, problem_id,since_id,until_id)
37 def self.find_in_range_by_user_and_problem(user_id, problem_id,since_id,until_id)
38 records = Submission.where(problem_id: problem_id,user_id: user_id)
38 records = Submission.where(problem_id: problem_id,user_id: user_id)
39 records = records.where('id >= ?',since_id) if since_id > 0
39 records = records.where('id >= ?',since_id) if since_id > 0
40 records = records.where('id <= ?',until_id) if until_id > 0
40 records = records.where('id <= ?',until_id) if until_id > 0
41 records.all
41 records.all
42 end
42 end
43
43
44 def self.find_last_for_all_available_problems(user_id)
44 def self.find_last_for_all_available_problems(user_id)
45 submissions = Array.new
45 submissions = Array.new
46 problems = Problem.find_available_problems
46 problems = Problem.find_available_problems
47 problems.each do |problem|
47 problems.each do |problem|
48 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
48 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
49 submissions << sub if sub!=nil
49 submissions << sub if sub!=nil
50 end
50 end
51 submissions
51 submissions
52 end
52 end
53
53
54 def self.find_by_user_problem_number(user_id, problem_id, number)
54 def self.find_by_user_problem_number(user_id, problem_id, number)
55 Submission.find(:first,
55 Submission.find(:first,
56 :conditions => {
56 :conditions => {
57 :user_id => user_id,
57 :user_id => user_id,
58 :problem_id => problem_id,
58 :problem_id => problem_id,
59 :number => number
59 :number => number
60 })
60 })
61 end
61 end
62
62
63 def self.find_all_by_user_problem(user_id, problem_id)
63 def self.find_all_by_user_problem(user_id, problem_id)
64 Submission.find(:all,
64 Submission.find(:all,
65 :conditions => {
65 :conditions => {
66 :user_id => user_id,
66 :user_id => user_id,
67 :problem_id => problem_id,
67 :problem_id => problem_id,
68 })
68 })
69 end
69 end
70
70
71 def download_filename
71 def download_filename
72 if self.problem.output_only
72 if self.problem.output_only
73 return self.source_filename
73 return self.source_filename
74 else
74 else
75 timestamp = self.submitted_at.localtime.strftime("%H%M%S")
75 timestamp = self.submitted_at.localtime.strftime("%H%M%S")
76 return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
76 return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
77 end
77 end
78 end
78 end
79
79
80 protected
80 protected
81
81
82 def self.find_option_in_source(option, source)
82 def self.find_option_in_source(option, source)
83 if source==nil
83 if source==nil
84 return nil
84 return nil
85 end
85 end
86 i = 0
86 i = 0
87 source.each_line do |s|
87 source.each_line do |s|
88 if s =~ option
88 if s =~ option
89 words = s.split
89 words = s.split
90 return words[1]
90 return words[1]
91 end
91 end
92 i = i + 1
92 i = i + 1
93 if i==10
93 if i==10
94 return nil
94 return nil
95 end
95 end
96 end
96 end
97 return nil
97 return nil
98 end
98 end
99
99
100 def self.find_language_in_source(source, source_filename="")
100 def self.find_language_in_source(source, source_filename="")
101 langopt = find_option_in_source(/^LANG:/,source)
101 langopt = find_option_in_source(/^LANG:/,source)
102 if langopt
102 if langopt
103 return (Language.find_by_name(langopt) ||
103 return (Language.find_by_name(langopt) ||
104 Language.find_by_pretty_name(langopt))
104 Language.find_by_pretty_name(langopt))
105 else
105 else
106 if source_filename
106 if source_filename
107 return Language.find_by_extension(source_filename.split('.').last)
107 return Language.find_by_extension(source_filename.split('.').last)
108 else
108 else
109 return nil
109 return nil
110 end
110 end
111 end
111 end
112 end
112 end
113
113
114 def self.find_problem_in_source(source, source_filename="")
114 def self.find_problem_in_source(source, source_filename="")
115 prob_opt = find_option_in_source(/^TASK:/,source)
115 prob_opt = find_option_in_source(/^TASK:/,source)
116 if problem = Problem.find_by_name(prob_opt)
116 if problem = Problem.find_by_name(prob_opt)
117 return problem
117 return problem
118 else
118 else
119 if source_filename
119 if source_filename
120 return Problem.find_by_name(source_filename.split('.').first)
120 return Problem.find_by_name(source_filename.split('.').first)
121 else
121 else
122 return nil
122 return nil
123 end
123 end
124 end
124 end
125 end
125 end
126
126
127 def assign_problem
127 def assign_problem
128 if self.problem_id!=-1
128 if self.problem_id!=-1
129 begin
129 begin
130 self.problem = Problem.find(self.problem_id)
130 self.problem = Problem.find(self.problem_id)
131 rescue ActiveRecord::RecordNotFound
131 rescue ActiveRecord::RecordNotFound
132 self.problem = nil
132 self.problem = nil
133 end
133 end
134 else
134 else
135 self.problem = Submission.find_problem_in_source(self.source,
135 self.problem = Submission.find_problem_in_source(self.source,
136 self.source_filename)
136 self.source_filename)
137 end
137 end
138 end
138 end
139
139
140 def assign_language
140 def assign_language
141 self.language = Submission.find_language_in_source(self.source,
141 self.language = Submission.find_language_in_source(self.source,
142 self.source_filename)
142 self.source_filename)
143 end
143 end
144
144
145 # validation codes
145 # validation codes
146 def must_specify_language
146 def must_specify_language
147 return if self.source==nil
147 return if self.source==nil
148
148
149 # for output_only tasks
149 # for output_only tasks
150 return if self.problem!=nil and self.problem.output_only
150 return if self.problem!=nil and self.problem.output_only
151
151
152 if self.language==nil
152 if self.language==nil
153 errors.add('source',"must specify programming language") unless self.language!=nil
153 errors.add('source',"must specify programming language") unless self.language!=nil
154 end
154 end
155 end
155 end
156
156
157 def must_have_valid_problem
157 def must_have_valid_problem
158 return if self.source==nil
158 return if self.source==nil
159 if self.problem==nil
159 if self.problem==nil
160 errors.add('problem',"must be specified.")
160 errors.add('problem',"must be specified.")
161 elsif (!self.problem.available) and (self.new_record?)
161 elsif (!self.problem.available) and (self.new_record?)
162 errors.add('problem',"must be valid.")
162 errors.add('problem',"must be valid.")
163 end
163 end
164 end
164 end
165
165
166 # callbacks
166 # callbacks
167 def assign_latest_number_if_new_recond
167 def assign_latest_number_if_new_recond
168 return if !self.new_record?
168 return if !self.new_record?
169 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
169 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
170 self.number = (latest==nil) ? 1 : latest.number + 1;
170 self.number = (latest==nil) ? 1 : latest.number + 1;
171 end
171 end
172
172
173 end
173 end
deleted file
You need to be logged in to leave comments. Login now