From 4fe6c22c9e3cba3e959298c67d7916dc8c11dfa4 Mon Sep 17 00:00:00 2001
From: Robert Munteanu <robert.munteanu@gmail.com>
Date: Wed, 21 Oct 2009 15:45:12 +0300
Subject: [PATCH 2/3] Issue #8612: MantisConnect does not expose Time Tracking on Notes or Issues

Expose the invidual time spent on each IssueNoteData. Checks are performed
to see if the user has the required access level.

A new utility function mci_get_time_tracking_from_note has been added to
centralise access control checks and conversions.
---
 api/soap/mantisconnect.php |    3 ++-
 api/soap/mc_api.php        |   19 +++++++++++++++++++
 api/soap/mc_issue_api.php  |   24 +++++++++++++++---------
 3 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/api/soap/mantisconnect.php b/api/soap/mantisconnect.php
index ca0f4cc..c757cc7 100644
--- a/api/soap/mantisconnect.php
+++ b/api/soap/mantisconnect.php
@@ -211,7 +211,8 @@ $l_oServer->wsdl->addComplexType(
 		'text'				=>	array( 'name' => 'text',			'type' => 'xsd:string', 'minOccurs' => '0'),
 		'view_state'		=>	array( 'name' => 'view_state',		'type' => 'tns:ObjectRef', 'minOccurs' => '0'),
 		'date_submitted'	=>	array( 'name' => 'date_submitted',	'type' => 'xsd:dateTime', 'minOccurs' => '0'),
-		'last_modified'		=>	array( 'name' => 'last_modified',	'type' => 'xsd:dateTime', 'minOccurs' => '0')
+		'last_modified'		=>	array( 'name' => 'last_modified',	'type' => 'xsd:dateTime', 'minOccurs' => '0'),
+		'time_tracking'		=> 	array( 'name' => 'time_tracking',	'type' => 'xsd:integer', 'minOccurs' => '0')
 	)
 );
 
diff --git a/api/soap/mc_api.php b/api/soap/mc_api.php
index 707808f..52c61de 100644
--- a/api/soap/mc_api.php
+++ b/api/soap/mc_api.php
@@ -329,6 +329,25 @@ function mci_category_as_array_by_id( $p_category_id ) {
 }
 
 /**
+ * Returns time tracking information from a bug note.
+ * 
+ * @param int $p_issue_id The id of the issue
+ * @param Array $p_note A note as passed to the soap api methods
+ * 
+ * @return String the string time entry to be added to the bugnote, in 'HH:mm' format
+ */
+function mci_get_time_tracking_from_note( $p_issue_id, $p_note) {
+	
+	if ( !access_has_bug_level( config_get( 'time_tracking_view_threshold' ), $p_issue_id ) )
+		return '00:00';
+
+	if ( !isset( $p_note['time_tracking'] ))
+		return '00:00';
+		
+	return db_minutes_to_hhmm($p_note['time_tracking']);
+}
+
+/**
  * SECURITY NOTE: these globals are initialized here to prevent them
  * being spoofed if register_globals is turned on
  */
