From e9c9112b38493522c2598e7bfb4f57580ee58665 Mon Sep 17 00:00:00 2001 From: huang Date: Fri, 4 Mar 2016 00:41:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=85=B1=E8=B5=84=E6=BA=90=E5=B0=81?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 58 ++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0bbdacff8..1c24d3395 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2296,21 +2296,54 @@ class UsersController < ApplicationController end end + # 公共资源搜索结果 + def get_resource_search_public user_course_ids, user_project_ids + @attachments = Attachment.where("((is_publish = 1 and container_id is not null and container_type in('Project','OrgSubfield','Principal','Course')) "+ + "or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}) and is_publish = 1 and container_id is not null)" + + "or (container_type = 'Project' and container_id in (#{user_project_ids.empty? ? '0': user_project_ids.join(',')}) and is_publish = 1 and container_id is not null)) and (filename like :p)" ,:p => search).order("created_on desc") + + # @attachments = Attachment.where("((is_public =1 and is_publish = 1 and container_id is not null)" + + # "or (author_id = #{params[:id]} and is_publish = 0)) and (filename like :p) ", :p => search).order("created_on desc") + end + + # 我的资源 搜索结果 + def get_resource_search_my author_id, user_course_ids, user_project_ids + @attachments = Attachment.where("((author_id = #{author_id} and is_publish = 1 and container_id is not null and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + "or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}) and is_publish = 1 and container_id is not null)" + + "or (container_type = 'Project' and container_id in (#{user_project_ids.empty? ? '0': user_project_ids.join(',')}) and is_publish = 1 and container_id is not null)) and (filename like :p)" ,:p => search).order("created_on desc") + + # @attachments = Attachment.where("((author_id = #{params[:id]} and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ + # " or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))) and (filename like :p) ",:p=>search).order("created_on desc") + end + + # 课程搜索结果 + def get_resource_search_course author_id + @attachments = Attachment.where("(author_id = #{author_id} and container_type = 'Course') or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')})) and (filename like :p) ",:p=>search).order("created_on desc") + end + + # 项目资源搜索结果 + def get_resource_search_project author_id + @attachments = Attachment.where("author_id = #{author_id} and container_type = 'Project' and (filename like :p)",:p=>search).order("created_on desc") + end + + # 附件 + def get_resource_search_attach author_id + @attachments = Attachment.where("author_id = #{author_id} and container_type in('Project','Issue','Document','Message','News','StudentWorkScore','HomewCommon') and (filename like :p)",:p=>search).order("created_on desc") + end + + # 个人 + def get_resource_search_principal author_id + @attachments = Attachment.where("author_id = #{author_id} and container_type = 'Principal' and (filename like :p)",:p=>search).order("created_on desc") + end + # 根据资源关键字进行搜索 def resource_search search = "%#{params[:search].strip.downcase}%" + user_course_ids = User.current.courses.map { |c| c.id} + user_project_ids = User.current.projects.map {|p| p.id} + # user_org_ids = User.current.organizations.map {|o| o.id} if(params[:type].nil? || params[:type].blank? || params[:type] == "1" || params[:type] == 'all') #全部 - if User.current.id.to_i == params[:id].to_i - user_course_ids = User.current.courses.map { |c| c.id} #我的资源库的话,那么应该是我上传的所有资源 加上 我加入的课程的所有资源 取交集并查询 - @attachments = Attachment.where("((author_id = #{params[:id]} and container_type in('Project','OrgSubfield','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon')) "+ - " or (container_type = 'Course' and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')}))) and (filename like :p) ",:p=>search).order("created_on desc") - else - user_course_ids = User.find(params[:id]).courses.visible.map { |c| c.id} #如果课程私有资源,那么要看这个资源的课程是不是在 这个user的所有我可见的课程中 - @attachments = Attachment.where("((author_id = #{params[:id]} and is_public = 1 and container_type in" + - " ('Project','Principal','Course','Issue','Document','Message','News','StudentWorkScore','HomewCommon'))"+ - " or (container_type = 'Course' and is_public = 1 and container_id in (#{user_course_ids.empty? ? '0': user_course_ids.join(',')})) )" + - " and (filename like :p) ",:p=>search).order("created_on desc") - end + @attachments = get_resource_search_my(params[:id], user_course_ids, user_project_ids) elsif params[:type] == "2" #课程资源 if User.current.id.to_i == params[:id].to_i user_course_ids = User.current.courses.map { |c| c.id} @@ -2341,8 +2374,7 @@ class UsersController < ApplicationController end elsif params[:type] == "6" #全部资源 # 公共资源库:所有公开资源或者我上传的私有资源 - @attachments = Attachment.where("((is_public =1 and is_publish = 1 and container_id is not null)" + - "or (author_id = #{params[:id]} and is_publish = 0)) and (filename like :p) ", :p => search).order("created_on desc") + @attachments = get_resource_search_public(params[:id], user_course_ids, user_project_ids) end @type = params[:type] @limit = 25