2014-05-29 20:02:02 +08:00
module HomeworkAttachHelper
2014-06-06 10:28:37 +08:00
#判断是否具有删除的权限
2014-05-29 20:02:02 +08:00
def attach_delete ( project )
if User . current . logged? && ( User . current . admin? || ( ! Member . where ( 'user_id = ? and project_id = ?' , User . current . id , project . bid . courses . first . id ) . first . nil? && ( Member . where ( 'user_id = ? and project_id = ?' , User . current . id , project . bid . courses . first . id ) . first . roles & Role . where ( 'id = ? or id = ?' , 3 , 7 ) ) . size > 0 ) || project . user_id == User . current . id )
true
else
false
end
2014-06-06 10:28:37 +08:00
end
#作业添加、编辑界面的tab页
def homework_settings_tabs f
@f = f
2014-06-10 14:27:55 +08:00
tabs = [ { :name = > 'info' , :partial = > 'homework_attach/edit_homework' , :label = > :label_information_plural } ,
{ :name = > 'members' , :partial = > 'homework_attach/homework_member' , :label = > :label_member_plural }
2014-06-06 10:28:37 +08:00
]
end
2014-06-10 08:47:21 +08:00
#作业可选成员列表分页
def render_new_members_for_homework members
#scope = Principal.active.sorted.not_member_of(project).like(params[:q])
#scope = project.members
2014-06-10 17:33:31 +08:00
#principals = paginateHelper members,10
2014-06-11 15:28:13 +08:00
#principals = members
2014-06-10 15:44:10 +08:00
#principal_count = members.count
#limit = 10
#principal_pages = Redmine::Pagination::Paginator.new principal_count, limit, params['page'] #by young
#offset ||= principal_pages.offset
#principals = members[offset, limit]
2014-06-11 15:28:13 +08:00
users = members . map ( & :user )
s = content_tag ( 'div' , member_check_box_tags_ex ( 'membership[user_ids][]' , users ) , :id = > 'principals' )
2014-06-10 15:44:10 +08:00
links = pagination_links_full ( @obj_pages , @obj_count , :per_page_links = > false ) { | text , parameters , options |
2014-06-11 15:28:13 +08:00
link_to text , get_homework_member_list_homework_attach_index_path ( parameters . merge ( :q = > params [ :q ] , bid_id : params [ :id ] || @homework ) ) , :remote = > true }
2014-06-10 15:44:10 +08:00
return s + content_tag ( 'div' , content_tag ( 'ul' , links ) , :class = > 'pagination_new' )
2014-05-29 20:02:02 +08:00
end
2014-06-10 08:47:21 +08:00
#扩展的checkbox生成
def member_check_box_tags_ex ( name , principals )
s = ''
principals . each do | member |
2014-06-11 15:28:13 +08:00
s << " <label> #{ check_box_tag name , member . id , false , :id = > nil } #{ h member . name } </label><br/> "
2014-06-10 08:47:21 +08:00
end
s . html_safe
end
2014-06-10 15:44:10 +08:00
def paginateHelper obj , pre_size = 20
@obj_count = obj . count
@obj_pages = Redmine :: Pagination :: Paginator . new @obj_count , pre_size , params [ 'page' ]
if obj . kind_of? ActiveRecord :: Base or obj . kind_of? ActiveRecord :: Relation
obj . limit ( @obj_pages . per_page ) . offset ( @obj_pages . offset )
elsif obj . kind_of? Array
obj [ @obj_pages . offset , @obj_pages . per_page ]
else
logger . error " [ApplicationController] Error : application_controller # paginateHelper ===> unknow category: #{ obj . class } "
raise RuntimeError , 'unknow type, Please input you type into this helper.'
end
end
2014-06-17 18:49:21 +08:00
def user_projects_option
2014-06-19 16:52:53 +08:00
cond = Project . visible_condition ( User . current ) + " AND projects.project_type <> 1 "
memberships = User . current . memberships . all ( :conditions = > cond )
projects = memberships . map ( & :project )
2014-06-20 13:52:17 +08:00
not_have_project = [ ]
2014-11-25 10:35:43 +08:00
not_have_project << Setting . please_chose
2014-06-20 13:52:17 +08:00
not_have_project << 0
2014-06-17 18:49:21 +08:00
type = [ ]
2014-06-20 13:52:17 +08:00
type << not_have_project
2014-06-17 18:49:21 +08:00
projects . each do | project |
2014-06-19 16:52:53 +08:00
if project != nil
option = [ ]
option << project . name
option << project . id
type << option
end
2014-06-17 18:49:21 +08:00
end
type
end
2014-11-02 09:39:18 +08:00
#判断指定用户是不是已经赞过该作业
def is_praise_homework user_id , obj_id
PraiseTread . where ( " user_id = #{ user_id } AND praise_tread_object_id = #{ obj_id } AND praise_tread_object_type = 'HomeworkAttach' " ) . empty?
end
#获取赞的总数
def praise_homework_count obj_id
PraiseTread . where ( " praise_tread_object_id = #{ obj_id } AND praise_tread_object_type = 'HomeworkAttach' " ) . count
end
2014-11-02 17:26:28 +08:00
#获取用户对作业的评分
def get_homework_score user , homework
2014-12-02 16:55:16 +08:00
temp = HomeworkAttach . find_by_sql ( " SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = #{ homework . id } AND rater_id = #{ user . id } " ) . first
@m_score = temp . nil? ? 0 :temp . stars . to_i
2014-11-02 17:26:28 +08:00
end
2014-11-03 11:01:17 +08:00
2014-12-04 18:01:53 +08:00
#获取评分对应的评论
def get_homework_review homework , is_teacher , user
homework . journals_for_messages . where ( " is_comprehensive_evaluation = #{ is_teacher ? 1 : 2 } and user_id = #{ user . id } " ) . order ( " created_on DESC " ) . first
end
2014-11-03 11:01:17 +08:00
def convert_array array
ary = " ( "
if array . nil? || array . count == 0
return " () "
end
2014-11-04 15:46:24 +08:00
array . length . times do | i |
if i == array . length - 1
ary += array [ i ] . id . to_s + " ) "
2014-11-03 11:01:17 +08:00
else
2014-11-04 15:46:24 +08:00
if ! ( array [ i ] . nil? || array [ i ] . id . nil? || array [ i ] . id . to_s == " " )
ary += array [ i ] . id . to_s + " , "
end
2014-11-03 11:01:17 +08:00
end
end
2014-11-04 15:46:24 +08:00
#array.each do |member|
# if member == array.last
# ary += member.id.to_s + ")"
# else
# ary += member.id.to_s + ","
# end
#end
2014-11-03 11:01:17 +08:00
ary
end
2015-01-30 16:58:05 +08:00
def get_student_batch_homework_list bid , user
student_batch_homework_list = HomeworkAttach . eager_load ( :attachments , :user , :rate_averages ) . find_by_sql ( " SELECT homework_attaches.*,
( SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND is_teacher_score = 1 AND stars IS NOT NULL ORDER BY updated_at DESC limit 0 , 1 ) AS t_score ,
( SELECT AVG ( stars ) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND is_teacher_score = 0 ) AS s_score ,
( SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND rater_id = #{User.current.id} AND is_teacher_score = 0) AS m_score
FROM homework_attaches
INNER JOIN homework_evaluations ON homework_evaluations . homework_attach_id = homework_attaches . id
WHERE homework_attaches . bid_id = #{bid.id} AND homework_evaluations.user_id = #{user.id} ORDER BY m_score DESC")
student_batch_homework_list
end
2015-02-06 11:22:29 +08:00
#########################################################
#sw
#获取学生未进行匿评的数量
#param: bid => 作业 user => 用户
#return 指定用户未进行匿评的作业的数量
#user必须是学生用户
#######################################################
def get_student_not_batch_homework_list bid , user
HomeworkAttach . find_by_sql ( " SELECT * FROM(SELECT homework_attaches.*,
( SELECT stars FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches . id AND rater_id = #{user.id} AND is_teacher_score = 0) AS m_score
FROM homework_attaches
INNER JOIN homework_evaluations ON homework_evaluations . homework_attach_id = homework_attaches . id
WHERE homework_attaches . bid_id = #{bid.id} AND homework_evaluations.user_id = #{user.id}) AS table1
WHERE table1 . m_score IS NULL " ).count
end
2014-05-29 20:02:02 +08:00
end