Added hooks to qtype import/export
[moodle-linuxchix.git] / lib / grade / grade_outcome.php
blob077fd62915739b92ac8a572d285a4696221e0c87
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 require_once('grade_object.php');
28 /**
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 {
33 /**
34 * DB Table (used by grade_object).
35 * @var string $table
37 var $table = 'grade_outcomes';
39 /**
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');
45 /**
46 * The course this outcome belongs to.
47 * @var int $courseid
49 var $courseid;
51 /**
52 * The shortname of the outcome.
53 * @var string $shortname
55 var $shortname;
57 /**
58 * The fullname of the outcome.
59 * @var string $fullname
61 var $fullname;
63 /**
64 * A full grade_scale object referenced by $this->scaleid.
65 * @var object $scale
67 var $scale;
69 /**
70 * The id of the scale referenced by this outcome.
71 * @var int $scaleid
73 var $scaleid;
75 /**
76 * The description of this outcome - FORMAT_MOODLE.
77 * @var string $description
79 var $description;
81 /**
82 * The userid of the person who last modified this outcome.
83 * @var int $usermodified
85 var $usermodified;
87 /**
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);
99 /**
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)) {
109 $goc = new object();
110 $goc->courseid = $this->courseid;
111 $goc->outcomeid = $this->id;
112 insert_record('grade_outcomes_courses', $goc);
115 return $result;
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)) {
127 $goc = new object();
128 $goc->courseid = $this->courseid;
129 $goc->outcomeid = $this->id;
130 insert_record('grade_outcomes_courses', $goc);
134 return $result;
138 * Finds and returns a grade_outcome instance based on params.
139 * @static
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.
150 * @static
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();
168 return $this->scale;
172 * Static function returning all global outcomes
173 * @static
174 * @return object
176 function fetch_all_global() {
177 if (!$outcomes = grade_outcome::fetch_all(array('courseid'=>null))) {
178 $outcomes = array();
180 return $outcomes;
184 * Static function returning all local course outcomes
185 * @static
186 * @param int $courseid
187 * @return object
189 function fetch_all_local($courseid) {
190 if (!$outcomes =grade_outcome::fetch_all(array('courseid'=>$courseid))) {
191 $outcomes = array();
193 return $outcomes;
197 * Static method - returns all outcomes available in course
198 * @static
199 * @param int $courseid
200 * @return array
202 function fetch_all_available($courseid) {
203 global $CFG;
205 $result = array();
206 $sql = "SELECT go.*
207 FROM {$CFG->prefix}grade_outcomes go, {$CFG->prefix}grade_outcomes_courses goc
208 WHERE go.id = goc.outcomeid AND goc.courseid = {$courseid}
209 ORDER BY go.id ASC";
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;
218 return $result;
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.
241 * @return boolean
243 function can_delete() {
244 if ($this->get_item_uses_count()) {
245 return false;
247 if (empty($this->courseid)) {
248 if ($this->get_course_uses_count()) {
249 return false;
252 return true;
256 * Returns the number of places where outcome is used.
257 * @return int
259 function get_course_uses_count() {
260 global $CFG;
262 if (!empty($this->courseid)) {
263 return 1;
266 return count_records('grade_outcomes_courses', 'outcomeid', $this->id);
270 * Returns the number of places where outcome is used.
271 * @return int
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
287 * @return float
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!');
297 return false;
300 $wheresql = '';
301 if (!is_null($courseid)) {
302 $wheresql = " AND mdl_grade_items.courseid = $courseid ";
305 $selectadd = '';
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
315 $wheresql";
317 $grades = get_records_sql($sql);
318 $retval = array();
320 if ($average !== false && count($grades) > 0) {
321 $count = 0;
322 $total = 0;
324 foreach ($grades as $k => $grade) {
325 // Skip null finalgrades
326 if (!is_null($grade->finalgrade)) {
327 $total += $grade->finalgrade;
328 $count++;
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);
342 return $retval;