2013-09-06 16:40:07 +08:00
class Course < ActiveRecord :: Base
include Redmine :: SafeAttributes
2014-05-29 16:35:34 +08:00
STATUS_ACTIVE = 1
STATUS_CLOSED = 5
STATUS_ARCHIVED = 9
2013-09-06 16:40:07 +08:00
2013-09-28 23:08:24 +08:00
attr_accessible :code , :extra , :name , :state , :tea_id , :time , :location , :state , :term , :password
2014-05-29 16:35:34 +08:00
belongs_to :project , :class_name = > 'Course' , :foreign_key = > :extra , primary_key : :identifier
2013-09-14 17:25:57 +08:00
belongs_to :teacher , :class_name = > 'User' , :foreign_key = > :tea_id # 定义一个方法teacher, 该方法通过tea_id来调用User表
2014-03-20 16:19:16 +08:00
belongs_to :school , :class_name = > 'School' , :foreign_key = > :school_id #定义一个方法school, 该方法通过school_id来调用School表
2013-09-06 16:40:07 +08:00
has_many :bid
2013-09-28 23:08:24 +08:00
validates_presence_of :password , :term
2014-05-09 17:13:23 +08:00
validates_format_of :class_period , :message = > " class period can only digital! " , :with = > / ^[1-9] \ d*$ /
2013-09-06 16:40:07 +08:00
safe_attributes 'extra' ,
'time' ,
'name' ,
'extra' ,
2013-09-12 10:41:15 +08:00
'code' ,
2013-09-12 17:25:40 +08:00
'location' ,
'tea_id' ,
2013-09-28 23:08:24 +08:00
'password' ,
2013-09-29 19:46:59 +08:00
'term' ,
'password'
2014-05-09 17:13:23 +08:00
2014-05-29 16:35:34 +08:00
scope :active , lambda { where ( :status = > STATUS_ACTIVE ) }
scope :status , lambda { | arg | where ( arg . blank? ? nil : { :status = > arg . to_i } ) }
scope :all_public , lambda { where ( :is_public = > true ) }
scope :visible , lambda { | * args | where ( Course . visible_condition ( args . shift || User . current , * args ) ) }
scope :allowed_to , lambda { | * args |
user = User . current
permission = nil
if args . first . is_a? ( Symbol )
permission = args . shift
else
user = args . shift
permission = args . shift
end
where ( Course . allowed_to_condition ( user , permission , * args ) )
}
2014-06-03 11:16:06 +08:00
def visible? ( user = User . current )
user . allowed_to? ( :view_course , self )
end
def self . visible_condition ( user , options = { } )
allowed_to_condition ( user , :view_course , options )
end
2014-05-29 16:35:34 +08:00
#自定义验证
2014-05-09 17:13:23 +08:00
def validate
if ! class_period . match ( [ 0 - 9 ] )
errors . add_to_base ( " class period can only digital " )
end
end
2014-02-15 16:34:09 +08:00
def get_endup_time
begin
end_time = Time . parse ( self . endup_time )
rescue Exception = > e
end_time = Time . parse ( " 3000-01-01 " )
Rails . logger . error " [Error] course endup_time error. ===> #{ e } "
ensure
return end_time
end
end
2013-09-06 16:40:07 +08:00
2014-02-22 09:44:14 +08:00
def get_time
begin
time = Date . new ( self . time ) . to_time
rescue Exception = > e
time = Time . parse ( " 3000-01-01 " )
Rails . logger . error " [Error] course time error. ===> #{ e } "
ensure
return time
end
end
2014-05-29 16:35:34 +08:00
def self . allowed_to_condition ( user , permission , options = { } )
perm = Redmine :: AccessControl . permission ( permission )
base_statement = ( perm && perm . read? ? " #{ Course . table_name } .status <> #{ Course :: STATUS_ARCHIVED } " : " #{ Course . table_name } .status = #{ Course :: STATUS_ACTIVE } " )
if perm && perm . project_module
# If the permission belongs to a project module, make sure the module is enabled
base_statement << " AND #{ Course . table_name } .id IN (SELECT em.project_id FROM #{ EnabledModule . table_name } em WHERE em.name=' #{ perm . project_module } ') "
end
if options [ :project ]
project_statement = " #{ Course . table_name } .id = #{ options [ :project ] . id } "
project_statement << " OR ( #{ Course . table_name } .lft > #{ options [ :project ] . lft } AND #{ Course . table_name } .rgt < #{ options [ :project ] . rgt } ) " if options [ :with_subprojects ]
base_statement = " ( #{ project_statement } ) AND ( #{ base_statement } ) "
end
if user . admin?
base_statement
else
statement_by_role = { }
unless options [ :member ]
role = user . logged? ? Role . non_member : Role . anonymous
if role . allowed_to? ( permission )
statement_by_role [ role ] = " #{ Course . table_name } .is_public = #{ connection . quoted_true } "
end
end
if user . logged?
user . projects_by_role . each do | role , projects |
if role . allowed_to? ( permission ) && projects . any?
statement_by_role [ role ] = " #{ Course . table_name } .id IN ( #{ projects . collect ( & :id ) . join ( ',' ) } ) "
end
end
end
if statement_by_role . empty?
" 1=0 "
else
if block_given?
statement_by_role . each do | role , statement |
if s = yield ( role , user )
statement_by_role [ role ] = " ( #{ statement } AND ( #{ s } )) "
end
end
end
" (( #{ base_statement } ) AND ( #{ statement_by_role . values . join ( ' OR ' ) } )) "
end
end
end
2013-09-06 16:40:07 +08:00
end