socialforge/app/services/syllabuses_service.rb

145 lines
3.7 KiB
Ruby
Raw Normal View History

2016-07-04 16:19:05 +08:00
#coding=utf-8
class SyllabusesService
include ApplicationHelper
include CoursesHelper
def judge_can_setting(sy,user)
sy[:can_setting] = sy[:user_id] == user.id ? true : false
sy[:can_setting] = false if sy[:id].nil?
sy.courses.each do |c|
c[:can_setting] = false
member = c.members.where("user_id=#{user.id} and course_id=#{c.id}")[0]
roleName = member.roles[0].name if member
if roleName && (roleName == "TeachingAsistant" || roleName == "Teacher" )
c[:can_setting] = true
end
if c.tea_id == user.id
c[:can_setting] = true
end
end
sy
end
2016-07-04 16:19:05 +08:00
#获取指定用户的课程大纲
def user_syllabus(user)
courses = CoursesService.new.user_courses_list(user)
other = Syllabus.new(title: '未命名课程',user_id: user.id)
2016-07-04 16:19:05 +08:00
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
courses = user.courses.not_deleted
syllabus_ids = courses.empty? ? '(-1)' : "(" + courses.map{|course| !course.syllabus_id.nil? && course.syllabus_id}.join(",") + ")"
syllabuses = Syllabus.where("id in #{syllabus_ids} or user_id = #{user.id}").order("updated_at desc")
user.syllabuses = syllabuses
user.syllabuses.each do |syllabus|
syllabus.courses = syllabus.courses.not_deleted
end
syllabuses.to_a << other
#管理权限 can_setting
syllabuses.each do |s|
s = judge_can_setting(s,user)
end
2016-07-04 16:19:05 +08:00
syllabuses
2016-07-04 16:19:05 +08:00
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
2016-07-04 16:19:05 +08:00
c.save!
after_create_course(c, user)
end
end
end
sy[:can_setting] = true
2016-07-04 16:19:05 +08:00
sy
end
#修改课程大纲的名称、班级名称、新增班级
def edit(user, option)
courses = []
status = -1
syllabus_id = option[:id]
sy = Syllabus.where("id=?",option[:id]).first
if sy && sy.user_id == user.id
syllabus_title = option[:title]
sy.title = syllabus_title
2016-07-08 18:12:22 +08:00
sy.save!
#修改班级名称
modify_courses = option[:modify_courses]
modify_courses.each do |c|
course = Course.where("id=?",c.id).first
if course && course.tea_id == user.id
course.name = c.name
!course.save
end
end
#新增班级
add_courses = option[:add_courses]
add_courses.each do |c|
course = Course.new()
course.name = c
course.tea_id = user.id
course.syllabus_id = sy.id
course.update_default_value
course.is_public = 0
course.save!
after_create_course(course, user)
end
status = 0
end
status
end
2016-07-04 16:19:05 +08:00
end