From f956b2795384bc690d095486af4d99d334aa3033 Mon Sep 17 00:00:00 2001 From: sw <939547590@qq.com> Date: Thu, 11 Jun 2015 15:31:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A=E7=A7=AF=E5=88=86=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E5=B9=B3=E5=9D=87=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/courses_controller.rb | 4 ++-- app/models/member.rb | 18 ++++++++++-------- app/views/courses/_show_member_score.html.erb | 6 +++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index e89f621c8..518cc01c4 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -836,7 +836,7 @@ class CoursesController < ApplicationController sql_select = "" if groupid == 0 sql_select = "SELECT members.*,( - SELECT SUM(student_works.final_score) + SELECT AVG(student_works.final_score) FROM student_works,homework_commons WHERE student_works.homework_common_id = homework_commons.id AND homework_commons.course_id = #{@course.id} @@ -848,7 +848,7 @@ class CoursesController < ApplicationController WHERE members.course_id = #{@course.id} ORDER BY score #{score_sort_by}" else sql_select = "SELECT members.*,( - SELECT SUM(student_works.final_score) + SELECT AVG(student_works.final_score) FROM student_works,homework_commons WHERE student_works.homework_common_id = homework_commons.id AND homework_commons.course_id = #{@course.id} diff --git a/app/models/member.rb b/app/models/member.rb index 2936392ab..7bde36287 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -116,21 +116,23 @@ class Member < ActiveRecord::Base # 查找每个学生每个作业的评分 def student_homework_score - score_count = 0 homework_score = StudentWork.find_by_sql("SELECT homework_commons.name,student_works.final_score as score FROM student_works,homework_commons WHERE student_works.homework_common_id = homework_commons.id AND homework_commons.course_id = #{self.course_id} AND student_works.user_id = #{self.user_id}") - homework_score.each do |homework| - mem_score = 0 - if homework[:score] - mem_score = homework[:score] - end - score_count = score_count + mem_score - end + score_count = StudentWork.joins(:homework_common).where("student_works.user_id = #{self.user_id} and homework_commons.course_id = #{self.course_id}").average(:final_score).try(:round, 2).to_f [homework_score, format("%0.2f", score_count)] end + + def student_work_score + StudentWork.joins(:homework_common).where("student_works.user_id = #{self.user_id} and homework_commons.course_id = #{self.course_id}") + end + + def student_work_score_avg + StudentWork.joins(:homework_common).where("student_works.user_id = #{self.user_id} and homework_commons.course_id = #{self.course_id}").average(:final_score).try(:round, 2).to_f + end + protected def validate_role diff --git a/app/views/courses/_show_member_score.html.erb b/app/views/courses/_show_member_score.html.erb index 0d250cf14..0fd1248c6 100644 --- a/app/views/courses/_show_member_score.html.erb +++ b/app/views/courses/_show_member_score.html.erb @@ -17,17 +17,17 @@

<%= @member_score.user.name %> 历次作业积分