Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Deprecated file, classes moved to autoloaded locations
  19   *
  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
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  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);
  32  
  33  /**
  34   * An array of records that is sortable by many fields.
  35   *
  36   * For more info on the ArrayObject class have a look at php.net.
  37   *
  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
  42   */
  43  class coursecat_sortable_records extends ArrayObject {
  44  
  45      /**
  46       * An array of sortable fields.
  47       * Gets set temporarily when sort is called.
  48       * @var array
  49       */
  50      protected $sortfields = array();
  51  
  52      /**
  53       * Sorts this array using the given fields.
  54       *
  55       * @param array $records
  56       * @param array $fields
  57       * @return array
  58       */
  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();
  64      }
  65  
  66      /**
  67       * Sorts the two records based upon many fields.
  68       *
  69       * This method should not be called itself, please call $sort instead.
  70       * It has been marked as access private as such.
  71       *
  72       * @access private
  73       * @param stdClass $a
  74       * @param stdClass $b
  75       * @return int
  76       */
  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;
  82              }
  83              if (is_null($b->$field) && !is_null($a->$field)) {
  84                  return $mult;
  85              }
  86  
  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;
  91                  }
  92              } else {
  93                  // Int fields.
  94                  if ($a->$field > $b->$field) {
  95                      return $mult;
  96                  }
  97                  if ($a->$field < $b->$field) {
  98                      return -$mult;
  99                  }
 100              }
 101          }
 102          return 0;
 103      }
 104  }