MDL-12182 More grade_item unit tests
[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 {
9a68cffc 160 $obj = grade_object::get_instance('grade_category');
161 $parent = $obj->lib_wrapper->get_record('grade_categories', 'id', $grade_category->parent);
b7cf8eac 162
9a68cffc 163 return grade_category::build_path($parent).$grade_category->id.'/';
ce385eb4 164 }
e5c674f1 165 }
166
8a31e65c 167 /**
f92dcad8 168 * Finds and returns a grade_category instance based on params.
61c33818 169 * @static
8a31e65c 170 *
f92dcad8 171 * @param array $params associative arrays varname=>value
172 * @return object grade_category instance or false if none found.
173 */
174 function fetch($params) {
795bee34 175 $obj = grade_object::get_instance('grade_category');
176 return $obj->fetch_helper('grade_categories', 'grade_category', $params);
f92dcad8 177 }
178
179 /**
180 * Finds and returns all grade_category instances based on params.
181 * @static
182 *
183 * @param array $params associative arrays varname=>value
184 * @return array array of grade_category insatnces or false if none found.
185 */
186 function fetch_all($params) {
795bee34 187 $obj = grade_object::get_instance('grade_category');
188 return $obj->fetch_all_helper('grade_categories', 'grade_category', $params);
ce385eb4 189 }
190
8f4a626d 191 /**
2cc4b0f9 192 * In addition to update() as defined in grade_object, call force_regrading of parent categories, if applicable.
aaff71da 193 * @param string $source from where was the object updated (mod/forum, manual, etc.)
194 * @return boolean success
8f4a626d 195 */
aaff71da 196 function update($source=null) {
b3ac6c3e 197 // load the grade item or create a new one
198 $this->load_grade_item();
199
200 // force recalculation of path;
201 if (empty($this->path)) {
9a68cffc 202 $this->path = grade_category::build_path($this);
c2efb501 203 $this->depth = substr_count($this->path, '/') - 1;
4a490db0 204 }
0fc7f624 205
89a5f827 206 $this->apply_forced_settings();
207
208 // these are exclusive
209 if ($this->droplow > 0) {
210 $this->keephigh = 0;
211 } else if ($this->keephigh > 0) {
212 $this->droplow = 0;
213 }
4a490db0 214
b3ac6c3e 215 // Recalculate grades if needed
216 if ($this->qualifies_for_regrading()) {
f8e6e4db 217 $this->force_regrading();
4a490db0 218 }
f8e6e4db 219
ced5ee59 220 $this->timemodified = time();
221
f8e6e4db 222 return parent::update($source);
8f4a626d 223 }
4a490db0 224
8f4a626d 225 /**
2cc4b0f9 226 * If parent::delete() is successful, send force_regrading message to parent category.
aaff71da 227 * @param string $source from where was the object deleted (mod/forum, manual, etc.)
228 * @return boolean success
8f4a626d 229 */
aaff71da 230 function delete($source=null) {
f13002d5 231 $grade_item = $this->load_grade_item();
aaefeda4 232 $obj = grade_object::get_instance('grade_category');
4a490db0 233
f615fbab 234 if ($this->is_course_category()) {
aaefeda4 235 if ($categories = $obj->fetch_all(array('courseid'=>$this->courseid))) {
f615fbab 236 foreach ($categories as $category) {
237 if ($category->id == $this->id) {
238 continue; // do not delete course category yet
239 }
240 $category->delete($source);
241 }
aaff71da 242 }
2b0f65e2 243
4fc9ec1e 244 if ($items = $grade_item->fetch_all(array('courseid'=>$this->courseid))) {
f615fbab 245 foreach ($items as $item) {
246 if ($item->id == $grade_item->id) {
247 continue; // do not delete course item yet
248 }
249 $item->delete($source);
250 }
251 }
252
253 } else {
254 $this->force_regrading();
2b0f65e2 255
f615fbab 256 $parent = $this->load_parent_category();
2b0f65e2 257
f615fbab 258 // Update children's categoryid/parent field first
4fc9ec1e 259 if ($children = $grade_item->fetch_all(array('categoryid'=>$this->id))) {
f615fbab 260 foreach ($children as $child) {
261 $child->set_parent($parent->id);
262 }
263 }
aaefeda4 264 if ($children = $obj->fetch_all(array('parent'=>$this->id))) {
f615fbab 265 foreach ($children as $child) {
266 $child->set_parent($parent->id);
267 }
aaff71da 268 }
269 }
f13002d5 270
aaff71da 271 // first delete the attached grade item and grades
272 $grade_item->delete($source);
f13002d5 273
274 // delete category itself
aaff71da 275 return parent::delete($source);
8f4a626d 276 }
4a490db0 277
ce385eb4 278 /**
279 * In addition to the normal insert() defined in grade_object, this method sets the depth
280 * and path for this object, and update the record accordingly. The reason why this must
281 * be done here instead of in the constructor, is that they both need to know the record's
4a490db0 282 * id number, which only gets created at insertion time.
f151b073 283 * This method also creates an associated grade_item if this wasn't done during construction.
aaff71da 284 * @param string $source from where was the object inserted (mod/forum, manual, etc.)
285 * @return int PK ID if successful, false otherwise
ce385eb4 286 */
aaff71da 287 function insert($source=null) {
b3ac6c3e 288
289 if (empty($this->courseid)) {
290 error('Can not insert grade category without course id!');
1f0e4921 291 return false;
b8ff92b6 292 }
4a490db0 293
b3ac6c3e 294 if (empty($this->parent)) {
aaefeda4 295 $course_category = $obj->fetch_course_category($this->courseid);
b3ac6c3e 296 $this->parent = $course_category->id;
ce385eb4 297 }
4a490db0 298
b3ac6c3e 299 $this->path = null;
300
ced5ee59 301 $this->timecreated = $this->timemodified = time();
302
aaff71da 303 if (!parent::insert($source)) {
b3ac6c3e 304 debugging("Could not insert this category: " . print_r($this, true));
305 return false;
306 }
307
f8e6e4db 308 $this->force_regrading();
309
b3ac6c3e 310 // build path and depth
aaff71da 311 $this->update($source);
4a490db0 312
aaff71da 313 return $this->id;
b3ac6c3e 314 }
315
f2c88356 316 /**
f615fbab 317 * Internal function - used only from fetch_course_category()
318 * Normal insert() can not be used for course category
319 * @param int $courseid
320 * @return bool success
f2c88356 321 */
b3ac6c3e 322 function insert_course_category($courseid) {
1f0e4921 323 $this->courseid = $courseid;
324 $this->fullname = get_string('coursegradecategory', 'grades');
325 $this->path = null;
326 $this->parent = null;
327 $this->aggregation = GRADE_AGGREGATE_MEAN;
4a490db0 328
190af29f 329 $this->apply_default_settings();
89a5f827 330 $this->apply_forced_settings();
331
ced5ee59 332 $this->timecreated = $this->timemodified = time();
333
aaff71da 334 if (!parent::insert('system')) {
b3ac6c3e 335 debugging("Could not insert this category: " . print_r($this, true));
336 return false;
f151b073 337 }
4a490db0 338
b3ac6c3e 339 // build path and depth
aaff71da 340 $this->update('system');
b3ac6c3e 341
aaff71da 342 return $this->id;
ce385eb4 343 }
4a490db0 344
8f4a626d 345 /**
346 * Compares the values held by this object with those of the matching record in DB, and returns
347 * whether or not these differences are sufficient to justify an update of all parent objects.
348 * This assumes that this object has an id number and a matching record in DB. If not, it will return false.
349 * @return boolean
350 */
2cc4b0f9 351 function qualifies_for_regrading() {
8f4a626d 352 if (empty($this->id)) {
6639ead3 353 debugging("Can not regrade non existing category");
8f4a626d 354 return false;
355 }
aaefeda4 356 $obj = grade_object::get_instance('grade_category');
357 $db_item = $obj->fetch(array('id'=>$this->id));
4a490db0 358
c2efb501 359 $aggregationdiff = $db_item->aggregation != $this->aggregation;
360 $keephighdiff = $db_item->keephigh != $this->keephigh;
361 $droplowdiff = $db_item->droplow != $this->droplow;
362 $aggonlygrddiff = $db_item->aggregateonlygraded != $this->aggregateonlygraded;
363 $aggoutcomesdiff = $db_item->aggregateoutcomes != $this->aggregateoutcomes;
364 $aggsubcatsdiff = $db_item->aggregatesubcats != $this->aggregatesubcats;
8f4a626d 365
c2efb501 366 return ($aggregationdiff || $keephighdiff || $droplowdiff || $aggonlygrddiff || $aggoutcomesdiff || $aggsubcatsdiff);
8f4a626d 367 }
8c846243 368
369 /**
f8e6e4db 370 * Marks the category and course item as needing update - categories are always regraded.
371 * @return void
8c846243 372 */
f8e6e4db 373 function force_regrading() {
374 $grade_item = $this->load_grade_item();
375 $grade_item->force_regrading();
8c846243 376 }
377
0aa32279 378 /**
0758a08e 379 * Generates and saves final grades in associated category grade item.
1994d890 380 * These immediate children must already have their own final grades.
0758a08e 381 * The category's aggregation method is used to generate final grades.
ac9b0805 382 *
383 * Please note that category grade is either calculated or aggregated - not both at the same time.
384 *
c86caae7 385 * This method must be used ONLY from grade_item::regrade_final_grades(),
ac9b0805 386 * because the calculation must be done in correct order!
b8ff92b6 387 *
4a490db0 388 * Steps to follow:
ac9b0805 389 * 1. Get final grades from immediate children
2df71235 390 * 3. Aggregate these grades
0758a08e 391 * 4. Save them in final grades of associated category grade item
0aa32279 392 */
f8e6e4db 393 function generate_grades($userid=null) {
b8ff92b6 394 global $CFG;
4a490db0 395
ac9b0805 396 $this->load_grade_item();
2cc4b0f9 397
398 if ($this->grade_item->is_locked()) {
399 return true; // no need to recalculate locked items
400 }
401
89a5f827 402 // find grade items of immediate children (category or grade items) and force site settings
61c33818 403 $depends_on = $this->grade_item->depends_on();
b3ac6c3e 404
f8e6e4db 405 if (empty($depends_on)) {
406 $items = false;
407 } else {
408 $gis = implode(',', $depends_on);
409 $sql = "SELECT *
410 FROM {$CFG->prefix}grade_items
411 WHERE id IN ($gis)";
4fc9ec1e 412 $items = $this->lib_wrapper->get_records_sql($sql);
f8e6e4db 413 }
4a490db0 414
f8e6e4db 415 if ($userid) {
416 $usersql = "AND g.userid=$userid";
417 } else {
418 $usersql = "";
b8ff92b6 419 }
4a490db0 420
aaefeda4 421 $grade_inst = grade_object::get_instance('grade_grade');
3f2b0c8a 422 $fields = 'g.'.implode(',g.', $grade_inst->required_fields);
423
f8e6e4db 424 // where to look for final grades - include grade of this item too, we will store the results there
61c33818 425 $gis = implode(',', array_merge($depends_on, array($this->grade_item->id)));
3f2b0c8a 426 $sql = "SELECT $fields
ac9b0805 427 FROM {$CFG->prefix}grade_grades g, {$CFG->prefix}grade_items gi
f8e6e4db 428 WHERE gi.id = g.itemid AND gi.id IN ($gis) $usersql
ac9b0805 429 ORDER BY g.userid";
b8ff92b6 430
9580a21f 431 // group the results by userid and aggregate the grades for this user
4fc9ec1e 432 if ($rs = $this->lib_wrapper->get_recordset_sql($sql)) {
03cedd62 433 $prevuser = 0;
434 $grade_values = array();
435 $excluded = array();
436 $oldgrade = null;
4fc9ec1e 437 while ($used = $this->lib_wrapper->rs_fetch_next_record($rs)) {
03cedd62 438 if ($used->userid != $prevuser) {
439 $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);
440 $prevuser = $used->userid;
441 $grade_values = array();
442 $excluded = array();
443 $oldgrade = null;
444 }
445 $grade_values[$used->itemid] = $used->finalgrade;
446 if ($used->excluded) {
447 $excluded[] = $used->itemid;
448 }
449 if ($this->grade_item->id == $used->itemid) {
450 $oldgrade = $used;
2df71235 451 }
b8ff92b6 452 }
03cedd62 453 $this->aggregate_grades($prevuser, $items, $grade_values, $oldgrade, $excluded);//the last one
4fc9ec1e 454 $this->lib_wrapper->rs_close($rs);
b8ff92b6 455 }
456
b8ff92b6 457 return true;
458 }
459
460 /**
ac9b0805 461 * internal function for category grades aggregation
ced5ee59 462 *
1994d890 463 * @param int $userid
464 * @param array $items
465 * @param array $grade_values
f1ad9e04 466 * @param object $oldgrade
1994d890 467 * @param bool $excluded
468 * @return boolean (just plain return;)
b8ff92b6 469 */
23207a1a 470 function aggregate_grades($userid, $items, $grade_values, $oldgrade, $excluded) {
e171963b 471 global $CFG;
b8ff92b6 472 if (empty($userid)) {
f8e6e4db 473 //ignore first call
b8ff92b6 474 return;
475 }
4a490db0 476
f8e6e4db 477 if ($oldgrade) {
4ac209d5 478 if (is_null($oldgrade->finalgrade)) {
479 $oldfinalgrade = null;
480 } else {
0758a08e 481 // we need proper floats here for !== comparison later
f1ad9e04 482 $oldfinalgrade = (float)$oldgrade->finalgrade;
0758a08e 483 }
4fc9ec1e 484 $grade = $this->get_instance('grade_grade', $oldgrade, false);
f8e6e4db 485 $grade->grade_item =& $this->grade_item;
b8ff92b6 486
f8e6e4db 487 } else {
488 // insert final grade - it will be needed later anyway
4fc9ec1e 489 $grade = $this->get_instance('grade_grade', array('itemid'=>$this->grade_item->id, 'userid'=>$userid), false);
f8e6e4db 490 $grade->grade_item =& $this->grade_item;
f1ad9e04 491 $grade->insert('system');
492 $oldfinalgrade = null;
f8e6e4db 493 }
f1ad9e04 494
c86caae7 495 // no need to recalculate locked or overridden grades
496 if ($grade->is_locked() or $grade->is_overridden()) {
2cc4b0f9 497 return;
ac9b0805 498 }
499
f8e6e4db 500 // can not use own final category grade in calculation
9580a21f 501 unset($grade_values[$this->grade_item->id]);
f8e6e4db 502
f1ad9e04 503
504 /// sum is a special aggregation types - it adjusts the min max, does not use relative values
505 if ($this->aggregation == GRADE_AGGREGATE_SUM) {
506 $this->sum_grades($grade, $oldfinalgrade, $items, $grade_values, $excluded);
507 return;
508 }
509
0758a08e 510 // if no grades calculation possible or grading not allowed clear final grade
9580a21f 511 if (empty($grade_values) or empty($items) or ($this->grade_item->gradetype != GRADE_TYPE_VALUE and $this->grade_item->gradetype != GRADE_TYPE_SCALE)) {
f8e6e4db 512 $grade->finalgrade = null;
f1ad9e04 513 if ($grade->finalgrade !== $oldfinalgrade) {
0758a08e 514 $grade->update('aggregation');
f8e6e4db 515 }
b8ff92b6 516 return;
517 }
0758a08e 518
f8e6e4db 519 /// normalize the grades first - all will have value 0...1
d5f0aa01 520 // ungraded items are not used in aggregation
23207a1a 521 foreach ($grade_values as $itemid=>$v) {
b8ff92b6 522 if (is_null($v)) {
523 // null means no grade
23207a1a 524 unset($grade_values[$itemid]);
525 continue;
526 } else if (in_array($itemid, $excluded)) {
527 unset($grade_values[$itemid]);
b8ff92b6 528 continue;
0aa32279 529 }
9a68cffc 530 $grade_values[$itemid] = grade_grade::standardise_score($v, $items[$itemid]->grademin, $items[$itemid]->grademax, 0, 1);
0aa32279 531 }
dda0c7e6 532
eacd3700 533 // use min grade if grade missing for these types
c2efb501 534 if (!$this->aggregateonlygraded) {
535 foreach($items as $itemid=>$value) {
536 if (!isset($grade_values[$itemid]) and !in_array($itemid, $excluded)) {
537 $grade_values[$itemid] = 0;
eacd3700 538 }
c2efb501 539 }
eacd3700 540 }
541
542 // limit and sort
9580a21f 543 $this->apply_limit_rules($grade_values);
544 asort($grade_values, SORT_NUMERIC);
4a490db0 545
d5f0aa01 546 // let's see we have still enough grades to do any statistics
9580a21f 547 if (count($grade_values) == 0) {
ac9b0805 548 // not enough attempts yet
f8e6e4db 549 $grade->finalgrade = null;
f1ad9e04 550 if ($grade->finalgrade !== $oldfinalgrade) {
0758a08e 551 $grade->update('aggregation');
b8ff92b6 552 }
553 return;
554 }
2df71235 555
d297269d 556 // do the maths
557 $agg_grade = $this->aggregate_values($grade_values, $items);
558
0758a08e 559 // recalculate the grade back to requested range
9a68cffc 560 $finalgrade = grade_grade::standardise_score($agg_grade, 0, 1, $this->grade_item->grademin, $this->grade_item->grademax);
d297269d 561
f1ad9e04 562 if (is_null($finalgrade)) {
563 $grade->finalgrade = null;
d297269d 564 } else {
f1ad9e04 565 $grade->finalgrade = (float)bounded_number($this->grade_item->grademin, $finalgrade, $this->grade_item->grademax);
d297269d 566 }
567
568 // update in db if changed
f1ad9e04 569 if ($grade->finalgrade !== $oldfinalgrade) {
0758a08e 570 $grade->update('aggregation');
d297269d 571 }
572
573 return;
574 }
575
89a5f827 576 /**
577 * Internal function - aggregation maths.
578 */
d297269d 579 function aggregate_values($grade_values, $items) {
b8ff92b6 580 switch ($this->aggregation) {
c2efb501 581 case GRADE_AGGREGATE_MEDIAN: // Middle point value in the set: ignores frequencies
9580a21f 582 $num = count($grade_values);
c186c7b2 583 $grades = array_values($grade_values);
584 if ($num % 2 == 0) {
9c8d38fa 585 $agg_grade = ($grades[intval($num/2)-1] + $grades[intval($num/2)]) / 2;
b8ff92b6 586 } else {
9c8d38fa 587 $agg_grade = $grades[intval(($num/2)-0.5)];
b8ff92b6 588 }
589 break;
ac9b0805 590
c2efb501 591 case GRADE_AGGREGATE_MIN:
9c8d38fa 592 $agg_grade = reset($grade_values);
b8ff92b6 593 break;
594
c2efb501 595 case GRADE_AGGREGATE_MAX:
9c8d38fa 596 $agg_grade = array_pop($grade_values);
b8ff92b6 597 break;
598
c2efb501 599 case GRADE_AGGREGATE_MODE: // the most common value, average used if multimode
9580a21f 600 $freq = array_count_values($grade_values);
95affb8a 601 arsort($freq); // sort by frequency keeping keys
602 $top = reset($freq); // highest frequency count
603 $modes = array_keys($freq, $top); // search for all modes (have the same highest count)
604 rsort($modes, SORT_NUMERIC); // get highes mode
9c8d38fa 605 $agg_grade = reset($modes);
d5fab31f 606 break;
95affb8a 607
1426edac 608 case GRADE_AGGREGATE_WEIGHTED_MEAN: // Weighted average of all existing final grades, weight specified in coef
9580a21f 609 $weightsum = 0;
610 $sum = 0;
eacd3700 611 foreach($grade_values as $itemid=>$grade_value) {
612 if ($items[$itemid]->aggregationcoef <= 0) {
9580a21f 613 continue;
614 }
eacd3700 615 $weightsum += $items[$itemid]->aggregationcoef;
616 $sum += $items[$itemid]->aggregationcoef * $grade_value;
9580a21f 617 }
618 if ($weightsum == 0) {
9c8d38fa 619 $agg_grade = null;
9580a21f 620 } else {
9c8d38fa 621 $agg_grade = $sum / $weightsum;
9580a21f 622 }
623 break;
624
1426edac 625 case GRADE_AGGREGATE_WEIGHTED_MEAN2: // Weighted average of all existing final grades, weight is the range of grade (ususally grademax)
626 $weightsum = 0;
627 $sum = 0;
628 foreach($grade_values as $itemid=>$grade_value) {
629 $weight = $items[$itemid]->grademax - $items[$itemid]->grademin;
630 if ($weight <= 0) {
631 continue;
632 }
633 $weightsum += $weight;
634 $sum += $weight * $grade_value;
635 }
636 if ($weightsum == 0) {
637 $agg_grade = null;
638 } else {
639 $agg_grade = $sum / $weightsum;
640 }
641 break;
642
c2efb501 643 case GRADE_AGGREGATE_EXTRACREDIT_MEAN: // special average
9580a21f 644 $num = 0;
645 $sum = 0;
eacd3700 646 foreach($grade_values as $itemid=>$grade_value) {
647 if ($items[$itemid]->aggregationcoef == 0) {
9580a21f 648 $num += 1;
649 $sum += $grade_value;
eacd3700 650 } else if ($items[$itemid]->aggregationcoef > 0) {
651 $sum += $items[$itemid]->aggregationcoef * $grade_value;
9580a21f 652 }
653 }
654 if ($num == 0) {
9c8d38fa 655 $agg_grade = $sum; // only extra credits or wrong coefs
9580a21f 656 } else {
9c8d38fa 657 $agg_grade = $sum / $num;
9580a21f 658 }
659 break;
660
c2efb501 661 case GRADE_AGGREGATE_MEAN: // Arithmetic average of all grade items (if ungraded aggregated, NULL counted as minimum)
ac9b0805 662 default:
9580a21f 663 $num = count($grade_values);
664 $sum = array_sum($grade_values);
9c8d38fa 665 $agg_grade = $sum / $num;
b8ff92b6 666 break;
667 }
668
d297269d 669 return $agg_grade;
0aa32279 670 }
0758a08e 671
0758a08e 672 /**
673 * internal function for category grades summing
674 *
f1ad9e04 675 * @param object $grade
0758a08e 676 * @param int $userid
f1ad9e04 677 * @param float $oldfinalgrade
0758a08e 678 * @param array $items
679 * @param array $grade_values
0758a08e 680 * @param bool $excluded
681 * @return boolean (just plain return;)
682 */
f1ad9e04 683 function sum_grades(&$grade, $oldfinalgrade, $items, $grade_values, $excluded) {
0758a08e 684 // ungraded and exluded items are not used in aggregation
685 foreach ($grade_values as $itemid=>$v) {
686 if (is_null($v)) {
687 unset($grade_values[$itemid]);
688 } else if (in_array($itemid, $excluded)) {
689 unset($grade_values[$itemid]);
690 }
691 }
692
693 $max = 0;
694
695 //find max grade
696 foreach ($items as $item) {
697 if ($item->gradetype != GRADE_TYPE_VALUE) {
698 continue; // sum only items with value grades, no scales and outcomes!
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
749 or $this->aggregation == GRADE_AGGREGATE_EXTRACREDIT_MEAN);
ba74762b 750
9580a21f 751 }
752
1c307f21 753 /**
b3ac6c3e 754 * Returns tree with all grade_items and categories as elements
755 * @static
756 * @param int $courseid
b3ac6c3e 757 * @param boolean $include_category_items as category children
758 * @return array
1c307f21 759 */
938e00b6 760 function fetch_course_tree($courseid, $include_category_items=false) {
aaefeda4 761 $obj = grade_object::get_instance('grade_category');
4fc9ec1e 762 $course_category = $obj->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;
4fc9ec1e 768 return $obj->_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') {
4fc9ec1e 792 $result['children'][$sortorder] = $this->_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') {
4fc9ec1e 797 $result['children'][++$sortorder] = $this->_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
4fc9ec1e 817 $cats = $this->lib_wrapper->get_records('grade_categories', 'courseid', $this->courseid);
818 $items = $this->lib_wrapper->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 }
b3ac6c3e 862 if ($catid == $this->id) {
863 $category = &$cats[$catid];
864 }
865 }
866
867 unset($items); // not needed
868 unset($cats); // not needed
869
4fc9ec1e 870 $children_array = $this->_get_children_recursion($category);
b3ac6c3e 871
872 ksort($children_array);
873
874 return $children_array;
875
876 }
877
938e00b6 878 function _get_children_recursion($category) {
b3ac6c3e 879
880 $children_array = array();
881 foreach($category->children as $sortorder=>$child) {
882 if (array_key_exists('itemtype', $child)) {
4fc9ec1e 883 $grade_item = $this->get_instance('grade_item', $child, false);
4faf5f99 884 if (in_array($grade_item->itemtype, array('course', 'category'))) {
885 $type = $grade_item->itemtype.'item';
886 $depth = $category->depth;
314c4336 887 } else {
888 $type = 'item';
889 $depth = $category->depth; // we use this to set the same colour
b3ac6c3e 890 }
4faf5f99 891 $children_array[$sortorder] = array('object'=>$grade_item, 'type'=>$type, 'depth'=>$depth);
4a490db0 892
7c8a963f 893 } else {
4fc9ec1e 894 $children = $this->_get_children_recursion($child);
895 $grade_category = $this->get_instance('grade_category', $child, false);
b3ac6c3e 896 if (empty($children)) {
314c4336 897 $children = array();
7c8a963f 898 }
4faf5f99 899 $children_array[$sortorder] = array('object'=>$grade_category, 'type'=>'category', 'depth'=>$grade_category->depth, 'children'=>$children);
314c4336 900 }
27f95e9b 901 }
902
b3ac6c3e 903 // sort the array
904 ksort($children_array);
905
27f95e9b 906 return $children_array;
907 }
4a490db0 908
f151b073 909 /**
ab53054f 910 * Uses get_grade_item to load or create a grade_item, then saves it as $this->grade_item.
f151b073 911 * @return object Grade_item
912 */
913 function load_grade_item() {
ac9b0805 914 if (empty($this->grade_item)) {
915 $this->grade_item = $this->get_grade_item();
916 }
ab53054f 917 return $this->grade_item;
918 }
4a490db0 919
ab53054f 920 /**
921 * Retrieves from DB and instantiates the associated grade_item object.
922 * If no grade_item exists yet, create one.
923 * @return object Grade_item
924 */
925 function get_grade_item() {
c91ed4be 926 if (empty($this->id)) {
927 debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set.");
928 return false;
929 }
930
b3ac6c3e 931 if (empty($this->parent)) {
932 $params = array('courseid'=>$this->courseid, 'itemtype'=>'course', 'iteminstance'=>$this->id);
933
934 } else {
935 $params = array('courseid'=>$this->courseid, 'itemtype'=>'category', 'iteminstance'=>$this->id);
936 }
4ac209d5 937
aaefeda4 938 $grade_item = grade_object::get_instance('grade_item');
f92dcad8 939
4fc9ec1e 940 if (!$grade_items = $grade_item->fetch_all($params)) {
b8ff92b6 941 // create a new one
4fc9ec1e 942 $grade_item = $this->get_instance('grade_item', $params, false);
b8ff92b6 943 $grade_item->gradetype = GRADE_TYPE_VALUE;
f8e6e4db 944 $grade_item->insert('system');
4a490db0 945
b8ff92b6 946 } else if (count($grade_items) == 1){
947 // found existing one
948 $grade_item = reset($grade_items);
4a490db0 949
b8ff92b6 950 } else {
951 debugging("Found more than one grade_item attached to category id:".$this->id);
ac9b0805 952 // return first one
953 $grade_item = reset($grade_items);
2c72af1f 954 }
955
ab53054f 956 return $grade_item;
f151b073 957 }
8c846243 958
959 /**
960 * Uses $this->parent to instantiate $this->parent_category based on the
961 * referenced record in the DB.
962 * @return object Parent_category
963 */
964 function load_parent_category() {
965 if (empty($this->parent_category) && !empty($this->parent)) {
ab53054f 966 $this->parent_category = $this->get_parent_category();
8c846243 967 }
968 return $this->parent_category;
4a490db0 969 }
970
ab53054f 971 /**
972 * Uses $this->parent to instantiate and return a grade_category object.
973 * @return object Parent_category
974 */
975 function get_parent_category() {
976 if (!empty($this->parent)) {
4fc9ec1e 977 $parent_category = $this->get_instance('grade_category', array('id' => $this->parent));
4a490db0 978 return $parent_category;
ab53054f 979 } else {
980 return null;
981 }
982 }
983
2186f72c 984 /**
4a490db0 985 * Returns the most descriptive field for this object. This is a standard method used
2186f72c 986 * when we do not know the exact type of an object.
987 * @return string name
988 */
989 function get_name() {
314c4336 990 if (empty($this->parent)) {
4fc9ec1e 991 $course = $this->lib_wrapper->get_record('course', 'id', $this->courseid);
410753fb 992 return format_string($course->fullname);
314c4336 993 } else {
994 return $this->fullname;
995 }
2186f72c 996 }
c91ed4be 997
0fc7f624 998 /**
999 * Sets this category's parent id. A generic method shared by objects that have a parent id of some kind.
f13002d5 1000 * @param int parentid
1001 * @return boolean success
0fc7f624 1002 */
f8e6e4db 1003 function set_parent($parentid, $source=null) {
f13002d5 1004 if ($this->parent == $parentid) {
1005 return true;
1006 }
1007
1008 if ($parentid == $this->id) {
1009 error('Can not assign self as parent!');
1010 }
1011
1012 if (empty($this->parent) and $this->is_course_category()) {
1013 error('Course category can not have parent!');
b3ac6c3e 1014 }
f13002d5 1015
1016 // find parent and check course id
aaefeda4 1017 $obj = grade_object::get_instance('grade_category');
1018 if (!$parent_category = $obj->fetch(array('id'=>$parentid, 'courseid'=>$this->courseid))) {
b3ac6c3e 1019 return false;
1020 }
1021
f8e6e4db 1022 $this->force_regrading();
b3ac6c3e 1023
1024 // set new parent category
f8e6e4db 1025 $this->parent = $parent_category->id;
1026 $this->parent_category =& $parent_category;
b3ac6c3e 1027 $this->path = null; // remove old path and depth - will be recalculated in update()
f8e6e4db 1028 $this->depth = null; // remove old path and depth - will be recalculated in update()
1029 $this->update($source);
b3ac6c3e 1030
15b462da 1031 return $this->update($source);
b3ac6c3e 1032 }
1033
1034 /**
1035 * Returns the final values for this grade category.
1036 * @param int $userid Optional: to retrieve a single final grade
1037 * @return mixed An array of all final_grades (stdClass objects) for this grade_item, or a single final_grade.
1038 */
1039 function get_final($userid=NULL) {
1040 $this->load_grade_item();
1041 return $this->grade_item->get_final($userid);
0fc7f624 1042 }
4a490db0 1043
0fc7f624 1044 /**
4a490db0 1045 * Returns the sortorder of the associated grade_item. This method is also available in
5fad5061 1046 * grade_item, for cases where the object type is not known.
0fc7f624 1047 * @return int Sort order
1048 */
1049 function get_sortorder() {
b3ac6c3e 1050 $this->load_grade_item();
1051 return $this->grade_item->get_sortorder();
0fc7f624 1052 }
1053
be7c0693 1054 /**
1055 * Returns the idnumber of the associated grade_item. This method is also available in
1056 * grade_item, for cases where the object type is not known.
1057 * @return string idnumber
1058 */
1059 function get_idnumber() {
1060 $this->load_grade_item();
1061 return $this->grade_item->get_idnumber();
1062 }
1063
0fc7f624 1064 /**
b3ac6c3e 1065 * Sets sortorder variable for this category.
4a490db0 1066 * This method is also available in grade_item, for cases where the object type is not know.
0fc7f624 1067 * @param int $sortorder
1068 * @return void
1069 */
1070 function set_sortorder($sortorder) {
b3ac6c3e 1071 $this->load_grade_item();
1072 $this->grade_item->set_sortorder($sortorder);
1073 }
1074
6639ead3 1075 /**
1076 * Move this category after the given sortorder - does not change the parent
1077 * @param int $sortorder to place after
1078 */
f13002d5 1079 function move_after_sortorder($sortorder) {
1080 $this->load_grade_item();
1081 $this->grade_item->move_after_sortorder($sortorder);
1082 }
1083
b3ac6c3e 1084 /**
f13002d5 1085 * Return true if this is the top most category that represents the total course grade.
b3ac6c3e 1086 * @return boolean
1087 */
1088 function is_course_category() {
1089 $this->load_grade_item();
1090 return $this->grade_item->is_course_item();
1091 }
1092
1093 /**
1094 * Return the top most course category.
1095 * @static
1096 * @return object grade_category instance for course grade
1097 */
1098 function fetch_course_category($courseid) {
1099
1100 // course category has no parent
aaefeda4 1101 $obj = grade_object::get_instance('grade_category');
1102 if ($course_category = $obj->fetch(array('courseid'=>$courseid, 'parent'=>null))) {
b3ac6c3e 1103 return $course_category;
1104 }
1105
1106 // create a new one
aaefeda4 1107 $course_category = grade_object::get_instance('grade_category');
b3ac6c3e 1108 $course_category->insert_course_category($courseid);
1109
1110 return $course_category;
0fc7f624 1111 }
4ac209d5 1112
79eabc2a 1113 /**
1114 * Is grading object editable?
1115 * @return boolean
1116 */
1117 function is_editable() {
1118 return true;
1119 }
1120
5fad5061 1121 /**
4a490db0 1122 * Returns the locked state/date of the associated grade_item. This method is also available in
1123 * grade_item, for cases where the object type is not known.
22e23c78 1124 * @return boolean
5fad5061 1125 */
2cc4b0f9 1126 function is_locked() {
5fad5061 1127 $this->load_grade_item();
22e23c78 1128 return $this->grade_item->is_locked();
5fad5061 1129 }
1130
1131 /**
1132 * Sets the grade_item's locked variable and updates the grade_item.
1133 * Method named after grade_item::set_locked().
1134 * @param int $locked 0, 1 or a timestamp int(10) after which date the item will be locked.
fb0e3570 1135 * @param boolean $cascade lock/unlock child objects too
2b0f65e2 1136 * @param boolean $refresh refresh grades when unlocking
9580a21f 1137 * @return boolean success if category locked (not all children mayb be locked though)
5fad5061 1138 */
fb0e3570 1139 function set_locked($lockedstate, $cascade=false, $refresh=true) {
5fad5061 1140 $this->load_grade_item();
2b0f65e2 1141
fb0e3570 1142 $result = $this->grade_item->set_locked($lockedstate, $cascade, true);
1143
1144 if ($cascade) {
1145 //process all children - items and categories
aaefeda4 1146 $grade_item = grade_object::get_instance('grade_item');
4fc9ec1e 1147 if ($children = $grade_item->fetch_all(array('categoryid'=>$this->id))) {
fb0e3570 1148 foreach($children as $child) {
1149 $child->set_locked($lockedstate, true, false);
1150 if (empty($lockedstate) and $refresh) {
1151 //refresh when unlocking
1152 $child->refresh_grades();
1153 }
2b0f65e2 1154 }
7a7a53d3 1155 }
aaefeda4 1156 $obj = grade_object::get_instance('grade_category');
1157 if ($children = $obj->fetch_all(array('parent'=>$this->id))) {
fb0e3570 1158 foreach($children as $child) {
1159 $child->set_locked($lockedstate, true, true);
1160 }
7a7a53d3 1161 }
1162 }
2b0f65e2 1163
b121b544 1164 return $result;
5fad5061 1165 }
4a490db0 1166
5fad5061 1167 /**
4a490db0 1168 * Returns the hidden state/date of the associated grade_item. This method is also available in
22e23c78 1169 * grade_item.
1170 * @return boolean
5fad5061 1171 */
22e23c78 1172 function is_hidden() {
5fad5061 1173 $this->load_grade_item();
22e23c78 1174 return $this->grade_item->is_hidden();
5fad5061 1175 }
1176
597f50e6 1177 /**
1178 * Check grade hidden status. Uses data from both grade item and grade.
1179 * @return boolean true if hiddenuntil, false if not
1180 */
1181 function is_hiddenuntil() {
1182 $this->load_grade_item();
1183 return $this->grade_item->is_hiddenuntil();
1184 }
1185
5fad5061 1186 /**
4a490db0 1187 * Sets the grade_item's hidden variable and updates the grade_item.
5fad5061 1188 * Method named after grade_item::set_hidden().
1189 * @param int $hidden 0, 1 or a timestamp int(10) after which date the item will be hidden.
f60c61b1 1190 * @param boolean $cascade apply to child objects too
5fad5061 1191 * @return void
1192 */
f60c61b1 1193 function set_hidden($hidden, $cascade=false) {
5fad5061 1194 $this->load_grade_item();
22e23c78 1195 $this->grade_item->set_hidden($hidden);
f60c61b1 1196 if ($cascade) {
aaefeda4 1197 $grade_item = grade_object::get_instance('grade_item');
4fc9ec1e 1198 if ($children = $grade_item->fetch_all(array('categoryid'=>$this->id))) {
f60c61b1 1199 foreach($children as $child) {
1200 $child->set_hidden($hidden, $cascade);
1201 }
f13002d5 1202 }
aaefeda4 1203 $obj = grade_object::get_instance('grade_category');
1204 if ($children = $obj->fetch_all(array('parent'=>$this->id))) {
f60c61b1 1205 foreach($children as $child) {
1206 $child->set_hidden($hidden, $cascade);
1207 }
f13002d5 1208 }
1209 }
5fad5061 1210 }
89a5f827 1211
190af29f 1212 /**
1213 * Applies default settings on this category
1214 * @return bool true if anything changed
1215 */
1216 function apply_default_settings() {
1217 global $CFG;
1218
1219 foreach ($this->forceable as $property) {
1220 if (isset($CFG->{"grade_$property"})) {
1221 if ($CFG->{"grade_$property"} == -1) {
1222 continue; //temporary bc before version bump
1223 }
1224 $this->$property = $CFG->{"grade_$property"};
1225 }
1226 }
1227 }
1228
89a5f827 1229 /**
1230 * Applies forced settings on this category
1231 * @return bool true if anything changed
1232 */
1233 function apply_forced_settings() {
1234 global $CFG;
1235
1236 $updated = false;
1237 foreach ($this->forceable as $property) {
190af29f 1238 if (isset($CFG->{"grade_$property"}) and isset($CFG->{"grade_{$property}_flag"}) and ((int)$CFG->{"grade_{$property}_flag"} & 1)) {
1239 if ($CFG->{"grade_$property"} == -1) {
1240 continue; //temporary bc before version bump
1241 }
89a5f827 1242 $this->$property = $CFG->{"grade_$property"};
1243 $updated = true;
1244 }
1245 }
1246
1247 return $updated;
1248 }
1249
1250 /**
1251 * Notification of change in forced category settings.
1252 * @static
1253 */
1254 function updated_forced_settings() {
1255 global $CFG;
1256 $sql = "UPDATE {$CFG->prefix}grade_items SET needsupdate=1 WHERE itemtype='course' or itemtype='category'";
4fc9ec1e 1257 $this->lib_wrapper->execute_sql($sql, false);
89a5f827 1258 }
4a490db0 1259}
8a31e65c 1260?>