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