Description:
added codejom_status model for computing level with basic user update
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r219:95cc49d72ca8 - - 9 files changed: 79 inserted, 1 deleted

@@ -0,0 +1,5
1 + class CodejomStatus < ActiveRecord::Base
2 +
3 + belongs_to :user
4 +
5 + end
@@ -0,0 +1,15
1 + class CreateCodejomStatuses < ActiveRecord::Migration
2 + def self.up
3 + create_table :codejom_statuses do |t|
4 + t.integer :user_id
5 + t.boolean :alive
6 + t.integer :num_problems_passed
7 +
8 + t.timestamps
9 + end
10 + end
11 +
12 + def self.down
13 + drop_table :codejom_statuses
14 + end
15 + end
@@ -0,0 +1,11
1 + # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 +
3 + one:
4 + user_id: 1
5 + alive: false
6 + num_problems_passed: 1
7 +
8 + two:
9 + user_id: 1
10 + alive: false
11 + num_problems_passed: 1
@@ -0,0 +1,8
1 + require 'test_helper'
2 +
3 + class CodejomStatusTest < ActiveSupport::TestCase
4 + # Replace this with your real tests.
5 + test "the truth" do
6 + assert true
7 + end
8 + end
@@ -195,96 +195,97
195 195 end
196 196
197 197 def submit_solution
198 198 problem = Problem.find(params[:id])
199 199 user = User.find(session[:user_id])
200 200 recent_assignment = user.get_recent_test_pair_assignment_for problem
201 201 if recent_assignment == nil
202 202 flash[:notice] = 'You have not requested for any input data for this problem. Please download an input first.'
203 203 redirect_to :action => 'list' and return
204 204 end
205 205
206 206 if recent_assignment.submitted
207 207 flash[:notice] = 'You have already submitted an incorrect solution for this input. Please download a new input data.'
208 208 redirect_to :action => 'list' and return
209 209 end
210 210
211 211 if params[:file] == nil
212 212 flash[:notice] = 'You have not submitted any output.'
213 213 redirect_to :action => 'list' and return
214 214 end
215 215
216 216 submitted_solution = params[:file].read
217 217 test_pair = recent_assignment.test_pair
218 218 passed = test_pair.grade(submitted_solution)
219 219 points = passed ? 100 : 0
220 220 submission = Submission.new(:user => user,
221 221 :problem => problem,
222 222 :source => submitted_solution,
223 223 :source_filename => params['file'].original_filename,
224 224 :language_id => 0,
225 225 :submitted_at => Time.new.gmtime,
226 226 :graded_at => Time.new.gmtime,
227 227 :points => points)
228 228 submission.save
229 229 recent_assignment.submitted = true
230 230 recent_assignment.save
231 231
232 232 status = user.get_submission_status_for(problem)
233 233 if status == nil
234 234 status = SubmissionStatus.new :user => user, :problem => problem, :submission_count => 0
235 235 end
236 236
237 237 status.submission_count += 1
238 238 status.passed = passed
239 239 status.save
240 240
241 241 if passed
242 242 flash[:notice] = 'Correct solution.'
243 + user.update_codejom_status
243 244 else
244 245 flash[:notice] = 'Incorrect solution.'
245 246 end
246 247 redirect_to :action => 'list'
247 248 end
248 249
249 250 protected
250 251
251 252 def prepare_announcements(recent=nil)
252 253 if Configuration.show_tasks_to?(@user)
253 254 @announcements = Announcement.find_published(true)
254 255 else
255 256 @announcements = Announcement.find_published
256 257 end
257 258 if recent!=nil
258 259 recent_id = recent.to_i
259 260 @announcements = @announcements.find_all { |a| a.id > recent_id }
260 261 end
261 262 end
262 263
263 264 def prepare_list_information
264 265 @user = User.find(session[:user_id])
265 266
266 267 all_problems = Problem.find_available_problems
267 268
268 269 passed = {}
269 270 sub_count = {}
270 271 @user.submission_statuses.each do |status|
271 272 if status.passed
272 273 passed[status.problem_id] = true
273 274 end
274 275 sub_count[status.problem_id] = status.submission_count
275 276 end
276 277
277 278 @problems = all_problems.reject { |problem| passed.has_key? problem.id }
278 279
279 280 @prob_submissions = Array.new
280 281 @problems.each do |p|
281 282 if sub_count.has_key? p.id
282 283 @prob_submissions << { :count => sub_count[p.id] }
283 284 else
284 285 @prob_submissions << { :count => 0 }
285 286 end
286 287 end
287 288 prepare_announcements
288 289 end
289 290
290 291 def check_viewability
@@ -1,79 +1,84
1 1 class Problem < ActiveRecord::Base
2 2
3 3 belongs_to :description
4 4 has_many :test_pairs, :dependent => :delete_all
5 5
6 6 validates_presence_of :name
7 7 validates_format_of :name, :with => /^\w+$/
8 8 validates_presence_of :full_name
9 9
10 10 DEFAULT_TIME_LIMIT = 1
11 11 DEFAULT_MEMORY_LIMIT = 32
12 12
13 13 def test_pair_count
14 14 @test_pair_count ||= test_pairs.size
15 15 end
16 16
17 17 def uses_random_test_pair?
18 18 test_pair_count != 0
19 19 end
20 20
21 21 def random_test_pair(forbidden_numbers=nil)
22 22 begin
23 23 test_num = 1 + rand(test_pair_count)
24 24 end while forbidden_numbers!=nil and forbidden_numbers.include? test_num
25 25 test_pairs.find_by_number test_num
26 26 end
27 27
28 28 def self.find_available_problems
29 29 find(:all, :conditions => {:available => true}, :order => "date_added DESC")
30 30 end
31 31
32 + # TODO: may try to optimize this using cache
33 + def self.available_problem_count
34 + return Problem.find_available_problems.length
35 + end
36 +
32 37 def self.create_from_import_form_params(params, old_problem=nil)
33 38 problem = old_problem || Problem.new
34 39 import_params = Problem.extract_params_and_check(params, problem)
35 40
36 41 if not problem.valid?
37 42 return problem, 'Error importing'
38 43 end
39 44
40 45 problem.full_score = 100
41 46 problem.date_added = Time.new
42 47 problem.test_allowed = true
43 48 problem.output_only = false
44 49 problem.available = false
45 50
46 51 if not problem.save
47 52 return problem, 'Error importing'
48 53 end
49 54
50 55 import_to_db = params.has_key? :import_to_db
51 56
52 57 importer = TestdataImporter.new(problem)
53 58
54 59 if not importer.import_from_file(import_params[:file],
55 60 import_params[:time_limit],
56 61 import_params[:memory_limit],
57 62 import_to_db)
58 63 problem.errors.add_to_base('Import error.')
59 64 end
60 65
61 66 return problem, importer.log_msg
62 67 end
63 68
64 69 protected
65 70
66 71 def self.to_i_or_default(st, default)
67 72 if st!=''
68 73 st.to_i
69 74 else
70 75 default
71 76 end
72 77 end
73 78
74 79 def self.extract_params_and_check(params, problem)
75 80 time_limit = Problem.to_i_or_default(params[:time_limit],
76 81 DEFAULT_TIME_LIMIT)
77 82 memory_limit = Problem.to_i_or_default(params[:memory_limit],
78 83 DEFAULT_MEMORY_LIMIT)
79 84
@@ -1,74 +1,77
1 1 require 'digest/sha1'
2 2
3 3 class User < ActiveRecord::Base
4 4
5 5 has_and_belongs_to_many :roles
6 6
7 7 has_many :test_requests, :order => "submitted_at DESC"
8 8
9 9 has_many :messages,
10 10 :class_name => "Message",
11 11 :foreign_key => "sender_id",
12 12 :order => 'created_at DESC'
13 13
14 14 has_many :replied_messages,
15 15 :class_name => "Message",
16 16 :foreign_key => "receiver_id",
17 17 :order => 'created_at DESC'
18 18
19 19 has_many :test_pair_assignments, :dependent => :delete_all
20 20 has_many :submission_statuses
21 21
22 22 has_one :contest_stat, :class_name => "UserContestStat"
23 23
24 24 belongs_to :site
25 25 belongs_to :country
26 26
27 + # For Code Jom
28 + has_one :codejom_status
29 +
27 30 named_scope :activated_users, :conditions => {:activated => true}
28 31
29 32 validates_presence_of :login
30 33 validates_uniqueness_of :login
31 34 validates_format_of :login, :with => /^[\_A-Za-z0-9]+$/
32 35 validates_length_of :login, :within => 3..30
33 36
34 37 validates_presence_of :full_name
35 38 validates_length_of :full_name, :minimum => 1
36 39
37 40 validates_presence_of :password, :if => :password_required?
38 41 validates_length_of :password, :within => 4..20, :if => :password_required?
39 42 validates_confirmation_of :password, :if => :password_required?
40 43
41 44 validates_format_of :email,
42 45 :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i,
43 46 :if => :email_validation?
44 47 validate :uniqueness_of_email_from_activated_users,
45 48 :if => :email_validation?
46 49 validate :enough_time_interval_between_same_email_registrations,
47 50 :if => :email_validation?
48 51
49 52 # these are for ytopc
50 53 # disable for now
51 54 #validates_presence_of :province
52 55
53 56 attr_accessor :password
54 57
55 58 before_save :encrypt_new_password
56 59 before_save :assign_default_site
57 60
58 61 def self.authenticate(login, password)
59 62 user = find_by_login(login)
60 63 return user if user && user.authenticated?(password)
61 64 end
62 65
63 66 def authenticated?(password)
64 67 if self.activated
65 68 hashed_password == User.encrypt(password,self.salt)
66 69 else
67 70 false
68 71 end
69 72 end
70 73
71 74 def admin?
72 75 self.roles.detect {|r| r.name == 'admin' }
73 76 end
74 77
@@ -176,96 +179,114
176 179 # Contest information
177 180
178 181 def contest_time_left
179 182 if Configuration.contest_mode?
180 183 return nil if site==nil
181 184 return site.time_left
182 185 elsif Configuration.indv_contest_mode?
183 186 time_limit = Configuration.contest_time_limit
184 187 if contest_stat==nil
185 188 return (Time.now.gmtime + time_limit) - Time.now.gmtime
186 189 else
187 190 finish_time = contest_stat.started_at + time_limit
188 191 current_time = Time.now.gmtime
189 192 if current_time > finish_time
190 193 return 0
191 194 else
192 195 return finish_time - current_time
193 196 end
194 197 end
195 198 else
196 199 return nil
197 200 end
198 201 end
199 202
200 203 def contest_finished?
201 204 if Configuration.contest_mode?
202 205 return false if site==nil
203 206 return site.finished?
204 207 elsif Configuration.indv_contest_mode?
205 208 time_limit = Configuration.contest_time_limit
206 209
207 210 return false if contest_stat==nil
208 211
209 212 return contest_time_left == 0
210 213 else
211 214 return false
212 215 end
213 216 end
214 217
215 218 def contest_started?
216 219 if Configuration.contest_mode?
217 220 return true if site==nil
218 221 return site.started
219 222 else
220 223 return true
221 224 end
222 225 end
223 226
227 + # For Code Jom
228 + def update_codejom_status
229 + status = codejom_status || CodejomStatus.new(:user => self)
230 + problem_count = Problem.available_problem_count
231 + status.num_problems_passed = (self.submission_statuses.find_all {|s| s.passed}).length
232 + status.alive = (problem_count - (status.num_problems_passed)) <= CODEJOM_MAX_ALIVE_LEVEL
233 + status.save
234 + end
235 +
236 + def codejom_level
237 + problem_count = Problem.available_problem_count
238 + if codejom_status!=nil
239 + return problem_count - codejom_status.num_problems_passed
240 + else
241 + return problem_count
242 + end
243 + end
244 +
224 245 protected
225 246 def encrypt_new_password
226 247 return if password.blank?
227 248 self.salt = (10+rand(90)).to_s
228 249 self.hashed_password = User.encrypt(self.password,self.salt)
229 250 end
230 251
231 252 def assign_default_site
232 253 # have to catch error when migrating (because self.site is not available).
233 254 begin
234 255 if self.site==nil
235 256 self.site = Site.find_by_name('default')
236 257 if self.site==nil
237 258 self.site = Site.find(1) # when 'default has be renamed'
238 259 end
239 260 end
240 261 rescue
241 262 end
242 263 end
243 264
244 265 def password_required?
245 266 self.hashed_password.blank? || !self.password.blank?
246 267 end
247 268
248 269 def self.encrypt(string,salt)
249 270 Digest::SHA1.hexdigest(salt + string)
250 271 end
251 272
252 273 def uniqueness_of_email_from_activated_users
253 274 user = User.activated_users.find_by_email(self.email)
254 275 if user and (user.login != self.login)
255 276 self.errors.add_to_base("Email has already been taken")
256 277 end
257 278 end
258 279
259 280 def enough_time_interval_between_same_email_registrations
260 281 return if !self.new_record?
261 282 return if self.activated
262 283 open_user = User.find_by_email(self.email,
263 284 :order => 'created_at DESC')
264 285 if open_user and open_user.created_at and
265 286 (open_user.created_at > Time.now.gmtime - 5.minutes)
266 287 self.errors.add_to_base("There are already unactivated registrations with this e-mail address (please wait for 5 minutes)")
267 288 end
268 289 end
269 290
270 291 def email_validation?
271 292 begin
@@ -58,48 +58,52
58 58
59 59 end
60 60
61 61 # Add new inflection rules using the following format
62 62 # (all these examples are active by default):
63 63 # Inflector.inflections do |inflect|
64 64 # inflect.plural /^(ox)$/i, '\1en'
65 65 # inflect.singular /^(ox)en/i, '\1'
66 66 # inflect.irregular 'person', 'people'
67 67 # inflect.uncountable %w( fish sheep )
68 68 # end
69 69
70 70 # Add new mime types for use in respond_to blocks:
71 71 # Mime::Type.register "text/richtext", :rtf
72 72 # Mime::Type.register "application/x-mobile", :mobile
73 73
74 74 # Include your application configuration below
75 75
76 76 # If you want to manage graders through web interface, set the path to
77 77 # the grader directory below. This dir is where raw, ev, ev-exam,
78 78 # scripts reside. All grader scripts will be in
79 79 # #{GRADER_ROOT_DIR}/scripts.
80 80 GRADER_ROOT_DIR = ''
81 81
82 82 # These are where inputs and outputs of test requests are stored
83 83 TEST_REQUEST_INPUT_FILE_DIR = RAILS_ROOT + '/data/test_request/input'
84 84 TEST_REQUEST_OUTPUT_FILE_DIR = RAILS_ROOT + '/data/test_request/output'
85 85
86 86 # To use ANALYSIS MODE, provide the testcases/testruns breakdown,
87 87 # and the directory of the grading result (usually in judge's dir).
88 88 TASK_GRADING_INFO_FILENAME = RAILS_ROOT + '/config/tasks.yml'
89 89
90 90 # TODO: change this to where results are kept.
91 91 GRADING_RESULT_DIR = 'RESULT-DIR'
92 92
93 93 # Change this to allow importing testdata into database as test-pairs.
94 94 # This is mainly for Code Jom contest.
95 95 ALLOW_TEST_PAIR_IMPORT = false
96 96
97 97 # Uncomment so that the system validates user e-mails
98 98 # VALIDATE_USER_EMAILS = true
99 99
100 100 # Uncomment so that Apache X-Sendfile is used when delivering files
101 101 # (e.g., in /tasks/view).
102 102 # USE_APACHE_XSENDFILE = true
103 103
104 104 # Uncomment so that configuration is read only once when the server is loaded
105 105 # Configuration.enable_caching
106 +
107 + # OPTIONS FOR CODE JOM
108 + # --------------------
109 + CODEJOM_MAX_ALIVE_LEVEL = 10
@@ -1,72 +1,80
1 1 # This file is auto-generated from the current state of the database. Instead of editing this file,
2 2 # please use the migrations feature of Active Record to incrementally modify your database, and
3 3 # then regenerate this schema definition.
4 4 #
5 5 # Note that this schema.rb definition is the authoritative source for your database schema. If you need
6 6 # to create the application database on another system, you should be using db:schema:load, not running
7 7 # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
8 8 # you'll amass, the slower it'll run and the greater likelihood for issues).
9 9 #
10 10 # It's strongly recommended to check this file into your version control system.
11 11
12 - ActiveRecord::Schema.define(:version => 20100124054458) do
12 + ActiveRecord::Schema.define(:version => 20100124191250) do
13 13
14 14 create_table "announcements", :force => true do |t|
15 15 t.string "author"
16 16 t.text "body"
17 17 t.boolean "published"
18 18 t.datetime "created_at"
19 19 t.datetime "updated_at"
20 20 t.boolean "frontpage", :default => false
21 21 t.boolean "contest_only", :default => false
22 22 t.string "title"
23 23 end
24 24
25 + create_table "codejom_statuses", :force => true do |t|
26 + t.integer "user_id"
27 + t.boolean "alive"
28 + t.integer "num_problems_passed"
29 + t.datetime "created_at"
30 + t.datetime "updated_at"
31 + end
32 +
25 33 create_table "configurations", :force => true do |t|
26 34 t.string "key"
27 35 t.string "value_type"
28 36 t.string "value"
29 37 t.datetime "created_at"
30 38 t.datetime "updated_at"
31 39 t.text "description"
32 40 end
33 41
34 42 create_table "countries", :force => true do |t|
35 43 t.string "name"
36 44 t.datetime "created_at"
37 45 t.datetime "updated_at"
38 46 end
39 47
40 48 create_table "descriptions", :force => true do |t|
41 49 t.text "body"
42 50 t.boolean "markdowned"
43 51 t.datetime "created_at"
44 52 t.datetime "updated_at"
45 53 end
46 54
47 55 create_table "grader_processes", :force => true do |t|
48 56 t.string "host", :limit => 20
49 57 t.integer "pid"
50 58 t.string "mode"
51 59 t.boolean "active"
52 60 t.datetime "created_at"
53 61 t.datetime "updated_at"
54 62 t.integer "task_id"
55 63 t.string "task_type"
56 64 t.boolean "terminated"
57 65 end
58 66
59 67 add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid"
60 68
61 69 create_table "languages", :force => true do |t|
62 70 t.string "name", :limit => 10
63 71 t.string "pretty_name"
64 72 t.string "ext", :limit => 10
65 73 t.string "common_ext"
66 74 end
67 75
68 76 create_table "messages", :force => true do |t|
69 77 t.integer "sender_id"
70 78 t.integer "receiver_id"
71 79 t.integer "replying_message_id"
72 80 t.text "body"
You need to be logged in to leave comments. Login now