2014-12-08 15:19:10 +08:00
|
|
|
|
class CoursesService
|
|
|
|
|
include ApplicationHelper
|
2014-12-10 20:53:41 +08:00
|
|
|
|
include CoursesHelper
|
2014-12-15 13:44:26 +08:00
|
|
|
|
#TODO:尚未整合权限系统
|
2014-12-08 15:19:10 +08:00
|
|
|
|
#参数school_id为0或不传时返回所有课程,否则返回对应学校的课程
|
|
|
|
|
#参数per_page_count分页功能,每页显示的课程数
|
|
|
|
|
#参数page分页功能,当前页码
|
|
|
|
|
def course_list params
|
|
|
|
|
@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|
|
|
|
|
|
course_list << {:course => course,:img_url => url_to_avatar(course)}
|
|
|
|
|
end
|
|
|
|
|
course_list
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-10 18:26:38 +08:00
|
|
|
|
#搜索课程
|
|
|
|
|
def search_course params
|
|
|
|
|
courses_all = Course.all_course
|
|
|
|
|
name = params[:name]
|
|
|
|
|
if name.blank?
|
|
|
|
|
raise 'sumbit empty'
|
|
|
|
|
end
|
|
|
|
|
@courses = courses_all.visible
|
|
|
|
|
if params[:name].present?
|
|
|
|
|
@courses_all = @courses.like(params[:name])
|
|
|
|
|
else
|
|
|
|
|
@courses_all = @courses;
|
|
|
|
|
end
|
|
|
|
|
@courses_all
|
|
|
|
|
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
|
|
|
|
|
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}
|
|
|
|
|
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
|
|
|
|
#课程通知列表
|
|
|
|
|
def course_news_list params
|
|
|
|
|
if params[:course_id] && @course==nil
|
|
|
|
|
@course = Course.find(params[:course_id])
|
|
|
|
|
end
|
|
|
|
|
scope = @course ? @course.news.course_visible : News.course_visible
|
2015-01-20 15:17:02 +08:00
|
|
|
|
news = []
|
|
|
|
|
scope.each do |n|
|
2015-01-20 17:42:18 +08:00
|
|
|
|
news << {: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}
|
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])
|
|
|
|
|
@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}
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#显示课程
|
|
|
|
|
def show_course(params,currnet_user)
|
2014-12-11 15:39:14 +08:00
|
|
|
|
course = Course.find(params[:id])
|
2014-12-15 16:53:20 +08:00
|
|
|
|
unless (course.is_public == 1 || currnet_user.member_of_course?(@course)|| currnet_user.admin?)
|
|
|
|
|
raise '403'
|
|
|
|
|
end
|
2014-12-11 15:39:14 +08:00
|
|
|
|
course
|
|
|
|
|
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]
|
|
|
|
|
@course.class_period = params[:class_period]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@issue_custom_fields = IssueCustomField.sorted.all
|
|
|
|
|
@trackers = Tracker.sorted.all
|
2014-12-11 15:39:14 +08:00
|
|
|
|
|
2014-12-11 17:11:05 +08:00
|
|
|
|
if @course.save
|
|
|
|
|
#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
|
|
|
|
|
@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]
|
|
|
|
|
course.class_period = params[:class_period]
|
|
|
|
|
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
|
|
|
|
|
course
|
|
|
|
|
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)
|
|
|
|
|
bids = course.homeworks.order('deadline DESC')
|
|
|
|
|
bids = bids.like(params[:name]) if params[:name].present?
|
|
|
|
|
homeworks = []
|
|
|
|
|
if is_course_teacher(current_user,course)
|
|
|
|
|
bids.each do |bid|
|
|
|
|
|
homeworks << show_homework_info(course,bid)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
homeworks
|
2014-12-15 13:44:26 +08:00
|
|
|
|
else
|
|
|
|
|
raise '403'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2015-01-20 15:17:02 +08:00
|
|
|
|
private
|
|
|
|
|
def show_homework_info course,bid
|
|
|
|
|
author = bid.author.lastname + bid.author.firstname
|
|
|
|
|
many_times = course.homeworks.index(bid) + 1
|
|
|
|
|
name = bid.name
|
|
|
|
|
homework_count = bid.homeworks.count #已提交的作业数量
|
|
|
|
|
student_questions_count = bid.commit.nil? ? 0 : bid.commit
|
|
|
|
|
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
|
|
|
|
|
{: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
|
|
|
|
|
|
2014-12-08 15:19:10 +08:00
|
|
|
|
end
|