3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 2001-2003 Martin Dougiamas http://dougiamas.com //
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. //
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: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once('grade_object.php');
29 * Class representing a grade outcome. It is responsible for handling its DB representation,
30 * modifying and returning its metadata.
32 class grade_outcome
extends grade_object
{
34 * DB Table (used by grade_object).
37 var $table = 'grade_outcomes';
40 * Array of class variables that are not part of the DB table fields
41 * @var array $nonfields
43 var $nonfields = array('table', 'nonfields', 'required_fields', 'scale');
46 * The course this outcome belongs to.
52 * The shortname of the outcome.
53 * @var string $shortname
58 * The fullname of the outcome.
59 * @var string $fullname
64 * A full grade_scale object referenced by $this->scaleid.
70 * The id of the scale referenced by this outcome.
76 * The description of this outcome - FORMAT_MOODLE.
77 * @var string $description
82 * The userid of the person who last modified this outcome.
83 * @var int $usermodified
88 * Deletes this outcome from the database.
89 * @param string $source from where was the object deleted (mod/forum, manual, etc.)
90 * @return boolean success
92 function delete($source=null) {
93 if (!empty($this->courseid
)) {
94 delete_records('grade_outcomes_courses', 'outcomeid', $this->id
, 'courseid', $this->courseid
);
96 return parent
::delete($source);
100 * Records this object in the Database, sets its id to the returned value, and returns that value.
101 * If successful this function also fetches the new object data from database and stores it
102 * in object properties.
103 * @param string $source from where was the object inserted (mod/forum, manual, etc.)
104 * @return int PK ID if successful, false otherwise
106 function insert($source=null) {
107 if ($result = parent
::insert($source)) {
108 if (!empty($this->courseid
)) {
110 $goc->courseid
= $this->courseid
;
111 $goc->outcomeid
= $this->id
;
112 insert_record('grade_outcomes_courses', $goc);
119 * In addition to update() it also updates grade_outcomes_courses if needed
120 * @param string $source from where was the object inserted
121 * @return boolean success
123 function update($source=null) {
124 if ($result = parent
::update($source)) {
125 if (!empty($this->courseid
)) {
126 if (!get_records('grade_outcomes_courses', 'courseid', $this->courseid
, 'outcomeid', $this->id
)) {
128 $goc->courseid
= $this->courseid
;
129 $goc->outcomeid
= $this->id
;
130 insert_record('grade_outcomes_courses', $goc);
138 * Finds and returns a grade_outcome instance based on params.
141 * @param array $params associative arrays varname=>value
142 * @return object grade_outcome instance or false if none found.
144 function fetch($params) {
145 return grade_object
::fetch_helper('grade_outcomes', 'grade_outcome', $params);
149 * Finds and returns all grade_outcome instances based on params.
152 * @param array $params associative arrays varname=>value
153 * @return array array of grade_outcome insatnces or false if none found.
155 function fetch_all($params) {
156 return grade_object
::fetch_all_helper('grade_outcomes', 'grade_outcome', $params);
160 * Instantiates a grade_scale object whose data is retrieved from the
161 * @return object grade_scale
163 function load_scale() {
164 if (empty($this->scale
->id
) or $this->scale
->id
!= $this->scaleid
) {
165 $this->scale
= grade_scale
::fetch(array('id'=>$this->scaleid
));
166 $this->scale
->load_items();
172 * Static function returning all global outcomes
176 function fetch_all_global() {
177 if (!$outcomes = grade_outcome
::fetch_all(array('courseid'=>null))) {
184 * Static function returning all local course outcomes
186 * @param int $courseid
189 function fetch_all_local($courseid) {
190 if (!$outcomes =grade_outcome
::fetch_all(array('courseid'=>$courseid))) {
197 * Static method - returns all outcomes available in course
199 * @param int $courseid
202 function fetch_all_available($courseid) {
207 FROM {$CFG->prefix}grade_outcomes go, {$CFG->prefix}grade_outcomes_courses goc
208 WHERE go.id = goc.outcomeid AND goc.courseid = {$courseid}
211 if ($datas = get_records_sql($sql)) {
212 foreach($datas as $data) {
213 $instance = new grade_outcome();
214 grade_object
::set_properties($instance, $data);
215 $result[$instance->id
] = $instance;
223 * Returns the most descriptive field for this object. This is a standard method used
224 * when we do not know the exact type of an object.
225 * @return string name
227 function get_name() {
228 return format_string($this->fullname
);
232 * Returns unique outcome short name.
233 * @return string name
235 function get_shortname() {
236 return $this->shortname
;
240 * Checks if outcome can be deleted.
243 function can_delete() {
244 if ($this->get_item_uses_count()) {
247 if (empty($this->courseid
)) {
248 if ($this->get_course_uses_count()) {
256 * Returns the number of places where outcome is used.
259 function get_course_uses_count() {
262 if (!empty($this->courseid
)) {
266 return count_records('grade_outcomes_courses', 'outcomeid', $this->id
);
270 * Returns the number of places where outcome is used.
273 function get_item_uses_count() {
274 return count_records('grade_items', 'outcomeid', $this->id
);
278 * Computes then returns extra information about this outcome and other objects that are linked to it.
279 * The average of all grades that use this outcome, for all courses (or 1 course if courseid is given) can
280 * be requested, and is returned as a float if requested alone. If the list of items that use this outcome
281 * is also requested, then a single array is returned, which contains the grade_items AND the average grade
282 * if such is still requested (array('items' => array(...), 'avg' => 2.30)). This combining of two
283 * methods into one is to save on DB queries, since both queries are similar and can be performed together.
284 * @param int $courseid An optional courseid to narrow down the average to 1 course only
285 * @param bool $average Whether or not to return the average grade for this outcome
286 * @param bool $items Whether or not to return the list of items using this outcome
289 function get_grade_info($courseid=null, $average=true, $items=false) {
290 if (!isset($this->id
)) {
291 debugging("You must setup the outcome's id before calling its get_grade_info() method!");
292 return false; // id must be defined for this to work
295 if ($average === false && $items === false) {
296 debugging('Either the 1st or 2nd param of grade_outcome::get_grade_info() must be true, or both, but not both false!');
301 if (!is_null($courseid)) {
302 $wheresql = " AND mdl_grade_items.courseid = $courseid ";
306 if ($items !== false) {
307 $selectadd = ', mdl_grade_items.* ';
310 $sql = "SELECT finalgrade $selectadd
311 FROM mdl_grade_grades, mdl_grade_items, mdl_grade_outcomes
312 WHERE mdl_grade_outcomes.id = mdl_grade_items.outcomeid
313 AND mdl_grade_items.id = mdl_grade_grades.itemid
314 AND mdl_grade_outcomes.id = $this->id
317 $grades = get_records_sql($sql);
320 if ($average !== false && count($grades) > 0) {
324 foreach ($grades as $k => $grade) {
325 // Skip null finalgrades
326 if (!is_null($grade->finalgrade
)) {
327 $total +
= $grade->finalgrade
;
330 unset($grades[$k]->finalgrade
);
333 $retval['avg'] = $total / $count;
336 if ($items !== false) {
337 foreach ($grades as $grade) {
338 $retval['items'][$grade->id
] = new grade_item($grade);