diff --git a/api/soap/mc_issue_api.php b/api/soap/mc_issue_api.php
index f2d5536..7b93a9b 100644
--- a/api/soap/mc_issue_api.php
+++ b/api/soap/mc_issue_api.php
@@ -258,7 +258,8 @@ function mci_issue_get_notes( $p_issue_id ) {
 	$t_lang = mci_get_user_lang( $t_user_id );
 	$t_project_id = bug_get_field( $p_issue_id, 'project_id' );
 	$t_user_bugnote_order = 'ASC'; // always get the notes in ascending order for consistency to the calling application.
-
+	$t_has_time_tracking_access = access_has_bug_level( config_get( 'time_tracking_view_threshold' ), $p_issue_id );
+	
 	$t_result = array();
 	foreach( bugnote_get_all_visible_bugnotes( $p_issue_id, $t_user_bugnote_order, 0 ) as $t_value ) {
 		$t_bugnote = array();
@@ -268,6 +269,8 @@ function mci_issue_get_notes( $p_issue_id ) {
 		$t_bugnote['last_modified'] = timestamp_to_iso8601( $t_value->last_modified );
 		$t_bugnote['text'] = $t_value->note;
 		$t_bugnote['view_state'] = mci_enum_get_array_by_id( $t_value->view_state, 'view_state', $t_lang );
+		$t_bugnote['time_tracking'] = $t_has_time_tracking_access ? $t_value->time_tracking : 0;
+		
 		$t_result[] = $t_bugnote;
 	}
 
@@ -419,7 +422,7 @@ function mc_issue_add( $p_username, $p_password, $p_issue ) {
 	$t_priority_id = isset( $p_issue['priority'] ) ? mci_get_priority_id( $p_issue['priority'] ) : config_get( 'default_bug_priority' );
 	$t_severity_id = isset( $p_issue['severity'] ) ?  mci_get_severity_id( $p_issue['severity'] ) : config_get( 'default_bug_severity' );
 	$t_status_id = isset ( $p_issue['status'] ) ? mci_get_status_id( $p_issue['status'] ) : config_get( 'bug_submit_status' );
-	$t_reproducibility_id = isset ( $p_issue['reproducibility'] ) ?  mci_get_reproducibility_id( $p_issue['reproducibility'] ) : config_get( 'default_bug_reproducibility' );
+	$t_reproducibility_id = isset ( $p_issue['reproducibility'] ) ? mci_get_reproducibility_id( $p_issue['reproducibility'] ) : config_get( 'default_bug_reproducibility' );
 	$t_resolution_id =  isset ( $p_issue['resolution'] ) ? mci_get_resolution_id( $p_issue['resolution'] ) : config_get('default_bug_resolution');
 	$t_projection_id = isset ( $p_issue['projection'] ) ? mci_get_projection_id( $p_issue['projection'] ) : config_get('default_bug_resolution');
 	$t_eta_id = isset ( $p_issue['eta'] ) ? mci_get_eta_id( $p_issue['eta'] ) : config_get('default_bug_eta');
@@ -535,7 +538,7 @@ function mc_issue_add( $p_username, $p_password, $p_issue ) {
 	$t_issue_id = $t_bug_data->create();
 
 	mci_issue_set_custom_fields( $t_issue_id, $p_issue['custom_fields'] );
-
+	
 	if( isset( $t_notes ) && is_array( $t_notes ) ) {
 		foreach( $t_notes as $t_note ) {
 			if( isset( $t_note['view_state'] ) ) {
@@ -545,7 +548,7 @@ function mc_issue_add( $p_username, $p_password, $p_issue ) {
 			}
 
 			$t_view_state_id = mci_get_enum_id_from_objectref( 'view_state', $t_view_state );
-			bugnote_add( $t_issue_id, $t_note['text'], '0:00', $t_view_state_id == VS_PRIVATE, BUGNOTE, '', $t_user_id, FALSE );
+			bugnote_add( $t_issue_id, $t_note['text'], mci_get_time_tracking_from_note($t_issue_id, $t_note), $t_view_state_id == VS_PRIVATE, BUGNOTE, '', $t_user_id, FALSE );
 		}
 	}
 
@@ -700,9 +703,9 @@ function mc_issue_update( $p_username, $p_password, $p_issue_id, $p_issue ) {
 
 	# submit the issue
 	$t_is_success = $t_bug_data->update( /* update_extended */ true, /* bypass_email */ true );
-
+	
 	mci_issue_set_custom_fields( $p_issue_id, $p_issue['custom_fields'] );
-
+	
 	if ( isset( $p_issue['notes'] ) && is_array( $p_issue['notes'] ) ) {
 		foreach ( $p_issue['notes'] as $t_note ) {
 			if ( isset( $t_note['view_state'] ) ) {
@@ -718,10 +721,13 @@ function mc_issue_update( $p_username, $p_password, $p_issue_id, $p_issue ) {
 					bugnote_set_text( $t_bugnote_id, $t_note['text'] );
 					bugnote_set_view_state( $t_bugnote_id, $t_view_state_id == VS_PRIVATE );
 					bugnote_date_update( $t_bugnote_id );
+					if ( isset( $t_note['time_tracking'] ))
+						bugnote_set_time_tracking($t_bugnote_id, mci_get_time_tracking_from_note($p_issue_id, $t_note));
 				}
 			} else {
 				$t_view_state_id = mci_get_enum_id_from_objectref( 'view_state', $t_view_state );
-				bugnote_add( $p_issue_id, $t_note['text'], '0:00', $t_view_state_id == VS_PRIVATE, BUGNOTE, '', $t_user_id, FALSE );
+				
+				bugnote_add( $p_issue_id, $t_note['text'], mci_get_time_tracking_from_note($p_issue_id, $t_note), $t_view_state_id == VS_PRIVATE, BUGNOTE, '', $t_user_id, FALSE );
 			}
 		}
 	}
@@ -802,9 +808,9 @@ function mc_issue_note_add( $p_username, $p_password, $p_issue_id, $p_note ) {
 			'id' => config_get( 'default_bug_view_status' ),
 		);
 	}
-
+	
 	$t_view_state_id = mci_get_enum_id_from_objectref( 'view_state', $t_view_state );
-	return bugnote_add( $p_issue_id, $p_note['text'], '0:00', $t_view_state_id == VS_PRIVATE, BUGNOTE, '', $t_user_id );
+	return bugnote_add( $p_issue_id, $p_note['text'], mci_get_time_tracking_from_note($p_issue_id, $p_note), $t_view_state_id == VS_PRIVATE, BUGNOTE, '', $t_user_id );
 }
 
 /**
-- 
1.6.4.2

