2013-11-06 16:43:07 +08:00
|
|
|
|
# encoding: utf-8
|
2013-10-31 10:57:46 +08:00
|
|
|
|
## This helper be included in applicationHelper
|
|
|
|
|
module CoursesHelper
|
2013-11-04 09:45:56 +08:00
|
|
|
|
=begin
|
2013-11-06 16:43:07 +08:00
|
|
|
|
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
|
2013-11-06 16:43:07 +08:00
|
|
|
|
TeacherRoles = [3, 4, 7, 9]
|
|
|
|
|
StudentRoles = [5, 10]
|
2014-03-24 10:18:42 +08:00
|
|
|
|
AllPeople = StudentRoles+TeacherRoles
|
2013-11-06 16:43:07 +08:00
|
|
|
|
## return people count
|
2014-03-24 10:18:42 +08:00
|
|
|
|
|
|
|
|
|
# 返回x项目成员数量,即roles表中定义的所有成员
|
|
|
|
|
def projectCount project
|
|
|
|
|
searchCountByRoles project, AllPeople
|
|
|
|
|
end
|
2013-11-06 16:43:07 +08:00
|
|
|
|
|
|
|
|
|
# 返回教师数量,即roles表中定义的Manager
|
|
|
|
|
def teacherCount project
|
2013-12-13 20:01:27 +08:00
|
|
|
|
searchCountByRoles project, TeacherRoles
|
2013-11-06 16:43:07 +08:00
|
|
|
|
# or
|
2013-12-13 20:01:27 +08:00
|
|
|
|
# searchTeacherAndAssistant(project).count
|
2013-11-06 16:43:07 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# 返回学生数量,即roles表中定义的Reporter
|
|
|
|
|
def studentCount project
|
2013-12-13 20:01:27 +08:00
|
|
|
|
searchCountByRoles project,StudentRoles
|
2013-11-06 16:43:07 +08:00
|
|
|
|
# or
|
2013-12-13 20:01:27 +08:00
|
|
|
|
# searchStudent(project).count
|
2013-11-06 16:43:07 +08:00
|
|
|
|
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
|
|
|
|
|
|
2013-11-06 16:43:07 +08:00
|
|
|
|
# =====================================================================================
|
|
|
|
|
# return people list
|
|
|
|
|
def searchTeacherAndAssistant project
|
|
|
|
|
searchPeopleByRoles(project, TeacherRoles)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def searchStudent project
|
|
|
|
|
searchPeopleByRoles(project, StudentRoles)
|
|
|
|
|
end
|
|
|
|
|
# =====================================================================================
|
|
|
|
|
|
|
|
|
|
def searchCountByRoles project, roles_id
|
2013-12-13 20:01:27 +08:00
|
|
|
|
members = searchPeopleByRoles project, roles_id
|
|
|
|
|
members.count
|
2013-11-06 16:43:07 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def searchPeopleByRoles project, roles_id
|
2013-12-13 20:01:27 +08:00
|
|
|
|
members = []
|
2013-11-06 16:43:07 +08:00
|
|
|
|
begin
|
2013-12-13 20:01:27 +08:00
|
|
|
|
members = project.members.joins(:member_roles).where("member_roles.role_id IN (:role_id)", {:role_id => roles_id})
|
2013-11-06 16:43:07 +08:00
|
|
|
|
rescue Exception => e
|
|
|
|
|
logger.error "[CoursesHelper] ===> #{e}"
|
|
|
|
|
end
|
2013-12-13 20:01:27 +08:00
|
|
|
|
members
|
2013-11-06 16:43:07 +08:00
|
|
|
|
end
|
|
|
|
|
|
2014-05-29 16:35:34 +08:00
|
|
|
|
def sort_courses(state)
|
|
|
|
|
content = ''.html_safe
|
|
|
|
|
case state
|
|
|
|
|
when 0
|
|
|
|
|
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:courses_sort_type => '1')))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_influence), courses_path(:courses_sort_type => '2')))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:courses_sort_type => '0'), :class=>"selected"), :class=>"selected")
|
|
|
|
|
when 1
|
|
|
|
|
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:courses_sort_type => '1'), :class=>"selected"), :class=>"selected")
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_influence), courses_path(:courses_sort_type => '2')))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:courses_sort_type => '0')))
|
|
|
|
|
when 2
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:courses_sort_type => '1')))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_influence), courses_path(:courses_sort_type => '2'), :class=>"selected"), :class=>"selected")
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:courses_sort_type => '0')))
|
|
|
|
|
end
|
|
|
|
|
content = content_tag('ul', content)
|
|
|
|
|
content_tag('div', content, :class => "tabs")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def render_course_hierarchy(courses)
|
|
|
|
|
render_course_nested_lists(courses) do |course|
|
|
|
|
|
s = link_to_course(course, {}, :class => "#{course.css_classes} #{User.current.member_of?(course) ? 'my-course' : nil}").html_safe
|
|
|
|
|
s
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-06 16:43:07 +08:00
|
|
|
|
#useless
|
2013-12-13 20:01:27 +08:00
|
|
|
|
def searchMembersByRole project, role_id
|
|
|
|
|
members = []
|
2013-11-06 16:43:07 +08:00
|
|
|
|
begin
|
2013-12-13 20:01:27 +08:00
|
|
|
|
members = project.members.joins(:member_roles).where("member_roles.role_id = :role_id", {:role_id => role_id })
|
2013-11-06 16:43:07 +08:00
|
|
|
|
rescue Exception => e
|
|
|
|
|
logger.error "[CoursesHelper] ===> #{e}"
|
|
|
|
|
end
|
2013-12-13 20:01:27 +08:00
|
|
|
|
members
|
2013-11-06 16:43:07 +08:00
|
|
|
|
end
|
|
|
|
|
|
2014-06-03 09:56:54 +08:00
|
|
|
|
def sort_course(state, school_id)
|
|
|
|
|
content = ''.html_safe
|
|
|
|
|
case state
|
|
|
|
|
when 0
|
2014-06-03 16:21:00 +08:00
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:course_sort_type => '0'), :school_id => school_id, :class=>"selected"), :class=>"selected")
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:course_sort_type => '1', :school_id => school_id)))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_activity), courses_path(:course_sort_type => '3', :school_id => school_id)))
|
2014-06-03 09:56:54 +08:00
|
|
|
|
|
|
|
|
|
when 1
|
2014-06-03 16:21:00 +08:00
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:course_sort_type => '0', :school_id => school_id)))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:course_sort_type => '1', :school_id => school_id), :class=>"selected"), :class=>"selected")
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_activity), courses_path(:course_sort_type => '3', :school_id => school_id)))
|
2014-06-03 09:56:54 +08:00
|
|
|
|
|
|
|
|
|
when 2
|
2014-06-03 16:21:00 +08:00
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:course_sort_type => '0', :school_id => school_id)))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:course_sort_type => '1', :school_id => school_id)))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_activity), courses_path(:course_sort_type => '3', :school_id => school_id)))
|
2014-06-03 09:56:54 +08:00
|
|
|
|
|
|
|
|
|
#gcm
|
|
|
|
|
when 3
|
2014-06-03 16:21:00 +08:00
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_time), courses_path(:course_sort_type => '0', :school_id => school_id)))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_active), courses_path(:course_sort_type => '1', :school_id => school_id)))
|
|
|
|
|
content << content_tag('li', link_to(l(:label_sort_by_activity), courses_path(:course_sort_type => '3', :school_id => school_id), :class=>"selected"), :class=>"selected")
|
2014-06-03 09:56:54 +08:00
|
|
|
|
end
|
|
|
|
|
#gcmend
|
|
|
|
|
|
|
|
|
|
content = content_tag('ul', content)
|
|
|
|
|
content_tag('div', content, :class => "tabs")
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-06 16:43:07 +08:00
|
|
|
|
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
|
2013-11-06 16:43:07 +08:00
|
|
|
|
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
|
2013-12-13 20:01:27 +08:00
|
|
|
|
|
2014-03-17 22:43:38 +08:00
|
|
|
|
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
|
|
|
|
|
|
2013-12-13 20:01:27 +08:00
|
|
|
|
def members_to_user_ids members
|
|
|
|
|
people = []
|
|
|
|
|
members.each { |member|
|
|
|
|
|
people << member.user_id
|
|
|
|
|
}
|
|
|
|
|
people
|
|
|
|
|
end
|
2014-03-17 22:43:38 +08:00
|
|
|
|
# 截至到2014-03-17 这个是最终的判断课程是否过期的方法
|
|
|
|
|
def course_endTime_timeout? project
|
2014-06-04 16:45:20 +08:00
|
|
|
|
end_time_str = Course.find_by_extra(project.try(:extra)).try(:endup_time)
|
2014-03-17 22:43:38 +08:00
|
|
|
|
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
|
2014-05-12 16:01:13 +08:00
|
|
|
|
def find_by_extra_from_project extra
|
|
|
|
|
Course.find_by_extra(try(extra))
|
|
|
|
|
end
|
2014-05-22 16:37:39 +08:00
|
|
|
|
#判断制定用户是不是当前课程的老师
|
2014-05-27 20:01:21 +08:00
|
|
|
|
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
|
2014-05-22 16:37:39 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2014-05-27 20:01:21 +08:00
|
|
|
|
people.include?(User.current)
|
2014-05-22 16:37:39 +08:00
|
|
|
|
end
|
2014-06-04 09:55:38 +08:00
|
|
|
|
#获取当前用户在指定作业下提交的作业的集合
|
|
|
|
|
def cur_user_homework_for_bid bid
|
|
|
|
|
cur_user_homework = HomeworkAttach.where("user_id = ? and bid_id = ?",User.current,params[:id])
|
|
|
|
|
cur_user_homework
|
|
|
|
|
end
|
2013-10-31 10:57:46 +08:00
|
|
|
|
end
|