navigation MDL-21529 Added a close icon to docked item panels + show/hide event impro...
[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
aa330ebb 50 /**
51 * Constructor. Sets local copies of user preferences and initialises grade_tree.
52 * @param int $userid
53 * @param object $gpr grade plugin return tracking object
54 * @param string $context
55 */
5c75a0a3 56 public function __construct($userid, $gpr, $context) {
57 global $CFG, $COURSE, $DB;
58 parent::__construct($COURSE->id, $gpr, $context);
aa330ebb 59
60574063 60 $this->showrank = grade_get_setting($this->courseid, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
61
aa330ebb 62 // get the user (for full name)
5c75a0a3 63 $this->user = $DB->get_record('user', array('id' => $userid));
aa330ebb 64
65 // base url for sorting by first/last name
66 $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
67 $this->pbarurl = $this->baseurl;
68
69 $this->setup_table();
70 }
71
72 /**
73 * Prepares the headers and attributes of the flexitable.
74 */
5c75a0a3 75 public function setup_table() {
aa330ebb 76 /*
60574063 77 * Table has 3 columns
78 *| course | final grade | rank (optional) |
79 */
aa330ebb 80
81 // setting up table headers
60574063 82 if ($this->showrank) {
83 $tablecolumns = array('coursename', 'grade', 'rank');
84 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
85 $this->get_lang_string('grade'),
86 $this->get_lang_string('rank', 'grades'));
87 } else {
88 $tablecolumns = array('coursename', 'grade');
89 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
90 $this->get_lang_string('grade'));
91 }
aa330ebb 92 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
93
94 $this->table->define_columns($tablecolumns);
95 $this->table->define_headers($tableheaders);
96 $this->table->define_baseurl($this->baseurl);
97
98 $this->table->set_attribute('cellspacing', '0');
99 $this->table->set_attribute('id', 'overview-grade');
100 $this->table->set_attribute('class', 'boxaligncenter generaltable');
101
102 $this->table->setup();
103 }
104
5c75a0a3 105 public function fill_table() {
c3b834b4 106 global $CFG, $DB, $OUTPUT;
aa330ebb 107
9a10e2d8 108 // MDL-11679, only show 'mycourses' instead of all courses
252d14db 109 if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname, showgrades')) {
38cb3391 110 $numusers = $this->get_numusers(false);
60574063 111
aa330ebb 112 foreach ($courses as $course) {
252d14db 113 if (!$course->showgrades) {
114 continue;
115 }
60574063 116 $courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'">'.$course->shortname.'</a>';
00006755 117 $canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id));
60574063 118
aa330ebb 119 // Get course grade_item
00006755 120 $course_item = grade_item::fetch_course_item($course->id);
121
122 // Get the stored grade
123 $course_grade = new grade_grade(array('itemid'=>$course_item->id, 'userid'=>$this->user->id));
124 $course_grade->grade_item =& $course_item;
125 $finalgrade = $course_grade->finalgrade;
126
127 if (!$canviewhidden and !is_null($finalgrade)) {
128 if ($course_grade->is_hidden()) {
129 $finalgrade = null;
130
131 } else {
132 // This is a really ugly hack, it will be fixed in 2.0
133 $items = grade_item::fetch_all(array('courseid'=>$course->id));
134 $grades = array();
135 $sql = "SELECT g.*
136 FROM {grade_grades} g
137 JOIN {grade_items} gi ON gi.id = g.itemid
138 WHERE g.userid = ? AND gi.courseid = ?";
139 if ($gradesrecords = $DB->get_records_sql($sql, array($this->user->id, $course->id))) {
140 foreach ($gradesrecords as $grade) {
141 $grades[$grade->itemid] = new grade_grade($grade, false);
142 }
143 unset($gradesrecords);
144 }
145 foreach ($items as $itemid=>$unused) {
146 if (!isset($grades[$itemid])) {
147 $grade_grade = new grade_grade();
148 $grade_grade->userid = $this->user->id;
149 $grade_grade->itemid = $items[$itemid]->id;
150 $grades[$itemid] = $grade_grade;
151 }
152 $grades[$itemid]->grade_item =& $items[$itemid];
153 }
154 $hiding_affected = grade_grade::get_hiding_affected($grades, $items);
155 if (array_key_exists($course_item->id, $hiding_affected['altered'])) {
156 $finalgrade = $hiding_affected['altered'][$course_item->id];
157
158 } else if (!empty($hiding_affected['unknown'][$course_item->id])) {
159 $finalgrade = null;
160 }
161
162 unset($hiding_affected);
163 unset($grades);
164 unset($items);
165 }
60574063 166 }
167
00006755 168 $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
aa330ebb 169
60574063 170 if (!$this->showrank) {
171 //nothing to do
172
173 } else if (!is_null($finalgrade)) {
aa330ebb 174 /// find the number of users with a higher grade
00006755 175 /// please note this can not work if hidden grades involved :-( to be fixed in 2.0
176 $params = array($finalgrade, $course_item->id);
aa330ebb 177 $sql = "SELECT COUNT(DISTINCT(userid))
d24832f9 178 FROM {grade_grades}
5c75a0a3 179 WHERE finalgrade IS NOT NULL AND finalgrade > ?
180 AND itemid = ?";
181 $rank = $DB->count_records_sql($sql, $params) + 1;
aa330ebb 182
60574063 183 $data[] = "$rank/$numusers";
184
aa330ebb 185 } else {
186 // no grade, no rank
60574063 187 $data[] = '-';
aa330ebb 188 }
189
60574063 190 $this->table->add_data($data);
aa330ebb 191 }
aa330ebb 192 return true;
60574063 193
aa330ebb 194 } else {
c3b834b4 195 echo $OUTPUT->notification(get_string('nocourses', 'grades'));
aa330ebb 196 return false;
197 }
198 }
199
200 /**
201 * Prints or returns the HTML from the flexitable.
202 * @param bool $return Whether or not to return the data instead of printing it directly.
203 * @return string
204 */
5c75a0a3 205 public function print_table($return=false) {
aa330ebb 206 ob_start();
207 $this->table->print_html();
208 $html = ob_get_clean();
209 if ($return) {
210 return $html;
211 } else {
212 echo $html;
213 }
214 }
215
216 /**
217 * Processes the data sent by the form (grades and feedbacks).
218 * @var array $data
219 * @return bool Success or Failure (array of errors).
220 */
653a8648 221 function process_data($data) {
5c75a0a3 222 }
653a8648 223 function process_action($target, $action) {
aa330ebb 224 }
60574063 225}
aa330ebb 226
60574063 227function grade_report_overview_settings_definition(&$mform) {
228 global $CFG;
229
230 $options = array(-1 => get_string('default', 'grades'),
231 0 => get_string('hide'),
232 1 => get_string('show'));
233
234 if (empty($CFG->grade_overviewreport_showrank)) {
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_showrank', get_string('showrank', 'grades'), $options);
d24832f9 241 $mform->setHelpButton('report_overview_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
aa330ebb 242}
60574063 243
6c3ef410 244