From 514dcb97a0afedd84e105786374e562e1897002d Mon Sep 17 00:00:00 2001 From: z9hang Date: Fri, 28 Nov 2014 11:33:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E8=B5=84=E6=BA=90=E6=8C=89?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E3=80=81=E4=B8=8B=E8=BD=BD=E6=AC=A1=E6=95=B0?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/files_controller.rb | 34 ++++++++++++++++++++------- app/helpers/files_helper.rb | 6 ++--- app/views/files/_course_list.html.erb | 23 +++++++++--------- app/views/files/search.js.erb | 2 +- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/app/controllers/files_controller.rb b/app/controllers/files_controller.rb index ac228e0d4..c20b5a0de 100644 --- a/app/controllers/files_controller.rb +++ b/app/controllers/files_controller.rb @@ -45,16 +45,28 @@ class FilesController < ApplicationController end def search + sort = "" + @sort = "" + @order = "" + @is_remote = true + if params[:sort] + order_by = params[:sort].split(":") + @sort = order_by[0] + if order_by.count > 1 + @order = order_by[1] + end + sort = "#{@sort} #{@order}" + end + begin - @is_remote = true q = "%#{params[:name].strip}%" #(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank? if params[:insite] - @result = find_public_attache q + @result = find_public_attache q,sort @result = visable_attachemnts_insite @result,@course @searched_attach = paginateHelper @result,10 else - @result = find_course_attache q,@course + @result = find_course_attache q,@course,sort @result = visable_attachemnts @result @searched_attach = paginateHelper @result,10 end @@ -65,18 +77,24 @@ class FilesController < ApplicationController end end - def find_course_attache keywords,course - resultSet = Attachment.where("(attachments.container_type = 'Course'And attachments.container_id = '#{course.id}') OR (attachments.container_type = 'HomeworkAttach' AND ) AND filename LIKE :like ", like: "%#{keywords}%"). - reorder("created_on DESC") + def find_course_attache keywords,course,sort = "" + if sort == "" + sort = "created_on DESC" + end + resultSet = Attachment.where("attachments.container_type = 'Course' And attachments.container_id = '#{course.id}' AND filename LIKE :like ", like: "%#{keywords}%"). + reorder(sort) #resultSet = Attachment.find_by_sql("SELECT `attachments`.* FROM `attachments` LEFT OUTER JOIN `homework_attaches` ON `attachments`.container_type = 'HomeworkAttach' AND `attachments`.container_id = `homework_attaches`.id LEFT OUTER JOIN `homework_for_courses` ON `homework_attaches`.bid_id = `homework_for_courses`.bid_id LEFT OUTER JOIN `homework_for_courses` AS H_C ON `attachments`.container_type = 'Bid' AND `attachments`.container_id = H_C.bid_id WHERE (`homework_for_courses`.course_id = 117 OR H_C.course_id = 117 OR (`attachments`.container_type = 'Course' AND `attachments`.container_id = 117)) AND `attachments`.filename LIKE '%#{keywords}%'").reorder("created_on DESC") end - def find_public_attache keywords + def find_public_attache keywords,sort = "" # StoresController#search 将每条文件都查出来,再次进行判断过滤。---> resultSet.to_a.map # 此时内容不多速度还可,但文件增长,每条判断多则进行3-4次表连接。 # 现在还木有思路 药丸 + if sort == "" + sort = "created_on DESC" + end resultSet = Attachment.where("attachments.container_type IS NOT NULL AND filename LIKE :like ", like: "%#{keywords}%"). - reorder("created_on DESC") + reorder(sort) end def index diff --git a/app/helpers/files_helper.rb b/app/helpers/files_helper.rb index 253ce34a5..c9a232c36 100644 --- a/app/helpers/files_helper.rb +++ b/app/helpers/files_helper.rb @@ -54,11 +54,11 @@ module FilesHelper s.html_safe end - #判断用户是否拥有课程,需用户在该课程中角色为教师且该课程属于当前学期或下一学期 - def has_course? user + #判断用户是否拥有除current_course以外的课程,需用户在该课程中角色为教师且该课程属于当前学期或下一学期 + def has_course? user,current_course result = false user.courses.each do |course| - if is_course_teacher(User.current,course) && course_in_current_or_next_term(course) + if current_course.id != course.id && is_course_teacher(User.current,course) && course_in_current_or_next_term(course) return true end end diff --git a/app/views/files/_course_list.html.erb b/app/views/files/_course_list.html.erb index 93e39f73f..02dc816b2 100644 --- a/app/views/files/_course_list.html.erb +++ b/app/views/files/_course_list.html.erb @@ -2,16 +2,15 @@

