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.
educoder/app/models/poll.rb

156 lines
6.2 KiB

6 years ago
class Poll < ApplicationRecord
belongs_to :course, counter_cache: true
belongs_to :user
belongs_to :exercise_bank, optional: true
# belongs_to :exercise_bank
has_many :poll_questions,dependent: :delete_all
has_many :poll_users, :dependent => :delete_all
6 years ago
has_many :users, :through => :poll_users #该文件被哪些用户提交答案过
has_many :poll_group_settings, :dependent => :delete_all
has_many :course_acts, class_name: 'CourseActivity', as: :course_act, dependent: :delete_all
6 years ago
has_many :tidings, as: :container, dependent: :delete_all
6 years ago
scope :publish_or_not, -> { where("polls_status > ? ",1)}
scope :only_public, -> {where("is_public = ?",true)}
scope :public_or_unset, -> { where("unified_setting = ?",true) }
scope :poll_by_ids, lambda { |ids| where(id: ids) unless ids.blank? }
scope :poll_by_status, lambda { |s| where(polls_status: s) unless s.blank? }
scope :poll_group_ended, -> {where("end_time is NOT NULL AND end_time <= ?",Time.now)}
scope :poll_search, lambda { |keywords|
where("polls_name LIKE ?", "%#{keywords}%") unless keywords.blank?}
validates :polls_name, length: { maximum: 60, too_long: "60 characters is the maximum allowed" }
after_create :create_polls_list
def create_polls_list
str = ""
self.course.students.find_each do |student|
str += "," if str != ""
str += "(#{student.user_id},#{self.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
end
if str != ""
sql = "insert into poll_users (user_id, poll_id, commit_status, created_at, updated_at) values" + str
ActiveRecord::Base.connection.execute sql
end
end
#获取学生视角的试卷用户
def get_poll_exercise_users
if unified_setting #试卷统一设置
poll_users
else
ex_group_setting_ids = poll_group_settings.poll_group_published.pluck(:course_group_id)
course_user_ids = course.students.where(course_group_id:ex_group_setting_ids).pluck(:user_id)
poll_users.where(user_id:course_user_ids)
end
end
# 统一设置,为当前老师有权限的分班学生,分班设置,也为当前老师有权限的分班的学生
def all_poll_users(user_id)
6 years ago
poll_all_users = poll_users
6 years ago
group_ids = poll_published_ids(user_id)
if group_ids.present?
6 years ago
poll_all_users = poll_all_users.where(user_id: course.students.where(course_group_id: group_ids).pluck(:user_id).uniq)
6 years ago
end
6 years ago
poll_all_users
6 years ago
end
#当前用户已发布的班级id和试卷分组已发布的班级id的交集
def poll_published_ids(user_id)
6 years ago
current_user_groups = course.teacher_course_ids(user_id)
6 years ago
if unified_setting
if course.none_group_count > 0 #有未分班的,则发布到未发布
un_group_ids = [0]
else
un_group_ids = []
end
(current_user_groups + un_group_ids).uniq #统一设置时为当前用户的分班id
else
ex_group_setting = poll_group_settings.pluck("course_group_id").uniq
ex_group_setting & current_user_groups #当前用户有权限的已发布的分班id #非统一设置时为当前用户有权限的且已发布分班的id
end
end
def get_poll_status(user_id)
user_group = course.course_members.find_by(user_id: user_id, is_active: 1)
if user_group.present?
if user_group.role == "STUDENT" #为学生
is_teacher = false
else
is_teacher = true
end
ex_time = get_poll_times(user_id,is_teacher)
pb_time = ex_time[:publish_time]
ed_time = ex_time[:end_time]
if pb_time.present? && ed_time.present? && pb_time <= Time.now && ed_time > Time.now
status = 2
elsif ed_time.present? && ed_time <= Time.now
status = 3
else
status = 1
end
else
status = polls_status
end
status
end
#获取问卷的发布时间和截止时间。teacher 为boolean,当为true时表示的是当前为老师
def get_poll_times(user_id,teacher)
if unified_setting
pb_time = publish_time
en_time = end_time
else
poll_group_setting = poll_group_settings
if teacher #当前为老师,为设置组的最大值和最小值
user_group = course.teacher_course_groups.get_user_groups(user_id)
user_group_ids = user_group.present? ? user_group.pluck(:course_group_id) : course.course_groups.pluck(:id)
user_poll_group_settings = poll_group_setting.find_in_poll_group("course_group_id",user_group_ids)
pb_time_min = user_poll_group_settings.publish_time_present.map(&:publish_time)
en_time_max = user_poll_group_settings.end_time_present.map(&:end_time)
pb_time = pb_time_min.size > 0 ? pb_time_min.min : nil
en_time = en_time_max.size > 0 ? en_time_max.max : nil
else
user_group = course.students.find_by(user_id: user_id)
if user_group.present?
user_group_id = user_group.course_group_id
user_p_group_setting = poll_group_setting.find_by(course_group_id: user_group_id)
pb_time = user_p_group_setting.present? ? user_p_group_setting.publish_time : nil
en_time = user_p_group_setting.present? ? user_p_group_setting.end_time : nil
else
pb_time = nil
en_time = nil
end
end
end
{
"publish_time":pb_time,
"end_time":en_time
}
end
#判断当前用户的答题状态
def check_user_votes_status(user)
poll_answer_user = poll_users.find_by(user_id: user.id)
6 years ago
# user_poll_status = get_poll_status(user.id)
6 years ago
user_status = 2
if poll_answer_user.present? && (poll_answer_user.start_at.present? || poll_answer_user.end_at.present?) #学生有过答题的,或者立即截止,但学生未做试卷的
user_status = poll_answer_user.commit_status
end
6 years ago
#
# if poll_answer_user.present? && poll_answer_user.start_at.blank? && user_poll_status == 3
# # user_status = 4
# user_status = 2 #问卷用户存在,且未开始答题,且问卷已截止时,返回未提交标示
# end
6 years ago
user_status
end
end