Use the new, central, create_guest_record() function. MDL-10375
[pfb-moodle.git] / lib / gradelib.php
blobca1af68032b1be15c1dbe8f7e535a0956fb3a35d
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
9 // //
10 // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * Library of functions for gradebook
29 * @author Moodle HQ developers
30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package moodlecore
35 define('GRADE_AGGREGATE_MEAN_ALL', 0);
36 define('GRADE_AGGREGATE_MEDIAN', 1);
37 define('GRADE_AGGREGATE_MEAN_GRADED', 2);
38 define('GRADE_AGGREGATE_MIN', 3);
39 define('GRADE_AGGREGATE_MAX', 4);
40 define('GRADE_AGGREGATE_MODE', 5);
42 define('GRADE_CHILDTYPE_ITEM', 0);
43 define('GRADE_CHILDTYPE_CAT', 1);
45 define('GRADE_ITEM', 0); // Used to compare class names with CHILDTYPE values
46 define('GRADE_CATEGORY', 1); // Used to compare class names with CHILDTYPE values
48 define('GRADE_CATEGORY_CONTRACTED', 0); // The state of a category header in the grader report
49 define('GRADE_CATEGORY_EXPANDED', 1); // The state of a category header in the grader report
51 define('GRADE_TYPE_NONE', 0);
52 define('GRADE_TYPE_VALUE', 1);
53 define('GRADE_TYPE_SCALE', 2);
54 define('GRADE_TYPE_TEXT', 3);
56 define('GRADE_UPDATE_OK', 0);
57 define('GRADE_UPDATE_FAILED', 1);
58 define('GRADE_UPDATE_MULTIPLE', 2);
59 define('GRADE_UPDATE_ITEM_DELETED', 3);
60 define('GRADE_UPDATE_ITEM_LOCKED', 4);
62 // Grate teables history tracking actions
63 define('GRADE_HISTORY_INSERT', 1);
64 define('GRADE_HISTORY_UPDATE', 2);
65 define('GRADE_HISTORY_DELETE', 3);
67 // Set up constants for report preferences
68 define('GRADER_REPORT_AGGREGATION_POSITION_LEFT', 0);
69 define('GRADER_REPORT_AGGREGATION_POSITION_RIGHT', 1);
70 define('GRADER_REPORT_AGGREGATION_VIEW_FULL', 0);
71 define('GRADER_REPORT_AGGREGATION_VIEW_COMPACT', 1);
72 define('GRADER_REPORT_GRADE_DISPLAY_TYPE_RAW', 0);
73 define('GRADER_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE', 1);
74 define('GRADER_REPORT_FEEDBACK_FORMAT_TEXT', 0);
75 define('GRADER_REPORT_FEEDBACK_FORMAT_HTML', 1);
78 require_once($CFG->libdir . '/grade/grade_category.php');
79 require_once($CFG->libdir . '/grade/grade_item.php');
80 require_once($CFG->libdir . '/grade/grade_grades.php');
81 require_once($CFG->libdir . '/grade/grade_scale.php');
82 require_once($CFG->libdir . '/grade/grade_outcome.php');
83 require_once($CFG->libdir . '/grade/grade_grades_text.php');
84 require_once($CFG->libdir . '/grade/grade_tree.php');
86 /***** PUBLIC GRADE API *****/
88 /**
89 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
90 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
91 * or key means do not change existing.
93 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
94 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
96 * @param string $source source of the grade such as 'mod/assignment', often used to prevent infinite loops when processing grade_updated events
97 * @param int $courseid id of course
98 * @param string $itemtype type of grade item - mod, block, gradecategory, calculated
99 * @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
100 * @param int $iteminstance instance it of graded subject
101 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
102 * @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating rgade_item definition only\
103 * @param mixed $itemdetails object or array describing the grading item, NULL if no change
105 function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
106 global $USER;
108 // only following grade_item properties can be changed in this function
109 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
111 // grade item identification
112 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
114 if (is_null($courseid) or is_null($itemtype)) {
115 debugging('Missing courseid or itemtype');
116 return GRADE_UPDATE_FAILED;
119 if (!$grade_items = grade_item::fetch_all($params)) {
120 // create a new one
121 $grade_item = false;
123 } else if (count($grade_items) == 1){
124 $grade_item = reset($grade_items);
125 unset($grade_items); //release memory
127 } else {
128 debugging('Found more than one grade item');
129 return GRADE_UPDATE_MULTIPLE;
132 if (!empty($itemdetails['deleted'])) {
133 if ($grade_item) {
134 if ($grade_item->delete($source)) {
135 return GRADE_UPDATE_OK;
136 } else {
137 return GRADE_UPDATE_FAILED;
140 return GRADE_UPDATE_OK;
143 /// Create or update the grade_item if needed
144 if (!$grade_item) {
145 if ($itemdetails) {
146 $itemdetails = (array)$itemdetails;
148 // grademin and grademax ignored when scale specified
149 if (array_key_exists('scaleid', $itemdetails)) {
150 if ($itemdetails['scaleid']) {
151 unset($itemdetails['grademin']);
152 unset($itemdetails['grademax']);
156 foreach ($itemdetails as $k=>$v) {
157 if (!in_array($k, $allowed)) {
158 // ignore it
159 continue;
161 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
162 // no grade item needed!
163 return GRADE_UPDATE_OK;
165 $params[$k] = $v;
168 $grade_item = new grade_item($params);
169 $grade_item->insert();
171 } else {
172 if ($grade_item->is_locked()) {
173 debugging('Grading item is locked!');
174 return GRADE_UPDATE_ITEM_LOCKED;
177 if ($itemdetails) {
178 $itemdetails = (array)$itemdetails;
179 $update = false;
180 foreach ($itemdetails as $k=>$v) {
181 if (!in_array($k, $allowed)) {
182 // ignore it
183 continue;
185 if ($grade_item->{$k} != $v) {
186 $grade_item->{$k} = $v;
187 $update = true;
190 if ($update) {
191 $grade_item->update();
196 /// Some extra checks
197 // do we use grading?
198 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
199 return GRADE_UPDATE_OK;
202 // no grade submitted
203 if (empty($grades)) {
204 return GRADE_UPDATE_OK;
207 /// Finally start processing of grades
208 if (is_object($grades)) {
209 $grades = array($grades);
210 } else {
211 if (array_key_exists('userid', $grades)) {
212 $grades = array($grades);
216 $failed = false;
217 foreach ($grades as $grade) {
218 $grade = (array)$grade;
219 if (empty($grade['userid'])) {
220 $failed = true;
221 debugging('Invalid userid in grade submitted');
222 continue;
223 } else {
224 $userid = $grade['userid'];
227 $rawgrade = false;
228 $feedback = false;
229 $feedbackformat = FORMAT_MOODLE;
231 if (array_key_exists('rawgrade', $grade)) {
232 $rawgrade = $grade['rawgrade'];
235 if (array_key_exists('feedback', $grade)) {
236 $feedback = $grade['feedback'];
239 if (array_key_exists('feedbackformat', $grade)) {
240 $feedbackformat = $grade['feedbackformat'];
243 if (array_key_exists('usermodified', $grade)) {
244 $usermodified = $grade['usermodified'];
245 } else {
246 $usermodified = $USER->id;
249 // update or insert the grade
250 if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat, $usermodified)) {
251 $failed = true;
255 if (!$failed) {
256 return GRADE_UPDATE_OK;
257 } else {
258 return GRADE_UPDATE_FAILED;
264 * Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
265 * This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
266 * If it's locked to the current use then the module can print a nice message or prevent editing in the module.
267 * If no $userid is given, the method will always return the grade_item's locked state.
268 * If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
269 * the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
270 * return the locked state of the specific grade.
272 * @param int $courseid id of course
273 * @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
274 * @param string $itemmodule 'forum, 'quiz', 'csv' etc
275 * @param int $iteminstance id of the item module
276 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
277 * @param int $userid ID of the graded user
278 * @return boolean Whether the grade is locked or not
280 function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
282 if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) {
283 return false;
285 } else if (count($grade_items) == 1){
286 $grade_item = reset($grade_items);
287 return $grade_item->is_locked($userid);
289 } else {
290 debugging('Found more than one grade item');
291 foreach ($grade_items as $grade_item) {
292 if ($grade_item->is_locked($userid)) {
293 return true;
296 return false;
300 /***** END OF PUBLIC API *****/
304 * Updates all final grades in course.
306 * @param int $courseid
307 * @param boolean $regradeall force regrading of all items
309 * @return boolean true if ok, array of errors if problems found
311 function grade_update_final_grades($courseid, $regradeall=false) {
313 if ($regradeall) {
314 set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
317 if (!$grade_items = grade_item::fetch_all(array('courseid'=>$courseid))) {
318 return true;
321 if (!$regradeall) {
322 $needsupdate = false;
323 $calculated = false;
324 foreach ($grade_items as $gid=>$gitem) {
325 $grade_item =& $grade_items[$gid];
326 if ($grade_item->needsupdate) {
327 $needsupdate = true;
329 if ($grade_item->is_calculated()) {
330 $calculated = true;
334 if (!$needsupdate) {
335 // no update needed
336 return true;
338 } else if ($calculated) {
339 // flag all calculated grade items with needsupdate
340 // we want to make sure all are ok, this can be improved later with proper dependency calculation
341 foreach ($grade_items as $gid=>$gitem) {
342 $grade_item =& $grade_items[$gid];
343 if (!$grade_item->is_calculated()) {
344 continue;
346 $grade_item->update_from_db(); // make sure we have current data, it might have been updated in this loop already
347 if (!$grade_item->needsupdate) {
348 //force recalculation and forced update of all parents
349 $grade_item->force_regrading();
353 // again make sure all date is up-to-date - the needsupdate flag might have changed
354 foreach ($grade_items as $gid=>$gitem) {
355 $grade_item =& $grade_items[$gid];
356 $grade_item->update_from_db();
357 unset($grade_item->category);
363 $errors = array();
365 // now the hard way with calculated grade_items or categories
366 $finalitems = array();
367 $finalids = array();
368 while (count($grade_items) > 0) {
369 $count = 0;
370 foreach ($grade_items as $gid=>$gitem) {
371 $grade_item =& $grade_items[$gid];
372 if (!$grade_item->needsupdate) {
373 $finalitems[$gid] = $grade_item;
374 $finalids[] = $gid;
375 unset($grade_items[$gid]);
376 continue;
379 //do we have all data for finalizing of this item?
380 $depends_on = $grade_item->depends_on();
382 $doupdate = true;
383 foreach ($depends_on as $did) {
384 if (!in_array($did, $finalids)) {
385 $doupdate = false;
389 //oki - let's update, calculate or aggregate :-)
390 if ($doupdate) {
391 $result = $grade_item->update_final_grades();
392 if ($result !== true) {
393 $errors = array_merge($errors, $result);
394 } else {
395 $finalitems[$gid] = $grade_item;
396 $finalids[] = $gid;
397 unset($grade_items[$gid]);
402 if ($count == 0) {
403 foreach($grade_items as $grade_item) {
404 $errors[] = 'Probably circular reference or broken calculation formula in grade_item id:'.$grade_item->id; // TODO: localize
406 break;
410 if (count($errors) == 0) {
411 return true;
412 } else {
413 return $errors;
418 * For backwards compatibility with old third-party modules, this function can
419 * be used to import all grades from activities with legacy grading.
420 * @param int $courseid or null if all courses
422 function grade_grab_legacy_grades($courseid=null) {
424 global $CFG;
426 if (!$mods = get_list_of_plugins('mod') ) {
427 error('No modules installed!');
430 if ($courseid) {
431 $course_sql = " AND cm.course=$courseid";
432 } else {
433 $course_sql = "";
436 foreach ($mods as $mod) {
438 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
439 continue;
442 if (!$module = get_record('modules', 'name', $mod)) {
443 //not installed
444 continue;
447 if (!$module->visible) {
448 //disabled module
449 continue;
452 $fullmod = $CFG->dirroot.'/mod/'.$mod;
454 // include the module lib once
455 if (file_exists($fullmod.'/lib.php')) {
456 include_once($fullmod.'/lib.php');
457 // look for modname_grades() function - old gradebook pulling function
458 // if present sync the grades with new grading system
459 $gradefunc = $mod.'_grades';
460 if (function_exists($gradefunc)) {
462 // get all instance of the activity
463 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
464 FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
465 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id $course_sql";
467 if ($modinstances = get_records_sql($sql)) {
468 foreach ($modinstances as $modinstance) {
469 grade_update_mod_grades($modinstance);
478 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
480 function grade_grab_grades() {
482 global $CFG;
484 if (!$mods = get_list_of_plugins('mod') ) {
485 error('No modules installed!');
488 foreach ($mods as $mod) {
490 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
491 continue;
494 if (!$module = get_record('modules', 'name', $mod)) {
495 //not installed
496 continue;
499 if (!$module->visible) {
500 //disabled module
501 continue;
504 $fullmod = $CFG->dirroot.'/mod/'.$mod;
506 // include the module lib once
507 if (file_exists($fullmod.'/lib.php')) {
508 include_once($fullmod.'/lib.php');
509 // look for modname_grades() function - old gradebook pulling function
510 // if present sync the grades with new grading system
511 $gradefunc = $mod.'_update_grades';
512 if (function_exists($gradefunc)) {
513 $gradefunc();
520 * Force full update of module grades in central gradebook - works for both legacy and converted activities.
521 * @param object $modinstance object with extra cmidnumber and modname property
522 * @return boolean success
524 function grade_update_mod_grades($modinstance) {
525 global $CFG;
527 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
528 if (!file_exists($fullmod.'/lib.php')) {
529 debugging('missing lib.php file in module');
530 return false;
532 include_once($fullmod.'/lib.php');
534 // does it use legacy grading?
535 $gradefunc = $modinstance->modname.'_grades';
536 $updategradesfunc = $modinstance->modname.'_update_grades';
537 $updateitemfunc = $modinstance->modname.'_grade_item_update';
539 if (function_exists($gradefunc)) {
540 if ($oldgrades = $gradefunc($modinstance->id)) {
542 $grademax = $oldgrades->maxgrade;
543 $scaleid = NULL;
544 if (!is_numeric($grademax)) {
545 // scale name is provided as a string, try to find it
546 if (!$scale = get_record('scale', 'name', $grademax)) {
547 debugging('Incorrect scale name! name:'.$grademax);
548 return false;
550 $scaleid = $scale->id;
553 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
554 debugging('Can not get/create legacy grade item!');
555 return false;
558 $grades = array();
559 foreach ($oldgrades->grades as $userid=>$usergrade) {
560 $grade = new object();
561 $grade->userid = $userid;
563 if ($usergrade == '-') {
564 // no grade
565 $grade->rawgrade = null;
567 } else if ($scaleid) {
568 // scale in use, words used
569 $gradescale = explode(",", $scale->scale);
570 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
572 } else {
573 // good old numeric value
574 $grade->rawgrade = $usergrade;
576 $grades[] = $grade;
579 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
580 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
583 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
584 //new grading supported, force updating of grades
585 $updateitemfunc($modinstance);
586 $updategradesfunc($modinstance);
588 } else {
589 // mudule does not support grading
592 return true;
596 * Get and update/create grade item for legacy modules.
598 function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
600 // does it already exist?
601 if ($grade_items = grade_grades::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>0))) {
602 if (count($grade_items) > 1) {
603 debugging('Multiple legacy grade_items found.');
604 return false;
607 $grade_item = reset($grade_items);
609 if (is_null($grademax) and is_null($scaleid)) {
610 $grade_item->gradetype = GRADE_TYPE_NONE;
612 } else if ($scaleid) {
613 $grade_item->gradetype = GRADE_TYPE_SCALE;
614 $grade_item->scaleid = $scaleid;
615 $grade_item->grademin = 1;
617 } else {
618 $grade_item->gradetype = GRADE_TYPE_VALUE;
619 $grade_item->grademax = $grademax;
620 $grade_item->grademin = 0;
623 $grade_item->itemname = $modinstance->name;
624 $grade_item->idnumber = $modinstance->cmidnumber;
626 $grade_item->update();
628 return $grade_item;
631 // create new one
632 $params = array('courseid' =>$modinstance->course,
633 'itemtype' =>'mod',
634 'itemmodule' =>$modinstance->modname,
635 'iteminstance'=>$modinstance->id,
636 'itemnumber' =>0,
637 'itemname' =>$modinstance->name,
638 'idnumber' =>$modinstance->cmidnumber);
640 if (is_null($grademax) and is_null($scaleid)) {
641 $params['gradetype'] = GRADE_TYPE_NONE;
643 } else if ($scaleid) {
644 $params['gradetype'] = GRADE_TYPE_SCALE;
645 $params['scaleid'] = $scaleid;
646 $grade_item->grademin = 1;
647 } else {
648 $params['gradetype'] = GRADE_TYPE_VALUE;
649 $params['grademax'] = $grademax;
650 $params['grademin'] = 0;
653 $grade_item = new grade_item($params);
654 $grade_item->insert();
656 return $grade_item;
660 * This function is used to migrade old date and settings from old gradebook into new grading system.
662 * TODO:
663 * - category weight not used - we would have to create extra top course grade calculated category
664 * - exta_credit item flag not used - does not fit all our aggregation types, could be used in SUM only
666 function grade_oldgradebook_upgrade($courseid) {
667 global $CFG;
669 $categories = array();
670 if ($oldcats = get_records('grade_category', 'courseid', $courseid)) {
671 foreach ($oldcats as $oldcat) {
672 $newcat = new grade_category(array('courseid'=>$courseid, 'fullname'=>$oldcat->name));
673 $newcat->droplow = $oldcat->drop_x_lowest;
674 $newcat->aggregation = GRADE_AGGREGATE_MEAN_GRADED;
676 if (empty($newcat->id)) {
677 $newcat->insert();
678 } else {
679 $newcat->update();
682 $categories[$oldcat->id] = $newcat;
684 $catitem = $newcat->get_grade_item();
685 $catitem->gradetype = GRADE_TYPE_VALUE;
686 $catitem->plusfactor = $oldcat->bonus_points;
687 $catitem->hidden = $oldcat->hidden;
688 $catitem->update();
692 // get all grade items with mod details
693 $sql = "SELECT gi.*, cm.idnumber as cmidnumber, m.name as modname
694 FROM {$CFG->prefix}grade_item gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
695 WHERE gi.courseid=$courseid AND m.id=gi.modid AND cm.instance=gi.cminstance
696 ORDER BY gi.sortorder ASC";
698 if ($olditems = get_records_sql($sql)) {
699 foreach ($olditems as $olditem) {
700 $newitem = new grade_item(array('courseid'=>$olditem->courseid, 'itemtype'=>'mod', 'itemmodule'=>$olditem->modname, 'iteminstance'=>$olditem->cminstance, 'itemnumber'=>0));
701 if (!empty($olditem->category)) {
702 // we do this low level stuff to get some speedup during upgrade
703 $newitem->set_parent($categories[$olditem->category]->id);
705 $newitem->gradetype = GRADE_TYPE_NONE;
706 $newitem->multfactor = $olditem->scale_grade;
707 if (empty($newitem->id)) {
708 $newitem->insert();
709 } else {
710 $newitem->update();
717 * Given a grade_category, grade_item or grade_grade, this function
718 * figures out the state of the object and builds then returns a div
719 * with the icons needed for the grader report.
721 * @param object $object
722 * @param object $tree (A complete grade_tree object)
723 * @return string HTML
725 function grade_get_icons($element, $tree) {
726 global $CFG;
727 global $USER;
729 // Load language strings
730 $straddfeedback = get_string("addfeedback", 'grades');
731 $stredit = get_string("edit");
732 $streditfeedback = get_string("editfeedback", 'grades');
733 $streditcalculation= get_string("editcalculation", 'grades');
734 $strfeedback = get_string("feedback");
735 $strmove = get_string("move");
736 $strmoveup = get_string("moveup");
737 $strmovedown = get_string("movedown");
738 $strmovehere = get_string("movehere");
739 $strcancel = get_string("cancel");
740 $stredit = get_string("edit");
741 $strdelete = get_string("delete");
742 $strhide = get_string("hide");
743 $strshow = get_string("show");
744 $strlock = get_string("lock", 'grades');
745 $strswitch_minus = get_string("contract", 'grades');
746 $strswitch_plus = get_string("expand", 'grades');
747 $strunlock = get_string("unlock", 'grades');
749 // Prepare container div
750 $html = '<div class="grade_icons">';
752 // Prepare reference variables
753 $eid = $element['eid'];
754 $object = $element['object'];
755 $type = $element['type'];
757 // Load user preferences
758 $aggregationview = get_user_preferences('grade_report_aggregationview', $CFG->grade_report_aggregationview);
760 // Icons shown when edit mode is on
761 if ($USER->gradeediting) {
762 // Edit icon (except for grade_grades)
763 if ($type == 'category') {
764 $html .= '<a href="report/grader/edit_category.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
765 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
766 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
768 } else if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
769 $html .= '<a href="report/grader/edit_item.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
770 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
771 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
773 } else if ($type == 'grade') {
774 // What is the purpose of edit_grade page?
776 $html .= '<a href="report/grader/edit_grade.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
777 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
778 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
782 // Calculation icon for items and categories
783 if ($type != 'grade') {
784 $html .= '<a href="report/grader/edit_calculation.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
785 $html .= '<img src="'.$CFG->pixpath.'/t/calc.gif" class="iconsmall" alt="'
786 .$streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
789 // Prepare Hide/Show icon state
790 $hide_show = 'hide';
791 if ($object->is_hidden()) {
792 $hide_show = 'show';
795 // Setup object identifier and show feedback icon if applicable
796 if ($type == 'grade' and $USER->gradefeedback) {
797 // Display Edit/Add feedback icon
798 if (empty($object->feedback)) {
799 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
800 . "&amp;action=add&amp;courseid=$object->courseid\">\n";
801 $html .= '<img src="'.$CFG->pixpath.'/t/feedback_add.gif" class="iconsmall" alt="'.$straddfeedback.'" '
802 . 'title="'.$straddfeedback.'" /></a>'. "\n";
803 } else {
804 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
805 . "&amp;action=edit&amp;courseid=$object->courseid\">\n";
806 $html .= '<img src="'.$CFG->pixpath.'/t/feedback.gif" class="iconsmall" alt="'.$streditfeedback.'" '
807 . 'title="'.$streditfeedback.'" onmouseover="return overlib(\''.$object->feedback.'\', CAPTION, \''
808 . $strfeedback.'\');" onmouseout="return nd();" /></a>'. "\n";
812 // Display Hide/Show icon
813 $html .= '<a href="report.php?report=grader&amp;target='.$eid
814 . "&amp;action=$hide_show$tree->commonvars\">\n";
815 $html .= '<img src="'.$CFG->pixpath.'/t/'.$hide_show.'.gif" class="iconsmall" alt="'
816 .${'str' . $hide_show}.'" title="'.${'str' . $hide_show}.'" /></a>'. "\n";
818 // Prepare lock/unlock string
819 $lock_unlock = 'lock';
820 if ($object->is_locked()) {
821 $lock_unlock = 'unlock';
824 // Print lock/unlock icon
825 $html .= '<a href="report.php?report=grader&amp;target='.$eid
826 . "&amp;action=$lock_unlock$tree->commonvars\">\n";
827 $html .= '<img src="'.$CFG->pixpath.'/t/'.$lock_unlock.'.gif" class="iconsmall" alt="'
828 .${'str' . $lock_unlock}.'" title="'.${'str' . $lock_unlock}.'" /></a>'. "\n";
830 // If object is a category, display expand/contract icon
831 if (get_class($object) == 'grade_category' && $aggregationview == GRADER_REPORT_AGGREGATION_VIEW_COMPACT) {
832 $expand_contract = 'switch_minus'; // Default: expanded
834 $state = get_user_preferences('grade_category_' . $object->id, GRADE_CATEGORY_EXPANDED);
836 if ($state == GRADE_CATEGORY_CONTRACTED) {
837 $expand_contract = 'switch_plus';
840 $html .= '<a href="report.php?report=grader&amp;target=' . $eid
841 . "&amp;action=$expand_contract$tree->commonvars\">\n";
842 $html .= '<img src="'.$CFG->pixpath.'/t/'.$expand_contract.'.gif" class="iconsmall" alt="'
843 .${'str' . $expand_contract}.'" title="'.${'str' . $expand_contract}.'" /></a>'. "\n";
845 } else {
846 if ($USER->gradefeedback) {
847 // Display view feedback icon
848 if (!empty($object->feedback)) {
849 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
850 . "&amp;action=view&amp;courseid=$object->courseid\">\n";
851 $html .= '<img onmouseover="return overlib(\''.$object->feedback.'\', CAPTION, \''
852 . $strfeedback.'\');" onmouseout="return nd();" '
853 . 'src="'.$CFG->pixpath.'/t/feedback.gif" class="iconsmall" alt="" /></a>'. "\n";
858 return $html . '</div>';