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