修改匿评作品详情接口,输出匿评作品详情的同时还输出对该作品的评论(包括,教师评分,匿评及留言)

This commit is contained in:
z9hang 2015-01-22 17:21:15 +08:00
parent a4d7e5f932
commit f1693e237b
6 changed files with 131 additions and 14 deletions

View File

@ -64,8 +64,9 @@ module Mobile
requires :token, type: String requires :token, type: String
end end
get ':homework_id/anonymous_works_show' do get ':homework_id/anonymous_works_show' do
works = Homeworks.get_service.anonymous_works_show params works,par = Homeworks.get_service.anonymous_works_show params.merge(:id => params[:homework_id]),current_user.nil? ? User.find(2):current_user
present :data, works ,with: Mobile::Entities::HomeworkAttach present :data, works ,with: Mobile::Entities::HomeworkAttach
present :otherdata,par,with: Mobile::Entities::AnonymousWorksParams
present :status, 0 present :status, 0
end end

View File

@ -0,0 +1,31 @@
module Mobile
module Entities
class AnonymousWorksParams < Grape::Entity
def self.anonymous_works_params_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(Hash) && !f.key?(field)
end
end
end
anonymous_works_params_expose :is_teacher
anonymous_works_params_expose :m_score
anonymous_works_params_expose :jours
expose :teacher_stars,using: Mobile::Entities::HomeworkJours do |f, opt|
if f.is_a?(Hash) && f.key?(:teacher_stars)
f[:teacher_stars]
end
end
expose :student_stars , using: Mobile::Entities::HomeworkJours do |f, opt|
if f.is_a?(Hash) && f.key?(:student_stars)
f[:student_stars]
end
end
anonymous_works_params_expose :is_anonymous_comments
anonymous_works_params_expose :cur_type
end
end
end

View File

@ -0,0 +1,23 @@
module Mobile
module Entities
class HomeworkJours < Grape::Entity
include Redmine::I18n
def self.homework_jours_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::SeemsRateableRates)
end
end
end
homework_jours_expose :rater_id
homework_jours_expose :rater_name
homework_jours_expose :created_at
homework_jours_expose :stars
expose :comment,using: Mobile::Entities::Jours do |f,opt|
f[:comment]
end
end
end
end

View File

@ -0,0 +1,27 @@
module Mobile
module Entities
class Jours < Grape::Entity
include Redmine::I18n
def self.jours_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::JournalsForMessage) && f.respond_to?(field)
if field == :created_on
format_time(f.send(field))
else
f.send(field)
end
end
end
end
expose :user,using: Mobile::Entities::User do |f, opt|
f.user
end
jours_expose :created_on
jours_expose :notes
end
end
end

View File

@ -25,4 +25,16 @@ module ApiHelper
homeworks += homeworks homeworks += homeworks
homeworks[index + 1 .. index + n] homeworks[index + 1 .. index + n]
end end
def stars_to_json_like starts,show_jour,homework,show_name
result = []
starts.each do |s|
comment = get_homework_review homework,show_jour,s.rater
rater_name = show_name ? s.rater.login : l(:label_anonymous)
rater_id = show_name ? s.rater.id : ''
result << {:rater_id =>rater_id ,:rater_name => rater_name,:created_at => format_time(s.created_at),:stars => s.stars,:comment => comment}
end
result
end
end end

View File

@ -5,6 +5,7 @@ class HomeworkService
include ApplicationHelper include ApplicationHelper
include WordsHelper include WordsHelper
include ApiHelper include ApiHelper
include HomeworkAttachHelper
# 作业详情(老师才显示启动匿评,学生不显示 # 作业详情(老师才显示启动匿评,学生不显示
# many_times 第几次(作业) # many_times 第几次(作业)
@ -100,20 +101,42 @@ class HomeworkService
# attachs 该作品的所有附件 # attachs 该作品的所有附件
# filename 文件名 # filename 文件名
# filedesc 文件描述 # filedesc 文件描述
def anonymous_works_show params def anonymous_works_show(params,current_user)
@homework = HomeworkAttach.find(params[:homework_id]) @homework = HomeworkAttach.find(params[:id])
name = @homework.name @bid = @homework.bid
desc = @homework.description @course = @bid.courses.first
datetime = @homework.created_at if current_user.admin? || current_user.member_of_course?(@course)
files = [] @stars_reates = @homework.rates(:quality)
unless @homework.attachments.empty? @is_teacher = is_course_teacher current_user,@course
attachs = @homework.attachments @has_evaluation = @stars_reates.where("rater_id = #{current_user.id} and is_teacher_score=#{@is_teacher ? 1 : 0}").first
attachs.each do |attach| @m_score = @has_evaluation.nil? ? 0 : @has_evaluation.stars
filename = attach.filename @teacher_stars = @stars_reates.where("is_teacher_score = 1") #老师评分列表
filedesc = attach.description unless attach.description.blank? @student_stars = @stars_reates.where("is_teacher_score = 0") #学生评分列表
@is_anonymous_comments = @bid.comment_status == 1 && !@homework.users.include?(current_user) && @homework.user != current_user && !@is_teacher #判断是不是匿评(开启匿评,当前用户不是作业的创建者或者参与者,不是老师)
jours = @homework.journals_for_messages.where("is_comprehensive_evaluation = 3 or is_comprehensive_evaluation is null").order("created_on DESC")#jours留言 is null条件用以兼容历史数据
#@jour = paginateHelper jours,5 #留言
#@cur_page = params[:cur_page] || 1
@cur_type = params[:cur_type] || 5
teacher_stars_json_like = stars_to_json_like(@teacher_stars,true,@homework,true)
student_stars_json_like = stars_to_json_like(@student_stars,false,@homework,(false || @is_teacher))
else
raise '403'
end end
end
@homework [@homework,{:is_teacher => @is_teacher,:m_score => @m_score,:jours => jours,:teacher_stars => teacher_stars_json_like,
:student_stars => student_stars_json_like,:is_anonymous_comments => @is_anonymous_comments,:cur_type => @cur_type}]
#name = @homework.name
#desc = @homework.description
#datetime = @homework.created_at
#files = []
#unless @homework.attachments.empty?
# attachs = @homework.attachments
# attachs.each do |attach|
# filename = attach.filename
# filedesc = attach.description unless attach.description.blank?
# end
#end
#{:name => name, :description => desc, :datetime => format_time(datetime)} #{:name => name, :description => desc, :datetime => format_time(datetime)}
end end