MDL-10207 Tabs added to category admin page and grader report page. I also changed...
[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
210611f6 48define('GRADE_TYPE_NONE', 0);
49define('GRADE_TYPE_VALUE', 1);
50define('GRADE_TYPE_SCALE', 2);
51define('GRADE_TYPE_TEXT', 3);
42bbccd7 52
612607bd 53define('GRADE_UPDATE_OK', 0);
54define('GRADE_UPDATE_FAILED', 1);
55define('GRADE_UPDATE_MULTIPLE', 2);
56define('GRADE_UPDATE_ITEM_DELETED', 3);
4cf1b9be 57define('GRADE_UPDATE_ITEM_LOCKED', 4);
612607bd 58
59
3058964f 60require_once($CFG->libdir . '/grade/grade_category.php');
61require_once($CFG->libdir . '/grade/grade_item.php');
62require_once($CFG->libdir . '/grade/grade_calculation.php');
ac9b0805 63require_once($CFG->libdir . '/grade/grade_grades.php');
d5bdb228 64require_once($CFG->libdir . '/grade/grade_scale.php');
5501446d 65require_once($CFG->libdir . '/grade/grade_outcome.php');
46566dd8 66require_once($CFG->libdir . '/grade/grade_history.php');
67require_once($CFG->libdir . '/grade/grade_grades_text.php');
8ff4550a 68require_once($CFG->libdir . '/grade/grade_tree.php');
60cf7430 69
612607bd 70/***** PUBLIC GRADE API *****/
71
c5b5f18d 72/**
73 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
ac9b0805 74 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
c5b5f18d 75 * or key means do not change existing.
4cf1b9be 76 *
c5b5f18d 77 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
e648f890 78 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
4cf1b9be 79 *
c5b5f18d 80 * @param string $source source of the grade such as 'mod/assignment', often used to prevent infinite loops when processing grade_updated events
81 * @param int $courseid id of course
82 * @param string $itemtype type of grade item - mod, block, gradecategory, calculated
83 * @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
84 * @param int $iteminstance instance it of graded subject
85 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
86 * @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating rgade_item definition only\
87 * @param mixed $itemdetails object or array describing the grading item, NULL if no change
88 */
b67ec72f 89function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
612607bd 90
c5b5f18d 91 // only following grade_item properties can be changed in this function
e648f890 92 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
612607bd 93
94 if (is_null($courseid) or is_null($itemtype)) {
95 debugging('Missing courseid or itemtype');
96 return GRADE_UPDATE_FAILED;
97 }
98
99 $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false);
100 if (!$grade_items = $grade_item->fetch_all_using_this()) {
101 // create a new one
102 $grade_item = false;
103
104 } else if (count($grade_items) == 1){
105 $grade_item = reset($grade_items);
106 unset($grade_items); //release memory
107
108 } else {
109
34e67f76 110 debugging('Found more than one grade item');
612607bd 111 return GRADE_UPDATE_MULTIPLE;
112 }
113
114/// Create or update the grade_item if needed
115 if (!$grade_item) {
116 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
117 if ($itemdetails) {
118 $itemdetails = (array)$itemdetails;
119 foreach ($itemdetails as $k=>$v) {
120 if (!in_array($k, $allowed)) {
121 // ignore it
122 continue;
123 }
124 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
125 // no grade item needed!
126 return GRADE_UPDATE_OK;
127 }
128 $params[$k] = $v;
129 }
130 }
f70152b7 131 $grade_item = new grade_item($params);
132 $grade_item->insert();
612607bd 133
134 } else {
2cc4b0f9 135 if ($grade_item->is_locked()) {
612607bd 136 debugging('Grading item is locked!');
4cf1b9be 137 return GRADE_UPDATE_ITEM_LOCKED;
612607bd 138 }
139
140 if ($itemdetails) {
141 $itemdetails = (array)$itemdetails;
142 $update = false;
143 foreach ($itemdetails as $k=>$v) {
144 if (!in_array($k, $allowed)) {
145 // ignore it
146 continue;
147 }
148 if ($grade_item->{$k} != $v) {
149 $grade_item->{$k} = $v;
150 $update = true;
151 }
152 }
153 if ($update) {
154 $grade_item->update();
155 }
156 }
157 }
158
159/// Some extra checks
160 // do we use grading?
161 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
162 return GRADE_UPDATE_OK;
163 }
164
165 // no grade submitted
b67ec72f 166 if (empty($grades)) {
612607bd 167 return GRADE_UPDATE_OK;
168 }
169
170 // no grading in deleted items
171 if ($grade_item->deleted) {
172 debugging('Grade item was already deleted!');
173 return GRADE_UPDATE_ITEM_DELETED;
174 }
175
176/// Finally start processing of grades
b67ec72f 177 if (is_object($grades)) {
178 $grades = array($grades);
612607bd 179 } else {
b67ec72f 180 if (array_key_exists('userid', $grades)) {
181 $grades = array($grades);
612607bd 182 }
183 }
184
4cf1b9be 185 $failed = false;
612607bd 186 foreach ($grades as $grade) {
187 $grade = (array)$grade;
188 if (empty($grade['userid'])) {
4cf1b9be 189 $failed = true;
190 debugging('Invalid userid in grade submitted');
191 continue;
ac9b0805 192 } else {
193 $userid = $grade['userid'];
612607bd 194 }
195
2cc4b0f9 196 $rawgrade = false;
ac9b0805 197 $feedback = false;
198 $feedbackformat = FORMAT_MOODLE;
199
200 if (array_key_exists('rawgrade', $grade)) {
201 $rawgrade = $grade['rawgrade'];
202 }
612607bd 203
4cf1b9be 204 if (array_key_exists('feedback', $grade)) {
ac9b0805 205 $feedback = $grade['feedback'];
612607bd 206 }
207
ac9b0805 208 if (array_key_exists('feedbackformat', $grade)) {
209 $feedbackformat = $grade['feedbackformat'];
612607bd 210 }
211
ac9b0805 212 // update or insert the grade
2cc4b0f9 213 $grade = $grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat);
ac9b0805 214
215 if (!$grade) {
4cf1b9be 216 $failed = true;
4cf1b9be 217 continue;
218 }
219
b67ec72f 220 // trigger grade_updated event notification
612607bd 221 $eventdata = new object();
ac9b0805 222
4cf1b9be 223 $eventdata->source = $source;
224 $eventdata->itemid = $grade_item->id;
225 $eventdata->courseid = $grade_item->courseid;
226 $eventdata->itemtype = $grade_item->itemtype;
227 $eventdata->itemmodule = $grade_item->itemmodule;
228 $eventdata->iteminstance = $grade_item->iteminstance;
229 $eventdata->itemnumber = $grade_item->itemnumber;
230 $eventdata->idnumber = $grade_item->idnumber;
ac9b0805 231 $eventdata->userid = $grade->userid;
232 $eventdata->rawgrade = $grade->rawgrade;
233
234 // load existing text annotation
235 if ($grade_text = $grade->load_text()) {
236 $eventdata->feedback = $grade_text->feedback;
237 $eventdata->feedbackformat = $grade_text->feedbackformat;
238 $eventdata->information = $grade_text->information;
239 $eventdata->informationformat = $grade_text->informationformat;
240 }
4cf1b9be 241
612607bd 242 events_trigger('grade_updated', $eventdata);
243 }
244
4cf1b9be 245 if (!$failed) {
246 return GRADE_UPDATE_OK;
247 } else {
248 return GRADE_UPDATE_FAILED;
249 }
612607bd 250}
251
b67ec72f 252
253/**
254* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
255* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
256* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
257* If no $userid is given, the method will always return the grade_item's locked state.
258* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
259* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
260* return the locked state of the specific grade.
261*
34e67f76 262* @param int $courseid id of course
b67ec72f 263* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
264* @param string $itemmodule 'forum, 'quiz', 'csv' etc
265* @param int $iteminstance id of the item module
34e67f76 266* @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
267* @param int $userid ID of the graded user
b67ec72f 268* @return boolean Whether the grade is locked or not
269*/
34e67f76 270function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
b67ec72f 271
34e67f76 272 $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false);
273 if (!$grade_items = $grade_item->fetch_all_using_this()) {
274 return false;
275
276 } else if (count($grade_items) == 1){
277 $grade_item = reset($grade_items);
278 return $grade_item->is_locked($userid);
279
280 } else {
281 debugging('Found more than one grade item');
282 foreach ($grade_items as $grade_item) {
283 if ($grade_item->is_locked($userid)) {
284 return true;
285 }
286 }
287 return false;
288 }
289}
b67ec72f 290
612607bd 291/***** END OF PUBLIC API *****/
292
34e67f76 293
5834dcdb 294/**
612607bd 295* Extracts from the gradebook all the grade items attached to the calling object.
296* For example, an assignment may want to retrieve all the grade_items for itself,
5834dcdb 297* and get three outcome scales in return. This will affect the grading interface.
298*
299* Note: Each parameter refines the search. So if you only give the courseid,
300* all the grade_items for this course will be returned. If you add the
301* itemtype 'mod', all grade_items for this courseif AND for the 'mod'
302* type will be returned, etc...
612607bd 303*
42bbccd7 304* @param int $courseid The id of the course to which the grade items belong
5834dcdb 305* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
306* @param string $itemmodule 'forum, 'quiz', 'csv' etc
307* @param int $iteminstance id of the item module
308* @param int $itemnumber Can be used to distinguish multiple grades for an activity
f70152b7 309* @param string $itemname The name of the grade item
42bbccd7 310* @param int $idnumber grade item Primary Key
311* @return array An array of grade items
5834dcdb 312*/
d185c3ee 313function grade_get_items($courseid, $itemtype=NULL, $itemmodule=NULL, $iteminstance=NULL, $itemnumber=NULL, $itemname=NULL, $idnumber=NULL) {
de420c11 314 $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemname', 'itemnumber', 'idnumber'), false);
3058964f 315 $grade_items = $grade_item->fetch_all_using_this();
42bbccd7 316 return $grade_items;
5834dcdb 317}
318
5834dcdb 319/**
ac9b0805 320 * Updates all final grades in course.
a8995b34 321 *
322 * @param int $courseid
b8ff92b6 323 * @return boolean true if ok, array of errors if problems found
a8995b34 324 */
b8ff92b6 325function grade_update_final_grades($courseid) {
326 $errors = array();
a8995b34 327 $grade_item = new grade_item();
328 $grade_item->courseid = $courseid;
b8ff92b6 329 if (!$grade_items = $grade_item->fetch_all_using_this()) {
330 return true;
331 }
612607bd 332
b8ff92b6 333 $needsupdate = false;
334 $calculated = false;
335 foreach ($grade_items as $gid=>$gitem) {
336 $grade_item =& $grade_items[$gid];
337 if ($grade_item->needsupdate) {
338 $needsupdate = true;
339 }
340 if ($grade_item->get_calculation()) {
341 $calculated = true;
342 }
343 }
a8995b34 344
b8ff92b6 345 // no update needed
346 if (!$needsupdate) {
347 return true;
348 }
349
350 // the easy way
351 if (!$calculated) {
352 foreach ($grade_items as $gid=>$gitem) {
353 $grade_item =& $grade_items[$gid];
354 if ($grade_item->needsupdate) {
ac9b0805 355 $result = $grade_item->update_final_grades();
b8ff92b6 356 if ($result !== true) {
357 $errors = array_merge($errors, $result);
358 }
a8995b34 359 }
360 }
b8ff92b6 361
362 if (count($errors) == 0) {
363 return true;
364 } else {
365 return $errors;
366 }
a8995b34 367 }
368
b8ff92b6 369 // now the hard way with calculated grade_items or categories
370 $finalitems = array();
371 $finalids = array();
372 while (count($grade_items) > 0) {
373 $count = 0;
374 foreach ($grade_items as $gid=>$gitem) {
375 $grade_item =& $grade_items[$gid];
376 if (!$grade_item->needsupdate and $grade_item->itemtype!='category' and !$grade_item->get_calculation()) {
377 $finalitems[$gid] = $grade_item;
378 $finalids[] = $gid;
379 unset($grade_items[$gid]);
380 continue;
381 }
382
383 $dependson = $grade_item->dependson();
384
385 //are we dealing with category with no calculated items?
386 // we can not trust the needsupdate flag because category might contain calculated items
387 if ($grade_item->itemtype=='category' and !$grade_item->needsupdate) {
388 $forceupdate = false;
389 foreach ($dependson as $childid) {
390 if (in_array($childid, $finalids)) {
391 $child = $finalitems[$childid];
392 } else {
393 $child = $grade_items[$childid];
394 }
395 if ($child->itemtype == 'category' or $child->get_calculation()) {
396 $forceupdate = true;
397 }
398 }
399
400 if ($forceupdate) {
2cc4b0f9 401 $grade_item->force_regrading();
b8ff92b6 402 } else {
403 $finalitems[$gid] = $grade_item;
404 $finalids[] = $gid;
405 unset($grade_items[$gid]);
406 continue;
407 }
408 }
409
410 //do we have all data for this item?
411 $doupdate = true;
412 foreach ($dependson as $did) {
413 if (!in_array($did, $finalids)) {
414 $doupdate = false;
415 }
416 }
417
418 //oki - let's update, calculate or aggregate :-)
419 if ($doupdate) {
ac9b0805 420 $result = $grade_item->update_final_grades();
b8ff92b6 421 if ($result !== true) {
422 $errors = array_merge($errors, $result);
423 } else {
424 $finalitems[$gid] = $grade_item;
425 $finalids[] = $gid;
426 unset($grade_items[$gid]);
427 }
428 }
429 }
430
431 if ($count == 0) {
432 foreach($grade_items as $grade_item) {
433 $errors[] = 'Probably circular reference in grade_item id:'.$grade_item->id; // TODO: localize
434 }
435 break;
436 }
437 }
438
439 if (count($errors) == 0) {
440 return true;
441 } else {
442 return $errors;
443 }
a8995b34 444}
967f222f 445
de420c11 446/**
d185c3ee 447 * For backwards compatibility with old third-party modules, this function can
448 * be used to import all grades from activities with legacy grading.
967f222f 449 */
de420c11 450function grade_grab_legacy_grades() {
612607bd 451
ac9b0805 452 global $CFG;
967f222f 453
454 if (!$mods = get_list_of_plugins('mod') ) {
455 error('No modules installed!');
456 }
457
458 foreach ($mods as $mod) {
459
460 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
461 continue;
462 }
463
d185c3ee 464 if (!$module = get_record('modules', 'name', $mod)) {
465 //not installed
466 continue;
467 }
468
469 if (!$module->visible) {
470 //disabled module
471 continue;
472 }
473
474 $fullmod = $CFG->dirroot.'/mod/'.$mod;
967f222f 475
476 // include the module lib once
477 if (file_exists($fullmod.'/lib.php')) {
478 include_once($fullmod.'/lib.php');
de420c11 479 // look for modname_grades() function - old gradebook pulling function
480 // if present sync the grades with new grading system
967f222f 481 $gradefunc = $mod.'_grades';
de420c11 482 if (function_exists($gradefunc)) {
483
484 // get all instance of the activity
d185c3ee 485 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
486 FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
487 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id";
de420c11 488
489 if ($modinstances = get_records_sql($sql)) {
967f222f 490 foreach ($modinstances as $modinstance) {
d185c3ee 491 grade_update_mod_grades($modinstance);
967f222f 492 }
493 }
494 }
495 }
496 }
497}
498
ac9b0805 499/**
500 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
501 */
502function grade_grab_grades() {
503
504 global $CFG;
505
506 if (!$mods = get_list_of_plugins('mod') ) {
507 error('No modules installed!');
508 }
509
510 foreach ($mods as $mod) {
511
512 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
513 continue;
514 }
515
516 if (!$module = get_record('modules', 'name', $mod)) {
517 //not installed
518 continue;
519 }
520
521 if (!$module->visible) {
522 //disabled module
523 continue;
524 }
525
526 $fullmod = $CFG->dirroot.'/mod/'.$mod;
527
528 // include the module lib once
529 if (file_exists($fullmod.'/lib.php')) {
530 include_once($fullmod.'/lib.php');
531 // look for modname_grades() function - old gradebook pulling function
532 // if present sync the grades with new grading system
533 $gradefunc = $mod.'_update_grades';
534 if (function_exists($gradefunc)) {
535 $gradefunc();
536 }
537 }
538 }
539}
540
d185c3ee 541/**
542 * Force full update of module grades in central gradebook - works for both legacy and converted activities.
543 * @param object $modinstance object with extra cmidnumber and modname property
544 * @return boolean success
545 */
546function grade_update_mod_grades($modinstance) {
547 global $CFG;
548
549 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
550 if (!file_exists($fullmod.'/lib.php')) {
551 debugging('missing lib.php file in module');
552 return false;
553 }
554 include_once($fullmod.'/lib.php');
555
556 // does it use legacy grading?
557 $gradefunc = $modinstance->modname.'_grades';
558 $updategradesfunc = $modinstance->modname.'_update_grades';
559 $updateitemfunc = $modinstance->modname.'_grade_item_update';
560
561 if (function_exists($gradefunc)) {
562 if ($oldgrades = $gradefunc($modinstance->id)) {
563
564 $grademax = $oldgrades->maxgrade;
565 $scaleid = NULL;
566 if (!is_numeric($grademax)) {
567 // scale name is provided as a string, try to find it
568 if (!$scale = get_record('scale', 'name', $grademax)) {
569 debugging('Incorrect scale name! name:'.$grademax);
570 return false;
571 }
572 $scaleid = $scale->id;
573 }
574
575 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
576 debugging('Can not get/create legacy grade item!');
577 return false;
578 }
579
580 $grades = array();
581 foreach ($oldgrades->grades as $userid=>$usergrade) {
582 $grade = new object();
583 $grade->userid = $userid;
584
585 if ($usergrade == '-') {
586 // no grade
ac9b0805 587 $grade->rawgrade = null;
d185c3ee 588
589 } else if ($scaleid) {
590 // scale in use, words used
591 $gradescale = explode(",", $scale->scale);
ac9b0805 592 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
d185c3ee 593
594 } else {
595 // good old numeric value
ac9b0805 596 $grade->rawgrade = $usergrade;
d185c3ee 597 }
598 $grades[] = $grade;
599 }
600
601 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
602 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
603 }
604
605 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
606 //new grading supported, force updating of grades
607 $updateitemfunc($modinstance);
608 $updategradesfunc($modinstance);
609
610 } else {
611 // mudule does not support grading
612 }
613
614 return true;
615}
de420c11 616
617/**
d185c3ee 618 * Get and update/create grade item for legacy modules.
de420c11 619 */
620function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
621
622 // does it already exist?
d185c3ee 623 if ($grade_items = grade_get_items($modinstance->course, 'mod', $modinstance->modname, $modinstance->id, 0)) {
de420c11 624 if (count($grade_items) > 1) {
d185c3ee 625 debugging('Multiple legacy grade_items found.');
de420c11 626 return false;
627 }
628
629 $grade_item = reset($grade_items);
de420c11 630
d185c3ee 631 if (is_null($grademax) and is_null($scaleid)) {
632 $grade_item->gradetype = GRADE_TYPE_NONE;
de420c11 633
d185c3ee 634 } else if ($scaleid) {
635 $grade_item->gradetype = GRADE_TYPE_SCALE;
636 $grade_item->scaleid = $scaleid;
de420c11 637
d185c3ee 638 } else {
639 $grade_item->gradetype = GRADE_TYPE_VALUE;
640 $grade_item->grademax = $grademax;
641 $grade_item->grademin = 0;
de420c11 642 }
643
d185c3ee 644 $grade_item->itemname = $modinstance->name;
645 $grade_item->idnumber = $modinstance->cmidnumber;
de420c11 646
d185c3ee 647 $grade_item->update();
de420c11 648
649 return $grade_item;
650 }
612607bd 651
de420c11 652 // create new one
d185c3ee 653 $params = array('courseid' =>$modinstance->course,
de420c11 654 'itemtype' =>'mod',
655 'itemmodule' =>$modinstance->modname,
656 'iteminstance'=>$modinstance->id,
d185c3ee 657 'itemnumber' =>0,
de420c11 658 'itemname' =>$modinstance->name,
659 'idnumber' =>$modinstance->cmidnumber);
660
d185c3ee 661 if (is_null($grademax) and is_null($scaleid)) {
662 $params['gradetype'] = GRADE_TYPE_NONE;
663
664 } else if ($scaleid) {
612607bd 665 $params['gradetype'] = GRADE_TYPE_SCALE;
de420c11 666 $params['scaleid'] = $scaleid;
667
668 } else {
612607bd 669 $params['gradetype'] = GRADE_TYPE_VALUE;
de420c11 670 $params['grademax'] = $grademax;
671 $params['grademin'] = 0;
672 }
673
f70152b7 674 $grade_item = new grade_item($params);
675 $grade_item->insert();
de420c11 676
f70152b7 677 return $grade_item;
de420c11 678}
679
b8ff92b6 680/**
681 * This function is used to migrade old date and settings from old gradebook into new grading system.
682 *
683 * TODO:
684 * - category weight not used - we would have to create extra top course grade calculated category
685 * - exta_credit item flag not used - does not fit all our aggregation types, could be used in SUM only
686 */
687function grade_oldgradebook_upgrade($courseid) {
688 global $CFG;
689
690 $categories = array();
691 if ($oldcats = get_records('grade_category', 'courseid', $courseid)) {
692 foreach ($oldcats as $oldcat) {
693 $newcat = new grade_category(array('courseid'=>$courseid, 'fullname'=>$oldcat->name));
694 $newcat->droplow = $oldcat->drop_x_lowest;
695 $newcat->aggregation = GRADE_AGGREGATE_MEAN_GRADED;
696
697 if (empty($newcat->id)) {
698 $newcat->insert();
699 } else {
700 $newcat->update();
701 }
702
703 $categories[$oldcat->id] = $newcat;
704
705 $catitem = $newcat->get_grade_item();
706 $catitem->gradetype = GRADE_TYPE_VALUE;
707 $catitem->plusfactor = $oldcat->bonus_points;
708 $catitem->hidden = $oldcat->hidden;
709 $catitem->update();
710 }
711 }
712
713 // get all grade items with mod details
714 $sql = "SELECT gi.*, cm.idnumber as cmidnumber, m.name as modname
715 FROM {$CFG->prefix}grade_item gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
716 WHERE gi.courseid=$courseid AND m.id=gi.modid AND cm.instance=gi.cminstance
717 ORDER BY gi.sortorder ASC";
718
719 if ($olditems = get_records_sql($sql)) {
720 foreach ($olditems as $olditem) {
721 $newitem = new grade_item(array('courseid'=>$olditem->courseid, 'itemtype'=>'mod', 'itemmodule'=>$olditem->modname, 'iteminstance'=>$olditem->cminstance, 'itemnumber'=>0));
722 if (!empty($olditem->category)) {
723 // we do this low level stuff to get some speedup during upgrade
724 $newitem->set_parent_id($categories[$olditem->category]->id);
725 }
726 $newitem->gradetype = GRADE_TYPE_NONE;
727 $newitem->multfactor = $olditem->scale_grade;
728 if (empty($newitem->id)) {
729 $newitem->insert();
730 } else {
731 $newitem->update();
732 }
733 }
734 }
735}
bfe7297e 736
60cf7430 737?>