MDL-10210 Removed the loading of the $CFG versions of the preferences when the user...
[moodle-linuxchix.git] / grade / report / user / lib.php
blob88d7a2600ed7265f1a4b73c6e602ec812482bf3a
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 * 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) {
37 global $CFG;
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.'&amp;userid='.$userid;
48 $this->pbarurl = $this->baseurl;
50 // Setup groups if requested
51 if ($this->get_pref('showgroups')) {
52 $this->setup_groups();
55 $this->setup_table();
58 /**
59 * Prepares the headers and attributes of the flexitable.
61 function setup_table() {
63 * Table has 6 columns
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'
91 ));
93 $this->table->setup();
96 function fill_table() {
97 global $CFG;
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);
106 ksort($grade_items);
108 $total = $grade_items[1];
109 unset($grade_items[1]);
110 $grade_items[] = $total;
112 foreach ($grade_items as $grade_item) {
114 $data = array();
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"/>';
126 } else {
127 $data[] = '';
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>';
135 } else {
136 $data[] = $grade_item->get_name();
139 /// prints the grade
141 if ($grade_grade->is_excluded()) {
142 $excluded = get_tring('excluded', 'grades').' ';
143 } else {
144 $excluded = '';
147 if ($grade_item->scaleid) {
148 // using scales
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.'-';
155 } else {
156 $data[] = $excluded.$scales[$grade_grade->finalgrade-1];
159 } else {
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).'%';
170 } else {
171 $percentage = '-';
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).'%';
180 } else {
181 // text grade
182 $percentage = '-';
185 $data[] = $percentage;
187 /// prints rank
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";
197 } else {
198 // no grade, no rank
199 $data[] = "-";
202 /// prints notes
203 if (!empty($grade_text->feedback)) {
204 $data[] = $grade_text->feedback;
205 } else {
206 $data[] = '&nbsp;';
208 $this->table->add_data($data);
211 return true;
212 } else {
213 notify(get_string('nogradeitem', 'grades'));
214 return false;
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.
221 * @return string
223 function print_table($return=false) {
224 ob_start();
225 $this->table->print_html();
226 $html = ob_get_clean();
227 if ($return) {
228 return $html;
229 } else {
230 echo $html;
235 * Processes the data sent by the form (grades and feedbacks).
236 * @var array $data
237 * @return bool Success or Failure (array of errors).
239 function process_data($data) {