3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * File in which the overview_report class is defined.
23 require_once($CFG->dirroot . '/grade/report/lib.php');
24 require_once($CFG->libdir.'/tablelib.php');
27 * Class providing an API for the overview report building and displaying.
31 class grade_report_overview extends grade_report {
40 * A flexitable to hold the data.
51 * show course/category totals if they contain hidden items
53 var $showtotalsifcontainhidden;
56 * Constructor. Sets local copies of user preferences and initialises grade_tree.
58 * @param object $gpr grade plugin return tracking object
59 * @param string $context
61 public function __construct($userid, $gpr, $context) {
62 global $CFG, $COURSE, $DB;
63 parent::__construct($COURSE->id, $gpr, $context);
65 $this->showrank = grade_get_setting($this->courseid, 'report_overview_showrank', !empty($CFG->grade_report_overview_showrank));
66 $this->showtotalsifcontainhidden = grade_get_setting($this->courseid, 'report_overview_showtotalsifcontainhidden', $CFG->grade_report_overview_showtotalsifcontainhidden);
68 // get the user (for full name)
69 $this->user = $DB->get_record('user', array('id' => $userid));
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;
79 * Prepares the headers and attributes of the flexitable.
81 public function setup_table() {
84 *| course | final grade | rank (optional) |
87 // setting up table headers
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'));
94 $tablecolumns = array('coursename', 'grade');
95 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
96 $this->get_lang_string('grade'));
98 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
100 $this->table->define_columns($tablecolumns);
101 $this->table->define_headers($tableheaders);
102 $this->table->define_baseurl($this->baseurl);
104 $this->table->set_attribute('cellspacing', '0');
105 $this->table->set_attribute('id', 'overview-grade');
106 $this->table->set_attribute('class', 'boxaligncenter generaltable');
108 $this->table->setup();
111 public function fill_table() {
112 global $CFG, $DB, $OUTPUT;
114 // MDL-11679, only show 'mycourses' instead of all courses
115 if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname, showgrades')) {
116 $numusers = $this->get_numusers(false);
118 foreach ($courses as $course) {
119 if (!$course->showgrades) {
122 $courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'&userid='.$this->user->id.'">'.$course->shortname.'</a>';
123 $canviewhidden = has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id));
125 // Get course grade_item
126 $course_item = grade_item::fetch_course_item($course->id);
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;
133 if (!$canviewhidden and !is_null($finalgrade)) {
134 if ($course_grade->is_hidden()) {
137 $finalgrade = $this->blank_hidden_total($course->id, $course_item, $finalgrade);
141 $data = array($courselink, grade_format_gradevalue($finalgrade, $course_item, true));
143 if (!$this->showrank) {
146 } else if (!is_null($finalgrade)) {
147 /// find the number of users with a higher grade
148 /// please note this can not work if hidden grades involved :-( to be fixed in 2.0
149 $params = array($finalgrade, $course_item->id);
150 $sql = "SELECT COUNT(DISTINCT(userid))
152 WHERE finalgrade IS NOT NULL AND finalgrade > ?
154 $rank = $DB->count_records_sql($sql, $params) + 1;
156 $data[] = "$rank/$numusers";
163 $this->table->add_data($data);
168 echo $OUTPUT->notification(get_string('nocourses', 'grades'));
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.
178 public function print_table($return=false) {
180 $this->table->print_html();
181 $html = ob_get_clean();
190 * Processes the data sent by the form (grades and feedbacks).
192 * @return bool Success or Failure (array of errors).
194 function process_data($data) {
196 function process_action($target, $action) {
200 function grade_report_overview_settings_definition(&$mform) {
204 $options = array(-1 => get_string('default', 'grades'),
205 0 => get_string('hide'),
206 1 => get_string('show'));
208 if (empty($CFG->grade_overviewreport_showrank)) {
209 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
211 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
214 $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
215 $mform->setHelpButton('report_overview_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
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') );
223 if (empty($CFG->grade_report_overview_showtotalsifcontainhidden)) {
224 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
226 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
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'));