优化《作业评分》

Signed-off-by: alan <547533434@qq.com>
This commit is contained in:
alan 2014-12-26 14:50:14 +08:00
parent f8dc9dcd35
commit d2aef552e6
4 changed files with 106 additions and 14 deletions

View File

@ -759,6 +759,7 @@ class ApplicationController < ActionController::Base
end
end
#查找首页相关信息
def find_first_page
@first_page = FirstPage.find_by_page_type('project')

View File

@ -216,7 +216,7 @@ class CoursesController < ApplicationController
@results = searchmember_by_name(student_homework_score, q)
elsif params[:ingroup]
@group = CourseGroup.find(params[:search_group_id])
@results = @results =searchgroupmember_by_name(student_homework_score, @group, q)
@results =searchgroupmember_by_name(student_homework_score, @group, q)
end
@result_count = @results.count
@ -881,17 +881,11 @@ class CoursesController < ApplicationController
end
def student_homework_score
teachers = find_course_teachers(@course)
homework_scores = Member.find_by_sql("SELECT id, user_id,course_id, course_group_id,created_on,IFNULL(SUM(CASE WHEN t_score <> 0 AND t_score IS NOT NULL THEN t_score ELSE s_score END),0) as score
FROM (
SELECT members.id as id, members.user_id AS user_id, members.course_id AS course_id, members.created_on as created_on,members.course_group_id AS course_group_id,
(SELECT AVG(seems_rateable_rates.stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id IN (#{teachers})) AS t_score,
(SELECT AVG(seems_rateable_rates.stars) FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id AND rater_id NOT IN (#{teachers})) AS s_score
FROM `homework_attaches` , members
WHERE members.course_id = #{@course.id}
AND members.user_id NOT IN (#{teachers}) AND homework_attaches.bid_id IN (SELECT bid_id FROM homework_for_courses WHERE course_id = #{@course.id})
AND homework_attaches.user_id = members.user_id
) AS table1 GROUP BY user_id ORDER BY score DESC")
#teachers = find_course_teachers(@course)
sql = ActiveRecord::Base.connection()
homework_scores = Member.find_by_sql("call member_score(#{@course.id})")
sql.close()
homework_scores
end
#获取课程的老师列表

View File

@ -53,7 +53,7 @@
<% members.each do |member| %>
<div class="cl"></div><!--st_box_top end-->
<div class="st_boxlist">
<% next if member.new_record? %>
<a href="#" class="st_img" style="float:left;">
<%= member.user.nil? ? '' : (image_tag(url_to_avatar(member.user), :width => 40, :height => 40)) %>
</a>
@ -101,7 +101,7 @@
</div>
</div>
<div class="pagination">
<%= pagination_links_full @obj_pages, @obj_count, :per_page_links => false %>
<%= pagination_links_full @obj_pages, @obj_count, :remote =>true, :per_page_links => false %>
</div>
<% else %>
<p class="nodata">

97
data/member_score.sql Normal file
View File

@ -0,0 +1,97 @@
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `member_score`(IN courseid INT)
/*LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT 'string'*/
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;
SELECT * FROM mems ORDER BY score DESC;
END$$
DELIMITER ;