socialforge/app/services/syllabuses_service.rb

151 lines
4.3 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)
2016-07-13 08:44:20 +08:00
# courses = CoursesService.new.user_courses_list(user)
#
# other = Syllabus.new(title: '未命名课程',user_id: user.id)
#
2016-07-13 08:44:20 +08:00
# courses.each do |c|
# other.courses << c[:course] unless c[:course].syllabus
# end
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")
2016-07-08 18:32:30 +08:00
syllabuses.each do |syllabus|
2016-07-08 18:52:21 +08:00
syllabus.courses = courses.where("syllabus_id = #{syllabus.id}").select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS updatetime").order("time desc")
end
2016-07-13 08:44:20 +08:00
# syllabuses.to_a << other
2016-07-13 08:44:20 +08:00
syllabuses.to_a
#管理权限 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
2016-07-12 17:00:01 +08:00
def send_wechat_create_class_notice user,course
count = ShieldWechatMessage.where("container_type='User' and container_id=#{user.id} and shield_type='Course' and shield_id=#{course.id}").count
if count == 0
ws = WechatService.new
title = "恭喜您创建班级成功。"
ws.create_class_notice user.id, "create_course_notice", course.id,title, course.name, user.show_name, 0, "点击查看班级详情。"
2016-07-12 17:00:01 +08:00
end
end
2016-07-04 16:19:05 +08:00
#创建大纲
# 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!
2016-07-12 17:00:01 +08:00
send_wechat_create_class_notice user,course
2016-07-04 16:19:05 +08:00
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)
2016-07-12 17:00:01 +08:00
send_wechat_create_class_notice user,c
2016-07-04 16:19:05 +08:00
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)
2016-07-12 17:00:01 +08:00
send_wechat_create_class_notice user,course
end
status = 0
end
status
end
2016-07-04 16:19:05 +08:00
end