3 * File in which the user_report class is defined.
7 require_once($CFG->dirroot
. '/grade/report/lib.php');
8 require_once($CFG->libdir
.'/tablelib.php');
11 * Class providing an API for the user report building and displaying.
15 class grade_report_user
extends grade_report
{
24 * A flexitable to hold the data.
30 * Constructor. Sets local copies of user preferences and initialises grade_tree.
31 * @param int $courseid
32 * @param string $context
33 * @param int $userid The id of the user
35 function grade_report_user($courseid, $context, $userid) {
37 parent
::grade_report($courseid, $context);
39 // get the user (for full name)
40 $this->user
= get_record('user', 'id', $userid);
42 // base url for sorting by first/last name
43 $this->baseurl
= $CFG->wwwroot
.'/grade/report?id='.$courseid.'&userid='.$userid;
50 * Prepares the headers and attributes of the flexitable.
52 function setup_table() {
55 *| pic | itemname/description | grade (grade_final) | percentage | rank | feedback |
58 // setting up table headers
59 $tablecolumns = array('itempic', 'itemname', 'grade', 'percentage', 'rank', 'feedback');
60 $tableheaders = array('', $this->get_lang_string('gradeitem', 'grades'), $this->get_lang_string('grade'),
61 $this->get_lang_string('percent', 'grades'), $this->get_lang_string('rank', 'grades'),
62 $this->get_lang_string('feedback'));
64 $this->table
= new flexible_table('grade-report-user-'.$this->courseid
);
66 $this->table
->define_columns($tablecolumns);
67 $this->table
->define_headers($tableheaders);
68 $this->table
->define_baseurl($this->baseurl
);
70 $this->table
->set_attribute('cellspacing', '0');
71 $this->table
->set_attribute('id', 'user-grade');
72 $this->table
->set_attribute('class', 'boxaligncenter generaltable');
74 // not sure tables should be sortable or not, because if we allow it then sorted resutls distort grade category structure and sortorder
75 $this->table
->set_control_variables(array(
76 TABLE_VAR_SORT
=> 'ssort',
77 TABLE_VAR_HIDE
=> 'shide',
78 TABLE_VAR_SHOW
=> 'sshow',
79 TABLE_VAR_IFIRST
=> 'sifirst',
80 TABLE_VAR_ILAST
=> 'silast',
81 TABLE_VAR_PAGE
=> 'spage'
84 $this->table
->setup();
87 function fill_table() {
90 if ($all_grade_items = grade_item
::fetch_all(array('courseid'=>$this->courseid
))) {
91 $grade_items = array();
92 foreach ($all_grade_items as $item) {
93 $grade_items[$item->sortorder
] = $item;
95 unset($all_grade_items);
98 $total = $grade_items[1];
99 unset($grade_items[1]);
100 $grade_items[] = $total;
102 foreach ($grade_items as $grade_item) {
106 $params->itemid
= $grade_item->id
;
107 $params->userid
= $this->user
->id
;
108 $grade_grades = new grade_grades($params);
109 $grade_text = $grade_grades->load_text();
111 /// prints mod icon if available
112 if ($grade_item->itemtype
== 'mod') {
113 $iconpath = $CFG->dirroot
.'/mod/'.$grade_item->itemmodule
.'/icon.gif';
114 $icon = $CFG->wwwroot
.'/mod/'.$grade_item->itemmodule
.'/icon.gif';
115 if (file_exists($iconpath)) {
116 $data[] = '<img src = "'.$icon.'" alt="'.$grade_item->itemname
.'" class="activityicon"/>';
122 // TODO: indicate items that "needsupdate" - missing final calculation
124 /// prints grade item name
125 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
126 $data[] = '<b>'.$grade_item->get_name().'</b>';
128 $data[] = $grade_item->get_name();
133 if ($grade_item->scaleid
) {
135 if ($scale = get_record('scale', 'id', $grade_item->scaleid
)) {
136 $scales = explode(",", $scale->scale
);
137 // reindex because scale is off 1
138 // invalid grade if gradeval < 1
139 if ((int) $grade_grades->finalgrade
< 1) {
142 $data[] = $scales[$grade_grades->finalgrade
-1];
146 // normal grade, or text, just display
147 $data[] = $this->get_grade_clean($grade_grades->finalgrade
);
150 /// prints percentage
152 if ($grade_item->gradetype
== GRADE_TYPE_VALUE
) {
153 // processing numeric grade
154 if ($grade_grades->finalgrade
) {
155 $percentage = $this->get_grade_clean(($grade_grades->finalgrade
/ $grade_item->grademax
) * 100).'%';
160 } else if ($grade_item->gradetype
== GRADE_TYPE_SCALE
) {
161 // processing scale grade
162 $scale = get_record('scale', 'id', $grade_item->scaleid
);
163 $scalevals = explode(",", $scale->scale
);
164 $percentage = $this->get_grade_clean(($grade_grades->finalgrade
) / count($scalevals) * 100).'%';
171 $data[] = $percentage;
174 if ($grade_grades->finalgrade
) {
175 /// find the number of users with a higher grade
176 $sql = "SELECT COUNT(DISTINCT(userid))
177 FROM {$CFG->prefix}grade_grades
178 WHERE finalgrade > $grade_grades->finalgrade
179 AND itemid = $grade_item->id";
180 $rank = count_records_sql($sql) +
1;
182 $data[] = "$rank/$numusers";
189 if (!empty($grade_text->feedback
)) {
190 $data[] = $grade_text->feedback
;
194 $this->table
->add_data($data);
199 notify(get_string('nogradeitem', 'grades'));
205 * Prints or returns the HTML from the flexitable.
206 * @param bool $return Whether or not to return the data instead of printing it directly.
209 function print_table($return=false) {
211 $this->table
->print_html();
212 $html = ob_get_clean();
221 * Processes the data sent by the form (grades and feedbacks).
223 * @return bool Success or Failure (array of errors).
225 function process_data($data) {
229 * Fetches and returns a count of all the users that will be shows on this page.
230 * @return int Count of users
232 function get_numusers() {
234 return count(get_role_users(@implode
(',', $CFG->gradebookroles
), $this->context
));