Commit c6c6c295 authored by Rebecca Blundell's avatar Rebecca Blundell Committed by Robert Lyon
Browse files

Bug 1752743: replace php 7.2 deprecated functions

replaced each() with foreach in core code.
replaced create_function() with anonymous function in core code.

Also removed some functions that were not used anymore

behatnotneeded

Change-Id: I300396a1ec04c75d7882b7ec5c9de76c3763e70d
parent 4ff97913
......@@ -253,7 +253,7 @@ function user_authorise($token, $useragent) {
// load existing profile information
$profilefields = array();
$profile_data = get_records_select_assoc('artefact', "owner=? AND artefacttype IN (" . join(",",array_map(create_function('$a','return db_quote($a);'),array_keys($element_list))) . ")", array($USER->get('id')), '','artefacttype, title');
$profile_data = get_records_select_assoc('artefact', "owner=? AND artefacttype IN (" . join(",",array_map( function($a) { return db_quote($a); },array_keys($element_list))) . ")", array($USER->get('id')), '','artefacttype, title');
if ($profile_data == false) {
$profile_data = array();
}
......
......@@ -1040,7 +1040,7 @@ class ArtefactTypeBlogPost extends ArtefactType {
}
// Get the attached files.
$postids = array_map(create_function('$a', 'return $a->id;'), $data);
$postids = array_map(function ($a) { return $a->id; }, $data);
$files = ArtefactType::attachments_from_id_list($postids);
if ($files) {
safe_require('artefact', 'file');
......
......@@ -165,7 +165,7 @@ class PluginBlocktypeInternalmedia extends MaharaCoreBlocktype {
'defaultvalue' => in_array($filetype, $currenttypes),
);
}
uasort($filetypes, create_function('$a, $b', 'return $a["title"] > $b["title"];'));
uasort($filetypes, function($a, $b) { return $a["title"] > $b["title"]; });
$options = array_merge(
array(
'description' => array(
......
......@@ -81,8 +81,8 @@ class HtmlExportFile extends HtmlExportArtefactPlugin {
public function get_summary() {
$smarty = $this->exporter->get_smarty();
$smarty->assign('filecount', count(array_filter($this->artefactdata, create_function('$a', 'return $a->get("artefacttype") != "folder";'))));
$smarty->assign('foldercount', count(array_filter($this->artefactdata, create_function('$a', 'return $a->get("artefacttype") == "folder";'))));
$smarty->assign('filecount', count(array_filter($this->artefactdata, function($a) { return $a->get("artefacttype") != "folder"; })));
$smarty->assign('foldercount', count(array_filter($this->artefactdata, function($a) { return $a->get("artefacttype") == "folder"; })));
$smarty->assign('spaceused', $this->exporter->get('user')->get('quotaused'));
return array(
......@@ -203,11 +203,11 @@ class HtmlExportFile extends HtmlExportArtefactPlugin {
$data = array();
$equality = ($folders) ? '==' : '!=';
$parent = (is_null($parent)) ? 'null': intval($parent);
$artefacts = array_filter($this->artefactdata, create_function('$a',
'return $a->get("parent") == ' . $parent
. ' && $a->get("artefacttype") ' . $equality . ' "folder"'
. ' && $a->get("owner") == ' . $this->owner . ';'
));
$this_owner = $this->owner;
$artefacts = array_filter($this->artefactdata, function($a) use ($parent, $equality, $this_owner) {
if ($a->get("parent") == $parent && $a->get("artefacttype") . $equality . "folder" && $a->get("owner") == $this_owner) { return true; };
});
foreach ($artefacts as $artefact) {
$size = '';
if ($artefact->get('artefacttype') != 'folder') {
......
......@@ -311,7 +311,7 @@ class PluginArtefactFile extends PluginArtefact {
GROUP BY a.owner", array()
);
if ($data) {
return array_map(create_function('$a', 'return $a->bytes;'), $data);
return array_map(function($a) { return $a->bytes; }, $data);
}
return array();
}
......@@ -326,7 +326,7 @@ class PluginArtefactFile extends PluginArtefact {
GROUP BY a.group", array()
);
if ($data) {
return array_map(create_function('$a', 'return $a->bytes;'), $data);
return array_map(function($a) { return $a->bytes; }, $data);
}
return array();
}
......
......@@ -61,7 +61,7 @@ class HtmlExportInternal extends HtmlExportArtefactPlugin {
// Export all profile fields except 'socialprofile'
unset($elementlist['socialprofile']);
$profilefields = get_column_sql('SELECT id FROM {artefact} WHERE "owner" = ? AND artefacttype IN ('
. join(",",array_map(create_function('$a','return db_quote($a);'), array_keys($elementlist)))
. join(",",array_map(function($a) { return db_quote($a); }, array_keys($elementlist)))
. ")", array($this->exporter->get('user')->get('id')));
foreach ($profilefields as $id) {
$artefact = artefact_instance_from_id($id);
......@@ -92,7 +92,7 @@ class HtmlExportInternal extends HtmlExportArtefactPlugin {
// Sort the data and then drop the weighting information
foreach ($sections as &$section) {
uasort($section, create_function('$a, $b', 'return $a["weight"] > $b["weight"];'));
uasort($section, function($a, $b) { return $a["weight"] > $b["weight"]; });
foreach ($section as &$data) {
$data = $data['html'];
}
......
......@@ -27,7 +27,7 @@ $element_required = ArtefactTypeProfile::get_mandatory_fields();
// load existing profile fields
$profilefields = array();
$profile_data = get_records_select_array('artefact', "owner=? AND artefacttype IN (" . join(",",array_map(create_function('$a','return db_quote($a);'),array_keys($element_list))) . ")", array($USER->get('id')));
$profile_data = get_records_select_array('artefact', "owner=? AND artefacttype IN (" . join(",",array_map(function($a) { return db_quote($a); },array_keys($element_list))) . ")", array($USER->get('id')));
if ($profile_data) {
foreach ($profile_data as $field) {
......
......@@ -1102,7 +1102,7 @@ abstract class ArtefactType implements IArtefactType {
$this->set('parent', $artefactcopies[$copyinfo->oldparent]->newid);
}
else {
$this->set('parent', $this->default_parent_for_copy($view, $template, array_map(create_function('$a', 'return $a->newid;'), $artefactcopies)));
$this->set('parent', $this->default_parent_for_copy($view, $template, array_map(function($a) { return $a->newid; }, $artefactcopies)));
}
}
......
......@@ -738,7 +738,14 @@ function auth_get_available_auth_types($institution=null) {
$row->description = get_string('notusable', 'auth.' . $row->name);
}
}
usort($result, create_function('$a, $b', 'if ($a->is_usable != $b->is_usable) return $b->is_usable; return strnatcasecmp($a->title, $b->title);'));
usort($result, function($a, $b) {
if ($a->is_usable != $b->is_usable) {
return $b->is_usable;
}
else {
return strnatcasecmp($a->title, $b->title);
}
});
return $result;
}
......@@ -1716,89 +1723,91 @@ function login_submit(Pieform $form, $values) {
$USER->username = $username;
reset($authinstances);
while ((list(, $authinstance) = each($authinstances)) && false == $authenticated) {
$auth = AuthFactory::create($authinstance->id);
if (!$auth->can_auto_create_users()) {
continue;
}
// catch semi-fatal auth errors, but allow next auth instance to be
// tried
try {
if ($auth->authenticate_user_account($USER, $password)) {
$authenticated = true;
foreach ($authinstances as $authinstance) {
if (false == $authenticated) {
$auth = AuthFactory::create($authinstance->id);
if (!$auth->can_auto_create_users()) {
continue;
}
else {
// catch semi-fatal auth errors, but allow next auth instance to be
// tried
try {
if ($auth->authenticate_user_account($USER, $password)) {
$authenticated = true;
}
else {
continue;
}
}
catch (AuthInstanceException $e) {
continue;
}
catch (UninitialisedAuthException $ue) {
log_info('Malformed auth instance "' . $auth->instancename . '" for institution "' . $auth->institution . '"');
continue;
}
}
catch (AuthInstanceException $e) {
continue;
}
catch (UninitialisedAuthException $ue) {
log_info('Malformed auth instance "' . $auth->instancename . '" for institution "' . $auth->institution . '"');
continue;
}
// Check now to see if the institution has its maximum quota of users
require_once('institution.php');
$institution = new Institution($authinstance->institution);
if ($institution->isFull()) {
$institution->send_admin_institution_is_full_message();
throw new AuthUnknownUserException('Institution has too many users');
}
// Check now to see if the institution has its maximum quota of users
require_once('institution.php');
$institution = new Institution($authinstance->institution);
if ($institution->isFull()) {
$institution->send_admin_institution_is_full_message();
throw new AuthUnknownUserException('Institution has too many users');
}
$USER->authinstance = $authinstance->id;
$userdata = $auth->get_user_info($username);
if ($userdata instanceof User) {
$userdata->reanimate($userdata->id, $authinstance->id);
return;
}
if (empty($userdata)) {
throw new AuthUnknownUserException("\"$username\" is not known");
}
$USER->authinstance = $authinstance->id;
$userdata = $auth->get_user_info($username);
if ($userdata instanceof User) {
$userdata->reanimate($userdata->id, $authinstance->id);
return;
}
if (empty($userdata)) {
throw new AuthUnknownUserException("\"$username\" is not known");
}
// Check for a suspended institution
if ($authinstance->suspended) {
$sitename = get_config('sitename');
throw new AccessTotallyDeniedException(get_string('accesstotallydenied_institutionsuspended', 'mahara', $authinstance->displayname, $sitename));
}
// Check for a suspended institution
if ($authinstance->suspended) {
$sitename = get_config('sitename');
throw new AccessTotallyDeniedException(get_string('accesstotallydenied_institutionsuspended', 'mahara', $authinstance->displayname, $sitename));
}
// We have the data - create the user
$USER->lastlogin = db_format_timestamp(time());
if (isset($userdata->firstname)) {
$USER->firstname = sanitize_firstname($userdata->firstname);
}
if (isset($userdata->lastname)) {
$USER->lastname = sanitize_firstname($userdata->lastname);
}
if (isset($userdata->email)) {
$USER->email = sanitize_email($userdata->email);
}
else {
// The user will be asked to populate this when they log in.
$USER->email = null;
}
// We have the data - create the user
$USER->lastlogin = db_format_timestamp(time());
if (isset($userdata->firstname)) {
$USER->firstname = sanitize_firstname($userdata->firstname);
}
if (isset($userdata->lastname)) {
$USER->lastname = sanitize_firstname($userdata->lastname);
}
if (isset($userdata->email)) {
$USER->email = sanitize_email($userdata->email);
}
else {
// The user will be asked to populate this when they log in.
$USER->email = null;
}
$profilefields = array();
foreach (array('studentid', 'preferredname') as $pf) {
if (isset($userdata->$pf)) {
$sanitize = 'sanitize_' . $pf;
if (($USER->$pf = $sanitize($userdata->$pf)) !== '') {
$profilefields[$pf] = $USER->$pf;
$profilefields = array();
foreach (array('studentid', 'preferredname') as $pf) {
if (isset($userdata->$pf)) {
$sanitize = 'sanitize_' . $pf;
if (($USER->$pf = $sanitize($userdata->$pf)) !== '') {
$profilefields[$pf] = $USER->$pf;
}
}
}
}
try {
// If this authinstance is a parent auth for some xmlrpc authinstance, pass it along to create_user
// so that this username also gets recorded as the username for sso from the remote sites.
$remoteauth = $auth->is_parent_authority();
create_user($USER, $profilefields, $institution, $remoteauth);
$USER->reanimate($USER->id, $authinstance->id);
}
catch (Exception $e) {
db_rollback();
throw $e;
try {
// If this authinstance is a parent auth for some xmlrpc authinstance, pass it along to create_user
// so that this username also gets recorded as the username for sso from the remote sites.
$remoteauth = $auth->is_parent_authority();
create_user($USER, $profilefields, $institution, $remoteauth);
$USER->reanimate($USER->id, $authinstance->id);
}
catch (Exception $e) {
db_rollback();
throw $e;
}
}
}
......
......@@ -317,7 +317,8 @@ class User {
*/
protected function populate($data) {
reset($this->defaults);
while(list($key, ) = each($this->defaults)) {
$keys = array_keys($this->defaults);
foreach ($keys as $key) {
if (property_exists($data, $key)) {
$this->set($key, $data->{$key});
}
......@@ -1627,7 +1628,8 @@ class LiveUser extends User {
if ($this->SESSION->is_live()) {
$this->authenticated = true;
while(list($key,) = each($this->defaults)) {
$keys = array_keys($this->defaults);
foreach ($keys as $key) {
$this->get($key);
}
}
......
......@@ -27,7 +27,7 @@ define('TITLE', get_string('groupinteractions', 'group'));
$interactiontypes = array_flip(
array_map(
create_function('$a', 'return $a->name;'),
function($a) { return $a->name; },
plugins_installed('interaction')
)
);
......
......@@ -280,7 +280,7 @@ class PluginImportLeap extends PluginImport {
global $USER;
$html = '<form name="ier" method="POST" action="">';
$installedplugins = array_map(create_function('$a', 'return $a->name;'), plugins_installed('artefact'));
$installedplugins = array_map(function($a) { return $a->name; }, plugins_installed('artefact'));
$orderedimportplugins = array('internal', 'file', 'blog', 'resume', 'plans', 'annotation');
foreach ($orderedimportplugins as $plugin) {
if (!in_array($plugin, $installedplugins)) {
......@@ -310,7 +310,7 @@ class PluginImportLeap extends PluginImport {
* @param $method method name
*/
public function call_import_method_plugins($method) {
$installedplugins = array_map(create_function('$a', 'return $a->name;'), plugins_installed('artefact'));
$installedplugins = array_map(function($a) { return $a->name; }, plugins_installed('artefact'));
$orderedimportplugins = array('internal', 'file', 'blog', 'resume', 'plans', 'annotation', 'comment');
foreach ($orderedimportplugins as $plugin) {
if (!in_array($plugin, $installedplugins)) {
......@@ -626,8 +626,8 @@ class PluginImportLeap extends PluginImport {
// NOTE: the strategy listing should already have the strategies for
// each entry sorted from highest to lowest score. If it doesn't, these
// two sorts fix that, but this should never happen.
//usort($a, create_function('$a, $b', 'return $a["score"] < $b["score"];'));
//usort($b, create_function('$a, $b', 'return $a["score"] < $b["score"];'));
//usort($a, function($a, $b) { return $a["score"] < $b["score"]; });
//usort($b, function($a, $b) { return $a["score"] < $b["score"]; });
return $a[0]['score'] < $b[0]['score'];
}
......@@ -1385,7 +1385,7 @@ class PluginImportLeap extends PluginImport {
$this->trace(" Found block with type {$attrs['blocktype']} at [$rowindex][$colindex][$order]", self::LOG_LEVEL_VERBOSE);
if ($blocktypes_installed === null) {
$blocktypes_installed = array_map(create_function('$a', 'return $a->name;'), plugins_installed('blocktype'));
$blocktypes_installed = array_map(function($a) { return $a->name; }, plugins_installed('blocktype'));
}
if (in_array($attrs['blocktype'], $blocktypes_installed)) {
......
......@@ -499,7 +499,7 @@ EOF;
// elsewhere that allowed this to happen
$forum->weight = $weights[$forum->id]->value;
}
usort($forums, create_function('$a, $b', 'return $a->weight > $b->weight;'));
usort($forums, function($a, $b) { return $a->weight > $b->weight; });
return $forums;
}
......
......@@ -315,56 +315,3 @@ function delete_interaction_submit(Pieform $form, $values) {
redirect('/interaction/' . $instance->get('plugin') . '/index.php?group=' . $instance->get('group'));
}
/**
* creates the information for the interaction_sideblock
*
* @param int $groupid the group the sideblock is for
* @param boolean (optional) $membership whether the user is a member
* @return array containing indices 'name', 'weight', 'data'
*/
function interaction_sideblock($groupid, $membership=true) {
$interactiontypes = array_flip(
array_map(
create_function('$a', 'return $a->name;'),
plugins_installed('interaction')
)
);
if (!$interactions = get_records_select_array('interaction_instance',
'"group" = ? AND deleted = ?', array($groupid, 0),
'plugin, ctime', 'id, plugin, title')) {
$interactions = array();
}
foreach ($interactions as $i) {
if (!is_array($interactiontypes[$i->plugin])) {
$interactiontypes[$i->plugin] = array();
}
$interactiontypes[$i->plugin][] = $i;
}
// Sort them according to how the plugin wants them sorted
if ($interactiontypes) {
foreach ($interactiontypes as $plugin => &$interactions) {
safe_require('interaction', $plugin);
$classname = generate_class_name('interaction', $plugin);
if (method_exists($classname, 'sideblock_sort')) {
$interactions = call_static_method($classname, 'sideblock_sort', $interactions);
}
}
}
$data = array(
'group' => $groupid,
'interactiontypes' => $interactiontypes,
'membership' => $membership,
);
// Add a sideblock for group interactions
return array(
'name' => 'groupinteractions',
'weight' => -5,
'data' => $data
);
}
......@@ -101,9 +101,8 @@ function pieform_element_weight_validate_element($element) {
*/
function pieform_element_weight_returnpolicy_renumber_from_zero($element, $value) {
$element['existing'] = pieform_element_weight_renumber_existing_from_zero($element['existing']);
$element['existing'][] = array('weight' => $value - 0.5, 'id' => null);
usort($element['existing'], create_function('$a, $b', 'return $a["weight"] > $b["weight"];'));
usort($element['existing'], function($a, $b) { return $a["weight"] > $b["weight"]; });
$return = array();
foreach ($element['existing'] as $existing) {
......@@ -113,7 +112,7 @@ function pieform_element_weight_returnpolicy_renumber_from_zero($element, $value
}
function pieform_element_weight_renumber_existing_from_zero($existing) {
usort($existing, create_function('$a, $b', 'return $a["weight"] > $b["weight"];'));
usort($existing, function($a, $b) { return $a["weight"] > $b["weight"]; });
$i = 0;
foreach ($existing as &$item) {
......
......@@ -1708,7 +1708,7 @@ function group_prepare_usergroups_for_display($groups, $returnto='mygroups') {
return;
}
$groupids = array_map(create_function('$a', 'return $a->id;'), $groups);
$groupids = array_map(function($a) { return $a->id; }, $groups);
$groupadmins = group_get_admins($groupids);
$i = 0;
......@@ -1778,7 +1778,7 @@ function build_grouplist_html($query, $limit, $offset, &$count=null, $institutio
$groups = search_group($query, $limit, $offset, 'all', '', $institution);
$count = $groups['count'];
if ($ids = array_map(create_function('$a', 'return intval($a->id);'), $groups['data'])) {
if ($ids = array_map(function($a) { return intval($a->id); }, $groups['data'])) {
$sumsql = "(m.role = 'admin')";
if (is_postgres()) {
$sumsql .= '::int';
......@@ -1960,7 +1960,7 @@ function group_get_grouptypes() {
function group_get_grouptype_options($currentgrouptype=null) {
$groupoptions = array();
$grouptypes = group_get_grouptypes();
$enabled = array_map(create_function('$a', 'return $a->name;'), plugins_installed('grouptype'));
$enabled = array_map(function($a) { return $a->name; }, plugins_installed('grouptype'));
if (is_null($currentgrouptype) || in_array($currentgrouptype, $enabled)) {
$grouptypes = array_intersect($enabled, $grouptypes);
}
......
......@@ -2153,19 +2153,6 @@ function event_find_owner_type($event) {
return array($ownerid, $ownertype);
}
/**
* function to convert an array of objects to
* an array containing one field per place
*
* @param array $array input array
* @param mixed $field field to look for in each object
*/
function mixed_array_to_field_array($array, $field) {
$repl_fun = create_function('$n, $field', '$n = (object)$n; return $n->{$field};');
$fields = array_pad(array(), count($array), $field);
return array_map($repl_fun, $array, $fields);
}
/**
* Used by XMLDB
*/
......@@ -3644,7 +3631,8 @@ function get_real_size($size=0) {
'k' => 1024,
);
while (list($key) = each($scan)) {
$keys = array_keys($scan);
foreach ($keys as $key) {
if (strlen($size) > strlen($key) && substr($size, -strlen($key)) == $key) {
$size = substr($size, 0, -strlen($key)) * $scan[$key];
return $size;
......@@ -3944,7 +3932,7 @@ function get_my_tags($limit=null, $cloud=true, $sort='freq', $excludeinstitution
$t->size = sprintf("%0.1f", $minsize + ($maxsize - $minsize) * $weight);
}
}
usort($tagrecords, create_function('$a,$b', 'return strnatcasecmp($a->tag, $b->tag);'));
usort($tagrecords, function($a, $b) { return strnatcasecmp($a->tag, $b->tag); });
}
foreach ($tagrecords as &$t) {
$t->tagurl = urlencode($t->tag);
......
......@@ -233,7 +233,7 @@ class Pieform {/*{{{*/
}
$this->data['configdirs'] = array_map(
create_function('$a', 'return substr($a, -1) == "/" ? substr($a, 0, -1) : $a;'),
function($a) { return substr($a, -1) == "/" ? substr($a, 0, -1) : $a; },
(array) $this->data['configdirs']);
......
......@@ -977,7 +977,7 @@ function get_group_user_search_results($group, $query, $offset, $limit, $members
);
if ($results['count']) {
$userids = array_map(create_function('$a', 'return $a["id"];'), $results['data']);
$userids = array_map(function($a) { return $a["id"];}, $results['data']);
$introductions = get_records_sql_assoc("SELECT \"owner\", description
FROM {artefact}
WHERE artefacttype = 'introduction'
......
......@@ -2058,7 +2058,7 @@ function get_users_data($userids, $getviews=true) {
function build_userlist_html(&$data, $page, $admingroups) {
if ($data['data']) {
$userlist = array_map(create_function('$u','return (int)$u[\'id\'];'), $data['data']);
$userlist = array_map(function($u) { return (int)$u['id']; }, $data['data']);
$userdata = get_users_data($userlist, $page == 'myfriends');
}
$smarty = smarty_core();
......@@ -3083,7 +3083,7 @@ function get_onlineusers($limit=10, $offset=0, $orderby='firstname,lastname') {
else {
$onlineusers = array();
}
$result['data'] = array_map(create_function('$a', 'return $a->id;'), $onlineusers);
$result['data'] = array_map(function($a) { return $a->id; }, $onlineusers);
return $result;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment