Wood-ize groupinfo boxes. Merged from 19_STABLE
[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;
8f6fdf43 318 $this->fullname = '?';
1f0e4921 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) {
66690b69 472 $oldfinalgrade = $oldgrade->finalgrade;
f3ac8eb4 473 $grade = new grade_grade($oldgrade, false);
f8e6e4db 474 $grade->grade_item =& $this->grade_item;
b8ff92b6 475
f8e6e4db 476 } else {
477 // insert final grade - it will be needed later anyway
f3ac8eb4 478 $grade = new grade_grade(array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
f8e6e4db 479 $grade->grade_item =& $this->grade_item;
f1ad9e04 480 $grade->insert('system');
481 $oldfinalgrade = null;
f8e6e4db 482 }
f1ad9e04 483
c86caae7 484 // no need to recalculate locked or overridden grades
485 if ($grade->is_locked() or $grade->is_overridden()) {
2cc4b0f9 486 return;
ac9b0805 487 }
488
f8e6e4db 489 // can not use own final category grade in calculation
9580a21f 490 unset($grade_values[$this->grade_item->id]);
f8e6e4db 491
f1ad9e04 492
493 /// sum is a special aggregation types - it adjusts the min max, does not use relative values
494 if ($this->aggregation == GRADE_AGGREGATE_SUM) {
495 $this->sum_grades($grade, $oldfinalgrade, $items, $grade_values, $excluded);
496 return;
497 }
498
0758a08e 499 // if no grades calculation possible or grading not allowed clear final grade
9580a21f 500 if (empty($grade_values) or empty($items) or ($this->grade_item->gradetype != GRADE_TYPE_VALUE and $this->grade_item->gradetype != GRADE_TYPE_SCALE)) {
f8e6e4db 501 $grade->finalgrade = null;
66690b69 502 if (!is_null($oldfinalgrade)) {
0758a08e 503 $grade->update('aggregation');
f8e6e4db 504 }
b8ff92b6 505 return;
506 }
0758a08e 507
f8e6e4db 508 /// normalize the grades first - all will have value 0...1
d5f0aa01 509 // ungraded items are not used in aggregation
23207a1a 510 foreach ($grade_values as $itemid=>$v) {
b8ff92b6 511 if (is_null($v)) {
512 // null means no grade
23207a1a 513 unset($grade_values[$itemid]);
514 continue;
515 } else if (in_array($itemid, $excluded)) {
516 unset($grade_values[$itemid]);
b8ff92b6 517 continue;
0aa32279 518 }
9a68cffc 519 $grade_values[$itemid] = grade_grade::standardise_score($v, $items[$itemid]->grademin, $items[$itemid]->grademax, 0, 1);
0aa32279 520 }
dda0c7e6 521
eacd3700 522 // use min grade if grade missing for these types
c2efb501 523 if (!$this->aggregateonlygraded) {
524 foreach($items as $itemid=>$value) {
525 if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
526 $grade_values[$itemid] = 0;
eacd3700 527 }
c2efb501 528 }
eacd3700 529 }
530
531 // limit and sort
9580a21f 532 $this->apply_limit_rules($grade_values);
533 asort($grade_values, SORT_NUMERIC);
4a490db0 534
d5f0aa01 535 // let's see we have still enough grades to do any statistics
9580a21f 536 if (count($grade_values) == 0) {
ac9b0805 537 // not enough attempts yet
f8e6e4db 538 $grade->finalgrade = null;
66690b69 539 if (!is_null($oldfinalgrade)) {
0758a08e 540 $grade->update('aggregation');
b8ff92b6 541 }
542 return;
543 }
2df71235 544
d297269d 545 // do the maths
546 $agg_grade = $this->aggregate_values($grade_values, $items);
547
0758a08e 548 // recalculate the grade back to requested range
9a68cffc 549 $finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $this->grade_item->grademin, $this->grade_item->grademax);
d297269d 550
f1ad9e04 551 if (is_null($finalgrade)) {
552 $grade->finalgrade = null;
d297269d 553 } else {
f1ad9e04 554 $grade->finalgrade = (float)bounded_number($this->grade_item->grademin, $finalgrade, $this->grade_item->grademax);
d297269d 555 }
556
557 // update in db if changed
66690b69 558 if (grade_floatval($grade->finalgrade) !== grade_floatval($oldfinalgrade)) {
0758a08e 559 $grade->update('aggregation');
d297269d 560 }
561
562 return;
563 }
564
89a5f827 565 /**
566 * Internal function - aggregation maths.
567 */
d297269d 568 function aggregate_values($grade_values, $items) {
b8ff92b6 569 switch ($this->aggregation) {
c2efb501 570 case GRADE_AGGREGATE_MEDIAN: // Middle point value in the set: ignores frequencies
9580a21f 571 $num = count($grade_values);
c186c7b2 572 $grades = array_values($grade_values);
573 if ($num % 2 == 0) {
9c8d38fa 574 $agg_grade = ($grades[intval($num/2)-1] + $grades[intval($num/2)]) / 2;
b8ff92b6 575 } else {
9c8d38fa 576 $agg_grade = $grades[intval(($num/2)-0.5)];
b8ff92b6 577 }
578 break;
ac9b0805 579
c2efb501 580 case GRADE_AGGREGATE_MIN:
9c8d38fa 581 $agg_grade = reset($grade_values);
b8ff92b6 582 break;
583
c2efb501 584 case GRADE_AGGREGATE_MAX:
9c8d38fa 585 $agg_grade = array_pop($grade_values);
b8ff92b6 586 break;
587
c2efb501 588 case GRADE_AGGREGATE_MODE: // the most common value, average used if multimode
9580a21f 589 $freq = array_count_values($grade_values);
95affb8a 590 arsort($freq); // sort by frequency keeping keys
591 $top = reset($freq); // highest frequency count
592 $modes = array_keys($freq, $top); // search for all modes (have the same highest count)
593 rsort($modes, SORT_NUMERIC); // get highes mode
9c8d38fa 594 $agg_grade = reset($modes);
d5fab31f 595 break;
95affb8a 596
1426edac 597 case GRADE_AGGREGATE_WEIGHTED_MEAN: // Weighted average of all existing final grades, weight specified in coef
9580a21f 598 $weightsum = 0;
599 $sum = 0;
eacd3700 600 foreach($grade_values as $itemid=>$grade_value) {
601 if ($items[$itemid]->aggregationcoef <= 0) {
9580a21f 602 continue;
603 }
eacd3700 604 $weightsum += $items[$itemid]->aggregationcoef;
605 $sum += $items[$itemid]->aggregationcoef * $grade_value;
9580a21f 606 }
607 if ($weightsum == 0) {
9c8d38fa 608 $agg_grade = null;
9580a21f 609 } else {
9c8d38fa 610 $agg_grade = $sum / $weightsum;
9580a21f 611 }
612 break;
613
1426edac 614 case GRADE_AGGREGATE_WEIGHTED_MEAN2: // Weighted average of all existing final grades, weight is the range of grade (ususally grademax)
615 $weightsum = 0;
616 $sum = 0;
617 foreach($grade_values as $itemid=>$grade_value) {
618 $weight = $items[$itemid]->grademax - $items[$itemid]->grademin;
619 if ($weight <= 0) {
620 continue;
621 }
622 $weightsum += $weight;
623 $sum += $weight * $grade_value;
624 }
625 if ($weightsum == 0) {
626 $agg_grade = null;
627 } else {
628 $agg_grade = $sum / $weightsum;
629 }
630 break;
631
c2efb501 632 case GRADE_AGGREGATE_EXTRACREDIT_MEAN: // special average
9580a21f 633 $num = 0;
634 $sum = 0;
eacd3700 635 foreach($grade_values as $itemid=>$grade_value) {
636 if ($items[$itemid]->aggregationcoef == 0) {
9580a21f 637 $num += 1;
638 $sum += $grade_value;
eacd3700 639 } else if ($items[$itemid]->aggregationcoef > 0) {
640 $sum += $items[$itemid]->aggregationcoef * $grade_value;
9580a21f 641 }
642 }
643 if ($num == 0) {
9c8d38fa 644 $agg_grade = $sum; // only extra credits or wrong coefs
9580a21f 645 } else {
9c8d38fa 646 $agg_grade = $sum / $num;
9580a21f 647 }
648 break;
649
c2efb501 650 case GRADE_AGGREGATE_MEAN: // Arithmetic average of all grade items (if ungraded aggregated, NULL counted as minimum)
ac9b0805 651 default:
9580a21f 652 $num = count($grade_values);
653 $sum = array_sum($grade_values);
9c8d38fa 654 $agg_grade = $sum / $num;
b8ff92b6 655 break;
656 }
657
d297269d 658 return $agg_grade;
0aa32279 659 }
0758a08e 660
0758a08e 661 /**
662 * internal function for category grades summing
663 *
f1ad9e04 664 * @param object $grade
0758a08e 665 * @param int $userid
f1ad9e04 666 * @param float $oldfinalgrade
0758a08e 667 * @param array $items
668 * @param array $grade_values
0758a08e 669 * @param bool $excluded
670 * @return boolean (just plain return;)
671 */
f1ad9e04 672 function sum_grades(&$grade, $oldfinalgrade, $items, $grade_values, $excluded) {
0758a08e 673 // ungraded and exluded items are not used in aggregation
674 foreach ($grade_values as $itemid=>$v) {
675 if (is_null($v)) {
676 unset($grade_values[$itemid]);
677 } else if (in_array($itemid, $excluded)) {
678 unset($grade_values[$itemid]);
679 }
680 }
681
d28f25a4 682 // use 0 if grade missing, droplow used and aggregating all items
683 if (!$this->aggregateonlygraded and !empty($this->droplow)) {
684 foreach($items as $itemid=>$value) {
685 if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
686 $grade_values[$itemid] = 0;
687 }
688 }
689 }
690
0758a08e 691 $max = 0;
692
693 //find max grade
694 foreach ($items as $item) {
2e0d37fe 695 if ($item->aggregationcoef > 0) {
696 // extra credit from this activity - does not affect total
697 continue;
0758a08e 698 }
91f9a62c 699 if ($item->gradetype == GRADE_TYPE_VALUE) {
700 $max += $item->grademax;
701 } else if ($item->gradetype == GRADE_TYPE_SCALE) {
702 $max += $item->grademax - 1; // scales min is 1
703 }
0758a08e 704 }
705
706 if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE){
707 $this->grade_item->grademax = $max;
708 $this->grade_item->grademin = 0;
709 $this->grade_item->gradetype = GRADE_TYPE_VALUE;
710 $this->grade_item->update('aggregation');
711 }
712
713 $this->apply_limit_rules($grade_values);
714
715 $sum = array_sum($grade_values);
66690b69 716 $grade->finalgrade = bounded_number($this->grade_item->grademin, $sum, $this->grade_item->grademax);
0758a08e 717
718 // update in db if changed
66690b69 719 if (grade_floatval($grade->finalgrade) !== grade_floatval($oldfinalgrade)) {
0758a08e 720 $grade->update('aggregation');
721 }
722
723 return;
724 }
725
adc2f286 726 /**
727 * Given an array of grade values (numerical indices), applies droplow or keephigh
728 * rules to limit the final array.
9580a21f 729 * @param array $grade_values
adc2f286 730 * @return array Limited grades.
731 */
9580a21f 732 function apply_limit_rules(&$grade_values) {
733 arsort($grade_values, SORT_NUMERIC);
adc2f286 734 if (!empty($this->droplow)) {
735 for ($i = 0; $i < $this->droplow; $i++) {
9580a21f 736 array_pop($grade_values);
adc2f286 737 }
4a490db0 738 } elseif (!empty($this->keephigh)) {
9580a21f 739 while (count($grade_values) > $this->keephigh) {
740 array_pop($grade_values);
adc2f286 741 }
742 }
0aa32279 743 }
744
9580a21f 745
746 /**
747 * Returns true if category uses special aggregation coeficient
748 * @return boolean true if coeficient used
749 */
750 function is_aggregationcoef_used() {
c2efb501 751 return ($this->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN
2e0d37fe 752 or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN
753 or $this->aggregation == GRADE_AGGREGATE_SUM);
ba74762b 754
9580a21f 755 }
756
1c307f21 757 /**
b3ac6c3e 758 * Returns tree with all grade_items and categories as elements
759 * @static
760 * @param int $courseid
b3ac6c3e 761 * @param boolean $include_category_items as category children
762 * @return array
1c307f21 763 */
938e00b6 764 function fetch_course_tree($courseid, $include_category_items=false) {
f3ac8eb4 765 $course_category = grade_category::fetch_course_category($courseid);
514a3467 766 $category_array = array('object'=>$course_category, 'type'=>'category', 'depth'=>1,
767 'children'=>$course_category->get_children($include_category_items));
768 $sortorder = 1;
769 $course_category->set_sortorder($sortorder);
770 $course_category->sortorder = $sortorder;
f3ac8eb4 771 return grade_category::_fetch_course_tree_recursion($category_array, $sortorder);
1c307f21 772 }
773
b3ac6c3e 774 function _fetch_course_tree_recursion($category_array, &$sortorder) {
775 // update the sortorder in db if needed
776 if ($category_array['object']->sortorder != $sortorder) {
777 $category_array['object']->set_sortorder($sortorder);
ce385eb4 778 }
ce385eb4 779
314c4336 780 // store the grade_item or grade_category instance with extra info
781 $result = array('object'=>$category_array['object'], 'type'=>$category_array['type'], 'depth'=>$category_array['depth']);
b3ac6c3e 782
783 // reuse final grades if there
784 if (array_key_exists('finalgrades', $category_array)) {
785 $result['finalgrades'] = $category_array['finalgrades'];
786 }
787
788 // recursively resort children
789 if (!empty($category_array['children'])) {
790 $result['children'] = array();
29d509f5 791 //process the category item first
792 $cat_item_id = null;
b3ac6c3e 793 foreach($category_array['children'] as $oldorder=>$child_array) {
314c4336 794 if ($child_array['type'] == 'courseitem' or $child_array['type'] == 'categoryitem') {
f3ac8eb4 795 $result['children'][$sortorder] = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
29d509f5 796 }
2b0f65e2 797 }
29d509f5 798 foreach($category_array['children'] as $oldorder=>$child_array) {
799 if ($child_array['type'] != 'courseitem' and $child_array['type'] != 'categoryitem') {
f3ac8eb4 800 $result['children'][++$sortorder] = grade_category::_fetch_course_tree_recursion($child_array, $sortorder);
b3ac6c3e 801 }
802 }
803 }
804
805 return $result;
ce385eb4 806 }
7c8a963f 807
808 /**
4a490db0 809 * Fetches and returns all the children categories and/or grade_items belonging to this category.
810 * By default only returns the immediate children (depth=1), but deeper levels can be requested,
a39cac25 811 * as well as all levels (0). The elements are indexed by sort order.
7c8a963f 812 * @return array Array of child objects (grade_category and grade_item).
813 */
938e00b6 814 function get_children($include_category_items=false) {
b3ac6c3e 815
816 // This function must be as fast as possible ;-)
817 // fetch all course grade items and categories into memory - we do not expect hundreds of these in course
818 // we have to limit the number of queries though, because it will be used often in grade reports
819
f3ac8eb4 820 $cats = get_records('grade_categories', 'courseid', $this->courseid);
821 $items = get_records('grade_items', 'courseid', $this->courseid);
4a490db0 822
b3ac6c3e 823 // init children array first
824 foreach ($cats as $catid=>$cat) {
825 $cats[$catid]->children = array();
27f95e9b 826 }
4a490db0 827
b3ac6c3e 828 //first attach items to cats and add category sortorder
829 foreach ($items as $item) {
830 if ($item->itemtype == 'course' or $item->itemtype == 'category') {
831 $cats[$item->iteminstance]->sortorder = $item->sortorder;
4a490db0 832
b3ac6c3e 833 if (!$include_category_items) {
834 continue;
835 }
836 $categoryid = $item->iteminstance;
837 } else {
838 $categoryid = $item->categoryid;
839 }
840
841 // prevent problems with duplicate sortorders in db
842 $sortorder = $item->sortorder;
843 while(array_key_exists($sortorder, $cats[$categoryid]->children)) {
f13002d5 844 //debugging("$sortorder exists in item loop");
b3ac6c3e 845 $sortorder++;
846 }
847
848 $cats[$categoryid]->children[$sortorder] = $item;
849
850 }
851
852 // now find the requested category and connect categories as children
853 $category = false;
854 foreach ($cats as $catid=>$cat) {
855 if (!empty($cat->parent)) {
856 // prevent problems with duplicate sortorders in db
857 $sortorder = $cat->sortorder;
858 while(array_key_exists($sortorder, $cats[$cat->parent]->children)) {
f13002d5 859 //debugging("$sortorder exists in cat loop");
b3ac6c3e 860 $sortorder++;
861 }
862
65370356 863 $cats[$cat->parent]->children[$sortorder] = &$cats[$catid];
b3ac6c3e 864 }
f3ac8eb4 865
b3ac6c3e 866 if ($catid == $this->id) {
867 $category = &$cats[$catid];
868 }
869 }
870
871 unset($items); // not needed
872 unset($cats); // not needed
873
f3ac8eb4 874 $children_array = grade_category::_get_children_recursion($category);
b3ac6c3e 875
876 ksort($children_array);
877
878 return $children_array;
879
880 }
881
938e00b6 882 function _get_children_recursion($category) {
b3ac6c3e 883
884 $children_array = array();
885 foreach($category->children as $sortorder=>$child) {
886 if (array_key_exists('itemtype', $child)) {
f3ac8eb4 887 $grade_item = new grade_item($child, false);
4faf5f99 888 if (in_array($grade_item->itemtype, array('course', 'category'))) {
889 $type = $grade_item->itemtype.'item';
890 $depth = $category->depth;
314c4336 891 } else {
892 $type = 'item';
893 $depth = $category->depth; // we use this to set the same colour
b3ac6c3e 894 }
4faf5f99 895 $children_array[$sortorder] = array('object'=>$grade_item, 'type'=>$type, 'depth'=>$depth);
4a490db0 896
7c8a963f 897 } else {
f3ac8eb4 898 $children = grade_category::_get_children_recursion($child);
899 $grade_category = new grade_category($child, false);
b3ac6c3e 900 if (empty($children)) {
314c4336 901 $children = array();
7c8a963f 902 }
4faf5f99 903 $children_array[$sortorder] = array('object'=>$grade_category, 'type'=>'category', 'depth'=>$grade_category->depth, 'children'=>$children);
314c4336 904 }
27f95e9b 905 }
906
b3ac6c3e 907 // sort the array
908 ksort($children_array);
909
27f95e9b 910 return $children_array;
911 }
4a490db0 912
f151b073 913 /**
ab53054f 914 * Uses get_grade_item to load or create a grade_item, then saves it as $this->grade_item.
f151b073 915 * @return object Grade_item
916 */
917 function load_grade_item() {
ac9b0805 918 if (empty($this->grade_item)) {
919 $this->grade_item = $this->get_grade_item();
920 }
ab53054f 921 return $this->grade_item;
922 }
4a490db0 923
ab53054f 924 /**
925 * Retrieves from DB and instantiates the associated grade_item object.
926 * If no grade_item exists yet, create one.
927 * @return object Grade_item
928 */
929 function get_grade_item() {
c91ed4be 930 if (empty($this->id)) {
931 debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set.");
932 return false;
933 }
934
b3ac6c3e 935 if (empty($this->parent)) {
936 $params = array('courseid'=>$this->courseid, 'itemtype'=>'course', 'iteminstance'=>$this->id);
937
938 } else {
939 $params = array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id);
940 }
4ac209d5 941
f3ac8eb4 942 if (!$grade_items = grade_item::fetch_all($params)) {
b8ff92b6 943 // create a new one
f3ac8eb4 944 $grade_item = new grade_item($params, false);
b8ff92b6 945 $grade_item->gradetype = GRADE_TYPE_VALUE;
f8e6e4db 946 $grade_item->insert('system');
4a490db0 947
b8ff92b6 948 } else if (count($grade_items) == 1){
949 // found existing one
950 $grade_item = reset($grade_items);
4a490db0 951
b8ff92b6 952 } else {
953 debugging("Found more than one grade_item attached to category id:".$this->id);
ac9b0805 954 // return first one
955 $grade_item = reset($grade_items);
2c72af1f 956 }
957
ab53054f 958 return $grade_item;
f151b073 959 }
8c846243 960
961 /**
962 * Uses $this->parent to instantiate $this->parent_category based on the
963 * referenced record in the DB.
964 * @return object Parent_category
965 */
966 function load_parent_category() {
967 if (empty($this->parent_category) && !empty($this->parent)) {
ab53054f 968 $this->parent_category = $this->get_parent_category();
8c846243 969 }
970 return $this->parent_category;
4a490db0 971 }
972
ab53054f 973 /**
974 * Uses $this->parent to instantiate and return a grade_category object.
975 * @return object Parent_category
976 */
977 function get_parent_category() {
978 if (!empty($this->parent)) {
f3ac8eb4 979 $parent_category = new grade_category(array('id' => $this->parent));
4a490db0 980 return $parent_category;
ab53054f 981 } else {
982 return null;
983 }
984 }
985
2186f72c 986 /**
4a490db0 987 * Returns the most descriptive field for this object. This is a standard method used
2186f72c 988 * when we do not know the exact type of an object.
989 * @return string name
990 */
991 function get_name() {
8f6fdf43 992 // For a course category, we return the course name if the fullname is set to '?' in the DB (empty in the category edit form)
993 if (empty($this->parent) && $this->fullname == '?') {
f3ac8eb4 994 $course = get_record('course', 'id', $this->courseid);
410753fb 995 return format_string($course->fullname);
314c4336 996 } else {
997 return $this->fullname;
998 }
2186f72c 999 }
c91ed4be 1000
0fc7f624 1001 /**
1002 * Sets this category's parent id. A generic method shared by objects that have a parent id of some kind.
f13002d5 1003 * @param int parentid
1004 * @return boolean success
0fc7f624 1005 */
f8e6e4db 1006 function set_parent($parentid, $source=null) {
f13002d5 1007 if ($this->parent == $parentid) {
1008 return true;
1009 }
1010
1011 if ($parentid == $this->id) {
1012 error('Can not assign self as parent!');
1013 }
1014
1015 if (empty($this->parent) and $this->is_course_category()) {
1016 error('Course category can not have parent!');
b3ac6c3e 1017 }
f13002d5 1018
1019 // find parent and check course id
f3ac8eb4 1020 if (!$parent_category = grade_category::fetch(array('id'=>$parentid, 'courseid'=>$this->courseid))) {
b3ac6c3e 1021 return false;
1022 }
1023
f8e6e4db 1024 $this->force_regrading();
b3ac6c3e 1025
1026 // set new parent category
f8e6e4db 1027 $this->parent = $parent_category->id;
1028 $this->parent_category =& $parent_category;
b3ac6c3e 1029 $this->path = null; // remove old path and depth - will be recalculated in update()
f8e6e4db 1030 $this->depth = null; // remove old path and depth - will be recalculated in update()
1031 $this->update($source);
b3ac6c3e 1032
15b462da 1033 return $this->update($source);
b3ac6c3e 1034 }
1035
1036 /**
1037 * Returns the final values for this grade category.
1038 * @param int $userid Optional: to retrieve a single final grade
1039 * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade.
1040 */
1041 function get_final($userid=NULL) {
1042 $this->load_grade_item();
1043 return $this->grade_item->get_final($userid);
0fc7f624 1044 }
4a490db0 1045
0fc7f624 1046 /**
4a490db0 1047 * Returns the sortorder of the associated grade_item. This method is also available in
5fad5061 1048 * grade_item, for cases where the object type is not known.
0fc7f624 1049 * @return int Sort order
1050 */
1051 function get_sortorder() {
b3ac6c3e 1052 $this->load_grade_item();
1053 return $this->grade_item->get_sortorder();
0fc7f624 1054 }
1055
be7c0693 1056 /**
1057 * Returns the idnumber of the associated grade_item. This method is also available in
1058 * grade_item, for cases where the object type is not known.
1059 * @return string idnumber
1060 */
1061 function get_idnumber() {
1062 $this->load_grade_item();
1063 return $this->grade_item->get_idnumber();
1064 }
1065
0fc7f624 1066 /**
b3ac6c3e 1067 * Sets sortorder variable for this category.
4a490db0 1068 * This method is also available in grade_item, for cases where the object type is not know.
0fc7f624 1069 * @param int $sortorder
1070 * @return void
1071 */
1072 function set_sortorder($sortorder) {
b3ac6c3e 1073 $this->load_grade_item();
1074 $this->grade_item->set_sortorder($sortorder);
1075 }
1076
6639ead3 1077 /**
1078 * Move this category after the given sortorder - does not change the parent
1079 * @param int $sortorder to place after
1080 */
f13002d5 1081 function move_after_sortorder($sortorder) {
1082 $this->load_grade_item();
1083 $this->grade_item->move_after_sortorder($sortorder);
1084 }
1085
b3ac6c3e 1086 /**
f13002d5 1087 * Return true if this is the top most category that represents the total course grade.
b3ac6c3e 1088 * @return boolean
1089 */
1090 function is_course_category() {
1091 $this->load_grade_item();
1092 return $this->grade_item->is_course_item();
1093 }
1094
1095 /**
1096 * Return the top most course category.
1097 * @static
1098 * @return object grade_category instance for course grade
1099 */
1100 function fetch_course_category($courseid) {
a4503119 1101 if (empty($courseid)) {
1102 debugging('Missing course id!');
1103 return false;
1104 }
b3ac6c3e 1105
1106 // course category has no parent
f3ac8eb4 1107 if ($course_category = grade_category::fetch(array('courseid'=>$courseid, 'parent'=>null))) {
b3ac6c3e 1108 return $course_category;
1109 }
1110
1111 // create a new one
f3ac8eb4 1112 $course_category = new grade_category();
b3ac6c3e 1113 $course_category->insert_course_category($courseid);
1114
1115 return $course_category;
0fc7f624 1116 }
4ac209d5 1117
79eabc2a 1118 /**
1119 * Is grading object editable?
1120 * @return boolean
1121 */
1122 function is_editable() {
1123 return true;
1124 }
1125
5fad5061 1126 /**
4a490db0 1127 * Returns the locked state/date of the associated grade_item. This method is also available in
1128 * grade_item, for cases where the object type is not known.
22e23c78 1129 * @return boolean
5fad5061 1130 */
2cc4b0f9 1131 function is_locked() {
5fad5061 1132 $this->load_grade_item();
22e23c78 1133 return $this->grade_item->is_locked();
5fad5061 1134 }
1135
1136 /**
1137 * Sets the grade_item's locked variable and updates the grade_item.
1138 * Method named after grade_item::set_locked().
1139 * @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
fb0e3570 1140 * @param boolean $cascade lock/unlock child objects too
2b0f65e2 1141 * @param boolean $refresh refresh grades when unlocking
9580a21f 1142 * @return boolean success if category locked (not all children mayb be locked though)
5fad5061 1143 */
fb0e3570 1144 function set_locked($lockedstate, $cascade=false, $refresh=true) {
5fad5061 1145 $this->load_grade_item();
2b0f65e2 1146
fb0e3570 1147 $result = $this->grade_item->set_locked($lockedstate, $cascade, true);
1148
1149 if ($cascade) {
1150 //process all children - items and categories
f3ac8eb4 1151 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
fb0e3570 1152 foreach($children as $child) {
1153 $child->set_locked($lockedstate, true, false);
1154 if (empty($lockedstate) and $refresh) {
1155 //refresh when unlocking
1156 $child->refresh_grades();
1157 }
2b0f65e2 1158 }
7a7a53d3 1159 }
f3ac8eb4 1160 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
fb0e3570 1161 foreach($children as $child) {
1162 $child->set_locked($lockedstate, true, true);
1163 }
7a7a53d3 1164 }
1165 }
2b0f65e2 1166
b121b544 1167 return $result;
5fad5061 1168 }
4a490db0 1169
5fad5061 1170 /**
4a490db0 1171 * Returns the hidden state/date of the associated grade_item. This method is also available in
22e23c78 1172 * grade_item.
1173 * @return boolean
5fad5061 1174 */
22e23c78 1175 function is_hidden() {
5fad5061 1176 $this->load_grade_item();
22e23c78 1177 return $this->grade_item->is_hidden();
5fad5061 1178 }
1179
597f50e6 1180 /**
1181 * Check grade hidden status. Uses data from both grade item and grade.
1182 * @return boolean true if hiddenuntil, false if not
1183 */
1184 function is_hiddenuntil() {
1185 $this->load_grade_item();
1186 return $this->grade_item->is_hiddenuntil();
1187 }
1188
5fad5061 1189 /**
4a490db0 1190 * Sets the grade_item's hidden variable and updates the grade_item.
5fad5061 1191 * Method named after grade_item::set_hidden().
1192 * @param int $hidden 0, 1 or a timestamp int(10) after which date the item will be hidden.
f60c61b1 1193 * @param boolean $cascade apply to child objects too
5fad5061 1194 * @return void
1195 */
f60c61b1 1196 function set_hidden($hidden, $cascade=false) {
5fad5061 1197 $this->load_grade_item();
22e23c78 1198 $this->grade_item->set_hidden($hidden);
f60c61b1 1199 if ($cascade) {
f3ac8eb4 1200 if ($children = grade_item::fetch_all(array('categoryid'=>$this->id))) {
f60c61b1 1201 foreach($children as $child) {
1202 $child->set_hidden($hidden, $cascade);
1203 }
f13002d5 1204 }
f3ac8eb4 1205 if ($children = grade_category::fetch_all(array('parent'=>$this->id))) {
f60c61b1 1206 foreach($children as $child) {
1207 $child->set_hidden($hidden, $cascade);
1208 }
f13002d5 1209 }
1210 }
5fad5061 1211 }
89a5f827 1212
190af29f 1213 /**
1214 * Applies default settings on this category
1215 * @return bool true if anything changed
1216 */
1217 function apply_default_settings() {
1218 global $CFG;
1219
1220 foreach ($this->forceable as $property) {
1221 if (isset($CFG->{"grade_$property"})) {
1222 if ($CFG->{"grade_$property"} == -1) {
1223 continue; //temporary bc before version bump
1224 }
1225 $this->$property = $CFG->{"grade_$property"};
1226 }
1227 }
1228 }
1229
89a5f827 1230 /**
1231 * Applies forced settings on this category
1232 * @return bool true if anything changed
1233 */
1234 function apply_forced_settings() {
1235 global $CFG;
1236
1237 $updated = false;
1238 foreach ($this->forceable as $property) {
190af29f 1239 if (isset($CFG->{"grade_$property"}) and isset($CFG->{"grade_{$property}_flag"}) and ((int)$CFG->{"grade_{$property}_flag"} & 1)) {
1240 if ($CFG->{"grade_$property"} == -1) {
1241 continue; //temporary bc before version bump
1242 }
89a5f827 1243 $this->$property = $CFG->{"grade_$property"};
1244 $updated = true;
1245 }
1246 }
1247
1248 return $updated;
1249 }
1250
1251 /**
1252 * Notification of change in forced category settings.
1253 * @static
1254 */
1255 function updated_forced_settings() {
1256 global $CFG;
1257 $sql = "UPDATE {$CFG->prefix}grade_items SET needsupdate=1 WHERE itemtype='course' or itemtype='category'";
f3ac8eb4 1258 execute_sql($sql, false);
89a5f827 1259 }
4a490db0 1260}
8a31e65c 1261?>