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