diff --git a/app/api/mobile/apis/homeworks.rb b/app/api/mobile/apis/homeworks.rb index abdf19390..cba295064 100644 --- a/app/api/mobile/apis/homeworks.rb +++ b/app/api/mobile/apis/homeworks.rb @@ -64,8 +64,9 @@ module Mobile requires :token, type: String end 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 :otherdata,par,with: Mobile::Entities::AnonymousWorksParams present :status, 0 end diff --git a/app/api/mobile/entities/anonymous_works_params.rb b/app/api/mobile/entities/anonymous_works_params.rb new file mode 100644 index 000000000..9ac9a5bd8 --- /dev/null +++ b/app/api/mobile/entities/anonymous_works_params.rb @@ -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 \ No newline at end of file diff --git a/app/api/mobile/entities/homework_jours.rb b/app/api/mobile/entities/homework_jours.rb new file mode 100644 index 000000000..27803b496 --- /dev/null +++ b/app/api/mobile/entities/homework_jours.rb @@ -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 \ No newline at end of file diff --git a/app/api/mobile/entities/jours.rb b/app/api/mobile/entities/jours.rb new file mode 100644 index 000000000..02f380352 --- /dev/null +++ b/app/api/mobile/entities/jours.rb @@ -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 diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb index 78f954ab4..8ff6f725c 100644 --- a/app/helpers/api_helper.rb +++ b/app/helpers/api_helper.rb @@ -25,4 +25,16 @@ module ApiHelper homeworks += homeworks homeworks[index + 1 .. index + n] 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 \ No newline at end of file diff --git a/app/services/homework_service.rb b/app/services/homework_service.rb index 33f74345f..a2c063e15 100644 --- a/app/services/homework_service.rb +++ b/app/services/homework_service.rb @@ -5,6 +5,7 @@ class HomeworkService include ApplicationHelper include WordsHelper include ApiHelper + include HomeworkAttachHelper # 作业详情(老师才显示启动匿评,学生不显示 ) # many_times 第几次(作业) @@ -100,20 +101,42 @@ class HomeworkService # attachs 该作品的所有附件 # filename 文件名 # filedesc 文件描述 - def anonymous_works_show params - @homework = HomeworkAttach.find(params[:homework_id]) - 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 + def anonymous_works_show(params,current_user) + @homework = HomeworkAttach.find(params[:id]) + @bid = @homework.bid + @course = @bid.courses.first + if current_user.admin? || current_user.member_of_course?(@course) + @stars_reates = @homework.rates(:quality) + @is_teacher = is_course_teacher current_user,@course + @has_evaluation = @stars_reates.where("rater_id = #{current_user.id} and is_teacher_score=#{@is_teacher ? 1 : 0}").first + @m_score = @has_evaluation.nil? ? 0 : @has_evaluation.stars + @teacher_stars = @stars_reates.where("is_teacher_score = 1") #老师评分列表 + @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 - @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)} end