183 lines
5.1 KiB
Ruby
183 lines
5.1 KiB
Ruby
# encoding: utf-8
|
||
## This helper be included in applicationHelper
|
||
module CoursesHelper
|
||
=begin
|
||
1. define TeacherRoles, StudentRoles
|
||
2. define count function
|
||
3. define search by roles
|
||
4. define search member function
|
||
=end
|
||
TeacherRoles = [3, 4, 7, 9]
|
||
StudentRoles = [5, 10]
|
||
AllPeople = StudentRoles+TeacherRoles
|
||
## return people count
|
||
|
||
# 返回x项目成员数量,即roles表中定义的所有成员
|
||
def projectCount project
|
||
searchCountByRoles project, AllPeople
|
||
end
|
||
|
||
# 返回教师数量,即roles表中定义的Manager
|
||
def teacherCount project
|
||
searchCountByRoles project, TeacherRoles
|
||
# or
|
||
# searchTeacherAndAssistant(project).count
|
||
end
|
||
|
||
# 返回学生数量,即roles表中定义的Reporter
|
||
def studentCount project
|
||
searchCountByRoles project,StudentRoles
|
||
# or
|
||
# searchStudent(project).count
|
||
end
|
||
|
||
# garble count 混淆数量
|
||
# alias projectCountOrigin projectCount
|
||
# def projectCount project
|
||
# count = projectCountOrigin project
|
||
# garble count
|
||
# end
|
||
|
||
alias teacherCountOrigin teacherCount
|
||
def teacherCount project
|
||
count = teacherCountOrigin project
|
||
garble count
|
||
end
|
||
|
||
alias studentCountOrigin studentCount
|
||
def studentCount project
|
||
count = studentCountOrigin project
|
||
garble count
|
||
end
|
||
|
||
def garble count
|
||
count = count.round( 1-count.to_s.size ).to_i
|
||
return count.to_s if count.to_s.size.eql?(1)
|
||
count.to_s << '+'
|
||
end
|
||
|
||
# =====================================================================================
|
||
# return people list
|
||
def searchTeacherAndAssistant project
|
||
searchPeopleByRoles(project, TeacherRoles)
|
||
end
|
||
|
||
def searchStudent project
|
||
searchPeopleByRoles(project, StudentRoles)
|
||
end
|
||
# =====================================================================================
|
||
|
||
def searchCountByRoles project, roles_id
|
||
members = searchPeopleByRoles project, roles_id
|
||
members.count
|
||
end
|
||
|
||
def searchPeopleByRoles project, roles_id
|
||
members = []
|
||
begin
|
||
members = project.members.joins(:member_roles).where("member_roles.role_id IN (:role_id)", {:role_id => roles_id})
|
||
rescue Exception => e
|
||
logger.error "[CoursesHelper] ===> #{e}"
|
||
end
|
||
members
|
||
end
|
||
|
||
#useless
|
||
def searchMembersByRole project, role_id
|
||
members = []
|
||
begin
|
||
members = project.members.joins(:member_roles).where("member_roles.role_id = :role_id", {:role_id => role_id })
|
||
rescue Exception => e
|
||
logger.error "[CoursesHelper] ===> #{e}"
|
||
end
|
||
members
|
||
end
|
||
|
||
def findCourseTime project
|
||
str = ""
|
||
begin
|
||
@course = Course.find_by_extra(@project.identifier)
|
||
date_format = l(:zh_date)[:formats][:default]
|
||
if @course
|
||
str = DateTime.parse(@course.setup_time.to_s).strftime("#{date_format}").to_s unless @course.setup_time.blank?
|
||
str << '-' unless @course.setup_time.blank?
|
||
str << DateTime.parse(@course.endup_time.to_s).strftime("#{date_format}").to_s unless @course.endup_time.blank?
|
||
end
|
||
rescue Exception => e
|
||
logger.error "[CoursesHelper] ===> #{e}"
|
||
end
|
||
str
|
||
end
|
||
|
||
def get_course_term project
|
||
str = ( project.try(:course_extra).try(:time).to_s << '.' << project.try(:course_extra).try(:term).to_s )
|
||
str[0..-4]
|
||
end
|
||
|
||
def members_to_user_ids members
|
||
people = []
|
||
members.each { |member|
|
||
people << member.user_id
|
||
}
|
||
people
|
||
end
|
||
# 截至到2014-03-17 这个是最终的判断课程是否过期的方法
|
||
def course_endTime_timeout? project
|
||
end_time_str = Course.find_by_extra(project.try(:identifier)).try(:endup_time)
|
||
begin
|
||
cTime = Time.parse(end_time_str.to_s)
|
||
rescue TypeError,ArgumentError
|
||
cTime = Time.parse("3000-01-01")
|
||
end
|
||
now = Time.now
|
||
|
||
now > cTime
|
||
end
|
||
def find_by_extra_from_project extra
|
||
Course.find_by_extra(try(extra))
|
||
end
|
||
#判断制定用户是不是当前课程的老师
|
||
def is_course_teacher user,course
|
||
people = []
|
||
course.members.each do |member|
|
||
role_id = member.roles.first.id
|
||
if TeacherRoles.include? role_id
|
||
people << member.user
|
||
end
|
||
end
|
||
people.include?(user)
|
||
end
|
||
#当前用户是不是指定课程的学生
|
||
def is_cur_course_student? course
|
||
people = []
|
||
course.members.each do |member|
|
||
if StudentRoles.include? member.roles.first.id
|
||
people << member.user
|
||
end
|
||
end
|
||
people.include?(User.current)
|
||
end
|
||
#获取当前用户在指定作业下提交的作业的集合
|
||
def cur_user_homework_for_bid bid
|
||
cur_user_homework = HomeworkAttach.where("user_id = ? and bid_id = ?",User.current,bid.id)
|
||
cur_user_homework
|
||
end
|
||
|
||
#判断当前用户对指定作业是否已经评价过
|
||
def has_evaluation? homework
|
||
seem_count = homework.rates(:quality).where("rater_id = ?",User.current).count
|
||
seem_count > 0
|
||
end
|
||
|
||
#获取指定作业的平均评分
|
||
def homework_score homework
|
||
stars_reates = homework.rates(:quality)
|
||
sum = 0
|
||
stars_reates.each do |star_reates|
|
||
sum = sum + star_reates.stars
|
||
end
|
||
stars_reates_count = stars_reates.count == 0 ? 1 : stars_reates.count
|
||
format("%.2f", sum * 1.0 / stars_reates_count)
|
||
end
|
||
end
|