2014-09-24 14:47:53 +08:00
class CoursesController < ApplicationController
layout 'base_courses'
include CoursesHelper
helper :activities
helper :members
helper :words
2015-02-11 20:23:50 +08:00
before_filter :auth_login1 , :only = > [ :show , :feedback ]
2014-09-24 14:47:53 +08:00
menu_item :overview
menu_item :feedback , :only = > :feedback
menu_item :homework , :only = > :homework
2015-01-27 15:29:09 +08:00
2014-11-12 16:51:07 +08:00
menu_item :new_homework , :only = > :new_homework
2014-09-24 14:47:53 +08:00
menu_item l ( :label_sort_by_time ) , :only = > :index
menu_item l ( :label_sort_by_active ) , :only = > :index
menu_item l ( :label_sort_by_influence ) , :only = > :index
before_filter :can_show_course , :except = > [ ]
2015-03-09 17:28:17 +08:00
before_filter :logged_user_by_apptoken , :only = > [ :show , :new_homework , :feedback ]
2014-10-16 10:56:13 +08:00
before_filter :find_course , :except = > [ :index , :search , :list , :new , :join , :unjoin , :create , :copy , :statistics , :new_join , :course , :enterprise_course , :course_enterprise , :view_homework_attaches , :join_private_courses ]
2014-09-24 14:47:53 +08:00
before_filter :authorize_course , :only = > [ :show , :settings , :edit , :update , :modules , :close , :reopen , :view_homework_attaches , :course ]
before_filter :authorize_course_global , :only = > [ :view_homework_attaches , :new , :create ]
before_filter :require_admin , :only = > [ :copy , :archive , :unarchive , :destroy , :calendar ]
2015-01-07 09:37:35 +08:00
before_filter :toggleCourse , :only = > [ :finishcourse , :restartcourse ]
2014-09-24 14:47:53 +08:00
before_filter :require_login , :only = > [ :join , :unjoin ]
2014-10-20 17:31:45 +08:00
#before_filter :allow_join, :only => [:join]
2014-12-29 16:57:31 +08:00
2014-09-24 14:47:53 +08:00
def join
if User . current . logged?
2014-12-12 14:54:01 +08:00
cs = CoursesService . new
2015-03-27 19:50:01 +08:00
user = User . current
join = cs . join_course params , user
2015-01-07 11:20:27 +08:00
@state = join [ :state ]
course = join [ :course ]
2014-10-17 08:59:14 +08:00
else
2014-12-12 14:54:01 +08:00
@state = 5 #未登录
2014-09-24 14:47:53 +08:00
end
respond_to do | format |
2015-03-27 19:50:01 +08:00
format . js { render :partial = > 'set_join' , :locals = > { :user = > user , :course = > course , :object_id = > params [ :object_id ] } }
2014-09-24 14:47:53 +08:00
end
2014-10-16 11:55:42 +08:00
rescue Exception = > e
2014-12-12 14:54:01 +08:00
@state = 4 #已经加入了课程
2014-10-16 11:55:42 +08:00
respond_to do | format |
format . js { render :partial = > 'set_join' , :locals = > { :user = > User . current , :course = > nil , :object_id = > nil } }
end
2014-09-24 14:47:53 +08:00
end
def unjoin
if User . current . logged?
2014-12-12 14:54:01 +08:00
cs = CoursesService . new
cs . exit_course params , User . current
2014-09-24 14:47:53 +08:00
end
respond_to do | format |
format . js { render :partial = > 'set_join' , :locals = > { :user = > User . current , :course = > Course . find ( params [ :object_id ] ) , :object_id = > params [ :object_id ] } }
end
end
2014-12-04 22:02:10 +08:00
2014-10-16 10:56:13 +08:00
def join_private_courses
respond_to do | format |
format . js
end
end
2014-09-24 14:47:53 +08:00
#更新课程信息
def update
2014-12-12 14:54:01 +08:00
cs = CoursesService . new
2015-03-01 17:31:38 +08:00
c = cs . edit_course params , @course , User . current
@course = c [ :course ]
2014-12-12 14:54:01 +08:00
if @course . errors . full_messages . count < = 0
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_successful_update )
redirect_to settings_course_url ( @course )
}
format . api { render_api_ok }
end
2014-09-24 14:47:53 +08:00
else
respond_to do | format |
format . html {
2014-10-17 13:51:18 +08:00
settings
2014-09-24 14:47:53 +08:00
render :action = > 'settings'
}
format . api { render_validation_errors ( @course ) }
end
end
end
def new_join
@course = Course . find ( params [ :object_id ] )
end
# 课程搜索
# add by nwb
def search
courses_all = Course . all_course
name = params [ :name ]
2014-12-11 15:37:48 +08:00
if name . blank?
@courses = [ ]
@courses_all = [ ]
@course_count = 0
@course_pages = Paginator . new @course_count , per_page_option , params [ 'page' ]
2014-09-24 14:47:53 +08:00
else
2014-12-11 15:37:48 +08:00
@courses = courses_all . visible
if params [ :name ] . present?
@courses_all = @courses . like ( params [ :name ] )
else
@courses_all = @courses ;
end
@course_count = @courses_all . count
@course_pages = Paginator . new @course_count , per_page_option , params [ 'page' ]
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
# 课程的动态数
@course_activity_count = Hash . new
@courses_all . each do | course |
@course_activity_count [ course . id ] = 0
end
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
case params [ :course_sort_type ]
when '0'
@courses = @courses_all . order ( " created_at desc " )
@s_type = 0
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
@course_activity_count = get_course_activity @courses , @course_activity_count
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
when '1'
@courses = @courses_all . order ( " course_ac_para desc " )
@s_type = 1
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
@course_activity_count = get_course_activity @courses , @course_activity_count
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
when '2'
@courses = @courses_all . order ( " watchers_count desc " )
@s_type = 2
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
@course_activity_count = get_course_activity @courses , @course_activity_count
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
when '3'
@course_activity_count = get_course_activity @courses_all , @course_activity_count_array
@courses = handle_course @courses_all , @course_activity_count
@s_type = 3
@courses = @courses [ @course_pages . offset , @course_pages . per_page ]
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
else
@s_type = 0
@courses = @courses_all . order ( " created_at desc " )
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
@course_activity_count = get_course_activity @courses , @course_activity_count
2014-09-24 14:47:53 +08:00
2014-12-11 15:37:48 +08:00
end
2014-09-24 14:47:53 +08:00
end
respond_to do | format |
format . html {
render :layout = > 'course_base'
scope = Course
unless params [ :closed ]
scope = scope . active
end
}
format . atom {
courses = Course . visible . order ( 'created_on DESC' ) . limit ( Setting . feeds_limit . to_i ) . all
render_feed ( courses , :title = > " #{ Setting . app_title } : #{ l ( :label_course_latest ) } " )
}
end
2014-12-10 18:26:38 +08:00
rescue Exception = > e
if e . message == 'sumbit empty'
( redirect_to courses_url , :notice = > l ( :label_sumbit_empty ) ; return )
end
2014-09-24 14:47:53 +08:00
end
2014-12-03 19:17:10 +08:00
def searchmembers
2014-12-06 12:34:02 +08:00
@subPage_title = l :label_student_list
2015-03-25 20:38:07 +08:00
@canShowCode = User . current . allowed_to? ( :as_teacher , @course ) && params [ :role ] != '1'
# @is_remote = true
2014-12-29 20:46:27 +08:00
@score_sort_by = " desc "
2014-12-03 19:17:10 +08:00
q = " #{ params [ :name ] . strip } "
2014-12-09 14:57:10 +08:00
if params [ :incourse ]
2015-03-25 20:38:07 +08:00
results = searchmember_by_name ( student_homework_score ( 0 , 0 , 0 , " desc " ) , q )
2014-12-09 15:16:43 +08:00
elsif params [ :ingroup ]
@group = CourseGroup . find ( params [ :search_group_id ] )
2015-03-25 20:38:07 +08:00
results = searchmember_by_name ( student_homework_score ( @group . id , 0 , 0 , " desc " ) , q )
2014-12-09 14:57:10 +08:00
end
2014-12-26 17:31:01 +08:00
@is_remote = true
2015-03-25 20:38:07 +08:00
@result_count = results . count
@results = paginateHelper results , 10
2015-02-02 17:01:07 +08:00
@search_name = q
2014-12-03 19:17:10 +08:00
end
def addgroups
2014-12-06 12:34:02 +08:00
@subPage_title = l :label_student_list
2014-12-04 19:09:10 +08:00
if params [ :group_name ]
group = CourseGroup . new
group . name = params [ :group_name ]
group . course_id = @course . id
group . save
end
2015-03-26 11:30:00 +08:00
@canShowCode = User . current . allowed_to? ( :as_teacher , @course ) && params [ :role ] != '1'
2014-12-05 16:21:53 +08:00
@is_remote = true
2014-12-24 18:31:45 +08:00
2014-12-04 19:09:10 +08:00
@course_groups = @course . course_groups
2014-12-03 19:17:10 +08:00
end
2014-12-08 16:50:20 +08:00
def deletegroup
CourseGroup . delete ( params [ :group_id ] )
@subPage_title = l :label_student_list
@canShowCode = isCourseTeacher ( User . current . id , @course ) && params [ :role ] != '1'
@is_remote = true
2014-12-24 18:31:45 +08:00
2014-12-08 16:50:20 +08:00
@course_groups = @course . course_groups
end
2014-12-03 19:17:10 +08:00
def updategroupname
2014-12-06 12:34:02 +08:00
@subPage_title = l :label_student_list
2014-12-04 19:09:10 +08:00
if params [ :group_name ]
group = CourseGroup . find ( params [ :group_id ] )
group . name = params [ :group_name ]
group . save
end
2014-12-05 10:53:43 +08:00
@canShowCode = isCourseTeacher ( User . current . id , @course ) && params [ :role ] != '1'
@is_remote = true
2014-12-24 18:31:45 +08:00
2014-12-04 19:09:10 +08:00
@course_groups = @course . course_groups
2014-12-03 19:17:10 +08:00
end
2014-12-19 23:12:32 +08:00
def valid_ajax
req = Hash . new ( false )
req [ :message ] = ''
valid_attr = params [ :valid ]
valid_value = params [ :value ]
2015-03-26 11:30:00 +08:00
group_id = params [ :group_id ]
# faker = CourseGroup.new
2014-12-19 23:12:32 +08:00
if valid_attr . eql? ( 'name' )
2015-03-26 11:30:00 +08:00
course = Course . find params [ :course_id ]
group_names = course . course_groups . map { | group | group . name unless group . id . to_s == group_id } . select { | group | ! group . nil? }
if group_names . include? ( valid_value )
req [ :valid ] = false
req [ :message ] = l ( :modal_valid_unpassing )
else
req [ :valid ] = true
req [ :message ] = l ( :modal_valid_passing )
end
# faker.name = valid_value
# faker.course_id = params[:course_id]
# faker.valid?
# req[:valid] = faker.errors[:name].blank?
# req[:message] = faker.errors[:name]
2014-12-19 23:12:32 +08:00
end
2015-03-26 11:30:00 +08:00
# req[:message] = l(:modal_valid_passing) if req[:message].blank?
2014-12-19 23:12:32 +08:00
render :json = > req
end
2014-12-04 22:02:10 +08:00
def join_group
2014-12-06 12:34:02 +08:00
@subPage_title = l :label_student_list
2014-12-04 22:02:10 +08:00
group = CourseGroup . find ( params [ :object_id ] )
member = Member . where ( :course_id = > @course . id , :user_id = > User . current . id ) . first
member . course_group_id = group . id
member . save
@group = group
2014-12-29 16:57:31 +08:00
2014-12-05 16:44:05 +08:00
@course_groups = @course . course_groups
2014-12-29 16:57:31 +08:00
2014-12-04 22:02:10 +08:00
search_group_members group
end
def unjoin_group
2014-12-06 12:34:02 +08:00
@subPage_title = l :label_student_list
2014-12-04 22:02:10 +08:00
group = CourseGroup . find ( params [ :object_id ] )
member = Member . where ( :course_id = > @course . id , :user_id = > User . current . id ) . first
member . course_group_id = 0
member . save
@group = group
2014-12-05 16:44:05 +08:00
@course_groups = @course . course_groups
2014-12-29 16:57:31 +08:00
2014-12-04 22:02:10 +08:00
search_group_members group
end
2014-12-03 19:17:10 +08:00
def searchgroupmembers
2014-12-06 12:34:02 +08:00
@subPage_title = l :label_student_list
2015-03-26 10:25:54 +08:00
@render_file = 'new_member_list'
2014-12-04 19:09:10 +08:00
@canShowCode = isCourseTeacher ( User . current . id , @course ) && params [ :role ] != '1'
@is_remote = true
2014-12-29 20:46:27 +08:00
@score_sort_by = " desc "
2014-12-04 19:09:10 +08:00
if params [ :group_id ] && params [ :group_id ] != " 0 "
@group = CourseGroup . find ( params [ :group_id ] )
2014-12-29 20:46:27 +08:00
@results = student_homework_score ( @group . id , 0 , 0 , " desc " )
2014-12-29 16:57:31 +08:00
@results = paginateHelper @results , 10
2014-12-04 19:09:10 +08:00
else
2014-12-29 16:57:31 +08:00
page_from = params [ :page ] . nil? ? 0 : ( params [ :page ] . to_i - 1 )
2014-12-31 08:48:14 +08:00
@results = student_homework_score ( 0 , page_from , 10 , " desc " )
2014-12-29 16:57:31 +08:00
@results = paginateHelper_for_members @results , 10
2014-12-04 19:09:10 +08:00
end
2014-12-04 22:02:10 +08:00
end
2014-12-24 18:31:45 +08:00
2014-09-24 14:47:53 +08:00
def member
## 有角色参数的才是课程,没有的就是项目
2014-12-05 14:48:33 +08:00
if ( User . current . admin? || @course . is_public == 1 || ( @course . is_public == 0 && User . current . member_of_course? ( @course ) ) )
2014-12-31 17:02:55 +08:00
2015-03-26 10:25:54 +08:00
@render_file = 'new_member_list'
2014-12-29 20:46:27 +08:00
@score_sort_by = " desc "
2015-03-25 20:13:48 +08:00
@canShowCode = User . current . allowed_to? ( :as_teacher , @course ) && params [ :role ] != '1'
2014-12-29 16:57:31 +08:00
@role = params [ :role ] . nil? ? '2' :params [ :role ]
@is_remote = true
2014-12-05 14:48:33 +08:00
@course_groups = @course . course_groups if @course . course_groups
2014-12-06 11:00:15 +08:00
@show_serch = params [ :role ] == '2'
2014-12-29 16:57:31 +08:00
case @role
2014-12-05 14:48:33 +08:00
when '1'
@subPage_title = l :label_teacher_list
2014-12-30 17:49:48 +08:00
@all_members = searchTeacherAndAssistant ( @course )
@members = paginateHelper @all_members , 10
2014-12-05 14:48:33 +08:00
when '2'
@subPage_title = l :label_student_list
2014-12-29 16:57:31 +08:00
page = params [ :page ] . nil? ? 0 : ( params [ 'page' ] . to_i - 1 )
2015-03-25 20:13:48 +08:00
@all_members = student_homework_score ( 0 , page , 10 , " desc " )
# @all_members = @course.members
2014-12-30 17:49:48 +08:00
@members = paginateHelper_for_members @all_members , 10
2014-12-29 16:57:31 +08:00
end
respond_to do | format |
if params [ :page ]
format . js
2014-12-02 14:41:36 +08:00
else
2014-12-29 16:57:31 +08:00
format . html { render :layout = > 'base_courses' }
end
2014-12-05 14:48:33 +08:00
end
else
render_403
2014-09-24 14:47:53 +08:00
end
2014-12-17 17:36:12 +08:00
end
2014-12-18 18:24:37 +08:00
2014-12-30 17:49:48 +08:00
def export_course_member_excel
@all_members = student_homework_score ( 0 , 0 , 0 , " desc " )
respond_to do | format |
format . xls {
send_data ( member_to_xls ( @all_members , @course . course_groups ) , :type = > " text/excel;charset=utf-8; header=present " ,
2014-12-31 09:48:17 +08:00
:filename = > " #{ @course . teacher . lastname . to_s + @course . teacher . firstname . to_s } _ #{ @course . name } _ #{ @course . time . to_s + @course . term } #{ l ( :excel_member_list ) } .xls " )
2014-12-30 17:49:48 +08:00
}
end
end
2014-12-18 18:24:37 +08:00
def member_score_sort
2014-12-29 20:46:27 +08:00
@canShowCode = isCourseTeacher ( User . current . id , @course ) && params [ :role ] != '1'
@subPage_title = l :label_student_list
2015-03-26 10:25:54 +08:00
@render_file = 'new_member_list'
2014-12-29 20:46:27 +08:00
@is_remote = true
@score_sort_by = params [ :sort_by ] if params [ :sort_by ]
2015-02-02 17:01:07 +08:00
@search_name = params [ :search_name ] if params [ :search_name ]
2014-12-29 20:46:27 +08:00
group_id = params [ :group_id ]
2015-02-02 17:01:07 +08:00
if ! @search_name . nil?
if group_id == '0'
page = params [ :page ] . nil? ? 0 : ( params [ 'page' ] . to_i - 1 )
@results = searchmember_by_name ( student_homework_score ( 0 , 0 , 0 , @score_sort_by ) , @search_name )
@result_count = @results . count
@results = paginateHelper @results , 10
else
@group = CourseGroup . find ( group_id )
@results = searchmember_by_name ( student_homework_score ( group_id , 0 , 0 , @score_sort_by ) , @search_name )
@result_count = @results . count
@results = paginateHelper @results , 10
end
2014-12-29 20:46:27 +08:00
else
2015-02-02 17:01:07 +08:00
if group_id == '0'
page = params [ :page ] . nil? ? 0 : ( params [ 'page' ] . to_i - 1 )
@results = student_homework_score ( 0 , page , 10 , @score_sort_by )
@results = paginateHelper_for_members @results , 10
else
@group = CourseGroup . find ( group_id )
@results = student_homework_score ( group_id , 0 , 0 , @score_sort_by )
@results = paginateHelper @results , 10
end
2014-12-29 20:46:27 +08:00
end
2014-12-18 18:24:37 +08:00
end
2014-12-17 17:36:12 +08:00
# 显示每个学生的作业评分详情
def show_member_score
@member_score = Member . find ( params [ :member_id ] ) if params [ :member_id ]
respond_to do | format |
format . html { render :layout = > 'course_base' }
format . js
2014-09-24 14:47:53 +08:00
end
end
def handle_course courses , activities
course_activity_count_array = activities . values ( )
course_array = [ ]
i = 0 ;
courses . each do | course |
course_array [ i ] = course
i = i + 1
end
courses = desc_sort_course_by_avtivity ( course_activity_count_array , course_array )
return courses
end
def settings
2014-10-08 15:06:29 +08:00
if User . current . allowed_to? ( :as_teacher , @course )
@issue_custom_fields = IssueCustomField . sorted . all
@issue_category || = IssueCategory . new
@member || = @course . members . new
@trackers = Tracker . sorted . all
2015-03-30 11:20:16 +08:00
@roles = Role . givable . all [ 3 .. 5 ]
@members = @course . member_principals . includes ( :roles , :principal ) . all . sort
2014-10-08 15:06:29 +08:00
else
render_403
end
2014-09-24 14:47:53 +08:00
end
def create
2014-12-11 17:11:05 +08:00
cs = CoursesService . new
2015-03-02 14:33:35 +08:00
@course = cs . create_course ( params , User . current ) [ :course ]
2014-12-11 17:11:05 +08:00
if @course . new_record?
respond_to do | format |
format . html { render :action = > 'new' , :layout = > 'base' } #Added by young
format . api { render_validation_errors ( @course ) }
end
else
respond_to do | format |
format . html {
flash [ :notice ] = l ( :notice_successful_create )
if params [ :continue ]
redirect_to new_course_url ( attrs , :course = > '0' )
elsif params [ :course_continue ]
redirect_to new_course_url ( :course = > '1' )
else
redirect_to settings_course_url ( @course , :course_type = > 1 )
end
}
format . api { render :action = > 'show' , :status = > :created , :location = > url_for ( :controller = > 'courses' , :action = > 'show' , :id = > @course . id ) }
2014-09-24 14:47:53 +08:00
end
2014-12-11 17:11:05 +08:00
end
2014-09-24 14:47:53 +08:00
end
2015-01-17 15:21:18 +08:00
def course
2014-09-24 14:47:53 +08:00
@school_id = params [ :school_id ]
per_page_option = 10
if @school_id == " 0 " or @school_id . nil?
@courses_all = Course . active . visible .
2014-12-31 17:02:55 +08:00
joins ( " LEFT JOIN #{ CourseStatus . table_name } ON #{ Course . table_name } .id = #{ CourseStatus . table_name } .course_id " )
2014-09-24 14:47:53 +08:00
else
@courses_all = Course . active . visible .
joins ( " LEFT JOIN #{ CourseStatus . table_name } ON #{ Course . table_name } .id = #{ CourseStatus . table_name } .course_id " ) .
joins ( :course_extra ) .
where ( " #{ Course . table_name } .school_id = ? " , @school_id )
end
@course_count = @courses_all . count
@course_pages = Paginator . new @course_count , per_page_option , params [ 'page' ]
@course_activity_count = Hash . new
@courses_all . each do | course |
@course_activity_count [ course . id ] = 0
end
case params [ :course_sort_type ]
when '0'
@courses = @courses_all . order ( " created_on desc " )
@s_type = 0
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
@course_activity_count = get_course_activity @courses , @course_activity_count
when '1'
@courses = @courses_all . order ( " course_ac_para desc " )
@s_type = 1
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
@course_activity_count = get_course_activity @courses , @course_activity_count
when '2'
@courses = @courses_all . order ( " watchers_count desc " )
@s_type = 2
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
@course_activity_count = get_course_activity @courses , @course_activity_count
when '3'
@course_activity_count = get_course_activity @courses_all , @course_activity_count
@courses = handle_course @courses_all , @course_activity_count
@s_type = 3
@courses = @courses [ @course_pages . offset , @course_pages . per_page ]
else
@s_type = 0
@courses = @courses_all . order ( " created_on desc " )
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
@course_activity_count = get_course_activity @courses , @course_activity_count
end
respond_to do | format |
format . html {
render :layout = > 'base'
}
format . api {
}
format . atom {
courses = Course . visible . order ( 'created_on DESC' ) . limit ( Setting . feeds_limit . to_i ) . all
render_feed ( courses , :title = > " #{ Setting . app_title } : #{ l ( :label_course_latest ) } " )
}
end
end
def new
@course_type = params [ :course_type ] || = params [ :course ]
@issue_custom_fields = IssueCustomField . sorted . all
@trackers = Tracker . sorted . all
@course = Course . new
@course . safe_attributes = params [ :course ]
2014-11-05 14:46:01 +08:00
month = Time . now . month
2015-03-26 15:12:14 +08:00
render :layout = > 'new_base'
2014-09-24 14:47:53 +08:00
end
def desc_sort_course_by_avtivity ( activity_count , courses )
return courses if activity_count . size < 2
( activity_count . size - 2 ) . downto ( 0 ) do | i |
( 0 .. i ) . each do | j |
if activity_count [ j ] < activity_count [ j + 1 ]
courses [ j ] , courses [ j + 1 ] = courses [ j + 1 ] , courses [ j ]
activity_count [ j ] , activity_count [ j + 1 ] = activity_count [ j + 1 ] , activity_count [ j ]
end
end
end
return courses
end
def index
2015-01-27 15:29:09 +08:00
if ! User . current . admin?
render_404
return
end
2014-09-24 14:47:53 +08:00
@course_type = params [ :course_type ]
@school_id = params [ :school_id ]
per_page_option = 10
2014-10-21 15:33:48 +08:00
if @school_id == " 0 " || @school_id . nil?
2014-09-24 14:47:53 +08:00
@courses_all = Course . active . visible .
joins ( " LEFT JOIN #{ CourseStatus . table_name } ON #{ Course . table_name } .id = #{ CourseStatus . table_name } .course_id " )
else
@courses_all = Course . active . visible .
joins ( " LEFT JOIN #{ CourseStatus . table_name } ON #{ Course . table_name } .id = #{ CourseStatus . table_name } .course_id " ) .
where ( " #{ Course . table_name } .school_id = ? " , @school_id )
end
@course_count = @courses_all . count
@course_pages = Paginator . new @course_count , per_page_option , params [ 'page' ]
@course_activity_count = Hash . new
@courses_all . each do | course |
@course_activity_count [ course . id ] = 0
end
case params [ :course_sort_type ]
when '0'
@courses = @courses_all . order ( " created_at desc " )
@s_type = 0
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
2014-10-21 15:33:48 +08:00
@course_activity_count = get_course_activity @courses , @course_activity_count
2014-09-24 14:47:53 +08:00
when '1'
@courses = @courses_all . order ( " course_ac_para desc " )
@s_type = 1
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
@course_activity_count = get_course_activity @courses , @course_activity_count
when '2'
@courses = @courses_all . order ( " watchers_count desc " )
@s_type = 2
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
@course_activity_count = get_course_activity @courses , @course_activity_count
when '3'
@course_activity_count = get_course_activity @courses_all , @course_activity_count
@courses = handle_course @courses_all , @course_activity_count
@s_type = 3
@courses = @courses [ @course_pages . offset , @course_pages . per_page ]
else
@s_type = 0
@courses = @courses_all . order ( " created_at desc " )
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
@course_activity_count = get_course_activity @courses , @course_activity_count
end
respond_to do | format |
format . html {
2015-01-27 15:29:09 +08:00
render :layout = > 'base'
2014-09-24 14:47:53 +08:00
}
format . atom {
courses = Course . visible . order ( 'created_on DESC' ) . limit ( Setting . feeds_limit . to_i ) . all
render_feed ( courses , :title = > " #{ Setting . app_title } : #{ l ( :label_course_latest ) } " )
}
end
end
def homework
2014-11-28 16:16:22 +08:00
if @course . is_public != 0 || User . current . member_of_course? ( @course ) || User . current . admin?
2015-03-19 17:28:36 +08:00
bids = @course . homeworks . order ( 'created_on DESC' )
bids = bids . like ( params [ :name ] ) if params [ :name ] . present?
@bids = paginateHelper bids , 10
@is_teacher = User . current . logged? && ( User . current . admin? || User . current . allowed_to? ( :as_teacher , @course ) )
2015-03-23 20:30:38 +08:00
@is_student = User . current . logged? && ( User . current . admin? || ( User . current . member_of_course? ( @course ) && ! @is_teacher ) )
2014-10-08 10:48:10 +08:00
render :layout = > 'base_courses'
2014-10-08 11:42:34 +08:00
else
render_403
2014-09-24 14:47:53 +08:00
end
end
# 新建作业
def new_homework
@homework = Bid . new
@homework . safe_attributes = params [ :bid ]
2014-11-19 10:34:36 +08:00
@homework . open_anonymous_evaluation = 1
2015-03-23 17:18:58 +08:00
@homework . deadline = ( Time . now + 3600 * 24 ) . strftime ( '%Y-%m-%d' )
2014-09-24 14:47:53 +08:00
if ( User . current . logged? && User . current . member_of_course? ( Course . find params [ :id ] ) )
render :layout = > 'base_courses'
else
render_403
end
end
def toggleCourse
@course_prefs = Course . find_by_extra ( @course . extra )
unless ( @course_prefs . teacher == User . current || User . current . admin? )
render_403
end
end
def get_courses
@user = User . current
membership = @user . coursememberships . all
membership . sort! { | older , newer | newer . created_on < = > older . created_on }
@memberships = [ ]
membership . collect { | e |
@memberships . push ( e )
}
@memberships_doing = [ ]
@memberships_done = [ ]
now_time = Time . now . year
@memberships . map { | e |
end_time = e . course . get_time . year
isDone = course_endTime_timeout? ( e . course )
if isDone
@memberships_done . push e
else
@memberships_doing . push e
end
}
end
def finishcourse
yesterday = Date . today . prev_day . to_time
@course_prefs . endup_time = yesterday
@save_flag = @course_prefs . save
get_courses
respond_to do | format |
format . js
end
end
def restartcourse
day = Time . parse ( " 3000-01-01 " )
@course_prefs . endup_time = day
@save_flag = @course_prefs . save
get_courses
respond_to do | format |
format . js {
render action : 'finishcourse'
}
end
end
def show
if params [ :jump ] && redirect_to_course_menu_item ( @course , params [ :jump ] )
return
end
@users_by_role = @course . users_by_role
if ( User . find_by_id ( CourseInfos . find_by_course_id ( @course . id ) . try ( :user_id ) ) )
@user = User . find_by_id ( CourseInfos . find_by_course_id ( @course . id ) . user_id )
end
@key = User . current . rss_key
#新增内容
@days = Setting . activity_days_default . to_i
if params [ :from ]
begin ; @date_to = params [ :from ] . to_date + 1 ; rescue ; end
end
has = {
" show_course_files " = > true ,
" show_course_news " = > true ,
" show_course_messages " = > true ,
" show_bids " = > true ,
2015-03-12 21:57:35 +08:00
" show_course_journals_for_messages " = > true ,
" show_homeworks " = > true
2014-09-24 14:47:53 +08:00
}
@date_to || = Date . today + 1
@date_from = ( @date_to - @days ) > @course . created_at . to_date ? ( @date_to - @days ) : @course . created_at . to_date
@author = ( params [ :user_id ] . blank? ? nil : User . active . find ( params [ :user_id ] ) )
2015-03-12 21:57:35 +08:00
@author || = @course . teacher
2014-09-24 14:47:53 +08:00
# 决定显示所用用户或单个用户活动
@activity = Redmine :: Activity :: Fetcher . new ( User . current , :course = > @course ,
:with_subprojects = > false ,
:author = > @author )
@activity . scope_select { | t | has [ " show_ #{ t } " ] }
# modify by nwb
# 添加私密性判断
if User . current . member_of_course? ( @course ) || User . current . admin?
2015-01-12 14:58:11 +08:00
events = @activity . events ( @days , @course . created_at )
2014-09-24 14:47:53 +08:00
else
2015-01-12 14:58:11 +08:00
events = @activity . events ( @days , @course . created_at , :is_public = > 1 )
2014-09-24 14:47:53 +08:00
end
# 无新动态时,显示老动态
if events . count == 0
if User . current . member_of_course? ( @course ) || User . current . admin?
events = @activity . events
else
events = @activity . events ( :is_public = > 1 )
end
end
2015-03-19 14:30:06 +08:00
events = paginateHelper events , 10
2014-09-24 14:47:53 +08:00
@events_by_day = events . group_by { | event | User . current . time_to_date ( event . event_datetime ) }
# documents
@sort_by = %w( category date title author ) . include? ( params [ :sort_by ] ) ? params [ :sort_by ] : 'category'
if ( User . find_by_id ( CourseInfos . find_by_course_id ( @course . id ) . try ( :user_id ) ) )
@user = User . find_by_id ( CourseInfos . find_by_course_id ( @course . id ) . user_id )
end
respond_to do | format |
format . html { render :layout = > 'base_courses' }
format . api
end
end
#判断指定用户是否为课程教师
def isCourseTeacher ( id , course )
result = false
user = User . find ( id )
2014-09-29 16:49:47 +08:00
if ! user . nil? && user . allowed_to? ( :as_teacher , course )
2014-09-24 14:47:53 +08:00
result = true
end
result
end
def feedback
2014-12-05 14:48:33 +08:00
if ( User . current . admin? || @course . is_public == 1 || ( @course . is_public == 0 && User . current . member_of_course? ( @course ) ) )
page = params [ :page ]
# Find the page of the requested reply
@jours = @course . journals_for_messages . where ( 'm_parent_id IS NULL' ) . order ( 'created_on DESC' )
@limit = 10
if params [ :r ] && page . nil?
offset = @jours . count ( :conditions = > [ " #{ JournalsForMessage . table_name } .id > ? " , params [ :r ] . to_i ] )
page = 1 + offset / @limit
end
@jour = paginateHelper @jours , 10
@state = false
respond_to do | format |
format . html { render :layout = > 'base_courses' }
format . api
end
else
render_403
2014-09-24 14:47:53 +08:00
end
end
2015-02-09 10:28:33 +08:00
#根据已有课程复制课程
#param id:已有课程ID
def copy_course
if @course
@new_course = Course . new @course . attributes
@new_course . tea_id = User . current . id
@new_course . created_at = DateTime . now
@new_course . updated_at = DateTime . now
@new_course . endup_time = nil
if @new_course . save
r = Role . givable . find_by_id ( Setting . new_project_user_role_id . to_i ) || Role . givable . first
m = Member . new ( :user = > User . current , :roles = > [ r ] )
m . project_id = - 1
course = CourseInfos . new ( :user_id = > User . current . id , :course_id = > @new_course . id )
#user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id)
if @new_course . is_public == 1
course_status = CourseStatus . create ( :course_id = > @new_course . id , :watchers_count = > 0 , :changesets_count = > 0 , :grade = > 0 , :course_type = > 1 )
end
@new_course . members << m
@new_course . course_infos << course
redirect_to settings_course_url @new_course
end
else
render_404
end
end
2014-09-24 14:47:53 +08:00
private
2014-10-21 08:55:55 +08:00
def allow_join course
if course_endTime_timeout? course
2014-09-24 14:47:53 +08:00
respond_to do | format |
format . js {
@state = 2
render :partial = > 'set_join' ,
:locals = > { :user = > User . current ,
:course = > Course . find ( params [ :object_id ] ) ,
:object_id = > params [ :object_id ]
}
}
end
end
end
#验证是否显示课程
def can_show_course
2014-10-16 11:09:45 +08:00
@first_page = FirstPage . find_by_page_type ( 'project' )
2014-09-24 14:47:53 +08:00
if @first_page . show_course == 2
render_404
end
end
2014-12-17 17:36:12 +08:00
2014-12-29 20:46:27 +08:00
def student_homework_score ( groupid , start_from , nums , score_sort_by )
2014-12-26 14:50:14 +08:00
#teachers = find_course_teachers(@course)
2014-12-29 16:57:31 +08:00
start_from = start_from * nums
2014-12-31 08:48:14 +08:00
sql_select = " "
if groupid == 0
if nums == 0
sql_select = " SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
2015-03-25 20:13:48 +08:00
WHERE members . course_id = #{@course.id} AND members.user_id in (SELECT students_for_courses.student_id FROM students_for_courses WHERE course_id = #{@course.id}) AND members.user_id = homework_attaches.user_id
AND homework_attaches . bid_id in ( SELECT bid_id FROM homework_for_courses WHERE course_id = #{@course.id}) GROUP BY members.user_id
UNION all
SELECT members . * , 0 as score FROM members , homework_attaches , students_for_courses WHERE members . course_id = #{@course.id} AND
students_for_courses . course_id = #{@course.id} and members.user_id = students_for_courses.student_id AND
members . user_id NOT IN ( SELECT homework_attaches . user_id FROM homework_attaches WHERE homework_attaches . bid_id in ( SELECT bid_id FROM homework_for_courses WHERE course_id = #{@course.id} )
)
GROUP BY members . user_id ORDER BY score #{score_sort_by}"
2014-12-31 08:48:14 +08:00
else
sql_select = " SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
2015-03-25 20:13:48 +08:00
WHERE members . course_id = #{@course.id} AND members.user_id in (SELECT students_for_courses.student_id FROM students_for_courses WHERE course_id = #{@course.id}) AND members.user_id = homework_attaches.user_id
AND homework_attaches . bid_id in ( SELECT bid_id FROM homework_for_courses WHERE course_id = #{@course.id}) GROUP BY members.user_id
UNION all
SELECT members . * , 0 as score FROM members , homework_attaches , students_for_courses WHERE members . course_id = #{@course.id} AND
students_for_courses . course_id = #{@course.id} and members.user_id = students_for_courses.student_id AND
members . user_id NOT IN ( SELECT homework_attaches . user_id FROM homework_attaches WHERE homework_attaches . bid_id in ( SELECT bid_id FROM homework_for_courses WHERE course_id = #{@course.id} )
)
GROUP BY members . user_id ORDER BY score #{score_sort_by} limit #{start_from}, #{nums}"
2014-12-31 08:48:14 +08:00
end
else
sql_select = " SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
2015-03-25 20:13:48 +08:00
WHERE members . course_id = #{@course.id} AND members.user_id in (SELECT students_for_courses.student_id FROM students_for_courses WHERE course_id = #{@course.id}) AND members.user_id = homework_attaches.user_id
and members . course_group_id = #{groupid} AND homework_attaches.bid_id in (SELECT bid_id FROM homework_for_courses WHERE course_id = #{@course.id})
GROUP BY members . user_id
UNION all
SELECT members . * , 0 as score FROM members , homework_attaches , students_for_courses WHERE members . course_id = #{@course.id}
and members . course_group_id = #{groupid} AND
students_for_courses . course_id = #{@course.id} and members.user_id = students_for_courses.student_id AND
members . user_id NOT IN ( SELECT homework_attaches . user_id FROM homework_attaches WHERE homework_attaches . bid_id in ( SELECT bid_id FROM homework_for_courses WHERE course_id = #{@course.id} )
)
GROUP BY members . user_id ORDER BY score #{score_sort_by}"
2014-12-31 08:48:14 +08:00
end
2014-12-26 14:50:14 +08:00
sql = ActiveRecord :: Base . connection ( )
2014-12-31 08:48:14 +08:00
homework_scores = Member . find_by_sql ( sql_select )
2014-12-26 14:50:14 +08:00
sql . close ( )
2014-12-31 08:48:14 +08:00
2014-12-24 18:31:45 +08:00
homework_scores
2014-12-22 10:31:09 +08:00
end
#获取课程的老师列表
def find_course_teachers course
searchTeacherAndAssistant ( course ) . map { | teacher | teacher . user_id } . join ( " , " )
end
2014-12-24 18:31:45 +08:00
#当加入,退出分班时查询分班的学生
def search_group_members group
@subPage_title = l :label_student_list
2015-03-26 10:25:54 +08:00
@render_file = 'new_member_list'
2014-12-24 18:31:45 +08:00
@canShowCode = isCourseTeacher ( User . current . id , @course ) && params [ :role ] != '1'
@is_remote = true
2014-12-29 20:46:27 +08:00
@score_sort_by = " desc "
2014-12-29 16:57:31 +08:00
page_from = params [ :page ] . nil? ? 0 : ( params [ :page ] . to_i - 1 )
2014-12-29 20:46:27 +08:00
@results = student_homework_score ( group . id , 0 , 0 , " desc " )
2014-12-29 16:57:31 +08:00
@results = paginateHelper @results , 10
2014-12-24 18:31:45 +08:00
end
2014-12-30 17:49:48 +08:00
def member_to_xls members , groups
xls_report = StringIO . new
book = Spreadsheet :: Workbook . new
sheet1 = book . create_worksheet :name = > " student "
blue = Spreadsheet :: Format . new :color = > :blue , :weight = > :bold , :size = > 10
sheet1 . row ( 0 ) . default_format = blue
sheet1 . row ( 0 ) . concat ( [ l ( :excel_user_id ) , l ( :excel_user_name ) , l ( :excel_nickname ) , l ( :excel_student_id ) , l ( :excel_mail ) , l ( :excel_homework_score ) ] )
count_row = 1
group0 = CourseGroup . new ( ) ;
group0 . id = 0 ;
group0 . name = l ( :excel_member_with_out_class )
groups_dup = groups . dup
groups_dup << group0
groups_dup . each do | group |
sheet1 [ count_row , 0 ] = l ( :excel_class )
sheet1 [ count_row , 1 ] = group . name
count_row += 1
members . each do | member |
if member . course_group_id == group . id
sheet1 [ count_row , 0 ] = member . user . id
sheet1 [ count_row , 1 ] = member . user . lastname . to_s + member . user . firstname . to_s
sheet1 [ count_row , 2 ] = member . user . login
sheet1 [ count_row , 3 ] = member . user . user_extensions . student_id
sheet1 [ count_row , 4 ] = member . user . mail
2014-12-31 09:48:17 +08:00
sheet1 [ count_row , 5 ] = format ( " %0.2f " , member . score . nil? ? 0 :member . score . to_s )
2014-12-30 17:49:48 +08:00
count_row += 1
end
end
end
book . write xls_report
xls_report . string
end
2014-12-31 17:02:55 +08:00
2014-09-24 14:47:53 +08:00
end