添加课程搜索处理

This commit is contained in:
nwb 2014-06-12 16:55:56 +08:00
parent c8900c279c
commit b178be984d
2 changed files with 81 additions and 0 deletions

View File

@ -87,6 +87,78 @@ class CoursesController < ApplicationController
end
end
# 课程搜索
# add by nwb
def search
courses_all = Course.all_course
@courses = courses_all.visible
if params[:name].present?
@courses_all = @courses.like(params[:name])
else
@courses_all = @courses;
end
@course_count = @courses_all.count
@course_pages = Paginator.new @course_count, per_page_option, params['page']
@course_activity_count=Hash.new
@courses_all.each do |course|
@course_activity_count[course.id]=0
end
case params[:course_sort_type]
when '0'
@courses = @courses_all.order("created_at desc")
@s_type = 0
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses,@course_activity_count
when '1'
@courses = @courses_all.order("course_ac_para desc")
@s_type = 1
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses,@course_activity_count
when '2'
@courses = @courses_all.order("watchers_count desc")
@s_type = 2
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses,@course_activity_count
when '3'
@course_activity_count=get_course_activity @courses_all,@course_activity_count_array
@courses=handle_course @courses_all,@course_activity_count
@s_type = 3
@courses = @courses[@course_pages.offset, @course_pages.per_page]
else
@s_type = 0
@courses = @courses_all.order("created_at desc")
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
@course_activity_count=get_course_activity @courses,@course_activity_count
end
respond_to do |format|
format.html {
render :layout => 'base'
scope = Course
unless params[:closed]
scope = scope.active
end
}
format.atom {
courses = Course.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all
render_feed(courses, :title => "#{Setting.app_title}: #{l(:label_course_latest)}")
}
end
end
def member
## 有角色参数的才是课程,没有的就是项目
@render_file = 'member_list'

View File

@ -55,6 +55,7 @@ class Course < ActiveRecord::Base
acts_as_customizable
scope :all_course
scope :active, lambda { where(:status => STATUS_ACTIVE) }
scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
scope :all_public, lambda { where(:is_public => true) }
@ -70,6 +71,14 @@ class Course < ActiveRecord::Base
end
where(Course.allowed_to_condition(user, permission, *args))
}
scope :like, lambda {|arg|
if arg.blank?
where(nil)
else
pattern = "%#{arg.to_s.strip.downcase}%"
where("LOWER(extra) LIKE :p OR LOWER(name) LIKE :p ", :p => pattern)
end
}
def visible?(user=User.current)
user.allowed_to?(:view_course, self)