2015-05-19 17:12:43 +08:00
class HomeworkCommonController < ApplicationController
2015-07-09 15:50:04 +08:00
require 'net/http'
2015-07-16 16:15:47 +08:00
require 'json'
2015-05-19 17:12:43 +08:00
layout " base_courses "
2015-06-16 14:45:57 +08:00
before_filter :find_course , :only = > [ :index , :new , :create , :next_step ]
2015-05-28 14:13:47 +08:00
before_filter :find_homework , :only = > [ :edit , :update , :alert_anonymous_comment , :start_anonymous_comment , :stop_anonymous_comment , :destroy ]
2015-05-20 17:53:01 +08:00
before_filter :teacher_of_course , :only = > [ :new , :create , :edit , :update , :destroy , :start_anonymous_comment , :stop_anonymous_comment , :alert_anonymous_comment ]
2015-06-18 09:31:31 +08:00
before_filter :member_of_course , :only = > [ :index ]
2015-05-19 17:12:43 +08:00
def index
2015-06-04 11:19:37 +08:00
homeworks = @course . homework_commons . order ( " created_at desc " )
2015-05-19 17:12:43 +08:00
@is_teacher = User . current . logged? && ( User . current . admin? || User . current . allowed_to? ( :as_teacher , @course ) )
@is_student = User . current . logged? && ( User . current . admin? || ( User . current . member_of_course? ( @course ) && ! @is_teacher ) )
2015-05-26 09:07:32 +08:00
@homeworks = paginateHelper homeworks , 20
2015-05-19 17:12:43 +08:00
respond_to do | format |
format . html
end
end
def new
2015-07-28 11:24:30 +08:00
# @homework_type = "1"
#
# @homework = HomeworkCommon.new
# @homework.safe_attributes = params[:homework_common]
# @homework.late_penalty = 0
# @homework.end_time = (Time.now + 3600 * 24).strftime('%Y-%m-%d')
# @homework.publish_time = Time.now.strftime('%Y-%m-%d')
#
# if @homework_type == "1"
# #匿评作业相关属性
# @homework_detail_manual = HomeworkDetailManual.new
# @homework_detail_manual.ta_proportion = 0.6
# @homework_detail_manual.absence_penalty = 0
# @homework_detail_manual.evaluation_num = 3
# @homework_detail_manual.evaluation_start = Time.now.strftime('%Y-%m-%d')
# @homework_detail_manual.evaluation_end = (Time.now + 3600 * 24).strftime('%Y-%m-%d')
# @homework.homework_detail_manual = @homework_detail_manual
# elsif @homework_type == "2"
# #编程作业相关属性
# @homework_detail_programing = HomeworkDetailPrograming.new
# @homework.homework_detail_programing = @homework_detail_programing
# end
2015-07-24 09:00:08 +08:00
respond_to do | format |
format . html
end
2015-06-16 14:45:57 +08:00
end
#新建作业下一步
def next_step
@homework_type = params [ :homework_common_type ]
2015-05-20 15:05:35 +08:00
@homework = HomeworkCommon . new
@homework . safe_attributes = params [ :homework_common ]
2015-06-04 10:32:59 +08:00
@homework . late_penalty = 0
2015-05-20 15:05:35 +08:00
@homework . end_time = ( Time . now + 3600 * 24 ) . strftime ( '%Y-%m-%d' )
@homework . publish_time = Time . now . strftime ( '%Y-%m-%d' )
2015-05-20 16:49:19 +08:00
2015-06-16 17:55:07 +08:00
if @homework_type == " 1 "
#匿评作业相关属性
@homework_detail_manual = HomeworkDetailManual . new
@homework_detail_manual . ta_proportion = 0 . 6
@homework_detail_manual . absence_penalty = 0
@homework_detail_manual . evaluation_num = 3
@homework_detail_manual . evaluation_start = Time . now . strftime ( '%Y-%m-%d' )
@homework_detail_manual . evaluation_end = ( Time . now + 3600 * 24 ) . strftime ( '%Y-%m-%d' )
@homework . homework_detail_manual = @homework_detail_manual
elsif @homework_type == " 2 "
#编程作业相关属性
@homework_detail_programing = HomeworkDetailPrograming . new
@homework . homework_detail_programing = @homework_detail_programing
end
2015-05-19 17:12:43 +08:00
respond_to do | format |
format . html
end
end
def create
2015-05-20 16:49:19 +08:00
if params [ :homework_common ]
homework = HomeworkCommon . new
homework . name = params [ :homework_common ] [ :name ]
homework . description = params [ :homework_common ] [ :description ]
homework . end_time = params [ :homework_common ] [ :end_time ]
homework . publish_time = params [ :homework_common ] [ :publish_time ]
homework . homework_type = params [ :homework_common ] [ :homework_type ]
homework . late_penalty = params [ :late_penalty ]
homework . user_id = User . current . id
homework . course_id = @course . id
2015-05-21 15:52:27 +08:00
homework . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( homework )
2015-06-16 17:55:07 +08:00
if homework . homework_type == 2
homework_detail_programing = HomeworkDetailPrograming . new
2015-07-21 15:09:52 +08:00
homework_detail_programing . language = params [ :language ]
2015-06-16 17:55:07 +08:00
homework_detail_programing . standard_code = params [ :standard_code ]
2015-07-20 16:37:40 +08:00
homework_detail_programing . ta_proportion = params [ :ta_proportion ] || 0 . 6
2015-07-09 15:50:04 +08:00
question = { title : homework . name , content : homework . description }
2015-07-15 14:57:47 +08:00
question [ :input ] = [ ]
question [ :output ] = [ ]
2015-06-16 17:55:07 +08:00
if params [ :input ] && params [ :output ]
params [ :input ] . each do | k , v |
2015-06-17 09:58:01 +08:00
if params [ :output ] . include? k
homework_test = HomeworkTest . new
homework_test . input = v
homework_test . output = params [ :output ] [ k ]
homework . homework_tests << homework_test
2015-07-15 14:57:47 +08:00
question [ :input ] << homework_test . input
question [ :output ] << homework_test . output
2015-06-16 17:55:07 +08:00
end
end
end
2015-07-16 16:15:47 +08:00
# uri = URI('http://test.gitlab.trustie.net/api/questions.json')
# req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
# req.body = question.to_json
# res = Net::HTTP.start(uri.hostname, uri.port) do |http|
# http.request(req)
# end
2015-07-16 17:54:42 +08:00
uri = URI ( 'http://192.168.80.21:8080/api/questions.json' )
2015-07-16 16:15:47 +08:00
body = question . 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
2015-07-15 14:57:47 +08:00
result = JSON . parse ( res . body )
homework_detail_programing . question_id = result [ " id " ] if result [ " status " ] && result [ " status " ] == 0
2015-06-16 17:55:07 +08:00
homework . homework_detail_programing = homework_detail_programing
else
#匿评作业相关属性
homework_detail_manual = HomeworkDetailManual . new
homework_detail_manual . ta_proportion = params [ :ta_proportion ] || 0 . 6
homework_detail_manual . comment_status = 1
homework_detail_manual . evaluation_start = params [ :evaluation_start ]
homework_detail_manual . evaluation_end = params [ :evaluation_end ]
homework_detail_manual . evaluation_num = params [ :evaluation_num ]
homework_detail_manual . absence_penalty = params [ :absence_penalty ]
homework . homework_detail_manual = homework_detail_manual
end
2015-05-20 16:49:19 +08:00
if homework . save
2015-07-24 09:47:20 +08:00
homework_detail_programing . save if homework_detail_programing
homework_detail_manual . save if homework_detail_manual
2015-05-20 16:49:19 +08:00
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_successful_create )
redirect_to homework_common_index_path ( :course = > @course . id )
}
end
return
end
end
2015-05-19 17:12:43 +08:00
2015-05-20 16:49:19 +08:00
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_failed_create )
redirect_to new_homework_common_path ( :course = > @course . id )
}
end
2015-05-19 17:12:43 +08:00
end
def edit
2015-05-20 16:49:19 +08:00
respond_to do | format |
format . html
end
2015-05-19 17:12:43 +08:00
end
def update
2015-05-20 17:01:08 +08:00
@homework . name = params [ :homework_common ] [ :name ]
@homework . description = params [ :homework_common ] [ :description ]
@homework . end_time = params [ :homework_common ] [ :end_time ]
@homework . publish_time = params [ :homework_common ] [ :publish_time ]
2015-07-18 11:38:00 +08:00
@homework . homework_type = params [ :homework_common ] [ :homework_type ] if params [ :homework_common ] [ :homework_type ]
2015-07-03 10:47:10 +08:00
unless @homework . late_penalty == params [ :late_penalty ]
2015-07-03 15:43:17 +08:00
@homework . student_works . where ( " created_at > ' #{ @homework . end_time } 23:59:59' " ) . each do | student_work |
2015-07-03 10:47:10 +08:00
student_work . late_penalty = params [ :late_penalty ]
student_work . save
end
@homework . late_penalty = params [ :late_penalty ]
end
2015-07-06 10:44:27 +08:00
# @homework.course_id = @course.id
2015-05-19 17:12:43 +08:00
2015-05-20 17:01:08 +08:00
#匿评作业相关属性
2015-07-06 16:28:33 +08:00
if @homework . homework_type == 1 && @homework_detail_manual
2015-07-03 10:47:10 +08:00
@homework_detail_manual . ta_proportion = params [ :ta_proportion ] || 0 . 6
@homework_detail_manual . evaluation_start = params [ :evaluation_start ]
@homework_detail_manual . evaluation_end = params [ :evaluation_end ]
@homework_detail_manual . evaluation_num = params [ :evaluation_num ]
unless @homework_detail_manual . absence_penalty == params [ :absence_penalty ]
if @homework_detail_manual . comment_status == 3 #当前作业处于匿评结束状态,修改缺评扣分才会修改每个作品应扣分的值
2015-07-03 11:41:53 +08:00
work_ids = " ( " + @homework . student_works . map ( & :id ) . join ( " , " ) + " ) "
@homework . student_works . each do | student_work |
absence_penalty_count = student_work . user . student_works_evaluation_distributions . where ( " student_work_id IN #{ work_ids } " ) . count - student_work . user . student_works_scores . where ( " student_work_id IN #{ work_ids } " ) . count
student_work . absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * @homework_detail_manual . absence_penalty : 0
2015-07-03 10:47:10 +08:00
student_work . save
end
end
@homework_detail_manual . absence_penalty = params [ :absence_penalty ]
end
2015-07-06 16:28:33 +08:00
elsif @homework . homework_type == 0 #普通作业, 缺评扣分为0分, 每个作品的缺评扣分改为0分, 防止某些作业在结束匿评之后改为普通作业
2015-07-03 10:47:10 +08:00
@homework . student_works . where ( " absence_penalty != 0 " ) . each do | student_work |
student_work . late_penalty = 0
student_work . save
end
2015-07-06 16:28:33 +08:00
@homework_detail_manual . absence_penalty = 0 if @homework_detail_manual
2015-07-03 10:47:10 +08:00
end
2015-06-17 10:29:41 +08:00
2015-07-06 16:28:33 +08:00
if @homework . homework_type == 2 && @homework_detail_programing #编程作业
2015-07-22 17:00:41 +08:00
@homework_detail_programing . language = params [ :language ]
2015-07-06 10:44:27 +08:00
@homework_detail_programing . standard_code = params [ :standard_code ]
2015-07-20 16:37:40 +08:00
@homework_detail_programing . ta_proportion = params [ :ta_proportion ] || 0 . 6
2015-07-06 10:44:27 +08:00
homework_tests = @homework . homework_tests
#需要删除的测试
ids = homework_tests . map ( & :id ) - params [ :input ] . keys . map ( & :to_i )
ids . each do | id |
homework_test = HomeworkTest . find id
homework_test . destroy if homework_test
end
2015-06-17 10:29:41 +08:00
if params [ :input ] && params [ :output ]
params [ :input ] . each do | k , v |
if params [ :output ] . include? k
homework_test = HomeworkTest . find_by_id k
if homework_test #已存在的测试,修改
homework_test . input = v
homework_test . output = params [ :output ] [ k ]
else #不存在的测试,增加
homework_test = HomeworkTest . new
homework_test . input = v
homework_test . output = params [ :output ] [ k ]
homework_test . homework_common = @homework
end
homework_test . save
end
end
end
2015-07-22 17:47:48 +08:00
#发送修改作业的请求
question = { title : @homework . name , content : @homework . description }
question [ :input ] = [ ]
question [ :output ] = [ ]
@homework . homework_tests . each do | test |
question [ :input ] << test . input
question [ :output ] << test . output
end
uri = URI ( " http://192.168.80.21:8080/api/questions/ #{ @homework_detail_programing . question_id } .json " )
body = question . to_json
res = Net :: HTTP . new ( uri . host , uri . port ) . start do | client |
request = Net :: HTTP :: Put . new ( uri . path )
request . body = body
request [ " Content-Type " ] = " application/json "
client . request ( request )
end
result = JSON . parse ( res . body )
2015-06-17 10:29:41 +08:00
end
2015-05-20 17:01:08 +08:00
2015-06-04 09:57:30 +08:00
@homework . save_attachments ( params [ :attachments ] )
render_attachment_warning_if_needed ( @homework )
2015-06-17 10:29:41 +08:00
if @homework . save
@homework_detail_manual . save if @homework_detail_manual
@homework_detail_programing . save if @homework_detail_programing
2015-05-20 17:01:08 +08:00
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_successful_edit )
redirect_to homework_common_index_path ( :course = > @course . id )
}
end
return
else
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_failed_edit )
redirect_to edit_homework_common_path ( @homework )
}
end
end
2015-05-19 17:12:43 +08:00
end
def destroy
2015-05-28 14:13:47 +08:00
if @homework . destroy
respond_to do | format |
format . html { redirect_to homework_common_index_path ( :course = > @course . id ) }
end
end
2015-05-19 17:12:43 +08:00
end
#开启匿评
2015-05-26 10:22:16 +08:00
#statue 1:启动成功, 2: 启动失败, 作业总数大于等于2份时才能启动匿评, 3:已开启匿评,请务重复开启,4:没有开启匿评的权限
2015-05-19 17:12:43 +08:00
def start_anonymous_comment
2015-05-26 10:22:16 +08:00
@statue = 4 and return unless User . current . admin? || User . current . allowed_to? ( :as_teacher , @course )
2015-06-30 09:47:54 +08:00
@statue = 5 and return if Time . parse ( @homework . end_time . to_s ) . strftime ( " %Y-%m-%d " ) > = Time . now . strftime ( " %Y-%m-%d " )
2015-05-26 10:22:16 +08:00
if @homework_detail_manual . comment_status == 1
student_works = @homework . student_works
if student_works && student_works . size > = 2
student_works . each_with_index do | work , index |
user = work . user
n = @homework_detail_manual . evaluation_num
n = n < student_works . size ? n : student_works . size - 1
assigned_homeworks = get_assigned_homeworks ( student_works , n , index )
assigned_homeworks . each do | h |
student_works_evaluation_distributions = StudentWorksEvaluationDistribution . new ( user_id : user . id , student_work_id : h . id )
student_works_evaluation_distributions . save
end
end
@homework_detail_manual . update_column ( 'comment_status' , 2 )
@statue = 1
else
@statue = 2
end
else
@statue = 3
end
2015-05-19 17:12:43 +08:00
end
#关闭匿评
def stop_anonymous_comment
2015-05-26 11:24:48 +08:00
@homework_detail_manual . update_column ( 'comment_status' , 3 )
2015-07-02 16:40:12 +08:00
work_ids = " ( " + @homework . student_works . map ( & :id ) . join ( " , " ) + " ) "
@homework . student_works . each do | student_work |
absence_penalty_count = student_work . user . student_works_evaluation_distributions . where ( " student_work_id IN #{ work_ids } " ) . count - student_work . user . student_works_scores . where ( " student_work_id IN #{ work_ids } " ) . count
student_work . absence_penalty = absence_penalty_count > 0 ? absence_penalty_count * @homework_detail_manual . absence_penalty : 0
student_work . save
end
2015-05-26 11:24:48 +08:00
respond_to do | format |
format . js
end
2015-05-19 17:12:43 +08:00
end
#提示
def alert_anonymous_comment
2015-05-26 10:00:37 +08:00
@cur_size = 0
@totle_size = 0
if @homework_detail_manual . comment_status == 1
@totle_size = @course . student . count
@cur_size = @homework . student_works . size
elsif @homework_detail_manual . comment_status == 2
@homework . student_works . map { | work | @totle_size += work . student_works_evaluation_distributions . count }
@cur_size = 0
2015-05-29 10:22:16 +08:00
@homework . student_works . map { | work | @cur_size += work . student_works_scores . where ( :reviewer_role = > 3 ) . count }
2015-05-26 10:00:37 +08:00
end
@percent = format ( " %.2f " , ( @cur_size . to_f / ( @totle_size == 0 ? 1 : @totle_size ) ) * 100 )
2015-05-19 17:12:43 +08:00
respond_to do | format |
format . js
end
end
private
#获取课程
def find_course
@course = Course . find params [ :course ]
rescue
render_404
end
#获取作业
def find_homework
@homework = HomeworkCommon . find params [ :id ]
2015-05-20 17:01:08 +08:00
@homework_detail_manual = @homework . homework_detail_manual
2015-07-06 16:28:33 +08:00
@homework_detail_programing = @homework . homework_detail_programing
2015-05-19 17:12:43 +08:00
@course = @homework . course
rescue
render_404
end
2015-05-20 17:53:01 +08:00
#是不是课程的老师
def teacher_of_course
2015-06-04 17:24:50 +08:00
render_403 unless User . current . allowed_to? ( :as_teacher , @course ) || User . current . admin?
2015-05-20 17:53:01 +08:00
end
2015-05-26 10:22:16 +08:00
2015-06-18 09:31:31 +08:00
#当前用户是不是课程的成员
def member_of_course
2015-06-25 14:53:33 +08:00
render_403 unless @course . is_public || User . current . member_of_course? ( @course ) || User . current . admin?
2015-06-18 09:31:31 +08:00
end
2015-05-26 10:22:16 +08:00
def get_assigned_homeworks ( student_works , n , index )
student_works += student_works
student_works [ index + 1 .. index + n ]
end
2015-05-19 17:12:43 +08:00
end