2007-04-23 09:00:32 +00:00
|
|
|
<?php // $Id$
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// //
|
|
|
|
// NOTICE OF COPYRIGHT //
|
|
|
|
// //
|
|
|
|
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
|
|
|
|
// http://moodle.com //
|
|
|
|
// //
|
|
|
|
// Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details: //
|
|
|
|
// //
|
|
|
|
// http://www.gnu.org/copyleft/gpl.html //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
2007-04-24 05:41:28 +00:00
|
|
|
* Library of functions for gradebook
|
2007-04-23 09:00:32 +00:00
|
|
|
*
|
|
|
|
* @author Moodle HQ developers
|
|
|
|
* @version $Id$
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
* @package moodlecore
|
|
|
|
*/
|
|
|
|
|
2007-06-18 13:43:40 +00:00
|
|
|
define('GRADE_AGGREGATE_MEAN_ALL', 0);
|
2007-04-24 05:41:28 +00:00
|
|
|
define('GRADE_AGGREGATE_MEDIAN', 1);
|
2007-06-18 13:43:40 +00:00
|
|
|
define('GRADE_AGGREGATE_MEAN_GRADED', 2);
|
|
|
|
define('GRADE_AGGREGATE_MIN', 3);
|
|
|
|
define('GRADE_AGGREGATE_MAX', 4);
|
2007-06-18 15:29:20 +00:00
|
|
|
define('GRADE_AGGREGATE_MODE', 5);
|
2007-06-05 22:58:37 +00:00
|
|
|
|
2007-05-03 07:10:22 +00:00
|
|
|
define('GRADE_CHILDTYPE_ITEM', 0);
|
|
|
|
define('GRADE_CHILDTYPE_CAT', 1);
|
2007-06-05 22:58:37 +00:00
|
|
|
|
2007-05-03 07:10:22 +00:00
|
|
|
define('GRADE_ITEM', 0); // Used to compare class names with CHILDTYPE values
|
|
|
|
define('GRADE_CATEGORY', 1); // Used to compare class names with CHILDTYPE values
|
2007-06-05 22:58:37 +00:00
|
|
|
|
2007-06-05 08:50:24 +00:00
|
|
|
define('GRADE_TYPE_NONE', 0);
|
|
|
|
define('GRADE_TYPE_VALUE', 1);
|
|
|
|
define('GRADE_TYPE_SCALE', 2);
|
|
|
|
define('GRADE_TYPE_TEXT', 3);
|
2007-04-24 05:41:28 +00:00
|
|
|
|
2007-06-05 22:58:37 +00:00
|
|
|
define('GRADE_UPDATE_OK', 0);
|
|
|
|
define('GRADE_UPDATE_FAILED', 1);
|
|
|
|
define('GRADE_UPDATE_MULTIPLE', 2);
|
|
|
|
define('GRADE_UPDATE_ITEM_DELETED', 3);
|
2007-06-09 14:16:20 +00:00
|
|
|
define('GRADE_UPDATE_ITEM_LOCKED', 4);
|
2007-06-05 22:58:37 +00:00
|
|
|
|
|
|
|
|
2007-04-27 07:28:41 +00:00
|
|
|
require_once($CFG->libdir . '/grade/grade_category.php');
|
|
|
|
require_once($CFG->libdir . '/grade/grade_item.php');
|
|
|
|
require_once($CFG->libdir . '/grade/grade_calculation.php');
|
2007-06-20 23:06:29 +00:00
|
|
|
require_once($CFG->libdir . '/grade/grade_grades.php');
|
2007-05-01 05:45:54 +00:00
|
|
|
require_once($CFG->libdir . '/grade/grade_scale.php');
|
2007-05-03 08:51:48 +00:00
|
|
|
require_once($CFG->libdir . '/grade/grade_outcome.php');
|
2007-05-04 07:40:51 +00:00
|
|
|
require_once($CFG->libdir . '/grade/grade_history.php');
|
|
|
|
require_once($CFG->libdir . '/grade/grade_grades_text.php');
|
2007-05-17 02:22:32 +00:00
|
|
|
require_once($CFG->libdir . '/grade/grade_tree.php');
|
2007-04-26 08:29:30 +00:00
|
|
|
|
2007-06-05 22:58:37 +00:00
|
|
|
/***** PUBLIC GRADE API *****/
|
|
|
|
|
2007-06-07 08:34:04 +00:00
|
|
|
/**
|
|
|
|
* Submit new or update grade; update/create grade_item definition. Grade must have userid specified,
|
2007-06-20 23:06:29 +00:00
|
|
|
* rawgrade and feedback with format are optional. rawgrade NULL means 'Not graded', missing property
|
2007-06-07 08:34:04 +00:00
|
|
|
* or key means do not change existing.
|
2007-06-09 14:16:20 +00:00
|
|
|
*
|
2007-06-07 08:34:04 +00:00
|
|
|
* Only following grade item properties can be changed 'itemname', 'idnumber', 'gradetype', 'grademax',
|
2007-06-11 10:55:31 +00:00
|
|
|
* 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted'.
|
2007-06-09 14:16:20 +00:00
|
|
|
*
|
2007-06-07 08:34:04 +00:00
|
|
|
* @param string $source source of the grade such as 'mod/assignment', often used to prevent infinite loops when processing grade_updated events
|
|
|
|
* @param int $courseid id of course
|
|
|
|
* @param string $itemtype type of grade item - mod, block, gradecategory, calculated
|
|
|
|
* @param string $itemmodule more specific then $itemtype - assignment, forum, etc.; maybe NULL for some item types
|
|
|
|
* @param int $iteminstance instance it of graded subject
|
|
|
|
* @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
|
|
|
|
* @param mixed $grades grade (object, array) or several grades (arrays of arrays or objects), NULL if updating rgade_item definition only\
|
|
|
|
* @param mixed $itemdetails object or array describing the grading item, NULL if no change
|
|
|
|
*/
|
2007-06-06 23:04:24 +00:00
|
|
|
function grade_update($source, $courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $grades=NULL, $itemdetails=NULL) {
|
2007-06-05 22:58:37 +00:00
|
|
|
|
2007-06-07 08:34:04 +00:00
|
|
|
// only following grade_item properties can be changed in this function
|
2007-06-11 10:55:31 +00:00
|
|
|
$allowed = array('itemname', 'idnumber', 'gradetype', 'grademax', 'grademin', 'scaleid', 'multfactor', 'plusfactor', 'deleted');
|
2007-06-05 22:58:37 +00:00
|
|
|
|
|
|
|
if (is_null($courseid) or is_null($itemtype)) {
|
|
|
|
debugging('Missing courseid or itemtype');
|
|
|
|
return GRADE_UPDATE_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false);
|
|
|
|
if (!$grade_items = $grade_item->fetch_all_using_this()) {
|
|
|
|
// create a new one
|
|
|
|
$grade_item = false;
|
|
|
|
|
|
|
|
} else if (count($grade_items) == 1){
|
|
|
|
$grade_item = reset($grade_items);
|
|
|
|
unset($grade_items); //release memory
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2007-06-10 20:34:07 +00:00
|
|
|
debugging('Found more than one grade item');
|
2007-06-05 22:58:37 +00:00
|
|
|
return GRADE_UPDATE_MULTIPLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create or update the grade_item if needed
|
|
|
|
if (!$grade_item) {
|
|
|
|
$params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
|
|
|
|
if ($itemdetails) {
|
|
|
|
$itemdetails = (array)$itemdetails;
|
|
|
|
foreach ($itemdetails as $k=>$v) {
|
|
|
|
if (!in_array($k, $allowed)) {
|
|
|
|
// ignore it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($k == 'gradetype' and $v == GRADE_TYPE_NONE) {
|
|
|
|
// no grade item needed!
|
|
|
|
return GRADE_UPDATE_OK;
|
|
|
|
}
|
|
|
|
$params[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
2007-06-12 20:19:38 +00:00
|
|
|
$grade_item = new grade_item($params);
|
|
|
|
$grade_item->insert();
|
2007-06-05 22:58:37 +00:00
|
|
|
|
|
|
|
} else {
|
2007-06-22 08:57:39 +00:00
|
|
|
if ($grade_item->is_locked()) {
|
2007-06-05 22:58:37 +00:00
|
|
|
debugging('Grading item is locked!');
|
2007-06-09 14:16:20 +00:00
|
|
|
return GRADE_UPDATE_ITEM_LOCKED;
|
2007-06-05 22:58:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($itemdetails) {
|
|
|
|
$itemdetails = (array)$itemdetails;
|
|
|
|
$update = false;
|
|
|
|
foreach ($itemdetails as $k=>$v) {
|
|
|
|
if (!in_array($k, $allowed)) {
|
|
|
|
// ignore it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($grade_item->{$k} != $v) {
|
|
|
|
$grade_item->{$k} = $v;
|
|
|
|
$update = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($update) {
|
|
|
|
$grade_item->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Some extra checks
|
|
|
|
// do we use grading?
|
|
|
|
if ($grade_item->gradetype == GRADE_TYPE_NONE) {
|
|
|
|
return GRADE_UPDATE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no grade submitted
|
2007-06-06 23:04:24 +00:00
|
|
|
if (empty($grades)) {
|
2007-06-05 22:58:37 +00:00
|
|
|
return GRADE_UPDATE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no grading in deleted items
|
|
|
|
if ($grade_item->deleted) {
|
|
|
|
debugging('Grade item was already deleted!');
|
|
|
|
return GRADE_UPDATE_ITEM_DELETED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Finally start processing of grades
|
2007-06-06 23:04:24 +00:00
|
|
|
if (is_object($grades)) {
|
|
|
|
$grades = array($grades);
|
2007-06-05 22:58:37 +00:00
|
|
|
} else {
|
2007-06-06 23:04:24 +00:00
|
|
|
if (array_key_exists('userid', $grades)) {
|
|
|
|
$grades = array($grades);
|
2007-06-05 22:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-09 14:16:20 +00:00
|
|
|
$failed = false;
|
2007-06-05 22:58:37 +00:00
|
|
|
foreach ($grades as $grade) {
|
|
|
|
$grade = (array)$grade;
|
|
|
|
if (empty($grade['userid'])) {
|
2007-06-09 14:16:20 +00:00
|
|
|
$failed = true;
|
|
|
|
debugging('Invalid userid in grade submitted');
|
|
|
|
continue;
|
2007-06-20 23:06:29 +00:00
|
|
|
} else {
|
|
|
|
$userid = $grade['userid'];
|
2007-06-05 22:58:37 +00:00
|
|
|
}
|
|
|
|
|
2007-06-22 08:57:39 +00:00
|
|
|
$rawgrade = false;
|
2007-06-20 23:06:29 +00:00
|
|
|
$feedback = false;
|
|
|
|
$feedbackformat = FORMAT_MOODLE;
|
|
|
|
|
|
|
|
if (array_key_exists('rawgrade', $grade)) {
|
|
|
|
$rawgrade = $grade['rawgrade'];
|
|
|
|
}
|
2007-06-05 22:58:37 +00:00
|
|
|
|
2007-06-09 14:16:20 +00:00
|
|
|
if (array_key_exists('feedback', $grade)) {
|
2007-06-20 23:06:29 +00:00
|
|
|
$feedback = $grade['feedback'];
|
2007-06-05 22:58:37 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 23:06:29 +00:00
|
|
|
if (array_key_exists('feedbackformat', $grade)) {
|
|
|
|
$feedbackformat = $grade['feedbackformat'];
|
2007-06-05 22:58:37 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 23:06:29 +00:00
|
|
|
// update or insert the grade
|
2007-06-22 08:57:39 +00:00
|
|
|
$grade = $grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat);
|
2007-06-20 23:06:29 +00:00
|
|
|
|
|
|
|
if (!$grade) {
|
2007-06-09 14:16:20 +00:00
|
|
|
$failed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-06 23:04:24 +00:00
|
|
|
// trigger grade_updated event notification
|
2007-06-05 22:58:37 +00:00
|
|
|
$eventdata = new object();
|
2007-06-20 23:06:29 +00:00
|
|
|
|
2007-06-09 14:16:20 +00:00
|
|
|
$eventdata->source = $source;
|
|
|
|
$eventdata->itemid = $grade_item->id;
|
|
|
|
$eventdata->courseid = $grade_item->courseid;
|
|
|
|
$eventdata->itemtype = $grade_item->itemtype;
|
|
|
|
$eventdata->itemmodule = $grade_item->itemmodule;
|
|
|
|
$eventdata->iteminstance = $grade_item->iteminstance;
|
|
|
|
$eventdata->itemnumber = $grade_item->itemnumber;
|
|
|
|
$eventdata->idnumber = $grade_item->idnumber;
|
2007-06-20 23:06:29 +00:00
|
|
|
$eventdata->userid = $grade->userid;
|
|
|
|
$eventdata->rawgrade = $grade->rawgrade;
|
|
|
|
|
|
|
|
// load existing text annotation
|
|
|
|
if ($grade_text = $grade->load_text()) {
|
|
|
|
$eventdata->feedback = $grade_text->feedback;
|
|
|
|
$eventdata->feedbackformat = $grade_text->feedbackformat;
|
|
|
|
$eventdata->information = $grade_text->information;
|
|
|
|
$eventdata->informationformat = $grade_text->informationformat;
|
|
|
|
}
|
2007-06-09 14:16:20 +00:00
|
|
|
|
2007-06-05 22:58:37 +00:00
|
|
|
events_trigger('grade_updated', $eventdata);
|
|
|
|
}
|
|
|
|
|
2007-06-09 14:16:20 +00:00
|
|
|
if (!$failed) {
|
|
|
|
return GRADE_UPDATE_OK;
|
|
|
|
} else {
|
|
|
|
return GRADE_UPDATE_FAILED;
|
|
|
|
}
|
2007-06-05 22:58:37 +00:00
|
|
|
}
|
|
|
|
|
2007-06-06 23:04:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
|
|
|
|
* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
|
|
|
|
* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
|
|
|
|
* If no $userid is given, the method will always return the grade_item's locked state.
|
|
|
|
* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
|
|
|
|
* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
|
|
|
|
* return the locked state of the specific grade.
|
|
|
|
*
|
2007-06-10 20:34:07 +00:00
|
|
|
* @param int $courseid id of course
|
2007-06-06 23:04:24 +00:00
|
|
|
* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
|
|
|
|
* @param string $itemmodule 'forum, 'quiz', 'csv' etc
|
|
|
|
* @param int $iteminstance id of the item module
|
2007-06-10 20:34:07 +00:00
|
|
|
* @param int $itemnumber most probably 0, modules can use other numbers when having more than one grades for each user
|
|
|
|
* @param int $userid ID of the graded user
|
2007-06-06 23:04:24 +00:00
|
|
|
* @return boolean Whether the grade is locked or not
|
|
|
|
*/
|
2007-06-10 20:34:07 +00:00
|
|
|
function grade_is_locked($courseid, $itemtype, $itemmodule, $iteminstance, $itemnumber, $userid=NULL) {
|
2007-06-06 23:04:24 +00:00
|
|
|
|
2007-06-10 20:34:07 +00:00
|
|
|
$grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'), false);
|
|
|
|
if (!$grade_items = $grade_item->fetch_all_using_this()) {
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else if (count($grade_items) == 1){
|
|
|
|
$grade_item = reset($grade_items);
|
|
|
|
return $grade_item->is_locked($userid);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
debugging('Found more than one grade item');
|
|
|
|
foreach ($grade_items as $grade_item) {
|
|
|
|
if ($grade_item->is_locked($userid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2007-06-06 23:04:24 +00:00
|
|
|
|
2007-06-05 22:58:37 +00:00
|
|
|
/***** END OF PUBLIC API *****/
|
|
|
|
|
2007-06-10 20:34:07 +00:00
|
|
|
|
2007-04-23 09:00:32 +00:00
|
|
|
/**
|
2007-06-05 22:58:37 +00:00
|
|
|
* Extracts from the gradebook all the grade items attached to the calling object.
|
|
|
|
* For example, an assignment may want to retrieve all the grade_items for itself,
|
2007-04-23 09:00:32 +00:00
|
|
|
* and get three outcome scales in return. This will affect the grading interface.
|
|
|
|
*
|
|
|
|
* Note: Each parameter refines the search. So if you only give the courseid,
|
|
|
|
* all the grade_items for this course will be returned. If you add the
|
|
|
|
* itemtype 'mod', all grade_items for this courseif AND for the 'mod'
|
|
|
|
* type will be returned, etc...
|
2007-06-05 22:58:37 +00:00
|
|
|
*
|
2007-04-24 05:41:28 +00:00
|
|
|
* @param int $courseid The id of the course to which the grade items belong
|
2007-04-23 09:00:32 +00:00
|
|
|
* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
|
|
|
|
* @param string $itemmodule 'forum, 'quiz', 'csv' etc
|
|
|
|
* @param int $iteminstance id of the item module
|
|
|
|
* @param int $itemnumber Can be used to distinguish multiple grades for an activity
|
2007-06-12 20:19:38 +00:00
|
|
|
* @param string $itemname The name of the grade item
|
2007-04-24 05:41:28 +00:00
|
|
|
* @param int $idnumber grade item Primary Key
|
|
|
|
* @return array An array of grade items
|
2007-04-23 09:00:32 +00:00
|
|
|
*/
|
2007-06-12 10:33:52 +00:00
|
|
|
function grade_get_items($courseid, $itemtype=NULL, $itemmodule=NULL, $iteminstance=NULL, $itemnumber=NULL, $itemname=NULL, $idnumber=NULL) {
|
2007-05-29 00:56:44 +00:00
|
|
|
$grade_item = new grade_item(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemname', 'itemnumber', 'idnumber'), false);
|
2007-04-27 07:28:41 +00:00
|
|
|
$grade_items = $grade_item->fetch_all_using_this();
|
2007-04-24 05:41:28 +00:00
|
|
|
return $grade_items;
|
2007-04-23 09:00:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-06-20 23:06:29 +00:00
|
|
|
* Updates all final grades in course.
|
2007-04-30 04:49:25 +00:00
|
|
|
*
|
|
|
|
* @param int $courseid
|
2007-06-18 13:43:40 +00:00
|
|
|
* @return boolean true if ok, array of errors if problems found
|
2007-04-30 04:49:25 +00:00
|
|
|
*/
|
2007-06-18 13:43:40 +00:00
|
|
|
function grade_update_final_grades($courseid) {
|
|
|
|
$errors = array();
|
2007-04-30 04:49:25 +00:00
|
|
|
$grade_item = new grade_item();
|
|
|
|
$grade_item->courseid = $courseid;
|
2007-06-18 13:43:40 +00:00
|
|
|
if (!$grade_items = $grade_item->fetch_all_using_this()) {
|
|
|
|
return true;
|
|
|
|
}
|
2007-06-05 22:58:37 +00:00
|
|
|
|
2007-06-18 13:43:40 +00:00
|
|
|
$needsupdate = false;
|
|
|
|
$calculated = false;
|
|
|
|
foreach ($grade_items as $gid=>$gitem) {
|
|
|
|
$grade_item =& $grade_items[$gid];
|
|
|
|
if ($grade_item->needsupdate) {
|
|
|
|
$needsupdate = true;
|
|
|
|
}
|
|
|
|
if ($grade_item->get_calculation()) {
|
|
|
|
$calculated = true;
|
|
|
|
}
|
|
|
|
}
|
2007-04-30 04:49:25 +00:00
|
|
|
|
2007-06-18 13:43:40 +00:00
|
|
|
// no update needed
|
|
|
|
if (!$needsupdate) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the easy way
|
|
|
|
if (!$calculated) {
|
|
|
|
foreach ($grade_items as $gid=>$gitem) {
|
|
|
|
$grade_item =& $grade_items[$gid];
|
|
|
|
if ($grade_item->needsupdate) {
|
2007-06-20 23:06:29 +00:00
|
|
|
$result = $grade_item->update_final_grades();
|
2007-06-18 13:43:40 +00:00
|
|
|
if ($result !== true) {
|
|
|
|
$errors = array_merge($errors, $result);
|
|
|
|
}
|
2007-04-30 04:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-18 13:43:40 +00:00
|
|
|
|
|
|
|
if (count($errors) == 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $errors;
|
|
|
|
}
|
2007-04-30 04:49:25 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 13:43:40 +00:00
|
|
|
// now the hard way with calculated grade_items or categories
|
|
|
|
$finalitems = array();
|
|
|
|
$finalids = array();
|
|
|
|
while (count($grade_items) > 0) {
|
|
|
|
$count = 0;
|
|
|
|
foreach ($grade_items as $gid=>$gitem) {
|
|
|
|
$grade_item =& $grade_items[$gid];
|
|
|
|
if (!$grade_item->needsupdate and $grade_item->itemtype!='category' and !$grade_item->get_calculation()) {
|
|
|
|
$finalitems[$gid] = $grade_item;
|
|
|
|
$finalids[] = $gid;
|
|
|
|
unset($grade_items[$gid]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dependson = $grade_item->dependson();
|
|
|
|
|
|
|
|
//are we dealing with category with no calculated items?
|
|
|
|
// we can not trust the needsupdate flag because category might contain calculated items
|
|
|
|
if ($grade_item->itemtype=='category' and !$grade_item->needsupdate) {
|
|
|
|
$forceupdate = false;
|
|
|
|
foreach ($dependson as $childid) {
|
|
|
|
if (in_array($childid, $finalids)) {
|
|
|
|
$child = $finalitems[$childid];
|
|
|
|
} else {
|
|
|
|
$child = $grade_items[$childid];
|
|
|
|
}
|
|
|
|
if ($child->itemtype == 'category' or $child->get_calculation()) {
|
|
|
|
$forceupdate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($forceupdate) {
|
2007-06-22 08:57:39 +00:00
|
|
|
$grade_item->force_regrading();
|
2007-06-18 13:43:40 +00:00
|
|
|
} else {
|
|
|
|
$finalitems[$gid] = $grade_item;
|
|
|
|
$finalids[] = $gid;
|
|
|
|
unset($grade_items[$gid]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//do we have all data for this item?
|
|
|
|
$doupdate = true;
|
|
|
|
foreach ($dependson as $did) {
|
|
|
|
if (!in_array($did, $finalids)) {
|
|
|
|
$doupdate = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//oki - let's update, calculate or aggregate :-)
|
|
|
|
if ($doupdate) {
|
2007-06-20 23:06:29 +00:00
|
|
|
$result = $grade_item->update_final_grades();
|
2007-06-18 13:43:40 +00:00
|
|
|
if ($result !== true) {
|
|
|
|
$errors = array_merge($errors, $result);
|
|
|
|
} else {
|
|
|
|
$finalitems[$gid] = $grade_item;
|
|
|
|
$finalids[] = $gid;
|
|
|
|
unset($grade_items[$gid]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($count == 0) {
|
|
|
|
foreach($grade_items as $grade_item) {
|
|
|
|
$errors[] = 'Probably circular reference in grade_item id:'.$grade_item->id; // TODO: localize
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($errors) == 0) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return $errors;
|
|
|
|
}
|
2007-04-30 04:49:25 +00:00
|
|
|
}
|
2007-05-04 02:15:46 +00:00
|
|
|
|
2007-05-29 00:56:44 +00:00
|
|
|
/**
|
2007-06-12 10:33:52 +00:00
|
|
|
* For backwards compatibility with old third-party modules, this function can
|
|
|
|
* be used to import all grades from activities with legacy grading.
|
2007-05-04 02:15:46 +00:00
|
|
|
*/
|
2007-05-29 00:56:44 +00:00
|
|
|
function grade_grab_legacy_grades() {
|
2007-06-05 22:58:37 +00:00
|
|
|
|
2007-06-20 23:06:29 +00:00
|
|
|
global $CFG;
|
2007-05-04 02:15:46 +00:00
|
|
|
|
|
|
|
if (!$mods = get_list_of_plugins('mod') ) {
|
|
|
|
error('No modules installed!');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($mods as $mod) {
|
|
|
|
|
|
|
|
if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
if (!$module = get_record('modules', 'name', $mod)) {
|
|
|
|
//not installed
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$module->visible) {
|
|
|
|
//disabled module
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fullmod = $CFG->dirroot.'/mod/'.$mod;
|
2007-05-04 02:15:46 +00:00
|
|
|
|
|
|
|
// include the module lib once
|
|
|
|
if (file_exists($fullmod.'/lib.php')) {
|
|
|
|
include_once($fullmod.'/lib.php');
|
2007-05-29 00:56:44 +00:00
|
|
|
// look for modname_grades() function - old gradebook pulling function
|
|
|
|
// if present sync the grades with new grading system
|
2007-05-04 02:15:46 +00:00
|
|
|
$gradefunc = $mod.'_grades';
|
2007-05-29 00:56:44 +00:00
|
|
|
if (function_exists($gradefunc)) {
|
|
|
|
|
|
|
|
// get all instance of the activity
|
2007-06-12 10:33:52 +00:00
|
|
|
$sql = "SELECT a.*, cm.idnumber as cmidnumber, m.name as modname
|
|
|
|
FROM {$CFG->prefix}$mod a, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
|
|
|
|
WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id";
|
2007-05-29 00:56:44 +00:00
|
|
|
|
|
|
|
if ($modinstances = get_records_sql($sql)) {
|
2007-05-04 02:15:46 +00:00
|
|
|
foreach ($modinstances as $modinstance) {
|
2007-06-12 10:33:52 +00:00
|
|
|
grade_update_mod_grades($modinstance);
|
2007-05-04 02:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-20 23:06:29 +00:00
|
|
|
/**
|
|
|
|
* For testing purposes mainly, reloads grades from all non legacy modules into gradebook.
|
|
|
|
*/
|
|
|
|
function grade_grab_grades() {
|
|
|
|
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
if (!$mods = get_list_of_plugins('mod') ) {
|
|
|
|
error('No modules installed!');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($mods as $mod) {
|
|
|
|
|
|
|
|
if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$module = get_record('modules', 'name', $mod)) {
|
|
|
|
//not installed
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$module->visible) {
|
|
|
|
//disabled module
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fullmod = $CFG->dirroot.'/mod/'.$mod;
|
|
|
|
|
|
|
|
// include the module lib once
|
|
|
|
if (file_exists($fullmod.'/lib.php')) {
|
|
|
|
include_once($fullmod.'/lib.php');
|
|
|
|
// look for modname_grades() function - old gradebook pulling function
|
|
|
|
// if present sync the grades with new grading system
|
|
|
|
$gradefunc = $mod.'_update_grades';
|
|
|
|
if (function_exists($gradefunc)) {
|
|
|
|
$gradefunc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
/**
|
|
|
|
* Force full update of module grades in central gradebook - works for both legacy and converted activities.
|
|
|
|
* @param object $modinstance object with extra cmidnumber and modname property
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
|
|
|
function grade_update_mod_grades($modinstance) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$fullmod = $CFG->dirroot.'/mod/'.$modinstance->modname;
|
|
|
|
if (!file_exists($fullmod.'/lib.php')) {
|
|
|
|
debugging('missing lib.php file in module');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
include_once($fullmod.'/lib.php');
|
|
|
|
|
|
|
|
// does it use legacy grading?
|
|
|
|
$gradefunc = $modinstance->modname.'_grades';
|
|
|
|
$updategradesfunc = $modinstance->modname.'_update_grades';
|
|
|
|
$updateitemfunc = $modinstance->modname.'_grade_item_update';
|
|
|
|
|
|
|
|
if (function_exists($gradefunc)) {
|
|
|
|
if ($oldgrades = $gradefunc($modinstance->id)) {
|
|
|
|
|
|
|
|
$grademax = $oldgrades->maxgrade;
|
|
|
|
$scaleid = NULL;
|
|
|
|
if (!is_numeric($grademax)) {
|
|
|
|
// scale name is provided as a string, try to find it
|
|
|
|
if (!$scale = get_record('scale', 'name', $grademax)) {
|
|
|
|
debugging('Incorrect scale name! name:'.$grademax);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$scaleid = $scale->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$grade_item = grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)) {
|
|
|
|
debugging('Can not get/create legacy grade item!');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grades = array();
|
|
|
|
foreach ($oldgrades->grades as $userid=>$usergrade) {
|
|
|
|
$grade = new object();
|
|
|
|
$grade->userid = $userid;
|
|
|
|
|
|
|
|
if ($usergrade == '-') {
|
|
|
|
// no grade
|
2007-06-20 23:06:29 +00:00
|
|
|
$grade->rawgrade = null;
|
2007-06-12 10:33:52 +00:00
|
|
|
|
|
|
|
} else if ($scaleid) {
|
|
|
|
// scale in use, words used
|
|
|
|
$gradescale = explode(",", $scale->scale);
|
2007-06-20 23:06:29 +00:00
|
|
|
$grade->rawgrade = array_search($usergrade, $gradescale) + 1;
|
2007-06-12 10:33:52 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// good old numeric value
|
2007-06-20 23:06:29 +00:00
|
|
|
$grade->rawgrade = $usergrade;
|
2007-06-12 10:33:52 +00:00
|
|
|
}
|
|
|
|
$grades[] = $grade;
|
|
|
|
}
|
|
|
|
|
|
|
|
grade_update('legacygrab', $grade_item->courseid, $grade_item->itemtype, $grade_item->itemmodule,
|
|
|
|
$grade_item->iteminstance, $grade_item->itemnumber, $grades);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (function_exists($updategradesfunc) and function_exists($updateitemfunc)) {
|
|
|
|
//new grading supported, force updating of grades
|
|
|
|
$updateitemfunc($modinstance);
|
|
|
|
$updategradesfunc($modinstance);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// mudule does not support grading
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2007-05-29 00:56:44 +00:00
|
|
|
|
|
|
|
/**
|
2007-06-12 10:33:52 +00:00
|
|
|
* Get and update/create grade item for legacy modules.
|
2007-05-29 00:56:44 +00:00
|
|
|
*/
|
|
|
|
function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid) {
|
|
|
|
|
|
|
|
// does it already exist?
|
2007-06-12 10:33:52 +00:00
|
|
|
if ($grade_items = grade_get_items($modinstance->course, 'mod', $modinstance->modname, $modinstance->id, 0)) {
|
2007-05-29 00:56:44 +00:00
|
|
|
if (count($grade_items) > 1) {
|
2007-06-12 10:33:52 +00:00
|
|
|
debugging('Multiple legacy grade_items found.');
|
2007-05-29 00:56:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$grade_item = reset($grade_items);
|
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
if (is_null($grademax) and is_null($scaleid)) {
|
|
|
|
$grade_item->gradetype = GRADE_TYPE_NONE;
|
2007-05-29 00:56:44 +00:00
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
} else if ($scaleid) {
|
|
|
|
$grade_item->gradetype = GRADE_TYPE_SCALE;
|
|
|
|
$grade_item->scaleid = $scaleid;
|
2007-05-29 00:56:44 +00:00
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
} else {
|
|
|
|
$grade_item->gradetype = GRADE_TYPE_VALUE;
|
|
|
|
$grade_item->grademax = $grademax;
|
|
|
|
$grade_item->grademin = 0;
|
2007-05-29 00:56:44 +00:00
|
|
|
}
|
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
$grade_item->itemname = $modinstance->name;
|
|
|
|
$grade_item->idnumber = $modinstance->cmidnumber;
|
2007-05-29 00:56:44 +00:00
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
$grade_item->update();
|
2007-05-29 00:56:44 +00:00
|
|
|
|
|
|
|
return $grade_item;
|
|
|
|
}
|
2007-06-05 22:58:37 +00:00
|
|
|
|
2007-05-29 00:56:44 +00:00
|
|
|
// create new one
|
2007-06-12 10:33:52 +00:00
|
|
|
$params = array('courseid' =>$modinstance->course,
|
2007-05-29 00:56:44 +00:00
|
|
|
'itemtype' =>'mod',
|
|
|
|
'itemmodule' =>$modinstance->modname,
|
|
|
|
'iteminstance'=>$modinstance->id,
|
2007-06-12 10:33:52 +00:00
|
|
|
'itemnumber' =>0,
|
2007-05-29 00:56:44 +00:00
|
|
|
'itemname' =>$modinstance->name,
|
|
|
|
'idnumber' =>$modinstance->cmidnumber);
|
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
if (is_null($grademax) and is_null($scaleid)) {
|
|
|
|
$params['gradetype'] = GRADE_TYPE_NONE;
|
|
|
|
|
|
|
|
} else if ($scaleid) {
|
2007-06-05 22:58:37 +00:00
|
|
|
$params['gradetype'] = GRADE_TYPE_SCALE;
|
2007-05-29 00:56:44 +00:00
|
|
|
$params['scaleid'] = $scaleid;
|
|
|
|
|
|
|
|
} else {
|
2007-06-05 22:58:37 +00:00
|
|
|
$params['gradetype'] = GRADE_TYPE_VALUE;
|
2007-05-29 00:56:44 +00:00
|
|
|
$params['grademax'] = $grademax;
|
|
|
|
$params['grademin'] = 0;
|
|
|
|
}
|
|
|
|
|
2007-06-12 20:19:38 +00:00
|
|
|
$grade_item = new grade_item($params);
|
|
|
|
$grade_item->insert();
|
2007-05-29 00:56:44 +00:00
|
|
|
|
2007-06-12 20:19:38 +00:00
|
|
|
return $grade_item;
|
2007-05-29 00:56:44 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 13:43:40 +00:00
|
|
|
/**
|
|
|
|
* This function is used to migrade old date and settings from old gradebook into new grading system.
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* - category weight not used - we would have to create extra top course grade calculated category
|
|
|
|
* - exta_credit item flag not used - does not fit all our aggregation types, could be used in SUM only
|
|
|
|
*/
|
|
|
|
function grade_oldgradebook_upgrade($courseid) {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
$categories = array();
|
|
|
|
if ($oldcats = get_records('grade_category', 'courseid', $courseid)) {
|
|
|
|
foreach ($oldcats as $oldcat) {
|
|
|
|
$newcat = new grade_category(array('courseid'=>$courseid, 'fullname'=>$oldcat->name));
|
|
|
|
$newcat->droplow = $oldcat->drop_x_lowest;
|
|
|
|
$newcat->aggregation = GRADE_AGGREGATE_MEAN_GRADED;
|
|
|
|
|
|
|
|
if (empty($newcat->id)) {
|
|
|
|
$newcat->insert();
|
|
|
|
} else {
|
|
|
|
$newcat->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
$categories[$oldcat->id] = $newcat;
|
|
|
|
|
|
|
|
$catitem = $newcat->get_grade_item();
|
|
|
|
$catitem->gradetype = GRADE_TYPE_VALUE;
|
|
|
|
$catitem->plusfactor = $oldcat->bonus_points;
|
|
|
|
$catitem->hidden = $oldcat->hidden;
|
|
|
|
$catitem->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all grade items with mod details
|
|
|
|
$sql = "SELECT gi.*, cm.idnumber as cmidnumber, m.name as modname
|
|
|
|
FROM {$CFG->prefix}grade_item gi, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
|
|
|
|
WHERE gi.courseid=$courseid AND m.id=gi.modid AND cm.instance=gi.cminstance
|
|
|
|
ORDER BY gi.sortorder ASC";
|
|
|
|
|
|
|
|
if ($olditems = get_records_sql($sql)) {
|
|
|
|
foreach ($olditems as $olditem) {
|
|
|
|
$newitem = new grade_item(array('courseid'=>$olditem->courseid, 'itemtype'=>'mod', 'itemmodule'=>$olditem->modname, 'iteminstance'=>$olditem->cminstance, 'itemnumber'=>0));
|
|
|
|
if (!empty($olditem->category)) {
|
|
|
|
// we do this low level stuff to get some speedup during upgrade
|
|
|
|
$newitem->set_parent_id($categories[$olditem->category]->id);
|
|
|
|
}
|
|
|
|
$newitem->gradetype = GRADE_TYPE_NONE;
|
|
|
|
$newitem->multfactor = $olditem->scale_grade;
|
|
|
|
if (empty($newitem->id)) {
|
|
|
|
$newitem->insert();
|
|
|
|
} else {
|
|
|
|
$newitem->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-14 09:38:41 +00:00
|
|
|
|
2007-04-26 08:29:30 +00:00
|
|
|
?>
|