Ticket #598: Refactored calculate_total_mark method in grade_entry_form.rb
Updated 1 year, 11 months ago
| Farah Juma | Reviewers | ||
| markus_developers | |||
| 598 | |||
| None | MarkUs Source Code Repository | ||
Mike suggested refactoring the calculate_total_mark method in grade_entry_form.rb. I've modified it to use the "sum" method instead of manually calculating the total mark.
I ran the functional tests and unit tests and there are no failures or errors. I also tested things manually to make sure the total mark is still getting calculated correctly.
| trunk/app/models/grade_entry_form.rb | |||
|---|---|---|---|
| Revision 1196 | New Change | ||
| ... | 24 lines hidden [Expand] | ||
def validate |
|||
| 25 |
return grade_entry_items.sum('out_of').to_i |
25 |
return grade_entry_items.sum('out_of').to_i |
| 26 |
end
|
26 |
end
|
| 27 |
|
27 |
|
| 28 |
# Determine the total mark for a particular student
|
28 |
# Determine the total mark for a particular student
|
| 29 |
def calculate_total_mark(student_id) |
29 |
def calculate_total_mark(student_id) |
| 30 | grade_entry_items = self.grade_entry_items |
30 | # Differentiate between a blank total mark and a total mark of 0 |
| 31 | total = 0.0 |
31 | total = "" |
| 32 | no_grades_entered = true |
32 | |
| 33 | |||
| 34 |
grade_entry_student = self.grade_entry_students.find_by_user_id(student_id) |
33 |
grade_entry_student = self.grade_entry_students.find_by_user_id(student_id) |
| 35 |
if !grade_entry_student.nil? |
34 |
if !grade_entry_student.nil? |
| 36 | grade_entry_items.each do |grade_entry_item| |
35 | total = grade_entry_student.grades.sum('grade') |
| 37 |
grade = Grade.find_by_grade_entry_student_id_and_grade_entry_item_id(grade_entry_student.id, |
||
| 38 |
grade_entry_item.id) |
||
| 39 |
if !grade.nil? and !grade.grade.nil? |
||
| 40 |
no_grades_entered = false |
||
| 41 |
total += grade.grade |
||
| 42 |
end
|
||
| 43 |
end
|
||
| 44 |
end
|
||
| 45 |
|
||
| 46 |
# Differentiate between a blank total mark and a total mark of 0
|
||
| 47 |
if no_grades_entered |
||
| 48 |
total = "" |
||
| 49 |
end
|
36 |
end
|
| 50 |
|
37 |
|
| 51 |
return total |
38 |
return total |
| 52 |
end
|
39 |
end
|
| 53 |
|
40 |
|
| ... | 79 lines hidden [Expand] | ||

Other reviews