socialforge/app/controllers/contestant_works_controller.rb

409 lines
18 KiB
Ruby

#encoding: utf-8
class ContestantWorksController < ApplicationController
layout "base_contests"
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,
:delete_work,:search_contest_students,:work_canrepeat,:add_group_member,:change_project]
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
#更新消息
noEvaluation = @contestwork.contest_messages.where("user_id =? and viewed =?", User.current.id, 0)
noEvaluation.update_all(:viewed => true)
@user = User.current
@student_work = ContestantWork.new
respond_to do |format|
format.html{ render :layout => "base_contests"}
end
end
def index
# 作业消息状态更新?
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)
@show_all = false
if @is_teacher || @is_judge
# 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
elsif User.current.member_of_contest?(@contest)
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
end
def show
#@score = student_work_score @work,User.current
@is_teacher = User.current.admin_of_contest?(@contest) || User.current.judge_of_contest?(@contest) || User.current.admin?
# if @homework.homework_detail_manual.comment_status == 2 && !@is_teacher && @work.user != User.current
# @student_work_scores = @work.student_works_scores.where("user_id = #{User.current.id} and reviewer_role = 3").order("updated_at desc")
# else
# @student_work_scores = student_work_score_record(@work)
# end
respond_to do |format|
format.js
end
end
def create
# 提交作品前先判断是否已经提交
@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
@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
end
def edit
@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
end
def update
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
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
end
def destroy
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.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
end
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
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
private
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
#获取题目
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