2014-09-28 17:14:09 +08:00
# 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
2016-01-18 15:48:26 +08:00
2016-01-20 15:43:33 +08:00
2016-01-18 15:48:26 +08:00
# 推荐的精品课程
2016-01-20 15:43:33 +08:00
def excellent_course_recommend new_course
q = " % #{ new_course . name . strip } % "
result = find_excelletn_course ( q , new_course )
2016-01-18 15:48:26 +08:00
end
# 查询符合条件的精品课程
2016-01-20 15:43:33 +08:00
# 用新课程名去匹配精品课程中按课程名和Tag名
def find_excelletn_course keywords , current_course
# 获取tag匹配结果ID
a_tags = [ ]
# kc = keywords.to_a
2016-01-20 15:59:30 +08:00
Course . visible . where ( " is_excellent =? " , 1 ) . each do | ec |
2016-01-20 15:43:33 +08:00
if ec . tags . any? { | value | current_course . name . include? ( value . to_s ) }
a_tags << ec . id
end
end
# sql = "SELECT distinct c.* FROM `courses` c, tags t, taggings ts where t.id = ts.tag_id and ts.taggable_id = c.id and c.is_excellent = 1 and is_delete = 0 and
# ts.taggable_type = 'Course' and t.name like '%#{keywords}%'"
# a_tags = Course.find_by_sql(sql).select{|course| course.is_public ==1 unless User.current.member_of_course?(course)}
# 通过elastic结果获取精品课程
a_courses = [ ]
2016-01-24 10:58:58 +08:00
#courses = Course.search(keywords)
#courses.each do |c|
# a_courses << c.id
#end
2016-01-20 15:43:33 +08:00
a_courses << a_tags unless a_tags . length == 0
2016-01-24 10:58:58 +08:00
# 课程本身不能搜索显示自己
excellent_ids = a_courses . flatten . uniq . delete_if { | i | i == current_course . id }
limit = 5 - excellent_ids . length . to_i
sql = " SELECT distinct c.id FROM course_activities cs, courses c where cs.course_id = c.id
and c . is_excellent = 1 and c . id != #{current_course.id} order by cs.updated_at desc;"
default_ecourse_ids = Course . find_by_sql ( sql ) . flatten
# REDO:时间紧,待优化
default_ids = [ ]
default_ecourse_ids . each do | de |
default_ids << de . id
end
default_ids = default_ids - excellent_ids
#default_ecourse = Course.where("id is not in (?)", ids).find_by_sql(sql).flatten.delete_if{|i| i == current_course.id}.flatten
arr_result = excellent_ids << default_ids
arr_result = arr_result . flatten . first ( 5 )
return arr_result
2016-01-20 15:43:33 +08:00
# 过滤条件:精品课程、本身不在搜索范围
2016-01-24 10:58:58 +08:00
#e_courses = Course.where("is_excellent =? and id in (?)",1, arr_result).where("id !=?", current_course.id)
2016-01-18 15:48:26 +08:00
end
# 判断精品课程是否可见,非课程成员无法查看私有课程
def visible_excellent_course obj
# if course.is_pu
end
2014-09-28 17:14:09 +08:00
# 返回x项目成员数量, 即roles表中定义的所有成员
def projectCount project
#searchCountByRoles project, AllPeople
project . members . count
end
# 返回教师数量, 即roles表中定义的Manager
def teacherCount project
2015-04-10 15:01:33 +08:00
project ? project . members . count - studentCount ( project ) . to_i : 0
2014-09-28 17:14:09 +08:00
# or
# searchTeacherAndAssistant(project).count
end
2015-11-24 17:29:57 +08:00
def show_nav? ( count )
count == 0 ? true : false
end
2015-03-17 15:30:15 +08:00
#课程模块需要展示的模块
def course_model
@nav_dispaly_course_all_label = 1
@nav_dispaly_forum_label = 1
@nav_dispaly_course_label = nil
@nav_dispaly_store_all_label = 1
end
2015-03-18 15:18:06 +08:00
#生成课程老师成员链接
def course_teacher_link teacher_num
2015-06-26 15:13:53 +08:00
if User . current . member_of_course? ( @course ) || User . current . admin?
2015-11-02 13:12:03 +08:00
link_to " #{ teacher_num } " , course_member_path ( @course , :role = > 1 ) , :class = > 'info_foot_num c_blue' , :id = > 'teacher_number'
2015-03-18 15:18:06 +08:00
else
content_tag 'span' , teacher_num , :class = > 'info_foot_num c_blue'
end
end
#生成课程学生列表连接
def course_student_link student_num
2015-06-26 15:13:53 +08:00
if ( User . current . logged? && @course . open_student == 1 ) || ( User . current . member_of_course? ( @course ) ) || User . current . admin?
2015-11-02 13:12:03 +08:00
link_to " #{ student_num } " , course_member_path ( @course , :role = > 2 ) , :class = > 'info_foot_num c_blue' , :id = > " student_number "
2015-03-18 15:18:06 +08:00
else
content_tag 'span' , student_num , :class = > 'info_foot_num c_blue'
end
end
def course_poll_count
2015-04-09 15:18:13 +08:00
Poll . where ( " polls_type = 'Course' and polls_group_id = #{ @course . id } and polls_status in (2,3) " ) . count
2015-03-18 15:18:06 +08:00
end
def course_feedback_count
@course . journals_for_messages . where ( 'm_parent_id IS NULL' ) . count
end
2016-02-20 16:18:28 +08:00
#当前学期
def current_time_and_term course
str = " "
term = cur_course_term
if ( course . time == course . end_time && course . term == course . end_term ) || ( course . end_term . nil? && course . end_time . nil? ) || course . time > Time . now . year
str = course . time . to_s + course . term . to_s
elsif course . time == Time . now . year && set_term_value ( cur_course_term ) < = set_term_value ( course . term )
str = course . time . to_s + course . term . to_s
elsif course . end_time < Time . now . year || ( course . end_time == Time . now . year && set_term_value ( cur_course_term ) > = set_term_value ( course . term ) )
str = course . end_time . to_s + course . end_term . to_s
else
str = Time . now . year . to_s + cur_course_term . to_s
end
str
end
def set_term_value term
val = 0
if term == " 春季学期 "
val = 1
elsif term == " 夏季学期 "
val = 2
elsif term == " 秋季学期 "
val = 3
end
val
end
2014-09-28 17:14:09 +08:00
# 返回学生数量, 即roles表中定义的Reporter
#def studentCount project
# searchStudent(project).count
# or
# searchStudent(project).count
# end
# 判断用户是否是课程的管理员
# add by nwb
def is_course_manager? ( user_id , course_id )
@result = false
@user_id = CourseInfo . find_by_course_id ( course_id )
if @user_id == user . id
@result = true
end
return @result
end
# 返回课程设置界面
def course_settings_tabs
tabs = [ { :name = > 'info' , :action = > :edit_course , :partial = > 'courses/edit' , :label = > :label_information_plural } ,
{ :name = > 'members' , :action = > :manage_members , :partial = > 'courses/settings/members' , :label = > :label_member_plural }
]
tabs . select { | tab | User . current . allowed_to? ( tab [ :action ] , @course ) }
end
#是否启动互评下拉框
def is_evaluation_option
type = [ ]
option1 = [ ]
option2 = [ ]
option1 << l ( :lable_start_mutual_evaluation )
option1 << 1
option2 << l ( :lable_close_mutual_evaluation )
option2 << 2
type << option1
type << option2
end
# garble count 混淆数量
# alias projectCountOrigin projectCount
# def projectCount project
# count = projectCountOrigin project
# garble count
# end
def homework_type_option
type = [ ]
option1 = [ ]
option2 = [ ]
option1 << l ( :label_task_submit_form_accessory )
option1 << 1
option2 << l ( :label_task_submit_form_project )
option2 << 2
type << option1
type << option2
end
def proportion_option
type = [ ]
i = 0
while i < = 100
option = [ ]
option << i . to_s + " % "
option << i
type << option
i = i + 10
end
type
end
#alias teacherCountOrigin teacherCount
#def teacherCount project
# count = teacherCountOrigin project
# garble count
#end
# 注意:此方法有问题,速度慢且结果不准
# alias studentCountOrigin studentCount
#def studentCount course
# count = studentCountOrigin course
#garble count
# end
2015-01-21 11:33:29 +08:00
#获取课程所有成员
2015-01-21 12:01:02 +08:00
def course_all_member course
2015-01-21 11:33:29 +08:00
course . members
end
2014-09-28 17:14:09 +08:00
# 学生人数计算
# add by nwb
def studentCount course
2015-04-10 15:01:33 +08:00
course ? course . student . count . to_s : 0 #course.student.count
2014-09-28 17:14:09 +08:00
end
#课程成员数计算
def memberCount course
count = searchStudent ( course ) . count + searchTeacherAndAssistant ( course ) . count
2014-12-05 11:30:19 +08:00
count . to_s
2014-09-28 17:14:09 +08:00
end
def eventToLanguageCourse event_type , course
case event_type
when " issue-note "
l :label_issue
when " issue "
l :label_issue
when " attachment "
l :label_attachment
when " news "
l :label_notification
else
" "
end
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)
members = [ ]
project . members . each do | m |
members << m if m && m . user && m . user . allowed_to? ( :as_teacher , project )
end
members
end
2014-12-02 14:41:36 +08:00
def search_student_in_group ( project , course_group_id )
2014-09-28 17:14:09 +08:00
#searchPeopleByRoles(project, StudentRoles)
members = [ ]
project . members . each do | m |
members << m if m && m . user && m . user . allowed_to? ( :as_student , project )
end
members
end
2015-05-26 10:00:37 +08:00
2014-09-28 17:14:09 +08:00
def searchStudent project
2014-12-18 18:24:37 +08:00
#searchPeopleByRoles(project, StudentRoles)
members = [ ]
project . members . each do | m |
if m && m . user && m . user . allowed_to? ( :as_student , project )
members << m
end
end
members
end
2014-12-17 17:36:12 +08:00
2014-12-18 18:24:37 +08:00
2014-12-03 19:17:10 +08:00
def searchStudent_by_name project , name
2014-09-28 17:14:09 +08:00
#searchPeopleByRoles(project, StudentRoles)
members = [ ]
project . members . each do | m |
2014-12-03 19:17:10 +08:00
if m && m . user && m . user . allowed_to? ( :as_student , project )
username = m . user [ :lastname ] . to_s + m . user [ :firstname ] . to_s
if ( m . user [ :login ] . to_s . include? ( name ) || m . user . user_extensions [ :student_id ] . to_s . include? ( name ) || username . include? ( name ) )
members << m
end
end
end
members
end
2014-12-24 18:31:45 +08:00
def searchmember_by_name members , name
#searchPeopleByRoles(project, StudentRoles)
mems = [ ]
2014-12-25 10:23:59 +08:00
if name != " "
2015-04-14 15:16:58 +08:00
name = name . to_s . downcase
2014-12-25 10:23:59 +08:00
members . each do | m |
2015-04-14 15:16:58 +08:00
username = m . user [ :lastname ] . to_s . downcase + m . user [ :firstname ] . to_s . downcase
if ( m . user [ :login ] . to_s . downcase . include? ( name ) || m . user . user_extensions [ :student_id ] . to_s . downcase . include? ( name ) || username . include? ( name ) )
2014-12-24 18:31:45 +08:00
mems << m
end
2014-12-25 10:23:59 +08:00
end
else
mems = members
2014-12-24 18:31:45 +08:00
end
mems
end
def searchgroupmember_by_name members , name , group
#searchPeopleByRoles(project, StudentRoles)
mems = [ ]
2014-12-25 10:23:59 +08:00
if name != " "
2014-12-24 18:31:45 +08:00
members . each do | m |
if m . course_group_id == group . id
username = m . user [ :lastname ] . to_s + m . user [ :firstname ] . to_s
if ( m . user [ :login ] . to_s . include? ( name ) || m . user . user_extensions [ :student_id ] . to_s . include? ( name ) || username . include? ( name ) )
mems << m
end
end
end
2014-12-25 10:23:59 +08:00
else
mems = members
end
2014-12-24 18:31:45 +08:00
mems
end
2014-12-09 15:16:43 +08:00
def searchgroupstudent_by_name ( group , project , name )
2014-12-09 14:57:10 +08:00
members = [ ]
group . members . each do | m |
if m && m . user && m . user . allowed_to? ( :as_student , project )
username = m . user [ :lastname ] . to_s + m . user [ :firstname ] . to_s
if ( m . user [ :login ] . to_s . include? ( name ) || m . user . user_extensions [ :student_id ] . to_s . include? ( name ) || username . include? ( name ) )
members << m
end
end
end
members
end
2014-12-04 22:02:10 +08:00
def searchStudent_by_group group , project
2014-12-04 19:09:10 +08:00
#searchPeopleByRoles(project, StudentRoles)
members = [ ]
group . members . each do | m |
2014-09-28 17:14:09 +08:00
members << m if m && m . user && m . user . allowed_to? ( :as_student , project )
end
members
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
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 |
2014-10-25 16:19:19 +08:00
s = link_to_course ( course , { } , :class = > " #{ course . css_classes } #{ User . current . member_of_course? ( course ) ? 'my-course' : nil } " ) . html_safe
2014-09-28 17:14:09 +08:00
s
end
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 sort_course ( state , school_id )
content = '' . html_safe
case state
when 0
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 ) ) )
when 1
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 ) ) )
when 2
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 ) ) )
#gcm
when 3
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 " )
end
#gcmend
content = content_tag ( 'ul' , content )
content_tag ( 'div' , content , :class = > " tabs " )
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
2015-03-10 09:45:36 +08:00
# added by meng
# 课程time+term简写( 2014.春/2014.秋)国际化输出
2014-09-28 17:14:09 +08:00
def get_course_term course
2015-03-10 09:45:36 +08:00
strterm = course . try ( :term ) . to_s
2015-03-17 15:06:15 +08:00
if ! ( User . current . language == 'zh' || User . current . language == '' )
2015-03-10 09:45:36 +08:00
strterm == '春季学期' ? strterm = 'spring term' : strterm = 'autumn term'
str = ( course . try ( :time ) . to_s << '.' << strterm )
str [ 0 .. - 6 ]
else
str = ( course . try ( :time ) . to_s << '.' << strterm )
str [ 0 .. - 4 ]
end
end
# added by meng
# 课程term( 春季学期/秋季学期)国际化输出
def get_course_term_locales course
str = course . try ( :term ) . to_s
2015-03-17 15:06:15 +08:00
if ! ( User . current . language == 'zh' || User . current . language == '' )
2015-03-10 09:45:36 +08:00
str == '春季学期' ? str = ' ' + 'spring term' : str = ' ' + 'autumn term'
end
return str
2014-09-28 17:14:09 +08:00
end
def members_to_user_ids members
people = [ ]
members . each { | member |
people << member . user_id
}
people
end
# 截至到2014-03-17 这个是最终的判断课程是否过期的方法
def course_endTime_timeout? course
end_time_str = course . 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
2015-05-19 17:12:43 +08:00
2014-09-28 17:14:09 +08:00
def find_by_extra_from_project extra
Course . find_by_extra ( try ( extra ) )
end
2015-05-19 17:12:43 +08:00
2014-09-28 17:14:09 +08:00
#判断指定用户是不是当前课程的老师
def is_course_teacher ( user , course )
user . allowed_to? ( :as_teacher , course )
end
2015-05-19 17:12:43 +08:00
2014-09-28 17:14:09 +08:00
#当前用户是不是指定课程的学生
def is_cur_course_student course
2014-11-21 11:33:11 +08:00
User . current . logged? && User . current . member_of_course? ( course ) && ! ( User . current . allowed_to? ( :as_teacher , course ) )
2014-09-28 17:14:09 +08:00
end
#获取当前用户在指定作业下提交的作业的集合
def cur_user_homework_for_bid bid
2014-11-05 14:13:50 +08:00
bid . homeworks . where ( " user_id = ? " , User . current )
2014-09-28 17:14:09 +08:00
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
#获取指定项目的得分
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
#获取指定作业的参与人员
#返回结果:张三、李四、王五
def homework_user_of_homework homework , is_teacher
homework_users = " "
homework . users . each do | user |
homework_users = homework_users + ( is_teacher ? ( user . lastname + user . firstname ) : user . login )
if user != homework . users . last
homework_users = homework_users + " 、 "
end
end
return homework_users
end
2014-10-30 09:32:33 +08:00
def get_courses_by_tag ( tag_name )
Course . tagged_with ( tag_name ) . order ( 'updated_at desc' )
2014-09-28 17:14:09 +08:00
end
#课程实践年份下拉框
2014-11-26 16:08:14 +08:00
def course_time_option year
2014-09-28 17:14:09 +08:00
type = [ ]
2016-01-11 11:07:03 +08:00
month = Time . now . month
2014-11-26 16:08:14 +08:00
now_year = year . nil? ? Time . now . year : ( Time . now . year < = year ? Time . now . year : year )
2016-02-20 16:18:28 +08:00
year = month < 2 && now_year > = Time . now . year ? now_year - 1 : now_year
2016-01-11 11:07:03 +08:00
for i in ( year .. year + 10 )
2014-09-28 17:14:09 +08:00
option = [ ]
option << i
option << i
type << option
end
type
end
#课程课时下拉框
def course_term_option
type = [ ]
option1 = [ ]
option1 << l ( :label_spring )
option1 << l ( :label_spring )
option2 = [ ]
2015-07-09 15:08:36 +08:00
option2 << l ( :label_summer )
option2 << l ( :label_summer )
option3 = [ ]
option3 << l ( :label_autumn )
option3 << l ( :label_autumn )
2014-09-28 17:14:09 +08:00
type << option1
type << option2
2015-07-09 15:08:36 +08:00
type << option3
2014-09-28 17:14:09 +08:00
type
end
2014-11-05 14:46:01 +08:00
def cur_course_term
month = Time . now . month
2016-02-20 16:18:28 +08:00
if month > = 9 || month < 2
2014-11-05 14:46:01 +08:00
term = " 秋季学期 "
2016-02-20 16:18:28 +08:00
elsif ( month > = 7 && Time . now . day > = 15 ) || month == 8
term = " 夏季学期 "
2014-11-05 14:46:01 +08:00
else
term = " 春季学期 "
end
term
end
2014-11-26 17:38:46 +08:00
def course_in_current_or_next_term course
is_current_term = false
is_next_term = false
2016-02-20 16:18:28 +08:00
year_now = Time . now . month < 2 ? Time . now . year - 1 :Time . now . year
2015-01-19 15:00:46 +08:00
if course . time == year_now && course . term == cur_course_term
2014-11-26 17:38:46 +08:00
is_current_term = true
end
2015-01-19 15:00:46 +08:00
if cur_course_term == " 秋季学期 " && course . time == ( year_now + 1 ) && course . term == " 春季学期 "
2014-11-26 17:38:46 +08:00
is_next_term = true
2015-07-10 15:00:59 +08:00
elsif cur_course_term == " 春季学期 " && course . time == year_now && course . term == " 夏季学期 "
2014-11-26 17:38:46 +08:00
is_next_term = true
2015-07-10 15:00:59 +08:00
elsif cur_course_term == " 夏季学期 " && course . time == year_now && course . term == " 秋季学期 "
2016-02-20 16:18:28 +08:00
is_next_term = true
2014-11-26 17:38:46 +08:00
end
is_current_term || is_next_term
end
2014-09-28 17:14:09 +08:00
#获取课程动态
def get_course_activity courses , activities
@course_ids = activities . keys ( )
days = Setting . activity_days_default . to_i
2015-04-09 15:18:13 +08:00
#原来课程动态计算当期时间前(一年+一月)的动态
# date_to ||= Date.today + 1
# #date_from = date_to - days-1.years
2014-09-28 17:14:09 +08:00
2015-04-09 15:18:13 +08:00
date_from = @course . created_at . to_date - days
2014-09-28 17:14:09 +08:00
#file_count
Attachment . where ( container_id : @course_ids , container_type : Course ) . where ( " created_on>? " , date_from ) . each do | attachment |
2015-04-09 15:18:13 +08:00
if attachment . is_public? || User . current . member_of_course? ( @course ) || User . current . admin?
2014-09-28 17:14:09 +08:00
activities [ attachment . container_id ] += 1
2015-04-09 15:18:13 +08:00
else
activities [ attachment . container_id ]
end
2014-09-28 17:14:09 +08:00
end
#message_count
Board . where ( course_id : @course_ids ) . each do | board |
2015-04-09 15:18:13 +08:00
countmessage = 0
# 课程人员退出课程后,之前在讨论区回帖不计入课程动态统计
board . messages . where ( " updated_on>? " , date_from ) . each do | message |
if message . author . member_of_course? ( @course )
countmessage += 1
end
end
activities [ board . course_id ] += countmessage
2014-09-28 17:14:09 +08:00
end
#news
News . where ( course_id : @course_ids ) . where ( " created_on>? " , date_from ) . each do | news |
2015-04-09 15:18:13 +08:00
if news . author . member_of_course? ( @course )
2014-09-28 17:14:09 +08:00
activities [ news . course_id ] += 1
2015-04-09 15:18:13 +08:00
end
2014-09-28 17:14:09 +08:00
end
2015-04-03 11:30:48 +08:00
#homework_count
2015-06-02 15:07:22 +08:00
# HomeworkForCourse.where(course_id: @course_ids).each do |homework|
# countbid=0
# # @bid_ids<<homework.bid_id
# Bid.where(id: homework.bid_id).where("created_on>?",date_from).each do |bid|
# countbid+=1
# end
# activities[homework.course_id]+=countbid
# end
2015-04-03 11:30:48 +08:00
2015-04-09 15:18:13 +08:00
#poll_count
2015-04-21 15:10:45 +08:00
# 动态目前只统计发布的问卷,关闭的问卷不在动态内显示
# Poll.where(polls_group_id: @course_ids, polls_type: Course, polls_status: 2||3).where("published_at>?",date_from).each do |poll|
Poll . where ( polls_group_id : @course_ids , polls_type : Course , polls_status : 2 || 3 ) . where ( " published_at>? " , date_from ) . each do | poll |
activities [ poll . polls_group_id ] += 1
end
2015-04-09 15:18:13 +08:00
#end
2015-04-03 11:30:48 +08:00
2014-10-23 00:16:07 +08:00
# 动态数 + 1 ( 某某创建了该课程 )
2015-04-09 15:18:13 +08:00
activities . each_pair { | key , value | activities [ key ] = value + 1 }
2014-09-28 17:14:09 +08:00
return activities
end
2015-03-18 16:34:25 +08:00
#获取某个课程的动态数
def course_activity_count course
course_activity_count = Hash . new
course_activity_count [ course . id ] = 0
count = get_course_activity ( [ course ] , course_activity_count ) [ course . id ]
count . nil? ? 0 : count
end
2015-03-18 17:14:26 +08:00
#重启、关闭课程按钮
def set_course_time course
2015-10-29 16:48:54 +08:00
# id = "finish_course_#{course.id}"
# linkPath = course_endTime_timeout?(course) ? restartcourse_course_path(course) : finishcourse_course_path(course, format: :js)
# desc = course_endTime_timeout?(course) ? l(:label_course_reload) : l(:label_course_closed)
# link_to "<span class='pr_close'></span>#{desc}".html_safe, linkPath, :remote => true, :method => :post, :id => id, :confirm => l(:label_course_closed_tips, :desc => desc), :class => "pr_join_a"
2015-03-18 17:14:26 +08:00
end
2015-03-18 18:03:17 +08:00
#加入课程、退出课程按钮
2015-03-18 17:14:26 +08:00
def join_in_course_header ( course , user , options = [ ] )
if user . logged?
2015-03-27 19:50:01 +08:00
joined = course . members . map { | member | member . user_id } . include? user . id
2016-01-26 15:59:39 +08:00
text = joined ? l ( :label_course_exit_student ) : l ( :label_course_join_student )
2015-03-18 17:14:26 +08:00
url = joined ? join_path ( :object_id = > course . id ) : try_join_path ( :object_id = > course . id )
method = joined ? 'delete' : 'post'
if joined
2016-01-26 15:59:39 +08:00
link = link_to ( text , url , :remote = > true , :method = > method , :class = > " pr_join_a " , :id = > " #{ course . id } " , :confirm = > l ( :text_are_you_sure_out ) )
2015-03-18 17:14:26 +08:00
else
2016-01-26 15:59:39 +08:00
link = link_to ( text , url , :remote = > true , :method = > method , :id = > " #{ course . id } " , :class = > " pr_join_a " )
2015-03-18 17:14:26 +08:00
end
else
2016-01-26 15:59:39 +08:00
link = " <span class='pr_join_span mr5' > #{ l ( :label_course_join_student ) } </span> "
2015-03-18 17:14:26 +08:00
end
link . html_safe
end
2015-03-19 17:43:20 +08:00
2015-03-30 17:44:40 +08:00
def visable_attachemnts_incourse course
2015-04-10 15:01:33 +08:00
return [ ] unless course
2015-03-30 17:44:40 +08:00
result = [ ]
course . attachments . each do | attachment |
2015-04-09 15:18:13 +08:00
if attachment . is_public? || User . current . member_of_course? ( course ) || User . current . admin?
2015-03-30 17:44:40 +08:00
result << attachment
end
end
result
end
2015-05-08 09:37:57 +08:00
def zh_course_role role
if role == " TeachingAsistant "
result = l ( :label_TA )
elsif role == " Teacher "
result = l ( :label_teacher )
elsif role == " Student "
result = l ( :label_student )
elsif role == " Manager "
result = l ( :field_admin )
end
result
end
2015-08-12 16:24:27 +08:00
#生成课程相关动态的链接
def course_activity_link activity
# activity = CourseActivity.first
title = " "
url = " "
2015-08-13 15:00:13 +08:00
if activity . course_act
case activity . course_act_type
when " Course "
title = activity . course_act . name
url = course_path activity . course
when " HomeworkCommon "
title = " 作业 " + activity . course_act . name
url = student_work_index_path ( :homework = > activity . course_act . id )
when " News "
title = " 通知公告 " + activity . course_act . title
url = course_news_index_path ( activity . course )
when " Attachment "
title = " 课件 " + activity . course_act . filename
url = course_files_path ( activity . course )
when " Message "
title = " 课程讨论区 " + activity . course_act . subject
url = course_boards_path ( activity . course , :parent_id = > activity . course_act . parent_id ? activity . course_act . parent_id : activity . course_act . id , :topic_id = > activity . course_act . id )
when " JournalsForMessage "
title = " 留言 " + activity . course_act . notes
url = course_feedback_path ( activity . course )
when " Poll "
title = " 问卷 " + activity . course_act . polls_name
2015-08-14 14:46:14 +08:00
url = poll_path ( activity . course_act_id )
2015-08-13 15:00:13 +08:00
end
2015-08-12 16:24:27 +08:00
end
link_to title . gsub ( / <(?!img)[^>]*> / , '' ) . html_safe , url , :class = > " problem_tit c_dblue fl fb "
end
#课程动态的描述
def course_activity_desc activity
desc = " "
2015-08-13 14:56:01 +08:00
if activity . course_act
case activity . course_act_type
when " Course "
desc = " "
when " HomeworkCommon "
desc = activity . course_act . description
when " News "
desc = activity . course_act . description
when " Attachment "
desc = " "
when " Message "
desc = activity . course_act . content
when " JournalsForMessage "
desc = " "
when " Poll "
desc = activity . course_act . polls_description
end
2015-08-12 16:24:27 +08:00
end
desc . html_safe
end
2015-11-25 15:16:33 +08:00
2015-11-26 21:17:04 +08:00
# 学生按作业总分排序, 取前8个
def hero_homework_score ( course , score_sort_by )
sql_select = " SELECT members.*,(
2016-01-29 11:02:57 +08:00
SELECT SUM ( IF ( student_works . final_score is null , null , student_works . final_score - student_works . absence_penalty - student_works . late_penalty ) )
2015-11-25 15:16:33 +08:00
FROM student_works , homework_commons
WHERE student_works . homework_common_id = homework_commons . id
AND homework_commons . course_id = #{course.id}
AND student_works . user_id = members . user_id
) AS score
FROM members
JOIN students_for_courses
ON students_for_courses . student_id = members . user_id AND students_for_courses . course_id = members . course_id
WHERE members . course_id = #{course.id} ORDER BY score #{score_sort_by} limit 9"
2015-11-26 21:17:04 +08:00
homework_scores = Member . find_by_sql ( sql_select )
end
2015-11-27 00:48:08 +08:00
def contributor_course_scor ( course_id )
2015-11-27 10:49:41 +08:00
ccs = CourseContributorScore . where ( " course_id =? " , course_id ) . order ( " total_score desc " ) . limit ( 9 )
2015-11-26 21:17:04 +08:00
end
2015-11-25 15:16:33 +08:00
end
2015-11-26 21:17:04 +08:00