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 * Flat structure similar to grade tree
35 * Constructor. Sets local copies of user preferences and initialises grade_tree.
36 * @param int $courseid
37 * @param object $gpr grade plugin return tracking object
38 * @param string $context
39 * @param int $userid The id of the user
41 function grade_report_user($courseid, $gpr, $context, $userid) {
43 parent
::grade_report($courseid, $gpr, $context);
45 $switch = grade_get_setting($this->courseid
, 'aggregationposition', $CFG->grade_aggregationposition
);
47 // Grab the grade_tree for this course
48 $this->gseq
= new grade_seq($this->courseid
, $switch);
50 // get the user (for full name)
51 $this->user
= get_record('user', 'id', $userid);
53 // base url for sorting by first/last name
54 $this->baseurl
= $CFG->wwwroot
.'/grade/report?id='.$courseid.'&userid='.$userid;
55 $this->pbarurl
= $this->baseurl
;
57 // always setup groups - no user preference here
58 $this->setup_groups();
64 * Prepares the headers and attributes of the flexitable.
66 function setup_table() {
69 *| pic | itemname/description | grade (grade_final) | percentage | rank | feedback |
72 // setting up table headers
73 $tablecolumns = array('itemname', 'category', 'grade', 'percentage', 'rank', 'feedback');
74 $tableheaders = array($this->get_lang_string('gradeitem', 'grades'), $this->get_lang_string('category'), $this->get_lang_string('grade'),
75 $this->get_lang_string('percent', 'grades'), $this->get_lang_string('rank', 'grades'),
76 $this->get_lang_string('feedback'));
78 $this->table
= new flexible_table('grade-report-user-'.$this->courseid
);
80 $this->table
->define_columns($tablecolumns);
81 $this->table
->define_headers($tableheaders);
82 $this->table
->define_baseurl($this->baseurl
);
84 $this->table
->set_attribute('cellspacing', '0');
85 $this->table
->set_attribute('id', 'user-grade');
86 $this->table
->set_attribute('class', 'boxaligncenter generaltable');
88 // not sure tables should be sortable or not, because if we allow it then sorted resutls distort grade category structure and sortorder
89 $this->table
->set_control_variables(array(
90 TABLE_VAR_SORT
=> 'ssort',
91 TABLE_VAR_HIDE
=> 'shide',
92 TABLE_VAR_SHOW
=> 'sshow',
93 TABLE_VAR_IFIRST
=> 'sifirst',
94 TABLE_VAR_ILAST
=> 'silast',
95 TABLE_VAR_PAGE
=> 'spage'
98 $this->table
->setup();
101 function fill_table() {
103 $numusers = $this->get_numusers(false); // total course users
105 foreach ($this->gseq
->items
as $element) {
106 $grade_item = $element['object'];
107 $decimalpoints = $grade_item->get_decimals();
110 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id
, 'userid'=>$this->user
->id
));
111 $grade_grade->grade_item
=& $grade_item;
113 // TODO: indicate items that "needsupdate" - missing final calculation
115 /// prints grade item name
116 if ($grade_item->is_course_item() or $grade_item->is_category_item()) {
117 $data[] = '<b>'.$grade_item->get_name().'</b>';
119 $data[] = $this->get_module_link($grade_item->get_name(), $grade_item->itemmodule
, $grade_item->iteminstance
);;
123 $cat = $grade_item->get_parent_category();
124 $data[] = $cat->fullname
;
128 $displaytype = $grade_item->get_displaytype();
130 if ($grade_grade->is_excluded()) {
131 $excluded = get_string('excluded', 'grades').' ';
136 if ((int) $grade_grade->finalgrade
< 1) {
138 } elseif ($grade_grade->is_hidden() && !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE
, $grade_item->courseid
))) {
139 $data[] = get_string('gradedon', 'grades', userdate($grade_grade->timemodified
));
140 } elseif ($grade_item->scaleid
) {
141 if ($scale = get_record('scale', 'id', $grade_item->scaleid
)) {
142 $scales = explode(",", $scale->scale
);
143 // reindex because scale is off 1
144 $data[] = $excluded.$scales[$grade_grade->finalgrade
-1];
147 $data[] = $excluded . grade_format_gradevalue($grade_grade->finalgrade
, $grade_item, true, $displaytype, $decimalpoints);
150 /// prints percentage
152 if ($grade_grade->is_hidden() && !has_capability('moodle/grade:viewhidden', get_context_instance(CONTEXT_COURSE
, $grade_item->courseid
))) {
153 if ((int) $grade_grade->finalgrade
< 1) {
156 $data[] = get_string('gradedon', 'grades', userdate($grade_grade->timemodified
));
159 if ($grade_item->gradetype
== GRADE_TYPE_VALUE
) {
160 // processing numeric grade
161 if ($grade_grade->finalgrade
) {
162 $percentage = format_float(($grade_grade->finalgrade
/ $grade_item->grademax
) * 100, $decimalpoints).'%';
167 } else if ($grade_item->gradetype
== GRADE_TYPE_SCALE
) {
168 // processing scale grade
169 $scale = get_record('scale', 'id', $grade_item->scaleid
);
170 $scalevals = explode(",", $scale->scale
);
171 $percentage = format_float(($grade_grade->finalgrade
) / count($scalevals) * 100, $decimalpoints).'%';
178 $data[] = $percentage;
181 if ($grade_grade->finalgrade
) {
182 /// find the number of users with a higher grade
183 $sql = "SELECT COUNT(DISTINCT(userid))
184 FROM {$CFG->prefix}grade_grades
185 WHERE finalgrade > $grade_grade->finalgrade
186 AND itemid = $grade_item->id";
187 $rank = count_records_sql($sql) +
1;
189 $data[] = "$rank/$numusers";
196 if (!empty($grade_grade->feedback
)) {
197 $data[] = format_text($grade_grade->feedback
, $grade_grade->feedbackformat
);
201 $this->table
->add_data($data);
208 * Prints or returns the HTML from the flexitable.
209 * @param bool $return Whether or not to return the data instead of printing it directly.
212 function print_table($return=false) {
214 $this->table
->print_html();
215 $html = ob_get_clean();
224 * Processes the data sent by the form (grades and feedbacks).
226 * @return bool Success or Failure (array of errors).
228 function process_data($data) {