编写《作业查询存储过程》

Signed-off-by: alan <547533434@qq.com>
This commit is contained in:
alan 2014-12-26 15:51:39 +08:00
parent d2aef552e6
commit b79b08fa8f
4 changed files with 107 additions and 104 deletions

View File

@ -285,7 +285,7 @@ class CoursesController < ApplicationController
member.course_group_id = group.id member.course_group_id = group.id
member.save member.save
@group = group @group = group
@members = student_homework_score
@course_groups = @course.course_groups @course_groups = @course.course_groups
@membercount = @members.count @membercount = @members.count
search_group_members group search_group_members group
@ -298,7 +298,7 @@ class CoursesController < ApplicationController
member.save member.save
@group = group @group = group
@course_groups = @course.course_groups @course_groups = @course.course_groups
@members = student_homework_score
@membercount = @members.count @membercount = @members.count
search_group_members group search_group_members group
end end
@ -309,12 +309,12 @@ class CoursesController < ApplicationController
@is_remote = true @is_remote = true
if params[:group_id] && params[:group_id] != "0" if params[:group_id] && params[:group_id] != "0"
@group = CourseGroup.find(params[:group_id]) @group = CourseGroup.find(params[:group_id])
@results = student_homework_score.find_all {|mem| mem.course_group_id == group.id} @results = student_homework_score.find_all {|mem| mem.course_group_id == @group.id}
else else
@results = student_homework_score @results = student_homework_score
end end
@@result = @results @@result = @results
@members = searchStudent(@course) @members = searchStudent(@course)
@membercount = @members.count @membercount = @members.count
@results = paginateHelper @results @results = paginateHelper @results

View File

@ -129,7 +129,7 @@ class MembersController < ApplicationController
member = Member.new(:role_ids => params[:membership][:role_ids], :user_id => user_id) member = Member.new(:role_ids => params[:membership][:role_ids], :user_id => user_id)
role = Role.find_by_id(params[:membership][:role_ids]) role = Role.find_by_id(params[:membership][:role_ids])
# 这里的判断只能通过角色名,可以弄成常量 # 这里的判断只能通过角色名,可以弄成常量
if role.name == "学生" if role.name == "学生" || role.name == "Student"
StudentsForCourse.create(:student_id => user_id, :course_id =>@course.id) StudentsForCourse.create(:student_id => user_id, :course_id =>@course.id)
end end
members << member members << member
@ -210,7 +210,7 @@ class MembersController < ApplicationController
if (params[:membership][:role_ids]) if (params[:membership][:role_ids])
role = Role.find(params[:membership][:role_ids][0]) role = Role.find(params[:membership][:role_ids][0])
# 这里的判断只能通过角色名,可以弄成常量 # 这里的判断只能通过角色名,可以弄成常量
if role.name == "学生" if role.name == "学生" || role.name == "Student"
StudentsForCourse.create(:student_id => @member.user_id, :course_id =>@course.id) StudentsForCourse.create(:student_id => @member.user_id, :course_id =>@course.id)
else else
joined = StudentsForCourse.where('student_id = ? and course_id = ?', @member.user_id,@course.id) joined = StudentsForCourse.where('student_id = ? and course_id = ?', @member.user_id,@course.id)

View File

