MDL-10231 merge grade_calculations and grade_items table + calculation improvements
[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
93 if (is_null($courseid) or is_null($itemtype)) {
94 debugging('Missing courseid or itemtype');
95 return GRADE_UPDATE_FAILED;
96 }
97
98 $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false);
99 if (!$grade_items = $grade_item->fetch_all_using_this()) {
100 // create a new one
101 $grade_item = false;
102
103 } else if (count($grade_items) == 1){
104 $grade_item = reset($grade_items);
105 unset($grade_items); //release memory
106
107 } else {
108
34e67f76 109 debugging('Found more than one grade item');
612607bd 110 return GRADE_UPDATE_MULTIPLE;
111 }
112
113/// Create or update the grade_item if needed
114 if (!$grade_item) {
115 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
116 if ($itemdetails) {
117 $itemdetails = (array)$itemdetails;
2e53372c 118
119 // grademin and grademax ignored when scale specified
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;
207
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
2cc4b0f9 221 $grade = $grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat);
ac9b0805 222
223 if (!$grade) {
4cf1b9be 224 $failed = true;
4cf1b9be 225 continue;
226 }
227
b67ec72f 228 // trigger grade_updated event notification
612607bd 229 $eventdata = new object();
ac9b0805 230
4cf1b9be 231 $eventdata->source = $source;
232 $eventdata->itemid = $grade_item->id;
233 $eventdata->courseid = $grade_item->courseid;
234 $eventdata->itemtype = $grade_item->itemtype;
235 $eventdata->itemmodule = $grade_item->itemmodule;
236 $eventdata->iteminstance = $grade_item->iteminstance;
237 $eventdata->itemnumber = $grade_item->itemnumber;
238 $eventdata->idnumber = $grade_item->idnumber;
ac9b0805 239 $eventdata->userid = $grade->userid;
240 $eventdata->rawgrade = $grade->rawgrade;
241
242 // load existing text annotation
243 if ($grade_text = $grade->load_text()) {
244 $eventdata->feedback = $grade_text->feedback;
245 $eventdata->feedbackformat = $grade_text->feedbackformat;
246 $eventdata->information = $grade_text->information;
247 $eventdata->informationformat = $grade_text->informationformat;
248 }
4cf1b9be 249
612607bd 250 events_trigger('grade_updated', $eventdata);
251 }
252
4cf1b9be 253 if (!$failed) {
254 return GRADE_UPDATE_OK;
255 } else {
256 return GRADE_UPDATE_FAILED;
257 }
612607bd 258}
259
b67ec72f 260
261/**
262* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
263* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
264* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
265* If no $userid is given, the method will always return the grade_item's locked state.
266* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
267* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
268* return the locked state of the specific grade.
269*
34e67f76 270* @param int $courseid id of course
b67ec72f 271* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
272* @param string $itemmodule 'forum, 'quiz', 'csv' etc
273* @param int $iteminstance id of the item module
34e67f76 274* @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
275* @param int $userid ID of the graded user
b67ec72f 276* @return boolean Whether the grade is locked or not
277*/
34e67f76 278function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
b67ec72f 279
34e67f76 280 $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false);
281 if (!$grade_items = $grade_item->fetch_all_using_this()) {
282 return false;
283
284 } else if (count($grade_items) == 1){
285 $grade_item = reset($grade_items);
286 return $grade_item->is_locked($userid);
287
288 } else {
289 debugging('Found more than one grade item');
290 foreach ($grade_items as $grade_item) {
291 if ($grade_item->is_locked($userid)) {
292 return true;
293 }
294 }
295 return false;
296 }
297}
b67ec72f 298
612607bd 299/***** END OF PUBLIC API *****/
300
34e67f76 301
5834dcdb 302/**
612607bd 303* Extracts from the gradebook all the grade items attached to the calling object.
304* For example, an assignment may want to retrieve all the grade_items for itself,
5834dcdb 305* and get three outcome scales in return. This will affect the grading interface.
306*
307* Note: Each parameter refines the search. So if you only give the courseid,
308* all the grade_items for this course will be returned. If you add the
309* itemtype 'mod', all grade_items for this courseif AND for the 'mod'
310* type will be returned, etc...
612607bd 311*
42bbccd7 312* @param int $courseid The id of the course to which the grade items belong
5834dcdb 313* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
314* @param string $itemmodule 'forum, 'quiz', 'csv' etc
315* @param int $iteminstance id of the item module
316* @param int $itemnumber Can be used to distinguish multiple grades for an activity
f70152b7 317* @param string $itemname The name of the grade item
42bbccd7 318* @param int $idnumber grade item Primary Key
319* @return array An array of grade items
5834dcdb 320*/
d185c3ee 321function grade_get_items($courseid, $itemtype=NULL, $itemmodule=NULL, $iteminstance=NULL, $itemnumber=NULL, $itemname=NULL, $idnumber=NULL) {
de420c11 322 $grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemname', 'itemnumber', 'idnumber'), false);
3058964f 323 $grade_items = $grade_item->fetch_all_using_this();
42bbccd7 324 return $grade_items;
5834dcdb 325}
326
5834dcdb 327/**
ac9b0805 328 * Updates all final grades in course.
a8995b34 329 *
330 * @param int $courseid
2e53372c 331 * @param boolean $regradeall force regrading of all items
332 *
b8ff92b6 333 * @return boolean true if ok, array of errors if problems found
a8995b34 334 */
2e53372c 335function grade_update_final_grades($courseid, $regradeall=false) {
612607bd 336
2e53372c 337 if ($regradeall) {
338 set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
b8ff92b6 339 }
a8995b34 340
2e53372c 341 $grade_item = new grade_item(array('courseid'=>$courseid), false);
342
343 if (!$grade_items = $grade_item->fetch_all_using_this()) {
b8ff92b6 344 return true;
345 }
346
2e53372c 347 if (!$regradeall) {
348 $needsupdate = false;
349 $calculated = false;
b8ff92b6 350 foreach ($grade_items as $gid=>$gitem) {
351 $grade_item =& $grade_items[$gid];
352 if ($grade_item->needsupdate) {
2e53372c 353 $needsupdate = true;
354 }
355 if ($grade_item->is_calculated()) {
356 $calculated = true;
a8995b34 357 }
358 }
2e53372c 359
360 if (!$needsupdate) {
361 // no update needed
b8ff92b6 362 return true;
2e53372c 363
364 } else if ($calculated) {
365 // flag all calculated grade items with needsupdate
366 // we want to make sure all are ok, this can be improved later with proper dependency calculation
367 foreach ($grade_items as $gid=>$gitem) {
368 $grade_item =& $grade_items[$gid];
369 if (!$grade_item->is_calculated()) {
370 continue;
371 }
372 $grade_item->update_from_db(); // make sure we have current data, it might have been updated in this loop already
373 if (!$grade_item->needsupdate) {
374 //force recalculation and forced update of all parents
375 $grade_item->force_regrading();
376 }
377 }
378
379 // again make sure all date is up-to-date - the needsupdate flag might have changed
380 foreach ($grade_items as $gid=>$gitem) {
381 $grade_item =& $grade_items[$gid];
382 $grade_item->update_from_db();
383 unset($grade_item->category);
384 }
b8ff92b6 385 }
a8995b34 386 }
387
2e53372c 388
389 $errors = array();
390
b8ff92b6 391 // now the hard way with calculated grade_items or categories
392 $finalitems = array();
393 $finalids = array();
394 while (count($grade_items) > 0) {
395 $count = 0;
396 foreach ($grade_items as $gid=>$gitem) {
397 $grade_item =& $grade_items[$gid];
2e53372c 398 if (!$grade_item->needsupdate) {
b8ff92b6 399 $finalitems[$gid] = $grade_item;
400 $finalids[] = $gid;
401 unset($grade_items[$gid]);
402 continue;
403 }
404
2e53372c 405 //do we have all data for finalizing of this item?
61c33818 406 $depends_on = $grade_item->depends_on();
b8ff92b6 407
b8ff92b6 408 $doupdate = true;
61c33818 409 foreach ($depends_on as $did) {
b8ff92b6 410 if (!in_array($did, $finalids)) {
411 $doupdate = false;
412 }
413 }
414
415 //oki - let's update, calculate or aggregate :-)
416 if ($doupdate) {
ac9b0805 417 $result = $grade_item->update_final_grades();
b8ff92b6 418 if ($result !== true) {
419 $errors = array_merge($errors, $result);
420 } else {
421 $finalitems[$gid] = $grade_item;
422 $finalids[] = $gid;
423 unset($grade_items[$gid]);
424 }
425 }
426 }
427
428 if ($count == 0) {
429 foreach($grade_items as $grade_item) {
2e53372c 430 $errors[] = 'Probably circular reference or broken calculation formula in grade_item id:'.$grade_item->id; // TODO: localize
b8ff92b6 431 }
432 break;
433 }
434 }
435
436 if (count($errors) == 0) {
437 return true;
438 } else {
439 return $errors;
440 }
a8995b34 441}
967f222f 442
de420c11 443/**
d185c3ee 444 * For backwards compatibility with old third-party modules, this function can
445 * be used to import all grades from activities with legacy grading.
967f222f 446 */
de420c11 447function grade_grab_legacy_grades() {
612607bd 448
ac9b0805 449 global $CFG;
967f222f 450
451 if (!$mods = get_list_of_plugins('mod') ) {
452 error('No modules installed!');
453 }
454
455 foreach ($mods as $mod) {
456
457 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
458 continue;
459 }
460
d185c3ee 461 if (!$module = get_record('modules', 'name', $mod)) {
462 //not installed
463 continue;
464 }
465
466 if (!$module->visible) {
467 //disabled module
468 continue;
469 }
470
471 $fullmod = $CFG->dirroot.'/mod/'.$mod;
967f222f 472
473 // include the module lib once
474 if (file_exists($fullmod.'/lib.php')) {
475 include_once($fullmod.'/lib.php');
de420c11 476 // look for modname_grades() function - old gradebook pulling function
477 // if present sync the grades with new grading system
967f222f 478 $gradefunc = $mod.'_grades';
de420c11 479 if (function_exists($gradefunc)) {
480
481 // get all instance of the activity
d185c3ee 482 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
483 FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
484 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id";
de420c11 485
486 if ($modinstances = get_records_sql($sql)) {
967f222f 487 foreach ($modinstances as $modinstance) {
d185c3ee 488 grade_update_mod_grades($modinstance);
967f222f 489 }
490 }
491 }
492 }
493 }
494}
495
ac9b0805 496/**
497 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
498 */
499function grade_grab_grades() {
500
501 global $CFG;
502
503 if (!$mods = get_list_of_plugins('mod') ) {
504 error('No modules installed!');
505 }
506
507 foreach ($mods as $mod) {
508
509 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
510 continue;
511 }
512
513 if (!$module = get_record('modules', 'name', $mod)) {
514 //not installed
515 continue;
516 }
517
518 if (!$module->visible) {
519 //disabled module
520 continue;
521 }
522
523 $fullmod = $CFG->dirroot.'/mod/'.$mod;
524
525 // include the module lib once
526 if (file_exists($fullmod.'/lib.php')) {
527 include_once($fullmod.'/lib.php');
528 // look for modname_grades() function - old gradebook pulling function
529 // if present sync the grades with new grading system
530 $gradefunc = $mod.'_update_grades';
531 if (function_exists($gradefunc)) {
532 $gradefunc();
533 }
534 }
535 }
536}
537
d185c3ee 538/**
539 * Force full update of module grades in central gradebook - works for both legacy and converted activities.
540 * @param object $modinstance object with extra cmidnumber and modname property
541 * @return boolean success
542 */
543function grade_update_mod_grades($modinstance) {
544 global $CFG;
545
546 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
547 if (!file_exists($fullmod.'/lib.php')) {
548 debugging('missing lib.php file in module');
549 return false;
550 }
551 include_once($fullmod.'/lib.php');
552
553 // does it use legacy grading?
554 $gradefunc = $modinstance->modname.'_grades';
555 $updategradesfunc = $modinstance->modname.'_update_grades';
556 $updateitemfunc = $modinstance->modname.'_grade_item_update';
557
558 if (function_exists($gradefunc)) {
559 if ($oldgrades = $gradefunc($modinstance->id)) {
560
561 $grademax = $oldgrades->maxgrade;
562 $scaleid = NULL;
563 if (!is_numeric($grademax)) {
564 // scale name is provided as a string, try to find it
565 if (!$scale = get_record('scale', 'name', $grademax)) {
566 debugging('Incorrect scale name! name:'.$grademax);
567 return false;
568 }
569 $scaleid = $scale->id;
570 }
571
572 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
573 debugging('Can not get/create legacy grade item!');
574 return false;
575 }
576
577 $grades = array();
578 foreach ($oldgrades->grades as $userid=>$usergrade) {
579 $grade = new object();
580 $grade->userid = $userid;
581
582 if ($usergrade == '-') {
583 // no grade
ac9b0805 584 $grade->rawgrade = null;
d185c3ee 585
586 } else if ($scaleid) {
587 // scale in use, words used
588 $gradescale = explode(",", $scale->scale);
ac9b0805 589 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
d185c3ee 590
591 } else {
592 // good old numeric value
ac9b0805 593 $grade->rawgrade = $usergrade;
d185c3ee 594 }
595 $grades[] = $grade;
596 }
597
598 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
599 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
600 }
601
602 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
603 //new grading supported, force updating of grades
604 $updateitemfunc($modinstance);
605 $updategradesfunc($modinstance);
606
607 } else {
608 // mudule does not support grading
609 }
610
611 return true;
612}
de420c11 613
614/**
d185c3ee 615 * Get and update/create grade item for legacy modules.
de420c11 616 */
617function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
618
619 // does it already exist?
d185c3ee 620 if ($grade_items = grade_get_items($modinstance->course, 'mod', $modinstance->modname, $modinstance->id, 0)) {
de420c11 621 if (count($grade_items) > 1) {
d185c3ee 622 debugging('Multiple legacy grade_items found.');
de420c11 623 return false;
624 }
625
626 $grade_item = reset($grade_items);
de420c11 627
d185c3ee 628 if (is_null($grademax) and is_null($scaleid)) {
629 $grade_item->gradetype = GRADE_TYPE_NONE;
de420c11 630
d185c3ee 631 } else if ($scaleid) {
632 $grade_item->gradetype = GRADE_TYPE_SCALE;
633 $grade_item->scaleid = $scaleid;
de420c11 634
d185c3ee 635 } else {
636 $grade_item->gradetype = GRADE_TYPE_VALUE;
637 $grade_item->grademax = $grademax;
638 $grade_item->grademin = 0;
de420c11 639 }
640
d185c3ee 641 $grade_item->itemname = $modinstance->name;
642 $grade_item->idnumber = $modinstance->cmidnumber;
de420c11 643
d185c3ee 644 $grade_item->update();
de420c11 645
646 return $grade_item;
647 }
612607bd 648
de420c11 649 // create new one
d185c3ee 650 $params = array('courseid' =>$modinstance->course,
de420c11 651 'itemtype' =>'mod',
652 'itemmodule' =>$modinstance->modname,
653 'iteminstance'=>$modinstance->id,
d185c3ee 654 'itemnumber' =>0,
de420c11 655 'itemname' =>$modinstance->name,
656 'idnumber' =>$modinstance->cmidnumber);
657
d185c3ee 658 if (is_null($grademax) and is_null($scaleid)) {
659 $params['gradetype'] = GRADE_TYPE_NONE;
660
661 } else if ($scaleid) {
612607bd 662 $params['gradetype'] = GRADE_TYPE_SCALE;
de420c11 663 $params['scaleid'] = $scaleid;
664
665 } else {
612607bd 666 $params['gradetype'] = GRADE_TYPE_VALUE;
de420c11 667 $params['grademax'] = $grademax;
668 $params['grademin'] = 0;
669 }
670
f70152b7 671 $grade_item = new grade_item($params);
672 $grade_item->insert();
de420c11 673
f70152b7 674 return $grade_item;
de420c11 675}
676
b8ff92b6 677/**
678 * This function is used to migrade old date and settings from old gradebook into new grading system.
679 *
680 * TODO:
681 * - category weight not used - we would have to create extra top course grade calculated category
682 * - exta_credit item flag not used - does not fit all our aggregation types, could be used in SUM only
683 */
684function grade_oldgradebook_upgrade($courseid) {
685 global $CFG;
686
687 $categories = array();
688 if ($oldcats = get_records('grade_category', 'courseid', $courseid)) {
689 foreach ($oldcats as $oldcat) {
690 $newcat = new grade_category(array('courseid'=>$courseid, 'fullname'=>$oldcat->name));
691 $newcat->droplow = $oldcat->drop_x_lowest;
692 $newcat->aggregation = GRADE_AGGREGATE_MEAN_GRADED;
693
694 if (empty($newcat->id)) {
695 $newcat->insert();
696 } else {
697 $newcat->update();
698 }
699
700 $categories[$oldcat->id] = $newcat;
701
702 $catitem = $newcat->get_grade_item();
703 $catitem->gradetype = GRADE_TYPE_VALUE;
704 $catitem->plusfactor = $oldcat->bonus_points;
705 $catitem->hidden = $oldcat->hidden;
706 $catitem->update();
707 }
708 }
709
710 // get all grade items with mod details
711 $sql = "SELECT gi.*, cm.idnumber as cmidnumber, m.name as modname
712 FROM {$CFG->prefix}grade_item gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
713 WHERE gi.courseid=$courseid AND m.id=gi.modid AND cm.instance=gi.cminstance
714 ORDER BY gi.sortorder ASC";
715
716 if ($olditems = get_records_sql($sql)) {
717 foreach ($olditems as $olditem) {
718 $newitem = new grade_item(array('courseid'=>$olditem->courseid, 'itemtype'=>'mod', 'itemmodule'=>$olditem->modname, 'iteminstance'=>$olditem->cminstance, 'itemnumber'=>0));
719 if (!empty($olditem->category)) {
720 // we do this low level stuff to get some speedup during upgrade
721 $newitem->set_parent_id($categories[$olditem->category]->id);
722 }
723 $newitem->gradetype = GRADE_TYPE_NONE;
724 $newitem->multfactor = $olditem->scale_grade;
725 if (empty($newitem->id)) {
726 $newitem->insert();
727 } else {
728 $newitem->update();
729 }
730 }
731 }
732}
bfe7297e 733
60cf7430 734?>