共有 <%= all_attachments.count%> 个资源

- - 资源列表的多样化排序将在下周上线... + <% if order == "asc" %> + 按 <%= link_to "时间",params.merge(:sort=>"created_on:desc"),:class => "f_b c_grey",:remote => @is_remote %><%= render partial: 'arrow_show',locals: { sort: sort,order:order,current:"created_on"} %> /  + <%= link_to "下载次数",params.merge(:sort=>"downloads:desc"),:class => "f_b c_grey",:remote => @is_remote %><%= render partial: 'arrow_show',locals: { sort: sort,order:order,current:"downloads"} %> 排序 + <% else %> + 按 <%= link_to "时间",params.merge(:sort=>"created_on:asc"),:class => "f_b c_grey" ,:remote => @is_remote %><%= render partial: 'arrow_show',locals: { sort: sort,order:order,current:"created_on"} %> /  + <%= link_to "下载次数",params.merge(:sort=>"downloads:asc"),:class => "f_b c_grey",:remote => @is_remote %><%= render partial: 'arrow_show',locals: { sort: sort,order:order,current:"downloads"} %> 排序 + <% end %>

@@ -21,7 +20,7 @@ <%= link_to_attachment file, :download => true,:text => truncate(file.filename,length: 35, omission: '...'), :title => file.filename+"\n"+file.description.to_s, :style => "overflow: hidden; white-space: nowrap;text-overflow: ellipsis;",:class => "c_dblue f_14 f_b f_l" %> <% if User.current.logged? %> <% if is_course_teacher(User.current,@course) && file.author_id == User.current.id %> - <%= link_to("选入我的其他课程",quote_resource_show_course_file_path(@course,file),:class => "f_l re_select",:remote => true) if has_course?(User.current) %> + <%= link_to("选入我的其他课程",quote_resource_show_course_file_path(@course,file),:class => "f_l re_select",:remote => true) if has_course?(User.current,@course) %> <% if delete_allowed && file.container_id == @course.id && file.container_type == "Course" %> @@ -31,7 +30,7 @@ <% end %> <% else %> - <%= link_to("选入我的课程",quote_resource_show_course_file_path(@course,file),:class => "f_l re_select",:remote => true) if has_course?(User.current) %> + <%= link_to("选入我的课程",quote_resource_show_course_file_path(@course,file),:class => "f_l re_select",:remote => true) if has_course?(User.current,@course) %> <% end %> <% else %> <% end %> diff --git a/app/views/files/search.js.erb b/app/views/files/search.js.erb index 84c9a4707..50f46d2f5 100644 --- a/app/views/files/search.js.erb +++ b/app/views/files/search.js.erb @@ -1 +1 @@ -$("#course_list").html("<%= escape_javascript(render :partial => 'course_list',:locals => {course: @course,all_attachments: @result,sort:"create_on",order:"",curse_attachments:@searched_attach})%>"); \ No newline at end of file +$("#course_list").html("<%= escape_javascript(render :partial => 'course_list',:locals => {course: @course,all_attachments: @result,sort:@sort,order:@order,curse_attachments:@searched_attach})%>"); \ No newline at end of file