socialforge/app/controllers/works_controller.rb

304 lines
10 KiB
Ruby

class WorksController < ApplicationController
layout "base_contests"
before_filter :find_contest, :only => [:index,:new,:create]
before_filter :find_contestwork, :only => [:edit,:update,:destroy,:score_rule_set,:open_contestant_works,:set_score_open,:alert_score_open_modal,
:set_score_rule]
before_filter :admin_of_contest, :only => [:new, :create, :edit, :update, :destroy,:score_rule_set,:open_contestant_works]
before_filter :member_of_contest, :only => [:index]
def index
search = "%#{params[:search].to_s.strip.downcase}%"
@new_homework = Work.new
@new_homework.contest = @contest
@page = params[:page] ? params[:page].to_i + 1 : 0
@is_teacher = User.current.logged? && (User.current.admin? || User.current.admin_of_contest?(@contest))
if @is_teacher
@homework_commons = @contest.works.where("name like '%#{search}%'").order("created_at desc")
else
@homework_commons = @contest.works.where("name like '%#{search}%' and publish_time <= '#{Date.today}'").order("created_at desc")
end
@is_new = params[:is_new]
@homeworks = paginateHelper @homework_commons,10
#设置at已读
ids = @homeworks.inject([]) do |ids, homework|
jids = homework.journals_for_messages.map(&:id)
jids ? ids + jids : ids
# homework.delay.set_jour_viewed
end
unless ids.empty?
User.current.at_messages.where(viewed: false,
at_message_type: 'JournalsForMessage',
at_message_id: ids).update_all(viewed: true)
end
@left_nav_type = 3
respond_to do |format|
format.js
format.html
end
end
def show
end
def new
end
def create
if User.current.logged?
if params[:homework_common]
homework = Work.new
homework.name = params[:homework_common][:name]
homework.description = params[:homework_common][:description]
homework.end_time = params[:homework_common][:end_time] || Date.today
if params[:homework_common][:publish_time] == ""
homework.publish_time = Date.today
else
homework.publish_time = params[:homework_common][:publish_time]
end
homework.work_type = params[:homework_type].to_i || 1
#homework.late_penalty = 0
#homework.teacher_priority = 1
homework.user_id = User.current.id
homework.contest_id = @contest.id
homework.save_attachments(params[:attachments])
render_attachment_warning_if_needed(homework)
homework.work_status = homework.publish_time > Date.today ? 0 : (homework.end_time < Date.today ? 2 : 1)
work_detail_manual = WorkDetailManual.new
work_detail_manual.evaluation_start = homework.end_time + 7
work_detail_manual.evaluation_end = work_detail_manual.evaluation_start + 7
work_detail_manual.evaluation_num = 3
homework.work_detail_manual = work_detail_manual
#分组作业
if homework.work_type == 3
homework_detail_group = WorkDetailGroup.new
homework.work_detail_group = homework_detail_group
homework_detail_group.min_num = params[:min_num].to_i
homework_detail_group.max_num = params[:max_num].to_i
homework_detail_group.base_on_project = params[:base_on_project].to_i
end
if homework.save
work_detail_manual.save if work_detail_manual
homework_detail_group.save if homework_detail_group
redirect_to works_path(:contest => @contest.id)
end
end
else
render_403
end
end
def update
if params[:homework_common]
@contestwork.name = params[:homework_common][:name]
@contestwork.description = params[:homework_common][:description]
if params[:homework_common][:publish_time] == ""
@contestwork.publish_time = Date.today
else
@contestwork.publish_time = params[:homework_common][:publish_time]
end
param_end_time = Time.parse(params[:homework_common][:end_time]).strftime("%Y-%m-%d")
homework_end_time = Time.parse(@contestwork.end_time.to_s).strftime("%Y-%m-%d")
work_detail_manual = @contestwork.work_detail_manual
if homework_end_time != param_end_time
eval_start = Time.parse(work_detail_manual.evaluation_start.to_s).strftime("%Y-%m-%d")
if eval_start.nil? || (eval_start <= param_end_time && @contestwork.work_status <= 2)
work_detail_manual.evaluation_start = @contestwork.end_time + 7
work_detail_manual.evaluation_end = work_detail_manual.evaluation_start + 7
end
if homework_end_time > param_end_time
@contestwork.contestant_works.each do |st|
if param_end_time < Time.parse(st.commit_time.to_s).strftime("%Y-%m-%d")
st.work_status = 2
st.save
end
end
else
@contestwork.contestant_works.where("work_status = 2").each do |st|
if param_end_time >= Time.parse(st.commit_time.to_s).strftime("%Y-%m-%d")
st.work_status = 1
st.save
end
end
end
end
@contestwork.end_time = params[:homework_common][:end_time] || Time.now
if params[:homework_type] && params[:homework_type].to_i != @contestwork.work_type
if @contestwork.work_type == 3
@contestwork.work_detail_group.destroy if @contestwork.work_detail_group
end
end
@contestwork.work_type = params[:homework_type].to_i || @contestwork.work_type
#status = false
if @contestwork.publish_time <= Date.today && @contestwork.work_status == 0
@contestwork.work_status = 1
elsif @contestwork.end_time < Date.today && @contestwork.work_status == 1
@contestwork.work_status = 2
#status = true
end
@contestwork.save_attachments(params[:attachments])
render_attachment_warning_if_needed(@contestwork)
#分组作业
if @contestwork.work_type == 3
@contestwork.work_detail_group ||= WorkDetailGroup.new
@homework_detail_group = @contestwork.work_detail_group
@homework_detail_group.min_num = params[:min_num].to_i if params[:min_num]
@homework_detail_group.max_num = params[:max_num].to_i if params[:max_num]
@homework_detail_group.base_on_project = params[:base_on_project] ? 1 : 0
end
if @contestwork.save
@homework_detail_group.save if @homework_detail_group
work_detail_manual.save if work_detail_manual
@hw_status = params[:hw_status].to_i
if @hw_status == 1
redirect_to user_contest_community_path(User.current.id)
elsif @hw_status == 2
redirect_to contest_path(@contest.id)
elsif @hw_status == 5
redirect_to contestant_works_path(:work => @contestwork.id)
else
redirect_to works_path(:contest => @contest.id)
end
end
end
end
def edit
@user = User.current
@hw_status = params[:hw_status].to_i
@homework = @contestwork
if @hw_status != 1
@left_nav_type = 3
respond_to do |format|
format.html{render :layout => 'base_contests'}
end
else
respond_to do |format|
format.html{render :layout => 'base_contest_community'}
end
end
end
def destroy
if @contestwork.destroy
respond_to do |format|
format.js {
@hw_status = params[:hw_status].to_i
}
end
end
end
def alert_score_open_modal
if params[:user_activity_id]
@user_activity_id = params[:user_activity_id]
else
@user_activity_id = -1
end
@hw_status = params[:hw_status].to_i
end
def set_score_open
@contestwork.update_attribute('score_open', params[:score_open].to_i)
@user_activity_id = params[:user_activity_id].to_i
@hw_status = params[:hw_status].to_i
@is_teacher = User.current.admin? || User.current.admin_of_contest?(@contest)
end
def open_contestant_works
@contestwork.update_attributes(:is_open => !@contestwork.is_open)
@user_activity_id = params[:user_activity_id]
@hw_status = params[:hw_status].to_i
@is_teacher = User.current.admin? || User.current.admin_of_contest?(@contest)
end
def score_rule_set
if params[:user_activity_id]
@user_activity_id = params[:user_activity_id]
else
@user_activity_id = -1
end
@hw_status = params[:hw_status].to_i
end
def set_score_rule
# 未启动在线评审时才能更改评审参数
if @contestwork.work_status < 3
@contestwork.online_evaluation = params[:online_evaluation].to_i ? 1 : 0
@contestwork.save
# 如果开启在线评审
if params[:online_evaluation]
work_detail_manual = @contestwork.work_detail_manual
work_detail_manual.evaluation_start = params[:evaluation_start]
work_detail_manual.evaluation_end = params[:evaluation_end]
work_detail_manual.evaluation_num = params[:evaluation_limit].to_i == 1 ? params[:evaluation_num] : -1
work_detail_manual.save
end
end
score_valid = params[:score_valid].to_i == 1 ? true : false
if score_valid != @contestwork.score_valid
@contestwork.score_valid = score_valid
@contestwork.save
@contestwork.contestant_works.has_committed.each do |c_work|
c_work.save
end
end
if params[:student_path] && params[:student_path] == "true"
redirect_to contestant_works_path(:work => @contestwork.id)
else
@user_activity_id = params[:user_activity_id].to_i
@hw_status = params[:hw_status].to_i
respond_to do |format|
format.js
end
end
end
private
#获取竞赛
def find_contest
@contest = Contest.find params[:contest]
rescue
render_404
end
#获取题目
def find_contestwork
@contestwork = Work.find params[:id]
@work_detail_group = @contestwork.work_detail_group
@contest = @contestwork.contest
rescue
render_404
end
#是不是管理员
def admin_of_contest
render_403 unless User.current.admin_of_contest?(@contest) || User.current.admin?
end
#当前用户是不是竞赛的成员
def member_of_contest
render_403 unless @contest.is_public==1 || User.current.member_of_contest?(@contest) || User.current.admin?
end
end