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