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

115 lines
3.5 KiB
Ruby
Raw Normal View History

2014-12-09 16:36:43 +08:00
module Mobile
module Entities
class Course < Grape::Entity
2015-02-10 10:29:47 +08:00
include Redmine::I18n
include ApplicationHelper
include ApiHelper
2014-12-09 16:36:43 +08:00
def self.course_expose(field)
expose field do |f,opt|
c = nil
2014-12-23 09:44:31 +08:00
if f.is_a? ::Course
c = f
2014-12-23 09:44:31 +08:00
else
c = f[:course]
end
if f.is_a?(Hash) && f.key?(field)
f[field] if f.is_a?(Hash) && f.key?(field)
#f.img_url if f.respond_to?(:img_url)
2015-02-10 10:29:47 +08:00
elsif field == :created_at || field == :updated_at
(format_time(c[field]) if (c.is_a?(Hash) && c.key?(field))) || (format_time(c.send(field)) if c.respond_to?(field))
2016-07-04 16:19:05 +08:00
elsif field == :member_count
::Course===c ? c.members.count : 0
else
(c[field] if (c.is_a?(Hash) && c.key?(field))) || (c.send(field) if c.respond_to?(field))
2014-12-23 09:44:31 +08:00
end
2014-12-09 16:36:43 +08:00
end
end
2014-12-23 09:44:31 +08:00
course_expose :img_url
2014-12-09 16:36:43 +08:00
course_expose :attachmenttype
course_expose :class_period
course_expose :code
course_expose :created_at
course_expose :description
course_expose :endup_time
course_expose :extra
course_expose :id
course_expose :inherit_members
course_expose :is_public
course_expose :lft
course_expose :location
course_expose :name
course_expose :open_student
# course_expose :password
course_expose :rgt
course_expose :school_id
course_expose :setup_time
course_expose :state
course_expose :status
course_expose :string
course_expose :tea_id
course_expose :term
course_expose :time
2016-06-24 09:01:12 +08:00
course_expose :invite_code
2016-06-24 14:59:50 +08:00
course_expose :qrcode
2014-12-09 16:36:43 +08:00
course_expose :updated_at
2015-05-04 17:36:48 +08:00
course_expose :course_student_num
2016-07-04 16:19:05 +08:00
course_expose :member_count
2016-09-20 14:58:37 +08:00
course_expose :groupnum
expose :can_setting, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
can_setting = false
2016-07-14 17:15:49 +08:00
if instance[:course]
course = instance[:course]
else
course = instance
end
member = course.members.where("user_id=#{current_user.id} and course_id=#{course.id}")[0]
2016-08-05 20:31:51 +08:00
role_ids = member.role_ids if member
2016-08-05 20:31:51 +08:00
if role_ids && (role_ids.include?(7) || role_ids.include?(9) )
can_setting = true
end
2016-07-14 17:15:49 +08:00
if course.tea_id == current_user.id
can_setting = true
end
can_setting
end
2016-08-10 16:08:30 +08:00
expose :is_member, if: lambda { |instance, options| options[:user] } do |instance, options|
current_user = options[:user]
if instance[:course]
course = instance[:course]
else
course = instance
end
current_user.member_of_course?(course)
end
expose :teacher, using: Mobile::Entities::User do |c, opt|
if c.is_a? ::Course
c.teacher
else
c[:course].teacher
end
end
expose :my_homework,using: Mobile::Entities::Homework do |f, opt|
2015-01-21 11:28:09 +08:00
f[:my_homework] if f.is_a?(Hash) && f.key?(:my_homework)
end
2016-07-27 14:30:19 +08:00
expose :is_creator, if: lambda { |instance, options| options[:user] } do |instance, options|
if instance[:course]
course = instance[:course]
else
course = instance
end
current_user = options[:user]
course.tea_id == current_user.id
end
course_expose :current_user_is_member
course_expose :current_user_is_teacher
2015-04-08 18:22:16 +08:00
course_expose :work_unit
course_expose :syllabus_title
2014-12-09 16:36:43 +08:00
end
end
end