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 object $gpr grade plugin return tracking object
33 * @param string $context
34 * @param int $userid The id of the user
36 function grade_report_user($courseid, $gpr, $context, $userid) {
38 parent
::grade_report($courseid, $gpr, $context);
40 // Grab the grade_tree for this course
41 $this->gtree
= new grade_tree($this->courseid
, true, $this->get_pref('aggregationposition'));
43 // get the user (for full name)
44 $this->user
= get_record('user', 'id', $userid);
46 // base url for sorting by first/last name
47 $this->baseurl
= $CFG->wwwroot
.'/grade/report?id='.$courseid.'&userid='.$userid;
48 $this->pbarurl
= $this->baseurl
;
50 // Setup groups if requested
51 if ($this->get_pref('showgroups')) {
52 $this->setup_groups();
59 * Prepares the headers and attributes of the flexitable.
61 function setup_table() {
64 *| pic | itemname/description | grade (grade_final) | percentage | rank | feedback |
67 // setting up table headers
68 $tablecolumns = array('itempic', 'itemname', 'grade', 'percentage', 'rank', 'feedback');
69 $tableheaders = array('', $this->get_lang_string('gradeitem', 'grades'), $this->get_lang_string('grade'),
70 $this->get_lang_string('percent', 'grades'), $this->get_lang_string('rank', 'grades'),
71 $this->get_lang_string('feedback'));
73 $this->table
= new flexible_table('grade-report-user-'.$this->courseid
);
75 $this->table
->define_columns($tablecolumns);
76 $this->table
->define_headers($tableheaders);
77 $this->table
->define_baseurl($this->baseurl
);
79 $this->table
->set_attribute('cellspacing', '0');
80 $this->table
->set_attribute('id', 'user-grade');
81 $this->table
->set_attribute('class', 'boxaligncenter generaltable');
83 // not sure tables should be sortable or not, because if we allow it then sorted resutls distort grade category structure and sortorder
84 $this->table
->set_control_variables(array(
85 TABLE_VAR_SORT
=> 'ssort',
86 TABLE_VAR_HIDE
=> 'shide',
87 TABLE_VAR_SHOW
=> 'sshow',
88 TABLE_VAR_IFIRST
=> 'sifirst',
89 TABLE_VAR_ILAST
=> 'silast',
90 TABLE_VAR_PAGE
=> 'spage'
93 $this->table
->setup();
96 function fill_table() {
98 $numusers = $this->get_numusers();
100 if ($all_grade_items = grade_item
::fetch_all(array('courseid'=>$this->courseid
))) {
101 $grade_items = array();
102 foreach ($all_grade_items as $item) {
103 $grade_items[$item->sortorder
] = $item;
105 unset($all_grade_items);
108 $total = $grade_items[1];
109 unset($grade_items[1]);
110 $grade_items[] = $total;
112 foreach ($grade_items as $grade_item) {
116 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id
, 'userid'=>$this->user
->id
));
117 $grade_text = $grade_grade->load_text();
119 /// prints mod icon if available
120 if ($grade_item->itemtype
== 'mod') {
121 $iconpath = $CFG->dirroot
.'/mod/'.$grade_item->itemmodule
.'/icon.gif';
122 $icon = $CFG->wwwroot
.'/mod/'.$grade_item->itemmodule
.'/icon.gif';
123 if (file_exists($iconpath)) {
124 $data[] = '<img src = "'.$icon.'" alt="'.$grade_item->itemname
.'" class="activityicon"/>';
130 // TODO: indicate items that "needsupdate" - missing final calculation
132 /// prints grade item name
133 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
134 $data[] = '<b>'.$grade_item->get_name().'</b>';
136 $data[] = $grade_item->get_name();
141 if ($grade_grade->is_excluded()) {
142 $excluded = get_tring('excluded', 'grades').' ';
147 if ($grade_item->scaleid
) {
149 if ($scale = get_record('scale', 'id', $grade_item->scaleid
)) {
150 $scales = explode(",", $scale->scale
);
151 // reindex because scale is off 1
152 // invalid grade if gradeval < 1
153 if ((int) $grade_grade->finalgrade
< 1) {
154 $data[] = $excluded.'-';
156 $data[] = $excluded.$scales[$grade_grade->finalgrade
-1];
160 // normal grade, or text, just display
161 $data[] = $excluded.$this->get_grade_clean($grade_grade->finalgrade
);
164 /// prints percentage
166 if ($grade_item->gradetype
== GRADE_TYPE_VALUE
) {
167 // processing numeric grade
168 if ($grade_grade->finalgrade
) {
169 $percentage = $this->get_grade_clean(($grade_grade->finalgrade
/ $grade_item->grademax
) * 100).'%';
174 } else if ($grade_item->gradetype
== GRADE_TYPE_SCALE
) {
175 // processing scale grade
176 $scale = get_record('scale', 'id', $grade_item->scaleid
);
177 $scalevals = explode(",", $scale->scale
);
178 $percentage = $this->get_grade_clean(($grade_grade->finalgrade
) / count($scalevals) * 100).'%';
185 $data[] = $percentage;
188 if ($grade_grade->finalgrade
) {
189 /// find the number of users with a higher grade
190 $sql = "SELECT COUNT(DISTINCT(userid))
191 FROM {$CFG->prefix}grade_grades
192 WHERE finalgrade > $grade_grade->finalgrade
193 AND itemid = $grade_item->id";
194 $rank = count_records_sql($sql) +
1;
196 $data[] = "$rank/$numusers";
203 if (!empty($grade_text->feedback
)) {
204 $data[] = $grade_text->feedback
;
208 $this->table
->add_data($data);
213 notify(get_string('nogradeitem', 'grades'));
219 * Prints or returns the HTML from the flexitable.
220 * @param bool $return Whether or not to return the data instead of printing it directly.
223 function print_table($return=false) {
225 $this->table
->print_html();
226 $html = ob_get_clean();
235 * Processes the data sent by the form (grades and feedbacks).
237 * @return bool Success or Failure (array of errors).
239 function process_data($data) {