重构项目和课程排序部分,修复分页数量错误

This commit is contained in:
yanxd 2014-03-21 16:46:30 +08:00
parent 20cd5941a8
commit 9c54c9c1b6
3 changed files with 57 additions and 59 deletions

View File

@ -49,7 +49,7 @@ class ProjectsController < ApplicationController
# before_filter :authorize, :except => [:new_join, :new_homework, :homework, :statistics, :search, :watcherlist, :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy, :member, :focus, :file,
# :statistics, :feedback, :course, :enterprise_course, :course_enterprise, :project_respond, :share,
# :show_projects_score, :issue_score_index, :news_score_index, :file_score_index, :code_submit_score_index, :projects_topic_score_index]
before_filter :authorize, :only => [:sort_project_demo, :show, :settings, :edit, :sort_project_members, :update, :modules, :close, :reopen]
before_filter :authorize, :only => [:show, :settings, :edit, :sort_project_members, :update, :modules, :close, :reopen]
before_filter :authorize_global, :only => [:new, :create]
before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy, :calendar]
#by young
@ -127,32 +127,39 @@ class ProjectsController < ApplicationController
def index
#Modified by nie
@project_type = params[:project_type]
@limit = 10
@project_count = Project.visible.where("project_type = ?", 0).count
@project_pages = Paginator.new @project_count, @limit, params['page']
if params[:project_sort_type].present?
per_page_option = 10
@projects_all = Project.active.visible.
joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id").
where("#{Project.table_name}.project_type = ? ", Project::ProjectType_project)
@project_count = @projects_all.count
@project_pages = Paginator.new @project_count, per_page_option, params['page']
case params[:project_sort_type]
when '0'
@projects = @projects = Project.visible.where("project_type = ?", 0).order("created_on desc").offset(@project_pages.offset).limit(@limit).all
@projects = @projects_all.order("created_on desc")
@s_type = 0
when '1'
@projects = sort_project_demo project_type = 0, order_by="grade desc", @project_pages.offset, @limit
@projects = @projects_all.order("grade desc")
@s_type = 1
when '2'
@projects = sort_project_demo project_type = 0, order_by="watchers_count desc", @project_pages.offset, @limit
@projects = @projects_all.order("watchers_count desc")
@s_type = 2
end
else
@projects = sort_project_demo project_type = 0, order_by="grade desc", @project_pages.offset, @limit
@projects = @projects = @projects_all.order("grade desc")
@s_type = 1
end
@projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page)
respond_to do |format|
format.html {
render :layout => 'base'
scope = Project
unless params[:closed]
scope = scope.active
end
# scope = Project
# unless params[:closed]
# scope = scope.active
# end
}
format.api {
# @offset, @limit = api_offset_and_limit
@ -168,34 +175,34 @@ class ProjectsController < ApplicationController
def course
@project_type = params[:project_type]
@limit = 10;
@project_count = Project.visible.where("project_type = 1").count
@project_pages = Paginator.new @project_count, @limit, params['page']
if params[:project_sort_type].present?
per_page_option = 10
@projects_all = Project.active.visible.
joins("LEFT JOIN #{ProjectStatus.table_name} ON #{Project.table_name}.id = #{ProjectStatus.table_name}.project_id").
where("#{Project.table_name}.project_type = ? ", Project::ProjectType_course)
@project_count = @projects_all.count
@project_pages = Paginator.new @project_count, per_page_option, params['page']
case params[:project_sort_type]
when '0'
@projects = Project.visible.where("project_type = ?", 1).order("created_on desc").offset(@project_pages.offset).limit(@limit).all
@projects = @projects_all.order("created_on desc")
@s_type = 0
when '1'
@projects = sort_project_demo 1, "course_ac_para desc", @project_pages.offset, @limit
@projects = @projects_all.order("course_ac_para desc")
@s_type = 1
when '2'
@projects = sort_project_demo 1, "watchers_count desc", @project_pages.offset, @limit
@projects = @projects_all.order("watchers_count desc")
@s_type = 2
end
else
# @projects = sort_project_demo 1, "course_ac_para desc", @project_pages.offset, @limit
@s_type = 0
@projects = Project.visible.where("project_type = ?", 1).order("created_on desc").offset(@project_pages.offset).limit(@limit).all
# @s_type = 0
@projects = @projects_all.order("created_on desc")
end
@projects = @projects.offset(@project_pages.offset).limit(@project_pages.per_page)
respond_to do |format|
format.html {
render :layout => 'base'
scope = Project
unless params[:closed]
scope = scope.active
end
}
format.api {
# @offset, @limit = api_offset_and_limit
@ -209,15 +216,6 @@ class ProjectsController < ApplicationController
end
end
def sort_project_demo project_type, order_by, offset, limit
#Project.visible.joins(:project_status).where("#{Project.table_name}.project_type = ?", project_type).order(order_by).offset(offset).limit(limit).all
Project.visible.find_by_sql("
SELECT *
FROM projects AS p RIGHT OUTER JOIN (
SELECT project_id,grade FROM project_statuses
WHERE project_type = #{project_type} ORDER BY #{order_by} LIMIT #{limit} OFFSET #{offset}) AS t ON p.id = t.project_id ")
end
def search
#modified by nie
@projects = Project.visible
@ -461,7 +459,7 @@ class ProjectsController < ApplicationController
project = ProjectInfo.new(:user_id => User.current.id, :project_id => @project.id)
user_grades = UserGrade.create(:user_id => User.current.id, :project_id => @project.id)
if params[:project][:is_public] == '1' || @course_tag=="1"
project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0)
project_status = ProjectStatus.create(:project_id => @project.id, :watchers_count => 0, :changesets_count => 0, :project_type => @project.project_type)
end
@project.members << m
@project.project_infos << project

View File

@ -38,7 +38,7 @@
<div class="pagination">
<ul>
<%= pagination_links_full @project_pages %>
<%= pagination_links_full @project_pages, @project_count %>
</ul>
</div>

View File

@ -34,7 +34,7 @@
<div class="pagination">
<ul>
<%= pagination_links_full @project_pages %>
<%= pagination_links_full @project_pages, @project_count %>
</ul>
</div>