function html_release_map($map)
{
    $release_properties = session_get_properties("release");
    $html_top_row = "<tr>" . NEWLINE;
    $html_bottom_row = "<tr>" . NEWLINE;
    foreach ($map as $key) {
        switch ($key) {
            case "release_link":
                $release_name = admin_get_release_name($release_properties['release_id']);
                $html_top_row .= "<td class='sub_menu' nowrap>";
                $html_top_row .= "<b><a href=release_page.php>" . lang_get('release_name') . "</a></b>";
                $html_top_row .= "</td>" . NEWLINE;
                $html_bottom_row .= "<td class='sub_menu' nowrap>";
                $html_bottom_row .= "{$release_name}";
                $html_bottom_row .= "</td>" . NEWLINE;
                break;
            case "build_link":
                $build_name = admin_get_build_name($release_properties['build_id']);
                $html_top_row .= "<td class='sub_menu' nowrap>";
                $html_top_row .= "<b><a href=build_page.php>" . lang_get('build_name') . "</a></b>";
                $html_top_row .= "</td>" . NEWLINE;
                $html_bottom_row .= "<td class='sub_menu' nowrap>";
                $html_bottom_row .= "{$build_name}";
                $html_bottom_row .= "</td>" . NEWLINE;
                break;
            case "testset_link":
                $testset_name = admin_get_testset_name($release_properties['testset_id']);
                $html_top_row .= "<td class='sub_menu' nowrap>";
                $html_top_row .= "<b><a href=testset_page.php>" . lang_get('testset_name') . "</a></b>";
                $html_top_row .= "</td>" . NEWLINE;
                $html_bottom_row .= "<td class='sub_menu' nowrap>";
                $html_bottom_row .= "{$testset_name}";
                $html_bottom_row .= "</td>" . NEWLINE;
                break;
            case "copy_testset_link":
                $testset_name = admin_get_testset_name($release_properties['testset_id']);
                $html_top_row .= "<td class='sub_menu' rowspan=2 nowrap>";
                $html_top_row .= "<b><a href=testset_page.php>" . lang_get('copy_testset_to') . "</a></b>";
                $html_top_row .= "</td>" . NEWLINE;
                $html_bottom_row .= "<td></td>" . NEWLINE;
                break;
            default:
                $html_top_row .= "<td class='sub_menu' nowrap><b>{$key}</b></td>" . NEWLINE;
                $html_bottom_row .= "<td></td>" . NEWLINE;
        }
        $html_top_row .= "<td class='sub_menu' rowspan=2 nowrap>&nbsp;</td>" . NEWLINE;
    }
    $html_top_row .= "</tr>" . NEWLINE;
    $html_bottom_row .= "</tr>" . NEWLINE;
    print "<table width=1 cellspacing=2>" . NEWLINE;
    print $html_top_row;
    print $html_bottom_row;
    print "</table>" . NEWLINE;
}
function results_email($project_id, $release_id, $build_id, $testset_id, $test_id, $recipients, $action)
{
    $display_generic_info = true;
    $display_generic_url = true;
    $generic_url = RTH_URL . "login.php?project_id={$project_id}&page=results_test_run_page.php&release_id={$release_id}&build_id={$build_id}&testset_id={$testset_id}&test_id={$test_id}";
    $username = session_get_username();
    $project_name = session_get_project_name();
    $release_name = admin_get_release_name($release_id);
    $build_name = admin_get_build_name($build_id);
    $testset_name = admin_get_testset_name($testset_id);
    $user_details = user_get_name_by_username($username);
    $first_name = $user_details[USER_FNAME];
    $last_name = $user_details[USER_LNAME];
    $row_test_detail = testset_query_test_details($testset_id, $test_id);
    $test_name = $row_test_detail[TEST_NAME];
    $status = $row_test_detail[TEST_TS_ASSOC_STATUS];
    $finished = $row_test_detail[TEST_TS_ASSOC_FINISHED];
    $assigned_to = $row_test_detail[TEST_TS_ASSOC_ASSIGNED_TO];
    $comments = $row_test_detail[TEST_TS_ASSOC_COMMENTS];
    $root_cause = $row_test_detail[TEST_RESULTS_ROOT_CAUSE];
    # CREATE EMAIL SUBJECT AND MESSAGE
    switch ($action) {
        case "test_run":
            $subject = "RTH: Test Run Notification - {$test_name}";
            $message = "Test {$test_name} has been run by {$first_name} {$last_name}\n" . NEWLINE;
            break;
        case "update_test_result":
            $subject = "RTH: Test Result has been Updated";
            $message = "The test result for {$test_name} has been updated by {$first_name} {$last_name}\n" . NEWLINE;
            break;
    }
    # Generic link to results page if the $generic_url variable has been set
    if ($display_generic_url) {
        $message .= "Click the following link to view the Test Results:" . NEWLINE;
        $message .= "{$generic_url}\n" . NEWLINE;
    }
    if ($display_generic_info) {
        $message .= "Project Name: {$project_name}\r" . NEWLINE;
        $message .= "Release Name: {$release_name}\r" . NEWLINE;
        $message .= "Build Name: {$build_name}\r" . NEWLINE;
        $message .= "TestSet Name: {$testset_name}\r\n\r" . NEWLINE;
        $message .= "Test Name: {$test_name}\r" . NEWLINE;
        $message .= "Status: {$status}\r" . NEWLINE;
        if (!empty($root_cause)) {
            $message .= "Root Cause: {$root_cause}\r" . NEWLINE;
        }
        $message .= "Comments: {$comments}\r\n\r" . NEWLINE;
    }
    email_send($recipients, $subject, $message);
}
$form_name = 'add_testset';
$action_page = 'testset_add_action.php';
$testset_edit_page = 'testset_edit_page.php';
$testset_add_tests_page = 'testset_add_tests_page.php';
$delete_page = 'delete_page.php';
$testset_copy_page = 'testset_copy_page.php';
$testset_edit_description_page = 'testset_edit_description_page.php';
$s_project_properties = session_get_project_properties();
$project_name = $s_project_properties['project_name'];
$project_id = $s_project_properties['project_id'];
$row_style = '';
$s_release_properties = session_set_properties("release", $_GET);
$release_id = $s_release_properties['release_id'];
$release_name = admin_get_release_name($release_id);
$build_id = $s_release_properties['build_id'];
$build_name = admin_get_build_name($build_id);
$s_table_display_options = session_set_display_options("testset", $_GET);
$order_by = $s_table_display_options['order_by'];
$order_dir = $s_table_display_options['order_dir'];
html_window_title();
html_print_body();
html_page_title($project_name . " - " . lang_get('release_page'));
html_page_header($db, $project_name);
html_print_menu();
html_release_map(array("release_link", "build_link", "<a href=testset_page.php>" . lang_get("testsets") . "</a>", lang_get('test_plan_history')));
$rows = testset_get_test_plan_log($_GET['test_plan_id']);
if (!empty($rows)) {
    print "<br><br>";
    print "<table class=width100 rules=cols>" . NEWLINE;
    print "<tr>" . NEWLINE;
    html_tbl_print_header(lang_get("file_name"));
 $project_num_of_finished_ba = $project_status[9];
 $project_num_of_wr_issue = $project_status[10];
 $pc_tests_not_run = 100 / $project_num_of_tests * ($project_num_of_tests - $testset_num_of_tests);
 $pc_passed_in_project = 100 / $project_num_of_tests * $testset_num_of_passed;
 $pc_failed_in_project = 100 / $project_num_of_tests * $testset_num_of_failed;
 $pc_awaiting_review_in_project = 100 / $project_num_of_tests * $testset_num_of_awaiting_review;
 $pc_wip_in_project = 100 / $project_num_of_tests * $testset_num_of_wip;
 $pc_not_started_in_project = 100 / $project_num_of_tests * $testset_num_of_not_started;
 $pc_not_running_in_project = 100 / $project_num_of_tests * $testset_num_of_not_running;
 $pc_finished_qa_in_project = 100 / $project_num_of_tests * $testset_num_of_finished_qa;
 $pc_finished_ba_in_project = 100 / $project_num_of_tests * $testset_num_of_finished_ba;
 $pc_wr_issue_in_project = 100 / $project_num_of_tests * $testset_num_of_wr_issue;
 # start img pie chart
 print "<img src=\"images/pie_chart_2_image.php";
 # chart title
 print "?graph_title=" . lang_get("status_of") . " " . admin_get_build_name($build_id) . ", " . admin_get_release_name($release_id);
 # legend
 print "&amp;legend='Passed','Failed','Awaiting Review','WIP','Not Running','Not Started','Not Used'";
 if ($project_name == "PCA") {
     print ",'Finished: QA Review', 'Finished: Business Review', 'WinRunner Issue'";
 }
 # pie 1 title
 print "&amp;p1_title=" . lang_get("all_tests_in") . " " . $project_name;
 # pie 1 theme
 if ($project_name == "PCA") {
     print "&amp;p1_theme=pca";
 } else {
     print "&amp;p1_theme=test";
 }
 # pie 1 data
 print "&amp;p1_data={$pc_passed_in_project},{$pc_failed_in_project},{$pc_awaiting_review_in_project},{$pc_wip_in_project},{$pc_not_running_in_project},{$pc_not_started_in_project},{$pc_tests_not_run}";
function file_add_test_plan($upload_file_temp_name, $upload_file_name, $build_id, $comments, $redirect_page, $version = "1.0")
{
    global $db;
    $tbl_test_plan = TEST_PLAN;
    $f_test_plan_id = TEST_PLAN . "." . TEST_PLAN_ID;
    $f_test_plan_build_id = TEST_PLAN . "." . TEST_PLAN_BUILDID;
    $f_test_plan_name = TEST_PLAN . "." . TEST_PLAN_NAME;
    $tbl_test_plan_version = TEST_PLAN_VERSION;
    $f_test_plan_version_id = TEST_PLAN_VERSION . "." . TEST_PLAN_VERSION_ID;
    $f_test_plan_version_test_plan_id = TEST_PLAN_VERSION . "." . TEST_PLAN_VERSION_TESTPLANID;
    $f_version = TEST_PLAN_VERSION . "." . TEST_PLAN_VERSION_VERSION;
    $f_uploaded_date = TEST_PLAN_VERSION . "." . TEST_PLAN_VERSION_UPLOADEDDATE;
    $f_uploaded_by = TEST_PLAN_VERSION . "." . TEST_PLAN_VERSION_UPLOADEDBY;
    $f_file_name = TEST_PLAN_VERSION . "." . TEST_PLAN_VERSION_FILENAME;
    $f_comments = TEST_PLAN_VERSION . "." . TEST_PLAN_VERSION_COMMMENTS;
    $current_date = date("Y-m-d H:i:s");
    $project_properties = session_get_project_properties();
    $file_upload_dir = $project_properties['test_plan_upload_path'];
    # create a unique name for the file using the time function
    $file_db_name = "TestPlan" . time() . "-{$upload_file_name}";
    if ($version == "1.0") {
        $q = "SELECT " . TEST_PLAN_NAME . " FROM " . TEST_PLAN . " WHERE " . TEST_PLAN_NAME . " = '{$upload_file_name}' AND " . TEST_PLAN_BUILDID . " = {$build_id}";
        $rs = db_query($db, $q);
        $num_rows = db_num_rows($db, $rs);
        if ($num_rows != 0) {
            error_report_show($redirect_page, DUPLICATE_FILE_NAME);
        }
    }
    if (!isset($upload_file_temp_name) || !is_uploaded_file($upload_file_temp_name)) {
        error_report_show($redirect_page, REQUIRED_FIELD_MISSING);
    }
    # The kid forgot to put sql in a function
    $test_plan_buid_id_f = TEST_PLAN_BUILDID;
    $test_plan_name_f = TEST_PLAN_NAME;
    if (move_uploaded_file($upload_file_temp_name, $file_upload_dir . $file_db_name)) {
        $q_insert = "\tINSERT INTO {$tbl_test_plan}\n\t\t\t\t\t\t\t( {$test_plan_buid_id_f}, {$test_plan_name_f})\n\t\t\t\t\t\tVALUES\n\t\t\t\t\t\t\t('{$build_id}', '{$upload_file_name}')";
        db_query($db, $q_insert);
        # Gets the ID that you just created from the insert, could have used the php insertid function
        # but decided against it as I wasn't sure if the php ODBC functions covered it
        $q_test_plan_id = "\tSELECT " . TEST_PLAN_ID . " FROM " . TEST_PLAN . " WHERE " . TEST_PLAN_BUILDID . " = {$build_id} AND " . TEST_PLAN_NAME . " = '{$upload_file_name}'";
        $test_plan_id = db_get_one($db, $q_test_plan_id);
        # looks like junior forgot to put sql in a function
        $test_plan_id_f = TEST_PLAN_VERSION_TESTPLANID;
        $test_plan_version_vers_f = TEST_PLAN_VERSION_VERSION;
        $uploaded_date_f = TEST_PLAN_VERSION_UPLOADEDDATE;
        $uploaded_by_f = TEST_PLAN_VERSION_UPLOADEDBY;
        $file_name_f = TEST_PLAN_VERSION_FILENAME;
        $comments_f = TEST_PLAN_VERSION_COMMMENTS;
        $q_version = "INSERT INTO {$tbl_test_plan_version}\n\t\t\t\t\t( {$test_plan_id_f}, {$test_plan_version_vers_f}, {$uploaded_date_f}, {$uploaded_by_f}, {$file_name_f}, {$comments_f} )\n\t\t\t\t\t  VALUES\n\t\t\t\t\t  \t('{$test_plan_id}', '{$version}', '{$current_date}', '" . session_get_username() . "', '{$file_db_name}', '{$comments}')";
        db_query($db, $q_version);
        $build_name = admin_get_build_name($build_id);
        /*
        		# Build up the message and format to be sent
        		$subject = $build_name." has had a new document added to it in Tempest";
        		$message = "A new document has been added to ".$build_name." in Tempest. Please check the new document and review any changes that may need to be made to the test.";
        
        		email_send(	user_mail_by_pref( PROJ_USER_EMAIL_TESTSET ),
        					$subject,
        					$message );
        
        		#######################################################################################################
        		#Add entry into the log table for the project
        		#######################################################################################################
        		$page_name = "ADMIN TESTSET";
        		$deletion = 'N';
        		$creation = 'N';
        		$upload = 'Y';
        		$action = "ADDED TEST PLAN TEST DOCUMENT $upload_file_temp_name to Build ID - $build_id";
        
        		log_activity( $page_name, $deletion, $creation, $upload, $action );
        
        		#logfile entry end
        		#######################################################################################################
        */
    } else {
        error_report_show($redirect_page, FAILED_FILE_UPLOAD);
    }
}
 html_tbl_print_header(lang_get('testset_name'));
 html_tbl_print_header(lang_get('build_name'));
 html_tbl_print_header(lang_get('release_name'));
 html_tbl_print_header(lang_get('tests'));
 # Display table headers based on the statuses of the project
 foreach ($statuses as $status) {
     html_tbl_print_header($status);
 }
 html_tbl_print_header(lang_get('testset_date_received'));
 print "</tr>" . NEWLINE;
 foreach ($rows_testsets as $row_testset) {
     $testset_id = $row_testset[TS_ID];
     $num_tests = admin_count_tests_in_testset($testset_id);
     $testset_name = admin_get_testset_name($testset_id);
     $testset_build_id = admin_get_build_id_from_testset_id($testset_id);
     $testset_build_name = admin_get_build_name($testset_build_id);
     $testset_release_id = admin_get_release_id_from_build_id($testset_build_id);
     $testset_release_name = admin_get_release_name($testset_release_id);
     $testset_date_created = $row_testset[TS_DATE_CREATED];
     $display_test_id = util_pad_id($testset_id);
     $results_url = $results_page . "?release_id={$testset_release_id}&amp;build_id={$testset_build_id}&amp;testset_id={$testset_id}";
     $row_style = html_tbl_alternate_bgcolor($row_style);
     print "<tr class={$row_style}>" . NEWLINE;
     if ($num_tests > 0) {
         print "<td class='tbl-data-c'><a href='{$results_url}'>{$display_test_id}</a></td>";
     } else {
         print "<td class='tbl-data-c'>{$display_test_id}</td>";
     }
     print "<td class='tbl-data-c'>{$testset_name}</td>";
     print "<td class='tbl-data-c'>{$testset_build_name}</td>";
     print "<td class='tbl-data-c'>{$testset_release_name}</td>";
function testset_email($project_id, $release_id, $build_id, $testset_id, $recipients, $action)
{
    $display_generic_info = true;
    $display_generic_url = true;
    $generic_url = RTH_URL . "login.php?project_id={$project_id}&page=results_page.php&release_id={$release_id}&build_id={$build_id}&testset_id={$testset_id}";
    $username = session_get_username();
    $project_name = session_get_project_name();
    $release_name = admin_get_release_name($release_id);
    $build_name = admin_get_build_name($build_id);
    $user_details = user_get_name_by_username($username);
    $first_name = $user_details[USER_FNAME];
    $last_name = $user_details[USER_LNAME];
    $testset_detail = testset_get_details_by_build($build_id, $testset_id);
    $testset_id = $testset_detail[TS_ID];
    $testset_name = $testset_detail[TS_NAME];
    $testset_date_created = $testset_detail[TS_DATE_CREATED];
    $testset_description = $testset_detail[TS_DESCRIPTION];
    # CREATE EMAIL SUBJECT AND MESSAGE
    switch ($action) {
        case "new_testset":
            $subject = "RTH: New TestSet for {$project_name}";
            $message = "TestSet {$testset_name} has been created by {$first_name} {$last_name}" . NEWLINE . NEWLINE;
            break;
    }
    # Generic link to results page if the $generic_url variable has been set
    if ($display_generic_url) {
        $message .= "Click the following link to view results:" . NEWLINE . NEWLINE;
        $message .= "{$generic_url}" . NEWLINE . NEWLINE;
        $message .= "Please update automated scripts with TESTSETID={$testset_id}" . NEWLINE . NEWLINE;
    }
    if ($display_generic_info) {
        $message .= "" . lang_get("project_name") . ": {$project_name}" . NEWLINE;
        $message .= "" . lang_get("release") . ": {$release_name}" . NEWLINE;
        $message .= "" . lang_get("build") . ": {$build_name}" . NEWLINE;
        $message .= "" . lang_get("testset_name") . ": {$testset_name}" . NEWLINE;
        $message .= "" . lang_get("description") . ": {$testset_description}" . NEWLINE;
        $message .= NEWLINE . "If you do not wish to be notified of any new testsets created, please edit your User profile by navigating to the Users link in RTH.";
    }
    # Convert any html entities stored in the DB back to characters.
    $message = util_unhtmlentities($message);
    email_send($recipients, $subject, $message);
}