Show More
Commit Description:
Merged online-registration branch changes r297:303 into the trunk...
Commit Description:
Merged online-registration branch changes r297:303 into the trunk
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@303 6386c4cd-e34a-4fa8-8920-d93eb39b512e
References:
File last commit:
Show/Diff file:
Action:
app/models/configuration.rb
| 123 lines
| 2.4 KiB
| text/x-ruby
| RubyLexer
|
|
r134 | require 'yaml' | ||
|
r122 | # | ||
# This class also contains various login of the system. | ||||
# | ||||
|
r76 | class Configuration < ActiveRecord::Base | ||
|
r122 | SYSTEM_MODE_CONF_KEY = 'system.mode' | ||
|
r146 | # set @@cache = true to only reload once. | ||
@@cache = false | ||||
|
r76 | @@configurations = nil | ||
|
r134 | @@task_grading_info = nil | ||
|
r76 | |||
def self.get(key) | ||||
|
r146 | if @@cache | ||
if @@configurations == nil | ||||
self.read_config | ||||
end | ||||
return @@configurations[key] | ||||
else | ||||
return Configuration.read_one_key(key) | ||||
|
r76 | end | ||
end | ||||
def self.[](key) | ||||
self.get(key) | ||||
end | ||||
def self.reload | ||||
self.read_config | ||||
end | ||||
def self.clear | ||||
@@configurations = nil | ||||
end | ||||
|
r158 | def self.enable_caching | ||
@@cache = true | ||||
end | ||||
|
r122 | # | ||
# View decision | ||||
# | ||||
def self.show_submitbox_to?(user) | ||||
mode = get(SYSTEM_MODE_CONF_KEY) | ||||
return false if mode=='analysis' | ||||
if (mode=='contest') | ||||
return false if (user.site!=nil) and | ||||
|
r129 | ((user.site.started!=true) or (user.site.finished?)) | ||
|
r122 | end | ||
return true | ||||
end | ||||
def self.show_tasks_to?(user) | ||||
mode = get(SYSTEM_MODE_CONF_KEY) | ||||
if (mode=='contest') | ||||
|
r129 | return false if (user.site!=nil) and (user.site.started!=true) | ||
|
r122 | end | ||
return true | ||||
end | ||||
|
r128 | |||
|
r134 | def self.show_grading_result | ||
return (get(SYSTEM_MODE_CONF_KEY)=='analysis') | ||||
end | ||||
|
r128 | def self.allow_test_request(user) | ||
mode = get(SYSTEM_MODE_CONF_KEY) | ||||
if (mode=='contest') | ||||
|
r129 | return false if (user.site!=nil) and ((user.site.started!=true) or (user.site.time_left < 30.minutes)) | ||
|
r128 | end | ||
return false if mode=='analysis' | ||||
return true | ||||
end | ||||
|
r134 | |||
def self.task_grading_info | ||||
if @@task_grading_info==nil | ||||
read_grading_info | ||||
end | ||||
return @@task_grading_info | ||||
end | ||||
|
r130 | |||
|
r76 | protected | ||
|
r146 | |||
def self.convert_type(val,type) | ||||
case type | ||||
when 'string' | ||||
return val | ||||
when 'integer' | ||||
return val.to_i | ||||
when 'boolean' | ||||
return (val=='true') | ||||
end | ||||
end | ||||
|
r76 | def self.read_config | ||
@@configurations = {} | ||||
Configuration.find(:all).each do |conf| | ||||
key = conf.key | ||||
val = conf.value | ||||
|
r146 | @@configurations[key] = Configuration.convert_type(val,conf.value_type) | ||
end | ||||
end | ||||
|
r76 | |||
|
r146 | def self.read_one_key(key) | ||
conf = Configuration.find_by_key(key) | ||||
|
r155 | if conf | ||
return Configuration.convert_type(conf.value,conf.value_type) | ||||
else | ||||
return nil | ||||
end | ||||
|
r76 | end | ||
|
r134 | |||
def self.read_grading_info | ||||
f = File.open(TASK_GRADING_INFO_FILENAME) | ||||
@@task_grading_info = YAML.load(f) | ||||
f.close | ||||
end | ||||
|
r76 | |||
end | ||||