Gradebook unit tests review
[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
eacd3700 35// category aggregation types
b8ff92b6 36define('GRADE_AGGREGATE_MEAN_ALL', 0);
9580a21f 37define('GRADE_AGGREGATE_MEAN_GRADED', 1);
eacd3700 38define('GRADE_AGGREGATE_MEDIAN_ALL', 2);
39define('GRADE_AGGREGATE_MEDIAN_GRADED', 3);
40define('GRADE_AGGREGATE_MIN_ALL', 4);
41define('GRADE_AGGREGATE_MIN_GRADED', 5);
42define('GRADE_AGGREGATE_MAX_ALL', 6);
43define('GRADE_AGGREGATE_MAX_GRADED', 7);
44define('GRADE_AGGREGATE_MODE_ALL', 8);
45define('GRADE_AGGREGATE_MODE_GRADED', 9);
46define('GRADE_AGGREGATE_WEIGHTED_MEAN_ALL', 10);
47define('GRADE_AGGREGATE_WEIGHTED_MEAN_GRADED', 11);
48define('GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL', 12);
49define('GRADE_AGGREGATE_EXTRACREDIT_MEAN_GRADED', 13);
50
51// grade types
210611f6 52define('GRADE_TYPE_NONE', 0);
53define('GRADE_TYPE_VALUE', 1);
54define('GRADE_TYPE_SCALE', 2);
55define('GRADE_TYPE_TEXT', 3);
42bbccd7 56
eacd3700 57// grade_update() return status
612607bd 58define('GRADE_UPDATE_OK', 0);
59define('GRADE_UPDATE_FAILED', 1);
60define('GRADE_UPDATE_MULTIPLE', 2);
61define('GRADE_UPDATE_ITEM_DELETED', 3);
4cf1b9be 62define('GRADE_UPDATE_ITEM_LOCKED', 4);
612607bd 63
aaff71da 64// Grate teables history tracking actions
65define('GRADE_HISTORY_INSERT', 1);
66define('GRADE_HISTORY_UPDATE', 2);
67define('GRADE_HISTORY_DELETE', 3);
68
bb384a8e 69define('GRADE_REPORT_AGGREGATION_POSITION_LEFT', 0);
70define('GRADE_REPORT_AGGREGATION_POSITION_RIGHT', 1);
71define('GRADE_REPORT_AGGREGATION_VIEW_FULL', 0);
72define('GRADE_REPORT_AGGREGATION_VIEW_COMPACT', 1);
db4c7968 73define('GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL', 0);
bb384a8e 74define('GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE', 1);
db4c7968 75define('GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER', 2);
e50ce569 76define('GRADE_REPORT_PREFERENCE_DEFAULT', 'default');
77define('GRADE_REPORT_PREFERENCE_INHERIT', 'inherit');
32b97bb2 78define('GRADE_REPORT_PREFERENCE_UNUSED', -1);
eea6690a 79
3058964f 80require_once($CFG->libdir . '/grade/grade_category.php');
81require_once($CFG->libdir . '/grade/grade_item.php');
3ee5c201 82require_once($CFG->libdir . '/grade/grade_grade.php');
d5bdb228 83require_once($CFG->libdir . '/grade/grade_scale.php');
5501446d 84require_once($CFG->libdir . '/grade/grade_outcome.php');
3ee5c201 85require_once($CFG->libdir . '/grade/grade_grade_text.php');
60cf7430 86
612607bd 87/***** PUBLIC GRADE API *****/
88
c5b5f18d 89/**
90 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
ac9b0805 91 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
c5b5f18d 92 * or key means do not change existing.
4cf1b9be 93 *
c5b5f18d 94 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
e648f890 95 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
4cf1b9be 96 *
c5b5f18d 97 * @param string $source source of the grade such as 'mod/assignment', often used to prevent infinite loops when processing grade_updated events
98 * @param int $courseid id of course
3a5ae660 99 * @param string $itemtype type of grade item - mod, block
c5b5f18d 100 * @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
101 * @param int $iteminstance instance it of graded subject
102 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
103 * @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating rgade_item definition only\
104 * @param mixed $itemdetails object or array describing the grading item, NULL if no change
105 */
b67ec72f 106function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
aaff71da 107 global $USER;
612607bd 108
c5b5f18d 109 // only following grade_item properties can be changed in this function
e648f890 110 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
612607bd 111
c4e4068f 112 // grade item identification
113 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
114
612607bd 115 if (is_null($courseid) or is_null($itemtype)) {
116 debugging('Missing courseid or itemtype');
117 return GRADE_UPDATE_FAILED;
118 }
119
c4e4068f 120 if (!$grade_items = grade_item::fetch_all($params)) {
612607bd 121 // create a new one
122 $grade_item = false;
123
124 } else if (count($grade_items) == 1){
125 $grade_item = reset($grade_items);
126 unset($grade_items); //release memory
127
128 } else {
34e67f76 129 debugging('Found more than one grade item');
612607bd 130 return GRADE_UPDATE_MULTIPLE;
131 }
132
aaff71da 133 if (!empty($itemdetails['deleted'])) {
134 if ($grade_item) {
135 if ($grade_item->delete($source)) {
136 return GRADE_UPDATE_OK;
137 } else {
138 return GRADE_UPDATE_FAILED;
139 }
140 }
141 return GRADE_UPDATE_OK;
142 }
143
612607bd 144/// Create or update the grade_item if needed
145 if (!$grade_item) {
612607bd 146 if ($itemdetails) {
147 $itemdetails = (array)$itemdetails;
2e53372c 148
772ddfbf 149 // grademin and grademax ignored when scale specified
2e53372c 150 if (array_key_exists('scaleid', $itemdetails)) {
151 if ($itemdetails['scaleid']) {
152 unset($itemdetails['grademin']);
153 unset($itemdetails['grademax']);
154 }
155 }
156
612607bd 157 foreach ($itemdetails as $k=>$v) {
158 if (!in_array($k, $allowed)) {
159 // ignore it
160 continue;
161 }
162 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
163 // no grade item needed!
164 return GRADE_UPDATE_OK;
165 }
166 $params[$k] = $v;
167 }
168 }
f70152b7 169 $grade_item = new grade_item($params);
170 $grade_item->insert();
612607bd 171
172 } else {
2cc4b0f9 173 if ($grade_item->is_locked()) {
612607bd 174 debugging('Grading item is locked!');
4cf1b9be 175 return GRADE_UPDATE_ITEM_LOCKED;
612607bd 176 }
177
178 if ($itemdetails) {
179 $itemdetails = (array)$itemdetails;
180 $update = false;
181 foreach ($itemdetails as $k=>$v) {
182 if (!in_array($k, $allowed)) {
183 // ignore it
184 continue;
185 }
186 if ($grade_item->{$k} != $v) {
187 $grade_item->{$k} = $v;
188 $update = true;
189 }
190 }
191 if ($update) {
192 $grade_item->update();
193 }
194 }
195 }
196
197/// Some extra checks
198 // do we use grading?
199 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
200 return GRADE_UPDATE_OK;
201 }
202
203 // no grade submitted
b67ec72f 204 if (empty($grades)) {
612607bd 205 return GRADE_UPDATE_OK;
206 }
207
612607bd 208/// Finally start processing of grades
b67ec72f 209 if (is_object($grades)) {
210 $grades = array($grades);
612607bd 211 } else {
b67ec72f 212 if (array_key_exists('userid', $grades)) {
213 $grades = array($grades);
612607bd 214 }
215 }
216
4cf1b9be 217 $failed = false;
612607bd 218 foreach ($grades as $grade) {
219 $grade = (array)$grade;
220 if (empty($grade['userid'])) {
4cf1b9be 221 $failed = true;
222 debugging('Invalid userid in grade submitted');
223 continue;
ac9b0805 224 } else {
225 $userid = $grade['userid'];
612607bd 226 }
227
2cc4b0f9 228 $rawgrade = false;
ac9b0805 229 $feedback = false;
230 $feedbackformat = FORMAT_MOODLE;
772ddfbf 231
ac9b0805 232 if (array_key_exists('rawgrade', $grade)) {
233 $rawgrade = $grade['rawgrade'];
234 }
612607bd 235
4cf1b9be 236 if (array_key_exists('feedback', $grade)) {
ac9b0805 237 $feedback = $grade['feedback'];
612607bd 238 }
239
ac9b0805 240 if (array_key_exists('feedbackformat', $grade)) {
241 $feedbackformat = $grade['feedbackformat'];
612607bd 242 }
243
aaff71da 244 if (array_key_exists('usermodified', $grade)) {
245 $usermodified = $grade['usermodified'];
246 } else {
247 $usermodified = $USER->id;
248 }
249
ac9b0805 250 // update or insert the grade
aaff71da 251 if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat, $usermodified)) {
4cf1b9be 252 $failed = true;
4cf1b9be 253 }
612607bd 254 }
255
4cf1b9be 256 if (!$failed) {
257 return GRADE_UPDATE_OK;
258 } else {
259 return GRADE_UPDATE_FAILED;
260 }
612607bd 261}
262
b67ec72f 263
264/**
3a5ae660 265 * Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
266 * If it's locked to the current use then the module can print a nice message or prevent editing in the module.
267 * If no $userid is given, the method will always return the grade_item's locked state.
268 * If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
269 * the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
270 * return the locked state of the specific grade.
271 *
272 * @param int $courseid id of course
273 * @param string $itemtype 'mod', 'block'
274 * @param string $itemmodule 'forum, 'quiz', etc.
275 * @param int $iteminstance id of the item module
276 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
277 * @param int $userid ID of the graded user
278 * @return boolean Whether the grade is locked or not
279 */
34e67f76 280function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
b67ec72f 281
f92dcad8 282 if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) {
34e67f76 283 return false;
284
285 } else if (count($grade_items) == 1){
286 $grade_item = reset($grade_items);
287 return $grade_item->is_locked($userid);
288
289 } else {
290 debugging('Found more than one grade item');
291 foreach ($grade_items as $grade_item) {
292 if ($grade_item->is_locked($userid)) {
293 return true;
294 }
295 }
296 return false;
297 }
298}
b67ec72f 299
3a5ae660 300/**
301 * Returns list of outcomes used in course together with current outcomes for this user
302 * @param int $courseid id of course
303 * @param string $itemtype 'mod', 'block'
304 * @param string $itemmodule 'forum, 'quiz', etc.
305 * @param int $iteminstance id of the item module
306 * @param int $userid ID of the graded user
307 * @return array of outcome information objects (scaleid, name, grade and locked status) indexed with itemnumbers
308 */
309function grade_get_outcomes($courseid, $itemtype, $itemmodule, $iteminstance, $userid) {
310 $result = array();
311 if ($items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
312 foreach ($items as $item) {
313 if (empty($item->outcomeid)) {
314 continue;
315 }
316 if (!$outcome = grade_outcome::fetch(array('id'=>$item->outcomeid))) {
317 debugging('Incorect outcomeid found');
318 continue;
319 }
320 // prepare outcome info with user grade
321 $o = new object();
322 $o->scaleid = $outcome->scaleid;
323 $o->name = $outcome->fullname;
324
325 if ($grade = $item->get_grade($userid,false)) {
326 $o->grade = $grade->finalgrade;
327 $o->locked = $grade->is_locked();
328 } else {
329 $o->grade = null;
330 $o->locked = $item->is_locked();
331 }
332 $o->grade = intval($o->grade); // 0 means no grade, int for scales
11a14999 333
3a5ae660 334 $result[$item->itemnumber] = $o;
335 }
336 }
337
338 return $result;
339}
340
341/**
342 * Updates outcomes of user
343 * @param int $courseid id of course
344 * @param string $itemtype 'mod', 'block'
345 * @param string $itemmodule 'forum, 'quiz', etc.
346 * @param int $iteminstance id of the item module
347 * @param int $userid ID of the graded user
348 */
349function grade_update_outcomes($source, $courseid, $itemtype, $itemmodule, $iteminstance, $userid, $data) {
350 if ($items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
351 foreach ($items as $item) {
352 if (!array_key_exists($item->itemnumber, $data)) {
353 continue;
354 }
355 $grade = $data[$item->itemnumber] < 1 ? null : $data[$item->itemnumber];
356 $item->update_final_grade($userid, $grade, $source);
11a14999 357 }
3a5ae660 358 }
359}
360
612607bd 361/***** END OF PUBLIC API *****/
362
60243313 363
364/**
365 * Verify nwe value of idnumber - checks for uniqueness of new idnubmers, old are kept intact
366 * @param string idnumber string (with magic quotes)
367 * @param object $cm used for course module idnumbers and items attached to modules
368 * @param object $gradeitem is item idnumber
369 * @return boolean true means idnumber ok
370 */
371function grade_verify_idnumber($idnumber, $grade_item=null, $cm=null) {
372 if ($idnumber == '') {
373 //we allow empty idnumbers
374 return true;
375 }
376
377 // keep existing even when not unique
378 if ($cm and $cm->idnumber == $idnumber) {
379 return true;
380 } else if ($grade_item and $grade_item->idnumber == $idnumber) {
381 return true;
382 }
383
384 if (get_records('course_modules', 'idnumber', $idnumber)) {
385 return false;
386 }
387
388 if (get_records('grade_items', 'idnumber', $idnumber)) {
389 return false;
390 }
391
392 return true;
393}
394
395/**
396 * Force final grade recalculation in all course items
397 * @param int $courseid
398 */
f8e6e4db 399function grade_force_full_regrading($courseid) {
400 set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
401}
34e67f76 402
5834dcdb 403/**
ac9b0805 404 * Updates all final grades in course.
a8995b34 405 *
406 * @param int $courseid
f8e6e4db 407 * @param int $userid if specified, try to do a quick regrading of grades of this user only
408 * @param object $updated_item the item in which
409 * @return boolean true if ok, array of errors if problems found (item id is used as key)
a8995b34 410 */
c86caae7 411function grade_regrade_final_grades($courseid, $userid=null, $updated_item=null) {
b8ff92b6 412
514a3467 413 $course_item = grade_item::fetch_course_item($courseid);
f04873a9 414
f8e6e4db 415 if ($userid) {
416 // one raw grade updated for one user
417 if (empty($updated_item)) {
418 error("updated_item_id can not be null!");
419 }
420 if ($course_item->needsupdate) {
421 $updated_item->force_regrading();
422 return 'Can not do fast regrading after updating of raw grades';
a8995b34 423 }
772ddfbf 424
f8e6e4db 425 } else {
426 if (!$course_item->needsupdate) {
427 // nothing to do :-)
b8ff92b6 428 return true;
b8ff92b6 429 }
a8995b34 430 }
431
f8e6e4db 432 $grade_items = grade_item::fetch_all(array('courseid'=>$courseid));
433 $depends_on = array();
434
435 // first mark all category and calculated items as needing regrading
436 // this is slower, but 100% accurate - this function is called only when there is
437 // a change in grading setup, update of individual grade does not trigger this function
438 foreach ($grade_items as $gid=>$gitem) {
fb46b5b6 439 if (!empty($updated_item) and $updated_item->id == $gid) {
f8e6e4db 440 $grade_items[$gid]->needsupdate = 1;
441
eacd3700 442 } else if ($gitem->is_course_item() or $gitem->is_category_item() or $gitem->is_calculated()) {
f8e6e4db 443 $grade_items[$gid]->needsupdate = 1;
444 }
2e53372c 445
f8e6e4db 446 // construct depends_on lookup array
447 $depends_on[$gid] = $grade_items[$gid]->depends_on();
448 }
2e53372c 449
d14ae855 450 $errors = array();
b8ff92b6 451 $finalids = array();
d14ae855 452 $gids = array_keys($grade_items);
eacd3700 453 $failed = 0;
d14ae855 454
455 while (count($finalids) < count($gids)) { // work until all grades are final or error found
456 $count = 0;
457 foreach ($gids as $gid) {
458 if (in_array($gid, $finalids)) {
459 continue; // already final
460 }
461
462 if (!$grade_items[$gid]->needsupdate) {
463 $finalids[] = $gid; // we can make it final - does not need update
b8ff92b6 464 continue;
465 }
466
b8ff92b6 467 $doupdate = true;
f8e6e4db 468 foreach ($depends_on[$gid] as $did) {
b8ff92b6 469 if (!in_array($did, $finalids)) {
470 $doupdate = false;
d14ae855 471 continue; // this item depends on something that is not yet in finals array
b8ff92b6 472 }
473 }
474
475 //oki - let's update, calculate or aggregate :-)
476 if ($doupdate) {
d14ae855 477 $result = $grade_items[$gid]->regrade_final_grades($userid);
f8e6e4db 478
479 if ($result === true) {
d14ae855 480 $grade_items[$gid]->regrading_finished();
f8e6e4db 481 $count++;
b8ff92b6 482 $finalids[] = $gid;
f8e6e4db 483 } else {
d14ae855 484 $grade_items[$gid]->force_regrading();
f8e6e4db 485 $errors[$gid] = $result;
b8ff92b6 486 }
487 }
488 }
489
490 if ($count == 0) {
eacd3700 491 $failed++;
492 } else {
493 $failed = 0;
494 }
495
496 if ($failed > 1) {
d14ae855 497 foreach($gids as $gid) {
498 if (in_array($gid, $finalids)) {
499 continue; // this one is ok
500 }
501 $grade_items[$gid]->force_regrading();
502 $errors[$grade_items[$gid]->id] = 'Probably circular reference or broken calculation formula'; // TODO: localize
b8ff92b6 503 }
d14ae855 504 break; // oki, found error
b8ff92b6 505 }
506 }
507
508 if (count($errors) == 0) {
509 return true;
510 } else {
511 return $errors;
512 }
a8995b34 513}
967f222f 514
de420c11 515/**
d185c3ee 516 * For backwards compatibility with old third-party modules, this function can
517 * be used to import all grades from activities with legacy grading.
739196ba 518 * @param int $courseid or null if all courses
967f222f 519 */
739196ba 520function grade_grab_legacy_grades($courseid=null) {
612607bd 521
ac9b0805 522 global $CFG;
967f222f 523
524 if (!$mods = get_list_of_plugins('mod') ) {
525 error('No modules installed!');
526 }
527
739196ba 528 if ($courseid) {
529 $course_sql = " AND cm.course=$courseid";
530 } else {
531 $course_sql = "";
532 }
533
967f222f 534 foreach ($mods as $mod) {
535
536 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
537 continue;
538 }
539
d185c3ee 540 if (!$module = get_record('modules', 'name', $mod)) {
541 //not installed
542 continue;
543 }
544
545 if (!$module->visible) {
546 //disabled module
547 continue;
548 }
549
550 $fullmod = $CFG->dirroot.'/mod/'.$mod;
967f222f 551
552 // include the module lib once
553 if (file_exists($fullmod.'/lib.php')) {
554 include_once($fullmod.'/lib.php');
de420c11 555 // look for modname_grades() function - old gradebook pulling function
556 // if present sync the grades with new grading system
967f222f 557 $gradefunc = $mod.'_grades';
de420c11 558 if (function_exists($gradefunc)) {
559
560 // get all instance of the activity
d185c3ee 561 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
562 FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
739196ba 563 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id $course_sql";
de420c11 564
565 if ($modinstances = get_records_sql($sql)) {
967f222f 566 foreach ($modinstances as $modinstance) {
d185c3ee 567 grade_update_mod_grades($modinstance);
967f222f 568 }
569 }
570 }
571 }
572 }
573}
574
ac9b0805 575/**
576 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
577 */
578function grade_grab_grades() {
579
580 global $CFG;
581
582 if (!$mods = get_list_of_plugins('mod') ) {
583 error('No modules installed!');
584 }
585
586 foreach ($mods as $mod) {
587
588 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
589 continue;
590 }
591
592 if (!$module = get_record('modules', 'name', $mod)) {
593 //not installed
594 continue;
595 }
596
597 if (!$module->visible) {
598 //disabled module
599 continue;
600 }
601
602 $fullmod = $CFG->dirroot.'/mod/'.$mod;
603
604 // include the module lib once
605 if (file_exists($fullmod.'/lib.php')) {
606 include_once($fullmod.'/lib.php');
607 // look for modname_grades() function - old gradebook pulling function
608 // if present sync the grades with new grading system
609 $gradefunc = $mod.'_update_grades';
610 if (function_exists($gradefunc)) {
611 $gradefunc();
612 }
613 }
614 }
615}
616
d185c3ee 617/**
618 * Force full update of module grades in central gradebook - works for both legacy and converted activities.
619 * @param object $modinstance object with extra cmidnumber and modname property
620 * @return boolean success
621 */
622function grade_update_mod_grades($modinstance) {
623 global $CFG;
624
625 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
626 if (!file_exists($fullmod.'/lib.php')) {
627 debugging('missing lib.php file in module');
628 return false;
629 }
630 include_once($fullmod.'/lib.php');
631
632 // does it use legacy grading?
633 $gradefunc = $modinstance->modname.'_grades';
634 $updategradesfunc = $modinstance->modname.'_update_grades';
635 $updateitemfunc = $modinstance->modname.'_grade_item_update';
636
637 if (function_exists($gradefunc)) {
638 if ($oldgrades = $gradefunc($modinstance->id)) {
639
640 $grademax = $oldgrades->maxgrade;
641 $scaleid = NULL;
642 if (!is_numeric($grademax)) {
643 // scale name is provided as a string, try to find it
644 if (!$scale = get_record('scale', 'name', $grademax)) {
645 debugging('Incorrect scale name! name:'.$grademax);
646 return false;
647 }
648 $scaleid = $scale->id;
649 }
650
651 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
652 debugging('Can not get/create legacy grade item!');
653 return false;
654 }
655
656 $grades = array();
657 foreach ($oldgrades->grades as $userid=>$usergrade) {
658 $grade = new object();
659 $grade->userid = $userid;
660
661 if ($usergrade == '-') {
662 // no grade
ac9b0805 663 $grade->rawgrade = null;
d185c3ee 664
665 } else if ($scaleid) {
666 // scale in use, words used
667 $gradescale = explode(",", $scale->scale);
ac9b0805 668 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
d185c3ee 669
670 } else {
671 // good old numeric value
ac9b0805 672 $grade->rawgrade = $usergrade;
d185c3ee 673 }
674 $grades[] = $grade;
675 }
676
677 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
678 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
679 }
680
681 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
682 //new grading supported, force updating of grades
683 $updateitemfunc($modinstance);
684 $updategradesfunc($modinstance);
685
686 } else {
687 // mudule does not support grading
688 }
689
690 return true;
691}
de420c11 692
693/**
d185c3ee 694 * Get and update/create grade item for legacy modules.
de420c11 695 */
696function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
697
698 // does it already exist?
42ff9ce6 699 if ($grade_items = grade_item::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>0))) {
de420c11 700 if (count($grade_items) > 1) {
d185c3ee 701 debugging('Multiple legacy grade_items found.');
de420c11 702 return false;
703 }
704
705 $grade_item = reset($grade_items);
de420c11 706
d185c3ee 707 if (is_null($grademax) and is_null($scaleid)) {
708 $grade_item->gradetype = GRADE_TYPE_NONE;
de420c11 709
d185c3ee 710 } else if ($scaleid) {
711 $grade_item->gradetype = GRADE_TYPE_SCALE;
712 $grade_item->scaleid = $scaleid;
97d608ba 713 $grade_item->grademin = 1;
de420c11 714
d185c3ee 715 } else {
97d608ba 716 $grade_item->gradetype = GRADE_TYPE_VALUE;
717 $grade_item->grademax = $grademax;
718 $grade_item->grademin = 0;
de420c11 719 }
720
d185c3ee 721 $grade_item->itemname = $modinstance->name;
722 $grade_item->idnumber = $modinstance->cmidnumber;
de420c11 723
d185c3ee 724 $grade_item->update();
de420c11 725
726 return $grade_item;
727 }
612607bd 728
de420c11 729 // create new one
d185c3ee 730 $params = array('courseid' =>$modinstance->course,
de420c11 731 'itemtype' =>'mod',
732 'itemmodule' =>$modinstance->modname,
733 'iteminstance'=>$modinstance->id,
d185c3ee 734 'itemnumber' =>0,
de420c11 735 'itemname' =>$modinstance->name,
736 'idnumber' =>$modinstance->cmidnumber);
737
d185c3ee 738 if (is_null($grademax) and is_null($scaleid)) {
739 $params['gradetype'] = GRADE_TYPE_NONE;
740
741 } else if ($scaleid) {
612607bd 742 $params['gradetype'] = GRADE_TYPE_SCALE;
de420c11 743 $params['scaleid'] = $scaleid;
b3ac6c3e 744 $grade_item->grademin = 1;
de420c11 745 } else {
612607bd 746 $params['gradetype'] = GRADE_TYPE_VALUE;
de420c11 747 $params['grademax'] = $grademax;
748 $params['grademin'] = 0;
749 }
750
f70152b7 751 $grade_item = new grade_item($params);
752 $grade_item->insert();
de420c11 753
f70152b7 754 return $grade_item;
de420c11 755}
756
bfe7297e 757
32b97bb2 758/**
759 * Builds an array of percentages indexed by integers for the purpose of building a select drop-down element.
760 * @param int $steps The value between each level.
761 * @param string $order 'asc' for 0-100 and 'desc' for 100-0
762 * @param int $lowest The lowest value to include
763 * @param int $highest The highest value to include
764 */
765function build_percentages_array($steps=1, $order='desc', $lowest=0, $highest=100) {
766 // TODO reject or implement
767}
42ff9ce6 768
60cf7430 769?>