Added hooks to qtype import/export
[moodle-linuxchix.git] / lib / gradelib.php
blob4211c70d42f276bed522c69794cd1772e5cc5cc5
1 <?php // $Id$
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 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * Library of functions for gradebook
29 * @author Moodle HQ developers
30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package moodlecore
35 // category aggregation types
36 define('GRADE_AGGREGATE_MEAN_ALL', 0);
37 define('GRADE_AGGREGATE_MEAN_GRADED', 1);
38 define('GRADE_AGGREGATE_MEDIAN_ALL', 2);
39 define('GRADE_AGGREGATE_MEDIAN_GRADED', 3);
40 define('GRADE_AGGREGATE_MIN_ALL', 4);
41 define('GRADE_AGGREGATE_MIN_GRADED', 5);
42 define('GRADE_AGGREGATE_MAX_ALL', 6);
43 define('GRADE_AGGREGATE_MAX_GRADED', 7);
44 define('GRADE_AGGREGATE_MODE_ALL', 8);
45 define('GRADE_AGGREGATE_MODE_GRADED', 9);
46 define('GRADE_AGGREGATE_WEIGHTED_MEAN_ALL', 10);
47 define('GRADE_AGGREGATE_WEIGHTED_MEAN_GRADED', 11);
48 define('GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL', 12);
49 define('GRADE_AGGREGATE_EXTRACREDIT_MEAN_GRADED', 13);
51 // grade types
52 define('GRADE_TYPE_NONE', 0);
53 define('GRADE_TYPE_VALUE', 1);
54 define('GRADE_TYPE_SCALE', 2);
55 define('GRADE_TYPE_TEXT', 3);
57 // grade_update() return status
58 define('GRADE_UPDATE_OK', 0);
59 define('GRADE_UPDATE_FAILED', 1);
60 define('GRADE_UPDATE_MULTIPLE', 2);
61 define('GRADE_UPDATE_ITEM_DELETED', 3);
62 define('GRADE_UPDATE_ITEM_LOCKED', 4);
64 // Grate teables history tracking actions
65 define('GRADE_HISTORY_INSERT', 1);
66 define('GRADE_HISTORY_UPDATE', 2);
67 define('GRADE_HISTORY_DELETE', 3);
69 define('GRADE_REPORT_AGGREGATION_POSITION_LEFT', 0);
70 define('GRADE_REPORT_AGGREGATION_POSITION_RIGHT', 1);
71 define('GRADE_REPORT_AGGREGATION_VIEW_FULL', 0);
72 define('GRADE_REPORT_AGGREGATION_VIEW_COMPACT', 1);
73 define('GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL', 0);
74 define('GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE', 1);
75 define('GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER', 2);
76 define('GRADE_REPORT_PREFERENCE_DEFAULT', 'default');
77 define('GRADE_REPORT_PREFERENCE_INHERIT', 'inherit');
78 define('GRADE_REPORT_PREFERENCE_UNUSED', -1);
80 require_once($CFG->libdir . '/grade/grade_category.php');
81 require_once($CFG->libdir . '/grade/grade_item.php');
82 require_once($CFG->libdir . '/grade/grade_grade.php');
83 require_once($CFG->libdir . '/grade/grade_scale.php');
84 require_once($CFG->libdir . '/grade/grade_outcome.php');
85 require_once($CFG->libdir . '/grade/grade_grade_text.php');
87 /***** PUBLIC GRADE API *****/
89 /**
90 * Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
91 * rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
92 * or key means do not change existing.
94 * Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
95 * 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
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
99 * @param string $itemtype type of grade item - mod, block
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
106 function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
107 global $USER;
109 // only following grade_item properties can be changed in this function
110 $allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
112 // grade item identification
113 $params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
115 if (is_null($courseid) or is_null($itemtype)) {
116 debugging('Missing courseid or itemtype');
117 return GRADE_UPDATE_FAILED;
120 if (!$grade_items = grade_item::fetch_all($params)) {
121 // create a new one
122 $grade_item = false;
124 } else if (count($grade_items) == 1){
125 $grade_item = reset($grade_items);
126 unset($grade_items); //release memory
128 } else {
129 debugging('Found more than one grade item');
130 return GRADE_UPDATE_MULTIPLE;
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;
141 return GRADE_UPDATE_OK;
144 /// Create or update the grade_item if needed
145 if (!$grade_item) {
146 if ($itemdetails) {
147 $itemdetails = (array)$itemdetails;
149 // grademin and grademax ignored when scale specified
150 if (array_key_exists('scaleid', $itemdetails)) {
151 if ($itemdetails['scaleid']) {
152 unset($itemdetails['grademin']);
153 unset($itemdetails['grademax']);
157 foreach ($itemdetails as $k=>$v) {
158 if (!in_array($k, $allowed)) {
159 // ignore it
160 continue;
162 if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
163 // no grade item needed!
164 return GRADE_UPDATE_OK;
166 $params[$k] = $v;
169 $grade_item = new grade_item($params);
170 $grade_item->insert();
172 } else {
173 if ($grade_item->is_locked()) {
174 debugging('Grading item is locked!');
175 return GRADE_UPDATE_ITEM_LOCKED;
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;
186 if ($grade_item->{$k} != $v) {
187 $grade_item->{$k} = $v;
188 $update = true;
191 if ($update) {
192 $grade_item->update();
197 /// Some extra checks
198 // do we use grading?
199 if ($grade_item->gradetype == GRADE_TYPE_NONE) {
200 return GRADE_UPDATE_OK;
203 // no grade submitted
204 if (empty($grades)) {
205 return GRADE_UPDATE_OK;
208 /// Finally start processing of grades
209 if (is_object($grades)) {
210 $grades = array($grades);
211 } else {
212 if (array_key_exists('userid', $grades)) {
213 $grades = array($grades);
217 $failed = false;
218 foreach ($grades as $grade) {
219 $grade = (array)$grade;
220 if (empty($grade['userid'])) {
221 $failed = true;
222 debugging('Invalid userid in grade submitted');
223 continue;
224 } else {
225 $userid = $grade['userid'];
228 $rawgrade = false;
229 $feedback = false;
230 $feedbackformat = FORMAT_MOODLE;
232 if (array_key_exists('rawgrade', $grade)) {
233 $rawgrade = $grade['rawgrade'];
236 if (array_key_exists('feedback', $grade)) {
237 $feedback = $grade['feedback'];
240 if (array_key_exists('feedbackformat', $grade)) {
241 $feedbackformat = $grade['feedbackformat'];
244 if (array_key_exists('usermodified', $grade)) {
245 $usermodified = $grade['usermodified'];
246 } else {
247 $usermodified = $USER->id;
250 // update or insert the grade
251 if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat, $usermodified)) {
252 $failed = true;
256 if (!$failed) {
257 return GRADE_UPDATE_OK;
258 } else {
259 return GRADE_UPDATE_FAILED;
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.
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
280 function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
282 if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) {
283 return false;
285 } else if (count($grade_items) == 1){
286 $grade_item = reset($grade_items);
287 return $grade_item->is_locked($userid);
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;
296 return false;
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
309 function grade_get_outcomes($courseid, $itemtype, $itemmodule, $iteminstance, $userid=0) {
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;
316 if (!$outcome = grade_outcome::fetch(array('id'=>$item->outcomeid))) {
317 debugging('Incorect outcomeid found');
318 continue;
320 // prepare outcome info with user grade
321 $o = new object();
322 $o->scaleid = $outcome->scaleid;
323 $o->name = $outcome->fullname;
325 if (empty($userid)) {
326 //no user info
327 } if ($grade = $item->get_grade($userid,false)) {
328 $o->grade = $grade->finalgrade;
329 $o->locked = $grade->is_locked();
330 } else {
331 $o->grade = null;
332 $o->locked = $item->is_locked();
334 $o->grade = intval($o->grade); // 0 means no grade, int for scales
336 $result[$item->itemnumber] = $o;
340 return $result;
344 * Updates outcomes of user
345 * @param int $courseid id of course
346 * @param string $itemtype 'mod', 'block'
347 * @param string $itemmodule 'forum, 'quiz', etc.
348 * @param int $iteminstance id of the item module
349 * @param int $userid ID of the graded user
351 function grade_update_outcomes($source, $courseid, $itemtype, $itemmodule, $iteminstance, $userid, $data) {
352 if ($items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
353 foreach ($items as $item) {
354 if (!array_key_exists($item->itemnumber, $data)) {
355 continue;
357 $grade = $data[$item->itemnumber] < 1 ? null : $data[$item->itemnumber];
358 $item->update_final_grade($userid, $grade, $source);
363 /***** END OF PUBLIC API *****/
367 * Verify nwe value of idnumber - checks for uniqueness of new idnubmers, old are kept intact
368 * @param string idnumber string (with magic quotes)
369 * @param object $cm used for course module idnumbers and items attached to modules
370 * @param object $gradeitem is item idnumber
371 * @return boolean true means idnumber ok
373 function grade_verify_idnumber($idnumber, $grade_item=null, $cm=null) {
374 if ($idnumber == '') {
375 //we allow empty idnumbers
376 return true;
379 // keep existing even when not unique
380 if ($cm and $cm->idnumber == $idnumber) {
381 return true;
382 } else if ($grade_item and $grade_item->idnumber == $idnumber) {
383 return true;
386 if (get_records('course_modules', 'idnumber', $idnumber)) {
387 return false;
390 if (get_records('grade_items', 'idnumber', $idnumber)) {
391 return false;
394 return true;
398 * Force final grade recalculation in all course items
399 * @param int $courseid
401 function grade_force_full_regrading($courseid) {
402 set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
406 * Updates all final grades in course.
408 * @param int $courseid
409 * @param int $userid if specified, try to do a quick regrading of grades of this user only
410 * @param object $updated_item the item in which
411 * @return boolean true if ok, array of errors if problems found (item id is used as key)
413 function grade_regrade_final_grades($courseid, $userid=null, $updated_item=null) {
415 $course_item = grade_item::fetch_course_item($courseid);
417 if ($userid) {
418 // one raw grade updated for one user
419 if (empty($updated_item)) {
420 error("updated_item_id can not be null!");
422 if ($course_item->needsupdate) {
423 $updated_item->force_regrading();
424 return 'Can not do fast regrading after updating of raw grades';
427 } else {
428 if (!$course_item->needsupdate) {
429 // nothing to do :-)
430 return true;
434 $grade_items = grade_item::fetch_all(array('courseid'=>$courseid));
435 $depends_on = array();
437 // first mark all category and calculated items as needing regrading
438 // this is slower, but 100% accurate
439 foreach ($grade_items as $gid=>$gitem) {
440 if (!empty($updated_item) and $updated_item->id == $gid) {
441 $grade_items[$gid]->needsupdate = 1;
443 } else if ($gitem->is_course_item() or $gitem->is_category_item() or $gitem->is_calculated()) {
444 $grade_items[$gid]->needsupdate = 1;
447 // construct depends_on lookup array
448 $depends_on[$gid] = $grade_items[$gid]->depends_on();
451 $errors = array();
452 $finalids = array();
453 $gids = array_keys($grade_items);
454 $failed = 0;
456 while (count($finalids) < count($gids)) { // work until all grades are final or error found
457 $count = 0;
458 foreach ($gids as $gid) {
459 if (in_array($gid, $finalids)) {
460 continue; // already final
463 if (!$grade_items[$gid]->needsupdate) {
464 $finalids[] = $gid; // we can make it final - does not need update
465 continue;
468 $doupdate = true;
469 foreach ($depends_on[$gid] as $did) {
470 if (!in_array($did, $finalids)) {
471 $doupdate = false;
472 continue; // this item depends on something that is not yet in finals array
476 //oki - let's update, calculate or aggregate :-)
477 if ($doupdate) {
478 $result = $grade_items[$gid]->regrade_final_grades($userid);
480 if ($result === true) {
481 $grade_items[$gid]->regrading_finished();
482 $grade_items[$gid]->check_locktime(); // do the locktime item locking
483 $count++;
484 $finalids[] = $gid;
486 } else {
487 $grade_items[$gid]->force_regrading();
488 $errors[$gid] = $result;
493 if ($count == 0) {
494 $failed++;
495 } else {
496 $failed = 0;
499 if ($failed > 1) {
500 foreach($gids as $gid) {
501 if (in_array($gid, $finalids)) {
502 continue; // this one is ok
504 $grade_items[$gid]->force_regrading();
505 $errors[$grade_items[$gid]->id] = 'Probably circular reference or broken calculation formula'; // TODO: localize
507 break; // oki, found error
511 if (count($errors) == 0) {
512 if (empty($userid)) {
513 // do the locktime locking of grades, but only when doing full regrading
514 grade_grade::check_locktime_all($gids);
516 return true;
517 } else {
518 return $errors;
523 * For backwards compatibility with old third-party modules, this function can
524 * be used to import all grades from activities with legacy grading.
525 * @param int $courseid or null if all courses
527 function grade_grab_legacy_grades($courseid=null) {
529 global $CFG;
531 if (!$mods = get_list_of_plugins('mod') ) {
532 error('No modules installed!');
535 if ($courseid) {
536 $course_sql = " AND cm.course=$courseid";
537 } else {
538 $course_sql = "";
541 foreach ($mods as $mod) {
543 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
544 continue;
547 if (!$module = get_record('modules', 'name', $mod)) {
548 //not installed
549 continue;
552 if (!$module->visible) {
553 //disabled module
554 continue;
557 $fullmod = $CFG->dirroot.'/mod/'.$mod;
559 // include the module lib once
560 if (file_exists($fullmod.'/lib.php')) {
561 include_once($fullmod.'/lib.php');
562 // look for modname_grades() function - old gradebook pulling function
563 // if present sync the grades with new grading system
564 $gradefunc = $mod.'_grades';
565 if (function_exists($gradefunc)) {
567 // get all instance of the activity
568 $sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
569 FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
570 WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id $course_sql";
572 if ($modinstances = get_records_sql($sql)) {
573 foreach ($modinstances as $modinstance) {
574 grade_update_mod_grades($modinstance);
583 * For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
585 function grade_grab_grades() {
587 global $CFG;
589 if (!$mods = get_list_of_plugins('mod') ) {
590 error('No modules installed!');
593 foreach ($mods as $mod) {
595 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
596 continue;
599 if (!$module = get_record('modules', 'name', $mod)) {
600 //not installed
601 continue;
604 if (!$module->visible) {
605 //disabled module
606 continue;
609 $fullmod = $CFG->dirroot.'/mod/'.$mod;
611 // include the module lib once
612 if (file_exists($fullmod.'/lib.php')) {
613 include_once($fullmod.'/lib.php');
614 // look for modname_grades() function - old gradebook pulling function
615 // if present sync the grades with new grading system
616 $gradefunc = $mod.'_update_grades';
617 if (function_exists($gradefunc)) {
618 $gradefunc();
625 * Force full update of module grades in central gradebook - works for both legacy and converted activities.
626 * @param object $modinstance object with extra cmidnumber and modname property
627 * @return boolean success
629 function grade_update_mod_grades($modinstance, $userid=0) {
630 global $CFG;
632 $fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
633 if (!file_exists($fullmod.'/lib.php')) {
634 debugging('missing lib.php file in module');
635 return false;
637 include_once($fullmod.'/lib.php');
639 // does it use legacy grading?
640 $gradefunc = $modinstance->modname.'_grades';
641 $updategradesfunc = $modinstance->modname.'_update_grades';
642 $updateitemfunc = $modinstance->modname.'_grade_item_update';
644 if (function_exists($gradefunc)) {
646 // legacy module - not yet converted
647 if ($oldgrades = $gradefunc($modinstance->id)) {
649 $grademax = $oldgrades->maxgrade;
650 $scaleid = NULL;
651 if (!is_numeric($grademax)) {
652 // scale name is provided as a string, try to find it
653 if (!$scale = get_record('scale', 'name', $grademax)) {
654 debugging('Incorrect scale name! name:'.$grademax);
655 return false;
657 $scaleid = $scale->id;
660 if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
661 debugging('Can not get/create legacy grade item!');
662 return false;
665 $grades = array();
666 foreach ($oldgrades->grades as $uid=>$usergrade) {
667 if ($userid and $uid != $userid) {
668 continue;
670 $grade = new object();
671 $grade->userid = $uid;
673 if ($usergrade == '-') {
674 // no grade
675 $grade->rawgrade = null;
677 } else if ($scaleid) {
678 // scale in use, words used
679 $gradescale = explode(",", $scale->scale);
680 $grade->rawgrade = array_search($usergrade, $gradescale) + 1;
682 } else {
683 // good old numeric value
684 $grade->rawgrade = $usergrade;
686 $grades[] = $grade;
689 grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
690 $grade_item->iteminstance, $grade_item->itemnumber, $grades);
693 } else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
694 //new grading supported, force updating of grades
695 $updateitemfunc($modinstance);
696 $updategradesfunc($modinstance, $userid);
698 } else {
699 // mudule does not support grading??
702 return true;
706 * Get and update/create grade item for legacy modules.
708 function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
710 // does it already exist?
711 if ($grade_items = grade_item::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>0))) {
712 if (count($grade_items) > 1) {
713 debugging('Multiple legacy grade_items found.');
714 return false;
717 $grade_item = reset($grade_items);
719 if (is_null($grademax) and is_null($scaleid)) {
720 $grade_item->gradetype = GRADE_TYPE_NONE;
722 } else if ($scaleid) {
723 $grade_item->gradetype = GRADE_TYPE_SCALE;
724 $grade_item->scaleid = $scaleid;
725 $grade_item->grademin = 1;
727 } else {
728 $grade_item->gradetype = GRADE_TYPE_VALUE;
729 $grade_item->grademax = $grademax;
730 $grade_item->grademin = 0;
733 $grade_item->itemname = $modinstance->name;
734 $grade_item->idnumber = $modinstance->cmidnumber;
736 $grade_item->update();
738 return $grade_item;
741 // create new one
742 $params = array('courseid' =>$modinstance->course,
743 'itemtype' =>'mod',
744 'itemmodule' =>$modinstance->modname,
745 'iteminstance'=>$modinstance->id,
746 'itemnumber' =>0,
747 'itemname' =>$modinstance->name,
748 'idnumber' =>$modinstance->cmidnumber);
750 if (is_null($grademax) and is_null($scaleid)) {
751 $params['gradetype'] = GRADE_TYPE_NONE;
753 } else if ($scaleid) {
754 $params['gradetype'] = GRADE_TYPE_SCALE;
755 $params['scaleid'] = $scaleid;
756 $grade_item->grademin = 1;
757 } else {
758 $params['gradetype'] = GRADE_TYPE_VALUE;
759 $params['grademax'] = $grademax;
760 $params['grademin'] = 0;
763 $grade_item = new grade_item($params);
764 $grade_item->insert();
766 return $grade_item;
770 * Remove all grade related course data - history is kept
771 * @param int $courseid
772 * @showfeedback boolean print feedback
774 function remove_course_grades($courseid, $showfeedback) {
775 $strdeleted = get_string('deleted');
777 $course_category = grade_category::fetch_course_category($courseid);
778 $course_category->delete('coursedelete');
779 if ($showfeedback) {
780 notify($strdeleted.' - '.get_string('grades', 'grades').', '.get_string('items', 'grades').', '.get_string('categories', 'grades'));
783 if ($outcomes = grade_outcome::fetch_all(array('courseid'=>$courseid))) {
784 foreach ($outcomes as $outcome) {
785 $outcome->delete('coursedelete');
788 delete_records('grade_outcomes_courses', 'courseid', $courseid);
789 if ($showfeedback) {
790 notify($strdeleted.' - '.get_string('outcomes', 'grades'));
793 if ($scales = grade_scale::fetch_all(array('courseid'=>$courseid))) {
794 foreach ($scales as $scale) {
795 $scale->delete('coursedelete');
798 if ($showfeedback) {
799 notify($strdeleted.' - '.get_string('scales'));
804 * Builds an array of percentages indexed by integers for the purpose of building a select drop-down element.
805 * @param int $steps The value between each level.
806 * @param string $order 'asc' for 0-100 and 'desc' for 100-0
807 * @param int $lowest The lowest value to include
808 * @param int $highest The highest value to include
810 function build_percentages_array($steps=1, $order='desc', $lowest=0, $highest=100) {
811 // TODO reject or implement
815 * Grading cron job
817 function grade_cron() {
818 global $CFG;
820 $now = time();
822 $sql = "SELECT i.*
823 FROM {$CFG->prefix}grade_items i
824 WHERE i.locked = 0 AND i.locktime > 0 AND i.locktime < $now AND EXISTS (
825 SELECT 'x' FROM {$CFG->prefix}grade_items c WHERE c.itemtype='course' AND c.needsupdate=0 AND c.courseid=i.courseid)";
827 // go through all courses that have proper final grades and lock them if needed
828 if ($rs = get_recordset_sql($sql)) {
829 if ($rs->RecordCount() > 0) {
830 while ($item = rs_fetch_next_record($rs)) {
831 $grade_item = new grade_item($item, false);
832 $grade_item->locked = $now;
833 $grade_item->update('locktime');
836 rs_close($rs);
839 $sql = "SELECT g.*
840 FROM {$CFG->prefix}grade_grades g, {$CFG->prefix}grade_items i
841 WHERE g.locked = 0 AND g.locktime > 0 AND g.locktime < $now AND g.itemid=i.id AND EXISTS (
842 SELECT 'x' FROM {$CFG->prefix}grade_items c WHERE c.itemtype='course' AND c.needsupdate=0 AND c.courseid=i.courseid)";
844 // go through all courses that have proper final grades and lock them if needed
845 if ($rs = get_recordset_sql($sql)) {
846 if ($rs->RecordCount() > 0) {
847 while ($grade = rs_fetch_next_record($rs)) {
848 $grade_grade = new grade_grade($grade, false);
849 $grade_grade->locked = $now;
850 $grade_grade->update('locktime');
853 rs_close($rs);