<?php
# ---------------------------------------------------------------------
# truc is a tool for requirement and use case tracking
# Copyright (C) 2006 ASDIS - http://sf.net/projects/truc
#
# (rth) Initial truc version based on rth
#       Copyright (C) 2005 George Holbrook - rth@lists.sourceforge.net
#
# This program is distributed under the terms and conditions of the GPL
# See the README and LICENSE files for details
#----------------------------------------------------------------------

if( isset($_POST['submit_button']) ) {

	require_once("test_req_assoc_action.php");
	exit;
}


include"./api/include_api.php";
auth_authenticate_user();


$page = basename(__FILE__);
$page                   = basename(__FILE__);
$action_page            = 'test_req_assoc_action.php';
$project_properties     = session_get_project_properties();
$s_project_name         = $project_properties['project_name'];
$s_project_id			= $project_properties['project_id'];
$s_test_details			= session_set_properties("test", $_GET);
$s_test_id				= $s_test_details['test_id'];
$row_style				= '';
$records 				= "";

$s_test_details		= session_set_properties("test", $_GET);
$test_id			= $s_test_details['test_id'];
$test_name       	= test_get_name($test_id);

$user_properties		= session_get_user_properties();
$user_id				= $user_properties['user_id'];
$username				= $user_properties['username'];
$project_id=$s_project_id;

        if (usecase_locked($project_id,$test_id,$username)) {
                 html_print_operation_failed( 'usecase_edit_page',error_report_msg(CAN_NOT_MODIFY_USECASE_LOCKED,usecase_locked_by($project_id,$test_id)), "test_detail_page.php?test_id=".$test_id);
                 exit;
       }

session_records(	"test_req_assoc",
					test_get_req_assoc_ids($s_test_id) );

$filter_per_page		= 100;
$filter_doc_type		= "";
$filter_status			= "";
$filter_area_covered	= "";
$filter_functionality	= "";
$filter_assign_release	= "";
$filter_show_versions	= "latest";
$filter_search			= "";
$filter_priority		= "";

$order_by 		= REQ_FILENAME;
$order_dir		= "ASC";
$page_number	= 1;

util_set_filter('per_page', $filter_per_page, $_POST);
util_set_filter('doc_type', $filter_doc_type, $_POST);
util_set_filter('status', $filter_status, $_POST);
util_set_filter('area_covered', $filter_area_covered, $_POST);
util_set_filter('functionality', $filter_functionality, $_POST);
util_set_filter('assign_release', $filter_assign_release, $_POST);
util_set_filter('requirement_search', $filter_search, $_POST);
util_set_filter('priority', $filter_priority, $_POST);

util_set_order_by($order_by, $_POST);
util_set_order_dir($order_dir, $_POST);
util_set_page_number($page_number, $_POST);

# Set or Reset the % covered session vars
if( sizeof($_POST) ) {
	session_validate_form_set($_POST);
} else {
	session_validate_form_reset();
}

html_window_title();
html_print_body();
html_page_title($s_project_name ." - ". lang_get('test_req_assoc_page') );
html_page_header( $db, $s_project_name );

html_print_menu();
test_menu_print ($page);

error_report_check( $_GET );

print"<br>\n";

print"<table class=width100>\n";

print"<tr>\n";
print"<th width='50%'>". lang_get('test_id') ."</td>\n";
print"<th width='50%'>". lang_get('test_name') ."</td>\n";
print"</tr>\n";

print"<tr>\n";
print"<td class=grid-data-c><a href='test_detail_page.php?test_id=$test_id'>". sprintf("%05s",$test_id) ."</a></td>\n";
print"<td class=grid-data-c>$test_name</td>\n";
print"</tr>\n";

print"</table>\n";

print"<form method=post action='$page'>\n";

print"<br>\n";
print"<div align=center>\n";

# Filtering for all versions doesn't make sense here
html_print_requirements_filter(	$s_project_id,
								$filter_doc_type,
								$filter_status,
								$filter_area_covered,
								$filter_functionality,
								$filter_assign_release,
								$filter_per_page,
								$filter_show_versions=null,
								$filter_search,
								$filter_priority );

								
print"</div>\n";

print"<br>";

