socialforge/app/api/mobile/entities/whomework.rb

119 lines
4.6 KiB
Ruby

# encoding: utf-8
module Mobile
module Entities
class Whomework <Grape::Entity
include ApiHelper
include ApplicationHelper
include Redmine::I18n
def self.whomework_expose(f)
expose f do |wh, opt|
if wh.is_a?(Hash) && wh.key?(f)
wh[f]
elsif wh.is_a?(::HomeworkCommon)
if wh.respond_to?(f)
if f == :created_at
format_time(wh.send(f))
else
wh.send(f)
end
else
case f
when :absence_penalty
wh.nil? || wh.homework_detail_manual.nil? ? 0 : wh.homework_detail_manual.absence_penalty
when :evaluation_start
wh.nil? || wh.homework_detail_manual.nil? ? nil : convert_to_time(wh.homework_detail_manual.evaluation_start, 0)
when :evaluation_end
wh.nil? || wh.homework_detail_manual.nil? ? nil : convert_to_time(wh.homework_detail_manual.evaluation_end, 1)
when :praise_count
get_activity_praise_num(wh)
when :comment_count
wh.journals_for_messages.count
when :course_name
wh.course.name
when :syllabus_title
wh.course.syllabus.nil? ? "" : wh.course.syllabus.title
when :act_type
'HomeworkCommon'
when :act_id
wh.id
when :title
wh.name
when :homework_commit_count
wh.student_works.has_committed.count
when :last_commit_info
if wh.student_works.has_committed.count > 0
lc = wh.student_works.has_committed.reorder("commit_time desc").first
{:lasttime=>time_from_now(lc.commit_time), :lastname=>lc.user.show_name}
end
when :last_score_info
if wh.student_works.has_committed.count > 0
sw_id = "("+wh.student_works.map{|sw| sw.id}.join(",")+")"
student_work_scores = StudentWorksScore.where("student_work_id in #{sw_id} and score is not null").reorder("created_at desc")
unless student_work_scores.empty?
last_score = student_work_scores.first
{:lasttime=>time_from_now(last_score.created_at), :lastname=>last_score.user.show_name}
end
end
end
end
end
end
end
expose :author, using: Mobile::Entities::User do |w, opt|
if w.is_a?(::HomeworkCommon)
w.user
end
end
expose :current_user, using: Mobile::Entities::User do |w, opt|
current_user
end
expose :name
expose :description
expose :publish_time
expose :end_time
expose :homework_type
expose :late_penalty
expose :course_id
expose :anonymous_comment
expose :quotes
expose :is_open
expose :id
whomework_expose :title
whomework_expose :act_type
whomework_expose :act_id
whomework_expose :course_name
whomework_expose :syllabus_title
whomework_expose :created_at
whomework_expose :absence_penalty
whomework_expose :evaluation_start
whomework_expose :evaluation_end
whomework_expose :praise_count
whomework_expose :comment_count
whomework_expose :homework_commit_count #作品提交数
whomework_expose :last_commit_info #最近提交信息
whomework_expose :last_score_info #最近评阅信息
expose :all_children, using: Mobile::Entities::Jours do |f, opt|
#f[:journals_for_messages] if f.is_a?(Hash) && f.key?(:journals_for_messages)
if f.is_a?(::HomeworkCommon)
# f.journals_for_messages.reverse
if !opt[:children] && opt[:type] == 0
opt[:children] = true
tStart = opt[:page]*5
tEnd = (opt[:page]+1)*5 - 1
all_comments = f.journals_for_messages.reorder("created_on desc")
all_comments[tStart..tEnd]
end
end
end
expose :has_praise , if: lambda { |instance, options| options[:user] } do |instance, options|
has_praise = false
current_user = options[:user]
obj = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",instance.id,instance.class.to_s,current_user.id)
has_praise = obj.empty? ? false : true
has_praise
end
end
end
end