Updated the HEAD build version to 20090525
[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/**
b9f49659 26 * Library of functions for gradebook - both public and internal
5834dcdb 27 *
28 * @author Moodle HQ developers
d886a7ea 29 * @version $Id$
5834dcdb 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');
60cf7430 41
b9f49659 42/////////////////////////////////////////////////////////////////////
43///// Start of public API for communication with modules/blocks /////
44/////////////////////////////////////////////////////////////////////
612607bd 45
c5b5f18d 46/**
47 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
ac9b0805 48 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
c5b5f18d 49 * or key means do not change existing.
4cf1b9be 50 *
c5b5f18d 51 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
f0362b5d 52 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted' and 'hidden'. 'reset' means delete all current grades including locked ones.
4cf1b9be 53 *
fcac8e51 54 * Manual, course or category items can not be updated by this function.
b9f49659 55 * @public
9c8d38fa 56 * @param string $source source of the grade such as 'mod/assignment'
c5b5f18d 57 * @param int $courseid id of course
3a5ae660 58 * @param string $itemtype type of grade item - mod, block
c5b5f18d 59 * @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
60 * @param int $iteminstance instance it of graded subject
61 * @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
b60b2ce6 62 * @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating grade_item definition only
c5b5f18d 63 * @param mixed $itemdetails object or array describing the grading item, NULL if no change
64 */
b67ec72f 65function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
9718765e 66 global $USER, $CFG, $DB;
612607bd 67
c5b5f18d 68 // only following grade_item properties can be changed in this function
1223d24a 69 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted', 'hidden');
25bcd908 70 // list of 10,5 numeric fields
71 $floats = array('grademin', 'grademax', 'multfactor', 'plusfactor');
612607bd 72
c4e4068f 73 // grade item identification
74 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
75
612607bd 76 if (is_null($courseid) or is_null($itemtype)) {
77 debugging('Missing courseid or itemtype');
78 return GRADE_UPDATE_FAILED;
79 }
80
c4e4068f 81 if (!$grade_items = grade_item::fetch_all($params)) {
612607bd 82 // create a new one
83 $grade_item = false;
84
85 } else if (count($grade_items) == 1){
86 $grade_item = reset($grade_items);
87 unset($grade_items); //release memory
88
89 } else {
34e67f76 90 debugging('Found more than one grade item');
612607bd 91 return GRADE_UPDATE_MULTIPLE;
92 }
93
aaff71da 94 if (!empty($itemdetails['deleted'])) {
95 if ($grade_item) {
96 if ($grade_item->delete($source)) {
97 return GRADE_UPDATE_OK;
98 } else {
99 return GRADE_UPDATE_FAILED;
100 }
101 }
102 return GRADE_UPDATE_OK;
103 }
104
612607bd 105/// Create or update the grade_item if needed
b159da78 106
612607bd 107 if (!$grade_item) {
612607bd 108 if ($itemdetails) {
109 $itemdetails = (array)$itemdetails;
2e53372c 110
772ddfbf 111 // grademin and grademax ignored when scale specified
2e53372c 112 if (array_key_exists('scaleid', $itemdetails)) {
113 if ($itemdetails['scaleid']) {
114 unset($itemdetails['grademin']);
115 unset($itemdetails['grademax']);
116 }
117 }
118
612607bd 119 foreach ($itemdetails as $k=>$v) {
120 if (!in_array($k, $allowed)) {
121 // ignore it
122 continue;
123 }
124 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
125 // no grade item needed!
126 return GRADE_UPDATE_OK;
127 }
128 $params[$k] = $v;
129 }
130 }
f70152b7 131 $grade_item = new grade_item($params);
132 $grade_item->insert();
612607bd 133
134 } else {
2cc4b0f9 135 if ($grade_item->is_locked()) {
d6bc2a81 136 // no notice() here, test returned value instead!
678e8898 137 return GRADE_UPDATE_ITEM_LOCKED;
612607bd 138 }
139
140 if ($itemdetails) {
141 $itemdetails = (array)$itemdetails;
142 $update = false;
143 foreach ($itemdetails as $k=>$v) {
144 if (!in_array($k, $allowed)) {
145 // ignore it
146 continue;
147 }
25bcd908 148 if (in_array($k, $floats)) {
149 if (grade_floats_different($grade_item->{$k}, $v)) {
150 $grade_item->{$k} = $v;
151 $update = true;
152 }
55231be0 153
25bcd908 154 } else {
155 if ($grade_item->{$k} != $v) {
156 $grade_item->{$k} = $v;
157 $update = true;
158 }
612607bd 159 }
160 }
161 if ($update) {
162 $grade_item->update();
163 }
164 }
165 }
166
f0362b5d 167/// reset grades if requested
168 if (!empty($itemdetails['reset'])) {
169 $grade_item->delete_all_grades('reset');
170 return GRADE_UPDATE_OK;
171 }
172
612607bd 173/// Some extra checks
174 // do we use grading?
175 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
176 return GRADE_UPDATE_OK;
177 }
178
179 // no grade submitted
b67ec72f 180 if (empty($grades)) {
612607bd 181 return GRADE_UPDATE_OK;
182 }
183
612607bd 184/// Finally start processing of grades
b67ec72f 185 if (is_object($grades)) {
55231be0 186 $grades = array($grades->userid=>$grades);
612607bd 187 } else {
b67ec72f 188 if (array_key_exists('userid', $grades)) {
55231be0 189 $grades = array($grades['userid']=>$grades);
190 }
191 }
192
193/// normalize and verify grade array
194 foreach($grades as $k=>$g) {
195 if (!is_array($g)) {
196 $g = (array)$g;
197 $grades[$k] = $g;
612607bd 198 }
55231be0 199
200 if (empty($g['userid']) or $k != $g['userid']) {
201 debugging('Incorrect grade array index, must be user id! Grade ignored.');
202 unset($grades[$k]);
203 }
204 }
205
206 if (empty($grades)) {
207 return GRADE_UPDATE_FAILED;
208 }
209
210 $count = count($grades);
9718765e 211 if ($count > 0 and $count < 200) {
9cf4a18b 212 list($uids, $params) = $DB->get_in_or_equal(array_keys($grades), SQL_PARAMS_NAMED, $start='uid0');
9718765e 213 $params['gid'] = $grade_item->id;
214 $sql = "SELECT * FROM {grade_grades} WHERE itemid = :gid AND userid $uids";
55231be0 215
216 } else {
9718765e 217 $sql = "SELECT * FROM {grade_grades} WHERE itemid = :gid";
218 $params = array('gid'=>$grade_item->id);
612607bd 219 }
220
9718765e 221 $rs = $DB->get_recordset_sql($sql, $params);
55231be0 222
4cf1b9be 223 $failed = false;
55231be0 224
225 while (count($grades) > 0) {
226 $grade_grade = null;
227 $grade = null;
228
9cf4a18b 229 foreach ($rs as $gd) {
230
55231be0 231 $userid = $gd->userid;
232 if (!isset($grades[$userid])) {
233 // this grade not requested, continue
234 continue;
235 }
236 // existing grade requested
237 $grade = $grades[$userid];
238 $grade_grade = new grade_grade($gd, false);
239 unset($grades[$userid]);
240 break;
241 }
242
243 if (is_null($grade_grade)) {
244 if (count($grades) == 0) {
245 // no more grades to process
246 break;
247 }
248
249 $grade = reset($grades);
250 $userid = $grade['userid'];
251 $grade_grade = new grade_grade(array('itemid'=>$grade_item->id, 'userid'=>$userid), false);
252 $grade_grade->load_optional_fields(); // add feedback and info too
253 unset($grades[$userid]);
612607bd 254 }
255
2cc4b0f9 256 $rawgrade = false;
ac9b0805 257 $feedback = false;
258 $feedbackformat = FORMAT_MOODLE;
ced5ee59 259 $usermodified = $USER->id;
260 $datesubmitted = null;
261 $dategraded = null;
772ddfbf 262
ac9b0805 263 if (array_key_exists('rawgrade', $grade)) {
264 $rawgrade = $grade['rawgrade'];
265 }
612607bd 266
4cf1b9be 267 if (array_key_exists('feedback', $grade)) {
ac9b0805 268 $feedback = $grade['feedback'];
612607bd 269 }
270
ac9b0805 271 if (array_key_exists('feedbackformat', $grade)) {
272 $feedbackformat = $grade['feedbackformat'];
612607bd 273 }
274
aaff71da 275 if (array_key_exists('usermodified', $grade)) {
276 $usermodified = $grade['usermodified'];
ced5ee59 277 }
278
279 if (array_key_exists('datesubmitted', $grade)) {
280 $datesubmitted = $grade['datesubmitted'];
281 }
282
283 if (array_key_exists('dategraded', $grade)) {
284 $dategraded = $grade['dategraded'];
aaff71da 285 }
286
ac9b0805 287 // update or insert the grade
55231be0 288 if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, $feedback, $feedbackformat, $usermodified, $dategraded, $datesubmitted, $grade_grade)) {
4cf1b9be 289 $failed = true;
4cf1b9be 290 }
612607bd 291 }
292
55231be0 293 if ($rs) {
9718765e 294 $rs->close();
55231be0 295 }
296
4cf1b9be 297 if (!$failed) {
298 return GRADE_UPDATE_OK;
299 } else {
300 return GRADE_UPDATE_FAILED;
301 }
612607bd 302}
303
3a5ae660 304/**
305 * Updates outcomes of user
fcac8e51 306 * Manual outcomes can not be updated.
b9f49659 307 * @public
fcac8e51 308 * @param string $source source of the grade such as 'mod/assignment'
3a5ae660 309 * @param int $courseid id of course
310 * @param string $itemtype 'mod', 'block'
311 * @param string $itemmodule 'forum, 'quiz', etc.
312 * @param int $iteminstance id of the item module
313 * @param int $userid ID of the graded user
d886a7ea 314 * @param array $data array itemnumber=>outcomegrade
3a5ae660 315 */
316function grade_update_outcomes($source, $courseid, $itemtype, $itemmodule, $iteminstance, $userid, $data) {
317 if ($items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
318 foreach ($items as $item) {
d886a7ea 319 if (!array_key_exists($item->itemnumber, $data)) {
3a5ae660 320 continue;
321 }
d886a7ea 322 $grade = $data[$item->itemnumber] < 1 ? null : $data[$item->itemnumber];
3a5ae660 323 $item->update_final_grade($userid, $grade, $source);
11a14999 324 }
3a5ae660 325 }
326}
327
6b5c722d 328/**
fcac8e51 329 * Returns grading information for given activity - optionally with users grades
330 * Manual, course or category items can not be queried.
b9f49659 331 * @public
6b5c722d 332 * @param int $courseid id of course
333 * @param string $itemtype 'mod', 'block'
334 * @param string $itemmodule 'forum, 'quiz', etc.
335 * @param int $iteminstance id of the item module
b9f49659 336 * @param int $userid_or_ids optional id of the graded user or array of ids; if userid not used, returns only information about grade_item
6b5c722d 337 * @return array of grade information objects (scaleid, name, grade and locked status, etc.) indexed with itemnumbers
338 */
b9f49659 339function grade_get_grades($courseid, $itemtype, $itemmodule, $iteminstance, $userid_or_ids=null) {
a3fbd494 340 global $CFG;
341
fcac8e51 342 $return = new object();
343 $return->items = array();
344 $return->outcomes = array();
6b5c722d 345
fcac8e51 346 $course_item = grade_item::fetch_course_item($courseid);
347 $needsupdate = array();
348 if ($course_item->needsupdate) {
349 $result = grade_regrade_final_grades($courseid);
350 if ($result !== true) {
351 $needsupdate = array_keys($result);
352 }
353 }
6b5c722d 354
fcac8e51 355 if ($grade_items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
356 foreach ($grade_items as $grade_item) {
a3fbd494 357 $decimalpoints = null;
358
fcac8e51 359 if (empty($grade_item->outcomeid)) {
360 // prepare information about grade item
361 $item = new object();
362 $item->itemnumber = $grade_item->itemnumber;
363 $item->scaleid = $grade_item->scaleid;
364 $item->name = $grade_item->get_name();
365 $item->grademin = $grade_item->grademin;
366 $item->grademax = $grade_item->grademax;
367 $item->gradepass = $grade_item->gradepass;
368 $item->locked = $grade_item->is_locked();
369 $item->hidden = $grade_item->is_hidden();
370 $item->grades = array();
371
372 switch ($grade_item->gradetype) {
373 case GRADE_TYPE_NONE:
374 continue;
6b5c722d 375
6b5c722d 376 case GRADE_TYPE_VALUE:
fcac8e51 377 $item->scaleid = 0;
6b5c722d 378 break;
379
fcac8e51 380 case GRADE_TYPE_TEXT:
381 $item->scaleid = 0;
382 $item->grademin = 0;
383 $item->grademax = 0;
384 $item->gradepass = 0;
6b5c722d 385 break;
fcac8e51 386 }
6b5c722d 387
fcac8e51 388 if (empty($userid_or_ids)) {
389 $userids = array();
390
391 } else if (is_array($userid_or_ids)) {
392 $userids = $userid_or_ids;
393
394 } else {
395 $userids = array($userid_or_ids);
6b5c722d 396 }
6b5c722d 397
fcac8e51 398 if ($userids) {
399 $grade_grades = grade_grade::fetch_users_grades($grade_item, $userids, true);
400 foreach ($userids as $userid) {
401 $grade_grades[$userid]->grade_item =& $grade_item;
402
403 $grade = new object();
404 $grade->grade = $grade_grades[$userid]->finalgrade;
405 $grade->locked = $grade_grades[$userid]->is_locked();
406 $grade->hidden = $grade_grades[$userid]->is_hidden();
407 $grade->overridden = $grade_grades[$userid]->overridden;
408 $grade->feedback = $grade_grades[$userid]->feedback;
409 $grade->feedbackformat = $grade_grades[$userid]->feedbackformat;
a3fbd494 410 $grade->usermodified = $grade_grades[$userid]->usermodified;
ced5ee59 411 $grade->datesubmitted = $grade_grades[$userid]->get_datesubmitted();
412 $grade->dategraded = $grade_grades[$userid]->get_dategraded();
fcac8e51 413
414 // create text representation of grade
5048575d 415 if ($grade_item->gradetype == GRADE_TYPE_TEXT or $grade_item->gradetype == GRADE_TYPE_NONE) {
416 $grade->grade = null;
417 $grade->str_grade = '-';
418 $grade->str_long_grade = $grade->str_grade;
55231be0 419
5048575d 420 } else if (in_array($grade_item->id, $needsupdate)) {
85a0a69f 421 $grade->grade = false;
422 $grade->str_grade = get_string('error');
423 $grade->str_long_grade = $grade->str_grade;
fcac8e51 424
425 } else if (is_null($grade->grade)) {
85a0a69f 426 $grade->str_grade = '-';
427 $grade->str_long_grade = $grade->str_grade;
fcac8e51 428
429 } else {
e9096dc2 430 $grade->str_grade = grade_format_gradevalue($grade->grade, $grade_item);
85a0a69f 431 if ($grade_item->gradetype == GRADE_TYPE_SCALE or $grade_item->get_displaytype() != GRADE_DISPLAY_TYPE_REAL) {
432 $grade->str_long_grade = $grade->str_grade;
433 } else {
434 $a = new object();
435 $a->grade = $grade->str_grade;
436 $a->max = grade_format_gradevalue($grade_item->grademax, $grade_item);
437 $grade->str_long_grade = get_string('gradelong', 'grades', $a);
438 }
fcac8e51 439 }
440
441 // create html representation of feedback
442 if (is_null($grade->feedback)) {
443 $grade->str_feedback = '';
444 } else {
445 $grade->str_feedback = format_text($grade->feedback, $grade->feedbackformat);
446 }
447
448 $item->grades[$userid] = $grade;
449 }
450 }
451 $return->items[$grade_item->itemnumber] = $item;
452
6b5c722d 453 } else {
fcac8e51 454 if (!$grade_outcome = grade_outcome::fetch(array('id'=>$grade_item->outcomeid))) {
455 debugging('Incorect outcomeid found');
456 continue;
457 }
458
459 // outcome info
460 $outcome = new object();
461 $outcome->itemnumber = $grade_item->itemnumber;
462 $outcome->scaleid = $grade_outcome->scaleid;
463 $outcome->name = $grade_outcome->get_name();
464 $outcome->locked = $grade_item->is_locked();
465 $outcome->hidden = $grade_item->is_hidden();
466
467 if (empty($userid_or_ids)) {
468 $userids = array();
469 } else if (is_array($userid_or_ids)) {
470 $userids = $userid_or_ids;
471 } else {
472 $userids = array($userid_or_ids);
473 }
6b5c722d 474
fcac8e51 475 if ($userids) {
476 $grade_grades = grade_grade::fetch_users_grades($grade_item, $userids, true);
477 foreach ($userids as $userid) {
478 $grade_grades[$userid]->grade_item =& $grade_item;
479
480 $grade = new object();
481 $grade->grade = $grade_grades[$userid]->finalgrade;
482 $grade->locked = $grade_grades[$userid]->is_locked();
483 $grade->hidden = $grade_grades[$userid]->is_hidden();
484 $grade->feedback = $grade_grades[$userid]->feedback;
485 $grade->feedbackformat = $grade_grades[$userid]->feedbackformat;
a3fbd494 486 $grade->usermodified = $grade_grades[$userid]->usermodified;
fcac8e51 487
488 // create text representation of grade
489 if (in_array($grade_item->id, $needsupdate)) {
490 $grade->grade = false;
491 $grade->str_grade = get_string('error');
492
493 } else if (is_null($grade->grade)) {
494 $grade->grade = 0;
495 $grade->str_grade = get_string('nooutcome', 'grades');
496
497 } else {
498 $grade->grade = (int)$grade->grade;
499 $scale = $grade_item->load_scale();
500 $grade->str_grade = format_string($scale->scale_items[(int)$grade->grade-1]);
501 }
502
503 // create html representation of feedback
504 if (is_null($grade->feedback)) {
505 $grade->str_feedback = '';
506 } else {
507 $grade->str_feedback = format_text($grade->feedback, $grade->feedbackformat);
508 }
509
510 $outcome->grades[$userid] = $grade;
511 }
512 }
d19f4770 513
514 if (isset($return->outcomes[$grade_item->itemnumber])) {
515 // itemnumber duplicates - lets fix them!
516 $newnumber = $grade_item->itemnumber + 1;
517 while(grade_item::fetch(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid, 'itemnumber'=>$newnumber))) {
518 $newnumber++;
519 }
520 $outcome->itemnumber = $newnumber;
521 $grade_item->itemnumber = $newnumber;
522 $grade_item->update('system');
523 }
524
d886a7ea 525 $return->outcomes[$grade_item->itemnumber] = $outcome;
fcac8e51 526
527 }
6b5c722d 528 }
529 }
530
fcac8e51 531 // sort results using itemnumbers
532 ksort($return->items, SORT_NUMERIC);
533 ksort($return->outcomes, SORT_NUMERIC);
534
535 return $return;
6b5c722d 536}
537
b9f49659 538///////////////////////////////////////////////////////////////////
539///// End of public API for communication with modules/blocks /////
540///////////////////////////////////////////////////////////////////
77dbe708 541
77dbe708 542
612607bd 543
b9f49659 544///////////////////////////////////////////////////////////////////
545///// Internal API: used by gradebook plugins and Moodle core /////
546///////////////////////////////////////////////////////////////////
e0724506 547
548/**
549 * Returns course gradebook setting
550 * @param int $courseid
551 * @param string $name of setting, maybe null if reset only
552 * @param bool $resetcache force reset of internal static cache
553 * @return string value, NULL if no setting
554 */
555function grade_get_setting($courseid, $name, $default=null, $resetcache=false) {
9718765e 556 global $DB;
557
e0724506 558 static $cache = array();
559
560 if ($resetcache or !array_key_exists($courseid, $cache)) {
561 $cache[$courseid] = array();
562
563 } else if (is_null($name)) {
564 return null;
565
566 } else if (array_key_exists($name, $cache[$courseid])) {
567 return $cache[$courseid][$name];
568 }
569
9718765e 570 if (!$data = $DB->get_record('grade_settings', array('courseid'=>$courseid, 'name'=>$name))) {
e0724506 571 $result = null;
572 } else {
573 $result = $data->value;
574 }
575
576 if (is_null($result)) {
577 $result = $default;
578 }
579
580 $cache[$courseid][$name] = $result;
581 return $result;
582}
583
26ed0305 584/**
585 * Returns all course gradebook settings as object properties
586 * @param int $courseid
587 * @return object
588 */
589function grade_get_settings($courseid) {
9718765e 590 global $DB;
591
26ed0305 592 $settings = new object();
593 $settings->id = $courseid;
594
9718765e 595 if ($records = $DB->get_records('grade_settings', array('courseid'=>$courseid))) {
26ed0305 596 foreach ($records as $record) {
597 $settings->{$record->name} = $record->value;
598 }
599 }
600
601 return $settings;
602}
603
e0724506 604/**
605 * Add/update course gradebook setting
606 * @param int $courseid
607 * @param string $name of setting
608 * @param string value, NULL means no setting==remove
609 * @return void
610 */
611function grade_set_setting($courseid, $name, $value) {
9718765e 612 global $DB;
613
e0724506 614 if (is_null($value)) {
9718765e 615 $DB->delete_records('grade_settings', array('courseid'=>$courseid, 'name'=>$name));
e0724506 616
9718765e 617 } else if (!$existing = $DB->get_record('grade_settings', array('courseid'=>$courseid, 'name'=>$name))) {
e0724506 618 $data = new object();
619 $data->courseid = $courseid;
9718765e 620 $data->name = $name;
621 $data->value = $value;
622 $DB->insert_record('grade_settings', $data);
e0724506 623
624 } else {
625 $data = new object();
626 $data->id = $existing->id;
9718765e 627 $data->value = $value;
628 $DB->update_record('grade_settings', $data);
e0724506 629 }
630
631 grade_get_setting($courseid, null, null, true); // reset the cache
632}
633
e9096dc2 634/**
635 * Returns string representation of grade value
636 * @param float $value grade value
637 * @param object $grade_item - by reference to prevent scale reloading
638 * @param bool $localized use localised decimal separator
b9f49659 639 * @param int $displaytype type of display - GRADE_DISPLAY_TYPE_REAL, GRADE_DISPLAY_TYPE_PERCENTAGE, GRADE_DISPLAY_TYPE_LETTER
e9096dc2 640 * @param int $decimalplaces number of decimal places when displaying float values
641 * @return string
642 */
643function grade_format_gradevalue($value, &$grade_item, $localized=true, $displaytype=null, $decimals=null) {
644 if ($grade_item->gradetype == GRADE_TYPE_NONE or $grade_item->gradetype == GRADE_TYPE_TEXT) {
645 return '';
646 }
647
648 // no grade yet?
649 if (is_null($value)) {
650 return '-';
651 }
652
653 if ($grade_item->gradetype != GRADE_TYPE_VALUE and $grade_item->gradetype != GRADE_TYPE_SCALE) {
654 //unknown type??
655 return '';
656 }
657
658 if (is_null($displaytype)) {
659 $displaytype = $grade_item->get_displaytype();
660 }
661
662 if (is_null($decimals)) {
663 $decimals = $grade_item->get_decimals();
664 }
665
666 switch ($displaytype) {
667 case GRADE_DISPLAY_TYPE_REAL:
7d10995c 668 return grade_format_gradevalue_real($value, $grade_item, $decimals, $localized);
e9096dc2 669
670 case GRADE_DISPLAY_TYPE_PERCENTAGE:
7d10995c 671 return grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized);
e9096dc2 672
673 case GRADE_DISPLAY_TYPE_LETTER:
7d10995c 674 return grade_format_gradevalue_letter($value, $grade_item);
e9096dc2 675
7d10995c 676 case GRADE_DISPLAY_TYPE_REAL_PERCENTAGE:
677 return grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ' (' .
678 grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ')';
679
680 case GRADE_DISPLAY_TYPE_REAL_LETTER:
681 return grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ' (' .
682 grade_format_gradevalue_letter($value, $grade_item) . ')';
683
684 case GRADE_DISPLAY_TYPE_PERCENTAGE_REAL:
685 return grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ' (' .
686 grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ')';
687
688 case GRADE_DISPLAY_TYPE_LETTER_REAL:
689 return grade_format_gradevalue_letter($value, $grade_item) . ' (' .
690 grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) . ')';
e9096dc2 691
7d10995c 692 case GRADE_DISPLAY_TYPE_LETTER_PERCENTAGE:
693 return grade_format_gradevalue_letter($value, $grade_item) . ' (' .
694 grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ')';
695
696 case GRADE_DISPLAY_TYPE_PERCENTAGE_LETTER:
697 return grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) . ' (' .
698 grade_format_gradevalue_letter($value, $grade_item) . ')';
e9096dc2 699 default:
700 return '';
701 }
702}
4b86bb08 703
7d10995c 704function grade_format_gradevalue_real($value, $grade_item, $decimals, $localized) {
705 if ($grade_item->gradetype == GRADE_TYPE_SCALE) {
706 if (!$scale = $grade_item->load_scale()) {
707 return get_string('error');
708 }
709
653a8648 710 $value = $grade_item->bounded_grade($value);
7d10995c 711 return format_string($scale->scale_items[$value-1]);
712
713 } else {
714 return format_float($value, $decimals, $localized);
715 }
716}
717
718function grade_format_gradevalue_percentage($value, $grade_item, $decimals, $localized) {
719 $min = $grade_item->grademin;
720 $max = $grade_item->grademax;
721 if ($min == $max) {
722 return '';
723 }
653a8648 724 $value = $grade_item->bounded_grade($value);
7d10995c 725 $percentage = (($value-$min)*100)/($max-$min);
726 return format_float($percentage, $decimals, $localized).' %';
727}
728
729function grade_format_gradevalue_letter($value, $grade_item) {
730 $context = get_context_instance(CONTEXT_COURSE, $grade_item->courseid);
731 if (!$letters = grade_get_letters($context)) {
732 return ''; // no letters??
733 }
734
653a8648 735 if (is_null($value)) {
736 return '-';
737 }
738
7d10995c 739 $value = grade_grade::standardise_score($value, $grade_item->grademin, $grade_item->grademax, 0, 100);
740 $value = bounded_number(0, $value, 100); // just in case
741 foreach ($letters as $boundary => $letter) {
742 if ($value >= $boundary) {
743 return format_string($letter);
744 }
745 }
746 return '-'; // no match? maybe '' would be more correct
747}
748
749
4b86bb08 750/**
751 * Returns grade options for gradebook category menu
752 * @param int $courseid
753 * @param bool $includenew include option for new category (-1)
754 * @return array of grade categories in course
755 */
756function grade_get_categories_menu($courseid, $includenew=false) {
757 $result = array();
758 if (!$categories = grade_category::fetch_all(array('courseid'=>$courseid))) {
759 //make sure course category exists
29c660c4 760 if (!grade_category::fetch_course_category($courseid)) {
4b86bb08 761 debugging('Can not create course grade category!');
762 return $result;
763 }
764 $categories = grade_category::fetch_all(array('courseid'=>$courseid));
765 }
766 foreach ($categories as $key=>$category) {
767 if ($category->is_course_category()) {
768 $result[$category->id] = get_string('uncategorised', 'grades');
769 unset($categories[$key]);
770 }
771 }
772 if ($includenew) {
773 $result[-1] = get_string('newcategory', 'grades');
774 }
775 $cats = array();
776 foreach ($categories as $category) {
777 $cats[$category->id] = $category->get_name();
778 }
779 asort($cats, SORT_LOCALE_STRING);
780
781 return ($result+$cats);
782}
e9096dc2 783
784/**
785 * Returns grade letters array used in context
786 * @param object $context object or null for defaults
787 * @return array of grade_boundary=>letter_string
788 */
789function grade_get_letters($context=null) {
9718765e 790 global $DB;
791
e9096dc2 792 if (empty($context)) {
284abb09 793 //default grading letters
794 return array('93'=>'A', '90'=>'A-', '87'=>'B+', '83'=>'B', '80'=>'B-', '77'=>'C+', '73'=>'C', '70'=>'C-', '67'=>'D+', '60'=>'D', '0'=>'F');
e9096dc2 795 }
796
797 static $cache = array();
798
799 if (array_key_exists($context->id, $cache)) {
800 return $cache[$context->id];
801 }
802
803 if (count($cache) > 100) {
804 $cache = array(); // cache size limit
805 }
806
807 $letters = array();
808
809 $contexts = get_parent_contexts($context);
810 array_unshift($contexts, $context->id);
811
812 foreach ($contexts as $ctxid) {
9718765e 813 if ($records = $DB->get_records('grade_letters', array('contextid'=>$ctxid), 'lowerboundary DESC')) {
e9096dc2 814 foreach ($records as $record) {
284abb09 815 $letters[$record->lowerboundary] = $record->letter;
e9096dc2 816 }
817 }
818
819 if (!empty($letters)) {
820 $cache[$context->id] = $letters;
821 return $letters;
822 }
823 }
824
825 $letters = grade_get_letters(null);
826 $cache[$context->id] = $letters;
827 return $letters;
828}
829
60243313 830
831/**
2c5e52e2 832 * Verify new value of idnumber - checks for uniqueness of new idnumbers, old are kept intact
60243313 833 * @param string idnumber string (with magic quotes)
204175c5 834 * @param int $courseid - id numbers are course unique only
60243313 835 * @param object $cm used for course module idnumbers and items attached to modules
836 * @param object $gradeitem is item idnumber
837 * @return boolean true means idnumber ok
838 */
204175c5 839function grade_verify_idnumber($idnumber, $courseid, $grade_item=null, $cm=null) {
9718765e 840 global $DB;
841
60243313 842 if ($idnumber == '') {
843 //we allow empty idnumbers
844 return true;
845 }
846
847 // keep existing even when not unique
848 if ($cm and $cm->idnumber == $idnumber) {
849 return true;
850 } else if ($grade_item and $grade_item->idnumber == $idnumber) {
851 return true;
852 }
853
9718765e 854 if ($DB->record_exists('course_modules', array('course'=>$courseid, 'idnumber'=>$idnumber))) {
60243313 855 return false;
856 }
857
9718765e 858 if ($DB->record_exists('grade_items', array('courseid'=>$courseid, 'idnumber'=>$idnumber))) {
60243313 859 return false;
860 }
861
862 return true;
863}
864
865/**
866 * Force final grade recalculation in all course items
867 * @param int $courseid
868 */
f8e6e4db 869function grade_force_full_regrading($courseid) {
9718765e 870 global $DB;
871 $DB->set_field('grade_items', 'needsupdate', 1, array('courseid'=>$courseid));
f8e6e4db 872}
34e67f76 873
653a8648 874/**
875 * Forces regrading of all site grades - usualy when chanign site setings
876 */
877function grade_force_site_regrading() {
878 global $CFG, $DB;
ae5a3939 879 $DB->set_field('grade_items', 'needsupdate', 1);
653a8648 880}
881
5834dcdb 882/**
ac9b0805 883 * Updates all final grades in course.
a8995b34 884 *
885 * @param int $courseid
f8e6e4db 886 * @param int $userid if specified, try to do a quick regrading of grades of this user only
887 * @param object $updated_item the item in which
888 * @return boolean true if ok, array of errors if problems found (item id is used as key)
a8995b34 889 */
c86caae7 890function grade_regrade_final_grades($courseid, $userid=null, $updated_item=null) {
b8ff92b6 891
514a3467 892 $course_item = grade_item::fetch_course_item($courseid);
f04873a9 893
f8e6e4db 894 if ($userid) {
895 // one raw grade updated for one user
896 if (empty($updated_item)) {
2f137aa1 897 print_error("cannotbenull", 'debug', '', "updated_item");
f8e6e4db 898 }
899 if ($course_item->needsupdate) {
900 $updated_item->force_regrading();
b45d8391 901 return array($course_item->id =>'Can not do fast regrading after updating of raw grades');
a8995b34 902 }
772ddfbf 903
f8e6e4db 904 } else {
905 if (!$course_item->needsupdate) {
906 // nothing to do :-)
b8ff92b6 907 return true;
b8ff92b6 908 }
a8995b34 909 }
910
f8e6e4db 911 $grade_items = grade_item::fetch_all(array('courseid'=>$courseid));
912 $depends_on = array();
913
914 // first mark all category and calculated items as needing regrading
fb0e3570 915 // this is slower, but 100% accurate
f8e6e4db 916 foreach ($grade_items as $gid=>$gitem) {
fb46b5b6 917 if (!empty($updated_item) and $updated_item->id == $gid) {
f8e6e4db 918 $grade_items[$gid]->needsupdate = 1;
919
eacd3700 920 } else if ($gitem->is_course_item() or $gitem->is_category_item() or $gitem->is_calculated()) {
f8e6e4db 921 $grade_items[$gid]->needsupdate = 1;
922 }
2e53372c 923
f8e6e4db 924 // construct depends_on lookup array
925 $depends_on[$gid] = $grade_items[$gid]->depends_on();
926 }
2e53372c 927
d14ae855 928 $errors = array();
b8ff92b6 929 $finalids = array();
d14ae855 930 $gids = array_keys($grade_items);
eacd3700 931 $failed = 0;
d14ae855 932
933 while (count($finalids) < count($gids)) { // work until all grades are final or error found
934 $count = 0;
935 foreach ($gids as $gid) {
936 if (in_array($gid, $finalids)) {
937 continue; // already final
938 }
939
940 if (!$grade_items[$gid]->needsupdate) {
941 $finalids[] = $gid; // we can make it final - does not need update
b8ff92b6 942 continue;
943 }
944
b8ff92b6 945 $doupdate = true;
f8e6e4db 946 foreach ($depends_on[$gid] as $did) {
b8ff92b6 947 if (!in_array($did, $finalids)) {
948 $doupdate = false;
d14ae855 949 continue; // this item depends on something that is not yet in finals array
b8ff92b6 950 }
951 }
952
953 //oki - let's update, calculate or aggregate :-)
954 if ($doupdate) {
d14ae855 955 $result = $grade_items[$gid]->regrade_final_grades($userid);
f8e6e4db 956
957 if ($result === true) {
d14ae855 958 $grade_items[$gid]->regrading_finished();
fb0e3570 959 $grade_items[$gid]->check_locktime(); // do the locktime item locking
f8e6e4db 960 $count++;
b8ff92b6 961 $finalids[] = $gid;
fb0e3570 962
f8e6e4db 963 } else {
d14ae855 964 $grade_items[$gid]->force_regrading();
f8e6e4db 965 $errors[$gid] = $result;
b8ff92b6 966 }
967 }
968 }
969
970 if ($count == 0) {
eacd3700 971 $failed++;
972 } else {
973 $failed = 0;
974 }
975
976 if ($failed > 1) {
d14ae855 977 foreach($gids as $gid) {
978 if (in_array($gid, $finalids)) {
979 continue; // this one is ok
980 }
981 $grade_items[$gid]->force_regrading();
982 $errors[$grade_items[$gid]->id] = 'Probably circular reference or broken calculation formula'; // TODO: localize
b8ff92b6 983 }
d14ae855 984 break; // oki, found error
b8ff92b6 985 }
986 }
987
988 if (count($errors) == 0) {
fb0e3570 989 if (empty($userid)) {
990 // do the locktime locking of grades, but only when doing full regrading
fed7cdc9 991 grade_grade::check_locktime_all($gids);
fb0e3570 992 }
b8ff92b6 993 return true;
994 } else {
995 return $errors;
996 }
a8995b34 997}
967f222f 998
ac9b0805 999/**
f0362b5d 1000 * Refetches data from all course activities
1001 * @param int $courseid
1002 * @param string $modname
1003 * @return success
ac9b0805 1004 */
f0362b5d 1005function grade_grab_course_grades($courseid, $modname=null) {
9718765e 1006 global $CFG, $DB;
ac9b0805 1007
f0362b5d 1008 if ($modname) {
1009 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
9718765e 1010 FROM {".$modname."} a, {course_modules} cm, {modules} m
1011 WHERE m.name=:modname AND m.visible=1 AND m.id=cm.module AND cm.instance=a.id AND cm.course=:courseid";
1012 $params = array('modname'=>$modname, 'courseid'=>$courseid);
f0362b5d 1013
9718765e 1014 if ($modinstances = $DB->get_records_sql($sql, $params)) {
f0362b5d 1015 foreach ($modinstances as $modinstance) {
1016 grade_update_mod_grades($modinstance);
1017 }
1018 }
1019 return;
1020 }
1021
ac9b0805 1022 if (!$mods = get_list_of_plugins('mod') ) {
2f137aa1 1023 print_error('nomodules', 'debug');
ac9b0805 1024 }
1025
1026 foreach ($mods as $mod) {
ac9b0805 1027 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
1028 continue;
1029 }
1030
ac9b0805 1031 $fullmod = $CFG->dirroot.'/mod/'.$mod;
1032
1033 // include the module lib once
1034 if (file_exists($fullmod.'/lib.php')) {
f0362b5d 1035 // get all instance of the activity
1036 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
9718765e 1037 FROM {".$mod."} a, {course_modules} cm, {modules} m
1038 WHERE m.name=:mod AND m.visible=1 AND m.id=cm.module AND cm.instance=a.id AND cm.course=:courseid";
1039 $params = array('mod'=>$mod, 'courseid'=>$courseid);
f0362b5d 1040
9718765e 1041 if ($modinstances = $DB->get_records_sql($sql, $params)) {
f0362b5d 1042 foreach ($modinstances as $modinstance) {
1043 grade_update_mod_grades($modinstance);
1044 }
ac9b0805 1045 }
1046 }
1047 }
1048}
1049
d185c3ee 1050/**
398a160d 1051 * Force full update of module grades in central gradebook
d185c3ee 1052 * @param object $modinstance object with extra cmidnumber and modname property
1053 * @return boolean success
1054 */
2b0f65e2 1055function grade_update_mod_grades($modinstance, $userid=0) {
9718765e 1056 global $CFG, $DB;
d185c3ee 1057
1058 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
1059 if (!file_exists($fullmod.'/lib.php')) {
653a8648 1060 debugging('missing lib.php file in module ' . $modinstance->modname);
d185c3ee 1061 return false;
1062 }
1063 include_once($fullmod.'/lib.php');
1064
d185c3ee 1065 $updategradesfunc = $modinstance->modname.'_update_grades';
1066 $updateitemfunc = $modinstance->modname.'_grade_item_update';
1067
398a160d 1068 if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
d185c3ee 1069 //new grading supported, force updating of grades
1070 $updateitemfunc($modinstance);
2b0f65e2 1071 $updategradesfunc($modinstance, $userid);
d185c3ee 1072
1073 } else {
2b0f65e2 1074 // mudule does not support grading??
d185c3ee 1075 }
1076
1077 return true;
1078}
de420c11 1079
b51ece5b 1080/**
1081 * Remove grade letters for given context
1082 * @param object $context
1083 */
1084function remove_grade_letters($context, $showfeedback) {
9718765e 1085 global $DB;
1086
b51ece5b 1087 $strdeleted = get_string('deleted');
1088
9718765e 1089 $DB->delete_records('grade_letters', array('contextid'=>$context->id));
b51ece5b 1090 if ($showfeedback) {
1091 notify($strdeleted.' - '.get_string('letters', 'grades'));
1092 }
1093}
f615fbab 1094/**
1095 * Remove all grade related course data - history is kept
1096 * @param int $courseid
e2b347e9 1097 * @param bool $showfeedback print feedback
f615fbab 1098 */
1099function remove_course_grades($courseid, $showfeedback) {
9718765e 1100 global $DB;
1101
f615fbab 1102 $strdeleted = get_string('deleted');
1103
1104 $course_category = grade_category::fetch_course_category($courseid);
1105 $course_category->delete('coursedelete');
1106 if ($showfeedback) {
1107 notify($strdeleted.' - '.get_string('grades', 'grades').', '.get_string('items', 'grades').', '.get_string('categories', 'grades'));
1108 }
1109
1110 if ($outcomes = grade_outcome::fetch_all(array('courseid'=>$courseid))) {
1111 foreach ($outcomes as $outcome) {
1112 $outcome->delete('coursedelete');
1113 }
1114 }
9718765e 1115 $DB->delete_records('grade_outcomes_courses', array('courseid'=>$courseid));
f615fbab 1116 if ($showfeedback) {
1117 notify($strdeleted.' - '.get_string('outcomes', 'grades'));
1118 }
1119
1120 if ($scales = grade_scale::fetch_all(array('courseid'=>$courseid))) {
1121 foreach ($scales as $scale) {
1122 $scale->delete('coursedelete');
1123 }
1124 }
1125 if ($showfeedback) {
1126 notify($strdeleted.' - '.get_string('scales'));
1127 }
b51ece5b 1128
9718765e 1129 $DB->delete_records('grade_settings', array('courseid'=>$courseid));
b51ece5b 1130 if ($showfeedback) {
1131 notify($strdeleted.' - '.get_string('settings', 'grades'));
1132 }
f615fbab 1133}
bfe7297e 1134
e2b347e9 1135/**
1136 * Called when course category deleted - cleanup gradebook
1137 * @param int $categoryid course category id
1138 * @param int $newparentid empty means everything deleted, otherwise id of category where content moved
1139 * @param bool $showfeedback print feedback
1140 */
1141function grade_course_category_delete($categoryid, $newparentid, $showfeedback) {
9718765e 1142 global $DB;
1143
e2b347e9 1144 $context = get_context_instance(CONTEXT_COURSECAT, $categoryid);
9718765e 1145 $DB->delete_records('grade_letters', array('contextid'=>$context->id));
e2b347e9 1146}
1147
8a0a6046 1148/**
1149 * Does gradebook cleanup when module uninstalled.
1150 */
1151function grade_uninstalled_module($modname) {
9718765e 1152 global $CFG, $DB;
8a0a6046 1153
1154 $sql = "SELECT *
9718765e 1155 FROM {grade_items}
1156 WHERE itemtype='mod' AND itemmodule=?";
8a0a6046 1157
1158 // go all items for this module and delete them including the grades
9718765e 1159 if ($rs = $DB->get_recordset_sql($sql, array($modname))) {
1160 foreach ($rs as $item) {
8a0a6046 1161 $grade_item = new grade_item($item, false);
1162 $grade_item->delete('moduninstall');
1163 }
9718765e 1164 $rs->close();
8a0a6046 1165 }
1166}
1167
2650c51e 1168/**
1169 * Grading cron job
1170 */
1171function grade_cron() {
9718765e 1172 global $CFG, $DB;
26101be8 1173
1174 $now = time();
1175
1176 $sql = "SELECT i.*
9718765e 1177 FROM {grade_items} i
1178 WHERE i.locked = 0 AND i.locktime > 0 AND i.locktime < ? AND EXISTS (
1179 SELECT 'x' FROM {grade_items} c WHERE c.itemtype='course' AND c.needsupdate=0 AND c.courseid=i.courseid)";
26101be8 1180
2650c51e 1181 // go through all courses that have proper final grades and lock them if needed
845fe812 1182 if ($rs = $DB->get_recordset_sql($sql, array($now))) {
9718765e 1183 foreach ($rs as $item) {
03cedd62 1184 $grade_item = new grade_item($item, false);
1185 $grade_item->locked = $now;
1186 $grade_item->update('locktime');
2650c51e 1187 }
9718765e 1188 $rs->close();
2650c51e 1189 }
26101be8 1190
fcac8e51 1191 $grade_inst = new grade_grade();
1192 $fields = 'g.'.implode(',g.', $grade_inst->required_fields);
1193
1194 $sql = "SELECT $fields
9718765e 1195 FROM {grade_grades} g, {grade_items} i
1196 WHERE g.locked = 0 AND g.locktime > 0 AND g.locktime < ? AND g.itemid=i.id AND EXISTS (
1197 SELECT 'x' FROM {grade_items} c WHERE c.itemtype='course' AND c.needsupdate=0 AND c.courseid=i.courseid)";
26101be8 1198
1199 // go through all courses that have proper final grades and lock them if needed
9718765e 1200 if ($rs = $DB->get_recordset_sql($sql, array($now))) {
1201 foreach ($rs as $grade) {
03cedd62 1202 $grade_grade = new grade_grade($grade, false);
1203 $grade_grade->locked = $now;
1204 $grade_grade->update('locktime');
26101be8 1205 }
9718765e 1206 $rs->close();
26101be8 1207 }
1208
1ee0df06 1209 //TODO: do not run this cleanup every cron invocation
1210 // cleanup history tables
f0362b5d 1211 if (!empty($CFG->gradehistorylifetime)) { // value in days
1212 $histlifetime = $now - ($CFG->gradehistorylifetime * 3600 * 24);
1213 $tables = array('grade_outcomes_history', 'grade_categories_history', 'grade_items_history', 'grade_grades_history', 'scale_history');
1214 foreach ($tables as $table) {
9718765e 1215 if ($DB->delete_records_select($table, "timemodified < ?", array($histlifetime))) {
f0362b5d 1216 mtrace(" Deleted old grade history records from '$table'");
1ee0df06 1217 }
1218 }
f0362b5d 1219 }
1220}
1221
1222/**
1223 * Resel all course grades
1224 * @param int $courseid
1225 * @return success
1226 */
1227function grade_course_reset($courseid) {
1228
1229 // no recalculations
1230 grade_force_full_regrading($courseid);
1231
1232 $grade_items = grade_item::fetch_all(array('courseid'=>$courseid));
1233 foreach ($grade_items as $gid=>$grade_item) {
1234 $grade_item->delete_all_grades('reset');
1235 }
1ee0df06 1236
f0362b5d 1237 //refetch all grades
1238 grade_grab_course_grades($courseid);
1ee0df06 1239
f0362b5d 1240 // recalculate all grades
1241 grade_regrade_final_grades($courseid);
1242 return true;
2650c51e 1243}
1244
b45d8391 1245/**
66690b69 1246 * Convert number to 5 decimalfloat, empty tring or null db compatible format
1247 * (we need this to decide if db value changed)
b45d8391 1248 * @param mixed number
1249 * @return mixed float or null
1250 */
1251function grade_floatval($number) {
66690b69 1252 if (is_null($number) or $number === '') {
b45d8391 1253 return null;
1254 }
66690b69 1255 // we must round to 5 digits to get the same precision as in 10,5 db fields
25bcd908 1256 // note: db rounding for 10,5 is different from php round() function
66690b69 1257 return round($number, 5);
b45d8391 1258}
25bcd908 1259
1260/**
1261 * Compare two float numbers safely. Uses 5 decimals php precision. Nulls accepted too.
1262 * Used for skipping of db updates
1263 * @param float $f1
1264 * @param float $f2
1265 * @return true if different
1266 */
1267function grade_floats_different($f1, $f2) {
1268 // note: db rounding for 10,5 is different from php round() function
1269 return (grade_floatval($f1) !== grade_floatval($f2));
1270}
1271
60cf7430 1272?>