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-07-21 10:09:57 +00:00
|
|
|
// category aggregation types
|
2007-06-18 13:43:40 +00:00
|
|
|
define('GRADE_AGGREGATE_MEAN_ALL', 0);
|
2007-07-18 19:29:32 +00:00
|
|
|
define('GRADE_AGGREGATE_MEAN_GRADED', 1);
|
2007-07-21 10:09:57 +00:00
|
|
|
define('GRADE_AGGREGATE_MEDIAN_ALL', 2);
|
|
|
|
define('GRADE_AGGREGATE_MEDIAN_GRADED', 3);
|
|
|
|
define('GRADE_AGGREGATE_MIN_ALL', 4);
|
|
|
|
define('GRADE_AGGREGATE_MIN_GRADED', 5);
|
|
|
|
define('GRADE_AGGREGATE_MAX_ALL', 6);
|
|
|
|
define('GRADE_AGGREGATE_MAX_GRADED', 7);
|
|
|
|
define('GRADE_AGGREGATE_MODE_ALL', 8);
|
|
|
|
define('GRADE_AGGREGATE_MODE_GRADED', 9);
|
|
|
|
define('GRADE_AGGREGATE_WEIGHTED_MEAN_ALL', 10);
|
|
|
|
define('GRADE_AGGREGATE_WEIGHTED_MEAN_GRADED', 11);
|
|
|
|
define('GRADE_AGGREGATE_EXTRACREDIT_MEAN_ALL', 12);
|
|
|
|
define('GRADE_AGGREGATE_EXTRACREDIT_MEAN_GRADED', 13);
|
|
|
|
|
|
|
|
// grade types
|
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-07-21 10:09:57 +00:00
|
|
|
// grade_update() return status
|
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-07-06 12:49:28 +00:00
|
|
|
// Grate teables history tracking actions
|
|
|
|
define('GRADE_HISTORY_INSERT', 1);
|
|
|
|
define('GRADE_HISTORY_UPDATE', 2);
|
|
|
|
define('GRADE_HISTORY_DELETE', 3);
|
|
|
|
|
2007-07-16 19:46:54 +00:00
|
|
|
define('GRADE_REPORT_AGGREGATION_POSITION_LEFT', 0);
|
|
|
|
define('GRADE_REPORT_AGGREGATION_POSITION_RIGHT', 1);
|
|
|
|
define('GRADE_REPORT_AGGREGATION_VIEW_FULL', 0);
|
|
|
|
define('GRADE_REPORT_AGGREGATION_VIEW_COMPACT', 1);
|
2007-07-17 19:04:50 +00:00
|
|
|
define('GRADE_REPORT_GRADE_DISPLAY_TYPE_REAL', 0);
|
2007-07-16 19:46:54 +00:00
|
|
|
define('GRADE_REPORT_GRADE_DISPLAY_TYPE_PERCENTAGE', 1);
|
2007-07-17 19:04:50 +00:00
|
|
|
define('GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER', 2);
|
2007-07-17 18:46:21 +00:00
|
|
|
define('GRADE_REPORT_PREFERENCE_DEFAULT', 'default');
|
|
|
|
define('GRADE_REPORT_PREFERENCE_INHERIT', 'inherit');
|
2007-07-18 11:34:55 +00:00
|
|
|
define('GRADE_REPORT_PREFERENCE_UNUSED', -1);
|
2007-07-13 07:05:04 +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');
|
2007-07-19 08:15:19 +00:00
|
|
|
require_once($CFG->libdir . '/grade/grade_grade.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-07-19 08:15:19 +00:00
|
|
|
require_once($CFG->libdir . '/grade/grade_grade_text.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
|
2007-07-31 22:56:29 +00:00
|
|
|
* @param string $itemtype type of grade item - mod, block
|
2007-06-07 08:34:04 +00:00
|
|
|
* @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-07-06 12:49:28 +00:00
|
|
|
global $USER;
|
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
|
|
|
|
2007-06-25 07:56:25 +00:00
|
|
|
// grade item identification
|
|
|
|
$params = compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber');
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-06-25 07:56:25 +00:00
|
|
|
if (!$grade_items = grade_item::fetch_all($params)) {
|
2007-06-05 22:58:37 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2007-07-06 12:49:28 +00:00
|
|
|
if (!empty($itemdetails['deleted'])) {
|
|
|
|
if ($grade_item) {
|
|
|
|
if ($grade_item->delete($source)) {
|
|
|
|
return GRADE_UPDATE_OK;
|
|
|
|
} else {
|
|
|
|
return GRADE_UPDATE_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GRADE_UPDATE_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-05 22:58:37 +00:00
|
|
|
/// Create or update the grade_item if needed
|
|
|
|
if (!$grade_item) {
|
|
|
|
if ($itemdetails) {
|
|
|
|
$itemdetails = (array)$itemdetails;
|
2007-06-22 18:33:32 +00:00
|
|
|
|
2007-06-23 16:51:09 +00:00
|
|
|
// grademin and grademax ignored when scale specified
|
2007-06-22 18:33:32 +00:00
|
|
|
if (array_key_exists('scaleid', $itemdetails)) {
|
|
|
|
if ($itemdetails['scaleid']) {
|
|
|
|
unset($itemdetails['grademin']);
|
|
|
|
unset($itemdetails['grademax']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-05 22:58:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2007-06-20 23:06:29 +00:00
|
|
|
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-07-06 12:49:28 +00:00
|
|
|
if (array_key_exists('usermodified', $grade)) {
|
|
|
|
$usermodified = $grade['usermodified'];
|
|
|
|
} else {
|
|
|
|
$usermodified = $USER->id;
|
|
|
|
}
|
|
|
|
|
2007-06-20 23:06:29 +00:00
|
|
|
// update or insert the grade
|
2007-07-06 12:49:28 +00:00
|
|
|
if (!$grade_item->update_raw_grade($userid, $rawgrade, $source, null, $feedback, $feedbackformat, $usermodified)) {
|
2007-06-09 14:16:20 +00:00
|
|
|
$failed = true;
|
|
|
|
}
|
2007-06-05 22:58:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
2007-07-31 22:56:29 +00:00
|
|
|
* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param int $courseid id of course
|
|
|
|
* @param string $itemtype 'mod', 'block'
|
|
|
|
* @param string $itemmodule 'forum, 'quiz', etc.
|
|
|
|
* @param int $iteminstance id of the item module
|
|
|
|
* @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
|
|
|
|
* @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-24 22:26:33 +00:00
|
|
|
if (!$grade_items = grade_item::fetch_all(compact('courseid', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber'))) {
|
2007-06-10 20:34:07 +00:00
|
|
|
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-07-31 22:56:29 +00:00
|
|
|
/**
|
|
|
|
* Returns list of outcomes used in course together with current outcomes for this user
|
|
|
|
* @param int $courseid id of course
|
|
|
|
* @param string $itemtype 'mod', 'block'
|
|
|
|
* @param string $itemmodule 'forum, 'quiz', etc.
|
|
|
|
* @param int $iteminstance id of the item module
|
|
|
|
* @param int $userid ID of the graded user
|
|
|
|
* @return array of outcome information objects (scaleid, name, grade and locked status) indexed with itemnumbers
|
|
|
|
*/
|
|
|
|
function grade_get_outcomes($courseid, $itemtype, $itemmodule, $iteminstance, $userid) {
|
|
|
|
$result = array();
|
|
|
|
if ($items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
|
|
|
|
foreach ($items as $item) {
|
|
|
|
if (empty($item->outcomeid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$outcome = grade_outcome::fetch(array('id'=>$item->outcomeid))) {
|
|
|
|
debugging('Incorect outcomeid found');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// prepare outcome info with user grade
|
|
|
|
$o = new object();
|
|
|
|
$o->scaleid = $outcome->scaleid;
|
|
|
|
$o->name = $outcome->fullname;
|
|
|
|
|
|
|
|
if ($grade = $item->get_grade($userid,false)) {
|
|
|
|
$o->grade = $grade->finalgrade;
|
|
|
|
$o->locked = $grade->is_locked();
|
|
|
|
} else {
|
|
|
|
$o->grade = null;
|
|
|
|
$o->locked = $item->is_locked();
|
|
|
|
}
|
|
|
|
$o->grade = intval($o->grade); // 0 means no grade, int for scales
|
|
|
|
|
|
|
|
$result[$item->itemnumber] = $o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates outcomes of user
|
|
|
|
* @param int $courseid id of course
|
|
|
|
* @param string $itemtype 'mod', 'block'
|
|
|
|
* @param string $itemmodule 'forum, 'quiz', etc.
|
|
|
|
* @param int $iteminstance id of the item module
|
|
|
|
* @param int $userid ID of the graded user
|
|
|
|
*/
|
|
|
|
function grade_update_outcomes($source, $courseid, $itemtype, $itemmodule, $iteminstance, $userid, $data) {
|
|
|
|
if ($items = grade_item::fetch_all(array('itemtype'=>$itemtype, 'itemmodule'=>$itemmodule, 'iteminstance'=>$iteminstance, 'courseid'=>$courseid))) {
|
|
|
|
foreach ($items as $item) {
|
|
|
|
if (!array_key_exists($item->itemnumber, $data)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$grade = $data[$item->itemnumber] < 1 ? null : $data[$item->itemnumber];
|
|
|
|
$item->update_final_grade($userid, $grade, $source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-05 22:58:37 +00:00
|
|
|
/***** END OF PUBLIC API *****/
|
|
|
|
|
2007-07-08 14:57:19 +00:00
|
|
|
function grade_force_full_regrading($courseid) {
|
|
|
|
set_field('grade_items', 'needsupdate', 1, 'courseid', $courseid);
|
|
|
|
}
|
2007-06-10 20:34:07 +00:00
|
|
|
|
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-07-08 14:57:19 +00:00
|
|
|
* @param int $userid if specified, try to do a quick regrading of grades of this user only
|
|
|
|
* @param object $updated_item the item in which
|
|
|
|
* @return boolean true if ok, array of errors if problems found (item id is used as key)
|
2007-04-30 04:49:25 +00:00
|
|
|
*/
|
2007-07-10 18:08:24 +00:00
|
|
|
function grade_regrade_final_grades($courseid, $userid=null, $updated_item=null) {
|
2007-06-18 13:43:40 +00:00
|
|
|
|
2007-07-29 13:22:50 +00:00
|
|
|
$course_item = grade_item::fetch_course_item($courseid);
|
2007-07-28 09:23:32 +00:00
|
|
|
|
2007-07-08 14:57:19 +00:00
|
|
|
if ($userid) {
|
|
|
|
// one raw grade updated for one user
|
|
|
|
if (empty($updated_item)) {
|
|
|
|
error("updated_item_id can not be null!");
|
|
|
|
}
|
|
|
|
if ($course_item->needsupdate) {
|
|
|
|
$updated_item->force_regrading();
|
|
|
|
return 'Can not do fast regrading after updating of raw grades';
|
2007-04-30 04:49:25 +00:00
|
|
|
}
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2007-07-08 14:57:19 +00:00
|
|
|
} else {
|
|
|
|
if (!$course_item->needsupdate) {
|
|
|
|
// nothing to do :-)
|
2007-06-18 13:43:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-04-30 04:49:25 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 14:57:19 +00:00
|
|
|
$grade_items = grade_item::fetch_all(array('courseid'=>$courseid));
|
|
|
|
$depends_on = array();
|
|
|
|
|
|
|
|
// first mark all category and calculated items as needing regrading
|
|
|
|
// this is slower, but 100% accurate - this function is called only when there is
|
|
|
|
// a change in grading setup, update of individual grade does not trigger this function
|
|
|
|
foreach ($grade_items as $gid=>$gitem) {
|
2007-07-13 07:51:10 +00:00
|
|
|
if (!empty($updated_item) and $updated_item->id == $gid) {
|
2007-07-08 14:57:19 +00:00
|
|
|
$grade_items[$gid]->needsupdate = 1;
|
|
|
|
|
2007-07-21 10:09:57 +00:00
|
|
|
} else if ($gitem->is_course_item() or $gitem->is_category_item() or $gitem->is_calculated()) {
|
2007-07-08 14:57:19 +00:00
|
|
|
$grade_items[$gid]->needsupdate = 1;
|
|
|
|
}
|
2007-06-22 18:33:32 +00:00
|
|
|
|
2007-07-08 14:57:19 +00:00
|
|
|
// construct depends_on lookup array
|
|
|
|
$depends_on[$gid] = $grade_items[$gid]->depends_on();
|
|
|
|
}
|
2007-06-22 18:33:32 +00:00
|
|
|
|
2007-07-19 17:36:36 +00:00
|
|
|
$errors = array();
|
2007-06-18 13:43:40 +00:00
|
|
|
$finalids = array();
|
2007-07-19 17:36:36 +00:00
|
|
|
$gids = array_keys($grade_items);
|
2007-07-21 10:09:57 +00:00
|
|
|
$failed = 0;
|
2007-07-19 17:36:36 +00:00
|
|
|
|
|
|
|
while (count($finalids) < count($gids)) { // work until all grades are final or error found
|
|
|
|
$count = 0;
|
|
|
|
foreach ($gids as $gid) {
|
|
|
|
if (in_array($gid, $finalids)) {
|
|
|
|
continue; // already final
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$grade_items[$gid]->needsupdate) {
|
|
|
|
$finalids[] = $gid; // we can make it final - does not need update
|
2007-06-18 13:43:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$doupdate = true;
|
2007-07-08 14:57:19 +00:00
|
|
|
foreach ($depends_on[$gid] as $did) {
|
2007-06-18 13:43:40 +00:00
|
|
|
if (!in_array($did, $finalids)) {
|
|
|
|
$doupdate = false;
|
2007-07-19 17:36:36 +00:00
|
|
|
continue; // this item depends on something that is not yet in finals array
|
2007-06-18 13:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//oki - let's update, calculate or aggregate :-)
|
|
|
|
if ($doupdate) {
|
2007-07-19 17:36:36 +00:00
|
|
|
$result = $grade_items[$gid]->regrade_final_grades($userid);
|
2007-07-08 14:57:19 +00:00
|
|
|
|
|
|
|
if ($result === true) {
|
2007-07-19 17:36:36 +00:00
|
|
|
$grade_items[$gid]->regrading_finished();
|
2007-07-08 14:57:19 +00:00
|
|
|
$count++;
|
2007-06-18 13:43:40 +00:00
|
|
|
$finalids[] = $gid;
|
2007-07-08 14:57:19 +00:00
|
|
|
} else {
|
2007-07-19 17:36:36 +00:00
|
|
|
$grade_items[$gid]->force_regrading();
|
2007-07-08 14:57:19 +00:00
|
|
|
$errors[$gid] = $result;
|
2007-06-18 13:43:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($count == 0) {
|
2007-07-21 10:09:57 +00:00
|
|
|
$failed++;
|
|
|
|
} else {
|
|
|
|
$failed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($failed > 1) {
|
2007-07-19 17:36:36 +00:00
|
|
|
foreach($gids as $gid) {
|
|
|
|
if (in_array($gid, $finalids)) {
|
|
|
|
continue; // this one is ok
|
|
|
|
}
|
|
|
|
$grade_items[$gid]->force_regrading();
|
|
|
|
$errors[$grade_items[$gid]->id] = 'Probably circular reference or broken calculation formula'; // TODO: localize
|
2007-06-18 13:43:40 +00:00
|
|
|
}
|
2007-07-19 17:36:36 +00:00
|
|
|
break; // oki, found error
|
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-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-07-01 19:45:13 +00:00
|
|
|
* @param int $courseid or null if all courses
|
2007-05-04 02:15:46 +00:00
|
|
|
*/
|
2007-07-01 19:45:13 +00:00
|
|
|
function grade_grab_legacy_grades($courseid=null) {
|
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!');
|
|
|
|
}
|
|
|
|
|
2007-07-01 19:45:13 +00:00
|
|
|
if ($courseid) {
|
|
|
|
$course_sql = " AND cm.course=$courseid";
|
|
|
|
} else {
|
|
|
|
$course_sql = "";
|
|
|
|
}
|
|
|
|
|
2007-05-04 02:15:46 +00:00
|
|
|
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
|
2007-07-01 19:45:13 +00:00
|
|
|
WHERE m.name='$mod' AND m.id=cm.module AND cm.instance=a.id $course_sql";
|
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-07-22 21:43:47 +00:00
|
|
|
if ($grade_items = grade_item::fetch_all(array('courseid'=>$modinstance->course, 'itemtype'=>'mod', 'itemmodule'=>$modinstance->modname, 'iteminstance'=>$modinstance->id, 'itemnumber'=>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-06-26 08:50:22 +00:00
|
|
|
$grade_item->grademin = 1;
|
2007-05-29 00:56:44 +00:00
|
|
|
|
2007-06-12 10:33:52 +00:00
|
|
|
} else {
|
2007-06-26 08:50:22 +00:00
|
|
|
$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;
|
2007-06-28 13:20:30 +00:00
|
|
|
$grade_item->grademin = 1;
|
2007-05-29 00:56:44 +00:00
|
|
|
} 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-05-14 09:38:41 +00:00
|
|
|
|
2007-07-18 11:34:55 +00:00
|
|
|
/**
|
|
|
|
* Builds an array of percentages indexed by integers for the purpose of building a select drop-down element.
|
|
|
|
* @param int $steps The value between each level.
|
|
|
|
* @param string $order 'asc' for 0-100 and 'desc' for 100-0
|
|
|
|
* @param int $lowest The lowest value to include
|
|
|
|
* @param int $highest The highest value to include
|
|
|
|
*/
|
|
|
|
function build_percentages_array($steps=1, $order='desc', $lowest=0, $highest=100) {
|
|
|
|
// TODO reject or implement
|
|
|
|
}
|
2007-07-22 21:43:47 +00:00
|
|
|
|
2007-04-26 08:29:30 +00:00
|
|
|
?>
|