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