2014-03-21 18:02:55 +08:00
class SchoolController < ApplicationController
2014-07-21 11:14:17 +08:00
layout 'course_base'
2014-04-25 09:29:18 +08:00
before_filter :require_admin , :only = > :upload_logo
2014-04-28 16:13:37 +08:00
def upload
uploaded_io = params [ :logo ]
2015-07-27 18:20:20 +08:00
school_id || = params [ :id ]
2015-10-19 17:04:15 +08:00
s1 = School . find ( school_id )
2014-04-28 16:13:37 +08:00
unless uploaded_io . nil?
File . open ( Rails . root . join ( 'public' , 'images' , 'school' , school_id . to_s + '.png' ) , 'wb' ) do | file |
file . write ( uploaded_io . read )
end
2015-10-19 17:04:15 +08:00
2014-04-28 16:13:37 +08:00
s1 . logo_link = '/images/school/' + school_id . to_s + '.png'
2015-10-19 17:04:15 +08:00
2015-07-27 18:20:20 +08:00
end
2015-10-19 17:04:15 +08:00
s1 . name = params [ :name ] unless params [ :name ] . blank?
s1 . province = params [ :province ] unless params [ :province ] . blank?
s1 . save
2015-07-27 18:20:20 +08:00
redirect_to admin_schools_url ( :school_name = > params [ :school_name ] )
2014-04-28 16:13:37 +08:00
end
2014-05-13 11:54:21 +08:00
2014-04-28 16:13:37 +08:00
def upload_logo
2015-07-27 18:20:20 +08:00
@school = School . find params [ :id ]
@school_name = params [ :school_name ]
2014-04-25 09:29:18 +08:00
end
2014-05-10 15:33:18 +08:00
#获取制定学校开设的课程数
def course_count school_id
School . find ( school_id ) . courses . count
end
2014-04-25 09:29:18 +08:00
2014-04-23 21:20:03 +08:00
def index
end
2014-04-30 20:26:15 +08:00
def get_province
@provinces = School . find_by_sql ( " select distinct province from schools " )
options = " "
@provinces . each do | p |
options << " <option value = ' #{ p . province } ' > #{ p . province } </option> "
end
2014-05-15 11:45:49 +08:00
# 取id取学校名
2014-05-15 13:53:06 +08:00
# 连接子表: 查询已添加用户的学校
school = School . select ( " id, name " ) .
joins ( " RIGHT JOIN (
SELECT DISTINCT school_id
FROM #{UserExtensions.table_name}
WHERE school_id IS NOT NULL ) AS sids ON schools . id = sids . school_id " ).
where ( " #{ School . table_name } .id IS NOT NULL " )
2014-05-15 11:45:49 +08:00
2014-05-09 17:08:42 +08:00
options_s = " "
2014-05-15 11:45:49 +08:00
school . each do | s |
2014-11-26 17:07:39 +08:00
options_s << " <li style = 'width: 33%; float: left'><a style='cursor: pointer;' id= #{ s . id } onclick='test(this.id, this.text)'> #{ s . name } </a></li> "
2014-05-09 17:08:42 +08:00
end
2014-05-15 11:45:49 +08:00
2014-05-09 17:08:42 +08:00
res = Hash . new
res [ :text ] = options
res [ :text_s ] = options_s
render :json = > res
2014-04-30 20:26:15 +08:00
end
2014-04-23 21:20:03 +08:00
2014-04-15 16:57:19 +08:00
def get_options
@school = School . where ( " province = ? " , params [ :province ] )
p = params [ :province ]
##@school = School.all
options = " "
@school . each do | s |
2014-04-23 09:22:13 +08:00
#options << "<option value=#{s.id}>#{s.name}</option>"
2014-11-26 17:07:39 +08:00
options << " <li style = 'width: 33%; float: left'><a style='cursor: pointer;' id= #{ s . id } onclick='test(this.id, this.text)'> #{ s . name } </a></li> "
2014-04-15 16:57:19 +08:00
end
2014-04-23 09:22:13 +08:00
2014-04-15 16:57:19 +08:00
render :text = > options
end
2014-05-12 18:46:39 +08:00
2014-04-23 21:20:03 +08:00
def get_schoollist
@school = School . where ( " province = ? " , params [ :province ] )
options = " "
@school . each do | s |
2014-11-26 17:07:39 +08:00
options << " <li style = 'width: 33%; float: left'><a style='cursor: pointer;' id= #{ s . id } onclick='test(this.id)'> #{ s . name } </a></li> "
2014-04-23 21:20:03 +08:00
end
render :text = > options
end
2014-04-24 08:58:24 +08:00
def search_school
2014-07-10 09:01:48 +08:00
q = " % #{ params [ :key_word ] . strip } % "
2014-07-10 16:21:55 +08:00
@school = School . where ( " name LIKE ? " , q )
@school = @school . where ( " province = ? " , params [ :province ] ) if ( params [ :province ] != '0' )
2014-04-24 08:58:24 +08:00
options = " "
@school . each do | s |
2014-11-26 17:07:39 +08:00
options << " <li style = 'width: 33%; float: left'> <a style='cursor: pointer;' id= #{ s . id } onclick='test(this.id)'> #{ s . name } </a></li> "
2014-04-24 08:58:24 +08:00
end
2014-07-10 16:21:55 +08:00
options = " <div class='flash error' id='flash_error'> #{ l ( :label_school_not_fount ) } </div> " if options . blank?
render :text = > options
2014-04-24 08:58:24 +08:00
end
2015-10-15 12:04:55 +08:00
#根据学校名字或者拼音来查询
def on_search
condition = " #{ params [ :name ] . strip } " . gsub ( " " , " " )
#将条件截断为汉字和拼音(全汉字 或者 全拼音 或者 汉字和拼音),
#获取拼音的第一次出现的位置
chinese = [ ]
pinyin = [ ]
condition . scan ( / . / ) . each_with_index do | char , index |
if char =~ / [a-zA-Z0-9] /
pinyin << char
2015-12-18 15:22:29 +08:00
elsif char =~ / \ ' /
2015-10-15 12:04:55 +08:00
else
chinese << char
end
end
if ( condition == '' )
2015-12-18 15:22:29 +08:00
@school = School . reorder ( 'pinyin' ) . page ( ( params [ :page ] . to_i || 1 ) - 1 ) . per ( 100 )
2015-10-20 10:11:07 +08:00
@school_count = School . count
2015-10-15 12:04:55 +08:00
else
2015-12-18 15:22:29 +08:00
@school = School . where ( " name like '% #{ chinese . join ( " " ) } %' and pinyin like '% #{ pinyin . join ( " " ) } %' " ) . reorder ( 'pinyin' ) . page ( ( params [ :page ] . to_i || 1 ) - 1 ) . per ( 100 )
2015-10-20 10:11:07 +08:00
@school_count = School . where ( " name like '% #{ chinese . join ( " " ) } %' and pinyin like '% #{ pinyin . join ( " " ) } %' " ) . count
2015-10-15 12:04:55 +08:00
end
result = [ ]
# @school.each do |sc|
# result << {:value=>sc.name,:data=>sc.id}
# end
2015-10-20 10:11:07 +08:00
render :json = > { :schools = > @school , :count = > @school_count } . to_json
2015-10-15 12:04:55 +08:00
end
2015-10-15 17:06:42 +08:00
#添加学校
def add_school
@school = School . new
@school . name = params [ :name ] . strip
@school . pinyin = Pinyin . t ( params [ :name ] . strip , splitter : '' )
@school . save
respond_to do | format |
format . js
end
end
2016-06-30 16:08:36 +08:00
#申请高校(单位) name:名称 province:省 city:市 address:地址 remarks:备注
def apply_add_school
data = { result : 0 , name :params [ :name ] , school_id : 0 }
2016-07-05 09:02:34 +08:00
#0 成功 1参数错误 2名称已存在 3.失败
2016-06-30 16:08:36 +08:00
data [ :result ] = 0
#检验参数
if params [ :name ] == " " || params [ :province ] == " " || params [ :city ] == " " || params [ :address ] == " "
data [ :result ] = 1
else
2016-07-05 09:02:34 +08:00
school = School . where ( " name = ' #{ params [ :name ] } ' " ) . first
if school
2016-06-30 16:08:36 +08:00
data [ :result ] = 2
else
school = School . new
school . name = params [ :name ] . strip
school . pinyin = Pinyin . t ( params [ :name ] . strip , splitter : '' )
2016-07-28 20:01:22 +08:00
school . province = params [ :province ]
2016-06-30 16:08:36 +08:00
#status 0未处理 1通过 2拒绝
applyschool = ApplyAddSchools . new
2016-07-05 09:02:34 +08:00
#用belongs_to 可以一起存数据库
applyschool . school = school
2016-06-30 16:08:36 +08:00
applyschool . name = school . name
applyschool . province = params [ :province ]
applyschool . city = params [ :city ]
applyschool . address = params [ :address ]
applyschool . remarks = params [ :remarks ]
2016-07-27 10:36:08 +08:00
applyschool . user_id = User . current . id
2016-07-05 09:02:34 +08:00
if applyschool . save
data [ :school_id ] = school . id
2016-07-29 16:11:26 +08:00
user_extention = User . current . extensions
user_extention . school_id = school . id
user_extention . save!
2016-07-05 09:02:34 +08:00
else
data [ :result ] = 3
end
2016-06-30 16:08:36 +08:00
end
end
render :json = > data
end
def search_repeat_schoolname
2016-07-05 09:02:34 +08:00
status = 0 #没有重复的
2016-06-30 16:08:36 +08:00
name = params [ :name ]
if name
2016-07-05 09:02:34 +08:00
school = School . where ( " name = ' #{ name } ' " ) . first
2016-06-30 16:08:36 +08:00
2016-07-05 09:02:34 +08:00
if school
status = 1 #有重复的
2016-06-30 16:08:36 +08:00
end
end
2016-07-28 14:49:45 +08:00
2016-06-30 16:08:36 +08:00
render :json = > status
end
2016-07-29 21:19:09 +08:00
def edit_apply_name
name = params [ :name ] || " "
status = - 1
if name != " "
applyschool = ApplyAddSchools . where ( " id=? " , params [ :id ] ) . first
applyschool . name = name . strip
if applyschool . school
applyschool . school . name = name
applyschool . school . pinyin = Pinyin . t ( name . strip , splitter : '' )
applyschool . school . save!
end
applyschool . save!
status = 0
end
2016-07-29 21:45:32 +08:00
render :json = > { :status = > status , :id = > params [ :id ] , :name = > name }
2016-07-29 21:19:09 +08:00
end
def edit_apply_address
address = params [ :address ] || " "
status = - 1
if address != " "
applyschool = ApplyAddSchools . where ( " id=? " , params [ :id ] ) . first
applyschool . address = address
applyschool . save!
status = 0
end
render :json = > { :status = > status , :id = > params [ :id ] , :address = > address }
end
def edit_apply_province
province = params [ :province ] || " "
city = params [ :city ] || " "
status = - 1
if ( province != " " ) && ( city != " " )
applyschool = ApplyAddSchools . where ( " id=? " , params [ :id ] ) . first
applyschool . province = province
applyschool . city = city
if applyschool . school
applyschool . school . province = province
applyschool . school . save!
end
applyschool . save!
status = 0
end
render :json = > { :status = > status , :id = > params [ :id ] , :province = > province , :city = > city }
end
2014-03-21 18:02:55 +08:00
end