You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/models/subject.rb

119 lines
5.1 KiB

6 years ago
class Subject < ActiveRecord::Base
#status :0 编辑中 1 审核中 2 发布
# belongs_to :user
has_many :users, :through => :subject_members
has_many :subject_members, :dependent => :destroy, :order => "subject_members.position asc"
6 years ago
has_many :career_stages, :through => :career_stage_subjects
has_many :career_stage_subjects, :dependent => :destroy
belongs_to :course_list
belongs_to :major
# score_count 只能适合在首页使用
attr_accessible :description, :name, :status, :visits, :user_id, :course_list_id, :major_id, :learning_notes, :introduction,
5 years ago
:homepage_show, :score_count, :publish_time, :updated_at, :excellent, :student_count, :participant_count
6 years ago
has_many :stages, :dependent => :destroy, :order => "stages.position ASC"
has_many :stage_shixuns, :dependent => :destroy
has_many :shixuns, :through => :stage_shixuns
has_many :tidings, :as => :container, :dependent => :destroy
belongs_to :repertoire
belongs_to :user
belongs_to :subject_level_system
6 years ago
scope :visible, lambda{where(status: 2)}
after_create :send_tiding
def send_tiding
self.tidings << Tiding.new(:user_id => self.user_id, :trigger_user_id => self.user_id, :belong_container_id => self.id, :belong_container_type =>'Subject', :tiding_type => "System", :viewed => 0)
end
def owner
User.find(self.user_id)
rescue ActiveRecord::RecordNotFound
render_404
end
def subject_choices
count = 0
self.stage_shixuns.each do |stage_shixun|
shixun = stage_shixun.shixun
count += shixun.challenges.where(:st => [1, 2]).count
end
count
end
def member_count
self.shixuns.map(&:myshixuns_count).sum
end
6 years ago
def subject_shixuns
count = 0
self.stage_shixuns.each do |stage_shixun|
shixun = stage_shixun.shixun
count += shixun.challenges.where(:st => 0).count
end
count
end
def subject_challenges
count = 0
self.stage_shixuns.each do |stage_shixun|
shixun = stage_shixun.shixun
count += shixun.challenges.count
end
count
end
def subject_users
Myshixun.find_by_sql("select count(*) as user_count from myshixuns where shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})").first.try(:user_count).to_i
# count = 0
# self.stage_shixuns.each do |stage_shixun|
# shixun = stage_shixun.shixun
# count += shixun.myshixuns_count
# end
# count
end
def subject_shixun_score
Challenge.find_by_sql("select sum(score) as shixun_count from challenges where st=0 and shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})").first.try(:shixun_count).to_i
end
def subject_shixun_choose_score
ChallengeChoose.find_by_sql("select sum(score) as choose_score from challenge_chooses where challenge_id in
(select distinct(id) from challenges where st !=0 and shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id}))").first.try(:choose_score).to_i
end
def subject_challenge_count
Challenge.find_by_sql("select count(*) as challenge_count from challenges where st=0 and shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})").first.try(:challenge_count).to_i
end
def subject_challenge_choose_count
Challenge.find_by_sql("select count(*) as challenge_choose_count from challenges where st!=0 and shixun_id in(SELECT distinct shixun_id FROM `stage_shixuns` where subject_id=#{self.id})").first.try(:challenge_choose_count)
end
def subject_score
self.subject_shixun_score.to_i + self.subject_shixun_choose_score.to_i
end
def my_subject_score
shixuns_id = self.stage_shixuns.map(&:shixun_id)
shixuns_id = shixuns_id.present? ? shixuns_id.join(",") : -1
my_shixun_score = Challenge.find_by_sql("select sum(c.score) as score FROM challenges c join games g on g.challenge_id = c.id where g.status = 2 and g.user_id = #{User.current.id} and c.shixun_id in(#{shixuns_id})").first.try(:score)
#my_choose_score = ChallengeChoose.find_by_sql("SELECT sum(g.final_score) score FROM (`challenge_chooses` cc join challenges c on cc.challenge_id = c.id) join games g on g.challenge_id = c.id where g.status = 2 and g.user_id = #{User.current.id} and c.shixun_id in(#{shixuns_id})").first.try(:score)
return my_shixun_score.to_i
end
def my_subject_progress
shixun_id = self.stage_shixuns.map(&:shixun_id)
challenge_id = Game.where(:user_id => User.current.id, :status => 2).pluck(:challenge_id)
my_challenge_count = Challenge.where(:id => challenge_id, :shixun_id => shixun_id).count
count = ((my_challenge_count.to_f / self.subject_challenge_count).round(2) * 100).to_i
end
def my_consume_time
shixuns_id = self.stage_shixuns.map(&:shixun_id)
shixuns_id = shixuns_id.present? ? shixuns_id.join(",") : -1
my_shixun_time = Challenge.find_by_sql("select sum(g.cost_time) as score FROM challenges c join games g on g.challenge_id = c.id where g.status = 2 and g.user_id = #{User.current.id} and c.shixun_id in(#{shixuns_id})").first.try(:score)
return my_shixun_time.to_i
end
end