@ -1,97 +1,100 @@
#encoding=UTF-8
DELIMITER $$ class SelectMemberScore < ActiveRecord::Migration
def up
CREATE sql = ("
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `member_score`(IN courseid INT) CREATE
/*LANGUAGE SQL
| [NOT] DETERMINISTIC PROCEDURE `member_score`(IN courseid INT)
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER } BEGIN
| COMMENT 'string'*/
BEGIN CREATE TEMPORARY TABLE IF NOT EXISTS course_teachers (
id INT
CREATE TEMPORARY TABLE IF NOT EXISTS course_teachers (
id INT );
TRUNCATE TABLE course_teachers;
);
TRUNCATE TABLE course_teachers; CREATE TEMPORARY TABLE IF NOT EXISTS mems (
id INT ,
CREATE TEMPORARY TABLE IF NOT EXISTS mems ( user_id INT,
id INT , course_id INT,
user_id INT, created_on DATETIME,
course_id INT, course_group_id INT,
created_on DATETIME, score FLOAT DEFAULT 0
course_group_id INT, );
score FLOAT DEFAULT 0 TRUNCATE TABLE mems;
);
TRUNCATE TABLE mems; CREATE TEMPORARY TABLE IF NOT EXISTS mem_home (
id INT,
CREATE TEMPORARY TABLE IF NOT EXISTS mem_home ( user_id INT,
id INT, course_id INT,
user_id INT, created_on DATETIME,
course_id INT, course_group_id INT,
created_on DATETIME, home_id INT,
course_group_id INT, score FLOAT DEFAULT 0
home_id INT, );
score FLOAT DEFAULT 0 TRUNCATE TABLE mem_home;
);
TRUNCATE TABLE mem_home; CREATE TEMPORARY TABLE IF NOT EXISTS t_scores (
CREATE TEMPORARY TABLE IF NOT EXISTS t_scores ( home_id INT,
score FLOAT
home_id INT, );
score FLOAT TRUNCATE TABLE t_scores;
);
TRUNCATE TABLE t_scores; CREATE TEMPORARY TABLE IF NOT EXISTS s_scores (
CREATE TEMPORARY TABLE IF NOT EXISTS s_scores ( home_id INT,
score FLOAT
home_id INT, );
score FLOAT TRUNCATE TABLE s_scores;
);
TRUNCATE TABLE s_scores; CREATE TEMPORARY TABLE IF NOT EXISTS scores (
CREATE TEMPORARY TABLE IF NOT EXISTS scores ( user_id INT,
score FLOAT
user_id INT, );
score FLOAT TRUNCATE TABLE scores;
);
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 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
INSERT INTO mems (id, user_id, course_id, created_on, course_group_id) FROM members WHERE course_id = courseid AND members.user_id NOT IN (SELECT id FROM course_teachers));
(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
INSERT INTO mem_home (id, user_id, course_id, created_on, course_group_id,home_id) AND members.user_id = homework_attaches.user_id AND members.user_id NOT IN (SELECT id FROM course_teachers)
(SELECT members.id, members.user_id, members.course_id, members.created_on, members.course_group_id, homework_attaches.id AND homework_attaches.bid_id IN (SELECT bid_id FROM homework_for_courses WHERE course_id = courseid ));
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) INSERT INTO t_scores (home_id, score) (SELECT rateable_id,AVG(seems_rateable_rates.stars)
AND homework_attaches.bid_id IN (SELECT bid_id FROM homework_for_courses WHERE course_id = courseid )); 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)
INSERT INTO t_scores (home_id, score) (SELECT rateable_id,AVG(seems_rateable_rates.stars) GROUP BY rateable_id);
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) INSERT INTO s_scores (home_id, score) (SELECT rateable_id,AVG(seems_rateable_rates.stars)
GROUP BY rateable_id); 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)
INSERT INTO s_scores (home_id, score) (SELECT rateable_id,AVG(seems_rateable_rates.stars) GROUP BY rateable_id) ;
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) UPDATE mem_home, t_scores SET mem_home.score = t_scores.score WHERE mem_home.home_id = t_scores.home_id ;
GROUP BY rateable_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;
UPDATE mem_home, t_scores SET mem_home.score = t_scores.score WHERE mem_home.home_id = t_scores.home_id ;
INSERT INTO scores (user_id, score) (SELECT user_id, SUM(score) FROM mem_home GROUP BY user_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; UPDATE mems, scores SET mems.score = scores.score WHERE mems.user_id = scores.user_id;
INSERT INTO scores (user_id, score) (SELECT user_id, SUM(score) FROM mem_home GROUP BY user_id); SELECT * FROM mems ORDER BY score DESC;
UPDATE mems, scores SET mems.score = scores.score WHERE mems.user_id = scores.user_id; END;
SELECT * FROM mems ORDER BY score DESC; ")
END$$ execute(sql)
end
DELIMITER ;
def down
end
end

View File

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