fixed wrong $options - this->keepdrop_options used instead
[moodle.git] / lib / gradelib.php
CommitLineData
5834dcdb 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
5834dcdb 4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Modular Object-Oriented Dynamic Learning Environment //
53461661 7// http://moodle.org //
5834dcdb 8// //
53461661 9// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
5834dcdb 10// //
11// This program is free software; you can redistribute it and/or modify //
12// it under the terms of the GNU General Public License as published by //
13// the Free Software Foundation; either version 2 of the License, or //
14// (at your option) any later version. //
15// //
16// This program is distributed in the hope that it will be useful, //
17// but WITHOUT ANY WARRANTY; without even the implied warranty of //
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19// GNU General Public License for more details: //
20// //
21// http://www.gnu.org/copyleft/gpl.html //
22// //
23///////////////////////////////////////////////////////////////////////////
24
25/**
42bbccd7 26 * Library of functions for gradebook
5834dcdb 27 *
28 * @author Moodle HQ developers
29 * @version $Id$
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 * @package moodlecore
32 */
33
53461661 34require_once($CFG->libdir . '/grade/constants.php');
eea6690a 35
3058964f 36require_once($CFG->libdir . '/grade/grade_category.php');
37require_once($CFG->libdir . '/grade/grade_item.php');
3ee5c201 38require_once($CFG->libdir . '/grade/grade_grade.php');
d5bdb228 39require_once($CFG->libdir . '/grade/grade_scale.php');
5501446d 40require_once($CFG->libdir . '/grade/grade_outcome.php');
3ee5c201 41require_once($CFG->libdir . '/grade/grade_grade_text.php');
60cf7430 42
612607bd 43/***** PUBLIC GRADE API *****/
44
c5b5f18d 45/**
46 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
ac9b0805 47 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
c5b5f18d 48 * or key means do not change existing.
4cf1b9be 49 *
c5b5f18d 50 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
e648f890 51 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
4cf1b9be 52 *
9c8d38fa 53 * @param string $source source of the grade such as 'mod/assignment'
c5b5f18d 54 * @param int $courseid id of course
3a5ae660 55 * @param string $itemtype type of grade item - mod, block
c5b5f18d 56 * @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
57 * @param int $iteminstance instance it of graded subject
58 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
b60b2ce6 59 * @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating grade_item definition only
c5b5f18d 60 * @param mixed $itemdetails object or array describing the grading item, NULL if no change
61 */
b67ec72f 62function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
aaff71da 63 global $USER;
612607bd 64
c5b5f18d 65 // only following grade_item properties can be changed in this function
e648f890 66 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
612607bd 67
c4e4068f 68 // grade item identification
69 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
70
612607bd 71 if (is_null($courseid) or is_null($itemtype)) {
72 debugging('Missing courseid or itemtype');
73 return GRADE_UPDATE_FAILED;
74 }
75
c4e4068f 76 if (!$grade_items = grade_item::fetch_all($params)) {
612607bd 77 // create a new one
78 $grade_item = false;
79
80 } else if (count($grade_items) == 1){
81 $grade_item = reset($grade_items);
82 unset($grade_items); //release memory
83
84 } else {
34e67f76 85 debugging('Found more than one grade item');
612607bd 86 return GRADE_UPDATE_MULTIPLE;
87 }
88
aaff71da 89 if (!empty($itemdetails['deleted'])) {
90 if ($grade_item) {
91 if ($grade_item->delete($source)) {
92 return GRADE_UPDATE_OK;
93 } else {
94 return GRADE_UPDATE_FAILED;
95 }
96 }
97 return GRADE_UPDATE_OK;
98 }
99
612607bd 100/// Create or update the grade_item if needed
b159da78 101
612607bd 102 if (!$grade_item) {
612607bd 103 if ($itemdetails) {
104 $itemdetails = (array)$itemdetails;
2e53372c 105
772ddfbf 106 // grademin and grademax ignored when scale specified
2e53372c 107 if (array_key_exists('scaleid', $itemdetails)) {
108 if ($itemdetails['scaleid']) {
109 unset($itemdetails['grademin']);
110 unset($itemdetails['grademax']);
111 }
112 }
113
612607bd 114 foreach ($itemdetails as $k=>$v) {
115 if (!in_array($k, $allowed)) {
116 // ignore it
117 continue;
118 }
119 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
120 // no grade item needed!
121 return GRADE_UPDATE_OK;
122 }
123 $params[$k] = $v;
124 }
125 }
f70152b7 126 $grade_item = new grade_item($params);
127 $grade_item->insert();
612607bd 128
129 } else {
2cc4b0f9 130 if ($grade_item->is_locked()) {
b159da78 131 $confirm_regrade = optional_param('confirm_regrade', 0, PARAM_INT);
132 if (!$confirm_regrade) {
133 $message = get_string('gradeitemislocked', 'grades', $grade_item->itemname);
134 $back_link = '';
135 $regrade_link = qualified_me() . '&amp;confirm_regrade=1';
136 notice_yesno($message, $regrade_link, $back_link);
137 return GRADE_UPDATE_ITEM_LOCKED;
138 }
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
612607bd 171/// Finally start processing of grades
b67ec72f 172 if (is_object($grades)) {
173 $grades = array($grades);
612607bd 174 } else {
b67ec72f 175 if (array_key_exists('userid', $grades)) {
176 $grades = array($grades);
612607bd 177 }
178 }
179
4cf1b9be 180 $failed = false;
612607bd 181 foreach ($grades as $grade) {
182 $grade = (array)$grade;
183 if (empty($grade['userid'])) {
4cf1b9be 184 $failed = true;
185 debugging('Invalid userid in grade submitted');
186 continue;
ac9b0805 187 } else {
188 $userid = $grade['userid'];
612607bd 189 }
190
2cc4b0f9 191 $rawgrade = false;
ac9b0805 192 $feedback = false;
193 $feedbackformat = FORMAT_MOODLE;
772ddfbf 194
ac9b0805 195 if (array_key_exists('rawgrade', $grade)) {
196 $rawgrade = $grade['rawgrade'];
197 }
612607bd 198
4cf1b9be 199 if (array_key_exists('feedback', $grade)) {
ac9b0805 200 $feedback = $grade['feedback'];
612607bd 201 }
202
ac9b0805 203 if (array_key_exists('feedbackformat', $grade)) {
204 $feedbackformat = $grade['feedbackformat'];
612607bd 205 }
206
aaff71da 207 if (array_key_exists('usermodified', $grade)) {
208 $usermodified = $grade['usermodified'];
209 } else {
210 $usermodified = $USER->id;
211 }
212
ac9b0805 213 // update or insert the grade
aaff71da 214 if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat, $usermodified)) {
4cf1b9be 215 $failed = true;
4cf1b9be 216 }
612607bd 217 }
218
4cf1b9be 219 if (!$failed) {
220 return GRADE_UPDATE_OK;
221 } else {
222 return GRADE_UPDATE_FAILED;
223 }
612607bd 224}
225
b67ec72f 226
227/**
3a5ae660 228 * Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
bc430af2 229 * If it's locked to the current user then the module can print a nice message or prevent editing in the module.
3a5ae660 230 * If no $userid is given, the method will always return the grade_item's locked state.
231 * If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
232 * the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
233 * return the locked state of the specific grade.
234 *
235 * @param int $courseid id of course
236 * @param string $itemtype 'mod', 'block'
237 * @param string $itemmodule 'forum, 'quiz', etc.
238 * @param int $iteminstance id of the item module
239 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
240 * @param int $userid ID of the graded user
241 * @return boolean Whether the grade is locked or not
242 */
34e67f76 243function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
b67ec72f 244
f92dcad8 245 if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) {
34e67f76 246 return false;
247
248 } else if (count($grade_items) == 1){
249 $grade_item = reset($grade_items);
250 return $grade_item->is_locked($userid);
251
252 } else {
253 debugging('Found more than one grade item');
254 foreach ($grade_items as $grade_item) {
255 if ($grade_item->is_locked($userid)) {
256 return true;
257 }
258 }
259 return false;
260 }
261}
b67ec72f 262
3a5ae660 263/**
264 * Returns list of outcomes used in course together with current outcomes for this user
265 * @param int $courseid id of course
266 * @param string $itemtype 'mod', 'block'
267 * @param string $itemmodule 'forum, 'quiz', etc.
268 * @param int $iteminstance id of the item module
269 * @param int $userid ID of the graded user
270 * @return array of outcome information objects (scaleid, name, grade and locked status) indexed with itemnumbers
271 */
e46f4d11 272function grade_get_outcomes($courseid, $itemtype, $itemmodule, $iteminstance, $userid=0) {
3a5ae660 273 $result = array();
274 if ($items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
275 foreach ($items as $item) {
276 if (empty($item->outcomeid)) {
277 continue;
278 }
279 if (!$outcome = grade_outcome::fetch(array('id'=>$item->outcomeid))) {
280 debugging('Incorect outcomeid found');
281 continue;
282 }
283 // prepare outcome info with user grade
284 $o = new object();
285 $o->scaleid = $outcome->scaleid;
286 $o->name = $outcome->fullname;
287
e46f4d11 288 if (empty($userid)) {
289 //no user info
290 } if ($grade = $item->get_grade($userid,false)) {
3a5ae660 291 $o->grade = $grade->finalgrade;
292 $o->locked = $grade->is_locked();
293 } else {
294 $o->grade = null;
295 $o->locked = $item->is_locked();
296 }
297 $o->grade = intval($o->grade); // 0 means no grade, int for scales
11a14999 298
3a5ae660 299 $result[$item->itemnumber] = $o;
300 }
301 }
302
303 return $result;
304}
305
306/**
307 * Updates outcomes of user
308 * @param int $courseid id of course
309 * @param string $itemtype 'mod', 'block'
310 * @param string $itemmodule 'forum, 'quiz', etc.
311 * @param int $iteminstance id of the item module
312 * @param int $userid ID of the graded user
313 */
314function grade_update_outcomes($source, $courseid, $itemtype, $itemmodule, $iteminstance, $userid, $data) {
315 if ($items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
316 foreach ($items as $item) {
317 if (!array_key_exists($item->itemnumber, $data)) {
318 continue;
319 }
320 $grade = $data[$item->itemnumber] < 1 ? null : $data[$item->itemnumber];
321 $item->update_final_grade($userid, $grade, $source);
11a14999 322 }
3a5ae660 323 }
324}
325
612607bd 326/***** END OF PUBLIC API *****/
327
60243313 328
329/**
2c5e52e2 330 * Verify new value of idnumber - checks for uniqueness of new idnumbers, old are kept intact
60243313 331 * @param string idnumber string (with magic quotes)
332 * @param object $cm used for course module idnumbers and items attached to modules
333 * @param object $gradeitem is item idnumber
334 * @return boolean true means idnumber ok
335 */
336function grade_verify_idnumber($idnumber, $grade_item=null, $cm=null) {
337 if ($idnumber == '') {
338 //we allow empty idnumbers
339 return true;
340 }
341
342 // keep existing even when not unique
343 if ($cm and $cm->idnumber == $idnumber) {
344 return true;
345 } else if ($grade_item and $grade_item->idnumber == $idnumber) {
346 return true;
347 }
348
349 if (get_records('course_modules', 'idnumber', $idnumber)) {
350 return false;
351 }
352
353 if (get_records('grade_items', 'idnumber', $idnumber)) {
354 return false;
355 }
356
357 return true;
358}
359
360/**
361 * Force final grade recalculation in all course items
362 * @param int $courseid
363 */
f8e6e4db 364function grade_force_full_regrading($courseid) {
365 set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
366}
34e67f76 367
5834dcdb 368/**
ac9b0805 369 * Updates all final grades in course.
a8995b34 370 *
371 * @param int $courseid
f8e6e4db 372 * @param int $userid if specified, try to do a quick regrading of grades of this user only
373 * @param object $updated_item the item in which
374 * @return boolean true if ok, array of errors if problems found (item id is used as key)
a8995b34 375 */
c86caae7 376function grade_regrade_final_grades($courseid, $userid=null, $updated_item=null) {
b8ff92b6 377
514a3467 378 $course_item = grade_item::fetch_course_item($courseid);
f04873a9 379
f8e6e4db 380 if ($userid) {
381 // one raw grade updated for one user
382 if (empty($updated_item)) {
383 error("updated_item_id can not be null!");
384 }
385 if ($course_item->needsupdate) {
386 $updated_item->force_regrading();
387 return 'Can not do fast regrading after updating of raw grades';
a8995b34 388 }
772ddfbf 389
f8e6e4db 390 } else {
391 if (!$course_item->needsupdate) {
392 // nothing to do :-)
b8ff92b6 393 return true;
b8ff92b6 394 }
a8995b34 395 }
396
f8e6e4db 397 $grade_items = grade_item::fetch_all(array('courseid'=>$courseid));
398 $depends_on = array();
399
400 // first mark all category and calculated items as needing regrading
fb0e3570 401 // this is slower, but 100% accurate
f8e6e4db 402 foreach ($grade_items as $gid=>$gitem) {
fb46b5b6 403 if (!empty($updated_item) and $updated_item->id == $gid) {
f8e6e4db 404 $grade_items[$gid]->needsupdate = 1;
405
eacd3700 406 } else if ($gitem->is_course_item() or $gitem->is_category_item() or $gitem->is_calculated()) {
f8e6e4db 407 $grade_items[$gid]->needsupdate = 1;
408 }
2e53372c 409
f8e6e4db 410 // construct depends_on lookup array
411 $depends_on[$gid] = $grade_items[$gid]->depends_on();
412 }
2e53372c 413
d14ae855 414 $errors = array();
b8ff92b6 415 $finalids = array();
d14ae855 416 $gids = array_keys($grade_items);
eacd3700 417 $failed = 0;
d14ae855 418
419 while (count($finalids) < count($gids)) { // work until all grades are final or error found
420 $count = 0;
421 foreach ($gids as $gid) {
422 if (in_array($gid, $finalids)) {
423 continue; // already final
424 }
425
426 if (!$grade_items[$gid]->needsupdate) {
427 $finalids[] = $gid; // we can make it final - does not need update
b8ff92b6 428 continue;
429 }
430
b8ff92b6 431 $doupdate = true;
f8e6e4db 432 foreach ($depends_on[$gid] as $did) {
b8ff92b6 433 if (!in_array($did, $finalids)) {
434 $doupdate = false;
d14ae855 435 continue; // this item depends on something that is not yet in finals array
b8ff92b6 436 }
437 }
438
439 //oki - let's update, calculate or aggregate :-)
440 if ($doupdate) {
d14ae855 441 $result = $grade_items[$gid]->regrade_final_grades($userid);
f8e6e4db 442
443 if ($result === true) {
d14ae855 444 $grade_items[$gid]->regrading_finished();
fb0e3570 445 $grade_items[$gid]->check_locktime(); // do the locktime item locking
f8e6e4db 446 $count++;
b8ff92b6 447 $finalids[] = $gid;
fb0e3570 448
f8e6e4db 449 } else {
d14ae855 450 $grade_items[$gid]->force_regrading();
f8e6e4db 451 $errors[$gid] = $result;
b8ff92b6 452 }
453 }
454 }
455
456 if ($count == 0) {
eacd3700 457 $failed++;
458 } else {
459 $failed = 0;
460 }
461
462 if ($failed > 1) {
d14ae855 463 foreach($gids as $gid) {
464 if (in_array($gid, $finalids)) {
465 continue; // this one is ok
466 }
467 $grade_items[$gid]->force_regrading();
468 $errors[$grade_items[$gid]->id] = 'Probably circular reference or broken calculation formula'; // TODO: localize
b8ff92b6 469 }
d14ae855 470 break; // oki, found error
b8ff92b6 471 }
472 }
473
474 if (count($errors) == 0) {
fb0e3570 475 if (empty($userid)) {
476 // do the locktime locking of grades, but only when doing full regrading
fed7cdc9 477 grade_grade::check_locktime_all($gids);
fb0e3570 478 }
b8ff92b6 479 return true;
480 } else {
481 return $errors;
482 }
a8995b34 483}
967f222f 484
de420c11 485/**
d185c3ee 486 * For backwards compatibility with old third-party modules, this function can
487 * be used to import all grades from activities with legacy grading.
739196ba 488 * @param int $courseid or null if all courses
967f222f 489 */
739196ba 490function grade_grab_legacy_grades($courseid=null) {
612607bd 491
ac9b0805 492 global $CFG;
967f222f 493
494 if (!$mods = get_list_of_plugins('mod') ) {
495 error('No modules installed!');
496 }
497
739196ba 498 if ($courseid) {
499 $course_sql = " AND cm.course=$courseid";
500 } else {
501 $course_sql = "";
502 }
503
967f222f 504 foreach ($mods as $mod) {
505
506 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
507 continue;
508 }
509
d185c3ee 510 if (!$module = get_record('modules', 'name', $mod)) {
511 //not installed
512 continue;
513 }
514
515 if (!$module->visible) {
516 //disabled module
517 continue;
518 }
519
520 $fullmod = $CFG->dirroot.'/mod/'.$mod;
967f222f 521
522 // include the module lib once
523 if (file_exists($fullmod.'/lib.php')) {
524 include_once($fullmod.'/lib.php');
de420c11 525 // look for modname_grades() function - old gradebook pulling function
526 // if present sync the grades with new grading system
967f222f 527 $gradefunc = $mod.'_grades';
de420c11 528 if (function_exists($gradefunc)) {
529
530 // get all instance of the activity
d185c3ee 531 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
532 FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
739196ba 533 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id $course_sql";
de420c11 534
535 if ($modinstances = get_records_sql($sql)) {
967f222f 536 foreach ($modinstances as $modinstance) {
d185c3ee 537 grade_update_mod_grades($modinstance);
967f222f 538 }
539 }
540 }
541 }
542 }
543}
544
ac9b0805 545/**
546 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
547 */
548function grade_grab_grades() {
549
550 global $CFG;
551
552 if (!$mods = get_list_of_plugins('mod') ) {
553 error('No modules installed!');
554 }
555
556 foreach ($mods as $mod) {
557
558 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
559 continue;
560 }
561
562 if (!$module = get_record('modules', 'name', $mod)) {
563 //not installed
564 continue;
565 }
566
567 if (!$module->visible) {
568 //disabled module
569 continue;
570 }
571
572 $fullmod = $CFG->dirroot.'/mod/'.$mod;
573
574 // include the module lib once
575 if (file_exists($fullmod.'/lib.php')) {
576 include_once($fullmod.'/lib.php');
577 // look for modname_grades() function - old gradebook pulling function
578 // if present sync the grades with new grading system
579 $gradefunc = $mod.'_update_grades';
580 if (function_exists($gradefunc)) {
581 $gradefunc();
582 }
583 }
584 }
585}
586
d185c3ee 587/**
588 * Force full update of module grades in central gradebook - works for both legacy and converted activities.
589 * @param object $modinstance object with extra cmidnumber and modname property
590 * @return boolean success
591 */
2b0f65e2 592function grade_update_mod_grades($modinstance, $userid=0) {
d185c3ee 593 global $CFG;
594
595 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
596 if (!file_exists($fullmod.'/lib.php')) {
597 debugging('missing lib.php file in module');
598 return false;
599 }
600 include_once($fullmod.'/lib.php');
601
602 // does it use legacy grading?
603 $gradefunc = $modinstance->modname.'_grades';
604 $updategradesfunc = $modinstance->modname.'_update_grades';
605 $updateitemfunc = $modinstance->modname.'_grade_item_update';
606
607 if (function_exists($gradefunc)) {
2b0f65e2 608
609 // legacy module - not yet converted
d185c3ee 610 if ($oldgrades = $gradefunc($modinstance->id)) {
611
612 $grademax = $oldgrades->maxgrade;
613 $scaleid = NULL;
614 if (!is_numeric($grademax)) {
615 // scale name is provided as a string, try to find it
616 if (!$scale = get_record('scale', 'name', $grademax)) {
617 debugging('Incorrect scale name! name:'.$grademax);
618 return false;
619 }
620 $scaleid = $scale->id;
621 }
622
623 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
624 debugging('Can not get/create legacy grade item!');
625 return false;
626 }
627
628 $grades = array();
2b0f65e2 629 foreach ($oldgrades->grades as $uid=>$usergrade) {
630 if ($userid and $uid != $userid) {
631 continue;
632 }
d185c3ee 633 $grade = new object();
2b0f65e2 634 $grade->userid = $uid;
d185c3ee 635
636 if ($usergrade == '-') {
637 // no grade
ac9b0805 638 $grade->rawgrade = null;
d185c3ee 639
640 } else if ($scaleid) {
641 // scale in use, words used
642 $gradescale = explode(",", $scale->scale);
ac9b0805 643 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
d185c3ee 644
645 } else {
646 // good old numeric value
ac9b0805 647 $grade->rawgrade = $usergrade;
d185c3ee 648 }
649 $grades[] = $grade;
650 }
651
652 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
653 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
654 }
655
656 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
657 //new grading supported, force updating of grades
658 $updateitemfunc($modinstance);
2b0f65e2 659 $updategradesfunc($modinstance, $userid);
d185c3ee 660
661 } else {
2b0f65e2 662 // mudule does not support grading??
d185c3ee 663 }
664
665 return true;
666}
de420c11 667
668/**
d185c3ee 669 * Get and update/create grade item for legacy modules.
de420c11 670 */
671function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
672
673 // does it already exist?
42ff9ce6 674 if ($grade_items = grade_item::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>0))) {
de420c11 675 if (count($grade_items) > 1) {
d185c3ee 676 debugging('Multiple legacy grade_items found.');
de420c11 677 return false;
678 }
679
680 $grade_item = reset($grade_items);
de420c11 681
d185c3ee 682 if (is_null($grademax) and is_null($scaleid)) {
683 $grade_item->gradetype = GRADE_TYPE_NONE;
de420c11 684
d185c3ee 685 } else if ($scaleid) {
686 $grade_item->gradetype = GRADE_TYPE_SCALE;
687 $grade_item->scaleid = $scaleid;
97d608ba 688 $grade_item->grademin = 1;
de420c11 689
d185c3ee 690 } else {
97d608ba 691 $grade_item->gradetype = GRADE_TYPE_VALUE;
692 $grade_item->grademax = $grademax;
693 $grade_item->grademin = 0;
de420c11 694 }
695
d185c3ee 696 $grade_item->itemname = $modinstance->name;
697 $grade_item->idnumber = $modinstance->cmidnumber;
de420c11 698
d185c3ee 699 $grade_item->update();
de420c11 700
701 return $grade_item;
702 }
612607bd 703
de420c11 704 // create new one
d185c3ee 705 $params = array('courseid' =>$modinstance->course,
de420c11 706 'itemtype' =>'mod',
707 'itemmodule' =>$modinstance->modname,
708 'iteminstance'=>$modinstance->id,
d185c3ee 709 'itemnumber' =>0,
de420c11 710 'itemname' =>$modinstance->name,
711 'idnumber' =>$modinstance->cmidnumber);
712
d185c3ee 713 if (is_null($grademax) and is_null($scaleid)) {
714 $params['gradetype'] = GRADE_TYPE_NONE;
715
716 } else if ($scaleid) {
612607bd 717 $params['gradetype'] = GRADE_TYPE_SCALE;
de420c11 718 $params['scaleid'] = $scaleid;
b3ac6c3e 719 $grade_item->grademin = 1;
de420c11 720 } else {
612607bd 721 $params['gradetype'] = GRADE_TYPE_VALUE;
de420c11 722 $params['grademax'] = $grademax;
723 $params['grademin'] = 0;
724 }
725
f70152b7 726 $grade_item = new grade_item($params);
727 $grade_item->insert();
de420c11 728
f70152b7 729 return $grade_item;
de420c11 730}
731
f615fbab 732/**
733 * Remove all grade related course data - history is kept
734 * @param int $courseid
735 * @showfeedback boolean print feedback
736 */
737function remove_course_grades($courseid, $showfeedback) {
738 $strdeleted = get_string('deleted');
739
740 $course_category = grade_category::fetch_course_category($courseid);
741 $course_category->delete('coursedelete');
742 if ($showfeedback) {
743 notify($strdeleted.' - '.get_string('grades', 'grades').', '.get_string('items', 'grades').', '.get_string('categories', 'grades'));
744 }
745
746 if ($outcomes = grade_outcome::fetch_all(array('courseid'=>$courseid))) {
747 foreach ($outcomes as $outcome) {
748 $outcome->delete('coursedelete');
749 }
750 }
751 delete_records('grade_outcomes_courses', 'courseid', $courseid);
752 if ($showfeedback) {
753 notify($strdeleted.' - '.get_string('outcomes', 'grades'));
754 }
755
756 if ($scales = grade_scale::fetch_all(array('courseid'=>$courseid))) {
757 foreach ($scales as $scale) {
758 $scale->delete('coursedelete');
759 }
760 }
761 if ($showfeedback) {
762 notify($strdeleted.' - '.get_string('scales'));
763 }
f615fbab 764}
bfe7297e 765
32b97bb2 766/**
767 * Builds an array of percentages indexed by integers for the purpose of building a select drop-down element.
768 * @param int $steps The value between each level.
769 * @param string $order 'asc' for 0-100 and 'desc' for 100-0
770 * @param int $lowest The lowest value to include
771 * @param int $highest The highest value to include
772 */
773function build_percentages_array($steps=1, $order='desc', $lowest=0, $highest=100) {
774 // TODO reject or implement
775}
42ff9ce6 776
2650c51e 777/**
778 * Grading cron job
779 */
780function grade_cron() {
26101be8 781 global $CFG;
782
783 $now = time();
784
785 $sql = "SELECT i.*
786 FROM {$CFG->prefix}grade_items i
787 WHERE i.locked = 0 AND i.locktime > 0 AND i.locktime < $now AND EXISTS (
1f4a0320 788 SELECT 'x' FROM {$CFG->prefix}grade_items c WHERE c.itemtype='course' AND c.needsupdate=0 AND c.courseid=i.courseid)";
26101be8 789
2650c51e 790 // go through all courses that have proper final grades and lock them if needed
26101be8 791 if ($rs = get_recordset_sql($sql)) {
2650c51e 792 if ($rs->RecordCount() > 0) {
26101be8 793 while ($item = rs_fetch_next_record($rs)) {
794 $grade_item = new grade_item($item, false);
795 $grade_item->locked = $now;
796 $grade_item->update('locktime');
2650c51e 797 }
798 }
799 rs_close($rs);
800 }
26101be8 801
802 $sql = "SELECT g.*
803 FROM {$CFG->prefix}grade_grades g, {$CFG->prefix}grade_items i
804 WHERE g.locked = 0 AND g.locktime > 0 AND g.locktime < $now AND g.itemid=i.id AND EXISTS (
1f4a0320 805 SELECT 'x' FROM {$CFG->prefix}grade_items c WHERE c.itemtype='course' AND c.needsupdate=0 AND c.courseid=i.courseid)";
26101be8 806
807 // go through all courses that have proper final grades and lock them if needed
808 if ($rs = get_recordset_sql($sql)) {
809 if ($rs->RecordCount() > 0) {
810 while ($grade = rs_fetch_next_record($rs)) {
1f4a0320 811 $grade_grade = new grade_grade($grade, false);
26101be8 812 $grade_grade->locked = $now;
813 $grade_grade->update('locktime');
814 }
815 }
816 rs_close($rs);
817 }
818
2650c51e 819}
820
60cf7430 821?>