优化《学生列表》分页查询

Signed-off-by: alan <547533434@qq.com>
This commit is contained in:
alan 2014-12-29 16:57:31 +08:00
parent 329be27567
commit fb398904b6
5 changed files with 158 additions and 42 deletions

View File

@ -759,6 +759,18 @@ class ApplicationController < ActionController::Base
end
end
def paginateHelper_for_members obj, pre_size=10
@obj_count = StudentsForCourse.find_by_sql("select count(id) as mem_count from students_for_courses where course_id = #{@course.id}")[0][:mem_count].to_s.to_i
@obj_pages = Paginator.new @obj_count, pre_size, params['page']
if obj.kind_of? ActiveRecord::Base or obj.kind_of? ActiveRecord::Relation
obj.limit(@obj_pages.per_page).offset(0)
elsif obj.kind_of? Array
obj[0, @obj_pages.per_page]
else
logger.error "[ApplicationController] Error : application_controller#paginateHelper ===> unknow category: #{obj.class}"
raise RuntimeError, 'unknow type, Please input you type into this helper.'
end
end
#查找首页相关信息
def find_first_page

View File

@ -23,7 +23,7 @@ class CoursesController < ApplicationController
before_filter :require_login, :only => [:join, :unjoin]
#before_filter :allow_join, :only => [:join]
@@result = nil
def join
if User.current.logged?
course = Course.find_by_id params[:object_id]
@ -213,10 +213,11 @@ class CoursesController < ApplicationController
q = "#{params[:name].strip}"
#(redirect_to stores_url, :notice => l(:label_sumbit_empty);return) if params[:name].blank?
if params[:incourse]
@results = searchmember_by_name(student_homework_score, q)
@results = searchmember_by_name(student_homework_score(0,0,0), q)
elsif params[:ingroup]
@group = CourseGroup.find(params[:search_group_id])
@results =searchgroupmember_by_name(student_homework_score, @group, q)
@results = searchmember_by_name(student_homework_score(@group.id,0,0), q)
end
@is_remote = true
@result_count = @results.count
@ -285,9 +286,9 @@ class CoursesController < ApplicationController
member.course_group_id = group.id
member.save
@group = group
@members = student_homework_score
@course_groups = @course.course_groups
@membercount = @members.count
search_group_members group
end
def unjoin_group
@ -298,8 +299,7 @@ class CoursesController < ApplicationController
member.save
@group = group
@course_groups = @course.course_groups
@members = student_homework_score
@membercount = @members.count
search_group_members group
end
def searchgroupmembers
@ -309,15 +309,19 @@ class CoursesController < ApplicationController
@is_remote = true
if params[:group_id] && params[:group_id] != "0"
@group = CourseGroup.find(params[:group_id])
@results = student_homework_score.find_all {|mem| mem.course_group_id == @group.id}
@results = student_homework_score(@group.id,0, 0)
@results = paginateHelper @results, 10
else
@results = student_homework_score
page_from = params[:page].nil? ? 0 : (params[:page].to_i - 1)
@results = student_homework_score(0,page_from, 10)
@results = paginateHelper_for_members @results, 10
end
@members = searchStudent(@course)
@membercount = @members.count
@results = paginateHelper @results
end
@ -325,31 +329,36 @@ class CoursesController < ApplicationController
## 有角色参数的才是课程,没有的就是项目
if (User.current.admin? || @course.is_public == 1 || (@course.is_public == 0 && User.current.member_of_course?(@course)))
@render_file = 'member_list'
@teachers= searchTeacherAndAssistant(@course)
@canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1'
@role = params[:role]
@is_remote = false
@role = params[:role].nil? ? '2':params[:role]
@is_remote = true
@course_groups = @course.course_groups if @course.course_groups
@show_serch = params[:role] == '2'
case params[:role]
case @role
when '1'
@subPage_title = l :label_teacher_list
@members = @teachers
@members = searchTeacherAndAssistant(@course)
@members = paginateHelper @members, 10
when '2'
@subPage_title = l :label_student_list
@members = student_homework_score
page = params[:page].nil? ? 0 : (params['page'].to_i - 1)
@members = student_homework_score(0,page, 10)
# @member_scores = @@member_scores
# @members = searchStudent_sort(@course, 'desc', @@member_scores)
@membercount = @members.count
@members = paginateHelper_for_members @members, 10
end
respond_to do |format|
if params[:page]
format.js
else
@subPage_title = ''
@members = @course.member_principals.includes(:roles, :principal).all.sort
format.html {render :layout => 'base_courses'}
end
end
@members = paginateHelper @members
render :layout => 'base_courses'
# render :layout => 'base_courses'
else
render_403
end
@ -378,19 +387,11 @@ class CoursesController < ApplicationController
respond_to do |format|
format.html {render :layout => 'course_base'}
format.js
end
end
#判断指定用户是否为课程教师
def isCourseTeacher(id)
result = false
if @teachers.find_by_user_id(id) != nil
result = true
end
result
end
def handle_course courses, activities
course_activity_count_array=activities.values()
@ -881,11 +882,13 @@ class CoursesController < ApplicationController
end
end
def student_homework_score
def student_homework_score(groupid,start_from, nums)
#teachers = find_course_teachers(@course)
start_from = start_from * nums
sql = ActiveRecord::Base.connection()
homework_scores = Member.find_by_sql("call member_score(#{@course.id})")
homework_scores = Member.find_by_sql("call member_score(#{@course.id},#{groupid},#{start_from},#{nums})")
sql.close()
homework_scores
end
@ -901,10 +904,10 @@ class CoursesController < ApplicationController
@canShowCode = isCourseTeacher(User.current.id,@course) && params[:role] != '1'
@is_remote = true
@members = searchStudent(@course)
@membercount = @members.count
@results = student_homework_score.find_all {|mem| mem.course_group_id == group.id}
@@result = @results
@results = paginateHelper @results
page_from = params[:page].nil? ? 0 : (params[:page].to_i - 1)
@results = student_homework_score(group.id,0,0)
@results = paginateHelper @results, 10
end
end

