From 0dbb4c6c540daae66c3019dd6ae40541e0d815e1 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 10 Jul 2019 10:09:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=89=E7=94=A8=E5=AE=9E=E8=AE=AD=E8=AF=BE?= =?UTF-8?q?=E7=A8=8B=E7=9A=84=E5=BC=B9=E6=A1=86=E6=94=B9=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../homework_commons_controller.rb | 49 ++++++------------- app/models/subject.rb | 5 ++ .../homework_commons/subjects.json.jbuilder | 6 ++- 3 files changed, 26 insertions(+), 34 deletions(-) diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index e662525f1..7b61f8d4e 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -829,7 +829,13 @@ class HomeworkCommonsController < ApplicationController if params[:order_by] == 'mine' current_user.my_shixuns.unhidden else - Shixun.unhidden + if current_user.admin? + 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 end ## 方向 @@ -903,34 +909,10 @@ class HomeworkCommonsController < ApplicationController # 选用实训课程 def subjects - search = params[:search] - type = params[:type] - # 显示所有未隐藏的、已发布的实训课程 - @subjects = Subject.select([:id, :name, :status, :repertoire_id]).visible.unhidden - - @tags = Repertoire.select([:id, :name]).where(id: @subjects.pluck(:repertoire_id).uniq).order("updated_at desc") - - if params[:search] && params[:search].strip != "" - @subjects = @subjects.joins(:user).where("subjects.name like ? or concat(users.lastname, users.firstname) like ?", - "%#{search}%", "%#{search}%") - end - - unless type.blank? || type == "all" - @subjects = @subjects.where(repertoire_id: type) - end - - @subjects = @subjects.reorder("subjects.created_at desc") - @subjects_count = @subjects.size - - ## 分页参数 - page = params[:page] || 1 - @subjects = @subjects.page(page).per(10) - - @subjects = @subjects.includes(:shixuns) - @tags = Repertoire.where(nil).order("updated_at desc") select = params[:select] # 路径导航类型 reorder = params[:order] || "myshixun_count" + sort = params[:sort] || "desc" search = params[:search] ## 分页参数 @@ -941,17 +923,17 @@ class HomeworkCommonsController < ApplicationController # 最热排序 if reorder == "myshixun_count" if select - @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, + @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, subjects.shixuns_count, sum(shixuns.myshixuns_count) AS myshixun_member_count FROM subjects join stage_shixuns on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id where subjects`.`hidden` = 0 AND `subjects`.`status` = 2 AND `subjects`.`name` like '%#{search}%' - AND `subjects`.`repertoire_id` = #{select} GROUP BY subjects.id ORDER BY myshixun_member_count DESC") + AND `subjects`.`repertoire_id` = #{select} GROUP BY subjects.id ORDER BY myshixun_member_count #{sort}") else - @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, + @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, subjects.shixuns_count, sum(shixuns.myshixuns_count) AS myshixun_member_count FROM subjects join stage_shixuns on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id where `subjects`.`hidden` = 0 AND `subjects`.`status` = 2 AND `subjects`.`name` like '%#{search}%' - GROUP BY subjects.id ORDER BY myshixun_member_count DESC") + GROUP BY subjects.id ORDER BY myshixun_member_count #{sort}") end else # 我的路径 @@ -973,17 +955,18 @@ class HomeworkCommonsController < ApplicationController end # 排序 - order_str = reorder == "publish_time" ? "status = 2 desc, publish_time asc" : "updated_at desc" + order_str = "updated_at #{sort}" @subjects = @subjects.reorder(order_str) end @total_count = @subjects.size if reorder != "myshixun_count" - @subjects = @subjects.page(page).per(limit).includes(:shixuns) + @subjects = @subjects.page(page).per(limit).includes(:shixuns, user: [user_extension: :school]) else @subjects = @subjects[offset, limit] - + subject_ids = @subjects.pluck(:id) + @subjects = Subject.where(id: subject_ids).order("field(id,#{subject_ids.join(',')})").includes(:shixuns, user: [user_extension: :school]) end end diff --git a/app/models/subject.rb b/app/models/subject.rb index ced610377..115eecd49 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -88,4 +88,9 @@ class Subject < ApplicationRecord def published? status == 2 end + + def shixun_tags + challenges = Challenge.where(shixun_id: shixuns.unhidden) + @tags = ChallengeTag.where(challenge_id: challenges).pluck(:name).uniq + end end \ No newline at end of file diff --git a/app/views/homework_commons/subjects.json.jbuilder b/app/views/homework_commons/subjects.json.jbuilder index ea63f2125..257fcc9d5 100644 --- a/app/views/homework_commons/subjects.json.jbuilder +++ b/app/views/homework_commons/subjects.json.jbuilder @@ -6,8 +6,12 @@ end json.subject_list @subjects do |subject| json.subject_id subject.id json.subject_name subject.name + json.challenge_tags subject.shixun_tags json.shixun_count subject.shixuns.unhidden.size json.myshixun_count subject.shixuns.pluck(:myshixuns_count).sum + json.creator subject.user&.full_name + json.creator_login subject.user&.login + json.school subject.user&.school_name end -json.subjects_count @subjects_count \ No newline at end of file +json.subjects_count @total_count \ No newline at end of file