2015-05-19 17:12:43 +08:00
class StudentWorkController < ApplicationController
layout " base_courses "
2015-05-26 18:14:53 +08:00
include StudentWorkHelper
2015-05-29 11:26:39 +08:00
require 'bigdecimal'
2015-07-16 18:13:43 +08:00
require " base64 "
2015-08-28 12:04:49 +08:00
before_filter :find_homework , :only = > [ :new , :index , :create , :student_work_absence_penalty , :absence_penalty_list , :evaluation_list , :program_test ]
2015-05-28 16:10:40 +08:00
before_filter :find_work , :only = > [ :edit , :update , :show , :destroy , :add_score , :praise_student_work ]
before_filter :member_of_course , :only = > [ :index , :new , :create , :show , :add_score , :praise_student_work ]
2015-05-20 17:53:01 +08:00
before_filter :author_of_work , :only = > [ :edit , :update , :destroy ]
2015-06-30 15:30:51 +08:00
before_filter :teacher_of_course , :only = > [ :student_work_absence_penalty , :absence_penalty_list , :evaluation_list ]
2015-07-17 15:53:28 +08:00
protect_from_forgery :except = > :set_program_score
2015-05-19 17:12:43 +08:00
2015-08-28 12:04:49 +08:00
###
def program_test
2015-09-02 21:38:13 +08:00
is_test = params [ :is_test ] == 'true'
2015-09-02 21:44:08 +08:00
resultObj = { status : 0 , results : [ ] , error_msg : '' , time : Time . now . strftime ( '%Y-%m-%d %T' ) }
2015-09-07 23:47:14 +08:00
student_work = find_or_save_student_work ( is_test )
2015-09-02 21:38:13 +08:00
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
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
2015-08-28 12:04:49 +08:00
render :json = > resultObj
end
2015-05-19 17:12:43 +08:00
def index
2015-09-08 09:43:41 +08:00
# 作业消息状态更新
2015-09-02 18:00:36 +08:00
@homework . course_messages . each do | homework_message |
2015-09-08 14:14:49 +08:00
if User . current . id == homework_message . user_id && homework_message . viewed == 0
homework_message . update_attributes ( :viewed = > true ) if homework_message . viewed == 0
2015-09-02 18:00:36 +08:00
end
end
2015-09-08 14:14:49 +08:00
# 作品打分消息状态更新
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
# 作品留言
2015-09-02 18:00:36 +08:00
# 消息end
2015-08-25 16:42:29 +08:00
#设置作业对应的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
2015-09-16 15:51:35 +08:00
##################################################################################################################
@stundet_works = @homework . student_works
2015-05-28 09:03:01 +08:00
@homework_commons = @course . homework_commons . order ( " created_at desc " )
2015-09-16 15:51:35 +08:00
@is_teacher = User . current . allowed_to? ( :as_teacher , @course )
2015-09-17 09:20:35 +08:00
@is_evaluation = @homework . homework_detail_manual . comment_status == 2 && ! @is_teacher #是不是匿评
2015-09-16 15:51:35 +08:00
2015-05-19 17:12:43 +08:00
respond_to do | format |
format . html
2015-05-28 11:07:28 +08:00
format . xls {
send_data ( homework_to_xls ( @stundet_works ) , :type = > " text/excel;charset=utf-8; header=present " ,
2015-05-28 12:02:26 +08:00
:filename = > " #{ @course . teacher . lastname . to_s + @course . teacher . firstname } _ #{ @course . name } _ #{ @course . time . to_s + @course . term } _ #{ @homework . name } #{ l ( :excel_homework_list ) } .xls " )
2015-05-28 11:07:28 +08:00
}
2015-05-19 17:12:43 +08:00
end
end
def new
2015-09-02 22:09:23 +08:00
if @homework . homework_type == 2
redirect_to new_user_commit_homework_users_path ( homework_id : @homework . id )
return
end
2015-09-02 19:41:45 +08:00
@user = User . current
2015-08-28 12:04:49 +08:00
@student_work = @homework . student_works . where ( " user_id = ? " , User . current . id ) . first
if @student_work . nil?
@student_work = StudentWork . new
2015-05-19 17:12:43 +08:00
end
2015-09-02 19:41:45 +08:00
respond_to do | format |
format . html { render :layout = > " new_base_user " }
end
2015-05-19 17:12:43 +08:00
end
def create
2015-05-21 15:52:27 +08:00
if params [ :student_work ]
2015-09-09 00:43:54 +08:00
student_work = StudentWork . find ( params [ :student_work_id ] ) if params [ :student_work_id ]
student_work || = StudentWork . new
student_work . name = params [ :student_work ] [ :name ]
student_work . description = params [ :student_work ] [ :description ]
student_work . project_id = params [ :student_work ] [ :project_id ]
student_work . homework_common_id = @homework . id
student_work . user_id = User . current . id
student_work . save_attachments ( params [ :attachments ] )
2015-09-16 10:26:13 +08:00
render_attachment_warning_if_needed ( student_work )
#提交作品时,计算是否迟交
2015-07-02 16:16:29 +08:00
if Time . parse ( @homework . end_time . to_s ) . strftime ( " %Y-%m-%d " ) < Time . parse ( Time . now . to_s ) . strftime ( " %Y-%m-%d " )
2015-09-17 10:06:04 +08:00
student_work . late_penalty = @homework . late_penalty
2015-09-16 10:26:13 +08:00
else
2015-09-09 00:43:54 +08:00
student_work . late_penalty = 0
2015-07-02 16:16:29 +08:00
end
2015-09-09 00:43:54 +08:00
if student_work . save
2015-05-21 15:52:27 +08:00
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_successful_create )
redirect_to student_work_index_url ( :homework = > @homework . id )
}
end
return
end
end
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_failed_create )
redirect_to new_student_work_url ( :homework = > @homework . id )
}
end
2015-05-19 17:12:43 +08:00
end
def edit
2015-09-02 19:52:44 +08:00
@user = User . current
2015-07-22 10:18:01 +08:00
if ! User . current . admin? && @homework . homework_type == 2 #编程作业不能修改作业
2015-07-08 17:19:11 +08:00
render_403
else
respond_to do | format |
2015-09-02 19:52:44 +08:00
format . html { render :layout = > " new_base_user " }
2015-07-08 17:19:11 +08:00
end
2015-05-28 17:09:38 +08:00
end
2015-05-19 17:12:43 +08:00
end
def update
2015-05-28 17:09:38 +08:00
if params [ :student_work ]
@work . name = params [ :student_work ] [ :name ]
@work . description = params [ :student_work ] [ :description ]
@work . project_id = params [ :student_work ] [ :project ]
@work . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @work )
if @work . save
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_successful_edit )
redirect_to student_work_index_url ( :homework = > @homework . id )
}
end
return
end
end
respond_to do | format |
format . html { redirect_to edit_student_work_url ( @work ) }
end
2015-05-19 17:12:43 +08:00
end
2015-05-20 17:53:01 +08:00
def show
2015-05-27 08:55:33 +08:00
@score = student_work_score @work , User . current
2015-05-28 15:54:42 +08:00
@is_teacher = User . current . allowed_to? ( :as_teacher , @course )
2015-05-21 17:17:54 +08:00
respond_to do | format |
format . js
end
2015-05-20 17:53:01 +08:00
end
def destroy
2015-07-08 17:19:11 +08:00
if @homework . homework_type == 2 #编程作业,作品提交后不可以删除
render_403
elsif @work . destroy
2015-05-28 17:18:37 +08:00
respond_to do | format |
format . html {
redirect_to student_work_index_url ( :homework = > @homework . id )
}
end
end
2015-05-20 17:53:01 +08:00
end
2015-05-27 11:25:32 +08:00
#添加评分,已评分则为修改评分
2015-05-22 14:45:01 +08:00
def add_score
2015-05-29 14:06:16 +08:00
render_403 and return if User . current == @work . user #不可以匿评自己的作品
2015-05-29 10:33:29 +08:00
@is_teacher = User . current . allowed_to? ( :as_teacher , @course )
2015-05-29 14:06:16 +08:00
#老师、教辅可以随时评分,学生只能在匿评作业的匿评阶段进行评分
2015-09-16 15:51:35 +08:00
render_403 and return unless @is_teacher || @homework . homework_detail_manual . comment_status == 2
2015-05-29 14:06:16 +08:00
@score = student_work_score @work , User . current
2015-05-27 16:01:00 +08:00
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 ]
2015-07-15 17:34:34 +08:00
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
2015-05-27 16:01:00 +08:00
@is_new = false
2015-05-26 18:14:53 +08:00
else
2015-05-27 16:01:00 +08:00
@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
2015-07-15 17:34:34 +08:00
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
2015-05-27 16:01:00 +08:00
@is_new = true
2015-05-26 18:14:53 +08:00
end
2015-05-26 17:51:56 +08:00
2015-05-27 16:01:00 +08:00
@score . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @score )
2015-05-26 18:14:53 +08:00
2015-05-27 16:01:00 +08:00
if @score . save
2015-05-27 17:32:14 +08:00
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
if @work . save
respond_to do | format |
format . js
end
2015-05-26 18:14:53 +08:00
end
2015-05-23 11:24:24 +08:00
end
end
2015-05-22 14:45:01 +08:00
2015-05-27 11:25:32 +08:00
#添加评分的回复
2015-05-23 11:24:24 +08:00
def add_score_reply
2015-05-27 15:11:11 +08:00
@score = StudentWorksScore . find params [ :score_id ]
2015-05-27 14:58:55 +08:00
@jour = @score . journals_for_messages . new ( :user_id = > User . current . id , :notes = > params [ :message ] , :reply_id = > 0 )
if @jour . save
2015-05-29 11:00:35 +08:00
@status = 1
else
@status = 2
end
respond_to do | format |
format . js
2015-05-23 11:24:24 +08:00
end
2015-05-22 14:45:01 +08:00
end
2015-05-27 11:25:32 +08:00
#删除评分的回复
def destroy_score_reply
2015-05-27 15:11:11 +08:00
@jour = JournalsForMessage . find params [ :jour_id ]
if @jour . destroy
respond_to do | format |
format . js
end
2015-05-27 11:25:32 +08:00
end
end
2015-05-28 16:10:40 +08:00
#为作品点赞
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
2015-06-30 15:30:51 +08:00
#缺评列表显示
2015-06-18 17:56:28 +08:00
def student_work_absence_penalty
2015-06-19 09:56:39 +08:00
order = params [ :order ] || " desc "
2015-06-19 16:23:28 +08:00
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(
2015-06-19 09:56:39 +08:00
SELECT * ,
2015-07-03 11:49:02 +08:00
( SELECT COUNT ( * ) FROM ` student_works_evaluation_distributions ` WHERE user_id = student_works . user_id AND student_work_id IN #{work_ids}) AS all_count,
2015-06-19 09:56:39 +08:00
( 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 `
2015-06-19 14:20:00 +08:00
WHERE homework_common_id = #{@homework.id}
2015-06-19 16:23:28 +08:00
) AS table_1
ORDER BY absence #{order}")
end
2015-06-19 09:56:39 +08:00
@order = order == " desc " ? " asc " : " desc "
2015-06-18 17:56:28 +08:00
respond_to do | format |
format . html
end
end
2015-06-30 15:30:51 +08:00
#导出缺评列表
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 * ,
2015-07-03 17:30:08 +08:00
( SELECT COUNT ( * ) FROM ` student_works_evaluation_distributions ` WHERE user_id = student_works . user_id AND student_work_id IN #{work_ids}) AS all_count,
2015-06-30 15:30:51 +08:00
( 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 {
send_data ( absence_penalty_list_xls ( @stundet_works ) , :type = > " text/excel;charset=utf-8; header=present " ,
2015-06-30 16:36:37 +08:00
:filename = > " #{ @course . teacher . lastname . to_s + @course . teacher . firstname } _ #{ @course . name } _ #{ @course . time . to_s + @course . term } _ #{ @homework . name } #{ l ( :excel_absence_list ) } .xls " )
2015-06-30 15:30:51 +08:00
}
end
end
#导出匿评列表
def evaluation_list
2015-06-30 16:36:37 +08:00
respond_to do | format |
format . xls {
send_data ( evaluation_list_xls ( @homework . student_works ) , :type = > " text/excel;charset=utf-8; header=present " ,
:filename = > " #{ @course . teacher . lastname . to_s + @course . teacher . firstname } _ #{ @course . name } _ #{ @course . time . to_s + @course . term } _ #{ @homework . name } #{ l ( :excel_evaluation_list ) } .xls " )
}
end
2015-06-30 15:30:51 +08:00
end
2015-07-10 17:20:04 +08:00
#设置编程作业得分
def set_program_score
2015-07-16 14:26:43 +08:00
stundet_work = StudentWork . find_by_id params [ :student_work_id ]
2015-07-17 15:31:49 +08:00
@course = stundet_work . homework_common . course
2015-07-17 16:12:29 +08:00
student_score_count = 0
2015-07-17 15:31:49 +08:00
if stundet_work && params [ :results ] && params [ :results ] . class . to_s == " Array "
2015-07-16 14:26:43 +08:00
homework_common = stundet_work . homework_common
params [ :results ] . each do | result |
2015-08-01 16:38:58 +08:00
homework_tests = homework_common . homework_tests . where ( " input = ' #{ result [ :input ] } ' AND output = ' #{ result [ :output ] } ' " )
homework_tests . each do | homework_test |
2015-07-17 15:31:49 +08:00
student_work_test = StudentWorkTest . new
student_work_test . student_work = stundet_work
student_work_test . homework_test = homework_test
student_work_test . result = result [ :status ]
2015-07-17 16:12:29 +08:00
if student_work_test . result == 0
student_score_count += 1
end
2015-07-22 10:06:55 +08:00
student_work_test . error_msg = params [ :compile_error_msg ]
2015-07-17 15:49:58 +08:00
student_work_test . save!
2015-07-16 14:26:43 +08:00
end
end
2015-07-17 16:12:29 +08:00
unless homework_common . homework_tests . empty?
stundet_work . student_score = student_score_count * 100 . 0 / homework_common . homework_tests . count
if stundet_work . teacher_score . nil?
if stundet_work . teaching_asistant_score . nil?
stundet_work . final_score = stundet_work . student_score
else
final_ta_score = BigDecimal . new ( " #{ stundet_work . teaching_asistant_score } " ) * BigDecimal . new ( " #{ homework_common . homework_detail_programing . ta_proportion } " )
final_s_score = BigDecimal . new ( " #{ stundet_work . student_score } " ) * ( BigDecimal . new ( '1.0' ) - BigDecimal . new ( " #{ homework_common . homework_detail_programing . ta_proportion } " ) )
final_score = final_ta_score + final_s_score
stundet_work . final_score = format ( " %.1f " , final_score . to_f )
end
end
stundet_work . save!
end
2015-07-10 17:20:04 +08:00
end
end
2015-05-19 17:12:43 +08:00
private
#获取作业
def find_homework
@homework = HomeworkCommon . find params [ :homework ]
@course = @homework . course
rescue
render_404
end
#获取作品
def find_work
@work = StudentWork . find params [ :id ]
2015-05-21 17:33:54 +08:00
@homework = @work . homework_common
@course = @homework . course
2015-05-19 17:12:43 +08:00
rescue
render_404
end
2015-05-20 17:53:01 +08:00
#是不是当前课程的成员
#当前课程成员才可以看到作品列表
def member_of_course
2015-06-25 14:56:53 +08:00
render_403 unless User . current . member_of_course? ( @course ) || User . current . admin?
2015-05-20 17:53:01 +08:00
end
#判断是不是当前作品的提交者
2015-05-28 17:18:37 +08:00
#提交者 && (非匿评作业 || 未开启匿评) 可以编辑作品
2015-05-20 17:53:01 +08:00
def author_of_work
2015-07-22 10:18:01 +08:00
render_403 unless User . current . admin? || ( User . current . id == @work . user_id && @homework . homework_type != 1 || @homework . homework_detail_manual . comment_status == 1 )
2015-05-20 17:53:01 +08:00
end
2015-05-28 09:46:35 +08:00
2015-06-30 15:30:51 +08:00
def teacher_of_course
render_403 unless User . current . allowed_to? ( :as_teacher , @course )
end
2015-05-28 09:46:35 +08:00
#根据条件过滤作业结果
def search_homework_member homeworks , name
2015-06-02 17:42:54 +08:00
name = name . downcase
2015-05-28 09:46:35 +08:00
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 )
}
select_homework
end
2015-05-28 11:07:28 +08:00
2015-06-30 15:30:51 +08:00
#作品列表转换为excel
2015-05-28 11:07:28 +08:00
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
2015-07-23 10:33:54 +08:00
if @homework . homework_type == 0 #普通作业
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_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 , 8 ] = homework . respond_to? ( " score " ) ? homework . score . nil? ? l ( :label_without_score ) : format ( " %.2f " , homework . score ) : l ( :label_without_score )
sheet1 [ count_row , 9 ] = format_time ( homework . created_at )
count_row += 1
end
elsif @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_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
2015-05-28 11:07:28 +08:00
end
book . write xls_report
xls_report . string
end
2015-06-30 15:30:51 +08:00
#缺评列表转换为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 |
2015-06-30 16:36:37 +08:00
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
2015-06-30 15:30:51 +08:00
count_row += 1
end
book . write xls_report
xls_report . string
end
2015-06-30 16:36:37 +08:00
#匿评列表转换为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
2015-08-28 12:04:49 +08:00
2015-09-07 23:47:14 +08:00
def find_or_save_student_work ( is_test )
2015-09-09 00:43:54 +08:00
student_work = StudentWork . where ( homework_common_id : @homework . id , user_id : User . current . id ) . first
2015-08-28 12:04:49 +08:00
if student_work . nil?
@homework . student_works . build (
name : params [ :title ] ,
description : params [ :src ] ,
2015-09-07 23:47:14 +08:00
user_id : User . current . id ,
is_test : is_test
2015-08-28 12:04:49 +08:00
)
unless @homework . save
2015-09-09 00:43:54 +08:00
logger . debug @homework . errors . full_messages
2015-08-28 12:04:49 +08:00
else
2015-09-10 11:34:10 +08:00
student_work = StudentWork . where ( homework_common_id : @homework . id , user_id : User . current . id ) . first
2015-08-28 12:04:49 +08:00
end
end
student_work
end
def test_realtime ( student_work , src )
2015-08-28 15:08:07 +08:00
url = " #{ Redmine :: Configuration [ 'judge_server' ] } api/realtime_test.json "
2015-08-28 12:04:49 +08:00
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
2015-09-17 11:41:24 +08:00
#成绩计算
def set_final_score
homework = HomeworkCommon . first
student_work = StudentWork . first
if homework && homework . homework_detail_manual
if homework . homework_type == 1 #匿评作业
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 } " ) * 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 } " ) * 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 } " ) * 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 } " ) * 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 } " ) * 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 } " ) * 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
2015-05-19 17:12:43 +08:00
end