Added some functions for the Sequencing and Navigation task for SCOMR 2004 compliance...
[moodle.git] / lib / gradelib.php
CommitLineData
5834dcdb 1<?php // $Id$
2
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///////////////////////////////////////////////////////////////////////////
25
26/**
42bbccd7 27 * Library of functions for gradebook
5834dcdb 28 *
29 * @author Moodle HQ developers
30 * @version $Id$
31 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
32 * @package moodlecore
33 */
34
42bbccd7 35define('GRADE_AGGREGATE_MEAN', 0);
36define('GRADE_AGGREGATE_MEDIAN', 1);
37define('GRADE_AGGREGATE_SUM', 2);
38define('GRADE_AGGREGATE_MODE', 3);
27f95e9b 39define('GRADE_CHILDTYPE_ITEM', 0);
40define('GRADE_CHILDTYPE_CAT', 1);
41define('GRADE_ITEM', 0); // Used to compare class names with CHILDTYPE values
42define('GRADE_CATEGORY', 1); // Used to compare class names with CHILDTYPE values
42bbccd7 43
3058964f 44require_once($CFG->libdir . '/grade/grade_category.php');
45require_once($CFG->libdir . '/grade/grade_item.php');
46require_once($CFG->libdir . '/grade/grade_calculation.php');
a8995b34 47require_once($CFG->libdir . '/grade/grade_grades_raw.php');
869807d8 48require_once($CFG->libdir . '/grade/grade_grades_final.php');
d5bdb228 49require_once($CFG->libdir . '/grade/grade_scale.php');
5501446d 50require_once($CFG->libdir . '/grade/grade_outcome.php');
60cf7430 51
5834dcdb 52/**
42bbccd7 53* Extracts from the gradebook all the grade items attached to the calling object.
5834dcdb 54* For example, an assignment may want to retrieve all the grade_items for itself,
55* and get three outcome scales in return. This will affect the grading interface.
56*
57* Note: Each parameter refines the search. So if you only give the courseid,
58* all the grade_items for this course will be returned. If you add the
59* itemtype 'mod', all grade_items for this courseif AND for the 'mod'
60* type will be returned, etc...
61*
42bbccd7 62* @param int $courseid The id of the course to which the grade items belong
5834dcdb 63* @param string $itemname The name of the grade item
64* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
65* @param string $itemmodule 'forum, 'quiz', 'csv' etc
66* @param int $iteminstance id of the item module
67* @param int $itemnumber Can be used to distinguish multiple grades for an activity
42bbccd7 68* @param int $idnumber grade item Primary Key
69* @return array An array of grade items
5834dcdb 70*/
3058964f 71function grade_get_items($courseid, $itemname=NULL, $itemtype=NULL, $itemmodule=NULL, $iteminstance=NULL, $itemnumber=NULL, $idnumber=NULL) {
72 $grade_item = new grade_item(compact('courseid', 'itemname', 'itemtype', 'itemmodule', 'iteminstance', 'itemnumber', 'idnumber'), false);
73 $grade_items = $grade_item->fetch_all_using_this();
42bbccd7 74 return $grade_items;
5834dcdb 75}
76
77
78/**
79* Creates a new grade_item in case it doesn't exist. This function would be called when a module
80* is created or updates, for example, to ensure grade_item entries exist.
81* It's not essential though--if grades are being added later and a matching grade_item doesn't
82* yet exist, the gradebook will create them on the fly.
83*
84* @param
85* @return mixed New grade_item id if successful
86*/
3058964f 87function grade_create_item($params) {
42bbccd7 88 $grade_item = new grade_item($params);
89 return $grade_item->insert();
5834dcdb 90}
91
92/**
93* For a given set of items, create a category to group them together (if one doesn't yet exist).
94* Modules may want to do this when they are created. However, the ultimate control is in the gradebook interface itself.
619a59a7 95*
96* @param int $courseid
42bbccd7 97* @param string $fullname The name of the new category
98* @param array $items An array of grade_items to group under the new category
5834dcdb 99* @param string $aggregation
100* @return mixed New grade_category id if successful
101*/
3058964f 102function grade_create_category($courseid, $fullname, $items, $aggregation=GRADE_AGGREGATE_MEAN) {
103 $grade_category = new grade_category(compact('courseid', 'fullname', 'items', 'aggregation'));
42bbccd7 104 return $grade_category->insert();
5834dcdb 105}
106
107
108/**
109* Tells a module whether a grade (or grade_item if $userid is not given) is currently locked or not.
110* This is a combination of the actual settings in the grade tables and a check on moodle/course:editgradeswhenlocked.
111* If it's locked to the current use then the module can print a nice message or prevent editing in the module.
3058964f 112* If no $userid is given, the method will always return the grade_item's locked state.
113* If a $userid is given, the method will first check the grade_item's locked state (the column). If it is locked,
114* the method will return true no matter the locked state of the specific grade being checked. If unlocked, it will
115* return the locked state of the specific grade.
116*
5834dcdb 117* @param string $itemtype 'mod', 'blocks', 'import', 'calculated' etc
118* @param string $itemmodule 'forum, 'quiz', 'csv' etc
119* @param int $iteminstance id of the item module
3058964f 120* @param int $itemnumber Optional number of the item to check
5834dcdb 121* @param int $userid ID of the user who owns the grade
122* @return boolean Whether the grade is locked or not
123*/
3058964f 124function grade_is_locked($itemtype, $itemmodule, $iteminstance, $itemnumber=NULL, $userid=NULL) {
125 $grade_item = new grade_item(compact('itemtype', 'itemmodule', 'iteminstance', 'itemnumber'));
126 return $grade_item->is_locked($userid);
5834dcdb 127}
128
a8995b34 129/**
130 * Updates all grade_grades_final for each grade_item matching the given attributes.
131 * The search is further restricted, so that only grade_items that have needs_update == TRUE
132 * or that use calculation are retrieved.
133 *
134 * @param int $courseid
135 * @param int $gradeitemid
136 * @return int Number of grade_items updated
137 */
138function grade_update_final_grades($courseid=NULL, $gradeitemid=NULL) {
139 $grade_item = new grade_item();
140 $grade_item->courseid = $courseid;
141 $grade_item->id = $gradeitemid;
142 $grade_items = $grade_item->fetch_all_using_this();
143
144 $count = 0;
145
146 foreach ($grade_items as $gi) {
147 $calculation = $gi->get_calculation();
148 if (!empty($calculation) || $gi->needsupdate) {
149 if ($gi->update_final_grade()) {
150 $count++;
151 }
152 }
153 }
154
155 return $count;
156}
60cf7430 157?>