MDL-15680 merged in all the functionality of the Edit categories and items page
[moodle.git] / grade / report / overview / lib.php
CommitLineData
aa330ebb 1<?php // $Id$
8ad36f4c 2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
aa330ebb 25/**
26 * File in which the overview_report class is defined.
27 * @package gradebook
28 */
29
30require_once($CFG->dirroot . '/grade/report/lib.php');
31require_once($CFG->libdir.'/tablelib.php');
32
33/**
34 * Class providing an API for the overview report building and displaying.
35 * @uses grade_report
36 * @package gradebook
37 */
38class grade_report_overview extends grade_report {
39
40 /**
41 * The user.
42 * @var object $user
43 */
5c75a0a3 44 public $user;
aa330ebb 45
46 /**
47 * A flexitable to hold the data.
48 * @var object $table
49 */
5c75a0a3 50 public $table;
aa330ebb 51
60574063 52 /**
53 * show student ranks
54 */
5c75a0a3 55 public $showrank;
60574063 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));
68
aa330ebb 69 // get the user (for full name)
5c75a0a3 70 $this->user = $DB->get_record('user', array('id' => $userid));
aa330ebb 71
72 // base url for sorting by first/last name
73 $this->baseurl = $CFG->wwwroot.'/grade/overview/index.php?id='.$userid;
74 $this->pbarurl = $this->baseurl;
75
76 $this->setup_table();
77 }
78
79 /**
80 * Prepares the headers and attributes of the flexitable.
81 */
5c75a0a3 82 public function setup_table() {
aa330ebb 83 /*
60574063 84 * Table has 3 columns
85 *| course | final grade | rank (optional) |
86 */
aa330ebb 87
88 // setting up table headers
60574063 89 if ($this->showrank) {
90 $tablecolumns = array('coursename', 'grade', 'rank');
91 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
92 $this->get_lang_string('grade'),
93 $this->get_lang_string('rank', 'grades'));
94 } else {
95 $tablecolumns = array('coursename', 'grade');
96 $tableheaders = array($this->get_lang_string('coursename', 'grades'),
97 $this->get_lang_string('grade'));
98 }
aa330ebb 99 $this->table = new flexible_table('grade-report-overview-'.$this->user->id);
100
101 $this->table->define_columns($tablecolumns);
102 $this->table->define_headers($tableheaders);
103 $this->table->define_baseurl($this->baseurl);
104
105 $this->table->set_attribute('cellspacing', '0');
106 $this->table->set_attribute('id', 'overview-grade');
107 $this->table->set_attribute('class', 'boxaligncenter generaltable');
108
109 $this->table->setup();
110 }
111
5c75a0a3 112 public function fill_table() {
aa330ebb 113 global $CFG;
aa330ebb 114
9a10e2d8 115 // MDL-11679, only show 'mycourses' instead of all courses
252d14db 116 if ($courses = get_my_courses($this->user->id, 'c.sortorder ASC', 'id, shortname, showgrades')) {
38cb3391 117 $numusers = $this->get_numusers(false);
60574063 118
aa330ebb 119 foreach ($courses as $course) {
252d14db 120 if (!$course->showgrades) {
121 continue;
122 }
60574063 123 $courselink = '<a href="'.$CFG->wwwroot.'/grade/report/user/index.php?id='.$course->id.'">'.$course->shortname.'</a>';
124
aa330ebb 125 // Get course grade_item
60574063 126 $grade_item = grade_item::fetch_course_item($course->id);
aa330ebb 127
128 // Get the grade
60574063 129 $grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$this->user->id));
130 $grade->grade_item =& $grade_item;
131 $finalgrade = $grade->finalgrade;
132
133 // TODO: this DOES NOT work properly if there are any hidden grades,
134 // rank might be wrong & totals might be different from user report!!!
135 if ($grade->is_hidden() and !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE, $course->id))) {
136 $finalgrade = null;
137 }
138
139 $data = array($courselink, grade_format_gradevalue($finalgrade, $grade_item, true));
aa330ebb 140
60574063 141 if (!$this->showrank) {
142 //nothing to do
143
144 } else if (!is_null($finalgrade)) {
aa330ebb 145 /// find the number of users with a higher grade
5c75a0a3 146 $params = array($finalgrade, $grade_item->id);
aa330ebb 147 $sql = "SELECT COUNT(DISTINCT(userid))
d24832f9 148 FROM {grade_grades}
5c75a0a3 149 WHERE finalgrade IS NOT NULL AND finalgrade > ?
150 AND itemid = ?";
151 $rank = $DB->count_records_sql($sql, $params) + 1;
aa330ebb 152
60574063 153 $data[] = "$rank/$numusers";
154
aa330ebb 155 } else {
156 // no grade, no rank
60574063 157 $data[] = '-';
aa330ebb 158 }
159
60574063 160 $this->table->add_data($data);
aa330ebb 161 }
aa330ebb 162 return true;
60574063 163
aa330ebb 164 } else {
165 notify(get_string('nocourses', 'grades'));
166 return false;
167 }
168 }
169
170 /**
171 * Prints or returns the HTML from the flexitable.
172 * @param bool $return Whether or not to return the data instead of printing it directly.
173 * @return string
174 */
5c75a0a3 175 public function print_table($return=false) {
aa330ebb 176 ob_start();
177 $this->table->print_html();
178 $html = ob_get_clean();
179 if ($return) {
180 return $html;
181 } else {
182 echo $html;
183 }
184 }
185
186 /**
187 * Processes the data sent by the form (grades and feedbacks).
188 * @var array $data
189 * @return bool Success or Failure (array of errors).
190 */
5c75a0a3 191 public function process_data($data) {
192 }
193
194 public function process_action($target, $action) {
aa330ebb 195 }
60574063 196}
aa330ebb 197
60574063 198function grade_report_overview_settings_definition(&$mform) {
199 global $CFG;
200
201 $options = array(-1 => get_string('default', 'grades'),
202 0 => get_string('hide'),
203 1 => get_string('show'));
204
205 if (empty($CFG->grade_overviewreport_showrank)) {
206 $options[-1] = get_string('defaultprev', 'grades', $options[0]);
207 } else {
208 $options[-1] = get_string('defaultprev', 'grades', $options[1]);
209 }
210
211 $mform->addElement('select', 'report_overview_showrank', get_string('showrank', 'grades'), $options);
d24832f9 212 $mform->setHelpButton('report_overview_showrank', array('showrank', get_string('showrank', 'grades'), 'grade'));
aa330ebb 213}
60574063 214
aa330ebb 215?>