选用实训的公用接口

topic_bank
cxt 5 years ago
parent ae29f70a14
commit 86d12711de

@ -8,11 +8,11 @@ class ShixunsController < ApplicationController
before_action :find_shixun, except: [:index, :new, :create, :menus, :get_recommend_shixuns, before_action :find_shixun, except: [:index, :new, :create, :menus, :get_recommend_shixuns,
:propaedeutics, :departments, :apply_shixun_mirror, :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, before_action :shixun_access_allowed, except: [:index, :new, :create, :menus, :get_recommend_shixuns,
:propaedeutics, :departments, :apply_shixun_mirror, :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 :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, before_action :allowed, only: [:update, :close, :update_propaedeutics, :settings, :publish,
@ -99,21 +99,32 @@ class ShixunsController < ApplicationController
end end
def shixun_list def shixun_list
# 全部实训/我的实训
type = params[:type] || "all" type = params[:type] || "all"
# 状态:已发布/未发布
status = params[:status] || "all"
# 超级管理员用户显示所有未隐藏的实训、非管理员显示所有已发布的实训(对本单位公开且未隐藏未关闭) # 超级管理员用户显示所有未隐藏的实训、非管理员显示所有已发布的实训(对本单位公开且未隐藏未关闭)
if type == "mine"
@shixuns = current_user.shixuns.none_closed
else
if current_user.admin? if current_user.admin?
@shixuns = Shixun.unhidden @shixuns = Shixun.none_closed.where(hidden: 0)
else else
none_shixun_ids = ShixunSchool.where("school_id != #{current_user.school_id}").pluck(:shixun_id) none_shixun_ids = ShixunSchool.where("school_id != #{current_user.school_id}").pluck(:shixun_id)
@shixuns = Shixun.where.not(id: none_shixun_ids).unhidden @shixuns = Shixun.where.not(id: none_shixun_ids).none_closed.where(hidden: 0)
end
end
unless status == "all"
@shixuns = status == "published" ? @shixuns.where(status: 2) : @shixuns.where(status: [0, 1])
end end
## 搜索关键字创建者、实训名称、院校名称 ## 搜索关键字创建者、实训名称、院校名称
unless params[:search].blank? unless params[:search].blank?
keyword = params[:search].strip keyword = params[:search].strip
@shixuns = @shixuns.joins(user: [user_extenison: :school]). @shixuns = @shixuns.joins(user: [user_extension: :school]).
where("schools.name like '%#{keyword}%' where("schools.name like '%#{keyword}%'
or concat(lastname, firstname) like '%#{keyword}%' or concat(lastname, firstname) like '%#{keyword}%'
or shixuns.name like '%#{keyword.split(" ").join("%")}%'").distinct or shixuns.name like '%#{keyword.split(" ").join("%")}%'").distinct
@ -128,7 +139,7 @@ class ShixunsController < ApplicationController
page = params[:page] || 1 page = params[:page] || 1
limit = params[:limit] || 15 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 end
## 获取顶部菜单 ## 获取顶部菜单

@ -59,6 +59,7 @@ class Shixun < ApplicationRecord
scope :visible, -> { where.not(status: -1) } scope :visible, -> { where.not(status: -1) }
scope :published, lambda{ where(status: 2) } scope :published, lambda{ where(status: 2) }
scope :published_closed, lambda{ where(status: [2, 3]) } 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 :unhidden, lambda{ where(hidden: 0, status: 2) }
scope :field_for_recommend, lambda{ select([:id, :name, :identifier, :myshixuns_count]) } scope :field_for_recommend, lambda{ select([:id, :name, :identifier, :myshixuns_count]) }
scope :find_by_ids,lambda{|k| where(id:k)} scope :find_by_ids,lambda{|k| where(id:k)}

@ -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

@ -179,6 +179,7 @@ Rails.application.routes.draw do
get :get_mirror_script get :get_mirror_script
post :apply_shixun_mirror post :apply_shixun_mirror
get :download_file get :download_file
get :shixun_list
end end
member do member do

Loading…
Cancel
Save