From 15f89e2872d8469da9d8cfa0ff5e7d7a4be39136 Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Wed, 1 May 2013 18:42:33 +1200 Subject: [PATCH] MDL-39422 course: converted closures to fix eAccelerator issues --- lib/coursecatlib.php | 111 +++++++++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 31 deletions(-) diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index 7112c47ec52..8902ecd2543 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -840,34 +840,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { } return; } - // sorting by multiple fields - uasort($records, function ($a, $b) use ($sortfields) { - foreach ($sortfields as $field => $mult) { - // nulls first - if (is_null($a->$field) && !is_null($b->$field)) { - return -$mult; - } - if (is_null($b->$field) && !is_null($a->$field)) { - return $mult; - } - - if (is_string($a->$field) || is_string($b->$field)) { - // string fields - if ($cmp = strcoll($a->$field, $b->$field)) { - return $mult * $cmp; - } - } else { - // int fields - if ($a->$field > $b->$field) { - return $mult; - } - if ($a->$field < $b->$field) { - return -$mult; - } - } - } - return 0; - }); + $records = coursecat_sortable_records::sort($records, $sortfields); } /** @@ -1038,7 +1011,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { if (!empty($search['search'])) { // search courses that have specified words in their names/summaries $searchterms = preg_split('|\s+|', trim($search['search']), 0, PREG_SPLIT_NO_EMPTY); - $searchterms = array_filter($searchterms, function ($v) { return strlen($v) > 1; } ); + $searchterms = array_filter($searchterms, create_function('$v', 'return strlen($v) > 1;')); $courselist = get_courses_search($searchterms, 'c.sortorder ASC', 0, 9999999, $totalcount); self::sort_records($courselist, $sortfields); $coursecatcache->set($cachekey, array_keys($courselist)); @@ -2095,8 +2068,11 @@ class course_in_list implements IteratorAggregate { if ($acceptedtypes !== '*') { // filter only files with allowed extensions require_once($CFG->libdir. '/filelib.php'); - $files = array_filter($files, function ($file) use ($acceptedtypes) { - return file_extension_in_typegroup($file->get_filename(), $acceptedtypes);} ); + foreach ($files as $key => $file) { + if (!file_extension_in_typegroup($file->get_filename(), $acceptedtypes)) { + unset($files[$key]); + } + } } if (count($files) > $CFG->courseoverviewfileslimit) { // return no more than $CFG->courseoverviewfileslimit files @@ -2170,3 +2146,76 @@ class course_in_list implements IteratorAggregate { return new ArrayIterator($ret); } } + +/** + * An array of records that is sortable by many fields. + * + * For more info on the ArrayObject class have a look at php.net. + * + * @package core + * @subpackage course + * @copyright 2013 Sam Hemelryk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class coursecat_sortable_records extends ArrayObject { + + /** + * An array of sortable fields. + * Gets set temporarily when sort is called. + * @var array + */ + protected $sortfields = array(); + + /** + * Sorts this array using the given fields. + * + * @param array $records + * @param array $fields + * @return array + */ + public static function sort(array $records, array $fields) { + $records = new coursecat_sortable_records($records); + $records->sortfields = $fields; + $records->uasort(array($records, 'sort_by_many_fields')); + return $records->getArrayCopy(); + } + + /** + * Sorts the two records based upon many fields. + * + * This method should not be called itself, please call $sort instead. + * It has been marked as access private as such. + * + * @access private + * @param stdClass $a + * @param stdClass $b + * @return int + */ + public function sort_by_many_fields($a, $b) { + foreach ($this->sortfields as $field => $mult) { + // nulls first + if (is_null($a->$field) && !is_null($b->$field)) { + return -$mult; + } + if (is_null($b->$field) && !is_null($a->$field)) { + return $mult; + } + + if (is_string($a->$field) || is_string($b->$field)) { + // string fields + if ($cmp = strcoll($a->$field, $b->$field)) { + return $mult * $cmp; + } + } else { + // int fields + if ($a->$field > $b->$field) { + return $mult; + } + if ($a->$field < $b->$field) { + return -$mult; + } + } + } + return 0; + } +} \ No newline at end of file