Permalink
Browse files

Optimise code in print_user_option_list()

Improve performance of code building the list of users for all projects
introduced in commit 21746dd.

By using an associative array, the foreach loop to remove duplicates can
be simplified (no need for an if statement) and we can also remove the
current user from the list more easily.

Affects issue #10130
  • Loading branch information...
1 parent 21746dd commit dab0021e5df378e2970ab130fa1ddd539c12c38b @dregad dregad committed Dec 12, 2012
Showing with 15 additions and 16 deletions.
  1. +15 −16 core/print_api.php
View
@@ -219,35 +219,34 @@ function print_captcha_input( $p_field_name ) {
#
# @todo from print_reporter_option_list
function print_user_option_list( $p_user_id, $p_project_id = null, $p_access = ANYBODY ) {
- $t_users = array();
- $t_users_temp = array();
+
if( null === $p_project_id ) {
$p_project_id = helper_get_current_project();
}
if( $p_project_id === ALL_PROJECTS ) {
$t_projects = user_get_accessible_projects( auth_get_current_user_id() );
- foreach( $t_projects as $t_project_id ){
+
+ # Get list of users having access level for all accessible projects
+ $t_users_temp = array();
+ foreach( $t_projects as $t_project_id ) {
$t_project_users_list = project_get_all_user_rows( $t_project_id, $p_access );
- $t_users_temp = array_merge( $t_users_temp,$t_project_users_list );
+ $t_users_temp = array_merge( $t_users_temp, $t_project_users_list );
}
-
- $t_users_id_list = array();
- # Remove current user from list (there is a "myself" value)
- $t_users_id_list[] = auth_get_current_user_id();
-
- # Deleting duplicate
+
+ # Build list of users as an associative array (to remove duplicates)
foreach( $t_users_temp as $t_user ) {
- if( !in_array($t_user['id'],$t_users_id_list ) ) {
- $t_users_id_list[] = $t_user['id'];
- $t_users[] = $t_user;
- }
+ $t_users[$t_user['id']] = $t_user;
+ }
+
+ # Remove current user from list (there is a "myself" value)
+ if( array_key_exists( auth_get_current_user_id(), $t_users ) ) {
+ unset( $t_users[auth_get_current_user_id()] );
}
} else {
$t_users = project_get_all_user_rows( $p_project_id, $p_access );
}
-
$t_display = array();
$t_sort = array();
$t_show_realname = ( ON == config_get( 'show_realname' ) );
@@ -482,7 +481,7 @@ function print_note_option_list( $p_user_id = '', $p_project_id = null, $p_thres
if ( null === $p_threshold ) {
$p_threshold = config_get( 'add_bugnote_threshold' );
}
-
+
print_user_option_list( $p_user_id, $p_project_id, $p_threshold );
}

0 comments on commit dab0021

Please sign in to comment.