$rows_requirement = requirement_get( $s_project_id, 
									 $page_number, 
									 $order_by, 
									 $order_dir, 
									 $filter_doc_type, 
									 $filter_status,
									 $filter_area_covered, 
									 $filter_functionality,
									 $filter_assign_release, 
									 $filter_show_versions, 
									 $filter_per_page,
									 $filter_search,
									 $filter_priority);
						
						
################################################################################
# Testset table

if($rows_requirement) {


	print"<div align=center>\n";

	print"<table class=width100 rules=cols>\n";

	# Table headers
	print"<tr class=tbl_header>\n";
	print"<th></th>";
	html_tbl_print_header( lang_get('req_id'), REQ_ID, $order_by, $order_dir );
	html_tbl_print_header( lang_get('req_name'), REQ_FILENAME, $order_by, $order_dir );
	html_tbl_print_header( lang_get('req_detail'), REQ_VERS_DETAIL, $order_by, $order_dir );
	html_tbl_print_header( lang_get('req_type'), REQ_DOC_TYPE_NAME, $order_by, $order_dir );
	html_tbl_print_header( lang_get('status'), REQ_VERS_STATUS, $order_by, $order_dir );
	html_tbl_print_header( lang_get('req_area'), REQ_AREA_COVERAGE, $order_by, $order_dir );
	html_tbl_print_header( lang_get('functionality') );
	html_tbl_print_header( lang_get('req_locked_by'), REQ_LOCKED_BY, $order_by, $order_dir );
	html_tbl_print_header( lang_get('req_locked_date'),	REQ_LOCKED_DATE, $order_by, $order_dir );
	print"</tr>\n";

	foreach($rows_requirement as $row_requirement) {

		$req_id					= $row_requirement[REQ_ID];
		$req_version_id			= $row_requirement[REQ_VERS_UNIQUE_ID];

		# Name of the % covered text input
		$pc_covered_input_name = "percent_covered_". $req_id;

		# Get the % covered from db
		$percent_covered = test_requirement_get_pc_covered($s_test_id, $req_id);

		# Save and get the % covered from the session
		$percent_covered = session_validate_form_get_field($pc_covered_input_name, $percent_covered);

		$row_style = html_tbl_alternate_bgcolor($row_style);

		$rows_functions = requirement_get_functionality($s_project_id, $row_requirement[REQ_ID]);

		if( session_records_ischecked("test_req_assoc", $req_id) ) {
			$checked = "checked";
		} else {
			$checked = "";
		}

		# Build list of records
		if( empty($records) ) {
			$records = $req_id." => ''";
		} else {
			$records .= ", ".$req_id." => ''";
		}


		# Rows
		print"<tr class='$row_style'>\n";
		print"<td><input type='checkbox' name='row_$req_id' $checked></td>";
		print"<td>".util_pad_id($row_requirement[REQ_ID])."</td>\n";
		print"<td class='tbl-l'>".$row_requirement[REQ_FILENAME]."</td>\n";
		print"<td class='tbl-l'>".$row_requirement[REQ_VERS_DETAIL]."</td>\n";
		print"<td class='tbl-l'>".$row_requirement[REQ_DOC_TYPE_NAME]."</td>\n";
		print"<td class='tbl-l'>".$row_requirement[REQ_VERS_STATUS]."</td>\n";
		print"<td class='tbl-l'>".$row_requirement[REQ_AREA_COVERAGE]."</td>\n";
		print"<td class='tbl-l'>";
		foreach($rows_functions as $key => $value) {

			print$value."<br>";
		}
		print"</td>\n";
		print"<td class='tbl-l'>".$row_requirement[REQ_LOCKED_BY]."</td>\n";
		print"<td class='tbl-l'>".$row_requirement[REQ_LOCKED_DATE]."</td>\n";
		print"</tr>\n";
	}

	print"</table>\n";

	print"</div>\n";

	//print lang_get("update").": &nbsp;\n";
	if( session_use_javascript() ) {
		print"<input id=select_all type=checkbox name=thispage onClick='checkAll( this )'>\n";
		print"<label for=select_all>".lang_get("select_all")."</label>";
		print"&nbsp;\n";
	}

	print"<div align=center>\n";
	print"<input type='submit' name=submit_button value='".lang_get("edit")."'>\n";
	print"</div>\n";

} else {

	print lang_get("no_requirements");
}

print"</div>\n";

print"<input type=hidden name=records value=\"$records\">\n";
print"</form>\n";

html_print_footer();
?>