MDL-15133 fixed regressions - thanks Howard
[moodle.git] / grade / report / overview / lib.php
CommitLineData
aa330ebb 1<?php // $Id$
8ad36f4c 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
aa330ebb 25/**
26 * File in which the overview_report class is defined.
27 * @package gradebook
28 */
29
30require_once($CFG->dirroot . '/grade/report/lib.php');
31require_once($CFG->libdir.'/tablelib.php');
32
33/**
34 * Class providing an API for the overview report building and displaying.
35 * @uses grade_report
36 * @package gradebook
37 */
38class grade_report_overview extends grade_report {
39
40 /**
41 * The user.
42 * @var object $user
43 */
44 var $user;
45
46 /**
47 * A flexitable to hold the data.
48 * @var object $table
49 */
50 var $table;
51
60574063 52 /**
53 * show student ranks
54 */
55 var $showrank;
56
aa330ebb 57 /**
58 * Constructor. Sets local copies of user preferences and initialises grade_tree.
59 * @param int $userid
60 * @param object $gpr grade plugin return tracking object
61 * @param string $context
62 */
63 function grade_report_overview($userid, $gpr, $context) {
64 global $CFG, $COURSE;
65 parent::grade_report($COURSE->id, $gpr, $context);
66
60574063 67 $this->showrank = grade_get_setting($this->courseid, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
68
aa330ebb 69 // get the user (for full name)
70 $this->user = get_record('user', 'id', $userid);
71
72 // base url for sorting by first/last name
73 $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
74 $this->pbarurl = $this->baseurl;
75
76 $this->setup_table();
77 }
78
79 /**
80 * Prepares the headers and attributes of the flexitable.
81 */
82 function setup_table() {
83 /*
60574063 84 * Table has 3 columns
85 *| course | final grade | rank (optional) |
86 */
aa330ebb 87
88 // setting up table headers
60574063 89 if ($this->showrank) {
90 $tablecolumns = array('coursename', 'grade', 'rank');
91 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
92 $this->get_lang_string('grade'),
93 $this->get_lang_string('rank', 'grades'));
94 } else {
95 $tablecolumns = array('coursename', 'grade');
96 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
97 $this->get_lang_string('grade'));
98 }
aa330ebb 99 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
100
101 $this->table->define_columns($tablecolumns);
102 $this->table->define_headers($tableheaders);
103 $this->table->define_baseurl($this->baseurl);
104
105 $this->table->set_attribute('cellspacing', '0');
106 $this->table->set_attribute('id', 'overview-grade');
107 $this->table->set_attribute('class', 'boxaligncenter generaltable');
108
109 $this->table->setup();
110 }
111
112 function fill_table() {
113 global $CFG;
aa330ebb 114
9a10e2d8 115 // MDL-11679, only show 'mycourses' instead of all courses
116 if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname')) {
38cb3391 117 $numusers = $this->get_numusers(false);
60574063 118
aa330ebb 119 foreach ($courses as $course) {
60574063 120 $courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'">'.$course->shortname.'</a>';
121
aa330ebb 122 // Get course grade_item
60574063 123 $grade_item = grade_item::fetch_course_item($course->id);
aa330ebb 124
125 // Get the grade
60574063 126 $grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$this->user->id));
127 $grade->grade_item =& $grade_item;
128 $finalgrade = $grade->finalgrade;
129
130 // TODO: this DOES NOT work properly if there are any hidden grades,
131 // rank might be wrong & totals might be different from user report!!!
132 if ($grade->is_hidden() and !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id))) {
133 $finalgrade = null;
134 }
135
136 $data = array($courselink, grade_format_gradevalue($finalgrade, $grade_item, true));
aa330ebb 137
60574063 138 if (!$this->showrank) {
139 //nothing to do
140
141 } else if (!is_null($finalgrade)) {
aa330ebb 142 /// find the number of users with a higher grade
143 $sql = "SELECT COUNT(DISTINCT(userid))
d24832f9 144 FROM {grade_grades}
60574063 145 WHERE finalgrade IS NOT NULL AND finalgrade > $finalgrade
146 AND itemid = {$grade_item->id}";
aa330ebb 147 $rank = count_records_sql($sql) + 1;
148
60574063 149 $data[] = "$rank/$numusers";
150
aa330ebb 151 } else {
152 // no grade, no rank
60574063 153 $data[] = '-';
aa330ebb 154 }
155
60574063 156 $this->table->add_data($data);
aa330ebb 157 }
aa330ebb 158 return true;
60574063 159
aa330ebb 160 } else {
161 notify(get_string('nocourses', 'grades'));
162 return false;
163 }
164 }
165
166 /**
167 * Prints or returns the HTML from the flexitable.
168 * @param bool $return Whether or not to return the data instead of printing it directly.
169 * @return string
170 */
171 function print_table($return=false) {
172 ob_start();
173 $this->table->print_html();
174 $html = ob_get_clean();
175 if ($return) {
176 return $html;
177 } else {
178 echo $html;
179 }
180 }
181
182 /**
183 * Processes the data sent by the form (grades and feedbacks).
184 * @var array $data
185 * @return bool Success or Failure (array of errors).
186 */
187 function process_data($data) {
188 }
60574063 189}
aa330ebb 190
60574063 191function grade_report_overview_settings_definition(&$mform) {
192 global $CFG;
193
194 $options = array(-1 => get_string('default', 'grades'),
195 0 => get_string('hide'),
196 1 => get_string('show'));
197
198 if (empty($CFG->grade_overviewreport_showrank)) {
199 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
200 } else {
201 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
202 }
203
204 $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
d24832f9 205 $mform->setHelpButton('report_overview_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
aa330ebb 206}
60574063 207
aa330ebb 208?>