From 9708c50899dc5dc6f1594295a8568613f9cd7917 Mon Sep 17 00:00:00 2001 From: z9hang Date: Tue, 3 Feb 2015 15:10:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BD=9C=E5=93=81=E8=AF=84?= =?UTF-8?q?=E5=88=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/homeworks.rb | 26 ++++++++++++++++++ app/services/homework_service.rb | 46 +++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/app/api/mobile/apis/homeworks.rb b/app/api/mobile/apis/homeworks.rb index cba295064..7a8c87987 100644 --- a/app/api/mobile/apis/homeworks.rb +++ b/app/api/mobile/apis/homeworks.rb @@ -70,6 +70,32 @@ module Mobile present :status, 0 end + desc "作品打分" + params do + requires :token, type: String + requires :is_teacher, type: String,desc: '是否为教师(匿评作品详情返回的结果中可获取此参数的值)' + requires :is_anonymous_comments, type: String, desc: '是否为匿评(匿评作品详情返回的结果中可获取此参数的值)' + optional :stars_value, type: Integer,desc: '用户给出的评分' + optional :cur_page,type: Integer,desc: '匿评作品详情返回的结果中可获取此参数的值' + optional :cur_type, type: Integer,desc: '匿评作品详情返回的结果中可获取此参数的值' + optional :user_message, type: String, desc: '用户评论' + end + + post ':homework_id/scoring' do + cs_params = { + new_form: params.reject{|k,v| [:token,:is_teacher,:is_anonymous_comments,:stars_value,:cur_page,:cur_type,:homework_id].include?(k)}, + token: params[:token], + is_teacher: params[:is_teacher], + is_anonymous_comments: params[:is_anonymous_comments], + stars_value: params[:stars_value], + cur_page: params[:cur_page], + cur_type: params[:cur_type], + homework_id: params[:homework_id] + } + Homeworks.get_service.add_score_and_jour cs_params,current_user + present :status, 0 + end + end end diff --git a/app/services/homework_service.rb b/app/services/homework_service.rb index a2c063e15..abf89229c 100644 --- a/app/services/homework_service.rb +++ b/app/services/homework_service.rb @@ -141,16 +141,56 @@ class HomeworkService end #作品打分/留言 - def add_score_and_jour params + def add_score_and_jour params,current_user @is_teacher,@is_anonymous_comments,@m_score = params[:is_teacher]=="true",params[:is_anonymous_comments]=="true",params[:stars_value] @cur_page,@cur_type = params[:cur_page] || 1,params[:cur_type] || 5 @homework = HomeworkAttach.find(params[:homework_id]) + comment_status = @homework.bid.comment_status + if @is_anonymous_comments && comment_status != 1 + case comment_status + when 0 + raise '尚未开启匿评!' + when 2 + raise '匿评已结束!' + end + end + if @is_anonymous_comments && ((@m_score.nil? || @m_score.blank?) || !(params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "")) + raise '您尚未打分或评论!' + end #保存评分 - @homework.rate(@m_score.to_i,User.current.id,:quality) if @m_score + homework = @homework + is_teacher = @is_teacher ? 1 : 0 + #保存评分@homework.rate(@m_score.to_i,User.current.id,:quality, (@is_teacher ? 1 : 0)) + if @m_score + rate = @homework.rates(:quality).where(:rater_id => current_user.id, :is_teacher_score => is_teacher).first + if rate + rate.stars = @m_score + rate.save! + else + @homework.rates(:quality).new(:stars => @m_score, :rater_id => current_user.id, :is_teacher_score => is_teacher).save! + end + + if homework.is_teacher_score == 0 + if is_teacher == 1 + homework.score = @m_score + homework.is_teacher_score = 1 + else + sql = "SELECT AVG(stars) as stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = #{homework.id}" + score= HomeworkAttach.find_by_sql(sql).first.stars + homework.score = score + end + else + if is_teacher == 1 + homework.score = @m_score + homework.is_teacher_score = 1 + end + end + homework.save! + end #保存评论 @is_comprehensive_evaluation = @is_teacher ? 1 : (@is_anonymous_comments ? 2 : 3) #判断当前评论是老师评论?匿评?留言 if params[:new_form] && params[:new_form][:user_message] && params[:new_form][:user_message] != "" #有没有留言 - @homework.addjours User.current.id, params[:new_form][:user_message],0,@is_comprehensive_evaluation + @homework.addjours current_user.id, params[:new_form][:user_message],0,@is_comprehensive_evaluation end end