Skip to content

Commit

Permalink
MDL-79890 core: Update uses of array_keys with multiple params
Browse files Browse the repository at this point in the history
These now call the newly minted `moodle_array_keys_filter` method.
  • Loading branch information
andrewnicols committed Nov 15, 2023
1 parent 0efbc79 commit 75587e2
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions course/lib.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion files/classes/converter.php
Expand Up @@ -165,7 +165,7 @@ public function poll_conversion(conversion $conversion) {
*/
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];
Expand Down
2 changes: 1 addition & 1 deletion lib/grade/grade_category.php
Expand Up @@ -1097,7 +1097,7 @@ public function aggregate_values_and_adjust_bounds($grade_values,
$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.
Expand Down
6 changes: 3 additions & 3 deletions lib/horde/framework/Horde/Imap/Client/Cache/Backend/Cache.php
Expand Up @@ -143,7 +143,7 @@ public function save()

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];
Expand Down Expand Up @@ -297,7 +297,7 @@ public function deleteMsgs($mailbox, $uids)
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));
Expand Down Expand Up @@ -416,7 +416,7 @@ protected function _loadSlice($mailbox, $slice)
$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]);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/horde/readme_moodle.txt
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/listlib.php
Expand Up @@ -628,7 +628,7 @@ public function image_spacer() {
*/
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);
Expand Down
3 changes: 2 additions & 1 deletion lib/moodlelib.php
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion mod/workshop/allocation/random/lib.php
Expand Up @@ -702,7 +702,7 @@ protected function shuffle_assoc(&$array) {
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));
}
}
Expand Down
2 changes: 1 addition & 1 deletion mod/workshop/eval/best/lib.php
Expand Up @@ -183,7 +183,7 @@ protected function process_assessments(array $assessments, array $diminfo, stdcl
}

// 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();
Expand Down
2 changes: 1 addition & 1 deletion question/format/blackboard_six/formatpool.php
Expand Up @@ -446,7 +446,7 @@ public function process_matching($xml, &$questions) {
$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) {
Expand Down
2 changes: 1 addition & 1 deletion question/format/blackboard_six/formatqti.php
Expand Up @@ -837,7 +837,7 @@ public function process_matching($quest, &$questions) {
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) {
Expand Down
2 changes: 1 addition & 1 deletion question/type/ddwtos/edit_ddwtos_form.php
Expand Up @@ -56,7 +56,7 @@ protected function choice_group($mform) {

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',
Expand Down

0 comments on commit 75587e2

Please sign in to comment.