adding some strings
[moodle-linuxchix.git] / grade / report / grader / lib.php
blob8f9ce4fe29f1831405a1d71d0012be05e98bec08
1 <?php // $Id$
2 /**
3 * File in which the grader_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 grader report building and displaying.
12 * @uses grade_report
13 * @package gradebook
15 class grade_report_grader extends grade_report {
16 /**
17 * The final grades.
18 * @var array $finalgrades
20 var $finalgrades;
22 /**
23 * The grade items.
24 * @var array $items
26 var $items;
28 /**
29 * Array of errors for bulk grades updating.
30 * @var array $gradeserror
32 var $gradeserror = array();
34 //// SQL-RELATED
36 /**
37 * The id of the grade_item by which this report will be sorted.
38 * @var int $sortitemid
40 var $sortitemid;
42 /**
43 * Sortorder used in the SQL selections.
44 * @var int $sortorder
46 var $sortorder;
48 /**
49 * An SQL fragment affecting the search for users.
50 * @var string $userselect
52 var $userselect;
54 /**
55 * List of collapsed captegories from user perference
56 * @var array $collapsed
58 var $collapsed;
60 /**
61 * Constructor. Sets local copies of user preferences and initialises grade_tree.
62 * @param int $courseid
63 * @param object $gpr grade plugin return tracking object
64 * @param string $context
65 * @param int $page The current page being viewed (when report is paged)
66 * @param int $sortitemid The id of the grade_item by which to sort the table
68 function grade_report_grader($courseid, $gpr, $context, $page=null, $sortitemid=null) {
69 global $CFG;
70 parent::grade_report($courseid, $gpr, $context, $page);
72 // load collapsed settings for this report
73 if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) {
74 $this->collapsed = unserialize($collapsed);
75 foreach ($this->collapsed as $key=>$id) {
76 if ($this->get_pref('aggregationview', $id) == GRADE_REPORT_AGGREGATION_VIEW_FULL) {
77 unset($this->collapsed[$key]); // full view categories can not be collapsed
80 } else {
81 $this->collapsed = array();
83 // Grab the grade_tree for this course
84 $this->gtree = new grade_tree($this->courseid, true, $this->get_pref('aggregationposition'), $this->collapsed);
86 $this->sortitemid = $sortitemid;
88 // base url for sorting by first/last name
89 $this->baseurl = 'index.php?id='.$this->courseid.'&amp;perpage='.$this->get_pref('studentsperpage')
90 .'&amp;page='.$this->page;
92 $this->pbarurl = 'index.php?id='.$this->courseid.'&amp;perpage='.$this->get_pref('studentsperpage');
94 // Setup groups if requested
95 if ($this->get_pref('showgroups')) {
96 $this->setup_groups();
99 $this->setup_sortitemid();
103 * Processes the data sent by the form (grades and feedbacks).
104 * @var array $data
105 * @return bool Success or Failure (array of errors).
107 function process_data($data) {
109 if (!has_capability('moodle/grade:override', $this->context)) {
110 return false;
113 // always initialize all arrays
114 $queue = array();
115 foreach ($data as $varname => $postedvalue) {
117 $needsupdate = false;
118 $note = false; // TODO implement note??
120 // skip, not a grade nor feedback
121 if (strpos($varname, 'grade') === 0) {
122 $data_type = 'grade';
123 } else if (strpos($varname, 'feedback') === 0) {
124 $data_type = 'feedback';
125 } else {
126 continue;
129 $gradeinfo = explode("_", $varname);
130 $userid = clean_param($gradeinfo[1], PARAM_INT);
131 $itemid = clean_param($gradeinfo[2], PARAM_INT);
133 $oldvalue = $data->{'old'.$varname};
135 // was change requested?
136 if ($oldvalue == $postedvalue) {
137 continue;
140 if (!$grade_item = grade_item::fetch(array('id'=>$itemid, 'courseid'=>$this->courseid))) { // we must verify course id here!
141 error('Incorrect grade item id');
144 // Pre-process grade
145 if ($data_type == 'grade') {
146 $feedback = false;
147 $feedbackformat = false;
148 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
149 if ($postedvalue == -1) { // -1 means no grade
150 $finalgrade = null;
151 } else {
152 $finalgrade = $postedvalue;
154 } else {
155 $trimmed = trim($postedvalue);
156 if (empty($trimmed)) { // empty string means no grade
157 $finalgrade = null;
158 } else {
159 $finalgrade = $this->format_grade($postedvalue);
163 } else if ($data_type == 'feedback') {
164 $finalgrade = false;
165 $trimmed = trim($postedvalue);
166 if (empty($trimmed)) {
167 $feedback = NULL;
168 } else {
169 $feedback = stripslashes($postedvalue);
173 $grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $note, $feedback);
176 return true;
181 * Setting the sort order, this depends on last state
182 * all this should be in the new table class that we might need to use
183 * for displaying grades.
185 function setup_sortitemid() {
187 global $SESSION;
189 if ($this->sortitemid) {
190 if (!isset($SESSION->gradeuserreport->sort)) {
191 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
192 } else {
193 // this is the first sort, i.e. by last name
194 if (!isset($SESSION->gradeuserreport->sortitemid)) {
195 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
196 } else if ($SESSION->gradeuserreport->sortitemid == $this->sortitemid) {
197 // same as last sort
198 if ($SESSION->gradeuserreport->sort == 'ASC') {
199 $this->sortorder = $SESSION->gradeuserreport->sort = 'DESC';
200 } else {
201 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
203 } else {
204 $this->sortorder = $SESSION->gradeuserreport->sort = 'ASC';
207 $SESSION->gradeuserreport->sortitemid = $this->sortitemid;
208 } else {
209 // not requesting sort, use last setting (for paging)
211 if (isset($SESSION->gradeuserreport->sortitemid)) {
212 $this->sortitemid = $SESSION->gradeuserreport->sortitemid;
214 if (isset($SESSION->gradeuserreport->sort)) {
215 $this->sortorder = $SESSION->gradeuserreport->sort;
216 } else {
217 $this->sortorder = 'ASC';
223 * pulls out the userids of the users to be display, and sort them
224 * the right outer join is needed because potentially, it is possible not
225 * to have the corresponding entry in grade_grades table for some users
226 * this is check for user roles because there could be some users with grades
227 * but not supposed to be displayed
229 function load_users() {
230 global $CFG;
232 if (is_numeric($this->sortitemid)) {
233 $sql = "SELECT u.id, u.firstname, u.lastname
234 FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
235 {$CFG->prefix}user u ON (u.id = g.userid AND g.itemid = $this->sortitemid)
236 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
237 $this->groupsql
238 WHERE ra.roleid in ($this->gradebookroles)
239 $this->groupwheresql
240 AND ra.contextid ".get_related_contexts_string($this->context)."
241 ORDER BY g.finalgrade $this->sortorder";
242 $this->users = get_records_sql($sql, $this->get_pref('studentsperpage') * $this->page,
243 $this->get_pref('studentsperpage'));
244 } else {
245 // default sort
246 // get users sorted by lastname
247 $this->users = get_role_users(@implode(',', $CFG->gradebookroles), $this->context, false,
248 'u.id, u.firstname, u.lastname', 'u.'.$this->sortitemid .' '. $this->sortorder,
249 false, $this->page * $this->get_pref('studentsperpage'), $this->get_pref('studentsperpage'),
250 $this->currentgroup);
251 // need to cut users down by groups
255 if (empty($this->users)) {
256 $this->userselect = '';
257 $this->users = array();
258 } else {
259 $this->userselect = 'AND g.userid in ('.implode(',', array_keys($this->users)).')';
262 return $this->users;
266 * Fetches and returns a count of all the users that will be shown on this page.
267 * @return int Count of users
269 function get_numusers() {
270 global $CFG;
272 $countsql = "SELECT COUNT(DISTINCT u.id)
273 FROM {$CFG->prefix}grade_grades g RIGHT OUTER JOIN
274 {$CFG->prefix}user u ON (u.id = g.userid AND g.itemid = $this->sortitemid)
275 LEFT JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
276 $this->groupsql
277 WHERE ra.roleid in ($this->gradebookroles)
278 $this->groupwheresql
279 AND ra.contextid ".get_related_contexts_string($this->context);
280 return count_records_sql($countsql);
284 * we supply the userids in this query, and get all the grades
285 * pulls out all the grades, this does not need to worry about paging
287 function load_final_grades() {
288 global $CFG;
290 // please note that we must fetch all grade_grades fields if we want to contruct grade_grade object from it!
291 $sql = "SELECT g.*, gt.feedback, gt.feedbackformat, gi.grademin, gi.grademax
292 FROM {$CFG->prefix}grade_items gi,
293 {$CFG->prefix}grade_grades g
294 LEFT JOIN {$CFG->prefix}grade_grades_text gt ON g.id = gt.gradeid
295 WHERE g.itemid = gi.id AND gi.courseid = $this->courseid $this->userselect";
297 if ($grades = get_records_sql($sql)) {
298 foreach ($grades as $grade) {
299 $this->finalgrades[$grade->userid][$grade->itemid] = $grade;
305 * Builds and returns a div with on/off toggles.
306 * @return string HTML code
308 function get_toggles_html() {
309 global $USER;
310 $html = '<div id="grade-report-toggles">';
311 if ($USER->gradeediting[$this->courseid]) {
312 if (has_capability('moodle/grade:manage', $this->context) or has_capability('moodle/grade:hide', $this->context)) {
313 $html .= $this->print_toggle('eyecons', true);
315 if (has_capability('moodle/grade:manage', $this->context)
316 or has_capability('moodle/grade:lock', $this->context)
317 or has_capability('moodle/grade:unlock', $this->context)) {
318 $html .= $this->print_toggle('locks', true);
320 if (has_capability('moodle/grade:manage', $this->context)) {
321 $html .= $this->print_toggle('calculations', true);
325 $html .= $this->print_toggle('averages', true);
326 $html .= $this->print_toggle('groups', true);
327 $html .= $this->print_toggle('ranges', true);
328 $html .= '</div>';
329 return $html;
333 * Shortcut function for printing the grader report toggles.
334 * @param string $type The type of toggle
335 * @param bool $return Whether to return the HTML string rather than printing it
336 * @return void
338 function print_toggle($type, $return=false) {
339 global $CFG;
341 $icons = array('eyecons' => 'hide',
342 'calculations' => 'calc',
343 'locks' => 'lock',
344 'averages' => 'sigma');
346 $pref_name = 'grade_report_show' . $type;
347 $show_pref = get_user_preferences($pref_name, $CFG->$pref_name);
349 $strshow = $this->get_lang_string('show' . $type, 'grades');
350 $strhide = $this->get_lang_string('hide' . $type, 'grades');
352 $show_hide = 'show';
353 $toggle_action = 1;
355 if ($show_pref) {
356 $show_hide = 'hide';
357 $toggle_action = 0;
360 if (array_key_exists($type, $icons)) {
361 $image_name = $icons[$type];
362 } else {
363 $image_name = $type;
366 $string = ${'str' . $show_hide};
368 $img = '<img src="'.$CFG->pixpath.'/t/'.$image_name.'.gif" class="iconsmall" alt="'
369 .$string.'" title="'.$string.'" />'. "\n";
371 $retval = '<div class="gradertoggle">' . $img . '<a href="' . $this->baseurl . "&amp;toggle=$toggle_action&amp;toggle_type=$type\">"
372 . $string . '</a></div>';
374 if ($return) {
375 return $retval;
376 } else {
377 echo $retval;
382 * Builds and returns the HTML code for the headers.
383 * @return string $headerhtml
385 function get_headerhtml() {
386 global $CFG, $USER;
388 $strsortasc = $this->get_lang_string('sortasc', 'grades');
389 $strsortdesc = $this->get_lang_string('sortdesc', 'grades');
390 $strfirstname = $this->get_lang_string('firstname');
391 $strlastname = $this->get_lang_string('lastname');
393 if ($this->sortitemid === 'lastname') {
394 if ($this->sortorder == 'ASC') {
395 $lastarrow = print_arrow('up', $strsortasc, true);
396 } else {
397 $lastarrow = print_arrow('down', $strsortdesc, true);
399 } else {
400 $lastarrow = '';
403 if ($this->sortitemid === 'firstname') {
404 if ($this->sortorder == 'ASC') {
405 $firstarrow = print_arrow('up', $strsortasc, true);
406 } else {
407 $firstarrow = print_arrow('down', $strsortdesc, true);
409 } else {
410 $firstarrow = '';
412 // Prepare Table Headers
413 $headerhtml = '';
415 $numrows = count($this->gtree->levels);
417 $columns_to_unset = array();
419 foreach ($this->gtree->levels as $key=>$row) {
420 if ($key == 0) {
421 // do not display course grade category
422 // continue;
425 $headerhtml .= '<tr class="heading">';
427 if ($key == $numrows - 1) {
428 $headerhtml .= '<th class="user"><a href="'.$this->baseurl.'&amp;sortitemid=firstname">' . $strfirstname . '</a> ' //TODO: localize
429 . $firstarrow. '/ <a href="'.$this->baseurl.'&amp;sortitemid=lastname">' . $strlastname . '</a>'. $lastarrow .'</th>';
430 } else {
431 $headerhtml .= '<td class="topleft">&nbsp;</td>';
434 foreach ($row as $columnkey => $element) {
435 $eid = $element['eid'];
436 $object = $element['object'];
437 $type = $element['type'];
438 $categorystate = @$element['categorystate'];
440 if (!empty($element['colspan'])) {
441 $colspan = 'colspan="'.$element['colspan'].'"';
442 } else {
443 $colspan = '';
446 if (!empty($element['depth'])) {
447 $catlevel = ' catlevel'.$element['depth'];
448 } else {
449 $catlevel = '';
452 // Element is a filler
453 if ($type == 'filler' or $type == 'fillerfirst' or $type == 'fillerlast') {
454 $headerhtml .= '<th class="'.$type.$catlevel.'" '.$colspan.'>&nbsp;</th>';
456 // Element is a category
457 else if ($type == 'category') {
458 $headerhtml .= '<th class="category'.$catlevel.'" '.$colspan.'>'.$element['object']->get_name();
459 $headerhtml .= $this->get_collapsing_icon($element);
461 // Print icons
462 if ($USER->gradeediting[$this->courseid]) {
463 $headerhtml .= $this->get_icons($element);
466 $headerhtml .= '</th>';
468 // Element is a grade_item
469 else {
470 if ($element['object']->id == $this->sortitemid) {
471 if ($this->sortorder == 'ASC') {
472 $arrow = print_arrow('up', $strsortasc, true);
473 } else {
474 $arrow = print_arrow('down', $strsortdesc, true);
476 } else {
477 $arrow = '';
480 $dimmed = '';
481 if ($element['object']->is_hidden()) {
482 $dimmed = ' dimmed_text ';
485 if ($object->itemtype == 'mod') {
486 $icon = '<img src="'.$CFG->modpixpath.'/'.$object->itemmodule.'/icon.gif" class="icon" alt="'
487 .$this->get_lang_string('modulename', $object->itemmodule).'"/>';
488 } else if ($object->itemtype == 'manual') {
489 //TODO: add manual grading icon
490 $icon = '<img src="'.$CFG->pixpath.'/t/edit.gif" class="icon" alt="'.$this->get_lang_string('manualgrade', 'grades')
491 .'"/>';
495 $headerhtml .= '<th class="'.$type.$catlevel.$dimmed.'"><a href="'.$this->baseurl.'&amp;sortitemid='
496 . $element['object']->id .'">'. $element['object']->get_name()
497 . '</a>' . $arrow;
499 $headerhtml .= $this->get_icons($element) . '</th>';
501 $this->items[$element['object']->sortorder] =& $element['object'];
506 $headerhtml .= '</tr>';
508 return $headerhtml;
512 * Builds and return the HTML rows of the table (grades headed by student).
513 * @return string HTML
515 function get_studentshtml() {
516 global $CFG, $USER;
517 $studentshtml = '';
518 $strfeedback = $this->get_lang_string("feedback");
519 $gradetabindex = 1;
520 $showuserimage = $this->get_pref('showuserimage');
521 $numusers = count($this->users);
523 // Preload scale objects for items with a scaleid
524 $scales_list = '';
525 $tabindices = array();
526 foreach ($this->items as $item) {
527 if (!empty($item->scaleid)) {
528 $scales_list .= "$item->scaleid,";
530 $tabindices[$item->id]['grade'] = $gradetabindex;
531 $tabindices[$item->id]['feedback'] = $gradetabindex + $numusers;
532 $gradetabindex += $numusers * 2;
534 $scales_array = array();
536 if (!empty($scales_list)) {
537 $scales_list = substr($scales_list, 0, -1);
538 $scales_array = get_records_list('scale', 'id', $scales_list);
541 foreach ($this->users as $userid => $user) {
542 // Student name and link
543 $user_pic = null;
544 if ($showuserimage) {
545 $user_pic = '<div class="userpic">' . print_user_picture($user->id, $this->courseid, true, 0, true) . '</div>';
548 $studentshtml .= '<tr><th class="user">' . $user_pic . '<a href="' . $CFG->wwwroot . '/user/view.php?id='
549 . $user->id . '">' . fullname($user) . '</a></th>';
551 foreach ($this->items as $itemid=>$item) {
552 // Get the decimal points preference for this item
553 $decimalpoints = $this->get_pref('decimalpoints', $item->id);
555 if (isset($this->finalgrades[$userid][$item->id])) {
556 $gradeval = $this->finalgrades[$userid][$item->id]->finalgrade;
558 $grade = new grade_grade($this->finalgrades[$userid][$item->id], false);
559 $grade->feedback = stripslashes_safe($this->finalgrades[$userid][$item->id]->feedback);
560 $grade->feedbackformat = $this->finalgrades[$userid][$item->id]->feedbackformat;
562 } else {
563 $gradeval = null;
564 $grade = new grade_grade(array('userid'=>$userid, 'itemid'=>$item->id), false);
565 $grade->feedback = '';
568 $grade->courseid = $this->courseid;
569 $grade->grade_item =& $this->items[$itemid]; // this speedsup is_hidden() and other grade_grade methods
571 // emulate grade element
572 $element = array('eid'=>'g'.$grade->id, 'object'=>$grade, 'type'=>'grade');
574 if ($grade->is_overridden()) {
575 $studentshtml .= '<td class="overridden">';
576 } else {
577 $studentshtml .= '<td>';
580 if ($grade->is_excluded()) {
581 $studentshtml .= get_string('excluded', 'grades'); // TODO: improve visual representation of excluded grades
584 // Do not show any icons if no grade (no record in DB to match)
585 // TODO: change edit/hide/etc. links to use itemid and userid to allow creating of new grade objects
586 if (!$item->needsupdate and !empty($grade->id) and $USER->gradeediting[$this->courseid]) {
587 $studentshtml .= $this->get_icons($element);
590 // if in editting mode, we need to print either a text box
591 // or a drop down (for scales)
592 // grades in item of type grade category or course are not directly editable
593 if ($item->needsupdate) {
594 $studentshtml .= '<span class="gradingerror">'.get_string('error').'</span>';
596 } else if ($USER->gradeediting[$this->courseid]) {
597 // We need to retrieve each grade_grade object from DB in order to
598 // know if they are hidden/locked
600 if ($item->scaleid && !empty($scales_array[$item->scaleid])) {
601 $scale = $scales_array[$item->scaleid];
603 $scales = explode(",", $scale->scale);
604 // reindex because scale is off 1
605 $i = 0;
606 foreach ($scales as $scaleoption) {
607 $i++;
608 $scaleopt[$i] = $scaleoption;
611 if ($this->get_pref('quickgrading') and $grade->is_editable()) {
612 $studentshtml .= '<input type="hidden" name="oldgrade_'.$userid.'_'
613 .$item->id.'" value="'.$gradeval.'"/>';
614 $studentshtml .= choose_from_menu($scaleopt, 'grade_'.$userid.'_'.$item->id,
615 $gradeval, $this->get_lang_string('nograde'), '', '-1',
616 true, false, $tabindices[$item->id]['grade']);
617 } elseif(!empty($scale)) {
618 $scales = explode(",", $scale->scale);
620 // invalid grade if gradeval < 1
621 if ((int) $gradeval < 1) {
622 $studentshtml .= '-';
623 } else {
624 $studentshtml .= $scales[$gradeval-1];
626 } else {
627 // no such scale, throw error?
630 } else if ($item->gradetype != GRADE_TYPE_TEXT) { // Value type
631 if ($this->get_pref('quickgrading') and $grade->is_editable()) {
632 $value = $this->get_grade_clean($gradeval, $decimalpoints);
633 $studentshtml .= '<input type="hidden" name="oldgrade_'.$userid.'_'.$item->id.'" value="'.$value.'" />';
634 $studentshtml .= '<input size="6" tabindex="' . $tabindices[$item->id]['grade'] . '" type="text" name="grade_'
635 .$userid.'_' .$item->id.'" value="'.$value.'" />';
636 } else {
637 $studentshtml .= $this->get_grade_clean($gradeval, $decimalpoints);
642 // If quickfeedback is on, print an input element
643 if ($this->get_pref('quickfeedback') and $grade->is_editable()) {
644 if ($this->get_pref('quickgrading')) {
645 $studentshtml .= '<br />';
647 $studentshtml .= '<input type="hidden" name="oldfeedback_'
648 .$userid.'_'.$item->id.'" value="' . s($grade->feedback) . '" />';
649 $studentshtml .= '<input tabindex="' . $tabindices[$item->id]['feedback'] . '" size="6" type="text" name="feedback_'
650 .$userid.'_'.$item->id.'" value="' . s($grade->feedback) . '" />';
653 } else {
654 // Percentage format if specified by user (check each item for a set preference)
655 $gradedisplaytype = $this->get_pref('gradedisplaytype', $item->id);
657 $percentsign = '';
658 $grademin = $item->grademin;
659 $grademax = $item->grademax;
661 if ($gradedisplaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE) {
662 if (!is_null($gradeval)) {
663 $gradeval = grade_grade::standardise_score($gradeval, $grademin, $grademax, 0, 100);
665 $percentsign = '%';
668 // If feedback present, surround grade with feedback tooltip
669 if (!empty($grade->feedback)) {
670 if ($grade->feedbackformat == 1) {
671 $overlib = "return overlib('" . s(ltrim($grade->feedback)) . "', FULLHTML);";
672 } else {
673 $overlib = "return overlib('" . ($grade->feedback) . "', CAPTION, '$strfeedback');";
676 $studentshtml .= '<span onmouseover="' . $overlib . '" onmouseout="return nd();">';
679 if ($item->needsupdate) {
680 $studentshtml .= '<span class="gradingerror">'.get_string('error').'</span>';
682 } else if ($gradedisplaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER) {
683 $letters = grade_report::get_grade_letters();
684 if (!is_null($gradeval)) {
685 $studentshtml .= grade_grade::get_letter($letters, $gradeval, $grademin, $grademax);
687 } else if ($item->scaleid && !empty($scales_array[$item->scaleid])
688 && $gradedisplaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL) {
689 $scale = $scales_array[$item->scaleid];
690 $scales = explode(",", $scale->scale);
692 // invalid grade if gradeval < 1
693 if ((int) $gradeval < 1) {
694 $studentshtml .= '-';
695 } else {
696 $studentshtml .= $scales[$gradeval-1];
698 } else {
699 if (is_null($gradeval)) {
700 $studentshtml .= '-';
701 } else {
702 $studentshtml .= $this->get_grade_clean($gradeval, $decimalpoints). $percentsign;
705 if (!empty($grade->feedback)) {
706 $studentshtml .= '</span>';
710 if (!empty($this->gradeserror[$item->id][$userid])) {
711 $studentshtml .= $this->gradeserror[$item->id][$userid];
714 $studentshtml .= '</td>' . "\n";
716 $studentshtml .= '</tr>';
718 return $studentshtml;
722 * Builds and return the HTML row of column totals.
723 * @param bool $grouponly Whether to return only group averages or all averages.
724 * @return string HTML
726 function get_avghtml($grouponly=false) {
727 global $CFG, $USER;
729 $averagesdisplaytype = $this->get_pref('averagesdisplaytype');
730 $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints');
731 $meanselection = $this->get_pref('meanselection');
732 $avghtml = '';
734 if ($grouponly) {
735 $straverage = get_string('groupavg', 'grades');
736 $showaverages = $this->currentgroup && $this->get_pref('showgroups');
737 $groupsql = $this->groupsql;
738 $groupwheresql = $this->groupwheresql;
739 } else {
740 $straverage = get_string('average', 'grades');
741 $showaverages = $this->get_pref('showaverages');
742 $groupsql = null;
743 $groupwheresql = null;
746 if ($meanselection == GRADE_AGGREGATE_MEAN_GRADED) {
747 // non empty grades
748 $meanstr = "AND NOT g.finalgrade IS NULL";
749 } else {
750 $meanstr = "";
752 if ($showaverages) {
754 /** SQL for finding the SUM grades of all visible users ($CFG->gradebookroles) or group sum*/
755 // do not sum -1 (no grade), treat as 0 for now
756 $SQL = "SELECT g.itemid, SUM(g.finalgrade) as sum, COUNT(DISTINCT(u.id)) as count
757 FROM {$CFG->prefix}grade_items gi LEFT JOIN
758 {$CFG->prefix}grade_grades g ON gi.id = g.itemid RIGHT OUTER JOIN
759 {$CFG->prefix}user u ON u.id = g.userid LEFT JOIN
760 {$CFG->prefix}role_assignments ra ON u.id = ra.userid
761 $groupsql
762 WHERE gi.courseid = $this->courseid
763 $groupwheresql
764 AND ra.roleid in ($this->gradebookroles)
765 AND ra.contextid ".get_related_contexts_string($this->context)."
766 $meanstr
767 GROUP BY g.itemid";
769 $sum_array = array();
770 $count_array = array();
771 $sums = get_records_sql($SQL);
773 foreach ($sums as $itemid => $csum) {
774 $sum_array[$itemid] = $csum->sum;
775 $count_array[$itemid] = $csum->count;
778 $avghtml = '<tr><th>'.$straverage.'</th>';
779 foreach ($this->items as $item) {
780 $decimalpoints = $this->get_pref('decimalpoints', $item->id);
781 // Determine which display type to use for this average
782 $gradedisplaytype = $this->get_pref('gradedisplaytype', $item->id);
783 if ($USER->gradeediting[$this->courseid]) {
784 $displaytype = GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL;
785 } elseif ($averagesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // Inherit specific column or general preference
786 $displaytype = $gradedisplaytype;
787 } else { // General preference overrides specific column display type
788 $displaytype = $averagesdisplaytype;
791 if ($averagesdecimalpoints != GRADE_REPORT_PREFERENCE_INHERIT) {
792 $decimalpoints = $averagesdecimalpoints;
795 if (empty($count_array[$item->id]) || !isset($sum_array[$item->id])) {
796 $avghtml .= '<td>-</td>';
797 } else {
798 $sum = $sum_array[$item->id];
800 if ($item->scaleid) {
801 if ($grouponly) {
802 $finalsum = $sum_array[$item->id];
803 $finalavg = $finalsum/$count_array[$item->id];
804 } else {
805 $finalavg = $sum/$count_array[$item->id];
808 $scaleval = round($this->get_grade_clean($finalavg, $decimalpoints));
809 $scale_object = new grade_scale(array('id' => $item->scaleid), false);
810 $gradehtml = $scale_object->get_nearest_item($scaleval);
811 $rawvalue = $scaleval;
812 } else {
813 $gradeval = $this->get_grade_clean($sum/$count_array[$item->id], $decimalpoints);
815 $gradehtml = round($gradeval, $decimalpoints);
816 $rawvalue = $gradeval;
819 if ($displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE) {
820 $gradeval = grade_grade::standardise_score($rawvalue, $item->grademin, $item->grademax, 0, 100);
821 $gradehtml = round($gradeval, $decimalpoints) . '%';
822 } elseif ($displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER) {
823 $letters = grade_report::get_grade_letters();
824 $gradehtml = grade_grade::get_letter($letters, $gradeval, $item->grademin, $item->grademax);
827 $avghtml .= '<td>'.$gradehtml.'</td>';
830 $avghtml .= '</tr>';
832 return $avghtml;
836 * Builds and return the HTML row of ranges for each column (i.e. range).
837 * @return string HTML
839 function get_rangehtml() {
840 global $USER;
842 $scalehtml = '';
843 if ($this->get_pref('showranges')) {
844 $rangesdisplaytype = $this->get_pref('rangesdisplaytype');
845 $rangesdecimalpoints = $this->get_pref('rangesdecimalpoints');
846 $scalehtml = '<tr><th class="range">'.$this->get_lang_string('range','grades').'</th>';
848 foreach ($this->items as $item) {
849 $decimalpoints = $this->get_pref('decimalpoints', $item->id);
850 // Determine which display type to use for this range
851 $gradedisplaytype = $this->get_pref('gradedisplaytype', $item->id);
853 if ($USER->gradeediting[$this->courseid]) {
854 $displaytype = GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL;
855 } elseif ($rangesdisplaytype == GRADE_REPORT_PREFERENCE_INHERIT) { // Inherit specific column or general preference
856 $displaytype = $gradedisplaytype;
857 } else { // General preference overrides specific column display type
858 $displaytype = $rangesdisplaytype;
861 if ($rangesdecimalpoints != GRADE_REPORT_PREFERENCE_INHERIT) {
862 $decimalpoints = $rangesdecimalpoints;
865 if ($displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL) {
866 $grademin = $this->get_grade_clean($item->grademin, $decimalpoints);
867 $grademax = $this->get_grade_clean($item->grademax, $decimalpoints);
868 } elseif ($displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE) {
869 $grademin = 0;
870 $grademax = 100;
871 } elseif ($displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER) {
872 $letters = grade_report::get_grade_letters();
873 $grademin = end($letters);
874 $grademax = reset($letters);
877 $scalehtml .= '<th class="range">'. $grademin.'-'. $grademax.'</th>';
879 $scalehtml .= '</tr>';
881 return $scalehtml;
885 * Given a grade_category, grade_item or grade_grade, this function
886 * figures out the state of the object and builds then returns a div
887 * with the icons needed for the grader report.
889 * @param object $object
890 * @return string HTML
892 function get_icons($element) {
893 global $CFG, $USER;
895 if (!$USER->gradeediting[$this->courseid]) {
896 return '<div class="grade_icons" />';
899 // Init all icons
900 $edit_icon = $this->gtree->get_edit_icon($element, $this->gpr);
901 $edit_calculation_icon = '';
902 $show_hide_icon = '';
903 $lock_unlock_icon = '';
905 if ($this->get_pref('showcalculations')) {
906 $edit_calculation_icon = $this->gtree->get_calculation_icon($element, $this->gpr);
909 if ($this->get_pref('showeyecons')) {
910 $show_hide_icon = $this->gtree->get_hiding_icon($element, $this->gpr);
913 if ($this->get_pref('showlocks')) {
914 $lock_unlock_icon = $this->gtree->get_locking_icon($element, $this->gpr);
917 return '<div class="grade_icons">'.$edit_icon.$edit_calculation_icon.$show_hide_icon.$lock_unlock_icon.'</div>';
921 * Given a category element returns collapsing +/- icon if available
922 * @param object $object
923 * @return string HTML
925 function get_collapsing_icon($element) {
926 global $CFG;
928 $contract_expand_icon = '';
929 // If object is a category, display expand/contract icon
930 if ($element['type'] == 'category' && $this->get_pref('aggregationview', $element['object']->id) == GRADE_REPORT_AGGREGATION_VIEW_COMPACT) {
931 // Load language strings
932 $strswitch_minus = $this->get_lang_string('contract', 'grades');
933 $strswitch_plus = $this->get_lang_string('expand', 'grades');
934 $expand_contract = 'switch_minus'; // Default: expanded
936 if (in_array($element['object']->id, $this->collapsed)) {
937 $expand_contract = 'switch_plus';
939 $url = $this->gpr->get_return_url(null, array('target'=>$element['eid'], 'action'=>$expand_contract, 'sesskey'=>sesskey()));
940 $contract_expand_icon = '<a href="'.$url.'"><img src="'.$CFG->pixpath.'/t/'.$expand_contract.'.gif" class="iconsmall" alt="'
941 .${'str'.$expand_contract}.'" title="'.${'str'.$expand_contract}.'" /></a>';
943 return $contract_expand_icon;
947 * Processes a single action against a category, grade_item or grade.
948 * @param string $target eid ({type}{id}, e.g. c4 for category4)
949 * @param string $action Which action to take (edit, delete etc...)
950 * @return
952 function process_action($target, $action) {
953 // TODO: this code should be in some grade_tree static method
954 $targettype = substr($target, 0, 1);
955 $targetid = substr($target, 1);
956 // TODO: end
958 if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) {
959 $collapsed = unserialize($collapsed);
960 } else {
961 $collapsed = array();
964 switch ($action) {
965 case 'switch_minus':
966 if (!in_array($targetid, $collapsed)) {
967 $collapsed[] = $targetid;
968 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
970 break;
972 case 'switch_plus':
973 $key = array_search($targetid, $collapsed);
974 if ($key !== false) {
975 unset($collapsed[$key]);
976 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
978 break;
980 default:
981 break;
984 return true;