3 * File in which the grader_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 grader report building and displaying.
15 class grade_report_grader
extends grade_report
{
18 * @var array $finalgrades
29 * Array of errors for bulk grades updating.
30 * @var array $gradeserror
32 var $gradeserror = array();
37 * The id of the grade_item by which this report will be sorted.
38 * @var int $sortitemid
43 * Sortorder used in the SQL selections.
49 * An SQL fragment affecting the search for users.
50 * @var string $userselect
55 * List of collapsed captegories from user perference
56 * @var array $collapsed
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) {
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
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
.'&perpage='.$this->get_pref('studentsperpage')
90 .'&page='.$this->page
;
92 $this->pbarurl
= 'index.php?id='.$this->courseid
.'&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).
105 * @return bool Success or Failure (array of errors).
107 function process_data($data) {
109 if (!has_capability('moodle/grade:override', $this->context
)) {
113 // always initialize all arrays
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';
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) {
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');
145 if ($data_type == 'grade') {
147 $feedbackformat = false;
148 if ($grade_item->gradetype
== GRADE_TYPE_SCALE
) {
149 if ($postedvalue == -1) { // -1 means no grade
152 $finalgrade = $postedvalue;
155 $trimmed = trim($postedvalue);
156 if (empty($trimmed)) { // empty string means no grade
159 $finalgrade = $this->format_grade($postedvalue);
163 } else if ($data_type == 'feedback') {
165 $trimmed = trim($postedvalue);
166 if (empty($trimmed)) {
169 $feedback = stripslashes($postedvalue);
173 $grade_item->update_final_grade($userid, $finalgrade, 'gradebook', $note, $feedback);
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() {
189 if ($this->sortitemid
) {
190 if (!isset($SESSION->gradeuserreport
->sort
)) {
191 $this->sortorder
= $SESSION->gradeuserreport
->sort
= 'ASC';
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
) {
198 if ($SESSION->gradeuserreport
->sort
== 'ASC') {
199 $this->sortorder
= $SESSION->gradeuserreport
->sort
= 'DESC';
201 $this->sortorder
= $SESSION->gradeuserreport
->sort
= 'ASC';
204 $this->sortorder
= $SESSION->gradeuserreport
->sort
= 'ASC';
207 $SESSION->gradeuserreport
->sortitemid
= $this->sortitemid
;
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
;
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() {
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
238 WHERE ra.roleid in ($this->gradebookroles)
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'));
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();
259 $this->userselect
= 'AND g.userid in ('.implode(',', array_keys($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() {
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
277 WHERE ra.roleid in ($this->gradebookroles)
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() {
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() {
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);
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
338 function print_toggle($type, $return=false) {
341 $icons = array('eyecons' => 'hide',
342 'calculations' => 'calc',
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');
360 if (array_key_exists($type, $icons)) {
361 $image_name = $icons[$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
. "&toggle=$toggle_action&toggle_type=$type\">"
372 . $string . '</a></div>';
382 * Builds and returns the HTML code for the headers.
383 * @return string $headerhtml
385 function get_headerhtml() {
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);
397 $lastarrow = print_arrow('down', $strsortdesc, true);
403 if ($this->sortitemid
=== 'firstname') {
404 if ($this->sortorder
== 'ASC') {
405 $firstarrow = print_arrow('up', $strsortasc, true);
407 $firstarrow = print_arrow('down', $strsortdesc, true);
412 // Prepare Table Headers
415 $numrows = count($this->gtree
->levels
);
417 $columns_to_unset = array();
419 foreach ($this->gtree
->levels
as $key=>$row) {
421 // do not display course grade category
425 $headerhtml .= '<tr class="heading">';
427 if ($key == $numrows - 1) {
428 $headerhtml .= '<th class="user"><a href="'.$this->baseurl
.'&sortitemid=firstname">' . $strfirstname . '</a> ' //TODO: localize
429 . $firstarrow. '/ <a href="'.$this->baseurl
.'&sortitemid=lastname">' . $strlastname . '</a>'. $lastarrow .'</th>';
431 $headerhtml .= '<td class="topleft"> </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'].'"';
446 if (!empty($element['depth'])) {
447 $catlevel = ' catlevel'.$element['depth'];
452 // Element is a filler
453 if ($type == 'filler' or $type == 'fillerfirst' or $type == 'fillerlast') {
454 $headerhtml .= '<th class="'.$type.$catlevel.'" '.$colspan.'> </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);
462 if ($USER->gradeediting
[$this->courseid
]) {
463 $headerhtml .= $this->get_icons($element);
466 $headerhtml .= '</th>';
468 // Element is a grade_item
470 if ($element['object']->id
== $this->sortitemid
) {
471 if ($this->sortorder
== 'ASC') {
472 $arrow = print_arrow('up', $strsortasc, true);
474 $arrow = print_arrow('down', $strsortdesc, true);
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')
495 $headerhtml .= '<th class="'.$type.$catlevel.$dimmed.'"><a href="'.$this->baseurl
.'&sortitemid='
496 . $element['object']->id
.'">'. $element['object']->get_name()
499 $headerhtml .= $this->get_icons($element) . '</th>';
501 $this->items
[$element['object']->sortorder
] =& $element['object'];
506 $headerhtml .= '</tr>';
512 * Builds and return the HTML rows of the table (grades headed by student).
513 * @return string HTML
515 function get_studentshtml() {
518 $strfeedback = $this->get_lang_string("feedback");
520 $showuserimage = $this->get_pref('showuserimage');
521 $numusers = count($this->users
);
523 // Preload scale objects for items with a scaleid
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
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
;
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">';
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
606 foreach ($scales as $scaleoption) {
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 .= '-';
624 $studentshtml .= $scales[$gradeval-1];
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.'" />';
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
) . '" />';
654 // Percentage format if specified by user (check each item for a set preference)
655 $gradedisplaytype = $this->get_pref('gradedisplaytype', $item->id
);
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);
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);";
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 .= '-';
696 $studentshtml .= $scales[$gradeval-1];
699 if (is_null($gradeval)) {
700 $studentshtml .= '-';
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) {
729 $averagesdisplaytype = $this->get_pref('averagesdisplaytype');
730 $averagesdecimalpoints = $this->get_pref('averagesdecimalpoints');
731 $meanselection = $this->get_pref('meanselection');
735 $straverage = get_string('groupavg', 'grades');
736 $showaverages = $this->currentgroup
&& $this->get_pref('showgroups');
737 $groupsql = $this->groupsql
;
738 $groupwheresql = $this->groupwheresql
;
740 $straverage = get_string('average', 'grades');
741 $showaverages = $this->get_pref('showaverages');
743 $groupwheresql = null;
746 if ($meanselection == GRADE_AGGREGATE_MEAN_GRADED
) {
748 $meanstr = "AND NOT g.finalgrade IS NULL";
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
762 WHERE gi.courseid = $this->courseid
764 AND ra.roleid in ($this->gradebookroles)
765 AND ra.contextid ".get_related_contexts_string($this->context
)."
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>';
798 $sum = $sum_array[$item->id
];
800 if ($item->scaleid
) {
802 $finalsum = $sum_array[$item->id
];
803 $finalavg = $finalsum/$count_array[$item->id
];
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;
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>';
836 * Builds and return the HTML row of ranges for each column (i.e. range).
837 * @return string HTML
839 function get_rangehtml() {
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
) {
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>';
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) {
895 if (!$USER->gradeediting
[$this->courseid
]) {
896 return '<div class="grade_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) {
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...)
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);
958 if ($collapsed = get_user_preferences('grade_report_grader_collapsed_categories')) {
959 $collapsed = unserialize($collapsed);
961 $collapsed = array();
966 if (!in_array($targetid, $collapsed)) {
967 $collapsed[] = $targetid;
968 set_user_preference('grade_report_grader_collapsed_categories', serialize($collapsed));
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));