<?php
	class PagerMessagesModel {

		public function postMessage( $source_user_id , $target_user_id , $message ) {
			$ins = array();
			$ins["poster_user_id"]=sprintf( "%d" , $source_user_id );
			$ins["target_user_id"]=sprintf( "%d" , $target_user_id );
			$ins["message"]=sprintf( "'%s'" , FNTH_db_addslashes( $message ) );
			FNTH_db_free_result(
				FNTH_db_query(
					FNTH_db_generate_insert_query( "pager_messages" , $ins ) ) );
			return FNTH_db_get_field( "select currval('pager_messages_pager_message_id_seq') as num" , "num" );
		}

		public function acknowledgeMessage( $user_id , $pager_message_id ) {
			$query = sprintf( "update pager_messages set is_target_acknowledged=true where target_user_id=%d and pager_message_id=%d" , $user_id , $pager_message_id );
			FNTH_db_free_result( FNTH_db_query( $query ) );
		}

		public function getUserUnacknowledgedMessages( $user_id ) {
			$query = sprintf( "select u.user_name,pm.* from users as u,pager_messages as pm where
				u.user_id=pm.poster_user_id and
				pm.target_user_id=%d and
				not pm.is_target_acknowledged order by pm.pager_message_id limit 1" , $user_id );
			return FNTH_db_get_resultset( $query );
		}
	}

?>
