From f93c18bc90d1fda86ec66ce9c33f984a77a40fdc Mon Sep 17 00:00:00 2001
From: Damien Regad <damien.regad@merckserono.net>
Date: Tue, 23 Nov 2010 22:27:30 +0100
Subject: [PATCH] Fix #12557: Select user from list when making them monitor an issue

This is more user friendly than having to type the the username, particularly
when Mantis is configured to display Realnames
---
 bug_monitor_add.php           |   14 +++-----
 bug_monitor_list_view_inc.php |   78 ++++++++++++++++++++++++++++++++++-------
 lang/strings_english.txt      |    2 +-
 3 files changed, 71 insertions(+), 23 deletions(-)

diff --git a/bug_monitor_add.php b/bug_monitor_add.php
index 6bcc027..ce55d4d 100644
--- a/bug_monitor_add.php
+++ b/bug_monitor_add.php
@@ -34,20 +34,16 @@
 	$f_bug_id = gpc_get_int( 'bug_id' );
 	$t_bug = bug_get( $f_bug_id, true );
 	$f_username = gpc_get_string( 'username', '' );
+	$f_user_id = gpc_get_string( 'user_id', '' );
 
 	$t_logged_in_user_id = auth_get_current_user_id();
 
-	if ( is_blank( $f_username ) ) {
+	if ( is_blank( $f_user_id ) ) {
 		$t_user_id = $t_logged_in_user_id;
 	} else {
-		$t_user_id = user_get_id_by_name( $f_username );
-		if ( $t_user_id === false ) {
-			$t_user_id = user_get_id_by_realname( $f_username );
-
-			if ( $t_user_id === false ) {
-				error_parameters( $f_username );
-				trigger_error( ERROR_USER_BY_NAME_NOT_FOUND, E_USER_ERROR );
-			}
+		$t_user_id = $f_user_id;
+		if( 0 == $t_user_id ) {
+			trigger_error( ERROR_USER_BY_ID_NOT_FOUND, E_USER_ERROR );
 		}
 	}
 
diff --git a/bug_monitor_list_view_inc.php b/bug_monitor_list_view_inc.php
index 9a539f5..e2d6c8f 100644
--- a/bug_monitor_list_view_inc.php
+++ b/bug_monitor_list_view_inc.php
@@ -37,12 +37,12 @@ if ( access_has_bug_level( config_get( 'show_monitor_list_threshold' ), $f_bug_i
 	$result = db_query_bound($query, Array( $c_bug_id ) );
 	$num_users = db_num_rows($result);
 
-	$t_users = array();
+	$t_users_monitoring = array();
 	for ( $i = 0; $i < $num_users; $i++ ) {
 		$row = db_fetch_array( $result );
-		$t_users[$i] = $row['user_id'];
+		$t_users_monitoring[$i] = $row['user_id'];
 	}
-	user_cache_array_rows( $t_users );
+	user_cache_array_rows( $t_users_monitoring );
 
 	echo '<a name="monitors" id="monitors" /><br />';
 
@@ -69,23 +69,75 @@ if ( access_has_bug_level( config_get( 'show_monitor_list_threshold' ), $f_bug_i
 			$t_can_delete_others = access_has_bug_level( config_get( 'monitor_delete_others_bug_threshold' ), $f_bug_id ); 
 	 		for ( $i = 0; $i < $num_users; $i++ ) {
 				echo ($i > 0) ? ', ' : '';
-				echo print_user( $t_users[$i] );
+				echo print_user( $t_users_monitoring[$i] );
 				if ( $t_can_delete_others ) {
-					echo ' [<a class="small" href="' . helper_mantis_url( 'bug_monitor_delete.php' ) . '?bug_id=' . $f_bug_id . '&user_id=' . $t_users[$i] . form_security_param( 'bug_monitor_delete' ) . '">' . lang_get( 'delete_link' ) . '</a>]';
+					echo ' [<a class="small" href="' . helper_mantis_url( 'bug_monitor_delete.php' ) 
+						. '?bug_id=' . $f_bug_id . '&user_id=' . $t_users_monitoring[$i] 
+						. form_security_param( 'bug_monitor_delete' ) . '">' 
+						. lang_get( 'delete_link' ) . '</a>]';
 				}
 	 		}
  		}
 
 		if ( access_has_bug_level( config_get( 'monitor_add_others_bug_threshold' ), $f_bug_id ) ) {
-			echo '<br /><br />', lang_get( 'username' );
+
+			# Build list of users who can monitor the bug, excluding those already monitoring it
+			# @@@ Code is mostly based on print_user_option_list - maybe modify that slightly, to avoid code duplication
+			$t_users_can_monitor = project_get_all_user_rows( $g_project_override, config_get( 'monitor_bug_threshold' ) );
+
+			$t_display = array();
+			$t_sort = array();
+			$t_show_realname = ( ON == config_get( 'show_realname' ) );
+			$t_sort_by_last_name = ( ON == config_get( 'sort_by_last_name' ) );
+
+			foreach( $t_users_can_monitor as $key => $t_user ) {
+
+				# If user is already monitoring the issue, remove them from list
+				if( in_array( $t_user['id'], $t_users_monitoring ) ) {
+					unset( $t_users_can_monitor[$key] );
+					continue;
+				}
+
+				$t_user_name = string_attribute( $t_user['username'] );
+				$t_sort_name = utf8_strtolower( $t_user_name );
+				if( $t_show_realname && ( $t_user['realname'] <> '' ) ) {
+					$t_user_name = string_attribute( $t_user['realname'] );
+					if( $t_sort_by_last_name ) {
+						$t_sort_name_bits = explode( ' ', utf8_strtolower( $t_user_name ), 2 );
+						$t_sort_name = ( isset( $t_sort_name_bits[1] ) ? $t_sort_name_bits[1] . ', ' : '' ) . $t_sort_name_bits[0];
+					} else {
+						$t_sort_name = utf8_strtolower( $t_user_name );
+					}
+				}
+				$t_display[] = $t_user_name;
+				$t_sort[] = $t_sort_name;
+			}
+
+			# Display form only if there are users who can monitor the bug
+			if( count( $t_users_can_monitor ) > 0 ) {
+				array_multisort( $t_sort, SORT_ASC, SORT_STRING, $t_users_can_monitor, $t_display );
+				echo '<br /><br />';
+?>
+				<form method="get" action="bug_monitor_add.php">
+				<?php echo form_security_field( 'bug_monitor_add' ) ?>
+					<input type="hidden" name="bug_id" value="<?php echo (integer)$f_bug_id; ?>" />
+					<select name="user_id">
+						<option value="0"></option>";
+<?php
+						# Build selection list with all users who can monitor this bug
+						foreach( $t_users_can_monitor as $key => $t_user ) {
+							echo '<option value="' . $t_user['id'] . '" ';
+							echo '>' . string_attribute( $t_display[$key] ) . '</option>';
+						}
+?>
+					</select>
+
+					<input type="submit" class="button" value="<?php echo lang_get( 'add_user_to_monitor' ) ?>" />
+				</form>
+<?php
+			}
+		}
 ?>
-		<form method="get" action="bug_monitor_add.php">
-		<?php echo form_security_field( 'bug_monitor_add' ) ?>
-			<input type="hidden" name="bug_id" value="<?php echo (integer)$f_bug_id; ?>" />
-			<input type="text" name="username" />
-			<input type="submit" class="button" value="<?php echo lang_get( 'add_user_to_monitor' ) ?>" />
-		</form>
-		<?php } ?>
 	</td>
 </tr>
 </table>
diff --git a/lang/strings_english.txt b/lang/strings_english.txt
index eb21912..da61f2e 100644
--- a/lang/strings_english.txt
+++ b/lang/strings_english.txt
@@ -1248,7 +1248,7 @@ $s_move_bug_button = 'Move';
 $s_attached_files = 'Attached Files';
 $s_publish = 'Publish';
 $s_cached = 'Cached';
-$s_add_user_to_monitor = 'Add';
+$s_add_user_to_monitor = 'Start Monitoring';
 
 # view_bug_inc.php
 
-- 
1.7.1

