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