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,:alert_open_student_works,:open_student_works,:set_score_open,:alert_score_open_modal] before_filter :admin_of_contest, :only => [:new, :create, :edit, :update, :destroy,:score_rule_set,:alert_open_student_works,:open_student_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 : 1 #分组作业 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 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") if homework_end_time != param_end_time 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 #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 @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.html { @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) else redirect_to works_path(:contest => @contest.id) end } 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