socialforge/app/services/courses_service.rb

208 lines
7.0 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class CoursesService
include ApplicationHelper
include CoursesHelper
#TODO:尚未整合权限系统
#参数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
#搜索课程
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
#课程老师或课程学生列表 TODO:更新业务逻辑,当前版本未包含分班功能
def course_teacher_or_student_list params,course,current_user
if course.is_a?(Course)
c = course
else
c = Course.find(course)
end
if !(current_user.admin? || c.is_public == 1 || (c.is_public == 0 && current_user.member_of_course?(c)))
raise '403'
end
@teachers= searchTeacherAndAssistant(c)
#@canShowCode = isCourseTeacher(User.current.id,course) && params[:role] != '1'
case params[:role]
when '1'
#@subPage_title = l :label_teacher_list
@members = searchTeacherAndAssistant(c)
when '2'
#@subPage_title = l :label_student_list
@members = searchStudent(c)
else
#@subPage_title = ''
@members = c.member_principals.includes(:roles, :principal).all.sort
end
@members
end
#课程通知列表
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
end
#显示课程通知
def show_course_news
end
def show_course params
course = Course.find(params[:id])
course
end
#创建课程
def create_course params
if User.current.user_extensions.identity
@course = Course.new
@course.extra = 'course' + DateTime.parse(Time.now.to_s).strftime('%Y-%m-%d_%H-%M-%S').to_s
@course.safe_attributes = params[:course]
@course.tea_id = User.current.id
@course.term = params[:term]
@course.time = params[:time]
#@course.school_id = params[:occupation]
@course.school_id = User.current.user_extensions.school_id
@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
if @course.save
#unless User.current.admin?
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 => @course.id)
#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
end
#编辑课程
def edit_course params,course
course.safe_attributes = params[:course]
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
#退出课程
def exit_course params,user
@member = Member.where('course_id = ? and user_id = ?', params[:object_id], user.id)
@member.first.destroy
joined = StudentsForCourse.where('student_id = ? and course_id = ?', user.id, params[:object_id])
joined.each do |join|
join.delete
end
end
#加入课程
#@state == 0 加入成功
#@state == 1 密码错误
#@state == 2 课程已过期 请联系课程管理员重启课程。(在配置课程处)
#@state == 3 您已经加入了课程
#@state == 4 您加入的课程不存在
#@state == 5 您还未登录
#@state 其他 未知错误,请稍后再试
def join_course params
course = Course.find_by_id params[:object_id]
if course
if course_endTime_timeout? course
@state = 2
else
if User.current.member_of_course?(course)
@state = 3
else
if params[:course_password] == course.password
members = []
members << Member.new(:role_ids => [10], :user_id => User.current.id)
course.members << members
StudentsForCourse.create(:student_id => User.current.id, :course_id => params[:object_id])
@state = 0
else
@state = 1
end
end
end
else
@state = 4
end
[@state,course]
end
#作业列表
#已提交的作业数量获取 bid.homeworks.count
#学生提问数量获取 bid.commit.nil? ? 0 : bid.commit
def homework_list params,current_user
if @course.is_public != 0 || current_user.member_of_course?(@course)
@offset, @limit = api_offset_and_limit({:limit => 10})
@bids = @course.homeworks.order('deadline DESC')
@bids = @bids.like(params[:name]) if params[:name].present?
else
raise '403'
end
end
end