2016-03-18 10:56:35 +08:00
#encoding: utf-8
2015-05-19 17:12:43 +08:00
class StudentWorkController < ApplicationController
layout " base_courses "
2015-05-26 18:14:53 +08:00
include StudentWorkHelper
2016-01-14 16:58:40 +08:00
include ApplicationHelper
2015-05-29 11:26:39 +08:00
require 'bigdecimal'
2015-07-16 18:13:43 +08:00
require " base64 "
2016-05-03 11:24:16 +08:00
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 , :work_canrepeat ]
2015-12-04 16:21:11 +08:00
before_filter :find_work , :only = > [ :edit , :update , :show , :destroy , :add_score , :praise_student_work , :retry_work , :revise_attachment ]
2016-02-25 16:18:24 +08:00
before_filter :member_of_course , :only = > [ :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-11-04 10:54:09 +08:00
before_filter :teacher_of_course , :only = > [ :student_work_absence_penalty , :absence_penalty_list , :evaluation_list , :set_score_rule , :forbidden_anonymous_comment ]
2016-02-25 16:18:24 +08:00
before_filter :is_logged , :only = > [ :index ]
2015-05-19 17:12:43 +08:00
2016-03-25 14:43:51 +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
2016-05-03 11:24:16 +08:00
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
2015-09-02 21:38:13 +08:00
else
2016-05-03 11:24:16 +08:00
student_work . late_penalty = 0
2015-09-02 21:38:13 +08:00
end
2016-05-03 11:24:16 +08:00
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
2015-09-02 21:38:13 +08:00
end
end
2016-05-03 11:24:16 +08:00
end
2015-08-28 12:04:49 +08:00
render :json = > resultObj
end
2016-03-29 09:02:54 +08:00
#行尾空格替换成□
def space_replace_1 ( str )
for i in 0 .. str . size
tChar = str [ i ]
if tChar != ' ' && tChar != " \n "
sFlag = false
eFlag = false
elsif tChar == ' ' && sFlag == false
tStart = i
sFlag = true
elsif tChar == " \n "
tEnd = i - 1
if sFlag == true
for j in tStart .. tEnd
str [ j ] = " □ "
end
sFlag = false
end
end
end
end
2016-03-26 13:44:13 +08:00
#由于负载问题 不要使用全局变量
2016-03-25 14:43:51 +08:00
#根据传入的tIndex确定是第几次测试
2016-03-29 09:02:54 +08:00
#之后如果觉得很卡 可以改成将结果传回JS再以参数形式传回来
2016-03-25 14:43:51 +08:00
def program_test_ex
2016-05-16 16:57:18 +08:00
2016-05-03 11:24:16 +08:00
tStarttime = Time . now
2016-03-25 14:43:51 +08:00
is_test = params [ :is_test ] == 'true'
2016-03-26 13:44:13 +08:00
resultObj = { status : 0 , results : [ ] , error_msg : '' , time : Time . now . strftime ( '%Y-%m-%d %T' ) , tseq : 1 , tcount : 1 , testid : 1 } #保存每测试一次返回的结果
2016-03-25 14:43:51 +08:00
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
2016-05-03 11:24:16 +08:00
#找到第index个测试的输入输出
index = params [ :tIndex ] . to_i
resultObj [ :tseq ] = index
test = @homework . homework_tests [ index - 1 ]
2016-03-25 14:43:51 +08:00
2016-05-03 11:24:16 +08:00
#请求测试
begin
2016-03-25 14:43:51 +08:00
result = test_realtime_ex ( test , params [ :src ] )
2016-05-03 11:24:16 +08:00
rescue Timeout :: Error
tEndtime = Time . now
tUsedtime = ( tEndtime . to_i - tStarttime . to_i ) * 1000 + ( tEndtime . usec - tStarttime . usec ) / 1000
2016-09-26 09:44:47 +08:00
logger . info " program_test_ex user wait time = #{ tUsedtime } 毫秒 "
2016-05-16 16:57:18 +08:00
#status 0:答案正确 -5program_test_ex 函数出错 -4judge代码出错 -3http超时 -2:编译错误 -1:答案错误 2:程序运行超时
resultObj [ :status ] = - 3
2016-05-03 11:24:16 +08:00
CodeTests . create ( :homework_id = > @homework . id , :language = > @homework . homework_detail_programing . language , :status = > - 3 , :wait_time = > tUsedtime , :student_work_id = > student_work . id )
2016-07-12 16:35:06 +08:00
rescue = > e
2016-05-16 16:57:18 +08:00
#-4 judge代码 出错
2016-09-26 09:44:47 +08:00
logger . info " program_test_error #{ e } "
2016-05-16 16:57:18 +08:00
resultObj [ :status ] = - 4
tmpstatus = - 4
CodeTests . create ( :homework_id = > @homework . id , :language = > @homework . homework_detail_programing . language , :status = > tmpstatus , :time_used = > 0 , :wait_time = > 0 , :student_work_id = > student_work . id )
2016-05-03 11:24:16 +08:00
end
2016-03-25 14:43:51 +08:00
2016-05-16 16:57:18 +08:00
begin
if resultObj [ :status ] != - 3 && resultObj [ :status ] != - 4
if result [ " status " ] . to_i != - 2
#result["results"].first['output'] = result["results"].first['output'].gsub(" ","□")
#result["results"].first['result'] = result["results"].first['result'].gsub(" ","□")
space_replace_1 ( result [ " results " ] . first [ 'output' ] )
space_replace_1 ( result [ " results " ] . first [ 'result' ] )
2016-05-03 11:24:16 +08:00
end
2016-03-25 14:43:51 +08:00
2016-05-16 16:57:18 +08:00
logger . debug result
2016-05-03 11:24:16 +08:00
2016-05-16 16:57:18 +08:00
#-1 默认值 0全部正确并结束 2 超时 -2 编译错误
resultObj [ :status ] = - 1
resultObj [ :results ] = result [ " results " ] . first #本次测试结果
2016-05-16 17:02:18 +08:00
if result [ " status " ] . to_i == - 2 #编译错误
2016-05-16 16:57:18 +08:00
result [ " error_msg " ] = result [ " error_msg " ] [ 0 .. 2047 ]
2016-05-16 17:02:18 +08:00
end
2016-05-16 16:57:18 +08:00
resultObj [ :error_msg ] = result [ " error_msg " ] #编译错误时的信息
#该状态用于存入CodeTests
tmpstatus = - 1
if result [ " status " ] . to_i == - 2 #编译错误
resultObj [ :results ] = result [ " error_msg " ]
resultObj [ :status ] = - 2
tmpstatus = - 2
elsif result [ " results " ] [ 0 ] [ " status " ] . to_i == 2
resultObj [ :status ] = 2
tmpstatus = 2
2016-05-03 11:24:16 +08:00
end
2016-05-16 16:57:18 +08:00
if result [ " status " ] == 0
tmpstatus = 0
end
2016-05-03 11:24:16 +08:00
2016-05-16 16:57:18 +08:00
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
#每次从数据库取出上次的结果加上本次的结果再存入数据库
if result [ " status " ] . to_i != - 2
result [ " results " ] . first [ 'user_wait' ] = tUsedtime
if result [ " results " ] [ 0 ] [ " status " ] . to_i == 2
result [ " status " ] = 2
end
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!
2016-07-12 16:49:15 +08:00
resultObj [ :time ] = student_work_test . created_at . strftime ( format = '%Y-%m-%d %H:%M:%S' )
2016-05-16 16:57:18 +08:00
resultObj [ :index ] = student_work . student_work_tests . count
end
#将每次用户等待时间都存起来以便管理界面显示用
tEndtime = Time . now
tUsedtime = ( tEndtime . to_i - tStarttime . to_i ) * 1000 + ( tEndtime . usec - tStarttime . usec ) / 1000
logger . debug " program_test_ex user wait time = #{ tUsedtime } 毫秒 "
time_used = 0
if result [ " status " ] . to_i != - 2
#至少一毫秒
time_used = result [ " results " ] . first [ 'time_used' ] == 0 ? 1 :result [ " results " ] . first [ 'time_used' ]
end
#0:答案正确 -3http超时 -2:编译错误 -1:答案错误 2:程序运行超时
CodeTests . create ( :homework_id = > @homework . id , :language = > @homework . homework_detail_programing . language , :status = > tmpstatus , :time_used = > time_used , :wait_time = > tUsedtime , :student_work_id = > student_work . id )
end
2016-05-03 11:24:16 +08:00
end
2016-05-16 16:57:18 +08:00
rescue
#-5 program_test_ex 函数出错
2016-09-26 09:44:47 +08:00
logger . info " program_test_error 2 "
2016-05-16 16:57:18 +08:00
resultObj [ :status ] = - 5
tmpstatus = - 5
CodeTests . create ( :homework_id = > @homework . id , :language = > @homework . homework_detail_programing . language , :status = > tmpstatus , :time_used = > 0 , :wait_time = > 0 , :student_work_id = > student_work . id )
2016-05-03 11:24:16 +08:00
end
2016-05-16 16:57:18 +08:00
#渲染返回结果
render :json = > resultObj
2016-03-25 14:43:51 +08:00
end
end
end
2016-05-03 11:24:16 +08:00
#找出该作业的所有提交作业
def find_all_student_work_by_homeid ( )
all_studentwork = StudentWork . where ( " homework_common_id = #{ @homework . id } and is_test = 0 " )
all_studentwork
end
def request_code_repeattest ( src )
url = " #{ Redmine :: Configuration [ 'jplag_server' ] } api/realtime_test.json "
factor = [ ]
src . each do | test |
factor << { work_id : test . id , des : test . description , created_at : test . created_at . to_i }
end
solutions = {
homeid : @homework . id ,
language : @homework . homework_detail_programing . language ,
factor : factor
}
uri = URI ( url )
body = solutions . to_json
logger . debug " send body "
logger . debug body
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 work_canrepeat
@homework_id = params [ :homework ]
@course_id = params [ :course_id ]
all_studentwork = find_all_student_work_by_homeid ( )
2016-07-14 10:06:37 +08:00
@work_count = all_studentwork . has_committed . count
2016-05-03 11:24:16 +08:00
end
#代码查重 status: 0完成 -2不需要查重 -1查重失败不支持该语言
def code_repeattest
tStarttime = Time . now
logger . debug " code_repeattest start is #{ tStarttime } } "
resultObj = { status : - 2 }
@homework = HomeworkCommon . find params [ :homework ]
2016-07-14 10:06:37 +08:00
all_studentwork = find_all_student_work_by_homeid ( ) . has_committed
2016-05-03 11:24:16 +08:00
if all_studentwork == nil
resultObj [ :status ] = - 2
elsif all_studentwork . count < = 1
resultObj [ :status ] = - 2
else
#@homework.homework_detail_programing.language、id、description
logger . debug " time1 is #{ Time . now . usec } "
result = request_code_repeattest ( all_studentwork )
logger . debug " time2 is #{ Time . now . usec } "
resultObj [ :status ] = result [ 'status' ] . to_i
# resultObj[:results] = result['results']
#Time.now, simi_id = simiworkid , simi_value = simivalue
if resultObj [ :status ] == 0
@homework . simi_time = Time . now
resultObj [ :comparetime ] = @homework . simi_time
@homework . update_column ( 'simi_time' , @homework . simi_time )
logger . debug " time3 is #{ Time . now . usec } "
result [ 'results' ] . each do | key , value |
@student_work = StudentWork . where ( " id =? " , key . to_i ) . first
@student_work . update_column ( 'simi_id' , value [ 'simiworkid' ] . to_i )
@student_work . update_column ( 'simi_value' , value [ 'simivalue' ] . to_i )
# sqlstr = "update student_works set simi_id=#{value['simiworkid']},simi_value=#{value['simivalue']} where id=#{key.to_i} "
# dbh.execute(sqlstr)
end
logger . debug " time4 is #{ Time . now . usec } "
2016-03-25 14:43:51 +08:00
end
end
2016-05-03 11:24:16 +08:00
tEndtime = Time . now
logger . debug " code_repeattest end is #{ tEndtime } } "
tUsedtime = ( tEndtime . to_i - tStarttime . to_i ) * 1000000 + ( tEndtime . usec - tStarttime . usec )
logger . debug " code_repeattest userd utime is #{ tUsedtime } "
render :json = > resultObj
2016-03-25 14:43:51 +08:00
end
2016-05-03 11:24:16 +08:00
#上次代码查重时间
def last_codecomparetime
resultObj = { status : 0 }
@homework = HomeworkCommon . find params [ :homework ]
#转换一下
if @homework . simi_time != nil
resultObj [ :comparetime ] = Time . parse ( @homework . simi_time . to_s ) . strftime ( " %Y-%m-%d %H:%M:%S " )
else
resultObj [ :comparetime ] = 0
end
render :json = > resultObj
end
2016-03-25 14:43:51 +08:00
2015-05-19 17:12:43 +08:00
def index
2016-09-13 14:57:45 +08:00
# 作业消息状态更新?
homeworkcommon_messages = CourseMessage . where ( :user_id = > User . current . id , :viewed = > 0 , :course_message_id = > @homework . id , :course_message_type = > " HomeWorkCommon " )
homeworkcommon_messages . update_all ( :viewed = > true )
studentwork_messages = CourseMessage . where ( :user_id = > User . current . id , :viewed = > 0 , :course_id = > @homework . course , :course_message_type = > " StudentWork " )
studentwork_messages . update_all ( :viewed = > true )
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 )
2016-09-13 14:57:45 +08:00
studentworks_scores . update_all ( :viewed = > true )
2015-09-08 14:14:49 +08:00
# 作品评论消息状态更新
journals_for_teacher = CourseMessage . where ( " user_id =? and course_id =? and course_message_type =? and viewed =? " , User . current . id , @homework . course , " JournalsForMessage " , 0 )
2016-09-13 14:57:45 +08:00
journals_for_teacher . update_all ( :viewed = > true )
2015-09-16 15:51:35 +08:00
##################################################################################################################
2016-05-23 14:47:10 +08:00
@order , @b_sort , @name , @group = params [ :order ] || " score " , params [ :sort ] || " desc " , params [ :name ] . to_s . strip || " " , params [ :group ]
2015-11-24 16:21:25 +08:00
@homework_commons = @course . homework_commons . where ( " publish_time <= ? " , Time . now . strftime ( " %Y-%m-%d " ) ) . order ( " created_at desc " )
2016-05-20 15:39:07 +08:00
@all_homework_commons = @course . homework_commons . order ( " created_at desc " )
2015-09-17 14:16:13 +08:00
@is_teacher = User . current . allowed_to? ( :as_teacher , @course ) || User . current . admin?
2015-09-25 14:33:33 +08:00
@is_evaluation = @homework . homework_detail_manual && @homework . homework_detail_manual . comment_status == 2 && ! @is_teacher #是不是匿评
2015-09-17 17:06:08 +08:00
@show_all = false
2015-09-22 16:16:55 +08:00
2016-07-26 10:38:04 +08:00
# 0表示没有分组的学生, -1表示所有分组的学生
2016-07-13 16:58:31 +08:00
if @group
if @group == " 0 "
none_group_students = @course . members . select { | member | member . course_group_id == 0 }
if none_group_students . empty?
student_in_group = '(0)'
else
student_in_group = '(' + none_group_students . map { | member | member . user_id } . join ( ',' ) + ')'
end
2016-07-25 19:53:56 +08:00
elsif @group == " -1 "
all_group_students = @course . members . select { | member | member . course_group_id }
if all_group_students . empty?
student_in_group = '(0)'
else
student_in_group = '(' + all_group_students . map { | member | member . user_id } . join ( ',' ) + ')'
end
2015-09-17 14:16:13 +08:00
else
2016-07-13 16:58:31 +08:00
course_group = CourseGroup . find_by_id ( @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
2015-09-22 16:16:55 +08:00
end
2016-07-13 16:58:31 +08:00
2016-03-11 16:17:51 +08:00
#开放作品 || 老师 || 超级管理员 || 禁用匿评&&作业截止&&已提交作品 显示所有列表
2016-07-08 13:17:14 +08:00
if ( @homework . is_open == 1 && @course . is_public == 1 ) || ( @homework . is_open == 1 && @course . is_public == 0 && User . current . member_of_course? ( @course ) ) || @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 . has_committed . where ( :user_id = > User . current . id ) . empty? )
2016-03-11 16:17:51 +08:00
if @order == 'lastname'
2016-09-06 15:07:43 +08:00
@stundet_works = search_homework_member @homework . student_works . no_copy . select ( " student_works.*,student_works.work_score 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
2016-03-11 16:17:51 +08:00
elsif @order == 'student_id'
2016-09-06 15:07:43 +08:00
@stundet_works = search_homework_member @homework . student_works . no_copy . select ( " student_works.*,student_works.work_score 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
2016-03-11 16:17:51 +08:00
else
2016-09-06 15:07:43 +08:00
@stundet_works = search_homework_member @homework . student_works . no_copy . select ( " student_works.*,student_works.work_score as score " ) . joins ( :user ) . where ( " users.id in #{ student_in_group } " ) . order ( " #{ @order } #{ @b_sort } " ) , @name
2016-03-11 16:17:51 +08:00
end
@show_all = true
elsif User . current . member_of_course? ( @course )
if @homework . homework_detail_manual . comment_status == 1 #学生 && 未开启匿评 只看到自己的
2016-02-25 16:18:24 +08:00
if @homework . homework_type == 3
pro = @homework . student_work_projects . where ( :user_id = > User . current . id ) . first
if pro . nil?
@stundet_works = [ ]
else
2016-04-27 16:21:35 +08:00
@stundet_works = @homework . student_works . select ( " student_works.*,student_works.work_score as score " ) . where ( :id = > pro . student_work_id )
2016-02-25 16:18:24 +08:00
end
2016-02-20 18:26:52 +08:00
else
2016-07-08 13:17:14 +08:00
@stundet_works = @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score as score " ) . where ( :user_id = > User . current . id )
2016-02-20 18:26:52 +08:00
end
2016-02-25 16:18:24 +08:00
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
2016-02-20 18:26:52 +08:00
else
2016-07-08 13:17:14 +08:00
my_work = @homework . student_works . has_committed . where ( :user_id = > User . current . id )
2016-02-20 18:26:52 +08:00
end
2016-02-25 16:18:24 +08:00
@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
2016-04-27 16:21:35 +08:00
my_work = @homework . student_works . select ( " student_works.*,student_works.work_score as score " ) . where ( :id = > pro . student_work_id )
2016-02-25 16:18:24 +08:00
end
2016-02-20 18:26:52 +08:00
else
2016-07-08 13:17:14 +08:00
my_work = @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score as score " ) . where ( :user_id = > User . current . id )
2016-02-25 16:18:24 +08:00
end
if my_work . empty?
@stundet_works = [ ]
else
2016-03-11 16:17:51 +08:00
if @order == 'lastname'
2016-07-08 13:17:14 +08:00
@stundet_works = search_homework_member @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score 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
2016-03-11 16:17:51 +08:00
elsif @order == 'student_id'
2016-07-08 13:17:14 +08:00
@stundet_works = search_homework_member @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score 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
2016-03-11 16:17:51 +08:00
else
2016-07-08 13:17:14 +08:00
@stundet_works = search_homework_member @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score as score " ) . joins ( :user ) . where ( " users.id in #{ student_in_group } " ) . order ( " #{ @order } #{ @b_sort } " ) , @name
2016-03-11 16:17:51 +08:00
end
2016-02-25 16:18:24 +08:00
@show_all = true
2016-02-20 18:26:52 +08:00
end
2015-12-11 14:57:09 +08:00
else
2015-09-22 16:16:55 +08:00
@stundet_works = [ ]
end
else
2016-02-25 16:18:24 +08:00
render_403
return
2015-09-17 14:16:13 +08:00
end
2016-02-25 16:18:24 +08:00
2016-07-08 13:17:14 +08:00
@student_work_count = ( search_homework_member @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score as score " ) . joins ( :user ) . where ( " users.id in #{ student_in_group } " ) . order ( " #{ @order } #{ @b_sort } " ) , @name ) . count
2015-09-17 14:16:13 +08:00
else
2016-07-08 13:17:14 +08:00
if ( @homework . is_open == 1 && @course . is_public == 1 ) || ( @homework . is_open == 1 && @course . is_public == 0 && User . current . member_of_course? ( @course ) ) || @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 . has_committed . where ( :user_id = > User . current . id ) . empty? )
2016-03-11 16:17:51 +08:00
if @order == 'lastname'
2016-09-06 15:07:43 +08:00
@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
2016-03-11 16:17:51 +08:00
elsif @order == 'student_id'
2016-09-06 15:07:43 +08:00
@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
2016-03-11 16:17:51 +08:00
else
2016-09-14 17:36:20 +08:00
@stundet_works = search_homework_member @homework . student_works . no_copy . select ( " student_works.*,student_works.work_score as score " ) . includes ( :user = > { :user_extensions = > [ ] } , :project = > { } , :student_works_scores = > { } ) . order ( " #{ @order } #{ @b_sort } " ) , @name
2016-03-11 16:17:51 +08:00
end
@show_all = true
elsif User . current . member_of_course? ( @course )
if @homework . homework_detail_manual . comment_status == 1 #学生 && 未开启匿评 只看到自己的
2016-02-25 16:18:24 +08:00
if @homework . homework_type == 3
pro = @homework . student_work_projects . where ( :user_id = > User . current . id ) . first
if pro . nil?
@stundet_works = [ ]
else
2016-04-27 16:21:35 +08:00
@stundet_works = @homework . student_works . select ( " student_works.*,student_works.work_score as score " ) . where ( :id = > pro . student_work_id )
2016-02-25 16:18:24 +08:00
end
2016-02-20 18:26:52 +08:00
else
2016-07-08 13:17:14 +08:00
@stundet_works = @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score as score " ) . where ( :user_id = > User . current . id )
2016-02-20 18:26:52 +08:00
end
2016-02-25 16:18:24 +08:00
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
2016-02-20 18:26:52 +08:00
else
2016-07-08 13:17:14 +08:00
my_work = @homework . student_works . has_committed . where ( :user_id = > User . current . id )
2016-02-20 18:26:52 +08:00
end
2016-02-25 16:18:24 +08:00
@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
2016-04-27 16:21:35 +08:00
my_work = @homework . student_works . select ( " student_works.*,student_works.work_score as score " ) . where ( :id = > pro . student_work_id )
2016-02-25 16:18:24 +08:00
end
2016-02-20 18:26:52 +08:00
else
2016-07-08 13:17:14 +08:00
my_work = @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score as score " ) . where ( :user_id = > User . current . id )
2016-02-25 16:18:24 +08:00
end
if my_work . empty?
@stundet_works = [ ]
else
2016-03-11 16:17:51 +08:00
if @order == 'lastname'
2016-07-08 13:17:14 +08:00
@stundet_works = search_homework_member @homework . student_works . has_committed . 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
2016-03-11 16:17:51 +08:00
elsif @order == 'student_id'
2016-07-08 13:17:14 +08:00
@stundet_works = search_homework_member @homework . student_works . has_committed . 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
2016-03-11 16:17:51 +08:00
else
2016-07-08 13:17:14 +08:00
@stundet_works = search_homework_member @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score as score " ) . order ( " #{ @order } #{ @b_sort } " ) , @name
2016-03-11 16:17:51 +08:00
end
2016-02-25 16:18:24 +08:00
@show_all = true
2016-02-20 18:26:52 +08:00
end
2015-12-11 14:57:09 +08:00
else
2015-09-22 16:16:55 +08:00
@stundet_works = [ ]
end
else
2016-02-25 16:18:24 +08:00
render_403
return
2015-09-22 16:16:55 +08:00
end
2016-07-08 13:17:14 +08:00
@student_work_count = ( search_homework_member @homework . student_works . has_committed . select ( " student_works.*,student_works.work_score as score " ) . order ( " #{ @order } #{ @b_sort } " ) , @name ) . count
2015-09-22 16:16:55 +08:00
2016-04-28 12:38:06 +08:00
end
2015-09-17 14:16:13 +08:00
@score = @b_sort == " desc " ? " asc " : " desc "
2016-04-22 11:27:30 +08:00
@is_focus = params [ :is_focus ] ? params [ :is_focus ] . to_i : 0
2016-03-24 15:04:50 +08:00
# 消息传过来的ID
@message_student_work_id = params [ :student_work_id ]
2016-07-22 15:32:37 +08:00
@left_nav_type = 3
2016-09-21 10:34:38 +08:00
@tab = params [ :tab ] . to_i
2015-05-19 17:12:43 +08:00
respond_to do | format |
2015-09-22 15:52:08 +08:00
format . js
2015-05-19 17:12:43 +08:00
format . html
2015-05-28 11:07:28 +08:00
format . xls {
2015-10-13 17:10:46 +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
send_data ( homework_to_xls ( @stundet_works ) , :type = > " text/excel;charset=utf-8; header=present " ,
2015-10-13 17:10:46 +08:00
:filename = > filename_for_content_disposition ( filename ) )
2015-05-28 11:07:28 +08:00
}
2015-05-19 17:12:43 +08:00
end
end
def new
2015-10-20 16:19:32 +08:00
#更新消息
noEvaluation = @homework . course_messages . where ( " user_id =? and viewed =? " , User . current . id , 0 )
noEvaluation . update_all ( :viewed = > true )
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
2016-07-08 13:17:14 +08:00
#@student_work = @homework.student_works.where("user_id = ?",User.current.id).first
#if @student_work.nil?
2015-08-28 12:04:49 +08:00
@student_work = StudentWork . new
2016-07-08 13:17:14 +08:00
#end
2015-09-02 19:41:45 +08:00
respond_to do | format |
2016-09-14 20:58:15 +08:00
format . html { render :layout = > " base_courses " }
2015-09-02 19:41:45 +08:00
end
2015-05-19 17:12:43 +08:00
end
def create
2015-11-23 17:08:17 +08:00
# 提交作品前先判断是否已经提交
2015-11-24 15:17:47 +08:00
@has_commit = false ;
2015-11-23 17:08:17 +08:00
if hsd_committed_work? ( User . current . id , @homework . id )
2016-07-08 13:17:14 +08:00
@work = StudentWork . where ( " user_id =? and homework_common_id =? and work_status != 0 " , User . current . id , @homework . id ) . first
2015-11-24 15:17:47 +08:00
@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
2015-11-23 17:08:17 +08:00
return
end
2015-05-21 15:52:27 +08:00
if params [ :student_work ]
2015-11-06 10:24:29 +08:00
@submit_result = true
2015-09-09 00:43:54 +08:00
student_work = StudentWork . find ( params [ :student_work_id ] ) if params [ :student_work_id ]
2016-07-08 13:17:14 +08:00
student_work = StudentWork . where ( " user_id =? and homework_common_id =? and work_status = 0 " , User . current . id , @homework . id ) . first
2015-09-09 00:43:54 +08:00
student_work || = StudentWork . new
2016-03-18 10:56:35 +08:00
student_work . name = params [ :student_work ] [ :name ] == " #{ @homework . name } 的作品提交(可修改) " ? " #{ @homework . name } 的作品提交 " : params [ :student_work ] [ :name ]
2015-09-09 00:43:54 +08:00
student_work . description = params [ :student_work ] [ :description ]
student_work . homework_common_id = @homework . id
student_work . user_id = User . current . id
2016-08-10 16:50:27 +08:00
student_work . commit_time = Time . now
2015-09-09 00:43:54 +08:00
student_work . save_attachments ( params [ :attachments ] )
2015-09-16 10:26:13 +08:00
render_attachment_warning_if_needed ( student_work )
2015-12-11 14:57:09 +08:00
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
2015-09-16 10:26:13 +08:00
#提交作品时,计算是否迟交
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
2016-07-08 13:17:14 +08:00
student_work . work_status = 2
2015-09-16 10:26:13 +08:00
else
2015-09-09 00:43:54 +08:00
student_work . late_penalty = 0
2016-07-08 13:17:14 +08:00
student_work . work_status = 1
2015-07-02 16:16:29 +08:00
end
2015-09-09 00:43:54 +08:00
if student_work . save
2016-09-06 15:07:43 +08:00
if @homework . homework_type == 3
if @homework . homework_detail_group . base_on_project == 1
@student_work_project . student_work_id = student_work . id
@student_work_project . save
elsif @homework . homework_detail_group . base_on_project == 0
stu_project = StudentWorkProject . new ( :homework_common_id = > @homework . id , :student_work_id = > student_work . id , :project_id = > student_work . project_id , :user_id = > student_work . user_id , :is_leader = > 1 )
2015-12-11 14:57:09 +08:00
stu_project . save
end
members = params [ :group_member_ids ] . split ( ',' )
2016-09-06 15:07:43 +08:00
for i in 1 .. members . count - 1
2016-09-06 17:06:31 +08:00
stu_work = StudentWork . new ( :name = > student_work . name , :description = > student_work . description , :user_id = > members [ i ] . to_i , :homework_common_id = > @homework . id , :project_id = > student_work . project_id , :late_penalty = > student_work . late_penalty , :work_status = > 3 , :commit_time = > student_work . commit_time )
2016-09-06 15:07:43 +08:00
if stu_work . save
stu_project = StudentWorkProject . new ( :homework_common_id = > @homework . id , :student_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 )
stu_project . save
2015-12-11 14:57:09 +08:00
end
end
end
2016-04-08 20:44:09 +08:00
@homework . update_column ( :updated_at , Time . now )
2016-01-12 15:53:15 +08:00
update_course_activity ( @homework . class , @homework . id )
update_user_activity ( @homework . class , @homework . id )
update_org_activity ( @homework . class , @homework . id )
2015-12-11 17:02:34 +08:00
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
2015-11-05 14:47:29 +08:00
@student_work = StudentWork . where ( homework_common_id : @homework . id , user_id : User . current . id ) . first
2015-05-21 15:52:27 +08:00
respond_to do | format |
2015-11-05 14:47:29 +08:00
format . js
2015-05-21 15:52:27 +08:00
end
return
end
end
2015-11-06 10:24:29 +08:00
@submit_result = false
2015-05-21 15:52:27 +08:00
respond_to do | format |
2015-11-06 10:24:29 +08:00
format . js
2015-05-21 15:52:27 +08:00
end
2015-05-19 17:12:43 +08:00
end
def edit
2015-09-02 19:52:44 +08:00
@user = User . current
2015-12-01 15:18:14 +08:00
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 " ) #编程作业不能修改作业|| 截止日期已到不能修改作业
2015-07-08 17:19:11 +08:00
render_403
else
respond_to do | format |
2016-09-14 20:58:15 +08:00
format . html { render :layout = > " base_courses " }
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 ]
2015-11-09 11:16:19 +08:00
@submit_result = true
2015-05-28 17:09:38 +08:00
@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
2016-03-23 17:19:25 +08:00
#如果学生作品被打分后修改,应该给老师提示
send_message_to_teacher ( @work )
2015-12-17 19:06:10 +08:00
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 )
2016-09-06 15:07:43 +08:00
user_ids = student_work_projects . empty? ? " (-1) " : " ( " + student_work_projects . map { | stu | stu . user_id } . join ( " , " ) + " ) "
student_works = @homework . student_works . where ( " user_id in #{ user_ids } " )
2016-09-06 16:38:29 +08:00
student_works . delete_all
2015-12-17 19:06:10 +08:00
student_work_projects . delete_all
members = params [ :group_member_ids ] . split ( ',' )
for i in 1 .. members . count - 1
2016-09-06 17:06:31 +08:00
stu_work = StudentWork . new ( :name = > @work . name , :description = > @work . description , :user_id = > members [ i ] . to_i , :homework_common_id = > @homework . id , :project_id = > @work . project_id , :late_penalty = > @work . late_penalty , :work_status = > 3 , :commit_time = > @work . commit_time )
2016-09-06 15:07:43 +08:00
if stu_work . save
stu_project = StudentWorkProject . new ( :homework_common_id = > @homework . id , :student_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 )
stu_project . save
2015-12-17 19:06:10 +08:00
end
end
end
2015-12-11 17:02:34 +08:00
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
2015-11-09 11:16:19 +08:00
@student_work = @work
respond_to do | format |
format . js
2015-05-28 17:09:38 +08:00
end
return
end
end
2015-11-09 11:16:19 +08:00
= begin
2015-05-28 17:09:38 +08:00
respond_to do | format |
format . html { redirect_to edit_student_work_url ( @work ) }
end
2015-11-09 11:16:19 +08:00
= end
@submit_result = false
respond_to do | format |
format . js
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-09-17 16:04:16 +08:00
@is_teacher = User . current . allowed_to? ( :as_teacher , @course ) || User . current . admin?
@student_work_scores = @work . student_works_scores . order ( " updated_at desc " )
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
2016-08-26 16:20:14 +08:00
if @homework . homework_type == 3
2016-09-06 15:07:43 +08:00
pros = @work . student_work_projects . where ( " is_leader = 0 " )
user_ids = pros . empty? ? " (-1) " : " ( " + pros . map { | stu | stu . user_id } . join ( " , " ) + " ) "
student_works = @homework . student_works . where ( " user_id in #{ user_ids } " )
2016-09-06 16:38:29 +08:00
student_works . delete_all
2016-09-06 15:07:43 +08:00
pros . delete_all
project = @work . student_work_projects . where ( " is_leader = 1 " ) . first
if @homework . homework_detail_group . base_on_project == 1
project . update_attributes ( :student_work_id = > nil )
else
project . destroy
2015-12-11 14:57:09 +08:00
end
2016-09-06 16:38:29 +08:00
@work . destroy
else
@work . attachments . destroy_all
@work . student_works_scores . destroy_all
@work . course_messages . destroy_all
@work . student_work_tests . destroy_all
@work . update_attributes ( :work_status = > 0 , :name = > " #{ @homework . name } 的作品提交 " , :description = > nil , :late_penalty = > 0 , :commit_time = > nil , :final_score = > nil , :teacher_score = > nil , :student_score = > nil , :teaching_asistant_score = > nil , :system_score = > 0 , :work_score = > nil , :project_id = > 0 )
@work . update_column ( " work_score " , nil )
2016-08-26 16:20:14 +08:00
end
respond_to do | format |
format . html {
redirect_to student_work_index_url ( :homework = > @homework . id )
}
2015-05-28 17:18:37 +08:00
end
2015-05-20 17:53:01 +08:00
end
2015-12-01 15:18:14 +08:00
def delete_work
@work = StudentWork . where ( " user_id =? and homework_common_id =? " , User . current . id , @homework . id ) . first
if @work
@work . destroy
2015-12-11 14:57:09 +08:00
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
2015-12-01 15:18:14 +08:00
end
2016-03-29 10:39:44 +08:00
redirect_to student_homeworks_user_path ( User . current . id )
2015-12-01 15:18:14 +08:00
end
def retry_work
2016-08-05 17:33:31 +08:00
if @homework . homework_type == 3
2016-09-06 15:07:43 +08:00
pros = @work . student_work_projects . where ( " is_leader = 0 " )
user_ids = pros . empty? ? " (-1) " : " ( " + pros . map { | stu | stu . user_id } . join ( " , " ) + " ) "
student_works = @homework . student_works . where ( " user_id in #{ user_ids } " )
2016-09-06 18:01:16 +08:00
student_works . delete_all
2016-09-06 15:07:43 +08:00
pros . delete_all
project = @work . student_work_projects . where ( " is_leader = 1 " ) . first
if @homework . homework_detail_group . base_on_project == 1
project . update_attributes ( :student_work_id = > nil )
else
project . destroy
2015-12-11 14:57:09 +08:00
end
2016-09-06 16:38:29 +08:00
@work . destroy
else
@work . update_attributes ( :work_status = > 0 , :name = > " #{ @homework . name } 的作品提交 " , :description = > nil , :late_penalty = > 0 , :commit_time = > nil , :project_id = > 0 )
@work . attachments . destroy_all
@work . course_messages . destroy_all
2016-08-05 17:33:31 +08:00
end
@student_work = StudentWork . new
respond_to do | format |
format . js
2015-12-01 15:18:14 +08:00
end
end
2015-05-27 11:25:32 +08:00
#添加评分,已评分则为修改评分
2015-05-22 14:45:01 +08:00
def add_score
2015-09-17 16:31:32 +08:00
@is_last = params [ :is_last ] == " true "
@is_teacher = User . current . allowed_to? ( :as_teacher , @course ) || User . current . admin?
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
2016-05-11 09:18:55 +08:00
#@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
@is_last_a = @work . student_works_scores . empty?
@new_score = StudentWorksScore . new
if @score && params [ :score ]
@new_score . score = params [ :score ] . to_i == - 1 ? @score . score : params [ :score ]
elsif @score . nil? && params [ :score ]
@new_score . score = params [ :score ] . to_i == - 1 ? nil : params [ :score ]
end
@new_score . comment = params [ :new_form ] [ :user_message ] if params [ :new_form ] && params [ :new_form ] [ :user_message ] && params [ :new_form ] [ :user_message ] != " "
@new_score . user_id = User . current . id
@new_score . student_work_id = @work . id
2016-07-08 15:02:50 +08:00
if @is_teacher && @work . work_status == 0
2016-08-10 16:50:27 +08:00
@work . update_attributes ( :work_status = > 1 , :commit_time = > Time . now )
2016-07-08 15:02:50 +08:00
end
2016-05-11 09:18:55 +08:00
if User . current . admin?
@new_score . reviewer_role = 1
else
2016-08-05 10:03:47 +08:00
role = User . current . members . where ( " course_id = ? " , @course . id ) . first . roles . where ( " is_current = 1 " ) . first . name
2016-05-11 09:18:55 +08:00
@new_score . reviewer_role = get_role_by_name ( role )
end
2015-05-27 16:01:00 +08:00
if @score
2016-05-10 15:26:08 +08:00
if @is_teacher
@is_new = true
@is_last_a = false
else
@is_new = false
2015-07-15 17:34:34 +08:00
end
2015-05-26 18:14:53 +08:00
else
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
2016-05-10 15:26:08 +08:00
@new_score . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @new_score )
2015-05-26 18:14:53 +08:00
2016-05-10 15:26:08 +08:00
if @new_score . save
2016-09-14 17:38:57 +08:00
if @homework . homework_type == 3
@is_group_leader = ! @work . student_work_projects . empty?
end
2016-05-10 15:26:08 +08:00
case @new_score . reviewer_role
2015-05-27 17:32:14 +08:00
when 1 #教师评分:最后一个教师评分为最终评分
2016-05-10 15:26:08 +08:00
@work . teacher_score = @new_score . score
2016-09-14 17:38:57 +08:00
if @is_group_leader
add_score_to_member @work , @homework , 1 , 'teacher_score' , @new_score . score
end
2015-05-27 17:32:14 +08:00
when 2 #教辅评分 教辅评分显示平均分
2016-05-11 09:18:55 +08:00
#@work.teaching_asistant_score = @work.student_works_scores.where(:reviewer_role => 2).average(:score).try(:round, 2).to_f
ts_score = StudentWorksScore . find_by_sql ( " SELECT AVG(score) AS score FROM (SELECT * FROM (SELECT * FROM student_works_scores WHERE student_work_id = #{ @work . id } AND reviewer_role = 2 AND score IS NOT NULL ORDER BY created_at DESC) AS t GROUP BY user_id) AS a " )
@work . teaching_asistant_score = ts_score . first . score . nil? ? nil : ts_score . first . score . try ( :round , 2 ) . to_f
2016-09-14 17:38:57 +08:00
if @is_group_leader
add_score_to_member @work , @homework , 2 , 'teaching_asistant_score' , @work . teaching_asistant_score
end
2015-05-27 17:32:14 +08:00
when 3 #学生评分 学生评分显示平均分
2016-05-11 09:18:55 +08:00
#@work.student_score = @work.student_works_scores.where(:reviewer_role => 3).average(:score).try(:round, 2).to_f
stu_score = StudentWorksScore . find_by_sql ( " SELECT AVG(score) AS score FROM (SELECT * FROM (SELECT * FROM student_works_scores WHERE student_work_id = #{ @work . id } AND reviewer_role = 3 ORDER BY created_at DESC) AS t GROUP BY user_id) AS a " )
@work . student_score = stu_score . first . score . try ( :round , 2 ) . to_f
2016-09-14 17:38:57 +08:00
if @is_group_leader
add_score_to_member @work , @homework , 3 , 'student_score' , @work . student_score
end
2015-05-27 17:32:14 +08:00
end
2016-05-18 14:49:49 +08:00
@homework . update_column ( 'updated_at' , Time . now )
2016-03-09 11:15:43 +08:00
update_course_activity ( @homework . class , @homework . id )
update_user_activity ( @homework . class , @homework . id )
update_org_activity ( @homework . class , @homework . id )
2015-05-27 17:32:14 +08:00
if @work . save
2016-05-11 09:18:55 +08:00
@work = @homework . student_works . select ( " student_works.*,student_works.work_score as score " ) . where ( :id = > @work . id ) . first
2016-08-22 15:24:12 +08:00
@count = @homework . student_works . has_committed . count
2015-05-27 17:32:14 +08:00
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-09-17 16:31:32 +08:00
@is_last = params [ :is_last ] == " true "
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
2016-05-11 09:18:55 +08:00
#提交未评分的评语
def add_comments_to_work
@work = StudentWork . find params [ :id ]
respond_to do | format |
format . js
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 "
2016-07-22 15:32:37 +08:00
@left_nav_type = 3
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 {
2015-10-13 17:10:46 +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
send_data ( absence_penalty_list_xls ( @stundet_works ) , :type = > " text/excel;charset=utf-8; header=present " ,
2015-10-13 17:10:46 +08:00
:filename = > filename_for_content_disposition ( filename ) )
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 {
2015-10-13 17:10:46 +08:00
filename = " #{ @course . teacher . lastname . to_s + @course . teacher . firstname } _ #{ @course . name } _ #{ @course . time . to_s + @course . term } _ #{ @homework . name } #{ l ( :excel_evaluation_list ) } .xls "
2015-06-30 16:36:37 +08:00
send_data ( evaluation_list_xls ( @homework . student_works ) , :type = > " text/excel;charset=utf-8; header=present " ,
2015-10-13 17:10:46 +08:00
:filename = > filename_for_content_disposition ( filename ) )
2015-06-30 16:36:37 +08:00
}
end
2015-06-30 15:30:51 +08:00
end
2015-09-17 20:20:20 +08:00
#设置评分规则
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
end
2016-04-15 17:06:45 +08:00
if params [ :absence_penalty ] && homework_detail_manual . absence_penalty . to_s != params [ :absence_penalty ] . to_s
2015-09-17 20:20:20 +08:00
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
2015-09-17 20:33:36 +08:00
student_work . absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * homework_detail_manual . absence_penalty : 0
2015-09-17 20:20:20 +08:00
student_work . save
end
end
2015-09-18 11:29:58 +08:00
homework_detail_manual . save if homework_detail_manual
2015-09-17 20:20:20 +08:00
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
2016-05-03 15:12:56 +08:00
@homework . update_column ( 'teacher_priority' , teacher_priority )
2015-09-18 11:29:58 +08:00
homework_detail_manual . save if homework_detail_manual
homework_detail_programing . save if homework_detail_programing
2015-09-17 20:20:20 +08:00
@homework . student_works . each do | student_work |
set_final_score @homework , student_work
student_work . save
end
end
2016-05-03 11:24:16 +08:00
2016-04-21 10:51:43 +08:00
@homework . save
2015-10-28 10:17:45 +08:00
if params [ :student_path ]
redirect_to student_work_index_url ( :homework = > @homework . id )
else
2015-12-07 10:54:18 +08:00
@user_activity_id = params [ :user_activity_id ] . to_i
2016-09-19 17:14:14 +08:00
@hw_status = params [ :hw_status ] . to_i
2015-10-28 10:17:45 +08:00
respond_to do | format |
format . js
end
2015-09-17 20:20:20 +08:00
end
end
2015-11-04 10:54:09 +08:00
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
2016-09-19 17:14:14 +08:00
@hw_status = params [ :hw_status ] . to_i
2015-11-04 10:54:09 +08:00
end
2015-12-04 16:21:11 +08:00
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
2015-12-25 17:07:27 +08:00
attachment = @work . attachments . where ( " attachtype = 7 " ) . first
attachment . update_attributes ( :description = > params [ :description ] )
2016-05-12 10:29:43 +08:00
if attachment . attachtype == 7
student_work = StudentWork . find attachment . container_id
CourseMessage . create ( :user_id = > student_work . homework_common . user_id , :course_id = > student_work . homework_common . course_id , :viewed = > false , :course_message_id = > attachment . container_id , :course_message_type = > 'StudentWork' , :status = > 2 )
end
2015-12-04 16:21:11 +08:00
respond_to do | format |
format . js
end
end
2015-12-08 17:37:15 +08:00
def new_student_work_project
@user_activity_id = params [ :user_activity_id ] . to_i
2016-09-19 17:14:14 +08:00
@hw_status = params [ :hw_status ] . to_i
2015-12-08 17:37:15 +08:00
respond_to do | format |
format . js
end
end
#创建作业的关联项目
def student_work_project
2016-09-06 15:07:43 +08:00
@work = @homework . student_works . where ( " user_id = #{ User . current . id } and work_status = 0 " ) . first
if @work
@work . update_column ( 'project_id' , params [ :projectName ] . to_i )
end
2015-12-09 10:29:54 +08:00
@project = StudentWorkProject . new
@project . homework_common_id = @homework . id
2016-09-06 15:07:43 +08:00
@project . project_id = params [ :projectName ] . to_i
2015-12-09 10:29:54 +08:00
@project . user_id = User . current . id
2015-12-11 14:57:09 +08:00
@project . is_leader = 1
2015-12-09 10:29:54 +08:00
if @project . save
@user_activity_id = params [ :user_activity_id ] . to_i
2016-09-19 17:14:14 +08:00
@hw_status = params [ :hw_status ] . to_i
2016-09-21 10:34:38 +08:00
@is_teacher = User . current . allowed_to? ( :as_teacher , @homework . course ) || User . current . admin?
2015-12-09 10:29:54 +08:00
respond_to do | format |
format . js
2015-12-08 17:37:15 +08:00
end
end
2015-12-09 10:29:54 +08:00
2015-12-08 17:37:15 +08:00
end
#查找学生创建的项目列表
def search_user_projects
condition = '%%'
if ! params [ :name ] . nil?
condition = " % #{ params [ :name ] . strip } % " . gsub ( " " , " " )
end
2016-01-13 18:42:25 +08:00
@project_ids = Project . visible . where ( " user_id = #{ User . current . id } and name like ' #{ condition } ' " )
2015-12-09 10:29:54 +08:00
@first = params [ :first ] . to_i
respond_to do | format |
format . js
end
2015-12-08 17:37:15 +08:00
end
2015-12-11 14:57:09 +08:00
#查找课程的学生
def search_course_students
name = " "
unless params [ :name ] . nil?
name = params [ :name ]
end
2015-12-17 15:28:28 +08:00
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
2015-12-11 14:57:09 +08:00
all_students = User . where ( " id in #{ all_student_ids } " )
2016-06-28 15:57:56 +08:00
student_work_id = @homework . student_work_projects . where ( " user_id=? and student_work_id is not null " , User . current . id ) . first . nil? ? - 1 : @homework . student_work_projects . where ( " user_id=? " , User . current . id ) . first . student_work_id
2016-09-06 17:51:13 +08:00
@commit_student_ids = @homework . student_works . where ( " id != #{ student_work_id } " ) . map { | student | student . user_id }
2015-12-11 14:57:09 +08:00
@users = searchstudent_by_name all_students , name
respond_to do | format |
format . js
end
end
2015-12-09 16:33:48 +08:00
def cancel_relate_project
2016-09-06 15:07:43 +08:00
@work = @homework . student_works . where ( " user_id = #{ User . current . id } and work_status = 0 " ) . first
if @work
@work . update_column ( 'project_id' , 0 )
end
2015-12-09 16:33:48 +08:00
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
2016-09-19 17:14:14 +08:00
@hw_status = params [ :hw_status ] . to_i
2016-09-21 10:34:38 +08:00
@is_teacher = User . current . allowed_to? ( :as_teacher , @homework . course ) || User . current . admin?
2015-12-09 16:33:48 +08:00
respond_to do | format |
format . js
end
end
end
2016-06-14 17:36:12 +08:00
def get_user_infor
req = Hash . new ( false )
user = User . where ( " id = #{ params [ :user_id ] . to_i } " ) . first
if user
req [ :id ] = user . id
req [ :name ] = user . show_name
req [ :student_id ] = user . user_extensions . student_id
req [ :valid ] = true
else
req [ :valid ] = false
end
render :json = > req
end
2015-05-19 17:12:43 +08:00
private
2015-12-11 14:57:09 +08:00
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
2015-11-23 17:08:17 +08:00
def hsd_committed_work? ( user , homework )
2016-07-08 13:17:14 +08:00
sw = StudentWork . where ( " user_id =? and homework_common_id =? and work_status != 0 " , user , homework ) . first
2015-11-23 17:08:17 +08:00
sw . nil? ? result = false : result = true
result
end
2015-05-19 17:12:43 +08:00
#获取作业
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
2015-09-17 20:20:20 +08:00
render_403 unless User . current . allowed_to? ( :as_teacher , @course ) || User . current . admin?
2015-06-30 15:30:51 +08:00
end
2016-02-25 16:18:24 +08:00
def is_logged
redirect_to signin_path unless User . current . logged?
end
2015-05-28 09:46:35 +08:00
#根据条件过滤作业结果
def search_homework_member homeworks , name
2015-09-17 14:16:13 +08:00
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
2015-05-28 09:46:35 +08:00
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
2016-06-24 10:50:40 +08:00
if @homework . homework_type == 1 #普通作业
2016-06-23 17:34:46 +08:00
if @homework . anonymous_comment == 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_homework_des ) ,
l ( :excel_t_score ) , l ( :excel_ta_score ) , l ( :excel_n_score ) , l ( :excel_a_penalty ) , l ( :excel_l_penalty ) , l ( :excel_f_score ) , l ( :excel_commit_time ) ] )
else
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_homework_des ) ,
l ( :excel_t_score ) , l ( :excel_ta_score ) , l ( :excel_l_penalty ) , l ( :excel_f_score ) , l ( :excel_commit_time ) ] )
end
2015-07-23 10:33:54 +08:00
count_row = 1
items . each do | homework |
sheet1 [ count_row , 0 ] = homework . user . id
2016-06-24 10:50:40 +08:00
sheet1 [ count_row , 1 ] = homework . user . show_name
2015-07-23 10:33:54 +08:00
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
2016-07-08 15:02:50 +08:00
sheet1 [ count_row , 6 ] = strip_html homework . description if ! homework . description . nil?
2016-05-11 14:51:49 +08:00
sheet1 [ count_row , 7 ] = homework . teacher_score . nil? ? l ( :label_without_score ) : homework . teacher_score . round ( 2 )
sheet1 [ count_row , 8 ] = homework . teaching_asistant_score . nil? ? l ( :label_without_score ) : homework . teaching_asistant_score . round ( 2 )
2016-06-23 17:34:46 +08:00
if @homework . anonymous_comment == 0
sheet1 [ count_row , 9 ] = homework . student_score . nil? ? l ( :label_without_score ) : homework . student_score . round ( 2 )
2016-06-24 18:23:00 +08:00
sheet1 [ count_row , 10 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . absence_penalty
sheet1 [ count_row , 11 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . late_penalty
2016-06-23 17:34:46 +08:00
sheet1 [ count_row , 12 ] = homework . respond_to? ( " score " ) ? homework . score . nil? ? l ( :label_without_score ) : homework . score . round ( 2 ) : l ( :label_without_score )
sheet1 [ count_row , 13 ] = format_time ( homework . created_at )
else
2016-06-24 18:23:00 +08:00
sheet1 [ count_row , 9 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . late_penalty
2016-06-23 17:34:46 +08:00
sheet1 [ count_row , 10 ] = homework . respond_to? ( " score " ) ? homework . score . nil? ? l ( :label_without_score ) : homework . score . round ( 2 ) : l ( :label_without_score )
sheet1 [ count_row , 11 ] = format_time ( homework . created_at )
end
2015-07-23 10:33:54 +08:00
count_row += 1
end
elsif @homework . homework_type == 2 #编程作业
2016-06-23 17:34:46 +08:00
if @homework . anonymous_comment == 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_homework_des ) ,
l ( :excel_t_score ) , l ( :excel_ta_score ) , l ( :excel_s_score ) , l ( :excel_n_score ) , l ( :excel_a_penalty ) , l ( :excel_l_penalty ) , l ( :excel_f_score ) , l ( :excel_commit_time ) ] )
else
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_homework_des ) ,
l ( :excel_t_score ) , l ( :excel_ta_score ) , l ( :excel_s_score ) , l ( :excel_l_penalty ) , l ( :excel_f_score ) , l ( :excel_commit_time ) ] )
end
2015-07-23 10:33:54 +08:00
count_row = 1
items . each do | homework |
sheet1 [ count_row , 0 ] = homework . user . id
2016-06-24 10:50:40 +08:00
sheet1 [ count_row , 1 ] = homework . user . show_name
2015-07-23 10:33:54 +08:00
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
2016-05-04 16:43:37 +08:00
sheet1 [ count_row , 6 ] = homework . description
2016-05-11 14:51:49 +08:00
sheet1 [ count_row , 7 ] = homework . teacher_score . nil? ? l ( :label_without_score ) : homework . teacher_score . round ( 2 )
sheet1 [ count_row , 8 ] = homework . teaching_asistant_score . nil? ? l ( :label_without_score ) : homework . teaching_asistant_score . round ( 2 )
sheet1 [ count_row , 9 ] = homework . system_score . nil? ? l ( :label_without_score ) : homework . system_score . round ( 2 )
2016-06-23 17:34:46 +08:00
if @homework . anonymous_comment == 0
sheet1 [ count_row , 10 ] = homework . student_score . nil? ? l ( :label_without_score ) : homework . student_score . round ( 2 )
2016-06-24 18:23:00 +08:00
sheet1 [ count_row , 11 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . absence_penalty
sheet1 [ count_row , 12 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . late_penalty
2016-06-23 17:34:46 +08:00
sheet1 [ count_row , 13 ] = homework . respond_to? ( " score " ) ? homework . score . nil? ? l ( :label_without_score ) : homework . score . round ( 2 ) : l ( :label_without_score )
sheet1 [ count_row , 14 ] = format_time ( homework . created_at )
else
2016-06-24 18:23:00 +08:00
sheet1 [ count_row , 10 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . late_penalty
2016-06-23 17:34:46 +08:00
sheet1 [ count_row , 11 ] = homework . respond_to? ( " score " ) ? homework . score . nil? ? l ( :label_without_score ) : homework . score . round ( 2 ) : l ( :label_without_score )
sheet1 [ count_row , 12 ] = format_time ( homework . created_at )
end
2015-07-23 10:33:54 +08:00
count_row += 1
end
2016-06-24 10:50:40 +08:00
elsif @homework . homework_type == 3 #分组作业
if @homework . anonymous_comment == 0
sheet1 . row ( 0 ) . concat ( [ l ( :excel_group_member ) , l ( :excel_homework_name ) , l ( :excel_homework_project ) , l ( :excel_homework_des ) ,
l ( :excel_t_score ) , l ( :excel_ta_score ) , l ( :excel_n_score ) , l ( :excel_a_penalty ) , l ( :excel_l_penalty ) , l ( :excel_f_score ) , l ( :excel_commit_time ) ] )
else
sheet1 . row ( 0 ) . concat ( [ l ( :excel_group_member ) , l ( :excel_homework_name ) , l ( :excel_homework_project ) , l ( :excel_homework_des ) ,
l ( :excel_t_score ) , l ( :excel_ta_score ) , l ( :excel_l_penalty ) , l ( :excel_f_score ) , l ( :excel_commit_time ) ] )
end
count_row = 1
items . each do | homework |
sheet1 [ count_row , 0 ] = get_group_member_names homework
sheet1 [ count_row , 1 ] = homework . name
sheet1 [ count_row , 2 ] = ( homework . project_id == 0 || homework . project_id . nil? ) ? l ( :excel_no_project ) : homework . project . name
sheet1 [ count_row , 3 ] = strip_html homework . description
sheet1 [ count_row , 4 ] = homework . teacher_score . nil? ? l ( :label_without_score ) : homework . teacher_score . round ( 2 )
sheet1 [ count_row , 5 ] = homework . teaching_asistant_score . nil? ? l ( :label_without_score ) : homework . teaching_asistant_score . round ( 2 )
if @homework . anonymous_comment == 0
sheet1 [ count_row , 6 ] = homework . student_score . nil? ? l ( :label_without_score ) : homework . student_score . round ( 2 )
2016-06-24 18:23:00 +08:00
sheet1 [ count_row , 7 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . absence_penalty
sheet1 [ count_row , 8 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . late_penalty
2016-06-24 10:50:40 +08:00
sheet1 [ count_row , 9 ] = homework . respond_to? ( " score " ) ? homework . score . nil? ? l ( :label_without_score ) : homework . score . round ( 2 ) : l ( :label_without_score )
sheet1 [ count_row , 10 ] = format_time ( homework . created_at )
else
2016-06-24 18:23:00 +08:00
sheet1 [ count_row , 6 ] = ( @homework . teacher_priority == 1 && ! homework . teacher_score . nil? ) ? 0 : homework . late_penalty
2016-06-24 10:50:40 +08:00
sheet1 [ count_row , 7 ] = homework . respond_to? ( " score " ) ? homework . score . nil? ? l ( :label_without_score ) : homework . score . round ( 2 ) : l ( :label_without_score )
sheet1 [ count_row , 8 ] = format_time ( homework . created_at )
end
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 )
2016-09-01 14:21:19 +08:00
student_work = StudentWork . where ( homework_common_id : @homework . id , user_id : User . current . id , is_test : is_test ) . first
2015-08-28 12:04:49 +08:00
if student_work . nil?
@homework . student_works . build (
2016-05-03 11:24:16 +08:00
name : params [ :title ] ,
description : params [ :src ] ,
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
2016-09-01 14:21:19 +08:00
student_work = StudentWork . where ( homework_common_id : @homework . id , user_id : User . current . id , is_test : is_test ) . 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
2016-05-03 11:24:16 +08:00
}
2015-08-28 12:04:49 +08:00
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 )
2016-03-25 14:43:51 +08:00
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 )
2015-08-28 12:04:49 +08:00
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
#成绩计算
2015-09-17 14:16:13 +08:00
def set_final_score homework , student_work
2015-09-17 11:41:24 +08:00
if homework && homework . homework_detail_manual
2015-12-11 19:13:34 +08:00
if homework . homework_type != 2 #匿评作业
2015-09-17 11:41:24 +08:00
if homework . teacher_priority == 1 #教师优先
if student_work . teacher_score
student_work . final_score = student_work . teacher_score
2016-04-27 16:21:35 +08:00
student_work . work_score = student_work . teacher_score
2015-09-17 11:41:24 +08:00
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
2016-05-03 15:12:56 +08:00
if student_work . final_score
score = student_work . final_score - student_work . absence_penalty - student_work . late_penalty
student_work . work_score = format ( " %.2f " , ( score < 0 ? 0 : score ) . to_f ) if score
else
student_work . work_score = nil
end
2015-09-17 11:41:24 +08:00
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
2016-05-03 15:12:56 +08:00
if student_work . final_score
score = student_work . final_score - student_work . absence_penalty - student_work . late_penalty
student_work . work_score = format ( " %.2f " , ( score < 0 ? 0 : score ) . to_f ) if score
else
student_work . work_score = nil
end
2015-09-17 11:41:24 +08:00
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
2016-04-27 16:21:35 +08:00
student_work . work_score = student_work . teacher_score
2015-09-17 11:41:24 +08:00
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
2015-09-18 09:32:25 +08:00
final_sy_score = BigDecimal . new ( " #{ student_work . system_score || 0 } " ) * BigDecimal . new ( " #{ ta_proportion } " )
2015-09-17 11:41:24 +08:00
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
2015-09-18 09:32:25 +08:00
final_sy_score = BigDecimal . new ( " #{ student_work . system_score || 0 } " ) * BigDecimal . new ( " #{ ta_proportion } " )
2015-09-17 11:41:24 +08:00
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
2015-09-18 09:32:25 +08:00
final_sy_score = BigDecimal . new ( " #{ student_work . system_score || 0 } " ) * BigDecimal . new ( " #{ homework . homework_detail_programing . ta_proportion } " )
2015-09-17 11:41:24 +08:00
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
2016-05-03 15:12:56 +08:00
if student_work . final_score
score = student_work . final_score - student_work . absence_penalty - student_work . late_penalty
student_work . work_score = format ( " %.2f " , ( score < 0 ? 0 : score ) . to_f ) if score
else
student_work . work_score = nil
end
2015-09-17 11:41:24 +08:00
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
2015-09-18 09:32:25 +08:00
final_sy_score = BigDecimal . new ( " #{ student_work . system_score || 0 } " ) * BigDecimal . new ( " #{ ta_proportion } " )
2015-09-17 11:41:24 +08:00
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
2015-09-18 09:32:25 +08:00
final_sy_score = BigDecimal . new ( " #{ student_work . system_score || 0 } " ) * BigDecimal . new ( " #{ ta_proportion } " )
2015-09-17 11:41:24 +08:00
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
2015-09-18 09:32:25 +08:00
final_sy_score = BigDecimal . new ( " #{ student_work . system_score || 0 } " ) * BigDecimal . new ( " #{ homework . homework_detail_programing . ta_proportion } " )
2015-09-17 11:41:24 +08:00
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
2016-05-03 15:12:56 +08:00
if student_work . final_score
score = student_work . final_score - student_work . absence_penalty - student_work . late_penalty
student_work . work_score = format ( " %.2f " , ( score < 0 ? 0 : score ) . to_f ) if score
else
student_work . work_score = nil
end
2015-09-17 11:41:24 +08:00
end
end
end
end
2016-02-25 09:26:43 +08:00
end