socialforge/app/helpers/courses_helper.rb

240 lines
8.1 KiB
Ruby
Raw Normal View History

# encoding: utf-8
## This helper be included in applicationHelper
module CoursesHelper
2013-11-04 09:45:56 +08:00
=begin
1. define TeacherRoles, StudentRoles
2. define count function
3. define search by roles
4. define search member function
2013-11-04 09:45:56 +08:00
=end
TeacherRoles = [3, 4, 7, 9]
StudentRoles = [5, 10]
2014-03-24 10:18:42 +08:00
AllPeople = StudentRoles+TeacherRoles
## return people count
2014-03-24 10:18:42 +08:00
# 返回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
2014-05-19 17:56:39 +08:00
# 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
2013-11-07 14:38:37 +08:00
@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)
course.members.joins(:member_roles).where("member_roles.role_id IN (:role_id) and members.user_id = #{user.id}", {:role_id => TeacherRoles}).count != 0
#修改为根据用户是否有发布任务的权限来判断用户是否是课程的老师
#is_teacher = false
#@membership = user.memberships.all(:conditions => Project.visible_condition(User.current))
#@membership.each do |membership|
# unless(membership.project.project_type==0)
# if user.allowed_to?({:controller => "projects", :action => "new_homework"}, membership.project, :global => false)
# is_teacher = true
# end
# end
#end
#is_teacher
end
#当前用户是不是指定课程的学生
def is_cur_course_student course
course.members.joins(:member_roles).where("member_roles.role_id IN (:role_id) and members.user_id = #{User.current.id}", {:role_id => StudentRoles}).count != 0
#修改:能新建占位且不能新建任务的角色判定为学生
#is_student = false
#@membership = User.current.memberships.all(:conditions => Project.visible_condition(User.current))
#@membership.each do |membership|
# unless(membership.project.project_type==0)
# if !User.current.allowed_to?({:controller => "projects", :action => "new_homework"}, membership.project, :global => false) && User.current.allowed_to?({:controller => "homework_attach", :action => "new"}, membership.project, :global => false)
# is_student = true
# end
# end
#end
#is_student
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 users_for_homework homework
homework.nil? ? [] : (homework.users + [homework.user])
end
#获取指定作业的最终评分
#最终评分 = 学生评分的平均分 * 0.4 +教师评分 * 0.6
def score_for_homework homework
if homework.bid.is_evaluation == 1 || homework.bid.is_evaluation == nil
return format("%.2f",(teacher_score_for_homework(homework).to_f * 0.6 + student_score_for_homework(homework).to_f * 0.4))
else
return teacher_score_for_homework homework
end
end
#获取作业的互评得分
def student_score_for_homework homework
member = searchPeopleByRoles(homework.bid.courses.first,TeacherRoles).first
student_stars = homework.rates(:quality).where("rater_id <> #{member.user_id}").select("stars")
student_stars_count = 0
student_stars.each do |star|
student_stars_count = student_stars_count + star.stars
end
return format("%.2f",student_stars_count / (student_stars.count == 0 ? 1 : student_stars.count))
end
#获取作业的教师评分
def teacher_score_for_homework homework
member = searchPeopleByRoles(homework.bid.courses.first,TeacherRoles).first
teacher_stars = homework.rates(:quality).where("rater_id = #{member.user_id}").select("stars").first
return format("%.2f",teacher_stars == nil ? 0 : teacher_stars.stars)
end
2014-06-19 20:15:15 +08:00
#获取指定作业的得分
def project_score project
issue_count = project.issues.count
issue_journal_count = project.issue_changes.count
issue_score = issue_count * 0.2
issue_journal_score = issue_journal_count * 0.1
finall_issue_score = issue_score + issue_journal_score
new_count = project.news.count
new_score = new_count * 0.1
finall_new_score = new_score
document_count = project.documents.count
file_score = document_count * 0.1
finall_file_score = file_score
changeset_count = project.changesets.count
code_submit_score = changeset_count * 0.3
finall_code_submit_score = code_submit_score
board_message_count = 0
project.boards.each do |board|
board_message_count += board.messages_count
end
topic_score = board_message_count * 0.1
#finall_topic_score = topic_score
finall_project_score = finall_issue_score + finall_new_score + finall_file_score + finall_code_submit_score + topic_score
format("%.2f",finall_project_score)
end
end