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
2014-03-21 18:02:55 +08:00
end