MDL-10496:
[moodle.git] / lib / gradelib.php
CommitLineData
5834dcdb 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
10// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
25
26/**
42bbccd7 27 * Library of functions for gradebook
5834dcdb 28 *
29 * @author Moodle HQ developers
30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package moodlecore
33 */
34
b8ff92b6 35define('GRADE_AGGREGATE_MEAN_ALL', 0);
42bbccd7 36define('GRADE_AGGREGATE_MEDIAN', 1);
b8ff92b6 37define('GRADE_AGGREGATE_MEAN_GRADED', 2);
38define('GRADE_AGGREGATE_MIN', 3);
39define('GRADE_AGGREGATE_MAX', 4);
95affb8a 40define('GRADE_AGGREGATE_MODE', 5);
612607bd 41
27f95e9b 42define('GRADE_CHILDTYPE_ITEM', 0);
43define('GRADE_CHILDTYPE_CAT', 1);
612607bd 44
27f95e9b 45define('GRADE_ITEM', 0); // Used to compare class names with CHILDTYPE values
46define('GRADE_CATEGORY', 1); // Used to compare class names with CHILDTYPE values
612607bd 47
8ace7bf7 48define('GRADE_CATEGORY_CONTRACTED', 0); // The state of a category header in the grader report
49define('GRADE_CATEGORY_EXPANDED', 1); // The state of a category header in the grader report
50
210611f6 51define('GRADE_TYPE_NONE', 0);
52define('GRADE_TYPE_VALUE', 1);
53define('GRADE_TYPE_SCALE', 2);
54define('GRADE_TYPE_TEXT', 3);
42bbccd7 55
612607bd 56define('GRADE_UPDATE_OK', 0);
57define('GRADE_UPDATE_FAILED', 1);
58define('GRADE_UPDATE_MULTIPLE', 2);
59define('GRADE_UPDATE_ITEM_DELETED', 3);
4cf1b9be 60define('GRADE_UPDATE_ITEM_LOCKED', 4);
612607bd 61
aaff71da 62// Grate teables history tracking actions
63define('GRADE_HISTORY_INSERT', 1);
64define('GRADE_HISTORY_UPDATE', 2);
65define('GRADE_HISTORY_DELETE', 3);
66
936f1350 67// Common directories
68define('GRADE_EDIT_DIR', $CFG->dirroot . '/grade/edit');
8ba894e0 69define('GRADE_EDIT_URL', $CFG->wwwroot . '/grade/edit');
612607bd 70
eea6690a 71// Grader reports
bb384a8e 72define('GRADE_REPORT_AGGREGATION_POSITION_LEFT', 0);
73define('GRADE_REPORT_AGGREGATION_POSITION_RIGHT', 1);
74define('GRADE_REPORT_AGGREGATION_VIEW_FULL', 0);
75define('GRADE_REPORT_AGGREGATION_VIEW_COMPACT', 1);
db4c7968 76define('GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL', 0);
bb384a8e 77define('GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE', 1);
db4c7968 78define('GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER', 2);
e50ce569 79define('GRADE_REPORT_PREFERENCE_DEFAULT', 'default');
80define('GRADE_REPORT_PREFERENCE_INHERIT', 'inherit');
eea6690a 81
3058964f 82require_once($CFG->libdir . '/grade/grade_category.php');
83require_once($CFG->libdir . '/grade/grade_item.php');
ac9b0805 84require_once($CFG->libdir . '/grade/grade_grades.php');
d5bdb228 85require_once($CFG->libdir . '/grade/grade_scale.php');
5501446d 86require_once($CFG->libdir . '/grade/grade_outcome.php');
46566dd8 87require_once($CFG->libdir . '/grade/grade_grades_text.php');
8ff4550a 88require_once($CFG->libdir . '/grade/grade_tree.php');
60cf7430 89
612607bd 90/***** PUBLIC GRADE API *****/
91
c5b5f18d 92/**
93 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
ac9b0805 94 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
c5b5f18d 95 * or key means do not change existing.
4cf1b9be 96 *
c5b5f18d 97 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
e648f890 98 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
4cf1b9be 99 *
c5b5f18d 100 * @param string $source source of the grade such as 'mod/assignment', often used to prevent infinite loops when processing grade_updated events
101 * @param int $courseid id of course
102 * @param string $itemtype type of grade item - mod, block, gradecategory, calculated
103 * @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
104 * @param int $iteminstance instance it of graded subject
105 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
106 * @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating rgade_item definition only\
107 * @param mixed $itemdetails object or array describing the grading item, NULL if no change
108 */
b67ec72f 109function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
aaff71da 110 global $USER;
612607bd 111
c5b5f18d 112 // only following grade_item properties can be changed in this function
e648f890 113 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
612607bd 114
c4e4068f 115 // grade item identification
116 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
117
612607bd 118 if (is_null($courseid) or is_null($itemtype)) {
119 debugging('Missing courseid or itemtype');
120 return GRADE_UPDATE_FAILED;
121 }
122
c4e4068f 123 if (!$grade_items = grade_item::fetch_all($params)) {
612607bd 124 // create a new one
125 $grade_item = false;
126
127 } else if (count($grade_items) == 1){
128 $grade_item = reset($grade_items);
129 unset($grade_items); //release memory
130
131 } else {
34e67f76 132 debugging('Found more than one grade item');
612607bd 133 return GRADE_UPDATE_MULTIPLE;
134 }
135
aaff71da 136 if (!empty($itemdetails['deleted'])) {
137 if ($grade_item) {
138 if ($grade_item->delete($source)) {
139 return GRADE_UPDATE_OK;
140 } else {
141 return GRADE_UPDATE_FAILED;
142 }
143 }
144 return GRADE_UPDATE_OK;
145 }
146
612607bd 147/// Create or update the grade_item if needed
148 if (!$grade_item) {
612607bd 149 if ($itemdetails) {
150 $itemdetails = (array)$itemdetails;
2e53372c 151
772ddfbf 152 // grademin and grademax ignored when scale specified
2e53372c 153 if (array_key_exists('scaleid', $itemdetails)) {
154 if ($itemdetails['scaleid']) {
155 unset($itemdetails['grademin']);
156 unset($itemdetails['grademax']);
157 }
158 }
159
612607bd 160 foreach ($itemdetails as $k=>$v) {
161 if (!in_array($k, $allowed)) {
162 // ignore it
163 continue;
164 }
165 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
166 // no grade item needed!
167 return GRADE_UPDATE_OK;
168 }
169 $params[$k] = $v;
170 }
171 }
f70152b7 172 $grade_item = new grade_item($params);
173 $grade_item->insert();
612607bd 174
175 } else {
2cc4b0f9 176 if ($grade_item->is_locked()) {
612607bd 177 debugging('Grading item is locked!');
4cf1b9be 178 return GRADE_UPDATE_ITEM_LOCKED;
612607bd 179 }
180
181 if ($itemdetails) {
182 $itemdetails = (array)$itemdetails;
183 $update = false;
184 foreach ($itemdetails as $k=>$v) {
185 if (!in_array($k, $allowed)) {
186 // ignore it
187 continue;
188 }
189 if ($grade_item->{$k} != $v) {
190 $grade_item->{$k} = $v;
191 $update = true;
192 }
193 }
194 if ($update) {
195 $grade_item->update();
196 }
197 }
198 }
199
200/// Some extra checks
201 // do we use grading?
202 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
203 return GRADE_UPDATE_OK;
204 }
205
206 // no grade submitted
b67ec72f 207 if (empty($grades)) {
612607bd 208 return GRADE_UPDATE_OK;
209 }
210
612607bd 211/// Finally start processing of grades
b67ec72f 212 if (is_object($grades)) {
213 $grades = array($grades);
612607bd 214 } else {
b67ec72f 215 if (array_key_exists('userid', $grades)) {
216 $grades = array($grades);
612607bd 217 }
218 }
219
4cf1b9be 220 $failed = false;
612607bd 221 foreach ($grades as $grade) {
222 $grade = (array)$grade;
223 if (empty($grade['userid'])) {
4cf1b9be 224 $failed = true;
225 debugging('Invalid userid in grade submitted');
226 continue;
ac9b0805 227 } else {
228 $userid = $grade['userid'];
612607bd 229 }
230
2cc4b0f9 231 $rawgrade = false;
ac9b0805 232 $feedback = false;
233 $feedbackformat = FORMAT_MOODLE;
772ddfbf 234
ac9b0805 235 if (array_key_exists('rawgrade', $grade)) {
236 $rawgrade = $grade['rawgrade'];
237 }
612607bd 238
4cf1b9be 239 if (array_key_exists('feedback', $grade)) {
ac9b0805 240 $feedback = $grade['feedback'];
612607bd 241 }
242
ac9b0805 243 if (array_key_exists('feedbackformat', $grade)) {
244 $feedbackformat = $grade['feedbackformat'];
612607bd 245 }
246
aaff71da 247 if (array_key_exists('usermodified', $grade)) {
248 $usermodified = $grade['usermodified'];
249 } else {
250 $usermodified = $USER->id;
251 }
252
ac9b0805 253 // update or insert the grade
aaff71da 254 if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat, $usermodified)) {
4cf1b9be 255 $failed = true;
4cf1b9be 256 }
612607bd 257 }
258
4cf1b9be 259 if (!$failed) {
260 return GRADE_UPDATE_OK;
261 } else {
262 return GRADE_UPDATE_FAILED;
263 }
612607bd 264}
265
b67ec72f 266
267/**
268* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
269* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
270* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
271* If no $userid is given, the method will always return the grade_item's locked state.
272* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
273* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
274* return the locked state of the specific grade.
275*
34e67f76 276* @param int $courseid id of course
b67ec72f 277* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
278* @param string $itemmodule 'forum, 'quiz', 'csv' etc
279* @param int $iteminstance id of the item module
34e67f76 280* @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
281* @param int $userid ID of the graded user
b67ec72f 282* @return boolean Whether the grade is locked or not
283*/
34e67f76 284function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
b67ec72f 285
f92dcad8 286 if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) {
34e67f76 287 return false;
288
289 } else if (count($grade_items) == 1){
290 $grade_item = reset($grade_items);
291 return $grade_item->is_locked($userid);
292
293 } else {
294 debugging('Found more than one grade item');
295 foreach ($grade_items as $grade_item) {
296 if ($grade_item->is_locked($userid)) {
297 return true;
298 }
299 }
300 return false;
301 }
302}
b67ec72f 303
612607bd 304/***** END OF PUBLIC API *****/
305
f8e6e4db 306function grade_force_full_regrading($courseid) {
307 set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
308}
34e67f76 309
5834dcdb 310/**
ac9b0805 311 * Updates all final grades in course.
a8995b34 312 *
313 * @param int $courseid
f8e6e4db 314 * @param int $userid if specified, try to do a quick regrading of grades of this user only
315 * @param object $updated_item the item in which
316 * @return boolean true if ok, array of errors if problems found (item id is used as key)
a8995b34 317 */
c86caae7 318function grade_regrade_final_grades($courseid, $userid=null, $updated_item=null) {
a8995b34 319
f8e6e4db 320 $course_item = grade_item::fetch_course_item($courseid);
b8ff92b6 321
f8e6e4db 322 if ($userid) {
323 // one raw grade updated for one user
324 if (empty($updated_item)) {
325 error("updated_item_id can not be null!");
326 }
327 if ($course_item->needsupdate) {
328 $updated_item->force_regrading();
329 return 'Can not do fast regrading after updating of raw grades';
a8995b34 330 }
772ddfbf 331
f8e6e4db 332 } else {
333 if (!$course_item->needsupdate) {
334 // nothing to do :-)
b8ff92b6 335 return true;
b8ff92b6 336 }
a8995b34 337 }
338
f8e6e4db 339 $grade_items = grade_item::fetch_all(array('courseid'=>$courseid));
340 $depends_on = array();
341
342 // first mark all category and calculated items as needing regrading
343 // this is slower, but 100% accurate - this function is called only when there is
344 // a change in grading setup, update of individual grade does not trigger this function
345 foreach ($grade_items as $gid=>$gitem) {
fb46b5b6 346 if (!empty($updated_item) and $updated_item->id == $gid) {
f8e6e4db 347 $grade_items[$gid]->needsupdate = 1;
348
349 } else if ($grade_items[$gid]->is_category_item() or $grade_items[$gid]->is_calculated()) {
350 $grade_items[$gid]->needsupdate = 1;
351 }
2e53372c 352
f8e6e4db 353 // construct depends_on lookup array
354 $depends_on[$gid] = $grade_items[$gid]->depends_on();
355 }
2e53372c 356 $errors = array();
357
b8ff92b6 358 $finalitems = array();
359 $finalids = array();
360 while (count($grade_items) > 0) {
f8e6e4db 361 $count = 0; // count how many items were updated in this cycle
b8ff92b6 362 foreach ($grade_items as $gid=>$gitem) {
363 $grade_item =& $grade_items[$gid];
2e53372c 364 if (!$grade_item->needsupdate) {
b8ff92b6 365 $finalitems[$gid] = $grade_item;
366 $finalids[] = $gid;
367 unset($grade_items[$gid]);
368 continue;
369 }
370
b8ff92b6 371 $doupdate = true;
f8e6e4db 372 foreach ($depends_on[$gid] as $did) {
b8ff92b6 373 if (!in_array($did, $finalids)) {
374 $doupdate = false;
f8e6e4db 375 break;
b8ff92b6 376 }
377 }
378
379 //oki - let's update, calculate or aggregate :-)
380 if ($doupdate) {
c86caae7 381 $result = $grade_item->regrade_final_grades($userid);
f8e6e4db 382
383 if ($result === true) {
384 $grade_item->regrading_finished();
385 $count++;
b8ff92b6 386 $finalitems[$gid] = $grade_item;
387 $finalids[] = $gid;
388 unset($grade_items[$gid]);
f8e6e4db 389 } else {
390 $grade_item->force_regrading();
391 $errors[$gid] = $result;
b8ff92b6 392 }
393 }
394 }
395
396 if ($count == 0) {
397 foreach($grade_items as $grade_item) {
f8e6e4db 398 $grade_item->force_regrading();
399 $errors[$grade_item->id] = 'Probably circular reference or broken calculation formula'; // TODO: localize
b8ff92b6 400 }
401 break;
402 }
403 }
404
405 if (count($errors) == 0) {
406 return true;
407 } else {
408 return $errors;
409 }
a8995b34 410}
967f222f 411
de420c11 412/**
d185c3ee 413 * For backwards compatibility with old third-party modules, this function can
414 * be used to import all grades from activities with legacy grading.
739196ba 415 * @param int $courseid or null if all courses
967f222f 416 */
739196ba 417function grade_grab_legacy_grades($courseid=null) {
612607bd 418
ac9b0805 419 global $CFG;
967f222f 420
421 if (!$mods = get_list_of_plugins('mod') ) {
422 error('No modules installed!');
423 }
424
739196ba 425 if ($courseid) {
426 $course_sql = " AND cm.course=$courseid";
427 } else {
428 $course_sql = "";
429 }
430
967f222f 431 foreach ($mods as $mod) {
432
433 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
434 continue;
435 }
436
d185c3ee 437 if (!$module = get_record('modules', 'name', $mod)) {
438 //not installed
439 continue;
440 }
441
442 if (!$module->visible) {
443 //disabled module
444 continue;
445 }
446
447 $fullmod = $CFG->dirroot.'/mod/'.$mod;
967f222f 448
449 // include the module lib once
450 if (file_exists($fullmod.'/lib.php')) {
451 include_once($fullmod.'/lib.php');
de420c11 452 // look for modname_grades() function - old gradebook pulling function
453 // if present sync the grades with new grading system
967f222f 454 $gradefunc = $mod.'_grades';
de420c11 455 if (function_exists($gradefunc)) {
456
457 // get all instance of the activity
d185c3ee 458 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
459 FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
739196ba 460 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id $course_sql";
de420c11 461
462 if ($modinstances = get_records_sql($sql)) {
967f222f 463 foreach ($modinstances as $modinstance) {
d185c3ee 464 grade_update_mod_grades($modinstance);
967f222f 465 }
466 }
467 }
468 }
469 }
470}
471
ac9b0805 472/**
473 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
474 */
475function grade_grab_grades() {
476
477 global $CFG;
478
479 if (!$mods = get_list_of_plugins('mod') ) {
480 error('No modules installed!');
481 }
482
483 foreach ($mods as $mod) {
484
485 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
486 continue;
487 }
488
489 if (!$module = get_record('modules', 'name', $mod)) {
490 //not installed
491 continue;
492 }
493
494 if (!$module->visible) {
495 //disabled module
496 continue;
497 }
498
499 $fullmod = $CFG->dirroot.'/mod/'.$mod;
500
501 // include the module lib once
502 if (file_exists($fullmod.'/lib.php')) {
503 include_once($fullmod.'/lib.php');
504 // look for modname_grades() function - old gradebook pulling function
505 // if present sync the grades with new grading system
506 $gradefunc = $mod.'_update_grades';
507 if (function_exists($gradefunc)) {
508 $gradefunc();
509 }
510 }
511 }
512}
513
d185c3ee 514/**
515 * Force full update of module grades in central gradebook - works for both legacy and converted activities.
516 * @param object $modinstance object with extra cmidnumber and modname property
517 * @return boolean success
518 */
519function grade_update_mod_grades($modinstance) {
520 global $CFG;
521
522 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
523 if (!file_exists($fullmod.'/lib.php')) {
524 debugging('missing lib.php file in module');
525 return false;
526 }
527 include_once($fullmod.'/lib.php');
528
529 // does it use legacy grading?
530 $gradefunc = $modinstance->modname.'_grades';
531 $updategradesfunc = $modinstance->modname.'_update_grades';
532 $updateitemfunc = $modinstance->modname.'_grade_item_update';
533
534 if (function_exists($gradefunc)) {
535 if ($oldgrades = $gradefunc($modinstance->id)) {
536
537 $grademax = $oldgrades->maxgrade;
538 $scaleid = NULL;
539 if (!is_numeric($grademax)) {
540 // scale name is provided as a string, try to find it
541 if (!$scale = get_record('scale', 'name', $grademax)) {
542 debugging('Incorrect scale name! name:'.$grademax);
543 return false;
544 }
545 $scaleid = $scale->id;
546 }
547
548 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
549 debugging('Can not get/create legacy grade item!');
550 return false;
551 }
552
553 $grades = array();
554 foreach ($oldgrades->grades as $userid=>$usergrade) {
555 $grade = new object();
556 $grade->userid = $userid;
557
558 if ($usergrade == '-') {
559 // no grade
ac9b0805 560 $grade->rawgrade = null;
d185c3ee 561
562 } else if ($scaleid) {
563 // scale in use, words used
564 $gradescale = explode(",", $scale->scale);
ac9b0805 565 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
d185c3ee 566
567 } else {
568 // good old numeric value
ac9b0805 569 $grade->rawgrade = $usergrade;
d185c3ee 570 }
571 $grades[] = $grade;
572 }
573
574 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
575 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
576 }
577
578 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
579 //new grading supported, force updating of grades
580 $updateitemfunc($modinstance);
581 $updategradesfunc($modinstance);
582
583 } else {
584 // mudule does not support grading
585 }
586
587 return true;
588}
de420c11 589
590/**
d185c3ee 591 * Get and update/create grade item for legacy modules.
de420c11 592 */
593function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
594
595 // does it already exist?
f92dcad8 596 if ($grade_items = grade_grades::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>0))) {
de420c11 597 if (count($grade_items) > 1) {
d185c3ee 598 debugging('Multiple legacy grade_items found.');
de420c11 599 return false;
600 }
601
602 $grade_item = reset($grade_items);
de420c11 603
d185c3ee 604 if (is_null($grademax) and is_null($scaleid)) {
605 $grade_item->gradetype = GRADE_TYPE_NONE;
de420c11 606
d185c3ee 607 } else if ($scaleid) {
608 $grade_item->gradetype = GRADE_TYPE_SCALE;
609 $grade_item->scaleid = $scaleid;
97d608ba 610 $grade_item->grademin = 1;
de420c11 611
d185c3ee 612 } else {
97d608ba 613 $grade_item->gradetype = GRADE_TYPE_VALUE;
614 $grade_item->grademax = $grademax;
615 $grade_item->grademin = 0;
de420c11 616 }
617
d185c3ee 618 $grade_item->itemname = $modinstance->name;
619 $grade_item->idnumber = $modinstance->cmidnumber;
de420c11 620
d185c3ee 621 $grade_item->update();
de420c11 622
623 return $grade_item;
624 }
612607bd 625
de420c11 626 // create new one
d185c3ee 627 $params = array('courseid' =>$modinstance->course,
de420c11 628 'itemtype' =>'mod',
629 'itemmodule' =>$modinstance->modname,
630 'iteminstance'=>$modinstance->id,
d185c3ee 631 'itemnumber' =>0,
de420c11 632 'itemname' =>$modinstance->name,
633 'idnumber' =>$modinstance->cmidnumber);
634
d185c3ee 635 if (is_null($grademax) and is_null($scaleid)) {
636 $params['gradetype'] = GRADE_TYPE_NONE;
637
638 } else if ($scaleid) {
612607bd 639 $params['gradetype'] = GRADE_TYPE_SCALE;
de420c11 640 $params['scaleid'] = $scaleid;
b3ac6c3e 641 $grade_item->grademin = 1;
de420c11 642 } else {
612607bd 643 $params['gradetype'] = GRADE_TYPE_VALUE;
de420c11 644 $params['grademax'] = $grademax;
645 $params['grademin'] = 0;
646 }
647
f70152b7 648 $grade_item = new grade_item($params);
649 $grade_item->insert();
de420c11 650
f70152b7 651 return $grade_item;
de420c11 652}
653
b8ff92b6 654/**
655 * This function is used to migrade old date and settings from old gradebook into new grading system.
656 *
657 * TODO:
658 * - category weight not used - we would have to create extra top course grade calculated category
659 * - exta_credit item flag not used - does not fit all our aggregation types, could be used in SUM only
660 */
661function grade_oldgradebook_upgrade($courseid) {
662 global $CFG;
663
664 $categories = array();
665 if ($oldcats = get_records('grade_category', 'courseid', $courseid)) {
666 foreach ($oldcats as $oldcat) {
667 $newcat = new grade_category(array('courseid'=>$courseid, 'fullname'=>$oldcat->name));
668 $newcat->droplow = $oldcat->drop_x_lowest;
669 $newcat->aggregation = GRADE_AGGREGATE_MEAN_GRADED;
670
671 if (empty($newcat->id)) {
672 $newcat->insert();
673 } else {
674 $newcat->update();
675 }
676
677 $categories[$oldcat->id] = $newcat;
678
679 $catitem = $newcat->get_grade_item();
680 $catitem->gradetype = GRADE_TYPE_VALUE;
681 $catitem->plusfactor = $oldcat->bonus_points;
682 $catitem->hidden = $oldcat->hidden;
683 $catitem->update();
684 }
685 }
686
687 // get all grade items with mod details
688 $sql = "SELECT gi.*, cm.idnumber as cmidnumber, m.name as modname
689 FROM {$CFG->prefix}grade_item gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
690 WHERE gi.courseid=$courseid AND m.id=gi.modid AND cm.instance=gi.cminstance
691 ORDER BY gi.sortorder ASC";
692
693 if ($olditems = get_records_sql($sql)) {
694 foreach ($olditems as $olditem) {
695 $newitem = new grade_item(array('courseid'=>$olditem->courseid, 'itemtype'=>'mod', 'itemmodule'=>$olditem->modname, 'iteminstance'=>$olditem->cminstance, 'itemnumber'=>0));
696 if (!empty($olditem->category)) {
697 // we do this low level stuff to get some speedup during upgrade
f13002d5 698 $newitem->set_parent($categories[$olditem->category]->id);
b8ff92b6 699 }
700 $newitem->gradetype = GRADE_TYPE_NONE;
701 $newitem->multfactor = $olditem->scale_grade;
702 if (empty($newitem->id)) {
703 $newitem->insert();
704 } else {
705 $newitem->update();
706 }
707 }
708 }
709}
bfe7297e 710
60cf7430 711?>