From 89e0f98034ac9acca951543600c86507cf6eaaf6 Mon Sep 17 00:00:00 2001 From: z9hang Date: Wed, 21 Jan 2015 15:29:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=8C=BF=E8=AF=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/homeworks.rb | 19 +++++++++++++++++ app/helpers/api_helper.rb | 6 ++++++ app/services/homework_service.rb | 36 +++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/app/api/mobile/apis/homeworks.rb b/app/api/mobile/apis/homeworks.rb index b1e38ad22..dd6708b22 100644 --- a/app/api/mobile/apis/homeworks.rb +++ b/app/api/mobile/apis/homeworks.rb @@ -31,6 +31,25 @@ module Mobile present :status, 0 end + desc "启动匿评" + params do + requires :token, type: String + end + post ':id/alert_homework_anonymous_comment' do + statue = Homeworks.get_service.start_anonymous_comment params,current_user.nil? ? User.find(2):current_user + messages = "" + case statue + when 1 + messages = "启动成功" + when 2 + messages = "启动失败,作业总数大于等于2份时才能启动匿评" + when 3 + messages = "已开启匿评,请务重复开启" + end + present :data,messages + present :status, statue + end + end end end diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb index 2b85d2be4..78f954ab4 100644 --- a/app/helpers/api_helper.rb +++ b/app/helpers/api_helper.rb @@ -19,4 +19,10 @@ module ApiHelper location << (user.user_extensions.location_city || '') location end + + + def get_assigned_homeworks(homeworks, n, index) + homeworks += homeworks + homeworks[index + 1 .. index + n] + end end \ No newline at end of file diff --git a/app/services/homework_service.rb b/app/services/homework_service.rb index 871ae9801..a659b167d 100644 --- a/app/services/homework_service.rb +++ b/app/services/homework_service.rb @@ -4,6 +4,7 @@ class HomeworkService include AttachmentsHelper include ApplicationHelper include WordsHelper + include ApiHelper # 作业详情(老师才显示启动匿评,学生不显示 ) # many_times 第几次(作业) @@ -27,7 +28,7 @@ class HomeworkService :description => description, :homework_state => state,:open_anonymous_evaluation => open_anonymous_evaluation} end - # 启动作业匿评 + # 启动作业匿评前提示信息 def alert_homework_anonymous_comment params @bid = Bid.find params[:id] @course = @bid.courses.first @@ -52,6 +53,39 @@ class HomeworkService @percent = format("%.2f",(@cur_size.to_f / ( @totle_size == 0 ? 1 : @totle_size)) * 100) [@bid,@totle_size,@cur_size,@percent] end + + #启动匿评 + #statue 1:启动成功,2:启动失败,作业总数大于等于2份时才能启动匿评,3:已开启匿评,请务重复开启 + def start_anonymous_comment params,current_user + @bid = Bid.find(params[:id]) + @course = @bid.courses.first + unless is_course_teacher(current_user,@course) || current_user.admin? + @statue = 4 + raise '403' + end + if(@bid.comment_status == 0) + homeworks = @bid.homeworks + if(homeworks && homeworks.size >= 2) + homeworks.each_with_index do |homework, index| + user = homework.user + n = @bid.evaluation_num + n = n < homeworks.size ? n : homeworks.size - 1 + assigned_homeworks = get_assigned_homeworks(homeworks, n, index) + assigned_homeworks.each do |h| + @homework_evaluation = HomeworkEvaluation.new(user_id: user.id, homework_attach_id: h.id) + @homework_evaluation.save + end + end + @bid.update_column('comment_status', 1) + @statue = 1 + else + @statue = 2 + end + else + @statue = 3 + end + @statue + end # 匿评作品详情 # attachs 该作品的所有附件