2016-12-22 10:01:37 +08:00
#encoding: utf-8
class ContestantWorksController < ApplicationController
2016-12-23 21:44:03 +08:00
layout " base_contests "
2016-12-22 10:01:37 +08:00
include ContestantWorksHelper
include ApplicationHelper
require 'bigdecimal'
require " base64 "
before_filter :find_contestwork , :only = > [ :new , :index , :create , :new_student_work_project , :student_work_project , :cancel_relate_project ,
2016-12-23 16:58:50 +08:00
:delete_work , :search_contest_students , :work_canrepeat , :add_group_member , :change_project ]
2016-12-22 10:01:37 +08:00
before_filter :find_work , :only = > [ :edit , :update , :show , :destroy , :add_score , :praise_student_work , :retry_work , :revise_attachment ]
before_filter :member_of_contest , :only = > [ :new , :create , :show ]
before_filter :author_of_work , :only = > [ :edit , :update , :destroy ]
before_filter :admin_of_contest , :only = > [ ]
before_filter :is_logged , :only = > [ :index ]
def new
2016-12-23 16:58:50 +08:00
#更新消息
2016-12-30 20:30:45 +08:00
if @contestwork . work_status == 1
noEvaluation = @contestwork . contest_messages . where ( " user_id =? and viewed =? " , User . current . id , 0 )
noEvaluation . update_all ( :viewed = > true )
2016-12-22 10:01:37 +08:00
2016-12-30 20:30:45 +08:00
@user = User . current
@student_work = ContestantWork . new
respond_to do | format |
format . html { render :layout = > " base_contests " }
end
else
render_403
2016-12-23 16:58:50 +08:00
end
2016-12-22 10:01:37 +08:00
end
def index
2016-12-23 16:58:50 +08:00
# 作业消息状态更新?
work_messages = ContestMessage . where ( :user_id = > User . current . id , :viewed = > 0 , :contest_message_id = > @contestwork . id , :contest_message_type = > " Work " )
work_messages . update_all ( :viewed = > true )
studentwork_messages = ContestMessage . where ( :user_id = > User . current . id , :viewed = > 0 , :contest_id = > @contestwork . contest , :contest_message_type = > " ContestantWork " )
studentwork_messages . update_all ( :viewed = > true )
# 作品打分消息状态更新
#studentworks_scores = ContestMessage.where("user_id =? and contest_id =? and contest_message_type =? and viewed =?", User.current.id, @contestwork.contest, "StudentWorksScore", 0)
#studentworks_scores.update_all(:viewed => true)
# 作品评论消息状态更新
#journals_for_teacher = ContestMessage.where("user_id =? and contest_id =? and contest_message_type =? and viewed =?", User.current.id, @contestwork.contest, "JournalsForMessage", 0)
#journals_for_teacher.update_all(:viewed => true)
##################################################################################################################
@order , @b_sort , @name , @group = params [ :order ] || " score " , params [ :sort ] || " desc " , params [ :name ] . to_s . strip || " " , params [ :group ]
@homework_commons = @contest . works . where ( " publish_time <= ? " , Time . now . strftime ( " %Y-%m-%d " ) ) . order ( " created_at desc " )
@all_homework_commons = @contest . works . order ( " created_at desc " )
@is_teacher = User . current . admin_of_contest? ( @contest ) || User . current . admin?
@is_judge = User . current . judge_of_contest? ( @contest )
2017-01-06 15:00:14 +08:00
@is_evaluation = @is_judge && @contestwork . work_status == 3 && @contestwork . online_evaluation
2016-12-22 10:01:37 +08:00
2016-12-23 16:58:50 +08:00
@show_all = false
2017-01-06 15:00:14 +08:00
is_judge_open = @is_judge && ( ! @contestwork . online_evaluation || ( @contestwork . online_evaluation && @contestwork . work_status == 4 ) )
is_contestant_open = User . current . contestant_of_contest? ( @contest ) && ( ( ! @contestwork . online_evaluation && @contestwork . work_status == 2 && @contestwork . score_open ) || ( @contestwork . online_evaluation && @contestwork . work_status == 4 && @contestwork . score_open ) )
if @is_teacher || is_judge_open || is_contestant_open
2016-12-23 16:58:50 +08:00
# if @order == 'lastname'
# @stundet_works = search_homework_member @homework.student_works.no_copy.select("student_works.*,student_works.work_score as score").joins(:user).order("CONVERT(lastname USING gbk) COLLATE gbk_chinese_ci #{@b_sort}, login #{@b_sort}"),@name
# elsif @order == 'student_id'
# @stundet_works = search_homework_member @homework.student_works.no_copy.select("student_works.*,student_works.work_score as score").joins("join user_extensions on student_works.user_id = user_extensions.user_id").order("#{@order} #{@b_sort}"),@name
# else
@stundet_works = @contestwork . contestant_works . no_copy . select ( " contestant_works.*,contestant_works.work_score as score " ) . includes ( :user = > { :user_extensions = > [ ] } , :project = > { } , :contestant_work_scores = > { } ) . order ( " #{ @order } #{ @b_sort } " )
#end
@show_all = true
2017-01-06 15:00:14 +08:00
elsif @is_evaluation
if @contestwork . work_detail_manual . evaluation_num == - 1
@stundet_works = @contestwork . contestant_works . no_copy . select ( " contestant_works.*,contestant_works.work_score as score " ) . includes ( :user = > { :user_extensions = > [ ] } , :project = > { } , :contestant_work_scores = > { } ) . order ( " #{ @order } #{ @b_sort } " )
else
@stundet_works = User . current . contestant_work_evaluation_distributions . map ( & :contestant_work ) . select { | cwork | cwork . work_id == @contestwork . id }
end
elsif User . current . judge_of_contest? ( @contest )
@stundet_works = [ ]
elsif User . current . contestant_of_contest? ( @contest )
2016-12-23 16:58:50 +08:00
if @contestwork . work_type == 3
pro = @contestwork . contestant_work_projects . where ( :user_id = > User . current . id ) . first
if pro . nil?
@stundet_works = [ ]
else
@stundet_works = @contestwork . contestant_works . select ( " contestant_works.*,contestant_works.work_score as score " ) . where ( :id = > pro . contestant_work_id )
end
else
@stundet_works = @contestwork . contestant_works . select ( " contestant_works.*,contestant_works.work_score as score " ) . where ( :user_id = > User . current . id )
end
else
render_403
return
end
@student_work_count = @contestwork . contestant_works . has_committed . select ( " contestant_works.*,contestant_works.work_score as score " ) . order ( " #{ @order } #{ @b_sort } " ) . count
@score = @b_sort == " desc " ? " asc " : " desc "
@is_focus = params [ :is_focus ] ? params [ :is_focus ] . to_i : 0
# 消息传过来的ID
@message_student_work_id = params [ :student_work_id ]
@left_nav_type = 3
@tab = params [ :tab ] . to_i
respond_to do | format |
format . js
format . html
# format.xls {
# filename = "#{@contest.user.lastname.to_s + @contest.user.firstname}_#{@contest.name}_#{@contestwork.name}#{l(:excel_homework_list)}.xls"
# send_data(homework_to_xls(@stundet_works), :type => "text/excel;charset=utf-8; header=present",
# :filename => filename_for_content_disposition(filename))
# }
end
2016-12-22 10:01:37 +08:00
end
def show
2017-01-06 15:00:14 +08:00
@score = ContestantWorkScore . where ( :user_id = > User . current . id , :contestant_work_id = > @work . id , :reviewer_role = > 2 ) . last
@is_evaluation = User . current . judge_of_contest? ( @contest ) && @contestwork . work_status == 3 && @contestwork . online_evaluation
2016-12-23 21:44:03 +08:00
@is_teacher = User . current . admin_of_contest? ( @contest ) || User . current . judge_of_contest? ( @contest ) || User . current . admin?
2017-01-06 15:00:14 +08:00
if @is_evaluation
2016-12-30 10:30:21 +08:00
@student_work_scores = @work . contestant_work_scores . where ( " user_id = #{ User . current . id } and reviewer_role = 2 " ) . order ( " updated_at desc " )
else
@student_work_scores = contestant_work_score_record ( @work )
end
2016-12-23 21:44:03 +08:00
respond_to do | format |
format . js
end
2016-12-22 10:01:37 +08:00
end
def create
2016-12-23 16:58:50 +08:00
# 提交作品前先判断是否已经提交
@has_commit = false ;
if hsd_committed_work? ( User . current . id , @contestwork . id )
@work = ContestantWork . where ( " user_id =? and work_id =? " , User . current . id , @contestwork . id ) . first
@has_commit = true ;
#flash[:notice] = l(:notice_successful_create)
#redirect_to edit_student_work_url(params[:student_work])
respond_to do | format |
format . js
end
return
end
if params [ :student_work ]
@submit_result = true
student_work = ContestantWork . find ( params [ :student_work_id ] ) if params [ :student_work_id ]
student_work || = ContestantWork . new
student_work . name = params [ :student_work ] [ :name ] == " #{ @contestwork . name } 的作品提交(可修改) " ? " #{ @contestwork . name } 的作品提交 " : params [ :student_work ] [ :name ]
student_work . description = params [ :student_work ] [ :description ]
student_work . work_id = @contestwork . id
student_work . user_id = User . current . id
student_work . commit_time = Time . now
student_work . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( student_work )
if @contestwork . work_type == 3 && @contestwork . work_detail_group . base_on_project
@student_work_project = @contestwork . contestant_work_projects . where ( " user_id = #{ User . current . id } " ) . first
student_work . project_id = @student_work_project . project_id
end
#提交作品时,计算是否迟交
student_work . work_status = Time . parse ( @contestwork . end_time . to_s ) . strftime ( " %Y-%m-%d " ) < Time . parse ( Time . now . to_s ) . strftime ( " %Y-%m-%d " ) ? 2 : 1
if student_work . save
if @contestwork . work_type == 3
if @contestwork . work_detail_group . base_on_project
@student_work_project . contestant_work_id = student_work . id
@student_work_project . save
else
stu_project = ContestantWorkProject . new ( :work_id = > @contestwork . id , :contestant_work_id = > student_work . id , :project_id = > student_work . project_id , :user_id = > student_work . user_id , :is_leader = > 1 , :contest_id = > @contestwork . contest_id )
stu_project . save
end
members = params [ :group_member_ids ] . split ( ',' )
for i in 1 .. members . count - 1
stu_work = ContestantWork . new ( :name = > student_work . name , :description = > student_work . description , :user_id = > members [ i ] . to_i , :work_id = > @contestwork . id , :project_id = > student_work . project_id , :work_status = > 3 , :commit_time = > student_work . commit_time )
if stu_work . save
stu_project = ContestantWorkProject . new ( :work_id = > @contestwork . id , :contestant_work_id = > student_work . id , :project_id = > stu_work . project_id == 0 ? - 1 : stu_work . project_id , :user_id = > members [ i ] . to_i , :is_leader = > 0 , :contest_id = > @contestwork . contest_id )
stu_project . save
end
end
end
@contestwork . update_column ( :updated_at , Time . now )
update_contest_activity ( @contestwork . class , @contestwork . id )
update_user_activity ( @contestwork . class , @contestwork . id )
contest_message = ContestMessage . new ( :user_id = > User . current . id , :content = > " new " , :contest_message_id = > student_work . id , :contest_id = > @contest . id , :contest_message_type = > " ContestantWork " , :status = > 9 ) #作品提交记录
contest_message . save
2016-12-22 10:01:37 +08:00
2016-12-23 16:58:50 +08:00
@student_work = ContestantWork . where ( work_id : @contestwork . id , user_id : User . current . id ) . first
respond_to do | format |
format . js
end
return
end
end
@submit_result = false
respond_to do | format |
format . js
end
2016-12-22 10:01:37 +08:00
end
def edit
2016-12-23 21:44:03 +08:00
@user = User . current
if Time . parse ( @contestwork . end_time . to_s ) . strftime ( " %Y-%m-%d " ) < Time . now . strftime ( " %Y-%m-%d " ) #编程作业不能修改作业|| 截止日期已到不能修改作业
render_403
else
respond_to do | format |
format . html { render :layout = > " base_contests " }
end
end
2016-12-22 10:01:37 +08:00
end
def update
2016-12-23 21:44:03 +08:00
if params [ :student_work ]
@submit_result = true
@work . name = params [ :student_work ] [ :name ]
@work . description = params [ :student_work ] [ :description ]
@work . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @work )
if @work . save
#如果学生作品被打分后修改,应该给老师提示
#send_message_to_teacher(@work)
if @contestwork . work_type == 3
@student_work_project = @contestwork . contestant_work_projects . where ( " user_id=? " , User . current . id ) . first
student_work_projects = @contestwork . contestant_work_projects . where ( " contestant_work_id=? and is_leader =? " , @work . id , 0 )
user_ids = student_work_projects . empty? ? " (-1) " : " ( " + student_work_projects . map { | stu | stu . user_id } . join ( " , " ) + " ) "
student_works = @contestwork . contestant_works . where ( " user_id in #{ user_ids } " )
student_works . delete_all
student_work_projects . delete_all
members = params [ :group_member_ids ] . split ( ',' )
for i in 1 .. members . count - 1
stu_work = ContestantWork . new ( :name = > @work . name , :description = > @work . description , :user_id = > members [ i ] . to_i , :work_id = > @contestwork . id , :project_id = > @work . project_id , :work_status = > 3 , :commit_time = > @work . commit_time )
if stu_work . save
stu_project = ContestantWorkProject . new ( :work_id = > @contestwork . id , :contestant_work_id = > @work . id , :project_id = > stu_work . project_id == 0 ? - 1 : stu_work . project_id , :user_id = > members [ i ] . to_i , :is_leader = > 0 , :contest_id = > @contestwork . contest_id )
stu_project . save
end
end
end
2016-12-22 10:01:37 +08:00
2016-12-23 21:44:03 +08:00
contest_message = ContestMessage . new ( :user_id = > User . current . id , :content = > " edit " , :contest_message_id = > @work . id , :contest_id = > @contest . id , :contest_message_type = > " ContestantWork " , :status = > 9 ) #作品提交记录
contest_message . save
@student_work = @work
respond_to do | format |
format . js
end
return
end
end
= begin
respond_to do | format |
format . html { redirect_to edit_student_work_url ( @work ) }
end
= end
@submit_result = false
respond_to do | format |
format . js
end
2016-12-22 10:01:37 +08:00
end
def destroy
2016-12-23 21:44:03 +08:00
if @contestwork . work_type == 3
pros = @work . contestant_work_projects . where ( " is_leader = 0 " )
user_ids = pros . empty? ? " (-1) " : " ( " + pros . map { | stu | stu . user_id } . join ( " , " ) + " ) "
student_works = @contestwork . contestant_works . where ( " user_id in #{ user_ids } " )
student_works . delete_all
pros . delete_all
2016-12-22 10:01:37 +08:00
2016-12-23 21:44:03 +08:00
project = @work . contestant_work_projects . where ( " is_leader = 1 " ) . first
if @contestwork . work_detail_group . base_on_project
project . update_attributes ( :contestant_work_id = > nil )
else
project . destroy
end
end
@work . destroy
@work . attachments . destroy_all
#@work.student_works_scores.destroy_all
@work . contest_messages . destroy_all
#@work.student_work_tests.destroy_all
respond_to do | format |
format . html {
redirect_to contestant_works_path ( :work = > @contestwork . id )
}
end
2016-12-22 10:01:37 +08:00
end
2016-12-23 16:58:50 +08:00
def delete_work
@work = ContestantWork . where ( " user_id =? and work_id =? " , User . current . id , @contestwork . id ) . first
if @work
@work . destroy
if @contestwork . work_type == 3 && @contestwork . work_detail_group . base_on_project
pros = @work . contestant_work_projects . where ( " is_leader = 0 " )
pros . each do | pro |
pro . destroy
end
project = @work . contestant_work_projects . where ( " is_leader = 1 " ) . first
project . update_attributes ( :contestant_work_id = > nil )
elsif @contestwork . work_type == 3 && ! @contestwork . work_detail_group . base_on_project
@work . contestant_work_projects . each do | pro2 |
pro2 . destroy
end
end
end
redirect_to works_path ( :contest = > @contestwork . contest_id )
end
def retry_work
if @contestwork . work_type == 3
pros = @work . contestant_work_projects . where ( " is_leader = 0 " )
user_ids = pros . empty? ? " (-1) " : " ( " + pros . map { | stu | stu . user_id } . join ( " , " ) + " ) "
student_works = @contestwork . contestant_works . where ( " user_id in #{ user_ids } " )
student_works . delete_all
pros . delete_all
project = @work . contestant_work_projects . where ( " is_leader = 1 " ) . first
if @contestwork . work_detail_group . base_on_project
project . update_attributes ( :contestant_work_id = > nil )
else
project . destroy
end
end
@work . destroy
@work . attachments . destroy_all
@work . contest_messages . destroy_all
@student_work = ContestantWork . new
respond_to do | format |
format . js
end
end
#查找竞赛中的参赛者
def search_contest_students
name = " "
unless params [ :name ] . nil?
name = params [ :name ]
end
if ! @contestwork . work_detail_group . base_on_project
all_student_ids = " ( " + @contestwork . contest . contestants . map { | student | student . student_id } . join ( " , " ) + " ) "
else
pro = Project . find @contestwork . contestant_work_projects . where ( " user_id=? " , User . current . id ) . first . project_id
all_student_ids = " ( " + pro . members . map { | member | member . user_id } . join ( " , " ) + " ) "
end
all_students = User . where ( " id in #{ all_student_ids } " )
student_work_id = @contestwork . contestant_work_projects . where ( " user_id=? and contestant_work_id is not null " , User . current . id ) . first . nil? ? - 1 : @contestwork . contestant_work_projects . where ( " user_id=? " , User . current . id ) . first . contestant_work_id
@commit_student_ids = @contestwork . contestant_works . where ( " id != #{ student_work_id } " ) . map { | student | student . user_id }
@users = searchstudent_by_name all_students , name
respond_to do | format |
format . js
end
end
2016-12-22 10:01:37 +08:00
def new_student_work_project
@user_activity_id = params [ :user_activity_id ] . to_i
@hw_status = params [ :hw_status ] . to_i
respond_to do | format |
format . js
end
end
#创建作业的关联项目
def student_work_project
@project = ContestantWorkProject . new
@project . work_id = @contestwork . id
@project . project_id = params [ :projectName ] . to_i
@project . user_id = User . current . id
@project . is_leader = 1
@project . contest_id = @contestwork . contest_id
if @project . save
@user_activity_id = params [ :user_activity_id ] . to_i
@hw_status = params [ :hw_status ] . to_i
@is_teacher = User . current . admin_of_contest? ( @contestwork . contest ) || User . current . admin?
respond_to do | format |
format . js
end
end
end
# 取消关联项目
def cancel_relate_project
relate_pro = ContestantWorkProject . where ( " user_id = #{ User . current . id } and work_id = #{ @contestwork . id } " ) . first
if relate_pro . destroy
@user_activity_id = params [ :user_activity_id ] . to_i
@hw_status = params [ :hw_status ] . to_i
@is_teacher = User . current . admin_of_contest? ( @contestwork . contest ) || User . current . admin?
respond_to do | format |
format . js
end
end
end
2016-12-30 10:30:21 +08:00
def add_score
@is_last = params [ :is_last ] == " true "
2017-01-06 15:00:14 +08:00
@is_evaluation = User . current . judge_of_contest? ( @contest ) && @contestwork . work_status == 3 && @contestwork . online_evaluation
#@is_teacher = User.current.admin_of_contest?(@contest) || User.current.judge_of_contest?(@contest) || User.current.admin?
2016-12-30 10:30:21 +08:00
#老师、教辅可以随时评分,学生只能在匿评作业的匿评阶段进行评分
2017-01-06 15:00:14 +08:00
render_403 and return unless @is_evaluation
2016-12-30 10:30:21 +08:00
@is_last_a = @work . contestant_work_scores . empty?
@new_score = ContestantWorkScore . new
@new_score . score = params [ :score ] . to_i
@new_score . comment = params [ :new_form ] [ :user_message ] if params [ :new_form ] && params [ :new_form ] [ :user_message ] && params [ :new_form ] [ :user_message ] != " "
@new_score . user_id = User . current . id
@new_score . contestant_work_id = @work . id
@new_score . reviewer_role = 2
# if User.current.admin?
# @new_score.reviewer_role = 1
# else
# role = User.current.members.where("course_id = ?",@course.id).first.roles.where("is_current = 1").first.name
# @new_score.reviewer_role = get_role_by_name(role)
# end
# if @score
# if @is_teacher
# @is_new = true
# @is_last_a = false
# else
# @is_new = false
# end
# else
# @is_new = true
# end
@new_score . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @new_score )
if @new_score . save
@contestwork . update_column ( 'updated_at' , Time . now )
update_contest_activity ( @contestwork . class , @contestwork . id )
update_user_activity ( @contestwork . class , @contestwork . id )
2017-01-06 15:00:14 +08:00
judge_score = ContestantWorkScore . find_by_sql ( " SELECT AVG(score) AS score FROM (SELECT * FROM (SELECT * FROM contestant_work_scores WHERE contestant_work_id = #{ @work . id } AND reviewer_role = 2 ORDER BY created_at DESC) AS t GROUP BY user_id) AS a " )
@work . judge_score = judge_score . first . score . nil? ? nil : judge_score . first . score . try ( :round , 2 ) . to_f
2016-12-30 10:30:21 +08:00
if @work . save
@work = @contestwork . contestant_works . select ( " contestant_works.*,contestant_works.work_score as score " ) . where ( :id = > @work . id ) . first
@count = @contestwork . contestant_works . has_committed . count
@student_work_scores = contestant_work_score_record @work
respond_to do | format |
format . js
end
end
end
end
2016-12-22 10:01:37 +08:00
private
2016-12-23 16:58:50 +08:00
def hsd_committed_work? ( user , homework )
sw = ContestantWork . where ( " user_id =? and work_id =? " , user , homework ) . first
sw . nil? ? result = false : result = true
result
end
2016-12-22 10:01:37 +08:00
#获取题目
def find_contestwork
@contestwork = Work . find params [ :work ]
@contest = @contestwork . contest
rescue
render_404
end
#获取作品
def find_work
@work = ContestantWork . find params [ :id ]
@contestwork = @work . work
@contest = @contestwork . contest
rescue
render_404
end
#是不是当前竞赛的成员
#当前竞赛成员才可以看到作品列表
def member_of_contest
render_403 unless User . current . member_of_contest? ( @contest ) || User . current . admin?
end
#判断是不是当前作品的提交者
#提交者 && (非匿评作业 || 未开启匿评) 可以编辑作品
def author_of_work
render_403 unless User . current . admin? || User . current . id == @work . user_id
end
def admin_of_contest
render_403 unless User . current . admin_of_contest? ( @contest ) || User . current . admin?
end
def is_logged
redirect_to signin_path unless User . current . logged?
end
end