View File

@ -0,0 +1 @@
$("#member_content").html("<%= escape_javascript( render :partial => @render_file, :locals => {:members => @members})%>");

View File

@ -0,0 +1,100 @@
class ProMemberScore < ActiveRecord::Migration
def up
sql_delete = ("DROP PROCEDURE IF EXISTS `member_score`;")
sql = ("
CREATE PROCEDURE `member_score`(IN courseid INT, IN groupid INT,IN start_from INT, IN nums INT)
BEGIN
CREATE TEMPORARY TABLE IF NOT EXISTS course_teachers (
id INT
);
TRUNCATE TABLE course_teachers;
CREATE TEMPORARY TABLE IF NOT EXISTS mems (
id INT ,
user_id INT,
course_id INT,
created_on DATETIME,
course_group_id INT,
score FLOAT DEFAULT 0
);
TRUNCATE TABLE mems;
CREATE TEMPORARY TABLE IF NOT EXISTS mem_home (
id INT,
user_id INT,
course_id INT,
created_on DATETIME,
course_group_id INT,
home_id INT,
score FLOAT DEFAULT 0
);
TRUNCATE TABLE mem_home;
CREATE TEMPORARY TABLE IF NOT EXISTS t_scores (
home_id INT,
score FLOAT
);
TRUNCATE TABLE t_scores;
CREATE TEMPORARY TABLE IF NOT EXISTS s_scores (
home_id INT,
score FLOAT
);
TRUNCATE TABLE s_scores;
CREATE TEMPORARY TABLE IF NOT EXISTS scores (
user_id INT,
score FLOAT
);
TRUNCATE TABLE scores;
INSERT INTO course_teachers (SELECT members.user_id FROM members WHERE members.user_id NOT IN (SELECT student_id FROM students_for_courses WHERE course_id = courseid));
INSERT INTO mems (id, user_id, course_id, created_on, course_group_id)
(SELECT members.id, members.user_id, members.course_id, members.created_on, members.course_group_id
FROM members WHERE course_id = courseid AND members.user_id NOT IN (SELECT id FROM course_teachers));
INSERT INTO mem_home (id, user_id, course_id, created_on, course_group_id,home_id)
(SELECT members.id, members.user_id, members.course_id, members.created_on, members.course_group_id, homework_attaches.id
FROM members, homework_attaches WHERE course_id = courseid
AND members.user_id = homework_attaches.user_id AND members.user_id NOT IN (SELECT id FROM course_teachers)
AND homework_attaches.bid_id IN (SELECT bid_id FROM homework_for_courses WHERE course_id = courseid ));
INSERT INTO t_scores (home_id, score) (SELECT rateable_id,AVG(seems_rateable_rates.stars)
FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id IN (SELECT mem_home.home_id FROM mem_home)
AND rater_id IN (SELECT id FROM course_teachers)
GROUP BY rateable_id);
INSERT INTO s_scores (home_id, score) (SELECT rateable_id,AVG(seems_rateable_rates.stars)
FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id IN (SELECT mem_home.home_id FROM mem_home)
AND rater_id NOT IN (SELECT id FROM course_teachers)
GROUP BY rateable_id) ;
UPDATE mem_home, t_scores SET mem_home.score = t_scores.score WHERE mem_home.home_id = t_scores.home_id ;
UPDATE mem_home, s_scores SET mem_home.score = s_scores.score WHERE mem_home.home_id = s_scores.home_id AND mem_home.score = 0;
INSERT INTO scores (user_id, score) (SELECT user_id, SUM(score) FROM mem_home GROUP BY user_id);
UPDATE mems, scores SET mems.score = scores.score WHERE mems.user_id = scores.user_id;
IF nums <> 0 THEN
SELECT * FROM mems ORDER BY score DESC LIMIT start_from, nums;
ELSE
SELECT * FROM mems WHERE course_group_id = groupid ORDER BY score DESC;
END IF;
END;
")
execute(sql_delete)
execute(sql)
end
def down
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20141226074532) do
ActiveRecord::Schema.define(:version => 20141229081716) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false