MDL-11515:
[moodle-linuxchix.git] / grade / report / user / lib.php
blob191261bb195814efa9170dda72e3ce5e89523ed1
1 <?php // $Id$
2 /**
3 * File in which the user_report class is defined.
4 * @package gradebook
5 */
7 require_once($CFG->dirroot . '/grade/report/lib.php');
8 require_once($CFG->libdir.'/tablelib.php');
10 /**
11 * Class providing an API for the user report building and displaying.
12 * @uses grade_report
13 * @package gradebook
15 class grade_report_user extends grade_report {
17 /**
18 * The user.
19 * @var object $user
21 var $user;
23 /**
24 * A flexitable to hold the data.
25 * @var object $table
27 var $table;
29 /**
30 * Flat structure similar to grade tree
32 var $gseq;
34 /**
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) {
42 global $CFG;
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.'&amp;userid='.$userid;
55 $this->pbarurl = $this->baseurl;
57 // always setup groups - no user preference here
58 $this->setup_groups();
60 $this->setup_table();
63 /**
64 * Prepares the headers and attributes of the flexitable.
66 function setup_table() {
68 * Table has 6 columns
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'
96 ));
98 $this->table->setup();
101 function fill_table() {
102 global $CFG;
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();
108 $data = array();
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>';
118 } else {
119 $data[] = $this->get_module_link($grade_item->get_name(), $grade_item->itemmodule, $grade_item->iteminstance);;
122 /// prints category
123 $cat = $grade_item->get_parent_category();
124 $data[] = $cat->fullname;
127 /// prints the grade
128 $displaytype = $grade_item->get_displaytype();
130 if ($grade_grade->is_excluded()) {
131 $excluded = get_string('excluded', 'grades').' ';
132 } else {
133 $excluded = '';
136 if ((int) $grade_grade->finalgrade < 1) {
137 $data[] = '-';
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];
146 } else {
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) {
154 $data[] = '-';
155 } else {
156 $data[] = get_string('gradedon', 'grades', userdate($grade_grade->timemodified));
158 } else {
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).'%';
163 } else {
164 $percentage = '-';
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).'%';
173 } else {
174 // text grade
175 $percentage = '-';
178 $data[] = $percentage;
180 /// prints rank
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";
190 } else {
191 // no grade, no rank
192 $data[] = "-";
195 /// prints notes
196 if (!empty($grade_grade->feedback)) {
197 $data[] = format_text($grade_grade->feedback, $grade_grade->feedbackformat);
198 } else {
199 $data[] = '&nbsp;';
201 $this->table->add_data($data);
204 return true;
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.
210 * @return string
212 function print_table($return=false) {
213 ob_start();
214 $this->table->print_html();
215 $html = ob_get_clean();
216 if ($return) {
217 return $html;
218 } else {
219 echo $html;
224 * Processes the data sent by the form (grades and feedbacks).
225 * @var array $data
226 * @return bool Success or Failure (array of errors).
228 function process_data($data) {