diff --git a/app/api/mobile/apis/new_comment.rb b/app/api/mobile/apis/new_comment.rb index e72daae6d..3c57636b5 100644 --- a/app/api/mobile/apis/new_comment.rb +++ b/app/api/mobile/apis/new_comment.rb @@ -112,7 +112,7 @@ module Mobile # end if reply_type == nil || reply_type == 0 homework_common = HomeworkCommon.find(params[:id]) - feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id]) + feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id], params[:id]) if (feedback.errors.empty?) homework_common.update_column(:updated_at, Time.now) result = 2 @@ -124,7 +124,7 @@ module Mobile homework_common = HomeworkCommon.find reply.jour_id options = {:notes => params[:content], :reply_id => reply.user_id,:user_id => current_user.id,:m_parent_id => params[:id].to_i,:m_reply_id => params[:id].to_i} - feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], reply.jour_id, options) + feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], reply.jour_id, reply.jour_id, options) if (feedback.errors.empty?) homework_common.update_column(:updated_at, Time.now) result = 2 @@ -189,6 +189,7 @@ module Mobile reply.content = params[:content] reply.parent_id = params[:id] reply.subject = "RE: #{topic.subject}" + reply.root_id = topic.id if message.children << reply result = 2 update_id = topic.id @@ -224,6 +225,7 @@ module Mobile :m_parent_id => parent_id, :m_reply_id => reply_id, :reply_id => reply_user_id, + :root_id => jour.id, :notes => content, :is_readed => false} jfm = jour.user.add_jour(nil, nil, nil, options) @@ -278,6 +280,7 @@ module Mobile blogComment.blog = blog.blog blogComment.content = params[:content] blogComment.title = "RE: #{blog.title}" + blogComment.root_id = blog.id if blog.children << blogComment result = 2 update_id = blog.id @@ -293,6 +296,7 @@ module Mobile parent = BlogComment.find params[:id] blogComment.parent_id = params[:id] #被回复的回复 blogComment.reply_id = parent.author.id #被回复者id + blogComment.root_id = blog.id if parent.children << blogComment result = 2 diff --git a/app/api/mobile/entities/activity.rb b/app/api/mobile/entities/activity.rb index b2581c319..3cd4ce6c0 100644 --- a/app/api/mobile/entities/activity.rb +++ b/app/api/mobile/entities/activity.rb @@ -25,8 +25,10 @@ module Mobile elsif ac.act_type == "News" ac.nil? || ac.act.nil? ? 0 : ac.act.comments.count elsif ac.act_type == "Message" || ac.act_type == "BlogComment" || ac.act_type == "JournalsForMessage" - all_comments = [] - ac.nil? || ac.act.nil? ? 0 : get_all_children(all_comments, ac.act).count + className = ac.act_type.classify.constantize + # all_comments = [] + # ac.nil? || ac.act.nil? ? 0 : get_all_children(all_comments, ac.act).count + ac.nil? || ac.act.nil? ? 0 : className.where("root_id = #{ac.act.id}").count elsif ac.act_type == "Issue" ac.nil? || ac.act.nil? ? 0 : ac.act.journals.where("notes is not null and notes != ''").count end diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 1508fc871..4cee2a47c 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -153,72 +153,10 @@ class WelcomeController < ApplicationController def search @name = params[:q] @search_type = params[:search_type] - case params[:search_type] - when 'all' - @alls = Elasticsearch::Model.search({ - query: { - multi_match: { - query: @name, - type:"most_fields", - operator: "or", - fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5'] - } - }, - highlight: { - pre_tags: [''], - post_tags: [''], - fields: { - login: {}, - firstname: {}, - lastname: {}, - name:{}, - description:{}, - filename:{}, - subject:{}, - content:{} - } - } - },[User,Course,Attachment,Project,Memo] ).page(params[:page] || 1).per(20).results - when 'user' - @users = User.search(@name).page(params[:page] || 1).per(20) - when 'project' - @projects = Project.search(@name).page(params[:page] || 1).per(20).results - when 'course' - @courses = Course.search(@name).page(params[:page] || 1).per(20).results - when 'attachment' - @attachments = Attachment.search(@name).page(params[:page] || 1).per(20).results - when 'memo' - @memos = Memo.search(@name).page(params[:page] || 1).per(20).results - else - @alls = Elasticsearch::Model.search({ - query: { - multi_match: { - query: @name, - type:"most_fields", - operator: "or", - fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5'] - } - }, - highlight: { - pre_tags: [''], - post_tags: [''], - fields: { - login: {}, - firstname: {}, - lastname: {}, - name:{}, - description:{}, - filename:{}, - subject:{}, - content:{} - } - } - },[User,Course,Attachment,Project,Memo] ).page(params[:page] || 1).per(20).results - - end + page = (params[:page] || 1).to_i @users_count = User.search(@name).results.total - + @syllabus_count = Syllabus.search(@name).results.total @course_count = Course.search(@name).results.total @attach_count = Attachment.search(@name).results.total @project_count = Project.search(@name).results.total @@ -229,7 +167,7 @@ class WelcomeController < ApplicationController query: @name, type:"most_fields", operator: "or", - fields: ['login', 'firstname','lastname','name','description^0.5','filename','subject','content^0.5'] + fields: ['login', 'firstname','lastname','title','name','description^0.5','filename','subject','content^0.5'] } }, highlight: { @@ -239,6 +177,7 @@ class WelcomeController < ApplicationController login: {}, firstname: {}, lastname: {}, + title:{}, name:{}, description:{}, filename:{}, @@ -246,7 +185,110 @@ class WelcomeController < ApplicationController content:{} } } - },[User,Course,Attachment,Project,Memo] ).results.total + },[User,Syllabus,Course,Attachment,Project,Memo] ).results.total + + + case params[:search_type] + when 'all' + if (page-1)*20 >= @total_count + page = 1 + params[:page] = "1" + end + @alls = Elasticsearch::Model.search({ + query: { + multi_match: { + query: @name, + type:"most_fields", + operator: "or", + fields: ['login', 'firstname','lastname','title','name','description^0.5','filename','subject','content^0.5'] + } + }, + highlight: { + pre_tags: [''], + post_tags: [''], + fields: { + login: {}, + firstname: {}, + lastname: {}, + title:{}, + name:{}, + description:{}, + filename:{}, + subject:{}, + content:{} + } + } + },[User,Syllabus,Course,Attachment,Project,Memo] ).page(page).per(20).results + when 'user' + if (page-1)*20 >= @users_count + page = 1 + params[:page] = "1" + end + @users = User.search(@name).page(page).per(20) + when 'syllabus' + if (page-1)*20 >= @syllabus_count + page = 1 + params[:page] = "1" + end + @syllabuses = Syllabus.search(@name).page(page).per(20).results + when 'project' + if (page-1)*20 >= @project_count + page = 1 + end + @projects = Project.search(@name).page(page).per(20).results + when 'course' + if (page-1)*20 >= @course_count + page = 1 + params[:page] = "1" + end + @courses = Course.search(@name).page(page).per(20).results + when 'attachment' + if (page-1)*20 >= @attach_count + page = 1 + params[:page] = "1" + end + @attachments = Attachment.search(@name).page(page).per(20).results + when 'memo' + if (page-1)*20 >= @memo_count + page = 1 + params[:page] = "1" + end + @memos = Memo.search(@name).page(page).per(20).results + else + if (page-1)*20 >= @total_count + page = 1 + params[:page] = "1" + end + @alls = Elasticsearch::Model.search({ + query: { + multi_match: { + query: @name, + type:"most_fields", + operator: "or", + fields: ['login', 'firstname','lastname','title','name','description^0.5','filename','subject','content^0.5'] + } + }, + highlight: { + pre_tags: [''], + post_tags: [''], + fields: { + login: {}, + firstname: {}, + lastname: {}, + title:{}, + name:{}, + description:{}, + filename:{}, + subject:{}, + content:{} + } + } + },[User,Syllabus,Course,Attachment,Project,Memo] ).page(page).per(20).results + + end + + + # search_type = params[:search_type].to_sym unless search_condition.blank? # search_by = params[:search_by] # diff --git a/app/models/syllabus.rb b/app/models/syllabus.rb index 93043b765..ea5d44dfd 100644 --- a/app/models/syllabus.rb +++ b/app/models/syllabus.rb @@ -2,6 +2,18 @@ class Syllabus < ActiveRecord::Base include Redmine::SafeAttributes include ApplicationHelper + #elasticsearch + include Elasticsearch::Model + #elasticsearch kaminari init + Kaminari::Hooks.init + Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari + settings index: { number_of_shards: 5 } do + mappings dynamic: 'false' do + indexes :title, analyzer: 'smartcn',index_options: 'offsets' + indexes :description, analyzer: 'smartcn',index_options: 'offsets' + indexes :updated_at, index:"not_analyzed", type:'date' + end + end acts_as_taggable acts_as_attachable has_many_kindeditor_assets :assets, :dependent => :destroy @@ -17,6 +29,12 @@ class Syllabus < ActiveRecord::Base validates :title, :user_id, presence: true + after_update :update_syllabus_ealasticsearch_index + after_create :create_syllabus_ealasticsearch_index + before_destroy :delete_syllabus_ealasticsearch_index + + scope :indexable,lambda { where(nil) }#用于elastic建索引的scope + scope :like, lambda {|arg| if arg.blank? where(nil) @@ -57,4 +75,55 @@ class Syllabus < ActiveRecord::Base jfm.save jfm end + + def create_syllabus_ealasticsearch_index + return if Rails.env.development? + self.__elasticsearch__.index_document + end + + def update_syllabus_ealasticsearch_index + return if Rails.env.development? + begin + self.__elasticsearch__.update_document + rescue => e + self.__elasticsearch__.index_document + end + end + + def delete_syllabus_ealasticsearch_index + return if Rails.env.development? + begin + self.__elasticsearch__.delete_document + rescue => e + + end + end + + def self.search(query) + __elasticsearch__.search( + { + query: { + multi_match: { + query: query, + type:"most_fields", + operator: "or", + fields: ['title'] + } + }, + sort: { + _score:{order: "desc" }, + updated_at:{order:"desc"} + + }, + highlight: { + pre_tags: [''], + post_tags: [''], + fields: { + title: {}, + description: {} + } + } + } + ) + end end diff --git a/app/models/user.rb b/app/models/user.rb index 3386836ed..ed17010c6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -431,6 +431,12 @@ class User < Principal name = lastname + firstname name.empty? || name.nil? ? login : name end + + def show_occupation + if self.user_extensions && self.user_extensions.school + self.user_extensions.school.name + end + end ## end def get_at_show_name diff --git a/app/views/layouts/_logined_header.html.erb b/app/views/layouts/_logined_header.html.erb index b8235ff1c..cdc752e7e 100644 --- a/app/views/layouts/_logined_header.html.erb +++ b/app/views/layouts/_logined_header.html.erb @@ -51,7 +51,7 @@ <% name = name%> <%= form_tag({controller: :welcome, action: :search },:class=>'navHomepageSearchBox', method: :get) do %> - " id="navHomepageSearchInput" class="navHomepageSearchInput" placeholder="请输入关键词搜索公开的班级、项目、用户、资源以及帖子"/> + " id="navHomepageSearchInput" class="navHomepageSearchInput" placeholder="请输入关键词搜索公开的课程、班级、项目、用户、资源以及帖子"/> diff --git a/app/views/welcome/_search_all_results.html.erb b/app/views/welcome/_search_all_results.html.erb index 3316cbc8a..4d3cc393d 100644 --- a/app/views/welcome/_search_all_results.html.erb +++ b/app/views/welcome/_search_all_results.html.erb @@ -16,15 +16,31 @@
<%= image_tag("search_icon_03.png", :width=>"8", :height=>"16" ,:class=>"fl") %><%= get_user_identity(User.find(item.id).user_extensions.identity) %>
-
  • <%= User.find(item.id).user_extensions && User.find(item.id).user_extensions.brief_introduction.present? ? User.find(item.id).user_extensions.brief_introduction : '这位童鞋很懒,什么也没有留下~'%>
  • + <% user = User.find(item.id)%> +
  • <%= user.user_extensions && user.user_extensions.brief_introduction.present? ? user.user_extensions.brief_introduction : '这位童鞋很懒,什么也没有留下~'%>
  • - 加入时间:<%= format_date( User.find(item.id).created_on)%> - 最后登录时间:<%= format_date( User.find(item.id).last_login_on)%> - <%= User.find(item.id).user_extensions.occupation.present? ? '单位:'+User.find(item.id).user_extensions.occupation : ''%>
  • + 加入时间:<%= format_date( user.created_on)%> + 最后登录时间:<%= format_date( user.last_login_on)%> + <%=user.show_occupation.nil? ? "": "单位:"+user.show_occupation %>
    + <% when 'syllabus'%> + <% when 'course'%>
    @@ -56,7 +73,8 @@
    <%= image_tag("search_icon_03.png", :width=>"8", :height=>"16" ,:class=>"fl") %>资源
    -
  • 发布者:<%= User.find(item.author_id).login%>(<%= User.find(item.author_id).realname%>) + <% user = User.find(item.author_id)%> +
  • 发布者:<%= user.login%>(<%= user.realname%>) 发布时间:<%= format_date(item.created_on)%> 下载次数:<%= item.downloads%>次 diff --git a/app/views/welcome/_search_attachment_results.html.erb b/app/views/welcome/_search_attachment_results.html.erb index e51706085..c670526f1 100644 --- a/app/views/welcome/_search_attachment_results.html.erb +++ b/app/views/welcome/_search_attachment_results.html.erb @@ -10,7 +10,8 @@
    <%= image_tag("search_icon_03.png", :width=>"8", :height=>"16" ,:class=>"fl") %>资源
  • -
  • 发布者:<%= User.find(attachment.author_id).login%>(<%= User.find(attachment.author_id).realname%>) + <% user = User.find(attachment.author_id)%> +
  • 发布者:<%= user.login%>(<%= user.realname%>) 发布时间:<%= format_date(attachment.created_on)%> 下载次数:<%= attachment.downloads%>次 @@ -21,6 +22,6 @@ <% end %>
    - {:controller => 'welcome', :action => 'search',:search_type=>'attachment'}%> + <%= paginate attachments,:params => {:controller => 'welcome', :action => 'search',:search_type=>'attachment'}%>
    <% end %> \ No newline at end of file diff --git a/app/views/welcome/_search_course_results.html.erb b/app/views/welcome/_search_course_results.html.erb index 322a47de5..dd4c9f405 100644 --- a/app/views/welcome/_search_course_results.html.erb +++ b/app/views/welcome/_search_course_results.html.erb @@ -13,10 +13,11 @@
  • <%= course.try(:highlight).try(:description) ? course.highlight.description[0].html_safe : (course.description.present? ? course.description : '暂时没有该班级描述') %>
  • - 教师:<%= User.find(course.tea_id).realname %> + <% user = User.find(course.tea_id)%> + 教师:<%= user.realname %> 授课时间:<%= course.time.to_s + course.term%> 更新时间:<%= format_date(course.updated_at)%> - <%= User.find(course.tea_id).user_extensions.occupation.present? ? '单位:'+User.find(course.tea_id).user_extensions.occupation : ''%>
  • + <%=user.show_occupation.nil? ? "": "单位:"+user.show_occupation %>
    diff --git a/app/views/welcome/_search_syllabus_results.html.erb b/app/views/welcome/_search_syllabus_results.html.erb new file mode 100644 index 000000000..163392341 --- /dev/null +++ b/app/views/welcome/_search_syllabus_results.html.erb @@ -0,0 +1,22 @@ +<% unless syllabuses.nil? || syllabuses.empty?%> + <% syllabuses.each do |item|%> + + <% end %> +
    + <%= paginate syllabuses,:params => {:controller => 'welcome', :action => 'search',:search_type=>'syllabus'}%> +
    +<% end %> \ No newline at end of file diff --git a/app/views/welcome/_search_user_results.html.erb b/app/views/welcome/_search_user_results.html.erb index 06a40d021..3b9e18fba 100644 --- a/app/views/welcome/_search_user_results.html.erb +++ b/app/views/welcome/_search_user_results.html.erb @@ -1,20 +1,21 @@ <% unless users.nil? || users.empty?%> -<% users.each do |user|%> +<% users.each do |item|%>