weekly release 2.5dev
[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
41 /**
42 * A flexitable to hold the data.
43 * @var object $table
44 */
5c75a0a3 45 public $table;
aa330ebb 46
60574063 47 /**
48 * show student ranks
49 */
5c75a0a3 50 public $showrank;
60574063 51
61541a5a
AD
52 /**
53 * show course/category totals if they contain hidden items
54 */
55 var $showtotalsifcontainhidden;
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 */
5c75a0a3 63 public function __construct($userid, $gpr, $context) {
64 global $CFG, $COURSE, $DB;
65 parent::__construct($COURSE->id, $gpr, $context);
aa330ebb 66
60574063 67 $this->showrank = grade_get_setting($this->courseid, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
0bc89073 68 $this->showtotalsifcontainhidden = grade_get_setting($this->courseid, 'report_overview_showtotalsifcontainhidden', $CFG->grade_report_overview_showtotalsifcontainhidden);
60574063 69
aa330ebb 70 // get the user (for full name)
5c75a0a3 71 $this->user = $DB->get_record('user', array('id' => $userid));
aa330ebb 72
73 // base url for sorting by first/last name
74 $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
75 $this->pbarurl = $this->baseurl;
76
77 $this->setup_table();
78 }
79
80 /**
81 * Prepares the headers and attributes of the flexitable.
82 */
5c75a0a3 83 public function setup_table() {
aa330ebb 84 /*
60574063 85 * Table has 3 columns
86 *| course | final grade | rank (optional) |
87 */
aa330ebb 88
89 // setting up table headers
60574063 90 if ($this->showrank) {
91 $tablecolumns = array('coursename', 'grade', 'rank');
92 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
93 $this->get_lang_string('grade'),
94 $this->get_lang_string('rank', 'grades'));
95 } else {
96 $tablecolumns = array('coursename', 'grade');
97 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
98 $this->get_lang_string('grade'));
99 }
aa330ebb 100 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
101
102 $this->table->define_columns($tablecolumns);
103 $this->table->define_headers($tableheaders);
104 $this->table->define_baseurl($this->baseurl);
105
106 $this->table->set_attribute('cellspacing', '0');
107 $this->table->set_attribute('id', 'overview-grade');
108 $this->table->set_attribute('class', 'boxaligncenter generaltable');
109
110 $this->table->setup();
111 }
112
5c75a0a3 113 public function fill_table() {
c3b834b4 114 global $CFG, $DB, $OUTPUT;
aa330ebb 115
df997f84
PS
116 // MDL-11679, only show user's courses instead of all courses
117 if ($courses = enrol_get_users_courses($this->user->id, false, 'id, shortname, showgrades')) {
38cb3391 118 $numusers = $this->get_numusers(false);
60574063 119
aa330ebb 120 foreach ($courses as $course) {
252d14db 121 if (!$course->showgrades) {
122 continue;
123 }
31eae0eb 124
d4060472 125 $coursecontext = context_course::instance($course->id);
31eae0eb
AD
126
127 if (!$course->visible && !has_capability('moodle/course:viewhiddencourses', $coursecontext)) {
128 // The course is hidden and the user isn't allowed to see it
129 continue;
130 }
131
8ebbb06a
SH
132 $courseshortname = format_string($course->shortname, true, array('context' => $coursecontext));
133 $courselink = html_writer::link(new moodle_url('/grade/report/user/index.php', array('id' => $course->id, 'userid' => $this->user->id)), $courseshortname);
134 $canviewhidden = has_capability('moodle/grade:viewhidden', $coursecontext);
60574063 135
aa330ebb 136 // Get course grade_item
00006755 137 $course_item = grade_item::fetch_course_item($course->id);
138
139 // Get the stored grade
140 $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
141 $course_grade->grade_item =& $course_item;
142 $finalgrade = $course_grade->finalgrade;
143
144 if (!$canviewhidden and !is_null($finalgrade)) {
145 if ($course_grade->is_hidden()) {
146 $finalgrade = null;
00006755 147 } else {
61541a5a 148 $finalgrade = $this->blank_hidden_total($course->id, $course_item, $finalgrade);
00006755 149 }
60574063 150 }
151
00006755 152 $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
aa330ebb 153
60574063 154 if (!$this->showrank) {
155 //nothing to do
156
157 } else if (!is_null($finalgrade)) {
aa330ebb 158 /// find the number of users with a higher grade
00006755 159 /// please note this can not work if hidden grades involved :-( to be fixed in 2.0
160 $params = array($finalgrade, $course_item->id);
aa330ebb 161 $sql = "SELECT COUNT(DISTINCT(userid))
d24832f9 162 FROM {grade_grades}
5c75a0a3 163 WHERE finalgrade IS NOT NULL AND finalgrade > ?
164 AND itemid = ?";
165 $rank = $DB->count_records_sql($sql, $params) + 1;
aa330ebb 166
60574063 167 $data[] = "$rank/$numusers";
168
aa330ebb 169 } else {
170 // no grade, no rank
60574063 171 $data[] = '-';
aa330ebb 172 }
173
60574063 174 $this->table->add_data($data);
aa330ebb 175 }
aa330ebb 176 return true;
60574063 177
aa330ebb 178 } else {
c3b834b4 179 echo $OUTPUT->notification(get_string('nocourses', 'grades'));
aa330ebb 180 return false;
181 }
182 }
183
184 /**
185 * Prints or returns the HTML from the flexitable.
186 * @param bool $return Whether or not to return the data instead of printing it directly.
187 * @return string
188 */
5c75a0a3 189 public function print_table($return=false) {
aa330ebb 190 ob_start();
191 $this->table->print_html();
192 $html = ob_get_clean();
193 if ($return) {
194 return $html;
195 } else {
196 echo $html;
197 }
198 }
199
200 /**
201 * Processes the data sent by the form (grades and feedbacks).
202 * @var array $data
203 * @return bool Success or Failure (array of errors).
204 */
653a8648 205 function process_data($data) {
5c75a0a3 206 }
653a8648 207 function process_action($target, $action) {
aa330ebb 208 }
60574063 209}
aa330ebb 210
60574063 211function grade_report_overview_settings_definition(&$mform) {
212 global $CFG;
213
61541a5a 214 //show rank
60574063 215 $options = array(-1 => get_string('default', 'grades'),
216 0 => get_string('hide'),
217 1 => get_string('show'));
218
219 if (empty($CFG->grade_overviewreport_showrank)) {
220 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
221 } else {
222 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
223 }
224
225 $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
bf03b43f 226 $mform->addHelpButton('report_overview_showrank', 'showrank', 'grades');
61541a5a
AD
227
228 //showtotalsifcontainhidden
229 $options = array(-1 => get_string('default', 'grades'),
230 GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
231 GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
232 GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades') );
233
234 if (empty($CFG->grade_report_overview_showtotalsifcontainhidden)) {
235 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
236 } else {
237 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
238 }
239
240 $mform->addElement('select', 'report_overview_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
0459cc4d 241 $mform->addHelpButton('report_overview_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
aa330ebb 242}
60574063 243
6c3ef410 244