2016-07-04 16:19:05 +08:00
#coding=utf-8
class SyllabusesService
include ApplicationHelper
include CoursesHelper
2016-07-08 16:23:07 +08:00
def judge_can_setting ( sy , user )
sy [ :can_setting ] = sy [ :user_id ] == user . id ? true : false
2016-07-08 16:40:24 +08:00
sy [ :can_setting ] = false if sy [ :id ] . nil?
2016-07-08 16:23:07 +08:00
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-08 16:23:07 +08:00
#
2016-07-13 08:44:20 +08:00
# courses.each do |c|
# other.courses << c[:course] unless c[:course].syllabus
# end
2016-08-13 18:35:11 +08:00
result = [ ]
i = 0
2016-07-08 16:23:07 +08:00
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 17:22:58 +08:00
2016-07-08 18:32:30 +08:00
syllabuses . each do | syllabus |
2016-08-13 18:35:11 +08:00
tmp_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 ( " updatetime desc " )
result [ i ] = { :title = > syllabus . title , :id = > syllabus . id , :user_id = > syllabus . user_id , :courses = > tmp_courses }
i = i + 1
2016-07-08 17:22:58 +08:00
end
2016-08-13 18:35:11 +08:00
# syllabuses.to_a
# syllabuses
result
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
2016-07-21 14:03:04 +08:00
title = " 恭喜您创建班级成功。 "
2016-08-10 16:08:30 +08:00
ws . create_class_notice user . id , " create_course_notice " , course . id , title , course . name , user . show_name , 1 , " 点击查看班级详情。 "
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
2016-07-06 16:23:12 +08:00
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
2016-07-08 16:23:07 +08:00
sy [ :can_setting ] = true
2016-07-04 16:19:05 +08:00
sy
end
2016-07-08 16:23:07 +08:00
#修改课程大纲的名称、班级名称、新增班级
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!
2016-07-08 16:23:07 +08:00
#修改班级名称
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 |
2016-07-08 17:22:58 +08:00
course = Course . new ( )
course . name = c
2016-07-08 16:23:07 +08:00
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
2016-07-08 16:23:07 +08:00
end
status = 0
end
status
end
2016-07-04 16:19:05 +08:00
2016-07-08 17:04:17 +08:00
end