socialforge/app/services/courses_service.rb

29 lines
1.3 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class CoursesService
include ApplicationHelper
#参数school_id为0或不传时返回所有课程否则返回对应学校的课程
#参数per_page_count分页功能每页显示的课程数
#参数page分页功能当前页码
def course_list params
@school_id = params[:school_id]
per_page_option = params[:per_page_count] || 10
page_no = params[:page] || 1
if @school_id == "0" || @school_id.nil?
@courses_all = Course.active.visible.
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id")
else
@courses_all = Course.active.visible.
joins("LEFT JOIN #{CourseStatus.table_name} ON #{Course.table_name}.id = #{CourseStatus.table_name}.course_id").
where("#{Course.table_name}.school_id = ?", @school_id)
end
@course_count = @courses_all.count
@course_pages = Redmine::Pagination::Paginator.new @course_count, per_page_option,page_no
@courses = @courses_all.order("created_at desc")
@courses = @courses.offset(@course_pages.offset).limit(@course_pages.per_page)
course_list = []
@courses.each do |course|
course_list << {:course => course,:img_url => url_to_avatar(course)}
end
course_list
end
end