#encoding: utf-8 class StudentWorkController < ApplicationController layout "base_courses" include StudentWorkHelper include ApplicationHelper require 'bigdecimal' require "base64" before_filter :find_homework, :only => [:new, :index, :create, :student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :program_test,:program_test_ex,:set_score_rule,:forbidden_anonymous_comment,:delete_work,:new_student_work_project,:student_work_project,:cancel_relate_project,:search_course_students] before_filter :find_work, :only => [:edit, :update, :show, :destroy, :add_score, :praise_student_work,:retry_work,:revise_attachment] before_filter :member_of_course, :only => [:new, :create, :show, :add_score, :praise_student_work] before_filter :author_of_work, :only => [:edit, :update, :destroy] before_filter :teacher_of_course, :only => [:student_work_absence_penalty, :absence_penalty_list, :evaluation_list, :set_score_rule, :forbidden_anonymous_comment] before_filter :is_logged, :only => [:index] ### def program_test is_test = params[:is_test] == 'true' resultObj = {status: 0, results: [], error_msg: '', time: Time.now.strftime('%Y-%m-%d %T')} student_work = find_or_save_student_work(is_test) unless student_work resultObj[:status] = 100 else if @homework.homework_type == 2 && @homework.homework_detail_programing result = test_realtime(student_work, params[:src]) logger.debug result resultObj[:status] = result["status"] resultObj[:results] = result["results"] resultObj[:error_msg] = result["error_msg"] results = result["results"] if result["status"].to_i == -2 #编译错误 results = [result["error_msg"]] end student_work_test = student_work.student_work_tests.build(status: result["status"], results: results, src: params[:src]) unless student_work.save resultObj[:status] = 200 else student_work.name = params[:title] student_work.description = params[:src] if Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.parse(Time.now.to_s).strftime("%Y-%m-%d") student_work.late_penalty = @homework.late_penalty else student_work.late_penalty = 0 end student_work.save resultObj[:status] = result["status"].to_i resultObj[:time] = student_work_test.created_at.to_s(:db) resultObj[:index] = student_work.student_work_tests.count end end end render :json => resultObj end #由于负载问题 不要使用全局变量 #根据传入的tIndex确定是第几次测试 def program_test_ex is_test = params[:is_test] == 'true' resultObj = {status: 0, results: [], error_msg: '', time: Time.now.strftime('%Y-%m-%d %T'),tseq:1,tcount:1,testid:1} #保存每测试一次返回的结果 student_work = find_or_save_student_work(is_test) resultObj[:tcount] = @homework.homework_tests.size unless student_work resultObj[:status] = 100 else if @homework.homework_type == 2 && @homework.homework_detail_programing #找到第index个测试的输入输出 index = params[:tIndex].to_i resultObj[:tseq] = index test = @homework.homework_tests[index - 1] #请求测试 result = test_realtime_ex(test, params[:src]) if result["status"].to_i != -2 result["results"].first['output'] = result["results"].first['output'].gsub(" ","□") result["results"].first['result'] = result["results"].first['result'].gsub(" ","□") end logger.debug result #-1 默认值 0全部正确并结束 2 超时 -2 编译错误 resultObj[:status] = -1 resultObj[:results] = result["results"].first #本次测试结果 resultObj[:error_msg] = result["error_msg"] #编译错误时的信息 if result["status"].to_i == -2 #编译错误 resultObj[:results] = result["error_msg"] resultObj[:status] = -2 elsif result["results"][0]["status"].to_i == 2 resultObj[:status] = 2 end unless student_work.save resultObj[:status] = 200 else student_work.name = params[:title] student_work.description = params[:src] if Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.parse(Time.now.to_s).strftime("%Y-%m-%d") student_work.late_penalty = @homework.late_penalty else student_work.late_penalty = 0 end #每次都要把数据存到数据库中 status = result["status"] if index == 1 student_work_test = student_work.student_work_tests.build(status: status, results: [resultObj[:results]],src: params[:src]) student_work_test.save! resultObj[:testid] = student_work_test.id else #先从数据库取出result student_work_test = StudentWorkTest.find(params[:testid]) results = student_work_test.results results << resultObj[:results] student_work_test.results = results student_work_test.status = (result["status"] != 0 ? result["status"] : student_work_test.status) student_work_test.save! status = student_work_test.status resultObj[:testid] = student_work_test.id end #超时或编译错误则直接返回了并存入数据库 if resultObj[:status] == 2 || resultObj[:status] == -2 || index == @homework.homework_tests.size if status == 0 resultObj[:status] = 0 end student_work.save! resultObj[:time] = student_work_test.created_at.to_s(:db) resultObj[:index] = student_work.student_work_tests.count end #渲染返回结果 render :json => resultObj end end end end def index # 作业消息状态更新 @homework.course_messages.each do |homework_message| if User.current.id == homework_message.user_id && homework_message.viewed == 0 homework_message.update_attributes(:viewed => true) if homework_message.viewed == 0 end end #修改作品提示消息更新 student_work_messages = CourseMessage.where("user_id =? and course_id =? and course_message_type =? and viewed =?", User.current.id, @homework.course, StudentWork.name, 0) student_work_messages.each do |message| message.update_attribute(:viewed, true) end # 作品打分消息状态更新 studentworks_scores = CourseMessage.where("user_id =? and course_id =? and course_message_type =? and viewed =?", User.current.id, @homework.course, "StudentWorksScore", 0) studentworks_scores.each do |studentworks_score| studentworks_score.update_attributes(:viewed => true) if studentworks_score.viewed == 0 end # 作品评论消息状态更新 journals_for_teacher = CourseMessage.where("user_id =? and course_id =? and course_message_type =? and viewed =?", User.current.id, @homework.course, "JournalsForMessage", 0) journals_for_teacher.each do |journal_for_teacher| journal_for_teacher.update_attributes(:viewed => true) end #不能参与作业匿评消息状态更新 no_evaluation = CourseMessage.where("user_id =? and course_id =? and course_message_type =? and viewed =? and status =?", User.current.id, @homework.course, "StudentWork", 0, 0) no_evaluation.update_all(:viewed => true) # 作品留言 # 消息end #设置作业对应的forge_messages表的viewed字段 query_student_work = @homework.course_messages query_student_work.each do |query| if User.current.id == query.user_id query.update_attributes(:viewed => true) end end ################################################################################################################## @order,@b_sort,@name,@group = params[:order] || "score",params[:sort] || "desc",params[:name] || "",params[:group] @homework_commons = @course.homework_commons.where("publish_time <= ?",Time.now.strftime("%Y-%m-%d")).order("created_at desc") @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin? @is_evaluation = @homework.homework_detail_manual && @homework.homework_detail_manual.comment_status == 2 && !@is_teacher #是不是匿评 @show_all = false course_group = CourseGroup.find_by_id(@group) if @group if course_group group_students = course_group.users if group_students.empty? student_in_group = '(0)' else student_in_group = '(' + group_students.map{|user| user.id}.join(',') + ')' end #开放作品 || 老师 || 超级管理员 || 禁用匿评&&作业截止&&已提交作品 显示所有列表 if @homework.is_open == 1 || @is_teacher || User.current.admin? || (User.current.member_of_course?(@course) && @homework.anonymous_comment == 1 && Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.now.strftime("%Y-%m-%d") && !@homework.student_works.where(:user_id => User.current.id).empty?) if @order == 'lastname' @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins(:user).where("users.id in #{student_in_group}").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.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins(:user).where("users.id in #{student_in_group}").joins("join user_extensions on student_works.user_id = user_extensions.user_id").order("#{@order} #{@b_sort}"),@name else @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins(:user).where("users.id in #{student_in_group}").order("#{@order} #{@b_sort}"),@name end @show_all = true elsif User.current.member_of_course?(@course) if @homework.homework_detail_manual.comment_status == 1 #学生 && 未开启匿评 只看到自己的 if @homework.homework_type == 3 pro = @homework.student_work_projects.where(:user_id => User.current.id).first if pro.nil? @stundet_works = [] else @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").where(:id => pro.student_work_id) end else @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").where(:user_id => User.current.id) end elsif @homework.homework_detail_manual.comment_status == 2 #学生 && 开启匿评 看到匿评列表 if @homework.homework_type == 3 pro = @homework.student_work_projects.where(:user_id => User.current.id).first if pro.nil? my_work = [] else my_work = @homework.student_works.where(:id => pro.student_work_id) end else my_work = @homework.student_works.where(:user_id => User.current.id) end @stundet_works = my_work + User.current.student_works_evaluation_distributions.map(&:student_work).select { |work| work.homework_common_id == @homework.id} elsif @homework.homework_detail_manual.comment_status == 3 #学生 && 关闭匿评 未提交作品之前列表为空,提交了作品看到所有的 if @homework.homework_type == 3 pro = @homework.student_work_projects.where(:user_id => User.current.id).first if pro.nil? my_work = [] else my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").where(:id => pro.student_work_id) end else my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").where(:user_id => User.current.id) end if my_work.empty? @stundet_works = [] else if @order == 'lastname' @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins(:user).where("users.id in #{student_in_group}").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.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins(:user).where("users.id in #{student_in_group}").joins("join user_extensions on student_works.user_id = user_extensions.user_id").order("#{@order} #{@b_sort}"),@name else @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins(:user).where("users.id in #{student_in_group}").order("#{@order} #{@b_sort}"),@name end @show_all = true end else @stundet_works = [] end else render_403 return end @student_work_count = (search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins(:user).where("users.id in #{student_in_group}").order("#{@order} #{@b_sort}"),@name).count else if @homework.is_open == 1 || @is_teacher || User.current.admin? || (User.current.member_of_course?(@course) && @homework.anonymous_comment == 1 && Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.now.strftime("%Y-%m-%d") && !@homework.student_works.where(:user_id => User.current.id).empty?) if @order == 'lastname' @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) 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.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins("join user_extensions on student_works.user_id = user_extensions.user_id").order("#{@order} #{@b_sort}"),@name else @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").order("#{@order} #{@b_sort}"),@name end @show_all = true elsif User.current.member_of_course?(@course) if @homework.homework_detail_manual.comment_status == 1 #学生 && 未开启匿评 只看到自己的 if @homework.homework_type == 3 pro = @homework.student_work_projects.where(:user_id => User.current.id).first if pro.nil? @stundet_works = [] else @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").where(:id => pro.student_work_id) end else @stundet_works = @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").where(:user_id => User.current.id) end elsif @homework.homework_detail_manual.comment_status == 2 #学生 && 开启匿评 看到匿评列表 if @homework.homework_type == 3 pro = @homework.student_work_projects.where(:user_id => User.current.id).first if pro.nil? my_work = [] else my_work = @homework.student_works.where(:id => pro.student_work_id) end else my_work = @homework.student_works.where(:user_id => User.current.id) end @stundet_works = my_work + User.current.student_works_evaluation_distributions.map(&:student_work).select { |work| work.homework_common_id == @homework.id} elsif @homework.homework_detail_manual.comment_status == 3 #学生 && 关闭匿评 未提交作品之前列表为空,提交了作品看到所有的 if @homework.homework_type == 3 pro = @homework.student_work_projects.where(:user_id => User.current.id).first if pro.nil? my_work = [] else my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").where(:id => pro.student_work_id) end else my_work = @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").where(:user_id => User.current.id) end if my_work.empty? @stundet_works = [] else if @order == 'lastname' @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) 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.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").joins("join user_extensions on student_works.user_id = user_extensions.user_id").order("#{@order} #{@b_sort}"),@name else @stundet_works = search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").order("#{@order} #{@b_sort}"),@name end @show_all = true end else @stundet_works = [] end else render_403 return end @student_work_count = (search_homework_member @homework.student_works.select("student_works.*,IF(final_score is null,null,IF(final_score = 0, 0, final_score - absence_penalty - late_penalty)) as score").order("#{@order} #{@b_sort}"),@name).count end @score = @b_sort == "desc" ? "asc" : "desc" # 消息传过来的ID @message_student_work_id = params[:student_work_id] respond_to do |format| format.js format.html format.xls { filename = "#{@course.teacher.lastname.to_s + @course.teacher.firstname}_#{@course.name}_#{@course.time.to_s + @course.term}_#{@homework.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 new #更新消息 noEvaluation = @homework.course_messages.where("user_id =? and viewed =?", User.current.id, 0) noEvaluation.update_all(:viewed => true) if @homework.homework_type==2 redirect_to new_user_commit_homework_users_path(homework_id: @homework.id) return end @user = User.current @student_work = @homework.student_works.where("user_id = ?",User.current.id).first if @student_work.nil? @student_work = StudentWork.new end respond_to do |format| format.html{ render :layout => "new_base_user"} end end def create # 提交作品前先判断是否已经提交 @has_commit = false; if hsd_committed_work?(User.current.id, @homework.id) @work = StudentWork.where("user_id =? and homework_common_id =?", User.current.id, @homework.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 = StudentWork.find(params[:student_work_id]) if params[:student_work_id] student_work ||= StudentWork.new student_work.name = params[:student_work][:name] == "#{@homework.name}的作品提交(可修改)" ? "#{@homework.name}的作品提交" : params[:student_work][:name] student_work.description = params[:student_work][:description] student_work.homework_common_id = @homework.id student_work.user_id = User.current.id student_work.save_attachments(params[:attachments]) render_attachment_warning_if_needed(student_work) if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 @student_work_project = @homework.student_work_projects.where("user_id = #{User.current.id}").first student_work.project_id = @student_work_project.project_id end #提交作品时,计算是否迟交 if Time.parse(@homework.end_time.to_s).strftime("%Y-%m-%d") < Time.parse(Time.now.to_s).strftime("%Y-%m-%d") student_work.late_penalty = @homework.late_penalty else student_work.late_penalty = 0 end if student_work.save if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 @student_work_project.student_work_id = student_work.id @student_work_project.save members = params[:group_member_ids].split(',') for i in 1 .. members.count-1 stu_project = StudentWorkProject.new stu_project.homework_common_id = @homework.id stu_project.student_work_id = student_work.id stu_project.project_id = @student_work_project.project_id stu_project.user_id = members[i].to_i stu_project.is_leader = 0 stu_project.save end elsif @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 0 members = params[:group_member_ids].split(',') for i in 0 .. members.count-1 stu_project = StudentWorkProject.new stu_project.homework_common_id = @homework.id stu_project.student_work_id = student_work.id stu_project.project_id = -1 stu_project.user_id = members[i].to_i if i == 0 stu_project.is_leader = 1 else stu_project.is_leader = 0 end stu_project.save end end @homework.update_attributes(:updated_at => Time.now) update_course_activity(@homework.class,@homework.id) update_user_activity(@homework.class,@homework.id) update_org_activity(@homework.class,@homework.id) course_message = CourseMessage.new(:user_id =>User.current.id,:content=>"new",:course_message_id=>student_work.id,:course_id => @course.id,:course_message_type=>"StudentWork", :status => 9) #作品提交记录 course_message.save @student_work = StudentWork.where(homework_common_id: @homework.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 (!User.current.admin? && @homework.homework_type == 2) || Time.parse(@homework.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 => "new_base_user"} 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 @homework.homework_type == 3 @student_work_project = @homework.student_work_projects.where("user_id=?",User.current.id).first student_work_projects = @homework.student_work_projects.where("student_work_id=? and is_leader =?",@work.id,0) student_work_projects.delete_all members = params[:group_member_ids].split(',') for i in 1 .. members.count-1 stu_project = StudentWorkProject.new stu_project.homework_common_id = @homework.id stu_project.student_work_id = @work.id if @homework.homework_detail_group.base_on_project == 1 stu_project.project_id = @student_work_project.project_id else @homework.homework_detail_group.base_on_project == 0 stu_project.project_id = -1 end stu_project.user_id = members[i].to_i stu_project.is_leader = 0 stu_project.save end end course_message = CourseMessage.new(:user_id =>User.current.id,:content=>"edit",:course_message_id=>@work.id,:course_id => @course.id,:course_message_type=>"StudentWork", :status => 9) #作品提交记录 course_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 show @score = student_work_score @work,User.current @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin? @student_work_scores = @work.student_works_scores.order("updated_at desc") respond_to do |format| format.js end end def destroy if @work.destroy if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 pros = @work.student_work_projects.where("is_leader = 0") pros.each do |pro| pro.destroy end project = @work.student_work_projects.where("is_leader = 1").first project.update_attributes(:student_work_id => nil) elsif @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 0 @work.student_work_projects.each do |pro2| pro2.destroy end end respond_to do |format| format.html { redirect_to student_work_index_url(:homework => @homework.id) } end end end def delete_work @work = StudentWork.where("user_id =? and homework_common_id =?", User.current.id, @homework.id).first if @work @work.destroy if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 pros = @work.student_work_projects.where("is_leader = 0") pros.each do |pro| pro.destroy end project = @work.student_work_projects.where("is_leader = 1").first project.update_attributes(:student_work_id => nil) elsif @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 0 @work.student_work_projects.each do |pro2| pro2.destroy end end end redirect_to user_homeworks_user_path(User.current.id) end def retry_work if @work.destroy if @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 1 pros = @work.student_work_projects.where("is_leader = 0") pros.each do |pro| pro.destroy end project = @work.student_work_projects.where("is_leader = 1").first project.update_attributes(:student_work_id => nil) elsif @homework.homework_type == 3 && @homework.homework_detail_group.base_on_project == 0 @work.student_work_projects.each do |pro2| pro2.destroy end end @student_work = StudentWork.new respond_to do |format| format.js end end end #添加评分,已评分则为修改评分 def add_score @is_last = params[:is_last] == "true" render_403 and return if User.current == @work.user #不可以匿评自己的作品 @is_teacher = User.current.allowed_to?(:as_teacher,@course) || User.current.admin? #老师、教辅可以随时评分,学生只能在匿评作业的匿评阶段进行评分 render_403 and return unless @is_teacher || @homework.homework_detail_manual.comment_status == 2 @score = student_work_score @work,User.current if @score @score.comment = params[:new_form][:user_message] if params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "" @score.score = params[:score] if params[:score] if User.current.admin? @score.reviewer_role = 1 else role = User.current.members.where("course_id = ?",@course.id).first.roles.first.name @score.reviewer_role = get_role_by_name(role) end @is_new = false else @is_last_a = @work.student_works_scores.empty? @score = StudentWorksScore.new @score.score = params[:score] if params[:score] @score.comment = params[:new_form][:user_message] if params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "" @score.user_id = User.current.id @score.student_work_id = @work.id if User.current.admin? @score.reviewer_role = 1 else role = User.current.members.where("course_id = ?",@course.id).first.roles.first.name @score.reviewer_role = get_role_by_name(role) end @is_new = true end @score.save_attachments(params[:attachments]) render_attachment_warning_if_needed(@score) if @score.save case @score.reviewer_role when 1 #教师评分:最后一个教师评分为最终评分 @work.teacher_score = @score.score when 2 #教辅评分 教辅评分显示平均分 @work.teaching_asistant_score = @work.student_works_scores.where(:reviewer_role => 2).average(:score).try(:round, 2).to_f when 3 #学生评分 学生评分显示平均分 @work.student_score = @work.student_works_scores.where(:reviewer_role => 3).average(:score).try(:round, 2).to_f end @homework.update_attributes(:updated_at => Time.now) update_course_activity(@homework.class,@homework.id) update_user_activity(@homework.class,@homework.id) update_org_activity(@homework.class,@homework.id) if @work.save respond_to do |format| format.js end end end end #添加评分的回复 def add_score_reply @score = StudentWorksScore.find params[:score_id] @is_last = params[:is_last] == "true" @jour = @score.journals_for_messages.new(:user_id => User.current.id,:notes =>params[:message], :reply_id => 0) if @jour.save @status = 1 else @status = 2 end respond_to do |format| format.js end end #删除评分的回复 def destroy_score_reply @jour = JournalsForMessage.find params[:jour_id] if @jour.destroy respond_to do |format| format.js end end end #为作品点赞 def praise_student_work pt = PraiseTread.new pt.user_id = User.current.id pt.praise_tread_object_id = @work.id pt.praise_tread_object_type = "StudentWork" pt.praise_or_tread = 1 if pt.save respond_to do |format| format.js end else render_404 end end #缺评列表显示 def student_work_absence_penalty order = params[:order] || "desc" if @homework.student_works.empty? @stundet_works = [] else work_ids = "(" + @homework.student_works.map(&:id).join(",") + ")" @stundet_works = StudentWork.find_by_sql("SELECT *,(all_count - has_count) AS absence FROM( SELECT * , (SELECT COUNT(*) FROM `student_works_evaluation_distributions` WHERE user_id = student_works.user_id AND student_work_id IN #{work_ids}) AS all_count, (SELECT COUNT(*) FROM `student_works_scores` WHERE user_id = student_works.user_id AND student_work_id IN #{work_ids}) AS has_count FROM `student_works` WHERE homework_common_id = #{@homework.id} ) AS table_1 ORDER BY absence #{order}") end @order = order == "desc" ? "asc" : "desc" respond_to do |format| format.html end end #导出缺评列表 def absence_penalty_list if @homework.student_works.empty? @stundet_works = [] else work_ids = "(" + @homework.student_works.map(&:id).join(",") + ")" @stundet_works = StudentWork.find_by_sql("SELECT * FROM (SELECT *,(all_count - has_count) AS absence FROM( SELECT * , (SELECT COUNT(*) FROM `student_works_evaluation_distributions` WHERE user_id = student_works.user_id AND student_work_id IN #{work_ids}) AS all_count, (SELECT COUNT(*) FROM `student_works_scores` WHERE user_id = student_works.user_id AND student_work_id IN #{work_ids}) AS has_count FROM `student_works` WHERE homework_common_id = #{@homework.id} ) AS table_1) AS table_2 where absence > 0 order by absence") end respond_to do |format| format.xls { filename = "#{@course.teacher.lastname.to_s + @course.teacher.firstname}_#{@course.name}_#{@course.time.to_s + @course.term}_#{@homework.name}#{l(:excel_absence_list)}.xls" send_data(absence_penalty_list_xls(@stundet_works), :type => "text/excel;charset=utf-8; header=present", :filename => filename_for_content_disposition(filename)) } end end #导出匿评列表 def evaluation_list respond_to do |format| format.xls { filename = "#{@course.teacher.lastname.to_s + @course.teacher.firstname}_#{@course.name}_#{@course.time.to_s + @course.term}_#{@homework.name}#{l(:excel_evaluation_list)}.xls" send_data(evaluation_list_xls(@homework.student_works), :type => "text/excel;charset=utf-8; header=present", :filename => filename_for_content_disposition(filename)) } end end #设置评分规则 def set_score_rule homework_detail_manual = @homework.homework_detail_manual homework_detail_programing = @homework.homework_detail_programing unless @homework.late_penalty.to_s == params[:late_penalty].to_s @homework.late_penalty = params[:late_penalty] @homework.student_works.where("created_at > '#{@homework.end_time} 23:59:59'").each do |student_work| student_work.late_penalty = @homework.late_penalty student_work.save end @homework.save end unless homework_detail_manual.absence_penalty.to_s == params[:absence_penalty].to_s homework_detail_manual.absence_penalty = params[:absence_penalty] if homework_detail_manual.comment_status == 3 #当前作业处于匿评结束状态,修改缺评扣分才会修改每个作品应扣分的值 work_ids = "(" + @homework.student_works.map(&:id).join(",") + ")" @homework.student_works.each do |student_work| absence_penalty_count = student_work.user.student_works_evaluation_distributions.where("student_work_id IN #{work_ids}").count - student_work.user.student_works_scores.where("student_work_id IN #{work_ids}").count student_work.absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * homework_detail_manual.absence_penalty : 0 student_work.save end end homework_detail_manual.save if homework_detail_manual end teacher_priority = params[:teacher_priority] ? 1 : 0 if homework_detail_manual.ta_proportion.to_s != params[:ta_proportion].to_s || @homework.teacher_priority.to_s != teacher_priority.to_s || (homework_detail_programing && homework_detail_programing.ta_proportion.to_s != params[:sy_proportion].to_s) homework_detail_manual.ta_proportion = params[:ta_proportion] homework_detail_programing.ta_proportion = params[:sy_proportion] if homework_detail_programing @homework.teacher_priority = teacher_priority @homework.save homework_detail_manual.save if homework_detail_manual homework_detail_programing.save if homework_detail_programing @homework.student_works.each do |student_work| set_final_score @homework,student_work student_work.save end end if params[:student_path] redirect_to student_work_index_url(:homework => @homework.id) else @user_activity_id = params[:user_activity_id].to_i @is_in_course = params[:is_in_course].to_i @course_activity = params[:course_activity].to_i respond_to do |format| format.js end end end def forbidden_anonymous_comment @homework.update_column('anonymous_comment', 1) homework_detail_manual = @homework.homework_detail_manual homework_detail_programing = @homework.homework_detail_programing if homework_detail_programing homework_detail_manual.update_column('ta_proportion', 0.4) homework_detail_programing.update_column('ta_proportion', 0.6) else homework_detail_manual.update_column('ta_proportion', 1.0) end @homework.student_works.each do |student_work| set_final_score @homework,student_work student_work.save end @user_activity_id = params[:user_activity_id].to_i @is_in_course = params[:is_in_course].to_i @course_activity = params[:course_activity].to_i end def revise_attachment Attachment.attach_filesex(@work, params[:attachments], params[:attachment_type]) revise_attachments = @work.attachments.where("attachtype = 7").reorder("created_on desc") if revise_attachments.count == 2 revise_attachments.last.destroy end attachment = @work.attachments.where("attachtype = 7").first attachment.update_attributes(:description => params[:description]) respond_to do |format| format.js end end def new_student_work_project @user_activity_id = params[:user_activity_id].to_i @is_in_course = params[:is_in_course].to_i @course_activity = params[:course_activity].to_i respond_to do |format| format.js end end #创建作业的关联项目 def student_work_project @project = StudentWorkProject.new @project.homework_common_id = @homework.id @project.project_id = (Project.find params[:projectName].to_i).id @project.user_id = User.current.id @project.is_leader = 1 if @project.save @user_activity_id = params[:user_activity_id].to_i @is_in_course = params[:is_in_course].to_i @course_activity = params[:course_activity].to_i respond_to do |format| format.js end end end #查找学生创建的项目列表 def search_user_projects condition = '%%' if !params[:name].nil? condition = "%#{params[:name].strip}%".gsub(" ","") end @project_ids = Project.visible.where("user_id = #{User.current.id} and name like '#{condition}'") @first = params[:first].to_i respond_to do |format| format.js end end #查找课程的学生 def search_course_students name = "" unless params[:name].nil? name = params[:name] end if @homework.homework_detail_group.base_on_project == 0 all_student_ids = "(" + @homework.course.student.map{|student| student.student_id}.join(",") + ")" else pro = Project.find @homework.student_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}") @commit_student_ids = @homework.student_work_projects.map{|student| student.user_id} @users = searchstudent_by_name all_students,name respond_to do |format| format.js end end def cancel_relate_project relate_pro = StudentWorkProject.where("user_id = #{User.current.id} and homework_common_id = #{@homework.id}").first if relate_pro.destroy @user_activity_id = params[:user_activity_id].to_i @is_in_course = params[:is_in_course].to_i @course_activity = params[:course_activity].to_i respond_to do |format| format.js end end end private def searchstudent_by_name users, name mems = [] if name != "" name = name.to_s.downcase users.each do |m| username = m.lastname.to_s.downcase + m.firstname.to_s.downcase if(m.login.to_s.downcase.include?(name) || m.user_extensions[:student_id].to_s.downcase.include?(name) || username.include?(name)) mems << m end end else mems = users end mems end def hsd_committed_work?(user, homework) sw = StudentWork.where("user_id =? and homework_common_id =?", user, homework).first sw.nil? ? result = false : result = true result end #获取作业 def find_homework @homework = HomeworkCommon.find params[:homework] @course = @homework.course rescue render_404 end #获取作品 def find_work @work = StudentWork.find params[:id] @homework = @work.homework_common @course = @homework.course rescue render_404 end #是不是当前课程的成员 #当前课程成员才可以看到作品列表 def member_of_course render_403 unless User.current.member_of_course?(@course) || User.current.admin? end #判断是不是当前作品的提交者 #提交者 && (非匿评作业 || 未开启匿评) 可以编辑作品 def author_of_work render_403 unless User.current.admin? || (User.current.id == @work.user_id && @homework.homework_type != 1 || @homework.homework_detail_manual.comment_status == 1 ) end def teacher_of_course render_403 unless User.current.allowed_to?(:as_teacher,@course) || User.current.admin? end def is_logged redirect_to signin_path unless User.current.logged? end #根据条件过滤作业结果 def search_homework_member homeworks,name if name == "" select_homework = homeworks else name = name.downcase select_homework = homeworks.select{ |homework| homework.user[:login].to_s.downcase.include?(name) || homework.user.user_extensions[:student_id].to_s.downcase.include?(name) || (homework.user[:lastname].to_s.downcase + homework.user[:firstname].to_s.downcase).include?(name) } end select_homework end #作品列表转换为excel def homework_to_xls items xls_report = StringIO.new book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet :name => "homework" blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 sheet1.row(0).default_format = blue if @homework.homework_type == 1 #匿评作业 sheet1.row(0).concat([l(:excel_user_id),l(:excel_user_name),l(:excel_nickname),l(:excel_student_id),l(:excel_mail),l(:excel_homework_name), l(:excel_t_score),l(:excel_ta_score), l(:excel_n_score),l(:excel_f_score),l(:excel_commit_time)]) count_row = 1 items.each do |homework| sheet1[count_row,0]=homework.user.id sheet1[count_row,1] = homework.user.lastname.to_s + homework.user.firstname.to_s sheet1[count_row,2] = homework.user.login sheet1[count_row,3] = homework.user.user_extensions.student_id sheet1[count_row,4] = homework.user.mail sheet1[count_row,5] = homework.name sheet1[count_row,6] = homework.teacher_score.nil? ? l(:label_without_score) : format("%.2f",homework.teacher_score) sheet1[count_row,7] = homework.teaching_asistant_score.nil? ? l(:label_without_score) : format("%.2f",homework.teaching_asistant_score) sheet1[count_row,8] = homework.student_score.nil? ? l(:label_without_score) : format("%.2f",homework.student_score) sheet1[count_row,9] = homework.respond_to?("score") ? homework.score.nil? ? l(:label_without_score) : format("%.2f",homework.score) : l(:label_without_score) sheet1[count_row,10] = format_time(homework.created_at) count_row += 1 end elsif @homework.homework_type == 2 #编程作业 sheet1.row(0).concat([l(:excel_user_id),l(:excel_user_name),l(:excel_nickname),l(:excel_student_id),l(:excel_mail),l(:excel_homework_name), l(:excel_t_score),l(:excel_ta_score), l(:excel_s_score),l(:excel_n_score),l(:excel_f_score),l(:excel_commit_time)]) count_row = 1 items.each do |homework| sheet1[count_row,0]=homework.user.id sheet1[count_row,1] = homework.user.lastname.to_s + homework.user.firstname.to_s sheet1[count_row,2] = homework.user.login sheet1[count_row,3] = homework.user.user_extensions.student_id sheet1[count_row,4] = homework.user.mail sheet1[count_row,5] = homework.name sheet1[count_row,6] = homework.teacher_score.nil? ? l(:label_without_score) : format("%.2f",homework.teacher_score) sheet1[count_row,7] = homework.teaching_asistant_score.nil? ? l(:label_without_score) : format("%.2f",homework.teaching_asistant_score) sheet1[count_row,8] = homework.system_score.nil? ? l(:label_without_score) : format("%.2f",homework.system_score) sheet1[count_row,9] = homework.student_score.nil? ? l(:label_without_score) : format("%.2f",homework.student_score) sheet1[count_row,10] = homework.respond_to?("score") ? homework.score.nil? ? l(:label_without_score) : format("%.2f",homework.score) : l(:label_without_score) sheet1[count_row,11] = format_time(homework.created_at) count_row += 1 end end book.write xls_report xls_report.string end #缺评列表转换为excel def absence_penalty_list_xls items xls_report = StringIO.new book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet :name => "homework" blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 sheet1.row(0).default_format = blue sheet1.row(0).concat([l(:excel_student_id),l(:excel_nickname),l(:excel_user_name),l(:lable_all_penalty),l(:lable_has_penalty),l(:lable_absence_penalty)]) count_row = 1 items.each do |homework| sheet1[count_row,0] = homework.user.user_extensions.student_id sheet1[count_row,1] = homework.user.login sheet1[count_row,2] = homework.user.lastname.to_s + homework.user.firstname.to_s sheet1[count_row,3] = homework.all_count sheet1[count_row,4] = homework.has_count sheet1[count_row,5] = homework.absence count_row += 1 end book.write xls_report xls_report.string end #匿评列表转换为excel def evaluation_list_xls items xls_report = StringIO.new book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet :name => "homework" blue = Spreadsheet::Format.new :color => :blue, :weight => :bold, :size => 10 sheet1.row(0).default_format = blue sheet1.row(0).concat([l(:label_work_name),l(:label_work_id),l(:label_work_autor),l(:label_evaluation_id),l(:label_evaluation_name), l(:label_evaluation_score),l(:label_evaluation_common),l(:label_evaluation_time)]) count_row = 1 items.each do |homework| homework.student_works_scores.where(:reviewer_role => 3).each do |score| sheet1[count_row,0] = homework.name sheet1[count_row,1] = homework.user.user_extensions.student_id sheet1[count_row,2] = homework.user.show_name sheet1[count_row,3] = score.user.user_extensions.student_id sheet1[count_row,4] = score.user.show_name sheet1[count_row,5] = score.score sheet1[count_row,6] = score.comment sheet1[count_row,7] = format_time(score.created_at) count_row += 1 end end book.write xls_report xls_report.string end def find_or_save_student_work(is_test) student_work = StudentWork.where(homework_common_id: @homework.id, user_id: User.current.id).first if student_work.nil? @homework.student_works.build( name: params[:title], description: params[:src], user_id: User.current.id, is_test: is_test ) unless @homework.save logger.debug @homework.errors.full_messages else student_work = StudentWork.where(homework_common_id: @homework.id, user_id: User.current.id).first end end student_work end def test_realtime(student_work, src) url = "#{Redmine::Configuration['judge_server']}api/realtime_test.json" factor = [] @homework.homework_tests.each do |test| factor << {input: test.input, output: test.output} end solutions = { src:src, language:@homework.homework_detail_programing.language, factor: factor } uri = URI(url) body = solutions.to_json res = Net::HTTP.new(uri.host, uri.port).start do |client| request = Net::HTTP::Post.new(uri.path) request.body = body request["Content-Type"] = "application/json" client.request(request) end JSON.parse(res.body) end def test_realtime_ex(test, src) url = "#{Redmine::Configuration['judge_server']}api/realtime_test.json" factor = [] factor << {input: test.input, output: test.output} solutions = { src:src, language:@homework.homework_detail_programing.language, factor: factor } uri = URI(url) body = solutions.to_json res = Net::HTTP.new(uri.host, uri.port).start do |client| request = Net::HTTP::Post.new(uri.path) request.body = body request["Content-Type"] = "application/json" client.request(request) end JSON.parse(res.body) end #成绩计算 def set_final_score homework,student_work if homework && homework.homework_detail_manual if homework.homework_type != 2 #匿评作业 if homework.teacher_priority == 1 #教师优先 if student_work.teacher_score student_work.final_score = student_work.teacher_score else if student_work.teaching_asistant_score.nil? student_work.final_score = student_work.student_score elsif student_work.student_score.nil? student_work.final_score = student_work.teaching_asistant_score else ta_proportion = homework.homework_detail_manual.ta_proportion final_ta_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{ta_proportion}") final_s_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{ta_proportion}")) final_score = final_ta_score + final_s_score student_work.final_score = format("%.2f",final_score.to_f) end end else #不考虑教师评分 if student_work.teaching_asistant_score.nil? student_work.final_score = student_work.student_score elsif student_work.student_score.nil? student_work.final_score = student_work.teaching_asistant_score else ta_proportion = homework.homework_detail_manual.ta_proportion final_ta_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{ta_proportion}") final_s_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{ta_proportion}")) final_score = final_ta_score + final_s_score student_work.final_score = format("%.2f",final_score.to_f) end end elsif homework.homework_type == 2 && homework.homework_detail_programing #编程作业-----设定:系统评分必定不为空 if homework.teacher_priority == 1 #教师优先 if student_work.teacher_score student_work.final_score = student_work.teacher_score else if student_work.teaching_asistant_score.nil? #教辅未评分 if student_work.student_score.nil? student_work.final_score = student_work.system_score else ta_proportion = homework.homework_detail_programing.ta_proportion + homework.homework_detail_manual.ta_proportion / 2 final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{ta_proportion}") final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{ta_proportion}")) final_score = final_sy_score + final_st_score student_work.final_score = format("%.2f",final_score.to_f) end elsif student_work.student_score.nil? #学生未评分 if student_work.teaching_asistant_score.nil? student_work.final_score = student_work.system_score else ta_proportion = homework.homework_detail_programing.ta_proportion + (1.0 - homework.homework_detail_manual.ta_proportion - homework.homework_detail_programing.ta_proportion) / 2 final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{ta_proportion}") final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{ta_proportion}")) final_score = final_sy_score + final_ts_score student_work.final_score = format("%.2f",final_score.to_f) end else final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{homework.homework_detail_programing.ta_proportion}") final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{homework.homework_detail_manual.ta_proportion}") final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{homework.homework_detail_programing.ta_proportion}") - BigDecimal.new("#{homework.homework_detail_manual.ta_proportion}")) final_score = final_sy_score + final_ts_score + final_st_score student_work.final_score = format("%.2f",final_score.to_f) end end else #不考虑教师评分 if student_work.teaching_asistant_score.nil? #教辅未评分 if student_work.student_score.nil? student_work.final_score = student_work.system_score else ta_proportion = homework.homework_detail_programing.ta_proportion + homework.homework_detail_manual.ta_proportion / 2 final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{ta_proportion}") final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{ta_proportion}")) final_score = final_sy_score + final_st_score student_work.final_score = format("%.2f",final_score.to_f) end elsif student_work.student_score.nil? #学生未评分 if student_work.teaching_asistant_score.nil? student_work.final_score = student_work.system_score else ta_proportion = homework.homework_detail_programing.ta_proportion + (1.0 - homework.homework_detail_manual.ta_proportion - homework.homework_detail_programing.ta_proportion) / 2 final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{ta_proportion}") final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{ta_proportion}")) final_score = final_sy_score + final_ts_score student_work.final_score = format("%.2f",final_score.to_f) end else final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{homework.homework_detail_programing.ta_proportion}") final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{homework.homework_detail_manual.ta_proportion}") final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{homework.homework_detail_programing.ta_proportion}") - BigDecimal.new("#{homework.homework_detail_manual.ta_proportion}")) final_score = final_sy_score + final_ts_score + final_st_score student_work.final_score = format("%.2f",final_score.to_f) end end end end end end