Permalink
Browse files

MDL-16881 gradebook: overview report does not ignore hidden grades if…

… user can not view them
  • Loading branch information...
1 parent c77c231 commit f06de5f7bcd042bff7017093184995fd3213f4e7 skodak committed Oct 14, 2008
Showing with 50 additions and 13 deletions.
  1. +50 −13 grade/report/overview/lib.php
@@ -121,32 +121,69 @@ function fill_table() {
continue;
}
$courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'">'.$course->shortname.'</a>';
+ $canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id));
// Get course grade_item
- $grade_item = grade_item::fetch_course_item($course->id);
-
- // Get the grade
- $grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$this->user->id));
- $grade->grade_item =& $grade_item;
- $finalgrade = $grade->finalgrade;
-
- // TODO: this DOES NOT work properly if there are any hidden grades,
- // rank might be wrong & totals might be different from user report!!!
- if ($grade->is_hidden() and !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id))) {
- $finalgrade = null;
+ $course_item = grade_item::fetch_course_item($course->id);
+
+ // Get the stored grade
+ $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
+ $course_grade->grade_item =& $course_item;
+ $finalgrade = $course_grade->finalgrade;
+
+ if (!$canviewhidden and !is_null($finalgrade)) {
+ if ($course_grade->is_hidden()) {
+ $finalgrade = null;
+
+ } else {
+ // This is a really ugly hack, it will be fixed in 2.0
+ $items = grade_item::fetch_all(array('courseid'=>$course->id));
+ $grades = array();
+ $sql = "SELECT g.*
+ FROM {$CFG->prefix}grade_grades g
+ JOIN {$CFG->prefix}grade_items gi ON gi.id = g.itemid
+ WHERE g.userid = {$this->user->id} AND gi.courseid = {$course->id}";
+ if ($gradesrecords = get_records_sql($sql)) {
+ foreach ($gradesrecords as $grade) {
+ $grades[$grade->itemid] = new grade_grade($grade, false);
+ }
+ unset($gradesrecords);
+ }
+ foreach ($items as $itemid=>$unused) {
+ if (!isset($grades[$itemid])) {
+ $grade_grade = new grade_grade();
+ $grade_grade->userid = $this->user->id;
+ $grade_grade->itemid = $items[$itemid]->id;
+ $grades[$itemid] = $grade_grade;
+ }
+ $grades[$itemid]->grade_item =& $items[$itemid];
+ }
+ $hiding_affected = grade_grade::get_hiding_affected($grades, $items);
+ if (array_key_exists($course_item->id, $hiding_affected['altered'])) {
+ $finalgrade = $hiding_affected['altered'][$course_item->id];
+
+ } else if (!empty($hiding_affected['unknown'][$course_item->id])) {
+ $finalgrade = null;
+ }
+
+ unset($hiding_affected);
+ unset($grades);
+ unset($items);
+ }
}
- $data = array($courselink, grade_format_gradevalue($finalgrade, $grade_item, true));
+ $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
if (!$this->showrank) {
//nothing to do
} else if (!is_null($finalgrade)) {
/// find the number of users with a higher grade
+ /// please note this can not work if hidden grades involved :-( to be fixed in 2.0
$sql = "SELECT COUNT(DISTINCT(userid))
FROM {$CFG->prefix}grade_grades
WHERE finalgrade IS NOT NULL AND finalgrade > $finalgrade
- AND itemid = {$grade_item->id}";
+ AND itemid = {$course_item->id}";
$rank = count_records_sql($sql) + 1;
$data[] = "$rank/$numusers";

0 comments on commit f06de5f

Please sign in to comment.