From 919b25bb06b17298f0349c82872687744b6763af Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Sep 2019 15:21:31 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=A2=98=E5=BA=93=E7=9A=84=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../graduation_topics_controller.rb | 2 ++ app/controllers/shixuns_controller.rb | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/app/controllers/graduation_topics_controller.rb b/app/controllers/graduation_topics_controller.rb index bd93401f3..0242bff21 100644 --- a/app/controllers/graduation_topics_controller.rb +++ b/app/controllers/graduation_topics_controller.rb @@ -230,6 +230,7 @@ class GraduationTopicsController < ApplicationController topic_repeat: topic.topic_repeat, province: topic.province, city: topic.city, + topic_type: topic.topic_type, course_list_id: @course.course_list_id) topic_bank.attachments.destroy_all else @@ -241,6 +242,7 @@ class GraduationTopicsController < ApplicationController topic_repeat: topic.topic_repeat, province: topic.province, city: topic.city, + topic_type: topic.topic_type, course_list_id: @course.course_list_id, user_id: current_user.id, graduation_topic_id: topic.id) diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 74d1d05e2..4555bd1cf 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -98,6 +98,39 @@ class ShixunsController < ApplicationController .each_with_object({}) { |r, obj| obj[r.shixun_id] = r.name } end + def shixun_list + type = params[:type] || "all" + + # 超级管理员用户显示所有未隐藏的实训、非管理员显示所有已发布的实训(对本单位公开且未隐藏未关闭) + if current_user.admin? + @shixuns = Shixun.unhidden + else + none_shixun_ids = ShixunSchool.where("school_id != #{current_user.school_id}").pluck(:shixun_id) + + @shixuns = Shixun.where.not(id: none_shixun_ids).unhidden + end + + ## 搜索关键字创建者、实训名称、院校名称 + unless params[:search].blank? + keyword = params[:search].strip + @shixuns = @shixuns.joins(user: [user_extenison: :school]). + where("schools.name like '%#{keyword}%' + or concat(lastname, firstname) like '%#{keyword}%' + or shixuns.name like '%#{keyword.split(" ").join("%")}%'").distinct + end + + ## 筛选 难度 + if params[:diff].present? && params[:diff].to_i != 0 + @shixuns = @shixuns.where(trainee: params[:diff]) + end + + @total_count = @shixuns.count + page = params[:page] || 1 + limit = params[:limit] || 15 + + @shixuns = @shixuns.includes(:challenges, user: [user_extension: :school]).page(page).per(limit) + end + ## 获取顶部菜单 def menus @repertoires = Repertoire.includes(sub_repertoires: [:tag_repertoires]).order("updated_at asc") From 86d12711de06ff5b4040060f17513a1ab87269f7 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Sep 2019 16:56:51 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=80=89=E7=94=A8=E5=AE=9E=E8=AE=AD?= =?UTF-8?q?=E7=9A=84=E5=85=AC=E7=94=A8=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/shixuns_controller.rb | 27 +++++++++++++++------ app/models/shixun.rb | 1 + app/views/shixuns/shixun_list.json.jbuilder | 15 ++++++++++++ config/routes.rb | 1 + 4 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 app/views/shixuns/shixun_list.json.jbuilder diff --git a/app/controllers/shixuns_controller.rb b/app/controllers/shixuns_controller.rb index 4555bd1cf..e910db8cc 100644 --- a/app/controllers/shixuns_controller.rb +++ b/app/controllers/shixuns_controller.rb @@ -8,11 +8,11 @@ class ShixunsController < ApplicationController before_action :find_shixun, except: [:index, :new, :create, :menus, :get_recommend_shixuns, :propaedeutics, :departments, :apply_shixun_mirror, - :get_mirror_script, :download_file] + :get_mirror_script, :download_file, :shixun_list] before_action :shixun_access_allowed, except: [:index, :new, :create, :menus, :get_recommend_shixuns, :propaedeutics, :departments, :apply_shixun_mirror, - :get_mirror_script, :download_file] + :get_mirror_script, :download_file, :shixun_list] before_action :find_repo_name, only: [:repository, :commits, :file_content, :update_file, :shixun_exec, :copy, :add_file] before_action :allowed, only: [:update, :close, :update_propaedeutics, :settings, :publish, @@ -99,21 +99,32 @@ class ShixunsController < ApplicationController end def shixun_list + # 全部实训/我的实训 type = params[:type] || "all" + # 状态:已发布/未发布 + status = params[:status] || "all" # 超级管理员用户显示所有未隐藏的实训、非管理员显示所有已发布的实训(对本单位公开且未隐藏未关闭) - if current_user.admin? - @shixuns = Shixun.unhidden + if type == "mine" + @shixuns = current_user.shixuns.none_closed else - none_shixun_ids = ShixunSchool.where("school_id != #{current_user.school_id}").pluck(:shixun_id) + if current_user.admin? + @shixuns = Shixun.none_closed.where(hidden: 0) + else + none_shixun_ids = ShixunSchool.where("school_id != #{current_user.school_id}").pluck(:shixun_id) + + @shixuns = Shixun.where.not(id: none_shixun_ids).none_closed.where(hidden: 0) + end + end - @shixuns = Shixun.where.not(id: none_shixun_ids).unhidden + unless status == "all" + @shixuns = status == "published" ? @shixuns.where(status: 2) : @shixuns.where(status: [0, 1]) end ## 搜索关键字创建者、实训名称、院校名称 unless params[:search].blank? keyword = params[:search].strip - @shixuns = @shixuns.joins(user: [user_extenison: :school]). + @shixuns = @shixuns.joins(user: [user_extension: :school]). where("schools.name like '%#{keyword}%' or concat(lastname, firstname) like '%#{keyword}%' or shixuns.name like '%#{keyword.split(" ").join("%")}%'").distinct @@ -128,7 +139,7 @@ class ShixunsController < ApplicationController page = params[:page] || 1 limit = params[:limit] || 15 - @shixuns = @shixuns.includes(:challenges, user: [user_extension: :school]).page(page).per(limit) + @shixuns = @shixuns.order("myshixuns_count desc").page(page).per(limit).includes(:shixun_info, :subjects, user: [user_extension: :school]) end ## 获取顶部菜单 diff --git a/app/models/shixun.rb b/app/models/shixun.rb index db6bca43e..33d36d026 100644 --- a/app/models/shixun.rb +++ b/app/models/shixun.rb @@ -59,6 +59,7 @@ class Shixun < ApplicationRecord scope :visible, -> { where.not(status: -1) } scope :published, lambda{ where(status: 2) } scope :published_closed, lambda{ where(status: [2, 3]) } + scope :none_closed, lambda{ where(status: [0, 1, 2]) } scope :unhidden, lambda{ where(hidden: 0, status: 2) } scope :field_for_recommend, lambda{ select([:id, :name, :identifier, :myshixuns_count]) } scope :find_by_ids,lambda{|k| where(id:k)} diff --git a/app/views/shixuns/shixun_list.json.jbuilder b/app/views/shixuns/shixun_list.json.jbuilder new file mode 100644 index 000000000..13c556014 --- /dev/null +++ b/app/views/shixuns/shixun_list.json.jbuilder @@ -0,0 +1,15 @@ +json.shixun_list @shixuns do |shixun| + json.shixun_id shixun.id + json.identifier shixun.identifier + json.shixun_name shixun.name + json.description shixun.description + json.myshixuns_count shixun.myshixuns_count + json.school shixun.user&.school_name + json.creator shixun.user&.full_name + json.level level_to_s(shixun.trainee) + json.subjects shixun.subjects do |subject| + json.(subject, :id, :name) + end +end + +json.shixuns_count @total_count \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 8085765b4..4ddd6dc80 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -179,6 +179,7 @@ Rails.application.routes.draw do get :get_mirror_script post :apply_shixun_mirror get :download_file + get :shixun_list end member do