MDL-79890 core: Update uses of array_keys with multiple params

These now call the newly minted `moodle_array_keys_filter` method.
This commit is contained in:
Andrew Nicols 2023-11-07 15:55:48 +08:00
parent 0efbc79e5f
commit 75587e23c6
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
12 changed files with 17 additions and 14 deletions

View File

@ -639,7 +639,7 @@ function course_add_cm_to_section($courseorid, $cmid, $sectionnum, $beforemod =
$modarray = explode(",", trim($section->sequence));
if (empty($section->sequence)) {
$newsequence = "$cmid";
} else if ($beforemod && ($key = array_keys($modarray, $beforemod))) {
} else if ($beforemod && ($key = moodle_array_keys_filter($modarray, $beforemod))) {
$insertarray = array($cmid, $beforemod);
array_splice($modarray, $key[0], 1, $insertarray);
$newsequence = implode(",", $modarray);
@ -1111,7 +1111,7 @@ function delete_mod_from_section($modid, $sectionid) {
$modarray = explode(",", $section->sequence);
if ($key = array_keys ($modarray, $modid)) {
if ($key = moodle_array_keys_filter($modarray, $modid)) {
array_splice($modarray, $key[0], 1);
$newsequence = implode(",", $modarray);
$DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id));

View File

@ -165,7 +165,7 @@ class converter {
*/
protected function get_next_converter($converters, $currentconverter = null) {
if ($currentconverter) {
$keys = array_keys($converters, $currentconverter);
$keys = moodle_array_keys_filter($converters, $currentconverter);
$key = $keys[0];
if (isset($converters[$key + 1])) {
return $converters[$key + 1];

View File

@ -1097,7 +1097,7 @@ class grade_category extends grade_object {
$freq = array_count_values($converted_grade_values);
arsort($freq); // sort by frequency keeping keys
$top = reset($freq); // highest frequency count
$modes = array_keys($freq, $top); // search for all modes (have the same highest count)
$modes = moodle_array_keys_filter($freq, $top); // Search for all modes (have the same highest count).
rsort($modes, SORT_NUMERIC); // get highest mode
$agg_grade = reset($modes);
// Record the weights as used.

View File

@ -143,7 +143,7 @@ extends Horde_Imap_Client_Cache_Backend
foreach (array_keys(array_flip($val['slice'])) as $slice) {
$data = array();
foreach (array_keys($s['s'], $slice) as $uid) {
foreach (moodle_array_keys_filter($s['s'], $slice) as $uid) {
$data[$uid] = is_array($d[$uid])
? serialize($d[$uid])
: $d[$uid];
@ -297,7 +297,7 @@ extends Horde_Imap_Client_Cache_Backend
foreach (array_unique($deleted) as $slice) {
/* Get rid of slice if less than 10% of capacity. */
if (($slice != $slicemap['i']) &&
($slice_uids = array_keys($slicemap['s'], $slice)) &&
($slice_uids = moodle_array_keys_filter($slicemap['s'], $slice)) &&
($this->_params['slicesize'] * 0.1) > count($slice_uids)) {
$this->_toUpdate($mailbox, 'add', $slice_uids);
$this->_cache->expire($this->_getCid($mailbox, $slice));
@ -416,7 +416,7 @@ extends Horde_Imap_Client_Cache_Backend
$ptr = &$this->_slicemap[$mailbox];
// Slice data is corrupt; remove from slicemap.
foreach (array_keys($ptr['s'], $slice) as $val) {
foreach (moodle_array_keys_filter($ptr['s'], $slice) as $val) {
unset($ptr['s'][$val]);
}

View File

@ -16,6 +16,8 @@ Description of import of Horde libraries
Notes:
* 2023-01-20 Applied patch https://github.com/horde/Util/pull/10
* 2023-01-20 Horde/Mail is copied from https://github.com/bytestream/Mail/tree/v2.7.1 for PHP 8.1 compatibility
* MDL-79890: Calls to array_keys() passing a second parameter have been modified to call `moodle_array_keys_filter` instead for PHP 8.3 compat.
This change is not fed upstream as Horde appears abandoned.
====
#!/bin/sh

View File

@ -628,7 +628,7 @@ abstract class list_item {
*/
public function create_children(&$records, &$children, $thisrecordid) {
//keys where value is $thisrecordid
$thischildren = array_keys($children, $thisrecordid);
$thischildren = moodle_array_keys_filter($children, $thisrecordid);
foreach ($thischildren as $child) {
$thisclass = get_class($this);
$newlistitem = new $thisclass($records[$child], $this->children, $this->attributes);

View File

@ -11049,9 +11049,10 @@ function exceeds_password_length(string $password, int $pepperlength = 0): bool
*
* @param array $array
* @param mixed $filter The value to filter on
* @param bool $strict Whether to apply a strit test with the filter
* @return array
*/
function moodle_array_keys_filter(array $array, mixed $filter, bool $strict): array {
function moodle_array_keys_filter(array $array, mixed $filter, bool $strict = false): array {
return array_keys(array_filter(
$array,
function($value, $key) use ($filter, $strict): bool {

View File

@ -702,7 +702,7 @@ class workshop_random_allocator implements workshop_allocator {
protected function filter_current_assessments(&$newallocations, $assessments) {
foreach ($assessments as $assessment) {
$allocation = array($assessment->reviewerid => $assessment->authorid);
$foundat = array_keys($newallocations, $allocation);
$foundat = moodle_array_keys_filter($newallocations, $allocation);
$newallocations = array_diff_key($newallocations, array_flip($foundat));
}
}

View File

@ -183,7 +183,7 @@ class workshop_best_evaluation extends workshop_evaluation {
}
// identify the best assessments - that is those with the shortest distance from the best assessment
$bestids = array_keys($distances, min($distances));
$bestids = moodle_array_keys_filter($distances, min($distances));
// for every assessment, calculate its distance from the nearest best assessment
$distances = array();

View File

@ -446,7 +446,7 @@ class qformat_blackboard_six_pool extends qformat_blackboard_six_base {
$choiceid = $this->getpath($choice,
array('@', 'id'), '', true);
$fiber = array_search($choiceid, $mappings);
$fiber = array_keys ($mappings, $choiceid);
$fiber = moodle_array_keys_filter($mappings, $choiceid);
foreach ($fiber as $correctanswerid) {
// We have found a correspondance for this choice so we need to take the associated answer.
foreach ($answers as $answer) {

View File

@ -837,7 +837,7 @@ class qformat_blackboard_six_qti extends qformat_blackboard_six_base {
if ($subanswertext != '') { // Only import non empty subanswers.
$subquestion = '';
$fiber = array_keys ($mappings, $choiceid);
$fiber = moodle_array_keys_filter($mappings, $choiceid);
foreach ($fiber as $correctanswerid) {
// We have found a correspondance for this subanswer so we need to take the associated subquestion.
foreach ($quest->RESPONSE_BLOCK->subquestions as $qid => $subq) {

View File

@ -56,7 +56,7 @@ class qtype_ddwtos_edit_form extends qtype_gapselect_edit_form_base {
protected function extra_slot_validation(array $slots, array $choices): ?string {
foreach ($slots as $slot) {
if (count(array_keys($slots, $slot)) > 1) {
if (count(array_filter($slots, fn($value) => $value == $slot)) > 1) {
$choice = $choices[$slot - 1];
if (!isset($choice['infinite']) || $choice['infinite'] != 1) {
return get_string('errorlimitedchoice', 'qtype_ddwtos',