diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index 39c61a318..f119e0e69 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -14,21 +14,30 @@ module CoursesHelper # 推荐的精品课程 - def excellent_course_recommend current_course - q = "%#{current_course.name.strip}%" - recomment_courses = [] - # 推荐过程过滤掉自身 - sql = "SELECT distinct c.* FROM course_activities cs, courses c where cs.course_id = c.id - and (c.is_excellent =1 or c.excellent_option =1) and c.is_public = 1 and c.id != #{current_course.id} order by cs.updated_at desc;" - ex_courses = Course.find_by_sql(sql) - # 使用课程的tag去和课程名称进行匹配 - c_courses = ex_courses.select{|ex_course| ex_course.tags.any?{|tag| q.include?(tag.to_s)}} - if c_courses.count < 3 - results = c_courses.length == 0 ? ex_courses.first(3) : (c_courses + ex_courses.first(3 - ex_courses.count)) - else - results = c_courses.first(3) + def excellent_course_recommend new_course + q = "%#{new_course.name.strip}%" + result = find_excelletn_course(q, new_course) + end + + # 查询符合条件的精品课程 + # 用新课程名去匹配精品课程中按课程名和Tag名 + def find_excelletn_course keywords, current_course + # 获取tag匹配结果ID + a_tags = [] + Course.where("is_excellent =? and is_public =?", 1, 1).each do |ec| + if ec.tags.any?{|value| current_course.name.include?(value.to_s)} + a_tags << ec.id + end end - results + # 课程本身不能搜索显示自己 + excellent_ids = a_tags.uniq.delete_if{|i| i == current_course.id} + sql = "SELECT distinct c.id FROM course_activities cs, courses c where cs.course_id = c.id + and (c.is_excellent =1 or c.excellent_option =1) and c.is_public = 1 and c.id != #{current_course.id} order by cs.updated_at desc;" + default_ids = Course.find_by_sql(sql).flatten.map { |c| c.id } + excellent_ids << default_ids.flatten + arr_result = excellent_ids.flatten.uniq.first(3) + excellent_courses = Course.find(arr_result) + return excellent_courses end # 判断精品课程是否可见,非课程成员无法查看私有课程