MDL-10364 Added calculation icon, changed customdata to set_data for feedback form.
[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
13b10724 62// Set up constants for report preferences
63define('GRADER_REPORT_AGGREGATION_POSITION_LEFT', 0);
64define('GRADER_REPORT_AGGREGATION_POSITION_RIGHT', 1);
65define('GRADER_REPORT_AGGREGATION_VIEW_FULL', 0);
66define('GRADER_REPORT_AGGREGATION_VIEW_COMPACT', 1);
67define('GRADER_REPORT_GRADE_DISPLAY_TYPE_RAW', 0);
68define('GRADER_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE', 1);
69define('GRADER_REPORT_FEEDBACK_FORMAT_TEXT', 0);
70define('GRADER_REPORT_FEEDBACK_FORMAT_HTML', 1);
71
612607bd 72
3058964f 73require_once($CFG->libdir . '/grade/grade_category.php');
74require_once($CFG->libdir . '/grade/grade_item.php');
ac9b0805 75require_once($CFG->libdir . '/grade/grade_grades.php');
d5bdb228 76require_once($CFG->libdir . '/grade/grade_scale.php');
5501446d 77require_once($CFG->libdir . '/grade/grade_outcome.php');
46566dd8 78require_once($CFG->libdir . '/grade/grade_history.php');
79require_once($CFG->libdir . '/grade/grade_grades_text.php');
8ff4550a 80require_once($CFG->libdir . '/grade/grade_tree.php');
60cf7430 81
612607bd 82/***** PUBLIC GRADE API *****/
83
c5b5f18d 84/**
85 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
ac9b0805 86 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
c5b5f18d 87 * or key means do not change existing.
4cf1b9be 88 *
c5b5f18d 89 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
e648f890 90 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
4cf1b9be 91 *
c5b5f18d 92 * @param string $source source of the grade such as 'mod/assignment', often used to prevent infinite loops when processing grade_updated events
93 * @param int $courseid id of course
94 * @param string $itemtype type of grade item - mod, block, gradecategory, calculated
95 * @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
96 * @param int $iteminstance instance it of graded subject
97 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
98 * @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating rgade_item definition only\
99 * @param mixed $itemdetails object or array describing the grading item, NULL if no change
100 */
b67ec72f 101function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
612607bd 102
c5b5f18d 103 // only following grade_item properties can be changed in this function
e648f890 104 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
612607bd 105
c4e4068f 106 // grade item identification
107 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
108
612607bd 109 if (is_null($courseid) or is_null($itemtype)) {
110 debugging('Missing courseid or itemtype');
111 return GRADE_UPDATE_FAILED;
112 }
113
c4e4068f 114 if (!$grade_items = grade_item::fetch_all($params)) {
612607bd 115 // create a new one
116 $grade_item = false;
117
118 } else if (count($grade_items) == 1){
119 $grade_item = reset($grade_items);
120 unset($grade_items); //release memory
121
122 } else {
34e67f76 123 debugging('Found more than one grade item');
612607bd 124 return GRADE_UPDATE_MULTIPLE;
125 }
126
127/// Create or update the grade_item if needed
128 if (!$grade_item) {
612607bd 129 if ($itemdetails) {
130 $itemdetails = (array)$itemdetails;
2e53372c 131
772ddfbf 132 // grademin and grademax ignored when scale specified
2e53372c 133 if (array_key_exists('scaleid', $itemdetails)) {
134 if ($itemdetails['scaleid']) {
135 unset($itemdetails['grademin']);
136 unset($itemdetails['grademax']);
137 }
138 }
139
612607bd 140 foreach ($itemdetails as $k=>$v) {
141 if (!in_array($k, $allowed)) {
142 // ignore it
143 continue;
144 }
145 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
146 // no grade item needed!
147 return GRADE_UPDATE_OK;
148 }
149 $params[$k] = $v;
150 }
151 }
f70152b7 152 $grade_item = new grade_item($params);
153 $grade_item->insert();
612607bd 154
155 } else {
2cc4b0f9 156 if ($grade_item->is_locked()) {
612607bd 157 debugging('Grading item is locked!');
4cf1b9be 158 return GRADE_UPDATE_ITEM_LOCKED;
612607bd 159 }
160
161 if ($itemdetails) {
162 $itemdetails = (array)$itemdetails;
163 $update = false;
164 foreach ($itemdetails as $k=>$v) {
165 if (!in_array($k, $allowed)) {
166 // ignore it
167 continue;
168 }
169 if ($grade_item->{$k} != $v) {
170 $grade_item->{$k} = $v;
171 $update = true;
172 }
173 }
174 if ($update) {
175 $grade_item->update();
176 }
177 }
178 }
179
180/// Some extra checks
181 // do we use grading?
182 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
183 return GRADE_UPDATE_OK;
184 }
185
186 // no grade submitted
b67ec72f 187 if (empty($grades)) {
612607bd 188 return GRADE_UPDATE_OK;
189 }
190
191 // no grading in deleted items
192 if ($grade_item->deleted) {
193 debugging('Grade item was already deleted!');
194 return GRADE_UPDATE_ITEM_DELETED;
195 }
196
197/// Finally start processing of grades
b67ec72f 198 if (is_object($grades)) {
199 $grades = array($grades);
612607bd 200 } else {
b67ec72f 201 if (array_key_exists('userid', $grades)) {
202 $grades = array($grades);
612607bd 203 }
204 }
205
4cf1b9be 206 $failed = false;
612607bd 207 foreach ($grades as $grade) {
208 $grade = (array)$grade;
209 if (empty($grade['userid'])) {
4cf1b9be 210 $failed = true;
211 debugging('Invalid userid in grade submitted');
212 continue;
ac9b0805 213 } else {
214 $userid = $grade['userid'];
612607bd 215 }
216
2cc4b0f9 217 $rawgrade = false;
ac9b0805 218 $feedback = false;
219 $feedbackformat = FORMAT_MOODLE;
772ddfbf 220
ac9b0805 221 if (array_key_exists('rawgrade', $grade)) {
222 $rawgrade = $grade['rawgrade'];
223 }
612607bd 224
4cf1b9be 225 if (array_key_exists('feedback', $grade)) {
ac9b0805 226 $feedback = $grade['feedback'];
612607bd 227 }
228
ac9b0805 229 if (array_key_exists('feedbackformat', $grade)) {
230 $feedbackformat = $grade['feedbackformat'];
612607bd 231 }
232
ac9b0805 233 // update or insert the grade
c4e4068f 234 if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat)) {
4cf1b9be 235 $failed = true;
4cf1b9be 236 }
612607bd 237 }
238
4cf1b9be 239 if (!$failed) {
240 return GRADE_UPDATE_OK;
241 } else {
242 return GRADE_UPDATE_FAILED;
243 }
612607bd 244}
245
b67ec72f 246
247/**
248* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
249* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
250* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
251* If no $userid is given, the method will always return the grade_item's locked state.
252* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
253* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
254* return the locked state of the specific grade.
255*
34e67f76 256* @param int $courseid id of course
b67ec72f 257* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
258* @param string $itemmodule 'forum, 'quiz', 'csv' etc
259* @param int $iteminstance id of the item module
34e67f76 260* @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
261* @param int $userid ID of the graded user
b67ec72f 262* @return boolean Whether the grade is locked or not
263*/
34e67f76 264function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
b67ec72f 265
f92dcad8 266 if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) {
34e67f76 267 return false;
268
269 } else if (count($grade_items) == 1){
270 $grade_item = reset($grade_items);
271 return $grade_item->is_locked($userid);
272
273 } else {
274 debugging('Found more than one grade item');
275 foreach ($grade_items as $grade_item) {
276 if ($grade_item->is_locked($userid)) {
277 return true;
278 }
279 }
280 return false;
281 }
282}
b67ec72f 283
612607bd 284/***** END OF PUBLIC API *****/
285
34e67f76 286
5834dcdb 287/**
ac9b0805 288 * Updates all final grades in course.
a8995b34 289 *
290 * @param int $courseid
2e53372c 291 * @param boolean $regradeall force regrading of all items
292 *
b8ff92b6 293 * @return boolean true if ok, array of errors if problems found
a8995b34 294 */
2e53372c 295function grade_update_final_grades($courseid, $regradeall=false) {
612607bd 296
2e53372c 297 if ($regradeall) {
298 set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
b8ff92b6 299 }
a8995b34 300
f89526e0 301 if (!$grade_items = grade_item::fetch_all(array('courseid'=>$courseid))) {
b8ff92b6 302 return true;
303 }
304
2e53372c 305 if (!$regradeall) {
306 $needsupdate = false;
307 $calculated = false;
b8ff92b6 308 foreach ($grade_items as $gid=>$gitem) {
309 $grade_item =& $grade_items[$gid];
310 if ($grade_item->needsupdate) {
2e53372c 311 $needsupdate = true;
312 }
313 if ($grade_item->is_calculated()) {
314 $calculated = true;
a8995b34 315 }
316 }
772ddfbf 317
2e53372c 318 if (!$needsupdate) {
319 // no update needed
b8ff92b6 320 return true;
772ddfbf 321
2e53372c 322 } else if ($calculated) {
323 // flag all calculated grade items with needsupdate
324 // we want to make sure all are ok, this can be improved later with proper dependency calculation
325 foreach ($grade_items as $gid=>$gitem) {
326 $grade_item =& $grade_items[$gid];
327 if (!$grade_item->is_calculated()) {
328 continue;
329 }
330 $grade_item->update_from_db(); // make sure we have current data, it might have been updated in this loop already
331 if (!$grade_item->needsupdate) {
332 //force recalculation and forced update of all parents
333 $grade_item->force_regrading();
334 }
772ddfbf 335 }
336
2e53372c 337 // again make sure all date is up-to-date - the needsupdate flag might have changed
338 foreach ($grade_items as $gid=>$gitem) {
339 $grade_item =& $grade_items[$gid];
340 $grade_item->update_from_db();
341 unset($grade_item->category);
342 }
b8ff92b6 343 }
a8995b34 344 }
345
2e53372c 346
347 $errors = array();
348
b8ff92b6 349 // now the hard way with calculated grade_items or categories
350 $finalitems = array();
351 $finalids = array();
352 while (count($grade_items) > 0) {
353 $count = 0;
354 foreach ($grade_items as $gid=>$gitem) {
355 $grade_item =& $grade_items[$gid];
2e53372c 356 if (!$grade_item->needsupdate) {
b8ff92b6 357 $finalitems[$gid] = $grade_item;
358 $finalids[] = $gid;
359 unset($grade_items[$gid]);
360 continue;
361 }
362
2e53372c 363 //do we have all data for finalizing of this item?
61c33818 364 $depends_on = $grade_item->depends_on();
b8ff92b6 365
b8ff92b6 366 $doupdate = true;
61c33818 367 foreach ($depends_on as $did) {
b8ff92b6 368 if (!in_array($did, $finalids)) {
369 $doupdate = false;
370 }
371 }
372
373 //oki - let's update, calculate or aggregate :-)
374 if ($doupdate) {
ac9b0805 375 $result = $grade_item->update_final_grades();
b8ff92b6 376 if ($result !== true) {
377 $errors = array_merge($errors, $result);
378 } else {
379 $finalitems[$gid] = $grade_item;
380 $finalids[] = $gid;
381 unset($grade_items[$gid]);
382 }
383 }
384 }
385
386 if ($count == 0) {
387 foreach($grade_items as $grade_item) {
2e53372c 388 $errors[] = 'Probably circular reference or broken calculation formula in grade_item id:'.$grade_item->id; // TODO: localize
b8ff92b6 389 }
390 break;
391 }
392 }
393
394 if (count($errors) == 0) {
395 return true;
396 } else {
397 return $errors;
398 }
a8995b34 399}
967f222f 400
de420c11 401/**
d185c3ee 402 * For backwards compatibility with old third-party modules, this function can
403 * be used to import all grades from activities with legacy grading.
739196ba 404 * @param int $courseid or null if all courses
967f222f 405 */
739196ba 406function grade_grab_legacy_grades($courseid=null) {
612607bd 407
ac9b0805 408 global $CFG;
967f222f 409
410 if (!$mods = get_list_of_plugins('mod') ) {
411 error('No modules installed!');
412 }
413
739196ba 414 if ($courseid) {
415 $course_sql = " AND cm.course=$courseid";
416 } else {
417 $course_sql = "";
418 }
419
967f222f 420 foreach ($mods as $mod) {
421
422 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
423 continue;
424 }
425
d185c3ee 426 if (!$module = get_record('modules', 'name', $mod)) {
427 //not installed
428 continue;
429 }
430
431 if (!$module->visible) {
432 //disabled module
433 continue;
434 }
435
436 $fullmod = $CFG->dirroot.'/mod/'.$mod;
967f222f 437
438 // include the module lib once
439 if (file_exists($fullmod.'/lib.php')) {
440 include_once($fullmod.'/lib.php');
de420c11 441 // look for modname_grades() function - old gradebook pulling function
442 // if present sync the grades with new grading system
967f222f 443 $gradefunc = $mod.'_grades';
de420c11 444 if (function_exists($gradefunc)) {
445
446 // get all instance of the activity
d185c3ee 447 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
448 FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
739196ba 449 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id $course_sql";
de420c11 450
451 if ($modinstances = get_records_sql($sql)) {
967f222f 452 foreach ($modinstances as $modinstance) {
d185c3ee 453 grade_update_mod_grades($modinstance);
967f222f 454 }
455 }
456 }
457 }
458 }
459}
460
ac9b0805 461/**
462 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
463 */
464function grade_grab_grades() {
465
466 global $CFG;
467
468 if (!$mods = get_list_of_plugins('mod') ) {
469 error('No modules installed!');
470 }
471
472 foreach ($mods as $mod) {
473
474 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
475 continue;
476 }
477
478 if (!$module = get_record('modules', 'name', $mod)) {
479 //not installed
480 continue;
481 }
482
483 if (!$module->visible) {
484 //disabled module
485 continue;
486 }
487
488 $fullmod = $CFG->dirroot.'/mod/'.$mod;
489
490 // include the module lib once
491 if (file_exists($fullmod.'/lib.php')) {
492 include_once($fullmod.'/lib.php');
493 // look for modname_grades() function - old gradebook pulling function
494 // if present sync the grades with new grading system
495 $gradefunc = $mod.'_update_grades';
496 if (function_exists($gradefunc)) {
497 $gradefunc();
498 }
499 }
500 }
501}
502
d185c3ee 503/**
504 * Force full update of module grades in central gradebook - works for both legacy and converted activities.
505 * @param object $modinstance object with extra cmidnumber and modname property
506 * @return boolean success
507 */
508function grade_update_mod_grades($modinstance) {
509 global $CFG;
510
511 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
512 if (!file_exists($fullmod.'/lib.php')) {
513 debugging('missing lib.php file in module');
514 return false;
515 }
516 include_once($fullmod.'/lib.php');
517
518 // does it use legacy grading?
519 $gradefunc = $modinstance->modname.'_grades';
520 $updategradesfunc = $modinstance->modname.'_update_grades';
521 $updateitemfunc = $modinstance->modname.'_grade_item_update';
522
523 if (function_exists($gradefunc)) {
524 if ($oldgrades = $gradefunc($modinstance->id)) {
525
526 $grademax = $oldgrades->maxgrade;
527 $scaleid = NULL;
528 if (!is_numeric($grademax)) {
529 // scale name is provided as a string, try to find it
530 if (!$scale = get_record('scale', 'name', $grademax)) {
531 debugging('Incorrect scale name! name:'.$grademax);
532 return false;
533 }
534 $scaleid = $scale->id;
535 }
536
537 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
538 debugging('Can not get/create legacy grade item!');
539 return false;
540 }
541
542 $grades = array();
543 foreach ($oldgrades->grades as $userid=>$usergrade) {
544 $grade = new object();
545 $grade->userid = $userid;
546
547 if ($usergrade == '-') {
548 // no grade
ac9b0805 549 $grade->rawgrade = null;
d185c3ee 550
551 } else if ($scaleid) {
552 // scale in use, words used
553 $gradescale = explode(",", $scale->scale);
ac9b0805 554 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
d185c3ee 555
556 } else {
557 // good old numeric value
ac9b0805 558 $grade->rawgrade = $usergrade;
d185c3ee 559 }
560 $grades[] = $grade;
561 }
562
563 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
564 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
565 }
566
567 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
568 //new grading supported, force updating of grades
569 $updateitemfunc($modinstance);
570 $updategradesfunc($modinstance);
571
572 } else {
573 // mudule does not support grading
574 }
575
576 return true;
577}
de420c11 578
579/**
d185c3ee 580 * Get and update/create grade item for legacy modules.
de420c11 581 */
582function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
583
584 // does it already exist?
f92dcad8 585 if ($grade_items = grade_grades::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>0))) {
de420c11 586 if (count($grade_items) > 1) {
d185c3ee 587 debugging('Multiple legacy grade_items found.');
de420c11 588 return false;
589 }
590
591 $grade_item = reset($grade_items);
de420c11 592
d185c3ee 593 if (is_null($grademax) and is_null($scaleid)) {
594 $grade_item->gradetype = GRADE_TYPE_NONE;
de420c11 595
d185c3ee 596 } else if ($scaleid) {
597 $grade_item->gradetype = GRADE_TYPE_SCALE;
598 $grade_item->scaleid = $scaleid;
97d608ba 599 $grade_item->grademin = 1;
de420c11 600
d185c3ee 601 } else {
97d608ba 602 $grade_item->gradetype = GRADE_TYPE_VALUE;
603 $grade_item->grademax = $grademax;
604 $grade_item->grademin = 0;
de420c11 605 }
606
d185c3ee 607 $grade_item->itemname = $modinstance->name;
608 $grade_item->idnumber = $modinstance->cmidnumber;
de420c11 609
d185c3ee 610 $grade_item->update();
de420c11 611
612 return $grade_item;
613 }
612607bd 614
de420c11 615 // create new one
d185c3ee 616 $params = array('courseid' =>$modinstance->course,
de420c11 617 'itemtype' =>'mod',
618 'itemmodule' =>$modinstance->modname,
619 'iteminstance'=>$modinstance->id,
d185c3ee 620 'itemnumber' =>0,
de420c11 621 'itemname' =>$modinstance->name,
622 'idnumber' =>$modinstance->cmidnumber);
623
d185c3ee 624 if (is_null($grademax) and is_null($scaleid)) {
625 $params['gradetype'] = GRADE_TYPE_NONE;
626
627 } else if ($scaleid) {
612607bd 628 $params['gradetype'] = GRADE_TYPE_SCALE;
de420c11 629 $params['scaleid'] = $scaleid;
b3ac6c3e 630 $grade_item->grademin = 1;
de420c11 631 } else {
612607bd 632 $params['gradetype'] = GRADE_TYPE_VALUE;
de420c11 633 $params['grademax'] = $grademax;
634 $params['grademin'] = 0;
635 }
636
f70152b7 637 $grade_item = new grade_item($params);
638 $grade_item->insert();
de420c11 639
f70152b7 640 return $grade_item;
de420c11 641}
642
b8ff92b6 643/**
644 * This function is used to migrade old date and settings from old gradebook into new grading system.
645 *
646 * TODO:
647 * - category weight not used - we would have to create extra top course grade calculated category
648 * - exta_credit item flag not used - does not fit all our aggregation types, could be used in SUM only
649 */
650function grade_oldgradebook_upgrade($courseid) {
651 global $CFG;
652
653 $categories = array();
654 if ($oldcats = get_records('grade_category', 'courseid', $courseid)) {
655 foreach ($oldcats as $oldcat) {
656 $newcat = new grade_category(array('courseid'=>$courseid, 'fullname'=>$oldcat->name));
657 $newcat->droplow = $oldcat->drop_x_lowest;
658 $newcat->aggregation = GRADE_AGGREGATE_MEAN_GRADED;
659
660 if (empty($newcat->id)) {
661 $newcat->insert();
662 } else {
663 $newcat->update();
664 }
665
666 $categories[$oldcat->id] = $newcat;
667
668 $catitem = $newcat->get_grade_item();
669 $catitem->gradetype = GRADE_TYPE_VALUE;
670 $catitem->plusfactor = $oldcat->bonus_points;
671 $catitem->hidden = $oldcat->hidden;
672 $catitem->update();
673 }
674 }
675
676 // get all grade items with mod details
677 $sql = "SELECT gi.*, cm.idnumber as cmidnumber, m.name as modname
678 FROM {$CFG->prefix}grade_item gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
679 WHERE gi.courseid=$courseid AND m.id=gi.modid AND cm.instance=gi.cminstance
680 ORDER BY gi.sortorder ASC";
681
682 if ($olditems = get_records_sql($sql)) {
683 foreach ($olditems as $olditem) {
684 $newitem = new grade_item(array('courseid'=>$olditem->courseid, 'itemtype'=>'mod', 'itemmodule'=>$olditem->modname, 'iteminstance'=>$olditem->cminstance, 'itemnumber'=>0));
685 if (!empty($olditem->category)) {
686 // we do this low level stuff to get some speedup during upgrade
f13002d5 687 $newitem->set_parent($categories[$olditem->category]->id);
b8ff92b6 688 }
689 $newitem->gradetype = GRADE_TYPE_NONE;
690 $newitem->multfactor = $olditem->scale_grade;
691 if (empty($newitem->id)) {
692 $newitem->insert();
693 } else {
694 $newitem->update();
695 }
696 }
697 }
698}
bfe7297e 699
8178f34c 700/**
b3ac6c3e 701 * Given a grade_category, grade_item or grade_grade, this function
8178f34c 702 * figures out the state of the object and builds then returns a div
703 * with the icons needed for the grader report.
704 *
705 * @param object $object
0a1ef2f2 706 * @param object $tree (A complete grade_tree object)
8178f34c 707 * @return string HTML
708 */
f13002d5 709function grade_get_icons($element, $tree) {
8178f34c 710 global $CFG;
1ec74ea5 711 global $USER;
13b10724 712
713 // Load language strings
1ec74ea5 714 $straddfeedback = get_string("addfeedback", 'grades');
8178f34c 715 $stredit = get_string("edit");
1ec74ea5 716 $streditfeedback = get_string("editfeedback", 'grades');
d2e87c0c 717 $streditcalculation= get_string("editcalculation", 'grades');
1ec74ea5 718 $strfeedback = get_string("feedback");
8178f34c 719 $strmove = get_string("move");
720 $strmoveup = get_string("moveup");
721 $strmovedown = get_string("movedown");
722 $strmovehere = get_string("movehere");
723 $strcancel = get_string("cancel");
724 $stredit = get_string("edit");
725 $strdelete = get_string("delete");
726 $strhide = get_string("hide");
727 $strshow = get_string("show");
728 $strlock = get_string("lock", 'grades');
8ace7bf7 729 $strswitch_minus = get_string("contract", 'grades');
730 $strswitch_plus = get_string("expand", 'grades');
8178f34c 731 $strunlock = get_string("unlock", 'grades');
732
13b10724 733 // Prepare container div
0a1ef2f2 734 $html = '<div class="grade_icons">';
b3ac6c3e 735
13b10724 736 // Prepare reference variables
f13002d5 737 $eid = $element['eid'];
738 $object = $element['object'];
739 $type = $element['type'];
740
13b10724 741 // Load user preferences
742 $aggregationview = get_user_preferences('grade_report_aggregationview', $CFG->grade_report_aggregationview);
743
1ec74ea5 744 // Icons shown when edit mode is on
745 if ($USER->gradeediting) {
746 // Edit icon (except for grade_grades)
f13002d5 747 if ($type == 'category') {
748 $html .= '<a href="report/grader/edit_category.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
749 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
750 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
739196ba 751
752 } else if ($type == 'item' or $type == 'courseitem' or $type == 'categoryitem') {
f13002d5 753 $html .= '<a href="report/grader/edit_item.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
1ec74ea5 754 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
755 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
739196ba 756
757 } else if ($type == 'grade') {
d2e87c0c 758 // What is the purpose of edit_grade page?
759 /*
739196ba 760 $html .= '<a href="report/grader/edit_grade.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
761 $html .= '<img src="'.$CFG->pixpath.'/t/edit.gif" class="iconsmall" alt="'
762 .$stredit.'" title="'.$stredit.'" /></a>'. "\n";
d2e87c0c 763 */
764 }
765
766 // Calculation icon for items and categories
767 if ($type != 'grade') {
768 $html .= '<a href="report/grader/edit_calculation.php?courseid='.$object->courseid.'&amp;id='.$object->id.'">';
769 $html .= '<img src="'.$CFG->pixpath.'/t/calc.gif" class="iconsmall" alt="'
770 .$streditcalculation.'" title="'.$streditcalculation.'" /></a>'. "\n";
1ec74ea5 771 }
0a1ef2f2 772
1ec74ea5 773 // Prepare Hide/Show icon state
774 $hide_show = 'hide';
775 if ($object->is_hidden()) {
776 $hide_show = 'show';
777 }
b3ac6c3e 778
1ec74ea5 779 // Setup object identifier and show feedback icon if applicable
d2e87c0c 780 if ($type == 'grade' and $USER->gradefeedback) {
f13002d5 781 // Display Edit/Add feedback icon
782 if (empty($object->feedback)) {
13b10724 783 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
784 . "&amp;action=add&amp;courseid=$object->courseid\">\n";
f13002d5 785 $html .= '<img src="'.$CFG->pixpath.'/t/feedback_add.gif" class="iconsmall" alt="'.$straddfeedback.'" '
786 . 'title="'.$straddfeedback.'" /></a>'. "\n";
787 } else {
13b10724 788 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
789 . "&amp;action=edit&amp;courseid=$object->courseid\">\n";
f13002d5 790 $html .= '<img src="'.$CFG->pixpath.'/t/feedback.gif" class="iconsmall" alt="'.$streditfeedback.'" '
791 . 'title="'.$streditfeedback.'" onmouseover="return overlib(\''.$object->feedback.'\', CAPTION, \''
792 . $strfeedback.'\');" onmouseout="return nd();" /></a>'. "\n";
1ec74ea5 793 }
794 }
8178f34c 795
1ec74ea5 796 // Display Hide/Show icon
f13002d5 797 $html .= '<a href="report.php?report=grader&amp;target='.$eid
1ec74ea5 798 . "&amp;action=$hide_show$tree->commonvars\">\n";
799 $html .= '<img src="'.$CFG->pixpath.'/t/'.$hide_show.'.gif" class="iconsmall" alt="'
800 .${'str' . $hide_show}.'" title="'.${'str' . $hide_show}.'" /></a>'. "\n";
8178f34c 801
1ec74ea5 802 // Prepare lock/unlock string
803 $lock_unlock = 'lock';
804 if ($object->is_locked()) {
805 $lock_unlock = 'unlock';
806 }
b3ac6c3e 807
1ec74ea5 808 // Print lock/unlock icon
f13002d5 809 $html .= '<a href="report.php?report=grader&amp;target='.$eid
1ec74ea5 810 . "&amp;action=$lock_unlock$tree->commonvars\">\n";
811 $html .= '<img src="'.$CFG->pixpath.'/t/'.$lock_unlock.'.gif" class="iconsmall" alt="'
812 .${'str' . $lock_unlock}.'" title="'.${'str' . $lock_unlock}.'" /></a>'. "\n";
b3ac6c3e 813
8ace7bf7 814 // If object is a category, display expand/contract icon
13b10724 815 if (get_class($object) == 'grade_category' && $aggregationview == GRADER_REPORT_AGGREGATION_VIEW_COMPACT) {
8ace7bf7 816 $expand_contract = 'switch_minus'; // Default: expanded
1ec74ea5 817
8ace7bf7 818 $state = get_user_preferences('grade_category_' . $object->id, GRADE_CATEGORY_EXPANDED);
b3ac6c3e 819
8ace7bf7 820 if ($state == GRADE_CATEGORY_CONTRACTED) {
821 $expand_contract = 'switch_plus';
822 }
823
f13002d5 824 $html .= '<a href="report.php?report=grader&amp;target=' . $eid
8ace7bf7 825 . "&amp;action=$expand_contract$tree->commonvars\">\n";
826 $html .= '<img src="'.$CFG->pixpath.'/t/'.$expand_contract.'.gif" class="iconsmall" alt="'
827 .${'str' . $expand_contract}.'" title="'.${'str' . $expand_contract}.'" /></a>'. "\n";
1ec74ea5 828 }
829 } else {
830 if ($USER->gradefeedback) {
657e52d4 831 // Display view feedback icon
1ec74ea5 832 if (!empty($object->feedback)) {
13b10724 833 $html .= '<a href="report/grader/edit_feedback.php?id=' . $object->id
657e52d4 834 . "&amp;action=view&amp;courseid=$object->courseid\">\n";
1ec74ea5 835 $html .= '<img onmouseover="return overlib(\''.$object->feedback.'\', CAPTION, \''
b3ac6c3e 836 . $strfeedback.'\');" onmouseout="return nd();" '
1ec74ea5 837 . 'src="'.$CFG->pixpath.'/t/feedback.gif" class="iconsmall" alt="" /></a>'. "\n";
b3ac6c3e 838 }
1ec74ea5 839 }
0a1ef2f2 840 }
8178f34c 841
0a1ef2f2 842 return $html . '</div>';
8178f34c 843}
1ec74ea5 844
60cf7430 845?>