2015-03-13 13:41:26 +08:00
#coding=utf-8
2014-12-08 15:19:10 +08:00
class CoursesService
include ApplicationHelper
2014-12-10 20:53:41 +08:00
include CoursesHelper
2015-01-30 16:58:05 +08:00
include HomeworkAttachHelper
2015-02-06 14:29:42 +08:00
include ApiHelper
2015-07-07 17:18:29 +08:00
include ActionView :: Helpers :: DateHelper
2015-03-01 17:31:38 +08:00
2014-12-08 15:19:10 +08:00
#参数school_id为0或不传时返回所有课程, 否则返回对应学校的课程
#参数per_page_count分页功能, 每页显示的课程数
#参数page分页功能, 当前页码
2015-02-10 17:03:59 +08:00
def course_list params , current_user
2014-12-08 15:19:10 +08:00
@school_id = params [ :school_id ]
per_page_option = params [ :per_page_count ] || 10
page_no = params [ :page ] || 1
if @school_id == " 0 " || @school_id . nil?
@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 = Redmine :: Pagination :: Paginator . new @course_count , per_page_option , page_no
@courses = @courses_all . order ( " created_at desc " )
@courses = @courses . offset ( @course_pages . offset ) . limit ( @course_pages . per_page )
course_list = [ ]
@courses . each do | course |
2015-02-10 17:03:59 +08:00
course_list << { :course = > course , :img_url = > url_to_avatar ( course ) , :current_user_is_member = > current_user . member_of_course? ( course ) , :current_user_is_teacher = > is_course_teacher ( current_user , course ) }
2014-12-08 15:19:10 +08:00
end
course_list
end
2014-12-10 18:26:38 +08:00
#搜索课程
2015-02-10 16:36:29 +08:00
def search_course params , current_user
2014-12-10 18:26:38 +08:00
courses_all = Course . all_course
name = params [ :name ]
if name . blank?
raise 'sumbit empty'
end
2015-03-19 10:13:43 +08:00
@courses = courses_all . visible ( current_user )
2014-12-10 18:26:38 +08:00
if params [ :name ] . present?
2015-04-16 15:48:15 +08:00
@courses_all = @courses . like ( params [ :name ] ) . order ( " created_at desc " )
2014-12-10 18:26:38 +08:00
else
2015-04-16 15:48:15 +08:00
@courses_all = @courses . order ( " created_at desc " ) ;
2014-12-10 18:26:38 +08:00
end
@courses_all
2015-02-10 16:36:29 +08:00
course_list = [ ]
@courses_all . each do | course |
course_list << { :course = > course , :img_url = > url_to_avatar ( course ) , :current_user_is_member = > current_user . member_of_course? ( course ) , :current_user_is_teacher = > is_course_teacher ( current_user , course ) }
end
course_list
2014-12-10 18:26:38 +08:00
end
#获取头像
def get_img obj
url_to_avatar ( obj )
end
2014-12-23 11:07:17 +08:00
#课程老师或课程学生列表
2014-12-15 10:41:09 +08:00
def course_teacher_or_student_list params , course , current_user
if course . is_a? ( Course )
c = course
else
c = Course . find ( course )
end
2014-12-23 15:24:18 +08:00
if current_user . nil? || ! ( current_user . admin? || c . is_public == 1 || ( c . is_public == 0 && current_user . member_of_course? ( c ) ) )
2014-12-15 10:41:09 +08:00
raise '403'
end
@teachers = searchTeacherAndAssistant ( c )
2014-12-10 20:53:41 +08:00
#@canShowCode = isCourseTeacher(User.current.id,course) && params[:role] != '1'
case params [ :role ]
when '1'
#@subPage_title = l :label_teacher_list
2014-12-15 10:41:09 +08:00
@members = searchTeacherAndAssistant ( c )
2014-12-10 20:53:41 +08:00
when '2'
#@subPage_title = l :label_student_list
2014-12-15 10:41:09 +08:00
@members = searchStudent ( c )
2014-12-10 20:53:41 +08:00
else
#@subPage_title = ''
2014-12-15 10:41:09 +08:00
@members = c . member_principals . includes ( :roles , :principal ) . all . sort
2014-12-10 20:53:41 +08:00
end
2015-01-07 15:24:43 +08:00
users = [ ]
@members . each do | m |
img_url = url_to_avatar ( m . user )
gender = m . user . user_extensions . gender . nil? ? 0 : m . user . user_extensions . gender
work_unit = get_user_work_unit m . user
location = get_user_location m . user
2015-05-08 08:53:31 +08:00
users << { :id = > m . user . id , :img_url = > img_url , :nickname = > m . user . login , :gender = > gender , :work_unit = > work_unit , :mail = > m . user . mail , :location = > location , :brief_introduction = > m . user . user_extensions . brief_introduction , :realname = > m . user . realname }
2015-01-07 15:24:43 +08:00
end
users
end
#获取用户的工作单位
def get_user_work_unit user
work_unit = " "
if user . user_extensions . identity == 0 || user . user_extensions . identity == 1
work_unit = user . user_extensions . school . name unless user . user_extensions . school . nil?
elsif user . user_extensions . identity == 3
work_unit = user . user_extensions . occupation
elsif user . user_extensions . identity == 2
work_unit = user . firstname
end
work_unit
end
#获取用户地区
def get_user_location user
location = " "
location << ( user . user_extensions . location || '' )
location << ( user . user_extensions . location_city || '' )
location
2014-12-10 20:53:41 +08:00
end
2014-12-11 15:39:14 +08:00
#课程通知列表
2015-02-04 11:57:37 +08:00
def course_news_list params , current_user
2014-12-11 15:39:14 +08:00
if params [ :course_id ] && @course == nil
@course = Course . find ( params [ :course_id ] )
end
2015-02-04 11:57:37 +08:00
if current_user . nil? || ! ( current_user . admin? || @course . is_public == 1 || ( @course . is_public == 0 && current_user . member_of_course? ( @course ) ) )
raise '403'
end
2015-06-30 15:17:22 +08:00
scope = @course ? @course . news . order ( " news.created_on desc " ) . course_visible ( current_user ) : News . order ( " news.created_on desc " ) . course_visible ( current_user )
2015-01-20 15:17:02 +08:00
news = [ ]
scope . each do | n |
2015-06-26 17:16:03 +08:00
news << { :id = > n . id , :title = > n . title , :author_name = > n . author . name , :author_id = > n . author . id , :author = > n . author , :description = > n . description , :created_on = > format_time ( n . created_on ) , :comments_count = > n . comments_count }
2015-01-20 15:17:02 +08:00
end
news
2014-12-11 15:39:14 +08:00
end
2014-12-15 16:53:20 +08:00
#查看新闻权限验证
2015-01-20 17:42:18 +08:00
def show_course_news_authorize ( current_user )
unless current_user . allowed_to? ( { :controller = > 'news' , :action = > 'show' } , false )
2014-12-15 16:53:20 +08:00
raise '403'
end
end
2014-12-15 13:44:26 +08:00
2014-12-15 16:53:20 +08:00
#显示课程通知(包括评论) 需验证权限
def show_course_news params , current_user
@news = News . find ( params [ :id ] )
2015-02-04 14:07:00 +08:00
@course = @news . course
if @course
if current_user . nil? || ! ( current_user . admin? || @course . is_public == 1 || ( @course . is_public == 0 && current_user . member_of_course? ( @course ) ) )
raise '403'
end
end
2014-12-15 16:53:20 +08:00
@comments = @news . comments
@comments . reverse! if current_user . wants_comments_in_reverse_order?
2015-01-20 17:42:18 +08:00
{ :news = > @news , :comments = > @comments }
2015-02-04 14:07:00 +08:00
2015-01-20 17:42:18 +08:00
#comments = []
#@comments.each do |comment|
# comments << {:author_id => comment.author_id,:author_name => comment.author.name,:commont_content => comment.comments,:time => format_time(comment.created_on)}
#end
#{:title => @news.title,:author_name => @news.author.name,:author_id => @news.author.id, :description => @news.description,:created_on => format_time(@news.created_on),
# :comments_count => @news.comments_count,:comments => comments}
2014-12-15 13:44:26 +08:00
end
2014-12-15 16:53:20 +08:00
#显示课程
2015-03-07 15:36:00 +08:00
def show_course ( params , current_user )
2014-12-11 15:39:14 +08:00
course = Course . find ( params [ :id ] )
2015-04-08 18:21:36 +08:00
if course . school
work_unit = course . school . name
else
work_unit = get_user_work_unit course . teacher
end
2015-03-07 15:36:00 +08:00
unless ( course . is_public == 1 || current_user . member_of_course? ( course ) || current_user . admin? )
2014-12-15 16:53:20 +08:00
raise '403'
end
2015-05-04 17:36:48 +08:00
{ :course = > course , :work_unit = > work_unit , :img_url = > url_to_avatar ( course ) , :current_user_is_member = > current_user . member_of_course? ( course ) , :current_user_is_teacher = > is_course_teacher ( current_user , course ) , :course_student_num = > course ? course . student . count . to_s : 0 }
2014-12-11 15:39:14 +08:00
end
2014-12-11 17:11:05 +08:00
#创建课程
2014-12-23 11:07:17 +08:00
#current_user当前用户对象( 不是id)
# params[:course][:name]:课程名称
#params[:course][:password]:密码
#params[:course][:description]:描述
#params[:course][:is_public]:是否公开1公开, 0私有
#params[:course][:open_student]:是否公开学生列表1公开, 0不公开, 不公开时非课程成员无法看到学生列表
#params[:course][:course_type]:暂时默认给1值。
#params[:term]:学期(秋季学期或春季学期)
#params[:time]: 年份( 例: 2014)
#params[:setup_time]:暂不传(貌似已经没用了)
#params[:endup_time]: 暂不传(貌似已经没用了)
#params[:class_period]:学时总数
2015-01-05 16:58:11 +08:00
def create_course ( params , current_user )
2014-12-23 11:07:17 +08:00
if current_user . user_extensions . identity
2014-12-11 17:11:05 +08:00
@course = Course . new
@course . extra = 'course' + DateTime . parse ( Time . now . to_s ) . strftime ( '%Y-%m-%d_%H-%M-%S' ) . to_s
2015-01-05 16:58:11 +08:00
@course . send ( :safe_attributes = , params [ :course ] , current_user )
#@course.safe_attributes(current_user,params[:course])
@course . tea_id = current_user . id
2014-12-11 17:11:05 +08:00
@course . term = params [ :term ]
@course . time = params [ :time ]
#@course.school_id = params[:occupation]
2015-01-05 16:58:11 +08:00
@course . school_id = current_user . user_extensions . school_id
2014-12-11 17:11:05 +08:00
@course . setup_time = params [ :setup_time ]
@course . endup_time = params [ :endup_time ]
2015-04-16 10:49:58 +08:00
@course . class_period = params [ :class_period ] . to_i
2015-04-03 11:30:48 +08:00
params [ :course ] [ :is_public ] ? @course . is_public = 1 : @course . is_public = 0
params [ :course ] [ :open_student ] ? @course . open_student = 1 : @course . open_student = 0
2015-07-01 10:02:50 +08:00
else
2014-12-11 17:11:05 +08:00
end
@issue_custom_fields = IssueCustomField . sorted . all
@trackers = Tracker . sorted . all
2014-12-11 15:39:14 +08:00
2015-07-01 10:02:50 +08:00
if @course && @course . save
2014-12-11 17:11:05 +08:00
#unless User.current.admin?
r = Role . givable . find_by_id ( Setting . new_project_user_role_id . to_i ) || Role . givable . first
2015-01-05 16:58:11 +08:00
m = Member . new ( :user = > current_user , :roles = > [ r ] )
2014-12-11 17:11:05 +08:00
m . project_id = - 1
2015-01-05 16:58:11 +08:00
course = CourseInfos . new ( :user_id = > current_user . id , :course_id = > @course . id )
2014-12-11 17:11:05 +08:00
#user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id)
if params [ :course ] [ :is_public ] == '1'
course_status = CourseStatus . create ( :course_id = > @course . id , :watchers_count = > 0 , :changesets_count = > 0 , :grade = > 0 , :course_type = > @course_tag )
end
@course . members << m
@course . course_infos << course
end
2015-02-12 15:51:52 +08:00
{ :course = > @course , :img_url = > url_to_avatar ( @course ) , :current_user_is_member = > current_user . member_of_course? ( @course ) , :current_user_is_teacher = > is_course_teacher ( current_user , @course ) }
2014-12-11 15:39:14 +08:00
end
2014-12-15 16:53:20 +08:00
#验证编辑课程的权限
2014-12-23 11:07:17 +08:00
#当前
2014-12-15 16:53:20 +08:00
def edit_course_authorize ( current_user , course )
unless current_user . allowed_to? ( { :controller = > 'courses' , :action = > 'update' } , course )
raise '403'
end
end
#编辑课程 需验证权限
2014-12-23 11:07:17 +08:00
# params[:course][:name]:课程名称
#params[:course][:password]:密码
#params[:course][:description]:描述
#params[:course][:is_public]:是否公开1公开, 0私有
#params[:course][:open_student]:是否公开学生列表1公开, 0不公开, 不公开时非课程成员无法看到学生列表
#params[:course][:course_type]:暂时默认给1值。
#params[:term]:学期(秋季学期或春季学期)
#params[:time]: 年份( 例: 2014)
#params[:class_period]:学时总数
2015-01-05 17:47:04 +08:00
def edit_course ( params , course , current_user )
course . send ( :safe_attributes = , params [ :course ] , current_user )
#course.safe_attributes = params[:course]
2014-12-12 14:54:01 +08:00
course . time = params [ :time ]
course . term = params [ :term ]
2015-04-16 10:56:48 +08:00
course . class_period = params [ :class_period ] . to_i
2015-03-26 17:17:31 +08:00
params [ :course ] [ :is_public ] ? course . is_public = 1 : course . is_public = 0
params [ :course ] [ :open_student ] ? course . open_student = 1 : course . open_student = 0
2014-12-12 14:54:01 +08:00
if course . save
if params [ :course ] [ :is_public ] == '0'
course_status = CourseStatus . find_by_course_id ( course . id )
course_status . destroy if course_status
elsif params [ :course ] [ :is_public ] == '1'
course_status = CourseStatus . find_by_course_id ( course . id )
course_status . destroy if course_status
course_status = CourseStatus . create ( :course_id = > course . id , :grade = > 0 )
end
end
2015-02-12 15:51:52 +08:00
{ :course = > course , :img_url = > url_to_avatar ( course ) , :current_user_is_member = > current_user . member_of_course? ( course ) , :current_user_is_teacher = > is_course_teacher ( current_user , course ) }
2014-12-12 14:54:01 +08:00
end
#退出课程
2014-12-23 11:07:17 +08:00
#object_id: 课程id
#user: 当前用户
2015-01-07 11:37:48 +08:00
#@state == 0 退出成功
#@state == 1 不在课程中
#@state == 2 您还未登录
#@state 其他 未知错误,请稍后再试
2014-12-12 14:54:01 +08:00
def exit_course params , user
2015-01-07 11:37:48 +08:00
if user . nil?
@state = 2
return @state
end
2014-12-12 14:54:01 +08:00
@member = Member . where ( 'course_id = ? and user_id = ?' , params [ :object_id ] , user . id )
2015-01-07 11:37:48 +08:00
if @member . nil? || @member . count == 0
@state = 1
return @state
end
2014-12-12 14:54:01 +08:00
@member . first . destroy
joined = StudentsForCourse . where ( 'student_id = ? and course_id = ?' , user . id , params [ :object_id ] )
joined . each do | join |
join . delete
2015-01-07 11:37:48 +08:00
@state = 0
2014-12-12 14:54:01 +08:00
end
2015-01-07 11:37:48 +08:00
@state
2014-12-12 14:54:01 +08:00
end
#加入课程
2014-12-23 11:07:17 +08:00
#object_id: 课程id
#course_password :加入课程的密码
2014-12-12 14:54:01 +08:00
#@state == 0 加入成功
#@state == 1 密码错误
#@state == 2 课程已过期 请联系课程管理员重启课程。(在配置课程处)
#@state == 3 您已经加入了课程
#@state == 4 您加入的课程不存在
#@state == 5 您还未登录
#@state 其他 未知错误,请稍后再试
2015-01-07 11:20:27 +08:00
def join_course params , current_user
2014-12-12 14:54:01 +08:00
course = Course . find_by_id params [ :object_id ]
2015-01-07 11:20:27 +08:00
@state = 10
2014-12-12 14:54:01 +08:00
if course
if course_endTime_timeout? course
@state = 2
else
2015-01-07 11:20:27 +08:00
if current_user . member_of_course? ( course )
2014-12-12 14:54:01 +08:00
@state = 3
else
if params [ :course_password ] == course . password
members = [ ]
2015-01-07 11:20:27 +08:00
members << Member . new ( :role_ids = > [ 10 ] , :user_id = > current_user . id )
2014-12-12 14:54:01 +08:00
course . members << members
2015-01-07 11:20:27 +08:00
StudentsForCourse . create ( :student_id = > current_user . id , :course_id = > params [ :object_id ] )
2014-12-12 14:54:01 +08:00
@state = 0
else
@state = 1
end
end
end
else
@state = 4
end
2015-01-07 11:20:27 +08:00
{ :state = > @state , :course = > course }
2014-12-12 14:54:01 +08:00
end
2014-12-15 13:44:26 +08:00
#作业列表
#已提交的作业数量获取 bid.homeworks.count
#学生提问数量获取 bid.commit.nil? ? 0 : bid.commit
def homework_list params , current_user
2015-01-20 15:17:02 +08:00
course = Course . find ( params [ :id ] )
if course . is_public != 0 || current_user . member_of_course? ( course )
2015-07-09 14:32:10 +08:00
bids = course . homework_commons . page ( params [ :page ] || 1 ) . per ( 20 ) . order ( 'created_at DESC' )
2015-01-20 15:17:02 +08:00
bids = bids . like ( params [ :name ] ) if params [ :name ] . present?
2015-07-09 14:32:10 +08:00
homeworks = [ ]
bids . each do | bid |
homeworks << show_homework_info ( course , bid , current_user , is_course_teacher ( current_user , course ) )
end
homeworks
2014-12-15 13:44:26 +08:00
else
raise '403'
end
end
2015-02-06 15:52:34 +08:00
def course_dynamic ( params , current_user )
2015-03-03 09:11:47 +08:00
@user = User . find ( params [ :id ] )
2015-04-15 10:30:56 +08:00
if current_user . nil? && ! current_user . admin? && ! @user . active?
2015-03-03 09:11:47 +08:00
raise '404'
return
2015-02-06 15:52:34 +08:00
end
2015-03-03 09:11:47 +08:00
if current_user == @user || current_user . admin?
membership = @user . coursememberships . all
2015-02-06 15:52:34 +08:00
else
2015-03-03 09:11:47 +08:00
membership = @user . coursememberships . all ( :conditions = > Course . visible_condition ( current_user ) )
2015-02-06 15:52:34 +08:00
end
2015-03-09 15:23:24 +08:00
if membership . nil? || membership . count == 0
2015-03-19 14:41:31 +08:00
raise l ( :label_no_courses , :locale = > get_user_language ( current_user ) )
2015-03-09 15:23:24 +08:00
end
2015-03-03 09:11:47 +08:00
membership . sort! { | older , newer | newer . created_on < = > older . created_on }
2015-03-01 16:32:10 +08:00
result = [ ]
2015-03-03 09:11:47 +08:00
membership . each do | mp |
course = mp . course
2015-03-09 15:23:24 +08:00
latest_course_dynamics = [ ]
latest_news = course . news . order ( " created_on desc " ) . first
unless latest_news . nil?
2015-05-27 08:56:34 +08:00
latest_course_dynamics << { :type = > 1 , :time = > latest_news . created_on ,
:message = > latest_news . author . realname << l ( :label_recently_updated_notification , :locale = > get_user_language ( current_user ) ) << " : " << latest_news . title }
2015-03-09 15:23:24 +08:00
end
latest_message = course . journals_for_messages . order ( " created_on desc " ) . first
unless latest_message . nil?
2015-05-27 08:56:34 +08:00
latest_course_dynamics << { :type = > 2 , :time = > latest_message . created_on , :message = > latest_message . user . realname << l ( :label_recently_updated_message , :locale = > get_user_language ( current_user ) ) << " : " <<latest_message.notes}
2015-03-09 15:23:24 +08:00
end
latest_attachment = course . attachments . order ( " created_on desc " ) . first
unless latest_attachment . nil?
2015-05-27 08:56:34 +08:00
latest_course_dynamics << { :type = > 3 , :time = > latest_attachment . created_on , :message = > latest_attachment . author . realname << l ( :label_recently_updated_courseware , :locale = > get_user_language ( current_user ) ) << " : " <<latest_attachment.filename}
2015-03-09 15:23:24 +08:00
end
2015-06-08 17:16:34 +08:00
latest_bid = course . homework_commons . order ( 'updated_at DESC' ) . first
2015-03-09 15:23:24 +08:00
unless latest_bid . nil?
2015-06-08 17:16:34 +08:00
latest_course_dynamics << { :type = > 4 , :time = > latest_bid . updated_at , :message = > latest_bid . user . realname << l ( :label_recently_updated_homework , :locale = > get_user_language ( current_user ) ) << " : " <<latest_bid.name}
2015-03-09 15:23:24 +08:00
end
2015-04-07 16:32:53 +08:00
# Time 2015-04-07 14:58:30
# Author lizanle
# Description 添加课程创建动态
if ( User . find_by_id ( CourseInfos . find_by_course_id ( course . id ) . try ( :user_id ) ) )
create_user_name = User . find_by_id ( CourseInfos . find_by_course_id ( course . id ) . user_id ) . realname
latest_course_dynamics << { :type = > 5 , :time = > course . created_at , :message = > l ( :label_recently , :locale = > get_user_language ( current_user ) ) << create_user_name << l ( :label_creat , :locale = > get_user_language ( current_user ) ) }
end
2015-03-09 15:23:24 +08:00
#每个作业中的最新留言
2015-06-08 17:16:34 +08:00
# messages = []
# course.homework_commons.each do |hc|
# jour = hc.journals_for_messages.order("created_on desc").first
# unless jour.nil?
# messages << jour
# end
# end
# unless messages.count == 0
# messages.sort!{|order,newer| newer.created_on <=> order.created_on}
# end
# latest_bid_message = messages.first
# unless latest_bid_message.nil?
# latest_course_dynamics << {:type => 4,:time => latest_bid_message.created_on,:message => l(:label_recently_updated_message,:locale => get_user_language(current_user))}
# end
2015-03-09 15:23:24 +08:00
#每个作业中学生最后提交的作业
homeworks = [ ]
2015-06-08 17:16:34 +08:00
course . homework_commons . each do | bid |
homework_attach = bid . student_works . order ( 'updated_at DESC' ) . first
2015-03-09 15:23:24 +08:00
unless homework_attach . nil?
homeworks << homework_attach
end
end
unless homeworks . count == 0
homeworks . sort! { | order , newer | newer . updated_at < = > order . updated_at }
end
latest_homework_attach = homeworks . first
unless latest_homework_attach . nil?
2015-05-27 08:56:34 +08:00
latest_course_dynamics << { :type = > 4 , :time = > latest_homework_attach . updated_at , :message = > latest_homework_attach . user . realname << l ( :label_recently_updated_homework , :locale = > get_user_language ( current_user ) ) << " : " << ( latest_homework_attach . name . nil? ? latest_homework_attach . description : latest_homework_attach . name ) }
2015-03-09 15:23:24 +08:00
end
latest_course_dynamics . sort! { | order , newer | newer [ :time ] < = > order [ :time ] }
latest_course_dynamic = latest_course_dynamics . first
unless latest_course_dynamic . nil?
2015-03-18 17:21:19 +08:00
result << { :course_name = > course . name , :course_id = > course . id , :course_img_url = > url_to_avatar ( course ) , :course_time = > course . time , :course_term = > course . term , :type = > latest_course_dynamic [ :type ] , :update_time = > latest_course_dynamic [ :time ] , :message = > latest_course_dynamic [ :message ] , :count = > nil }
2015-03-03 09:11:47 +08:00
end
end
2015-03-09 17:32:38 +08:00
result . sort! { | order , newer | newer [ :update_time ] < = > order [ :update_time ] }
2015-03-01 16:32:10 +08:00
result
2015-02-06 14:29:42 +08:00
end
2015-04-29 17:46:39 +08:00
# 课程课件
def course_attachments params
result = [ ]
2015-07-03 15:32:57 +08:00
course = Course . find ( params [ :course_id ] )
attachments = course . attachments . order ( " created_on " )
2015-05-05 17:19:06 +08:00
if ! params [ :name ] . nil? && params [ :name ] != " "
2015-07-03 15:32:57 +08:00
attachments . each do | atta |
result << atta if atta . filename . include? ( params [ :name ] )
2015-05-05 17:19:06 +08:00
end
else
2015-07-03 15:32:57 +08:00
result = attachments
2015-04-29 17:46:39 +08:00
end
result
end
2015-05-04 17:36:48 +08:00
# 课程学生列表
def course_members params
2015-05-05 17:19:06 +08:00
@all_members = searchmember_by_name ( student_homework_score ( 0 , params [ :course_id ] , 10 , " desc " ) , params [ :name ] )
2015-05-04 17:36:48 +08:00
end
2015-05-13 09:56:34 +08:00
def show_member_score params
@member_score = Member . find ( params [ :member_id ] ) if params [ :member_id ]
atta = @member_score . student_homework_score [ 0 ]
result = [ ]
atta . each do | t |
if ! params [ :homeworkName ] . nil? && params [ :homeworkName ] != " "
result << { :name = > t [ :name ] , :score = > t [ :score ] } if t [ :name ] . include? ( params [ :homeworkName ] )
else
result << { :name = > t [ :name ] , :score = > t [ :score ] }
end
end
result
end
2015-05-15 09:06:36 +08:00
# 设置人员为课程教辅
def set_as_assitant_teacher params
members = [ ]
#找到课程
course = Course . find ( params [ :course_id ] )
#新建课程人员
member = Member . new ( :role_ids = > [ 7 ] , :user_id = > params [ :user_id ] , :course_id = > params [ :course_id ] )
joined = StudentsForCourse . where ( 'student_id = ? and course_id = ?' , member . user_id , course . id )
joined . each do | join |
join . delete
end
member . course_group_id = 0
members << member
course . members << members
#将课程人员设置为教辅
end
def del_assitant_teacher params
member = Member . where ( " user_id = ? and course_id = ? " , params [ :user_id ] , params [ :course_id ] )
member . each do | m |
m . destroy
end
user_admin = CourseInfos . where ( " user_id = ? and course_id = ? " , params [ :user_id ] , params [ :course_id ] )
if user_admin . size > 0
user_admin . each do | user |
user . destroy
end
end
joined = StudentsForCourse . where ( 'student_id = ? and course_id = ?' , params [ :user_id ] , params [ :course_id ] )
joined . each do | join |
join . delete
end
end
2015-05-15 16:13:32 +08:00
def create_course_notice params , current_user
n = News . new ( :course_id = > params [ :course_id ] , :author_id = > current_user . id , :title = > params [ :title ] , :description = > params [ :desc ] )
n . save
{ :id = > n . id , :title = > n . title , :author_name = > n . author . name , :author_id = > n . author . id , :description = > n . description , :created_on = > format_time ( n . created_on ) , :comments_count = > n . comments_count }
end
2015-01-20 15:17:02 +08:00
private
2015-05-05 17:19:06 +08:00
def searchmember_by_name members , name
#searchPeopleByRoles(project, StudentRoles)
mems = [ ]
if name != " "
name = name . to_s . downcase
members . each do | m |
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 ) )
mems << m
end
end
else
mems = members
end
mems
end
2015-01-30 16:58:05 +08:00
def show_homework_info course , bid , current_user , is_course_teacher
2015-06-08 17:16:34 +08:00
author_real_name = bid . user . lastname + bid . user . firstname
many_times = course . homework_commons . index ( bid ) + 1
2015-01-20 15:17:02 +08:00
name = bid . name
2015-06-08 17:16:34 +08:00
homework_count = bid . student_works . count #已提交的作业数量
#student_questions_count = bid.journals_for_messages.where('m_parent_id IS NULL').count
2015-01-20 15:17:02 +08:00
description = bid . description
#if is_course_teacher(User.current, course) && @bid.open_anonymous_evaluation == 1 && @bid.homeworks.count >= 2
2015-06-26 09:18:12 +08:00
#state = bid.homework_detail_manual.comment_status
if ! bid . nil?
if bid . homework_type == 1 && bid . homework_detail_manual
case bid . homework_detail_manual . comment_status
when 1
state = show_homework_deadline bid
when 2
state = " 正在匿评中 "
when 3
state = " 匿评已结束 "
end
elsif bid . homework_type == 0
state = " 未启用匿评 "
elsif bid . homework_type == 2
state = " 编程作业 "
else
end
end
2015-07-09 14:32:10 +08:00
# studentlist = []
# bid.student_works.order("created_at desc").page(1).per(6).each do |work|
# studentlist << work.user
# end
2015-07-09 14:57:27 +08:00
# unless is_course_teacher
# homework_for_anonymous_comments = get_student_batch_homework_list bid,current_user
# end
2015-01-20 15:17:02 +08:00
#end
2015-06-08 17:16:34 +08:00
open_anonymous_evaluation = bid . homework_detail_manual . comment_status
2015-07-09 14:32:10 +08:00
{ :course_name = > course . name , :course_id = > course . id , :id = > bid . id ,
:author = > bid . user , :author_real_name = > author_real_name ,
:homework_times = > many_times , :homework_name = > name ,
:homework_count = > homework_count , :student_questions_count = > 0 ,
:description = > description , :homework_state = > state ,
:open_anonymous_evaluation = > open_anonymous_evaluation ,
2015-07-09 14:57:27 +08:00
#:homework_for_anonymous_comments => homework_for_anonymous_comments,
2015-07-09 14:32:10 +08:00
:created_on = > bid . created_at , :deadline = > bid . end_time ,
:homework_notsubmit_num = > bid . course . members . count - bid . student_works . count ,
:homework_submit_num = > bid . student_works . count ,
:homework_status_student = > get_homework_status ( bid ) , :homework_status_teacher = > homework_status_desc ( bid ) ,
:student_evaluation_part = > get_evaluation_part ( bid , 3 ) ,
:ta_evaluation_part = > get_evaluation_part ( bid , 2 ) , :homework_anony_type = > bid . homework_type == 1 && ! bid . homework_detail_manual . nil? }
2015-03-03 17:36:05 +08:00
2015-01-30 16:58:05 +08:00
end
#显示作业列表的同时显示分配给当前学生匿评的作业
def show_homework_info_with_batch course , bid
author = bid . author . lastname + bid . author . firstname
many_times = course . homeworks . index ( bid ) + 1
name = bid . name
homework_count = bid . homeworks . count #已提交的作业数量
2015-04-09 16:25:19 +08:00
student_questions_count = bid . journals_for_messages . where ( 'm_parent_id IS NULL' ) . count
2015-01-30 16:58:05 +08:00
description = bid . description
#if is_course_teacher(User.current, course) && @bid.open_anonymous_evaluation == 1 && @bid.homeworks.count >= 2
state = bid . comment_status
#end
open_anonymous_evaluation = bid . open_anonymous_evaluation
2015-01-20 15:17:02 +08:00
{ :course_name = > course . name , :id = > bid . id , :course_teacher = > author , :homework_times = > many_times , :homework_name = > name , :homework_count = > homework_count , :student_questions_count = > student_questions_count ,
:description = > description , :homework_state = > state , :open_anonymous_evaluation = > open_anonymous_evaluation }
end
2015-02-06 14:29:42 +08:00
2015-05-04 17:36:48 +08:00
def student_homework_score ( groupid , course_id , nums , score_sort_by )
#teachers = find_course_teachers(@course)
#start_from = start_from * nums
sql_select = " "
if groupid == 0
if nums == 0
sql_select = " SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
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}"
else
sql_select = " SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
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}"
end
else
sql_select = " SELECT members.*, SUM(homework_attaches.score) as score FROM members, homework_attaches
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}"
end
sql = ActiveRecord :: Base . connection ( )
homework_scores = Member . find_by_sql ( sql_select )
sql . close ( )
homework_scores
end
2015-06-19 15:05:25 +08:00
#app新版api
#
#
#课程动态
public
def all_course_dynamics params , current_user
#获取当前用户的所有课程
@user = User . find ( params [ :id ] )
if current_user . nil? && ! current_user . admin? && ! @user . active?
raise '404'
return
end
if current_user == @user || current_user . admin?
2015-07-17 15:19:28 +08:00
membership = @user . coursememberships . page ( 1 ) . per ( 15 )
2015-06-19 15:05:25 +08:00
else
2015-07-17 15:19:28 +08:00
membership = @user . coursememberships . page ( 1 ) . per ( 15 ) . all ( :conditions = > Course . visible_condition ( current_user ) )
2015-06-19 15:05:25 +08:00
end
if membership . nil? || membership . count == 0
raise l ( :label_no_courses , :locale = > get_user_language ( current_user ) )
end
membership . sort! { | older , newer | newer . created_on < = > older . created_on }
#定义一个数组集合, 存放hash数组, 该hash数组包括课程的信息, 并包含课程的最新发布的资源, 最新的讨论区留言, 最新的作业, 最新的通知
result = [ ]
#对用户所有的课程进行循环,找到每个课程最新发布的资源,最新的讨论区留言,最新的作业,最新的通知,并存进数组
membership . each do | mp |
course = mp . course
latest_course_dynamics = [ ]
2015-07-07 17:18:29 +08:00
2015-06-19 15:05:25 +08:00
# 课程通知
2015-06-30 15:17:22 +08:00
latest_news = course . news . page ( 1 ) . per ( 2 ) . order ( " created_on desc " )
unless latest_news . first . nil?
latest_course_dynamics << { :type = > 1 , :time = > latest_news . first . created_on , :count = > course . news . count ,
:news = > latest_news . all }
2015-06-19 15:05:25 +08:00
end
2015-02-06 14:29:42 +08:00
2015-06-19 15:05:25 +08:00
# 课程讨论区
2015-07-16 17:56:35 +08:00
latest_message = course . boards . first . topics . page ( 1 ) . per ( 2 )
unless latest_message . first . nil?
latest_course_dynamics << { :type = > 2 , :time = > latest_message . first . created_on , :count = > course . boards . nil? ? 0 : course . boards . first . topics . count ,
:topics = > latest_message . all }
end
2015-05-13 09:56:34 +08:00
2015-06-19 15:05:25 +08:00
# 课程资源
2015-07-02 17:31:30 +08:00
# latest_attachment = course.attachments.order("created_on desc").page(1).per(2)
# unless latest_attachment.first.nil?
# latest_course_dynamics << {:type => 3, :time => latest_attachment.first.created_on,:count =>course.attachments.count , :documents=>latest_attachment}
# dynamics_count += 1
# end
2015-05-13 09:56:34 +08:00
2015-06-19 15:05:25 +08:00
#课程作业 已经交的学生列表( 暂定显示6人) , 未交的学生列表, 作业的状态
2015-06-30 15:17:22 +08:00
homeworks = course . homework_commons . page ( 1 ) . per ( 2 ) . order ( 'created_at desc' )
unless homeworks . first . nil?
latest_course_dynamics << { :type = > 4 , :time = > homeworks . first . updated_at , :count = > course . homework_commons . count , :homeworks = > homeworks }
2015-06-19 15:05:25 +08:00
end
2015-07-07 17:18:29 +08:00
latest_course_dynamics . sort! { | order , newer | newer [ :time ] < = > order [ :time ] }
# 课程学霸 学生总分数排名靠前的5个人
homework_count = course . homework_commons . count
2015-07-13 11:10:04 +08:00
sql = " select users.*,ROUND(sum(student_works.final_score),2) score from student_works left outer join users on student_works.user_id = users.id " <<
" where homework_common_id in ( select id from homework_commons where homework_commons.course_id = #{ course . id } ) GROUP BY student_works.user_id ORDER BY score desc limit 0,4 "
2015-07-07 17:18:29 +08:00
better_students = User . find_by_sql ( sql )
# 找出在课程讨论区发帖回帖数最多的
active_students = [ ]
2015-07-13 11:10:04 +08:00
sql1 = " select users.*,count(author_id)*2 active_count from messages " <<
2015-07-07 17:18:29 +08:00
" LEFT JOIN users on messages.author_id = users.id " <<
" where messages.board_id in (select id from boards where boards.course_id = #{ course . id } ) " <<
" GROUP BY messages.author_id ORDER BY count(author_id) desc " <<
" limit 0,4 "
active_students = User . find_by_sql ( sql1 )
if homework_count != 0 && ! better_students . empty?
2015-07-20 15:03:42 +08:00
latest_course_dynamics << { :type = > 6 , :time = > " 1970-01-01 0:0:0 +0800 " , :count = > 4 , :better_students = > better_students }
2015-07-07 17:18:29 +08:00
end
unless active_students . empty?
2015-07-20 15:03:42 +08:00
latest_course_dynamics << { :type = > 7 , :time = > " 1970-01-01 0:0:0 +0800 " , :count = > 4 , :active_students = > active_students }
2015-07-07 17:18:29 +08:00
end
2015-06-19 15:05:25 +08:00
latest_course_dynamic = latest_course_dynamics . first
unless latest_course_dynamic . nil?
2015-07-07 17:18:29 +08:00
result << { :course_name = > course . name , :current_user_is_member = > current_user . member_of_course? ( course ) , :current_user_is_teacher = > is_course_teacher ( current_user , course ) , :course_id = > course . id , :course_img_url = > url_to_avatar ( course ) , :course_time = > course . time , :course_term = > course . term , :message = > " " , :dynamics = > latest_course_dynamics ,
2015-07-20 15:03:42 +08:00
:course_student_num = > course ? course . members . count : 0 , :time_from_now = > distance_of_time_in_words ( Time . now , latest_course_dynamic [ :time ] . to_time ) << " 前 " , :time = > latest_course_dynamic [ :time ] . to_time }
2015-06-19 15:05:25 +08:00
end
end
#返回数组集合
2015-07-20 15:03:42 +08:00
result . sort! { | order , newer | newer [ :time ] < = > order [ :time ] }
2015-06-19 15:05:25 +08:00
result
end
2015-05-15 09:06:36 +08:00
2015-07-01 16:56:03 +08:00
# 获取课程历次作业的学生总成绩
def students_score_list params , current_user
page = ( params [ :page ] || 1 ) - 1
2015-07-09 14:32:10 +08:00
user_list = [ ]
max_size = 0
2015-07-08 17:36:17 +08:00
if params [ :type ] == 1
2015-07-13 11:10:04 +08:00
sql = " select users.*,ROUND(sum(student_works.final_score),2) score from student_works left outer join users on student_works.user_id = users.id " <<
2015-07-10 17:58:42 +08:00
" where homework_common_id in ( select id from homework_commons where homework_commons.course_id = #{ params [ :course_id ] } ) GROUP BY student_works.user_id ORDER BY score desc limit #{ page * 10 } ,10 "
2015-07-09 14:32:10 +08:00
sql_count = " select count(distinct(student_works.user_id) ) " <<
" from student_works left outer join users on student_works.user_id = users.id " <<
" where homework_common_id in " <<
" ( select id from homework_commons where homework_commons.course_id = #{ params [ :course_id ] } ) "
max_size = ActiveRecord :: Base . connection ( ) . select_value ( sql_count )
user_list = User . find_by_sql ( sql )
2015-07-08 17:36:17 +08:00
else
sql1 = " select users.*,count(author_id)*2 active_count from messages " <<
" LEFT JOIN users on messages.author_id = users.id " <<
" where messages.board_id in (select id from boards where boards.course_id = #{ params [ :course_id ] } ) " <<
" GROUP BY messages.author_id ORDER BY count(author_id) desc " <<
" limit #{ page * 10 } ,10 "
2015-07-09 14:32:10 +08:00
sql1_count = " select count(DISTINCT(messages.author_id)) " <<
" from messages LEFT JOIN users on messages.author_id = users.id " <<
" where messages.board_id in (select id from boards where boards.course_id = #{ params [ :course_id ] } ) "
max_size = ActiveRecord :: Base . connection ( ) . select_value ( sql1_count )
user_list = User . find_by_sql ( sql1 )
2015-07-08 17:36:17 +08:00
end
2015-07-09 14:32:10 +08:00
{ :user_list = > user_list , :max_size = > max_size }
2015-07-01 16:56:03 +08:00
end
2015-07-02 17:31:30 +08:00
# 获取某次作业的所有作业列表
def student_work_list params , current_user
2015-07-18 17:31:29 +08:00
is_teacher = current_user . allowed_to? ( :as_teacher , Course . find ( params [ :course_id ] ) )
2015-07-02 17:31:30 +08:00
homework = HomeworkCommon . find ( params [ :homework_id ] )
student_works = [ ]
#老师 || 非匿评作业 || 匿评结束 显示所有的作品
show_all = is_teacher || homework . homework_type != 1 || homework . homework_detail_manual . comment_status == 3
if show_all
if homework . homework_type == 1 || is_teacher || current_user . admin?
student_works = homework . student_works . page ( params [ :page ] || 1 ) . per ( 10 ) . order ( " final_score desc " )
else
my_work = homework . student_works . where ( :user_id = > current_user . id )
if my_work . empty?
student_works = [ ]
else
student_works = homework . student_works . page ( params [ :page ] || 1 ) . per ( 10 ) . order ( " final_score desc " )
end
end
else #学生
if homework . homework_detail_manual . comment_status == 1 #未开启匿评,只显示我的作品
student_works = homework . student_works . where ( :user_id = > current_user . id ) . page ( params [ :page ] || 1 ) . per ( 10 )
elsif homework . homework_detail_manual . comment_status == 2 #匿评列表,显示匿评作品和我的作品
#is_evaluation = true
my_work = homework . student_works . where ( :user_id = > current_user . id ) . page ( params [ :page ] || 1 ) . per ( 10 )
student_works = my_work + current_user . student_works_evaluation_distributions . map ( & :student_work ) . select { | work | work . homework_common_id == homework . id }
end
end
student_works
end
2015-07-16 17:56:35 +08:00
# 获取课程的讨论区信息
def board_message_list params , current_user
# 课程讨论区
course = Course . find ( params [ :course_id ] )
latest_message = course . boards . first . topics . page ( params [ :page ] || 1 ) . per ( 10 )
end
#获取回复列表
def board_message_reply_list params , current_user
board = Board . find ( params [ :board_id ] )
reply_list = board . topics . where ( " id = #{ params [ :msg_id ] } " ) . first . children . order ( " created_on desc " ) . page ( params [ :page ] || 1 ) . per ( 10 )
end
#回复讨论区
def board_message_reply params , current_user
author = Message . find ( params [ :parent_id ] ) . author
quote = " <blockquote> " << author . realname << " ( " << author . nickname << " )写到: <br/> " << params [ :quote ] << " <div class='cl'></div></blockquote> "
reply = Message . new
reply . author = current_user
reply . board = Board . find ( params [ :board_id ] )
params [ :reply ] = { }
params [ :reply ] [ :subject ] = params [ :subject ] #本回复标题
params [ :reply ] [ :content ] = params [ :content ] #本回复内容
params [ :reply ] [ :quote ] = { }
params [ :reply ] [ :quote ] [ :quote ] = params [ :quote ] #本回复引用的内容, 也是父id内容
params [ :reply ] [ :parent_topic ] = params [ :parent_id ] # 父id
params [ :reply ] [ :board_id ] = params [ :board_id ] #讨论区id
params [ :reply ] [ :id ] = params [ :root_id ] #根id
reply . safe_attributes = params [ :reply ]
if params [ :root_id ] == params [ :parent_id ]
reply . content = reply . content
else
reply . content = quote + reply . content
end
Message . find ( params [ :root_id ] ) . children << reply
reply
end
2015-07-04 15:53:01 +08:00
# # 开启匿评
# #statue 1:启动成功, 2: 启动失败, 作业总数大于等于2份时才能启动匿评, 3:已开启匿评,请务重复开启,4:没有开启匿评的权限
# def start_anonymous_comment params,current_user
# homework = HomeworkCommon.find(params[:homework_id])
# return {:status=>4} unless current_user.admin? || current_user.allowed_to?(:as_teacher,Course.find(params[:course_id]))
# return {:status=>5} if Time.parse(homework.end_time.to_s).strftime("%Y-%m-%d") >= Time.now.strftime("%Y-%m-%d")
# homework_detail_manual = homework.homework_detail_manual
# if homework_detail_manual.comment_status == 1
# student_works = homework.student_works
# if student_works && student_works.size >=2
# student_works.each_with_index do |work, index|
# user = work.user
# n = homework_detail_manual.evaluation_num
# n = n < student_works.size ? n : student_works.size - 1
# assigned_homeworks = get_assigned_homeworks(student_works, n, index)
# assigned_homeworks.each do |h|
# student_works_evaluation_distributions = StudentWorksEvaluationDistribution.new(user_id: user.id, student_work_id: h.id)
# student_works_evaluation_distributions.save
# end
# end
# homework_detail_manual.update_column('comment_status', 2)
# statue = 1
# else
# statue = 2
# end
# else
# statue = 3
# end
# {:status => statue}
# end
#
# def get_assigned_homeworks(student_works, n, index)
# student_works += student_works
# student_works[index + 1 .. index + n]
# end
2015-07-03 12:22:33 +08:00
2015-06-30 15:17:22 +08:00
end
2015-05-15 09:06:36 +08:00