Merge branch 'MDL-68861-master' of git://github.com/vmdef/moodle
[moodle.git] / lib / coursecatlib.php
blob2079e4d185417a7a591bfd0afaf5a38ce68b06b5
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Deprecated file, classes moved to autoloaded locations
20 * @package core
21 * @subpackage course
22 * @copyright 2013 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 debugging('Class coursecat is now alias to autoloaded class core_course_category, ' .
29 'course_in_list is an alias to core_course_list_element. '.
30 'Class coursecat_sortable_records is deprecated without replacement. Do not include coursecatlib.php',
31 DEBUG_DEVELOPER);
33 /**
34 * An array of records that is sortable by many fields.
36 * For more info on the ArrayObject class have a look at php.net.
38 * @package core
39 * @subpackage course
40 * @copyright 2013 Sam Hemelryk
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class coursecat_sortable_records extends ArrayObject {
45 /**
46 * An array of sortable fields.
47 * Gets set temporarily when sort is called.
48 * @var array
50 protected $sortfields = array();
52 /**
53 * Sorts this array using the given fields.
55 * @param array $records
56 * @param array $fields
57 * @return array
59 public static function sort(array $records, array $fields) {
60 $records = new coursecat_sortable_records($records);
61 $records->sortfields = $fields;
62 $records->uasort(array($records, 'sort_by_many_fields'));
63 return $records->getArrayCopy();
66 /**
67 * Sorts the two records based upon many fields.
69 * This method should not be called itself, please call $sort instead.
70 * It has been marked as access private as such.
72 * @access private
73 * @param stdClass $a
74 * @param stdClass $b
75 * @return int
77 public function sort_by_many_fields($a, $b) {
78 foreach ($this->sortfields as $field => $mult) {
79 // Nulls first.
80 if (is_null($a->$field) && !is_null($b->$field)) {
81 return -$mult;
83 if (is_null($b->$field) && !is_null($a->$field)) {
84 return $mult;
87 if (is_string($a->$field) || is_string($b->$field)) {
88 // String fields.
89 if ($cmp = strcoll($a->$field, $b->$field)) {
90 return $mult * $cmp;
92 } else {
93 // Int fields.
94 if ($a->$field > $b->$field) {
95 return $mult;
97 if ($a->$field < $b->$field) {
98 return -$mult;
102 return 0;