MDL-9667 - convert_tree_to_html doesn't set css class properly if
[moodle.git] / lib / grade / grade_category.php
CommitLineData
8a31e65c 1<?php // $Id$
2
3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
8// http://moodle.com //
9// //
1f0e4921 10// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
8a31e65c 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
26require_once('grade_object.php');
27
3058964f 28class grade_category extends grade_object {
8a31e65c 29 /**
7c8a963f 30 * The DB table.
8a31e65c 31 * @var string $table
32 */
33 var $table = 'grade_categories';
4a490db0 34
8a31e65c 35 /**
3f2b0c8a 36 * Array of required table fields, must start with 'id'.
37 * @var array $required_fields
8a31e65c 38 */
3f2b0c8a 39 var $required_fields = array('id', 'courseid', 'parent', 'depth', 'path', 'fullname', 'aggregation',
40 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes',
41 'aggregatesubcats', 'timecreated', 'timemodified');
4a490db0 42
8a31e65c 43 /**
44 * The course this category belongs to.
45 * @var int $courseid
46 */
47 var $courseid;
4a490db0 48
8a31e65c 49 /**
50 * The category this category belongs to (optional).
4a490db0 51 * @var int $parent
8a31e65c 52 */
e5c674f1 53 var $parent;
4a490db0 54
8c846243 55 /**
56 * The grade_category object referenced by $this->parent (PK).
57 * @var object $parent_category
58 */
59 var $parent_category;
27f95e9b 60
e5c674f1 61 /**
62 * The number of parents this category has.
63 * @var int $depth
64 */
65 var $depth = 0;
66
67 /**
c2efb501 68 * Shows the hierarchical path for this category as /1/2/3/ (like course_categories), the last number being
e5c674f1 69 * this category's autoincrement ID number.
70 * @var string $path
71 */
72 var $path;
73
8a31e65c 74 /**
75 * The name of this category.
76 * @var string $fullname
77 */
78 var $fullname;
4a490db0 79
8a31e65c 80 /**
81 * A constant pointing to one of the predefined aggregation strategies (none, mean, median, sum etc) .
4a490db0 82 * @var int $aggregation
8a31e65c 83 */
c2efb501 84 var $aggregation = GRADE_AGGREGATE_MEAN;
4a490db0 85
8a31e65c 86 /**
87 * Keep only the X highest items.
88 * @var int $keephigh
89 */
f92dcad8 90 var $keephigh = 0;
4a490db0 91
8a31e65c 92 /**
93 * Drop the X lowest items.
94 * @var int $droplow
95 */
f92dcad8 96 var $droplow = 0;
4a490db0 97
c2efb501 98 /**
99 * Aggregate only graded items
100 * @var int $aggregateonlygraded
101 */
190af29f 102 var $aggregateonlygraded = 0;
c2efb501 103
29d509f5 104 /**
105 * Aggregate outcomes together with normal items
c2efb501 106 * @var int $aggregateoutcomes
29d509f5 107 */
108 var $aggregateoutcomes = 0;
109
c2efb501 110 /**
111 * Ignore subcategories when aggregating
112 * @var int $aggregatesubcats
113 */
114 var $aggregatesubcats = 0;
115
8a31e65c 116 /**
117 * Array of grade_items or grade_categories nested exactly 1 level below this category
118 * @var array $children
119 */
120 var $children;
8a31e65c 121
7c8a963f 122 /**
4a490db0 123 * A hierarchical array of all children below this category. This is stored separately from
7c8a963f 124 * $children because it is more memory-intensive and may not be used as often.
125 * @var array $all_children
126 */
127 var $all_children;
128
f151b073 129 /**
130 * An associated grade_item object, with itemtype=category, used to calculate and cache a set of grade values
131 * for this category.
132 * @var object $grade_item
133 */
134 var $grade_item;
135
b3ac6c3e 136 /**
137 * Temporary sortorder for speedup of children resorting
138 */
139 var $sortorder;
140
89a5f827 141 /**
142 * List of options which can be "forced" from site settings.
143 */
144 var $forceable = array('aggregation', 'keephigh', 'droplow', 'aggregateonlygraded', 'aggregateoutcomes', 'aggregatesubcats');
145
e5c674f1 146 /**
147 * Builds this category's path string based on its parents (if any) and its own id number.
148 * This is typically done just before inserting this object in the DB for the first time,
ce385eb4 149 * or when a new parent is added or changed. It is a recursive function: once the calling
150 * object no longer has a parent, the path is complete.
151 *
152 * @static
153 * @param object $grade_category
154 * @return int The depth of this category (2 means there is one parent)
e5c674f1 155 */
ce385eb4 156 function build_path($grade_category) {
157 if (empty($grade_category->parent)) {
c2efb501 158 return '/'.$grade_category->id.'/';
ce385eb4 159 } else {
f3ac8eb4 160 $parent = get_record('grade_categories', 'id', $grade_category->parent);
9a68cffc 161 return grade_category::build_path($parent).$grade_category->id.'/';
ce385eb4 162 }
e5c674f1 163 }
164
8a31e65c 165 /**
f92dcad8 166 * Finds and returns a grade_category instance based on params.
61c33818 167 * @static
8a31e65c 168 *
f92dcad8 169 * @param array $params associative arrays varname=>value
170 * @return object grade_category instance or false if none found.
171 */
172 function fetch($params) {
f3ac8eb4 173 return grade_object::fetch_helper('grade_categories', 'grade_category', $params);
f92dcad8 174 }
175
176 /**
177 * Finds and returns all grade_category instances based on params.
178 * @static
179 *
180 * @param array $params associative arrays varname=>value
181 * @return array array of grade_category insatnces or false if none found.
182 */
183 function fetch_all($params) {
f3ac8eb4 184 return grade_object::fetch_all_helper('grade_categories', 'grade_category', $params);
ce385eb4 185 }
186
8f4a626d 187 /**
2cc4b0f9 188 * In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
aaff71da 189 * @param string $source from where was the object updated (mod/forum, manual, etc.)
190 * @return boolean success
8f4a626d 191 */
aaff71da 192 function update($source=null) {
b3ac6c3e 193 // load the grade item or create a new one
194 $this->load_grade_item();
195
196 // force recalculation of path;
197 if (empty($this->path)) {
9a68cffc 198 $this->path = grade_category::build_path($this);
c2efb501 199 $this->depth = substr_count($this->path, '/') - 1;
4a490db0 200 }
0fc7f624 201
89a5f827 202 $this->apply_forced_settings();
203
204 // these are exclusive
205 if ($this->droplow > 0) {
206 $this->keephigh = 0;
207 } else if ($this->keephigh > 0) {
208 $this->droplow = 0;
209 }
4a490db0 210
b3ac6c3e 211 // Recalculate grades if needed
212 if ($this->qualifies_for_regrading()) {
f8e6e4db 213 $this->force_regrading();
4a490db0 214 }
f8e6e4db 215
ced5ee59 216 $this->timemodified = time();
217
f8e6e4db 218 return parent::update($source);
8f4a626d 219 }
4a490db0 220
8f4a626d 221 /**
2cc4b0f9 222 * If parent::delete() is successful, send force_regrading message to parent category.
aaff71da 223 * @param string $source from where was the object deleted (mod/forum, manual, etc.)
224 * @return boolean success
8f4a626d 225 */
aaff71da 226 function delete($source=null) {
f13002d5 227 $grade_item = $this->load_grade_item();
4a490db0 228
f615fbab 229 if ($this->is_course_category()) {
f3ac8eb4 230 if ($categories = grade_category::fetch_all(array('courseid'=>$this->courseid))) {
f615fbab 231 foreach ($categories as $category) {
232 if ($category->id == $this->id) {
233 continue; // do not delete course category yet
234 }
235 $category->delete($source);
236 }
aaff71da 237 }
2b0f65e2 238
f3ac8eb4 239 if ($items = grade_item::fetch_all(array('courseid'=>$this->courseid))) {
f615fbab 240 foreach ($items as $item) {
241 if ($item->id == $grade_item->id) {
242 continue; // do not delete course item yet
243 }
244 $item->delete($source);
245 }
246 }
247
248 } else {
249 $this->force_regrading();
2b0f65e2 250
f615fbab 251 $parent = $this->load_parent_category();
2b0f65e2 252
f615fbab 253 // Update children's categoryid/parent field first
f3ac8eb4 254 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
f615fbab 255 foreach ($children as $child) {
256 $child->set_parent($parent->id);
257 }
258 }
f3ac8eb4 259 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
f615fbab 260 foreach ($children as $child) {
261 $child->set_parent($parent->id);
262 }
aaff71da 263 }
264 }
f13002d5 265
aaff71da 266 // first delete the attached grade item and grades
267 $grade_item->delete($source);
f13002d5 268
269 // delete category itself
aaff71da 270 return parent::delete($source);
8f4a626d 271 }
4a490db0 272
ce385eb4 273 /**
274 * In addition to the normal insert() defined in grade_object, this method sets the depth
275 * and path for this object, and update the record accordingly. The reason why this must
276 * be done here instead of in the constructor, is that they both need to know the record's
4a490db0 277 * id number, which only gets created at insertion time.
f151b073 278 * This method also creates an associated grade_item if this wasn't done during construction.
aaff71da 279 * @param string $source from where was the object inserted (mod/forum, manual, etc.)
280 * @return int PK ID if successful, false otherwise
ce385eb4 281 */
aaff71da 282 function insert($source=null) {
b3ac6c3e 283
284 if (empty($this->courseid)) {
285 error('Can not insert grade category without course id!');
b8ff92b6 286 }
4a490db0 287
b3ac6c3e 288 if (empty($this->parent)) {
f3ac8eb4 289 $course_category = grade_category::fetch_course_category($this->courseid);
b3ac6c3e 290 $this->parent = $course_category->id;
ce385eb4 291 }
4a490db0 292
b3ac6c3e 293 $this->path = null;
294
ced5ee59 295 $this->timecreated = $this->timemodified = time();
296
aaff71da 297 if (!parent::insert($source)) {
b3ac6c3e 298 debugging("Could not insert this category: " . print_r($this, true));
299 return false;
300 }
301
f8e6e4db 302 $this->force_regrading();
303
b3ac6c3e 304 // build path and depth
aaff71da 305 $this->update($source);
4a490db0 306
aaff71da 307 return $this->id;
b3ac6c3e 308 }
309
f2c88356 310 /**
f615fbab 311 * Internal function - used only from fetch_course_category()
312 * Normal insert() can not be used for course category
313 * @param int $courseid
314 * @return bool success
f2c88356 315 */
b3ac6c3e 316 function insert_course_category($courseid) {
1f0e4921 317 $this->courseid = $courseid;
318 $this->fullname = get_string('coursegradecategory', 'grades');
319 $this->path = null;
320 $this->parent = null;
0c87b5aa 321 $this->aggregation = GRADE_AGGREGATE_WEIGHTED_MEAN2;
4a490db0 322
190af29f 323 $this->apply_default_settings();
89a5f827 324 $this->apply_forced_settings();
325
ced5ee59 326 $this->timecreated = $this->timemodified = time();
327
aaff71da 328 if (!parent::insert('system')) {
b3ac6c3e 329 debugging("Could not insert this category: " . print_r($this, true));
330 return false;
f151b073 331 }
4a490db0 332
b3ac6c3e 333 // build path and depth
aaff71da 334 $this->update('system');
b3ac6c3e 335
aaff71da 336 return $this->id;
ce385eb4 337 }
4a490db0 338
8f4a626d 339 /**
340 * Compares the values held by this object with those of the matching record in DB, and returns
341 * whether or not these differences are sufficient to justify an update of all parent objects.
342 * This assumes that this object has an id number and a matching record in DB. If not, it will return false.
343 * @return boolean
344 */
2cc4b0f9 345 function qualifies_for_regrading() {
8f4a626d 346 if (empty($this->id)) {
6639ead3 347 debugging("Can not regrade non existing category");
8f4a626d 348 return false;
349 }
f3ac8eb4 350
351 $db_item = grade_category::fetch(array('id'=>$this->id));
4a490db0 352
c2efb501 353 $aggregationdiff = $db_item->aggregation != $this->aggregation;
354 $keephighdiff = $db_item->keephigh != $this->keephigh;
355 $droplowdiff = $db_item->droplow != $this->droplow;
356 $aggonlygrddiff = $db_item->aggregateonlygraded != $this->aggregateonlygraded;
357 $aggoutcomesdiff = $db_item->aggregateoutcomes != $this->aggregateoutcomes;
358 $aggsubcatsdiff = $db_item->aggregatesubcats != $this->aggregatesubcats;
8f4a626d 359
c2efb501 360 return ($aggregationdiff || $keephighdiff || $droplowdiff || $aggonlygrddiff || $aggoutcomesdiff || $aggsubcatsdiff);
8f4a626d 361 }
8c846243 362
363 /**
f8e6e4db 364 * Marks the category and course item as needing update - categories are always regraded.
365 * @return void
8c846243 366 */
f8e6e4db 367 function force_regrading() {
368 $grade_item = $this->load_grade_item();
369 $grade_item->force_regrading();
8c846243 370 }
371
0aa32279 372 /**
0758a08e 373 * Generates and saves final grades in associated category grade item.
1994d890 374 * These immediate children must already have their own final grades.
0758a08e 375 * The category's aggregation method is used to generate final grades.
ac9b0805 376 *
377 * Please note that category grade is either calculated or aggregated - not both at the same time.
378 *
c86caae7 379 * This method must be used ONLY from grade_item::regrade_final_grades(),
ac9b0805 380 * because the calculation must be done in correct order!
b8ff92b6 381 *
4a490db0 382 * Steps to follow:
ac9b0805 383 * 1. Get final grades from immediate children
2df71235 384 * 3. Aggregate these grades
0758a08e 385 * 4. Save them in final grades of associated category grade item
0aa32279 386 */
f8e6e4db 387 function generate_grades($userid=null) {
b8ff92b6 388 global $CFG;
4a490db0 389
ac9b0805 390 $this->load_grade_item();
2cc4b0f9 391
392 if ($this->grade_item->is_locked()) {
393 return true; // no need to recalculate locked items
394 }
395
89a5f827 396 // find grade items of immediate children (category or grade items) and force site settings
61c33818 397 $depends_on = $this->grade_item->depends_on();
b3ac6c3e 398
f8e6e4db 399 if (empty($depends_on)) {
400 $items = false;
401 } else {
402 $gis = implode(',', $depends_on);
403 $sql = "SELECT *
404 FROM {$CFG->prefix}grade_items
405 WHERE id IN ($gis)";
f3ac8eb4 406 $items = get_records_sql($sql);
f8e6e4db 407 }
4a490db0 408
f8e6e4db 409 if ($userid) {
410 $usersql = "AND g.userid=$userid";
411 } else {
412 $usersql = "";
b8ff92b6 413 }
4a490db0 414
f3ac8eb4 415 $grade_inst = new grade_grade();
3f2b0c8a 416 $fields = 'g.'.implode(',g.', $grade_inst->required_fields);
417
f8e6e4db 418 // where to look for final grades - include grade of this item too, we will store the results there
61c33818 419 $gis = implode(',', array_merge($depends_on, array($this->grade_item->id)));
3f2b0c8a 420 $sql = "SELECT $fields
ac9b0805 421 FROM {$CFG->prefix}grade_grades g, {$CFG->prefix}grade_items gi
f8e6e4db 422 WHERE gi.id = g.itemid AND gi.id IN ($gis) $usersql
ac9b0805 423 ORDER BY g.userid";
b8ff92b6 424
9580a21f 425 // group the results by userid and aggregate the grades for this user
f3ac8eb4 426 if ($rs = get_recordset_sql($sql)) {
03cedd62 427 $prevuser = 0;
428 $grade_values = array();
429 $excluded = array();
430 $oldgrade = null;
f3ac8eb4 431 while ($used = rs_fetch_next_record($rs)) {
03cedd62 432 if ($used->userid != $prevuser) {
433 $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);
434 $prevuser = $used->userid;
435 $grade_values = array();
436 $excluded = array();
437 $oldgrade = null;
438 }
439 $grade_values[$used->itemid] = $used->finalgrade;
440 if ($used->excluded) {
441 $excluded[] = $used->itemid;
442 }
443 if ($this->grade_item->id == $used->itemid) {
444 $oldgrade = $used;
2df71235 445 }
b8ff92b6 446 }
03cedd62 447 $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);//the last one
f3ac8eb4 448 rs_close($rs);
b8ff92b6 449 }
450
b8ff92b6 451 return true;
452 }
453
454 /**
ac9b0805 455 * internal function for category grades aggregation
ced5ee59 456 *
1994d890 457 * @param int $userid
458 * @param array $items
459 * @param array $grade_values
f1ad9e04 460 * @param object $oldgrade
1994d890 461 * @param bool $excluded
462 * @return boolean (just plain return;)
b8ff92b6 463 */
23207a1a 464 function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded) {
e171963b 465 global $CFG;
b8ff92b6 466 if (empty($userid)) {
f8e6e4db 467 //ignore first call
b8ff92b6 468 return;
469 }
4a490db0 470
f8e6e4db 471 if ($oldgrade) {
4ac209d5 472 if (is_null($oldgrade->finalgrade)) {
473 $oldfinalgrade = null;
474 } else {
0758a08e 475 // we need proper floats here for !== comparison later
f1ad9e04 476 $oldfinalgrade = (float)$oldgrade->finalgrade;
0758a08e 477 }
f3ac8eb4 478 $grade = new grade_grade($oldgrade, false);
f8e6e4db 479 $grade->grade_item =& $this->grade_item;
b8ff92b6 480
f8e6e4db 481 } else {
482 // insert final grade - it will be needed later anyway
f3ac8eb4 483 $grade = new grade_grade(array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
f8e6e4db 484 $grade->grade_item =& $this->grade_item;
f1ad9e04 485 $grade->insert('system');
486 $oldfinalgrade = null;
f8e6e4db 487 }
f1ad9e04 488
c86caae7 489 // no need to recalculate locked or overridden grades
490 if ($grade->is_locked() or $grade->is_overridden()) {
2cc4b0f9 491 return;
ac9b0805 492 }
493
f8e6e4db 494 // can not use own final category grade in calculation
9580a21f 495 unset($grade_values[$this->grade_item->id]);
f8e6e4db 496
f1ad9e04 497
498 /// sum is a special aggregation types - it adjusts the min max, does not use relative values
499 if ($this->aggregation == GRADE_AGGREGATE_SUM) {
500 $this->sum_grades($grade, $oldfinalgrade, $items, $grade_values, $excluded);
501 return;
502 }
503
0758a08e 504 // if no grades calculation possible or grading not allowed clear final grade
9580a21f 505 if (empty($grade_values) or empty($items) or ($this->grade_item->gradetype != GRADE_TYPE_VALUE and $this->grade_item->gradetype != GRADE_TYPE_SCALE)) {
f8e6e4db 506 $grade->finalgrade = null;
f1ad9e04 507 if ($grade->finalgrade !== $oldfinalgrade) {
0758a08e 508 $grade->update('aggregation');
f8e6e4db 509 }
b8ff92b6 510 return;
511 }
0758a08e 512
f8e6e4db 513 /// normalize the grades first - all will have value 0...1
d5f0aa01 514 // ungraded items are not used in aggregation
23207a1a 515 foreach ($grade_values as $itemid=>$v) {
b8ff92b6 516 if (is_null($v)) {
517 // null means no grade
23207a1a 518 unset($grade_values[$itemid]);
519 continue;
520 } else if (in_array($itemid, $excluded)) {
521 unset($grade_values[$itemid]);
b8ff92b6 522 continue;
0aa32279 523 }
9a68cffc 524 $grade_values[$itemid] = grade_grade::standardise_score($v, $items[$itemid]->grademin, $items[$itemid]->grademax, 0, 1);
0aa32279 525 }
dda0c7e6 526
eacd3700 527 // use min grade if grade missing for these types
c2efb501 528 if (!$this->aggregateonlygraded) {
529 foreach($items as $itemid=>$value) {
530 if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
531 $grade_values[$itemid] = 0;
eacd3700 532 }
c2efb501 533 }
eacd3700 534 }
535
536 // limit and sort
9580a21f 537 $this->apply_limit_rules($grade_values);
538 asort($grade_values, SORT_NUMERIC);
4a490db0 539
d5f0aa01 540 // let's see we have still enough grades to do any statistics
9580a21f 541 if (count($grade_values) == 0) {
ac9b0805 542 // not enough attempts yet
f8e6e4db 543 $grade->finalgrade = null;
f1ad9e04 544 if ($grade->finalgrade !== $oldfinalgrade) {
0758a08e 545 $grade->update('aggregation');
b8ff92b6 546 }
547 return;
548 }
2df71235 549
d297269d 550 // do the maths
551 $agg_grade = $this->aggregate_values($grade_values, $items);
552
0758a08e 553 // recalculate the grade back to requested range
9a68cffc 554 $finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $this->grade_item->grademin, $this->grade_item->grademax);
d297269d 555
f1ad9e04 556 if (is_null($finalgrade)) {
557 $grade->finalgrade = null;
d297269d 558 } else {
f1ad9e04 559 $grade->finalgrade = (float)bounded_number($this->grade_item->grademin, $finalgrade, $this->grade_item->grademax);
d297269d 560 }
561
562 // update in db if changed
f1ad9e04 563 if ($grade->finalgrade !== $oldfinalgrade) {
0758a08e 564 $grade->update('aggregation');
d297269d 565 }
566
567 return;
568 }
569
89a5f827 570 /**
571 * Internal function - aggregation maths.
572 */
d297269d 573 function aggregate_values($grade_values, $items) {
b8ff92b6 574 switch ($this->aggregation) {
c2efb501 575 case GRADE_AGGREGATE_MEDIAN: // Middle point value in the set: ignores frequencies
9580a21f 576 $num = count($grade_values);
c186c7b2 577 $grades = array_values($grade_values);
578 if ($num % 2 == 0) {
9c8d38fa 579 $agg_grade = ($grades[intval($num/2)-1] + $grades[intval($num/2)]) / 2;
b8ff92b6 580 } else {
9c8d38fa 581 $agg_grade = $grades[intval(($num/2)-0.5)];
b8ff92b6 582 }
583 break;
ac9b0805 584
c2efb501 585 case GRADE_AGGREGATE_MIN:
9c8d38fa 586 $agg_grade = reset($grade_values);
b8ff92b6 587 break;
588
c2efb501 589 case GRADE_AGGREGATE_MAX:
9c8d38fa 590 $agg_grade = array_pop($grade_values);
b8ff92b6 591 break;
592
c2efb501 593 case GRADE_AGGREGATE_MODE: // the most common value, average used if multimode
9580a21f 594 $freq = array_count_values($grade_values);
95affb8a 595 arsort($freq); // sort by frequency keeping keys
596 $top = reset($freq); // highest frequency count
597 $modes = array_keys($freq, $top); // search for all modes (have the same highest count)
598 rsort($modes, SORT_NUMERIC); // get highes mode
9c8d38fa 599 $agg_grade = reset($modes);
d5fab31f 600 break;
95affb8a 601
1426edac 602 case GRADE_AGGREGATE_WEIGHTED_MEAN: // Weighted average of all existing final grades, weight specified in coef
9580a21f 603 $weightsum = 0;
604 $sum = 0;
eacd3700 605 foreach($grade_values as $itemid=>$grade_value) {
606 if ($items[$itemid]->aggregationcoef <= 0) {
9580a21f 607 continue;
608 }
eacd3700 609 $weightsum += $items[$itemid]->aggregationcoef;
610 $sum += $items[$itemid]->aggregationcoef * $grade_value;
9580a21f 611 }
612 if ($weightsum == 0) {
9c8d38fa 613 $agg_grade = null;
9580a21f 614 } else {
9c8d38fa 615 $agg_grade = $sum / $weightsum;
9580a21f 616 }
617 break;
618
1426edac 619 case GRADE_AGGREGATE_WEIGHTED_MEAN2: // Weighted average of all existing final grades, weight is the range of grade (ususally grademax)
620 $weightsum = 0;
621 $sum = 0;
622 foreach($grade_values as $itemid=>$grade_value) {
623 $weight = $items[$itemid]->grademax - $items[$itemid]->grademin;
624 if ($weight <= 0) {
625 continue;
626 }
627 $weightsum += $weight;
628 $sum += $weight * $grade_value;
629 }
630 if ($weightsum == 0) {
631 $agg_grade = null;
632 } else {
633 $agg_grade = $sum / $weightsum;
634 }
635 break;
636
c2efb501 637 case GRADE_AGGREGATE_EXTRACREDIT_MEAN: // special average
9580a21f 638 $num = 0;
639 $sum = 0;
eacd3700 640 foreach($grade_values as $itemid=>$grade_value) {
641 if ($items[$itemid]->aggregationcoef == 0) {
9580a21f 642 $num += 1;
643 $sum += $grade_value;
eacd3700 644 } else if ($items[$itemid]->aggregationcoef > 0) {
645 $sum += $items[$itemid]->aggregationcoef * $grade_value;
9580a21f 646 }
647 }
648 if ($num == 0) {
9c8d38fa 649 $agg_grade = $sum; // only extra credits or wrong coefs
9580a21f 650 } else {
9c8d38fa 651 $agg_grade = $sum / $num;
9580a21f 652 }
653 break;
654
c2efb501 655 case GRADE_AGGREGATE_MEAN: // Arithmetic average of all grade items (if ungraded aggregated, NULL counted as minimum)
ac9b0805 656 default:
9580a21f 657 $num = count($grade_values);
658 $sum = array_sum($grade_values);
9c8d38fa 659 $agg_grade = $sum / $num;
b8ff92b6 660 break;
661 }
662
d297269d 663 return $agg_grade;
0aa32279 664 }
0758a08e 665
0758a08e 666 /**
667 * internal function for category grades summing
668 *
f1ad9e04 669 * @param object $grade
0758a08e 670 * @param int $userid
f1ad9e04 671 * @param float $oldfinalgrade
0758a08e 672 * @param array $items
673 * @param array $grade_values
0758a08e 674 * @param bool $excluded
675 * @return boolean (just plain return;)
676 */
f1ad9e04 677 function sum_grades(&$grade, $oldfinalgrade, $items, $grade_values, $excluded) {
0758a08e 678 // ungraded and exluded items are not used in aggregation
679 foreach ($grade_values as $itemid=>$v) {
680 if (is_null($v)) {
681 unset($grade_values[$itemid]);
682 } else if (in_array($itemid, $excluded)) {
683 unset($grade_values[$itemid]);
684 }
685 }
686
687 $max = 0;
688
689 //find max grade
690 foreach ($items as $item) {
691 if ($item->gradetype != GRADE_TYPE_VALUE) {
2e0d37fe 692 // sum only items with value grades, no scales and outcomes!
693 unset($grade_values[$item->id]);
694 continue;
695 }
696 if ($item->aggregationcoef > 0) {
697 // extra credit from this activity - does not affect total
698 continue;
0758a08e 699 }
700 $max += $item->grademax;
701 }
702
703 if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE){
704 $this->grade_item->grademax = $max;
705 $this->grade_item->grademin = 0;
706 $this->grade_item->gradetype = GRADE_TYPE_VALUE;
707 $this->grade_item->update('aggregation');
708 }
709
710 $this->apply_limit_rules($grade_values);
711
712 $sum = array_sum($grade_values);
f1ad9e04 713 $grade->finalgrade = (float)bounded_number($this->grade_item->grademin, $sum, $this->grade_item->grademax);
0758a08e 714
715 // update in db if changed
f1ad9e04 716 if ($grade->finalgrade !== $oldfinalgrade) {
0758a08e 717 $grade->update('aggregation');
718 }
719
720 return;
721 }
722
adc2f286 723 /**
724 * Given an array of grade values (numerical indices), applies droplow or keephigh
725 * rules to limit the final array.
9580a21f 726 * @param array $grade_values
adc2f286 727 * @return array Limited grades.
728 */
9580a21f 729 function apply_limit_rules(&$grade_values) {
730 arsort($grade_values, SORT_NUMERIC);
adc2f286 731 if (!empty($this->droplow)) {
732 for ($i = 0; $i < $this->droplow; $i++) {
9580a21f 733 array_pop($grade_values);
adc2f286 734 }
4a490db0 735 } elseif (!empty($this->keephigh)) {
9580a21f 736 while (count($grade_values) > $this->keephigh) {
737 array_pop($grade_values);
adc2f286 738 }
739 }
0aa32279 740 }
741
9580a21f 742
743 /**
744 * Returns true if category uses special aggregation coeficient
745 * @return boolean true if coeficient used
746 */
747 function is_aggregationcoef_used() {
c2efb501 748 return ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN
2e0d37fe 749 or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN
750 or $this->aggregation == GRADE_AGGREGATE_SUM);
ba74762b 751
9580a21f 752 }
753
1c307f21 754 /**
b3ac6c3e 755 * Returns tree with all grade_items and categories as elements
756 * @static
757 * @param int $courseid
b3ac6c3e 758 * @param boolean $include_category_items as category children
759 * @return array
1c307f21 760 */
938e00b6 761 function fetch_course_tree($courseid, $include_category_items=false) {
f3ac8eb4 762 $course_category = grade_category::fetch_course_category($courseid);
514a3467 763 $category_array = array('object'=>$course_category, 'type'=>'category', 'depth'=>1,
764 'children'=>$course_category->get_children($include_category_items));
765 $sortorder = 1;
766 $course_category->set_sortorder($sortorder);
767 $course_category->sortorder = $sortorder;
f3ac8eb4 768 return grade_category::_fetch_course_tree_recursion($category_array, $sortorder);
1c307f21 769 }
770
b3ac6c3e 771 function _fetch_course_tree_recursion($category_array, &$sortorder) {
772 // update the sortorder in db if needed
773 if ($category_array['object']->sortorder != $sortorder) {
774 $category_array['object']->set_sortorder($sortorder);
ce385eb4 775 }
ce385eb4 776
314c4336 777 // store the grade_item or grade_category instance with extra info
778 $result = array('object'=>$category_array['object'], 'type'=>$category_array['type'], 'depth'=>$category_array['depth']);
b3ac6c3e 779
780 // reuse final grades if there
781 if (array_key_exists('finalgrades', $category_array)) {
782 $result['finalgrades'] = $category_array['finalgrades'];
783 }
784
785 // recursively resort children
786 if (!empty($category_array['children'])) {
787 $result['children'] = array();
29d509f5 788 //process the category item first
789 $cat_item_id = null;
b3ac6c3e 790 foreach($category_array['children'] as $oldorder=>$child_array) {
314c4336 791 if ($child_array['type'] == 'courseitem' or $child_array['type'] == 'categoryitem') {
f3ac8eb4 792 $result['children'][$sortorder] = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
29d509f5 793 }
2b0f65e2 794 }
29d509f5 795 foreach($category_array['children'] as $oldorder=>$child_array) {
796 if ($child_array['type'] != 'courseitem' and $child_array['type'] != 'categoryitem') {
f3ac8eb4 797 $result['children'][++$sortorder] = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
b3ac6c3e 798 }
799 }
800 }
801
802 return $result;
ce385eb4 803 }
7c8a963f 804
805 /**
4a490db0 806 * Fetches and returns all the children categories and/or grade_items belonging to this category.
807 * By default only returns the immediate children (depth=1), but deeper levels can be requested,
a39cac25 808 * as well as all levels (0). The elements are indexed by sort order.
7c8a963f 809 * @return array Array of child objects (grade_category and grade_item).
810 */
938e00b6 811 function get_children($include_category_items=false) {
b3ac6c3e 812
813 // This function must be as fast as possible ;-)
814 // fetch all course grade items and categories into memory - we do not expect hundreds of these in course
815 // we have to limit the number of queries though, because it will be used often in grade reports
816
f3ac8eb4 817 $cats = get_records('grade_categories', 'courseid', $this->courseid);
818 $items = get_records('grade_items', 'courseid', $this->courseid);
4a490db0 819
b3ac6c3e 820 // init children array first
821 foreach ($cats as $catid=>$cat) {
822 $cats[$catid]->children = array();
27f95e9b 823 }
4a490db0 824
b3ac6c3e 825 //first attach items to cats and add category sortorder
826 foreach ($items as $item) {
827 if ($item->itemtype == 'course' or $item->itemtype == 'category') {
828 $cats[$item->iteminstance]->sortorder = $item->sortorder;
4a490db0 829
b3ac6c3e 830 if (!$include_category_items) {
831 continue;
832 }
833 $categoryid = $item->iteminstance;
834 } else {
835 $categoryid = $item->categoryid;
836 }
837
838 // prevent problems with duplicate sortorders in db
839 $sortorder = $item->sortorder;
840 while(array_key_exists($sortorder, $cats[$categoryid]->children)) {
f13002d5 841 //debugging("$sortorder exists in item loop");
b3ac6c3e 842 $sortorder++;
843 }
844
845 $cats[$categoryid]->children[$sortorder] = $item;
846
847 }
848
849 // now find the requested category and connect categories as children
850 $category = false;
851 foreach ($cats as $catid=>$cat) {
852 if (!empty($cat->parent)) {
853 // prevent problems with duplicate sortorders in db
854 $sortorder = $cat->sortorder;
855 while(array_key_exists($sortorder, $cats[$cat->parent]->children)) {
f13002d5 856 //debugging("$sortorder exists in cat loop");
b3ac6c3e 857 $sortorder++;
858 }
859
860 $cats[$cat->parent]->children[$sortorder] = $cat;
861 }
f3ac8eb4 862
b3ac6c3e 863 if ($catid == $this->id) {
864 $category = &$cats[$catid];
865 }
866 }
867
868 unset($items); // not needed
869 unset($cats); // not needed
870
f3ac8eb4 871 $children_array = grade_category::_get_children_recursion($category);
b3ac6c3e 872
873 ksort($children_array);
874
875 return $children_array;
876
877 }
878
938e00b6 879 function _get_children_recursion($category) {
b3ac6c3e 880
881 $children_array = array();
882 foreach($category->children as $sortorder=>$child) {
883 if (array_key_exists('itemtype', $child)) {
f3ac8eb4 884 $grade_item = new grade_item($child, false);
4faf5f99 885 if (in_array($grade_item->itemtype, array('course', 'category'))) {
886 $type = $grade_item->itemtype.'item';
887 $depth = $category->depth;
314c4336 888 } else {
889 $type = 'item';
890 $depth = $category->depth; // we use this to set the same colour
b3ac6c3e 891 }
4faf5f99 892 $children_array[$sortorder] = array('object'=>$grade_item, 'type'=>$type, 'depth'=>$depth);
4a490db0 893
7c8a963f 894 } else {
f3ac8eb4 895 $children = grade_category::_get_children_recursion($child);
896 $grade_category = new grade_category($child, false);
b3ac6c3e 897 if (empty($children)) {
314c4336 898 $children = array();
7c8a963f 899 }
4faf5f99 900 $children_array[$sortorder] = array('object'=>$grade_category, 'type'=>'category', 'depth'=>$grade_category->depth, 'children'=>$children);
314c4336 901 }
27f95e9b 902 }
903
b3ac6c3e 904 // sort the array
905 ksort($children_array);
906
27f95e9b 907 return $children_array;
908 }
4a490db0 909
f151b073 910 /**
ab53054f 911 * Uses get_grade_item to load or create a grade_item, then saves it as $this->grade_item.
f151b073 912 * @return object Grade_item
913 */
914 function load_grade_item() {
ac9b0805 915 if (empty($this->grade_item)) {
916 $this->grade_item = $this->get_grade_item();
917 }
ab53054f 918 return $this->grade_item;
919 }
4a490db0 920
ab53054f 921 /**
922 * Retrieves from DB and instantiates the associated grade_item object.
923 * If no grade_item exists yet, create one.
924 * @return object Grade_item
925 */
926 function get_grade_item() {
c91ed4be 927 if (empty($this->id)) {
928 debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set.");
929 return false;
930 }
931
b3ac6c3e 932 if (empty($this->parent)) {
933 $params = array('courseid'=>$this->courseid, 'itemtype'=>'course', 'iteminstance'=>$this->id);
934
935 } else {
936 $params = array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id);
937 }
4ac209d5 938
f3ac8eb4 939 if (!$grade_items = grade_item::fetch_all($params)) {
b8ff92b6 940 // create a new one
f3ac8eb4 941 $grade_item = new grade_item($params, false);
b8ff92b6 942 $grade_item->gradetype = GRADE_TYPE_VALUE;
f8e6e4db 943 $grade_item->insert('system');
4a490db0 944
b8ff92b6 945 } else if (count($grade_items) == 1){
946 // found existing one
947 $grade_item = reset($grade_items);
4a490db0 948
b8ff92b6 949 } else {
950 debugging("Found more than one grade_item attached to category id:".$this->id);
ac9b0805 951 // return first one
952 $grade_item = reset($grade_items);
2c72af1f 953 }
954
ab53054f 955 return $grade_item;
f151b073 956 }
8c846243 957
958 /**
959 * Uses $this->parent to instantiate $this->parent_category based on the
960 * referenced record in the DB.
961 * @return object Parent_category
962 */
963 function load_parent_category() {
964 if (empty($this->parent_category) && !empty($this->parent)) {
ab53054f 965 $this->parent_category = $this->get_parent_category();
8c846243 966 }
967 return $this->parent_category;
4a490db0 968 }
969
ab53054f 970 /**
971 * Uses $this->parent to instantiate and return a grade_category object.
972 * @return object Parent_category
973 */
974 function get_parent_category() {
975 if (!empty($this->parent)) {
f3ac8eb4 976 $parent_category = new grade_category(array('id' => $this->parent));
4a490db0 977 return $parent_category;
ab53054f 978 } else {
979 return null;
980 }
981 }
982
2186f72c 983 /**
4a490db0 984 * Returns the most descriptive field for this object. This is a standard method used
2186f72c 985 * when we do not know the exact type of an object.
986 * @return string name
987 */
988 function get_name() {
314c4336 989 if (empty($this->parent)) {
f3ac8eb4 990 $course = get_record('course', 'id', $this->courseid);
410753fb 991 return format_string($course->fullname);
314c4336 992 } else {
993 return $this->fullname;
994 }
2186f72c 995 }
c91ed4be 996
0fc7f624 997 /**
998 * Sets this category's parent id. A generic method shared by objects that have a parent id of some kind.
f13002d5 999 * @param int parentid
1000 * @return boolean success
0fc7f624 1001 */
f8e6e4db 1002 function set_parent($parentid, $source=null) {
f13002d5 1003 if ($this->parent == $parentid) {
1004 return true;
1005 }
1006
1007 if ($parentid == $this->id) {
1008 error('Can not assign self as parent!');
1009 }
1010
1011 if (empty($this->parent) and $this->is_course_category()) {
1012 error('Course category can not have parent!');
b3ac6c3e 1013 }
f13002d5 1014
1015 // find parent and check course id
f3ac8eb4 1016 if (!$parent_category = grade_category::fetch(array('id'=>$parentid, 'courseid'=>$this->courseid))) {
b3ac6c3e 1017 return false;
1018 }
1019
f8e6e4db 1020 $this->force_regrading();
b3ac6c3e 1021
1022 // set new parent category
f8e6e4db 1023 $this->parent = $parent_category->id;
1024 $this->parent_category =& $parent_category;
b3ac6c3e 1025 $this->path = null; // remove old path and depth - will be recalculated in update()
f8e6e4db 1026 $this->depth = null; // remove old path and depth - will be recalculated in update()
1027 $this->update($source);
b3ac6c3e 1028
15b462da 1029 return $this->update($source);
b3ac6c3e 1030 }
1031
1032 /**
1033 * Returns the final values for this grade category.
1034 * @param int $userid Optional: to retrieve a single final grade
1035 * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade.
1036 */
1037 function get_final($userid=NULL) {
1038 $this->load_grade_item();
1039 return $this->grade_item->get_final($userid);
0fc7f624 1040 }
4a490db0 1041
0fc7f624 1042 /**
4a490db0 1043 * Returns the sortorder of the associated grade_item. This method is also available in
5fad5061 1044 * grade_item, for cases where the object type is not known.
0fc7f624 1045 * @return int Sort order
1046 */
1047 function get_sortorder() {
b3ac6c3e 1048 $this->load_grade_item();
1049 return $this->grade_item->get_sortorder();
0fc7f624 1050 }
1051
be7c0693 1052 /**
1053 * Returns the idnumber of the associated grade_item. This method is also available in
1054 * grade_item, for cases where the object type is not known.
1055 * @return string idnumber
1056 */
1057 function get_idnumber() {
1058 $this->load_grade_item();
1059 return $this->grade_item->get_idnumber();
1060 }
1061
0fc7f624 1062 /**
b3ac6c3e 1063 * Sets sortorder variable for this category.
4a490db0 1064 * This method is also available in grade_item, for cases where the object type is not know.
0fc7f624 1065 * @param int $sortorder
1066 * @return void
1067 */
1068 function set_sortorder($sortorder) {
b3ac6c3e 1069 $this->load_grade_item();
1070 $this->grade_item->set_sortorder($sortorder);
1071 }
1072
6639ead3 1073 /**
1074 * Move this category after the given sortorder - does not change the parent
1075 * @param int $sortorder to place after
1076 */
f13002d5 1077 function move_after_sortorder($sortorder) {
1078 $this->load_grade_item();
1079 $this->grade_item->move_after_sortorder($sortorder);
1080 }
1081
b3ac6c3e 1082 /**
f13002d5 1083 * Return true if this is the top most category that represents the total course grade.
b3ac6c3e 1084 * @return boolean
1085 */
1086 function is_course_category() {
1087 $this->load_grade_item();
1088 return $this->grade_item->is_course_item();
1089 }
1090
1091 /**
1092 * Return the top most course category.
1093 * @static
1094 * @return object grade_category instance for course grade
1095 */
1096 function fetch_course_category($courseid) {
1097
1098 // course category has no parent
f3ac8eb4 1099 if ($course_category = grade_category::fetch(array('courseid'=>$courseid, 'parent'=>null))) {
b3ac6c3e 1100 return $course_category;
1101 }
1102
1103 // create a new one
f3ac8eb4 1104 $course_category = new grade_category();
b3ac6c3e 1105 $course_category->insert_course_category($courseid);
1106
1107 return $course_category;
0fc7f624 1108 }
4ac209d5 1109
79eabc2a 1110 /**
1111 * Is grading object editable?
1112 * @return boolean
1113 */
1114 function is_editable() {
1115 return true;
1116 }
1117
5fad5061 1118 /**
4a490db0 1119 * Returns the locked state/date of the associated grade_item. This method is also available in
1120 * grade_item, for cases where the object type is not known.
22e23c78 1121 * @return boolean
5fad5061 1122 */
2cc4b0f9 1123 function is_locked() {
5fad5061 1124 $this->load_grade_item();
22e23c78 1125 return $this->grade_item->is_locked();
5fad5061 1126 }
1127
1128 /**
1129 * Sets the grade_item's locked variable and updates the grade_item.
1130 * Method named after grade_item::set_locked().
1131 * @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
fb0e3570 1132 * @param boolean $cascade lock/unlock child objects too
2b0f65e2 1133 * @param boolean $refresh refresh grades when unlocking
9580a21f 1134 * @return boolean success if category locked (not all children mayb be locked though)
5fad5061 1135 */
fb0e3570 1136 function set_locked($lockedstate, $cascade=false, $refresh=true) {
5fad5061 1137 $this->load_grade_item();
2b0f65e2 1138
fb0e3570 1139 $result = $this->grade_item->set_locked($lockedstate, $cascade, true);
1140
1141 if ($cascade) {
1142 //process all children - items and categories
f3ac8eb4 1143 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
fb0e3570 1144 foreach($children as $child) {
1145 $child->set_locked($lockedstate, true, false);
1146 if (empty($lockedstate) and $refresh) {
1147 //refresh when unlocking
1148 $child->refresh_grades();
1149 }
2b0f65e2 1150 }
7a7a53d3 1151 }
f3ac8eb4 1152 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
fb0e3570 1153 foreach($children as $child) {
1154 $child->set_locked($lockedstate, true, true);
1155 }
7a7a53d3 1156 }
1157 }
2b0f65e2 1158
b121b544 1159 return $result;
5fad5061 1160 }
4a490db0 1161
5fad5061 1162 /**
4a490db0 1163 * Returns the hidden state/date of the associated grade_item. This method is also available in
22e23c78 1164 * grade_item.
1165 * @return boolean
5fad5061 1166 */
22e23c78 1167 function is_hidden() {
5fad5061 1168 $this->load_grade_item();
22e23c78 1169 return $this->grade_item->is_hidden();
5fad5061 1170 }
1171
597f50e6 1172 /**
1173 * Check grade hidden status. Uses data from both grade item and grade.
1174 * @return boolean true if hiddenuntil, false if not
1175 */
1176 function is_hiddenuntil() {
1177 $this->load_grade_item();
1178 return $this->grade_item->is_hiddenuntil();
1179 }
1180
5fad5061 1181 /**
4a490db0 1182 * Sets the grade_item's hidden variable and updates the grade_item.
5fad5061 1183 * Method named after grade_item::set_hidden().
1184 * @param int $hidden 0, 1 or a timestamp int(10) after which date the item will be hidden.
f60c61b1 1185 * @param boolean $cascade apply to child objects too
5fad5061 1186 * @return void
1187 */
f60c61b1 1188 function set_hidden($hidden, $cascade=false) {
5fad5061 1189 $this->load_grade_item();
22e23c78 1190 $this->grade_item->set_hidden($hidden);
f60c61b1 1191 if ($cascade) {
f3ac8eb4 1192 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
f60c61b1 1193 foreach($children as $child) {
1194 $child->set_hidden($hidden, $cascade);
1195 }
f13002d5 1196 }
f3ac8eb4 1197 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
f60c61b1 1198 foreach($children as $child) {
1199 $child->set_hidden($hidden, $cascade);
1200 }
f13002d5 1201 }
1202 }
5fad5061 1203 }
89a5f827 1204
190af29f 1205 /**
1206 * Applies default settings on this category
1207 * @return bool true if anything changed
1208 */
1209 function apply_default_settings() {
1210 global $CFG;
1211
1212 foreach ($this->forceable as $property) {
1213 if (isset($CFG->{"grade_$property"})) {
1214 if ($CFG->{"grade_$property"} == -1) {
1215 continue; //temporary bc before version bump
1216 }
1217 $this->$property = $CFG->{"grade_$property"};
1218 }
1219 }
1220 }
1221
89a5f827 1222 /**
1223 * Applies forced settings on this category
1224 * @return bool true if anything changed
1225 */
1226 function apply_forced_settings() {
1227 global $CFG;
1228
1229 $updated = false;
1230 foreach ($this->forceable as $property) {
190af29f 1231 if (isset($CFG->{"grade_$property"}) and isset($CFG->{"grade_{$property}_flag"}) and ((int)$CFG->{"grade_{$property}_flag"} & 1)) {
1232 if ($CFG->{"grade_$property"} == -1) {
1233 continue; //temporary bc before version bump
1234 }
89a5f827 1235 $this->$property = $CFG->{"grade_$property"};
1236 $updated = true;
1237 }
1238 }
1239
1240 return $updated;
1241 }
1242
1243 /**
1244 * Notification of change in forced category settings.
1245 * @static
1246 */
1247 function updated_forced_settings() {
1248 global $CFG;
1249 $sql = "UPDATE {$CFG->prefix}grade_items SET needsupdate=1 WHERE itemtype='course' or itemtype='category'";
f3ac8eb4 1250 execute_sql($sql, false);
89a5f827 1251 }
4a490db0 1252}
8a31e65c 1253?>