64 lines
1.6 KiB
Ruby
64 lines
1.6 KiB
Ruby
#coding=utf-8
|
|
|
|
class SyllabusesService
|
|
|
|
include ApplicationHelper
|
|
include CoursesHelper
|
|
|
|
#获取指定用户的课程大纲
|
|
def user_syllabus(user)
|
|
courses = CoursesService.new.user_courses_list(user)
|
|
|
|
other = Syllabus.new(title: '未命名课程')
|
|
|
|
courses.each do |c|
|
|
other.courses << c[:course] unless c[:course].syllabus
|
|
end
|
|
|
|
user.syllabuses.each do |syllabus|
|
|
syllabus.courses = syllabus.courses.not_deleted
|
|
end
|
|
|
|
user.syllabuses.to_a << other
|
|
end
|
|
|
|
def after_create_course(course, user)
|
|
#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, :roles => [r])
|
|
m.project_id = -1
|
|
course_info = CourseInfos.new(:user_id => user.id, :course_id => course.id)
|
|
#user_grades = UserGrade.create(:user_id => User.current.id, :course_id => @course.id)
|
|
course.members << m
|
|
course.course_infos << course_info
|
|
end
|
|
|
|
#创建大纲
|
|
# params {title: '大纲名称', [{course}, {course}]}
|
|
def create(user, title, courses = [])
|
|
sy = Syllabus.new(title: title, user_id: user.id)
|
|
ActiveRecord::Base.transaction() do
|
|
sy.save!
|
|
|
|
courses.each do |course|
|
|
if ::Course === course
|
|
course.syllabus_id = sy.id
|
|
course.save!
|
|
elsif Hash === course
|
|
c = ::Course.new(course)
|
|
c.tea_id = user.id
|
|
c.syllabus_id = sy.id
|
|
c.update_default_value
|
|
c.is_public = 0
|
|
c.save!
|
|
after_create_course(c, user)
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
sy
|
|
end
|
|
|
|
|
|
end |