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