MDL-9268
[moodle.git] / lib / gradelib.php
CommitLineData
5834dcdb 1<?php // $Id$
2
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///////////////////////////////////////////////////////////////////////////
25
26/**
42bbccd7 27 * Library of functions for gradebook
5834dcdb 28 *
29 * @author Moodle HQ developers
30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package moodlecore
33 */
34
b8ff92b6 35define('GRADE_AGGREGATE_MEAN_ALL', 0);
42bbccd7 36define('GRADE_AGGREGATE_MEDIAN', 1);
b8ff92b6 37define('GRADE_AGGREGATE_MEAN_GRADED', 2);
38define('GRADE_AGGREGATE_MIN', 3);
39define('GRADE_AGGREGATE_MAX', 4);
95affb8a 40define('GRADE_AGGREGATE_MODE', 5);
612607bd 41
27f95e9b 42define('GRADE_CHILDTYPE_ITEM', 0);
43define('GRADE_CHILDTYPE_CAT', 1);
612607bd 44
27f95e9b 45define('GRADE_ITEM', 0); // Used to compare class names with CHILDTYPE values
46define('GRADE_CATEGORY', 1); // Used to compare class names with CHILDTYPE values
612607bd 47
8ace7bf7 48define('GRADE_CATEGORY_CONTRACTED', 0); // The state of a category header in the grader report
49define('GRADE_CATEGORY_EXPANDED', 1); // The state of a category header in the grader report
50
210611f6 51define('GRADE_TYPE_NONE', 0);
52define('GRADE_TYPE_VALUE', 1);
53define('GRADE_TYPE_SCALE', 2);
54define('GRADE_TYPE_TEXT', 3);
42bbccd7 55
612607bd 56define('GRADE_UPDATE_OK', 0);
57define('GRADE_UPDATE_FAILED', 1);
58define('GRADE_UPDATE_MULTIPLE', 2);
59define('GRADE_UPDATE_ITEM_DELETED', 3);
4cf1b9be 60define('GRADE_UPDATE_ITEM_LOCKED', 4);
612607bd 61
aaff71da 62// Grate teables history tracking actions
63define('GRADE_HISTORY_INSERT', 1);
64define('GRADE_HISTORY_UPDATE', 2);
65define('GRADE_HISTORY_DELETE', 3);
66
13b10724 67// Set up constants for report preferences
68define('GRADER_REPORT_AGGREGATION_POSITION_LEFT', 0);
69define('GRADER_REPORT_AGGREGATION_POSITION_RIGHT', 1);
70define('GRADER_REPORT_AGGREGATION_VIEW_FULL', 0);
71define('GRADER_REPORT_AGGREGATION_VIEW_COMPACT', 1);
72define('GRADER_REPORT_GRADE_DISPLAY_TYPE_RAW', 0);
73define('GRADER_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE', 1);
74define('GRADER_REPORT_FEEDBACK_FORMAT_TEXT', 0);
75define('GRADER_REPORT_FEEDBACK_FORMAT_HTML', 1);
76
612607bd 77
3058964f 78require_once($CFG->libdir . '/grade/grade_category.php');
79require_once($CFG->libdir . '/grade/grade_item.php');
ac9b0805 80require_once($CFG->libdir . '/grade/grade_grades.php');
d5bdb228 81require_once($CFG->libdir . '/grade/grade_scale.php');
5501446d 82require_once($CFG->libdir . '/grade/grade_outcome.php');
46566dd8 83require_once($CFG->libdir . '/grade/grade_grades_text.php');
8ff4550a 84require_once($CFG->libdir . '/grade/grade_tree.php');
60cf7430 85
612607bd 86/***** PUBLIC GRADE API *****/
87
c5b5f18d 88/**
89 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
ac9b0805 90 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
c5b5f18d 91 * or key means do not change existing.
4cf1b9be 92 *
c5b5f18d 93 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
e648f890 94 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
4cf1b9be 95 *
c5b5f18d 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
104 */
b67ec72f 105function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
aaff71da 106 global $USER;
612607bd 107
c5b5f18d 108 // only following grade_item properties can be changed in this function
e648f890 109 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
612607bd 110
c4e4068f 111 // grade item identification
112 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
113
612607bd 114 if (is_null($courseid) or is_null($itemtype)) {
115 debugging('Missing courseid or itemtype');
116 return GRADE_UPDATE_FAILED;
117 }
118
c4e4068f 119 if (!$grade_items = grade_item::fetch_all($params)) {
612607bd 120 // create a new one
121 $grade_item = false;
122
123 } else if (count($grade_items) == 1){
124 $grade_item = reset($grade_items);
125 unset($grade_items); //release memory
126
127 } else {
34e67f76 128 debugging('Found more than one grade item');
612607bd 129 return GRADE_UPDATE_MULTIPLE;
130 }
131
aaff71da 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;
138 }
139 }
140 return GRADE_UPDATE_OK;
141 }
142
612607bd 143/// Create or update the grade_item if needed
144 if (!$grade_item) {
612607bd 145 if ($itemdetails) {
146 $itemdetails = (array)$itemdetails;
2e53372c 147
772ddfbf 148 // grademin and grademax ignored when scale specified
2e53372c 149 if (array_key_exists('scaleid', $itemdetails)) {
150 if ($itemdetails['scaleid']) {
151 unset($itemdetails['grademin']);
152 unset($itemdetails['grademax']);
153 }
154 }
155
612607bd 156 foreach ($itemdetails as $k=>$v) {
157 if (!in_array($k, $allowed)) {
158 // ignore it
159 continue;
160 }
161 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
162 // no grade item needed!
163 return GRADE_UPDATE_OK;
164 }
165 $params[$k] = $v;
166 }
167 }
f70152b7 168 $grade_item = new grade_item($params);
169 $grade_item->insert();
612607bd 170
171 } else {
2cc4b0f9 172 if ($grade_item->is_locked()) {
612607bd 173 debugging('Grading item is locked!');
4cf1b9be 174 return GRADE_UPDATE_ITEM_LOCKED;
612607bd 175 }
176
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;
184 }
185 if ($grade_item->{$k} != $v) {
186 $grade_item->{$k} = $v;
187 $update = true;
188 }
189 }
190 if ($update) {
191 $grade_item->update();
192 }
193 }
194 }
195
196/// Some extra checks
197 // do we use grading?
198 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
199 return GRADE_UPDATE_OK;
200 }
201
202 // no grade submitted
b67ec72f 203 if (empty($grades)) {
612607bd 204 return GRADE_UPDATE_OK;
205 }
206
612607bd 207/// Finally start processing of grades
b67ec72f 208 if (is_object($grades)) {
209 $grades = array($grades);
612607bd 210 } else {
b67ec72f 211 if (array_key_exists('userid', $grades)) {
212 $grades = array($grades);
612607bd 213 }
214 }
215
4cf1b9be 216 $failed = false;
612607bd 217 foreach ($grades as $grade) {
218 $grade = (array)$grade;
219 if (empty($grade['userid'])) {
4cf1b9be 220 $failed = true;
221 debugging('Invalid userid in grade submitted');
222 continue;
ac9b0805 223 } else {
224 $userid = $grade['userid'];
612607bd 225 }
226
2cc4b0f9 227 $rawgrade = false;
ac9b0805 228 $feedback = false;
229 $feedbackformat = FORMAT_MOODLE;
772ddfbf 230
ac9b0805 231 if (array_key_exists('rawgrade', $grade)) {
232 $rawgrade = $grade['rawgrade'];
233 }
612607bd 234
4cf1b9be 235 if (array_key_exists('feedback', $grade)) {
ac9b0805 236 $feedback = $grade['feedback'];
612607bd 237 }
238
ac9b0805 239 if (array_key_exists('feedbackformat', $grade)) {
240 $feedbackformat = $grade['feedbackformat'];
612607bd 241 }
242
aaff71da 243 if (array_key_exists('usermodified', $grade)) {
244 $usermodified = $grade['usermodified'];
245 } else {
246 $usermodified = $USER->id;
247 }
248
ac9b0805 249 // update or insert the grade
aaff71da 250 if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat, $usermodified)) {
4cf1b9be 251 $failed = true;
4cf1b9be 252 }
612607bd 253 }
254
4cf1b9be 255 if (!$failed) {
256 return GRADE_UPDATE_OK;
257 } else {
258 return GRADE_UPDATE_FAILED;
259 }
612607bd 260}
261
b67ec72f 262
263/**
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.
271*
34e67f76 272* @param int $courseid id of course
b67ec72f 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
34e67f76 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
b67ec72f 278* @return boolean Whether the grade is locked or not
279*/
34e67f76 280function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
b67ec72f 281
f92dcad8 282 if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) {
34e67f76 283 return false;
284
285 } else if (count($grade_items) == 1){
286 $grade_item = reset($grade_items);
287 return $grade_item->is_locked($userid);
288
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;
294 }
295 }
296 return false;
297 }
298}
b67ec72f 299
612607bd 300/***** END OF PUBLIC API *****/
301
34e67f76 302
5834dcdb 303/**
ac9b0805 304 * Updates all final grades in course.
a8995b34 305 *
306 * @param int $courseid
2e53372c 307 * @param boolean $regradeall force regrading of all items
308 *
b8ff92b6 309 * @return boolean true if ok, array of errors if problems found
a8995b34 310 */
2e53372c 311function grade_update_final_grades($courseid, $regradeall=false) {
612607bd 312
2e53372c 313 if ($regradeall) {
314 set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
b8ff92b6 315 }
a8995b34 316
f89526e0 317 if (!$grade_items = grade_item::fetch_all(array('courseid'=>$courseid))) {
b8ff92b6 318 return true;
319 }
320
2e53372c 321 if (!$regradeall) {
322 $needsupdate = false;
323 $calculated = false;
b8ff92b6 324 foreach ($grade_items as $gid=>$gitem) {
325 $grade_item =& $grade_items[$gid];
326 if ($grade_item->needsupdate) {
2e53372c 327 $needsupdate = true;
328 }
329 if ($grade_item->is_calculated()) {
330 $calculated = true;
a8995b34 331 }
332 }
772ddfbf 333
2e53372c 334 if (!$needsupdate) {
335 // no update needed
b8ff92b6 336 return true;
772ddfbf 337
2e53372c 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;
345 }
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();
350 }
772ddfbf 351 }
352
2e53372c 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);
358 }
b8ff92b6 359 }
a8995b34 360 }
361
2e53372c 362
363 $errors = array();
364
b8ff92b6 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];
2e53372c 372 if (!$grade_item->needsupdate) {
b8ff92b6 373 $finalitems[$gid] = $grade_item;
374 $finalids[] = $gid;
375 unset($grade_items[$gid]);
376 continue;
377 }
378
2e53372c 379 //do we have all data for finalizing of this item?
61c33818 380 $depends_on = $grade_item->depends_on();
b8ff92b6 381
b8ff92b6 382 $doupdate = true;
61c33818 383 foreach ($depends_on as $did) {
b8ff92b6 384 if (!in_array($did, $finalids)) {
385 $doupdate = false;
386 }
387 }
388
389 //oki - let's update, calculate or aggregate :-)
390 if ($doupdate) {
ac9b0805 391 $result = $grade_item->update_final_grades();
b8ff92b6 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]);
398 }
399 }
400 }
401
402 if ($count == 0) {
403 foreach($grade_items as $grade_item) {
2e53372c 404 $errors[] = 'Probably circular reference or broken calculation formula in grade_item id:'.$grade_item->id; // TODO: localize
b8ff92b6 405 }
406 break;
407 }
408 }
409
410 if (count($errors) == 0) {
411 return true;
412 } else {
413 return $errors;
414 }
a8995b34 415}
967f222f 416
de420c11 417/**
d185c3ee 418 * For backwards compatibility with old third-party modules, this function can
419 * be used to import all grades from activities with legacy grading.
739196ba 420 * @param int $courseid or null if all courses
967f222f 421 */
739196ba 422function grade_grab_legacy_grades($courseid=null) {
612607bd 423
ac9b0805 424 global $CFG;
967f222f 425
426 if (!$mods = get_list_of_plugins('mod') ) {
427 error('No modules installed!');
428 }
429
739196ba 430 if ($courseid) {
431 $course_sql = " AND cm.course=$courseid";
432 } else {
433 $course_sql = "";
434 }
435
967f222f 436 foreach ($mods as $mod) {
437
438 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
439 continue;
440 }
441
d185c3ee 442 if (!$module = get_record('modules', 'name', $mod)) {
443 //not installed
444 continue;
445 }
446
447 if (!$module->visible) {
448 //disabled module
449 continue;
450 }
451
452 $fullmod = $CFG->dirroot.'/mod/'.$mod;
967f222f 453
454 // include the module lib once
455 if (file_exists($fullmod.'/lib.php')) {
456 include_once($fullmod.'/lib.php');
de420c11 457 // look for modname_grades() function - old gradebook pulling function
458 // if present sync the grades with new grading system
967f222f 459 $gradefunc = $mod.'_grades';
de420c11 460 if (function_exists($gradefunc)) {
461
462 // get all instance of the activity
d185c3ee 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
739196ba 465 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id $course_sql";
de420c11 466
467 if ($modinstances = get_records_sql($sql)) {
967f222f 468 foreach ($modinstances as $modinstance) {
d185c3ee 469 grade_update_mod_grades($modinstance);
967f222f 470 }
471 }
472 }
473 }
474 }
475}
476
ac9b0805 477/**
478 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
479 */
480function grade_grab_grades() {
481
482 global $CFG;
483
484 if (!$mods = get_list_of_plugins('mod') ) {
485 error('No modules installed!');
486 }
487
488 foreach ($mods as $mod) {
489
490 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
491 continue;
492 }
493
494 if (!$module = get_record('modules', 'name', $mod)) {
495 //not installed
496 continue;
497 }
498
499 if (!$module->visible) {
500 //disabled module
501 continue;
502 }
503
504 $fullmod = $CFG->dirroot.'/mod/'.$mod;
505
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();
514 }
515 }
516 }
517}
518
d185c3ee 519/**
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
523 */
524function grade_update_mod_grades($modinstance) {
525 global $CFG;
526
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;
531 }
532 include_once($fullmod.'/lib.php');
533
534 // does it use legacy grading?
535 $gradefunc = $modinstance->modname.'_grades';
536 $updategradesfunc = $modinstance->modname.'_update_grades';
537 $updateitemfunc = $modinstance->modname.'_grade_item_update';
538
539 if (function_exists($gradefunc)) {
540 if ($oldgrades = $gradefunc($modinstance->id)) {
541
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;
549 }
550 $scaleid = $scale->id;
551 }
552
553 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
554 debugging('Can not get/create legacy grade item!');
555 return false;
556 }
557
558 $grades = array();
559 foreach ($oldgrades->grades as $userid=>$usergrade) {
560 $grade = new object();
561 $grade->userid = $userid;
562
563 if ($usergrade == '-') {
564 // no grade
ac9b0805 565 $grade->rawgrade = null;
d185c3ee 566
567 } else if ($scaleid) {
568 // scale in use, words used
569 $gradescale = explode(",", $scale->scale);
ac9b0805 570 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
d185c3ee 571
572 } else {
573 // good old numeric value
ac9b0805 574 $grade->rawgrade = $usergrade;
d185c3ee 575 }
576 $grades[] = $grade;
577 }
578
579 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
580 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
581 }
582
583 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
584 //new grading supported, force updating of grades
585 $updateitemfunc($modinstance);
586 $updategradesfunc($modinstance);
587
588 } else {
589 // mudule does not support grading
590 }
591
592 return true;
593}
de420c11 594
595/**
d185c3ee 596 * Get and update/create grade item for legacy modules.
de420c11 597 */
598function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
599
600 // does it already exist?
f92dcad8 601 if ($grade_items = grade_grades::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>0))) {
de420c11 602 if (count($grade_items) > 1) {
d185c3ee 603 debugging('Multiple legacy grade_items found.');
de420c11 604 return false;
605 }
606
607 $grade_item = reset($grade_items);
de420c11 608
d185c3ee 609 if (is_null($grademax) and is_null($scaleid)) {
610 $grade_item->gradetype = GRADE_TYPE_NONE;
de420c11 611
d185c3ee 612 } else if ($scaleid) {
613 $grade_item->gradetype = GRADE_TYPE_SCALE;
614 $grade_item->scaleid = $scaleid;
97d608ba 615 $grade_item->grademin = 1;
de420c11 616
d185c3ee 617 } else {
97d608ba 618 $grade_item->gradetype = GRADE_TYPE_VALUE;
619 $grade_item->grademax = $grademax;
620 $grade_item->grademin = 0;
de420c11 621 }
622
d185c3ee 623 $grade_item->itemname = $modinstance->name;
624 $grade_item->idnumber = $modinstance->cmidnumber;
de420c11 625
d185c3ee 626 $grade_item->update();
de420c11 627
628 return $grade_item;
629 }
612607bd 630
de420c11 631 // create new one
d185c3ee 632 $params = array('courseid' =>$modinstance->course,
de420c11 633 'itemtype' =>'mod',
634 'itemmodule' =>$modinstance->modname,
635 'iteminstance'=>$modinstance->id,
d185c3ee 636 'itemnumber' =>0,
de420c11 637 'itemname' =>$modinstance->name,
638 'idnumber' =>$modinstance->cmidnumber);
639
d185c3ee 640 if (is_null($grademax) and is_null($scaleid)) {
641 $params['gradetype'] = GRADE_TYPE_NONE;
642
643 } else if ($scaleid) {
612607bd 644 $params['gradetype'] = GRADE_TYPE_SCALE;
de420c11 645 $params['scaleid'] = $scaleid;
b3ac6c3e 646 $grade_item->grademin = 1;
de420c11 647 } else {
612607bd 648 $params['gradetype'] = GRADE_TYPE_VALUE;
de420c11 649 $params['grademax'] = $grademax;
650 $params['grademin'] = 0;
651 }
652
f70152b7 653 $grade_item = new grade_item($params);
654 $grade_item->insert();
de420c11 655
f70152b7 656 return $grade_item;
de420c11 657}
658
b8ff92b6 659/**
660 * This function is used to migrade old date and settings from old gradebook into new grading system.
661 *
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
665 */
666function grade_oldgradebook_upgrade($courseid) {
667 global $CFG;
668
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;
675
676 if (empty($newcat->id)) {
677 $newcat->insert();
678 } else {
679 $newcat->update();
680 }
681
682 $categories[$oldcat->id] = $newcat;
683
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();
689 }
690 }
691
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";
697
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
f13002d5 703 $newitem->set_parent($categories[$olditem->category]->id);
b8ff92b6 704 }
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();
711 }
712 }
713 }
714}
bfe7297e 715
8178f34c 716/**
b3ac6c3e 717 * Given a grade_category, grade_item or grade_grade, this function
8178f34c 718 * figures out the state of the object and builds then returns a div
719 * with the icons needed for the grader report.
720 *
721 * @param object $object
0a1ef2f2 722 * @param object $tree (A complete grade_tree object)
8178f34c 723 * @return string HTML
724 */
f13002d5 725function grade_get_icons($element, $tree) {
8178f34c 726 global $CFG;
1ec74ea5 727 global $USER;
aaff71da 728
13b10724 729 // Load language strings
1ec74ea5 730 $straddfeedback = get_string("addfeedback", 'grades');
8178f34c 731 $stredit = get_string("edit");
1ec74ea5 732 $streditfeedback = get_string("editfeedback", 'grades');
d2e87c0c 733 $streditcalculation= get_string("editcalculation", 'grades');
1ec74ea5 734 $strfeedback = get_string("feedback");
8178f34c 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');
8ace7bf7 745 $strswitch_minus = get_string("contract", 'grades');
746 $strswitch_plus = get_string("expand", 'grades');
8178f34c 747 $strunlock = get_string("unlock", 'grades');
748
13b10724 749 // Prepare container div
0a1ef2f2 750 $html = '<div class="grade_icons">';
b3ac6c3e 751
13b10724 752 // Prepare reference variables
f13002d5 753 $eid = $element['eid'];
754 $object = $element['object'];
755 $type = $element['type'];
756
aaff71da 757 // Load user preferences
9aa1e448 758 $aggregationview = get_user_preferences('grade_report_aggregationview', $CFG->grade_report_aggregationview);
759 $showeyecons = get_user_preferences('grade_report_showeyecons', $CFG->grade_report_showeyecons);
760 $showlocks = get_user_preferences('grade_report_showlocks', $CFG->grade_report_showlocks);
761 $shownotes = get_user_preferences('grade_report_notes', $CFG->grade_report_notes);
762 $showcalculations = get_user_preferences('grade_report_showcalculations', $CFG->grade_report_showcalculations);
13b10724 763
1ec74ea5 764 // Icons shown when edit mode is on
765 if ($USER->gradeediting) {
766 // Edit icon (except for grade_grades)
f13002d5 767 if ($type == 'category') {
768 $html .= '<a href="report/grader/edit_category.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
769 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
770 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
739196ba 771
772 } else if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
f13002d5 773 $html .= '<a href="report/grader/edit_item.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
1ec74ea5 774 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
775 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
739196ba 776
777 } else if ($type == 'grade') {
d2e87c0c 778 // What is the purpose of edit_grade page?
779 /*
739196ba 780 $html .= '<a href="report/grader/edit_grade.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
781 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
782 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
9aa1e448 783 */
d2e87c0c 784 }
9aa1e448 785
786 /* Not sure if there is a good reason to have a calculation icon: the calculation field is in the grade_edit form */
787 /*
d2e87c0c 788 // Calculation icon for items and categories
789 if ($type != 'grade') {
790 $html .= '<a href="report/grader/edit_calculation.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
791 $html .= '<img src="'.$CFG->pixpath.'/t/calc.gif" class="iconsmall" alt="'
9aa1e448 792 .$streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
793 }
794 */
795
796 if ($shownotes) {
797 // Setup object identifier and show feedback icon if applicable
798 if ($type == 'grade' and $USER->gradefeedback) {
799 // Display Edit/Add feedback icon
800 if (empty($object->feedback)) {
801 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
802 . "&amp;action=add&amp;courseid=$object->courseid\">\n";
803 $html .= '<img src="'.$CFG->pixpath.'/t/feedback_add.gif" class="iconsmall" alt="'.$straddfeedback.'" '
804 . 'title="'.$straddfeedback.'" /></a>'. "\n";
805 } else {
806 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
807 . "&amp;action=edit&amp;courseid=$object->courseid\">\n";
808 $html .= '<img src="'.$CFG->pixpath.'/t/feedback.gif" class="iconsmall" alt="'.$streditfeedback.'" '
809 . 'title="'.$streditfeedback.'" onmouseover="return overlib(\''.$object->feedback.'\', CAPTION, \''
810 . $strfeedback.'\');" onmouseout="return nd();" /></a>'. "\n";
811 }
812 }
1ec74ea5 813 }
0a1ef2f2 814
9aa1e448 815 if ($showeyecons) {
b3ac6c3e 816
9aa1e448 817 // Prepare Hide/Show icon state
818 $hide_show = 'hide';
819 if ($object->is_hidden()) {
820 $hide_show = 'show';
1ec74ea5 821 }
9aa1e448 822
823 // Display Hide/Show icon
824 $html .= '<a href="report.php?report=grader&amp;target='.$eid
825 . "&amp;action=$hide_show$tree->commonvars\">\n";
826 $html .= '<img src="'.$CFG->pixpath.'/t/'.$hide_show.'.gif" class="iconsmall" alt="'
827 .${'str' . $hide_show}.'" title="'.${'str' . $hide_show}.'" /></a>'. "\n";
1ec74ea5 828 }
8178f34c 829
9aa1e448 830 if ($showlocks) {
831 // Prepare lock/unlock string
832 $lock_unlock = 'lock';
833 if ($object->is_locked()) {
834 $lock_unlock = 'unlock';
835 }
8178f34c 836
9aa1e448 837 // Print lock/unlock icon
838 $html .= '<a href="report.php?report=grader&amp;target='.$eid
839 . "&amp;action=$lock_unlock$tree->commonvars\">\n";
840 $html .= '<img src="'.$CFG->pixpath.'/t/'.$lock_unlock.'.gif" class="iconsmall" alt="'
841 .${'str' . $lock_unlock}.'" title="'.${'str' . $lock_unlock}.'" /></a>'. "\n";
1ec74ea5 842 }
b3ac6c3e 843
8ace7bf7 844 // If object is a category, display expand/contract icon
13b10724 845 if (get_class($object) == 'grade_category' && $aggregationview == GRADER_REPORT_AGGREGATION_VIEW_COMPACT) {
8ace7bf7 846 $expand_contract = 'switch_minus'; // Default: expanded
1ec74ea5 847
8ace7bf7 848 $state = get_user_preferences('grade_category_' . $object->id, GRADE_CATEGORY_EXPANDED);
b3ac6c3e 849
8ace7bf7 850 if ($state == GRADE_CATEGORY_CONTRACTED) {
851 $expand_contract = 'switch_plus';
852 }
853
f13002d5 854 $html .= '<a href="report.php?report=grader&amp;target=' . $eid
8ace7bf7 855 . "&amp;action=$expand_contract$tree->commonvars\">\n";
856 $html .= '<img src="'.$CFG->pixpath.'/t/'.$expand_contract.'.gif" class="iconsmall" alt="'
857 .${'str' . $expand_contract}.'" title="'.${'str' . $expand_contract}.'" /></a>'. "\n";
1ec74ea5 858 }
859 } else {
9aa1e448 860 if ($shownotes) {
657e52d4 861 // Display view feedback icon
1ec74ea5 862 if (!empty($object->feedback)) {
aaff71da 863 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
657e52d4 864 . "&amp;action=view&amp;courseid=$object->courseid\">\n";
1ec74ea5 865 $html .= '<img onmouseover="return overlib(\''.$object->feedback.'\', CAPTION, \''
b3ac6c3e 866 . $strfeedback.'\');" onmouseout="return nd();" '
1ec74ea5 867 . 'src="'.$CFG->pixpath.'/t/feedback.gif" class="iconsmall" alt="" /></a>'. "\n";
b3ac6c3e 868 }
1ec74ea5 869 }
0a1ef2f2 870 }
8178f34c 871
0a1ef2f2 872 return $html . '</div>';
8178f34c 873}
1ec74ea5 874
60cf7430 875?>