testlink1.9.6 /lib/functions/testplan.class.php

Language PHP Lines 4096
MD5 Hash 0e0b90199993b6c3e7c62e0e24e2edc3 Estimated Cost $63,098 (why?)
Repository https://bitbucket.org/pfernandez/testlink1.9.6 View Raw File
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
<?php
/** 
 * TestLink Open Source Project - http://testlink.sourceforge.net/
 * This script is distributed under the GNU General Public License 2 or later. 
 *
 * Manages test plan operations and related items like Custom fields, 
 * Builds, Custom fields, etc
 *
 * @package 	TestLink
 * @author 		franciscom
 * @copyright 	2007-2009, TestLink community 
 * @version    	CVS: $Id: testplan.class.php,v 1.209 2010/08/31 19:40:15 mx-julian Exp $
 * @link 		http://www.teamst.org/index.php
 *
 *
 * @internal Revisions:
 *	20100830 - franciscom - get_linked_tcversions() - missing cast to array	$my['filters']['exec_status']
 *							urgencyImportanceToPriorityLevel() - refactored
 *	20100827 - franciscom - new method wrapper - hasLinkedPlatforms()
 *  20100727 - asimon - BUGID 3629: modified statement in get_linked_tcversions()
 *  20100725 - asimon - BUGID 3497 and hopefully also 3530 fixed in unlink_tcversions()
 *  20100723 - asimon - commented out some old debug message in copy_linked_tcversions()
 *  20100722 - asimon - added missing $debugMsg to get_linked_items()
 *  20100721 - asimon - BUGID 3406: added user_assignments_per_build option to get_linked_tcversions()
 *	20100711 - franciscom - BUGID 3564 -> getPlatforms()
 *	20100614 - eloff - refactor getStatusTotalsByPriority() to same style as the other getStatusTotals...()
 *	20100610 - eloff - BUGID 3515 - getStatusTotals() now takes platforms into account
 *	20100602 - franciscom - copy_as() - force Platforms Link copy when user choose Test Case Copy
 *  20100527 - Julian - BUGID 3492 - Added execution notes to sql statement of get_linked_tcversions
 *  20100525 - Julian - changed default for steps_info option on get_linked_tcversions() to false
 *  					-> performance improvement because not all steps are loaded per default
 *	20100520 - franciscom - getTestCaseSiblings() join bug
 *	20100518 - franciscom - BUGID 3473
 *	20100516 - franciscom - BUGID 3465: Delete Test Project - User Execution Assignment is not deleted
 *	20100506 - franciscom - new method - get_linked_items_id(), that has perfomance advantages
 *							over get_linked_tcversions() when only info needed is test case id.
 *
 *	20100505 - franciscom - BUGID 3434 - get_keywords_map() - refactoring trying to improve performance
 *	20100505 - franciscom - BUGID 3430 - copy_milestones() - need to check if start date is NOT NULL
 *	20100425 - franciscom - BUGID 2463 - changes in getStatusTotalsByAssignedTesterPlatform()
 * 	20100417 - franciscom - get_linked_tcversions() added importance on output data
 *                          BUGID 3356: Failed Test Cases" report is not updated when a test case 
 *                                      has been changed from "Failed" to "Passed"		 
 *
 *  20100217 - asimon - added parameters open and active to getNumberOfBuilds()
 *  20100214 - franciscom - BUGID 2455, BUGID 3026 - Contribution by julian,asimon
 *  20100206 - eloff - BUGID 3060 - Adding getStatusTotalsByPriority()
 *  20100206 - eloff - BUGID 3060 - Adding urgencyImportanceToPriorityLevel() method
 *  20100201 - franciscom - BUGID 3121 - Adding Platform to test plan after the execution completed,
 *                                       reports are not shown appropriate 	
 *  20100112 - franciscom - getPlatforms() - interface changes
 *	20100106 - franciscom - Multiple Test Case Steps Feature
 *                          Affected Methods: get_linked_tcversions()
 *	20091111 - franciscom - BUGID 2938 - getTestCaseSiblings(), getTestCaseNextSibling()
 *  20091031 - franciscom - tallyResultsForReport()
 *  20091027 - franciscom - BUGID 2500 - get_linked_tcversions()
 *  20091025 - franciscom - new method - getStatusTotalsByPlatform()
 *                          bug found on getNotExecutedLinkedTCVersionsDetailed()
 *                          missing testplan_id on execution join
 *
 *	20091010 - franciscom - getNotExecutedLinkedTCVersionsDetailed() new options
 *  20091004 - franciscom - get_linked_tcversions() - fixed query when requesting exec status filtering.
 *                                                  - added more columns to output record set
 *  20090923 - franciscom - link_tcversions() - will return data
 *  20090920 - franciscom - getStatusTotals(), will replace some result.class method
 *  20090919 - franciscom - copy_as(), copy_linked_tcversions() added contribution (refactored)
 *                          to copy user assignment.
 *
 * 	20090822 - franciscom - changeLinkedTCVersionsPlatform() - new method
 *                          countLinkedTCVersionsByPlatform() - new method
 *  20090814 - franciscom - link_tcversions() - interface changes - due to platform feature
 *  20090516 - franciscom - BUGID - is_public
 *                          create(),update() changed
 *  20090509 - franciscom - BUGID - build class manage release_date
 *  20090411 - franciscom - BUGID 2369 - link_tcversions() - interface changes
 *  20090214 - franciscom - BUGID 2099 - get_linked_tcversions() - added new columns in output recordset
 *  20090208 - franciscom - testplan class - new method get_build_by_id()
 *  20090201 - franciscom - copy_milestones() - wrong SQL sentece 
 *                          A,B,C fields renamed to lower case a,b,c to avoid problems
 *                          between differnt database (case and no case sensitive)
 *  20081227 - franciscom - BUGID 1913 - filter by same results on ALL previous builds
 *                          get_same_status_for_build_set(), get_prev_builds()
 *
 *  20081214 - franciscom - Thanks to postgres found missing CAST() on SUM() 
 *  20081206 - franciscom - BUGID 1910 - get_estimated_execution_time() - added new filter
 *                                       get_linked_tcversions() - added test suites filter 
 *  20080820 - franciscom - added get_estimated_execution_time() as result of contributed idea.
 *
 *  20080811 - franciscom - BUGID 1650 (REQ)
 *  20080614 - franciscom - get_linked_and_newest_tcversions() - fixed bug  (thanks to PostGres)
 *  20080428 - franciscom - supporting multiple keywords in get_linked_tcversions()
 *                          (based on contribution by Eugenia Drosdezki)
 *  20080310 - sbouffard - contribution added NHB.name to recordset (useful for API methods).  
 *  20071010 - franciscom - BUGID     MSSQL reserved word problem - open
 *  20070927 - franciscom - BUGID 1069
 *                          added _natsort_builds() (see natsort info on PHP manual).
 *                          get_builds() add call to _natsort_builds()
 *                          get_builds_for_html_options() add call to natsort()
 *  20070310 - franciscom - BUGID 731
 *  20070306 - franciscom - BUGID 705 - changes in get_linked_tcversions()
 **/

/** related functionality */
require_once( dirname(__FILE__) . '/tree.class.php' );
require_once( dirname(__FILE__) . '/assignment_mgr.class.php' );
require_once( dirname(__FILE__) . '/attachments.inc.php' );

/**
 * class to coordinate and manage Test Plans
 * @package 	TestLink
 * @todo havlatm: create class testplanEdit (as extension of testplan class) and 
 *		move here create,edit,delete,copy related stuff
 * @TODO franciscom - absolutely disagree with suggested approach, see no value - 20090611
 */
class testplan extends tlObjectWithAttachments
{
	/** query options */
	const GET_ALL=null;
	const GET_ACTIVE_BUILD=1;
	const GET_INACTIVE_BUILD=0;
	const GET_OPEN_BUILD=1;
	const GET_CLOSED_BUILD=0;
	const ACTIVE_BUILDS=1;
	const ENABLED=1;

	/** @var database handler */
	var $db;

	var $tree_manager;
	var $assignment_mgr;
	var $cfield_mgr;
	var $tcase_mgr;
   
	var $assignment_types;
	var $assignment_status;

	/** message to show on GUI */
	var $user_feedback_message = '';

	var $node_types_descr_id;
	var $node_types_id_descr;
	
	/**
	 * testplan class constructor
	 * 
	 * @param resource &$db reference to database handler
	 */
	function __construct(&$db)
	{
	    $this->db = &$db;
	    $this->tree_manager = New tree($this->db);
		$this->node_types_descr_id=$this->tree_manager->get_available_node_types();
		$this->node_types_id_descr=array_flip($this->node_types_descr_id);
      
	    $this->assignment_mgr = new assignment_mgr($this->db);
	    $this->assignment_types = $this->assignment_mgr->get_available_types();
	    $this->assignment_status = $this->assignment_mgr->get_available_status();

    	$this->cfield_mgr = new cfield_mgr($this->db);
    	$this->tcase_mgr = New testcase($this->db);
		$this->platform_mgr = new tlPlatform($this->db);
   	
	    tlObjectWithAttachments::__construct($this->db,'testplans');
	}


	/**
	 * creates a tesplan on Database, for a testproject.
	 * 
	 * @param string $name: testplan name
	 * @param string $notes: testplan notes
	 * @param string $testproject_id: testplan parent
	 * 
	 * @return integer status code
	 * 		if everything ok -> id of new testplan (node id).
	 * 		if problems -> 0.
	 */
	function create($name,$notes,$testproject_id,$is_active=1,$is_public=1)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$node_types=$this->tree_manager->get_available_node_types();
		$tplan_id = $this->tree_manager->new_node($testproject_id,$node_types['testplan'],$name);
		
		$active_status=intval($is_active) > 0 ? 1 : 0;
		$public_status=intval($is_public) > 0 ? 1 : 0;
		
		$sql = "/* $debugMsg */ " . 
		       " INSERT INTO {$this->tables['testplans']} (id,notes,testproject_id,active,is_public) " .
			   " VALUES ( {$tplan_id} " . ", '" . $this->db->prepare_string($notes) . "'," . 
			   $testproject_id . "," . $active_status . "," . $public_status . ")";
		$result = $this->db->exec_query($sql);
		$id = 0;
		if ($result)
		{
			$id = $tplan_id;
		}

		return $id;
	}


	/**
	 * update testplan information
	 * 
	 * @param integer $id Test plan identifier
	 * @param string $name: testplan name
	 * @param string $notes: testplan notes
	 * @param boolean $is_active
	 * 
	 * @return integer result code (1=ok)
	 */
	function update($id,$name,$notes,$is_active=null,$is_public=null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		$do_update = 1;
		$result = null;
		// $active = to_boolean($is_active);
		$name = trim($name);
		
		// two tables to update and we have no transaction yet.
		$rsa = $this->get_by_id($id);
		$duplicate_check = (strcmp($rsa['name'],$name) != 0 );
		
		if($duplicate_check)
		{
			$rs = $this->get_by_name($name,$rsa['parent_id']);
			$do_update = is_null($rs);
		}
		
		if($do_update)
		{
			// Update name
            $sql = "/* $debugMsg */ ";
			$sql .= "UPDATE {$this->tables['nodes_hierarchy']} " .
				    "SET name='" . $this->db->prepare_string($name) . "'" .
				    "WHERE id={$id}";
			$result = $this->db->exec_query($sql);
			
			if($result)
			{
				$add_upd='';
				if( !is_null($is_active) )
				{
					$add_upd .=',active=' . (intval($is_active) > 0 ? 1 : 0);
				}
				if( !is_null($is_public) )
				{
					$add_upd .=',is_public=' . (intval($is_public) > 0 ? 1:0);
				}
				
				$sql = " UPDATE {$this->tables['testplans']} " .
					" SET notes='" . $this->db->prepare_string($notes). "' " .
					" {$add_upd} WHERE id=" . $id;
				$result = $this->db->exec_query($sql);
			}
		}
		return ($result ? 1 : 0);
	}


	/*
	 function: get_by_name
	 get information about a testplan using name as access key.
	 Search can be narrowed, givin a testproject id as filter criteria.
	 
	 args: name: testplan name
	 [tproject_id]: default:0 -> system wide search i.e. inside all testprojects
	 
	 returns: if nothing found -> null
	 if found -> array where every element is a map with following keys:
	 id: testplan id
	 notes:
	 active: active status
	 is_open: open status
	 name: testplan name
	 testproject_id
	 */
	function get_by_name($name,$tproject_id = 0)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
        $sql = "/* $debugMsg */ ";
		$sql .= " SELECT testplans.*, NH.name " .
			    " FROM {$this->tables['testplans']} testplans, " .
			    " {$this->tables['nodes_hierarchy']} NH" .
			    " WHERE testplans.id=NH.id " .
			    " AND NH.name = '" . $this->db->prepare_string($name) . "'";
		
		if($tproject_id > 0 )
		{
			$sql .= " AND NH.parent_id={$tproject_id}";
		}
		
		$recordset = $this->db->get_recordset($sql);
		return($recordset);
	}


	/*
	 function: get_by_id
	 
	 args : id: testplan id
	 
	 returns: map with following keys:
	 id: testplan id
	 name: testplan name
	 notes: testplan notes
	 testproject_id
	 active
	 is_open
	 parent_id
	 */
	function get_by_id($id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
        $sql = "/* $debugMsg */ " .
		       " SELECT testplans.*,NH.name,NH.parent_id " .
			   " FROM {$this->tables['testplans']} testplans, " .
			   " {$this->tables['nodes_hierarchy']} NH " .
			   " WHERE testplans.id = NH.id AND  testplans.id = {$id}";
		$recordset = $this->db->get_recordset($sql);
		return($recordset ? $recordset[0] : null);
	}


	/*
  		function: get_all
            get array of info for every test plan,
            without considering Test Project and any other kind of filter.
            Every array element contains an assoc array

  		args : -

  		returns: array, every element is a  map with following keys:
           id: testplan id
           name: testplan name
           notes: testplan notes
           testproject_id
           active
           is_open
           parent_id
	*/
	function get_all()
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
        $sql = "/* $debugMsg */ " .
		       " SELECT testplans.*, NH.name " .
			   " FROM {$this->tables['testplans']} testplans, " .
			   " {$this->tables['nodes_hierarchy']} NH " .
			   " WHERE testplans.id=NH.id";
		$recordset = $this->db->get_recordset($sql);
		return $recordset;
	}

	/*
	  function: count_testcases
            get number of testcases linked to a testplan

	  args: id: testplan id

            [platform_id]: null => do not filter by platform
            
	  returns: number
	*/
	public function count_testcases($id,$platform_id=null)
	{
		$sql_filter = '';
		if( !is_null($platform_id) )
		{
			$sql_filter = " AND platform_id={$platform_id} ";
		}
		
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
        $sql = "/* $debugMsg */ " .
	           " SELECT COUNT(testplan_id) AS qty " .
		       " FROM {$this->tables['testplan_tcversions']} " .
			   " WHERE testplan_id={$id} {$sql_filter}";
		$recordset = $this->db->get_recordset($sql);
		$qty = 0;
		if(!is_null($recordset))
		{
			$qty = $recordset[0]['qty'];
		}
		return $qty;
	}


	/*
	  function: tcversionInfoForAudit
            get info regarding tcversions, to generate useful audit messages
            

	  args :
        $tplan_id: test plan id
        $items_to_link: map key=tc_id 
                        value: tcversion_id
	  returns: -

	  rev: 20080629 - franciscom - audit message improvements
	*/
	function tcversionInfoForAudit($tplan_id,&$items)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		
		// Get human readeable info for audit
		$ret=array();
		$tcase_cfg = config_get('testcase_cfg');
		$dummy=reset($items);
		
		list($ret['tcasePrefix'],$tproject_id) = $this->tcase_mgr->getPrefix($dummy);
		$ret['tcasePrefix'] .= $tcase_cfg->glue_character;
		
        $sql = "/* $debugMsg */ " .
		       " SELECT TCV.id, tc_external_id, version, NHB.name " .
			   " FROM {$this->tables['tcversions']} TCV,{$this->tables['nodes_hierarchy']} NHA, " .
			   " {$this->tables['nodes_hierarchy']} NHB " .
			   " WHERE NHA.id=TCV.id " .
			   " AND NHB.id=NHA.parent_id  " .
			   " AND TCV.id IN (" . implode(',',$items) . ")";
		
		$ret['info']=$this->db->fetchRowsIntoMap($sql,'id');  
		$ret['tplanInfo']=$this->get_by_id($tplan_id);                                                          
		
		return $ret;
	}


	/**
	 * associates version of different test cases to a test plan.
	 * this is the way to populate a test plan

 	 args :
        $id: test plan id
        $items_to_link: map key=tc_id 
                        value= map with
                               key: platform_id (can be 0)
                               value: tcversion_id
                        passed by reference for speed
	  returns: -

	  rev: 20080629 - franciscom - audit message improvements
	*/
	function link_tcversions($id,&$items_to_link,$userId)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		
		// Get human readeable info for audit
		$title_separator = config_get('gui_title_separator_1');
		$auditInfo=$this->tcversionInfoForAudit($id,$items_to_link['tcversion']);
		$platformInfo = $this->platform_mgr->getLinkedToTestplanAsMap($id);
		$platformLabel = lang_get('platform');
		
		// Important: MySQL do not support default values on datetime columns that are functions
		// that's why we are using db_now().
		$sql = "/* $debugMsg */ " .
		       "INSERT INTO {$this->tables['testplan_tcversions']} " .
			   "(testplan_id,author_id,creation_ts,tcversion_id,platform_id) " . 
			   " VALUES ({$id},{$userId},{$this->db->db_now()},";
        $features=null;
		foreach($items_to_link['items'] as $tcase_id => $items)
		{
			foreach($items as $platform_id => $tcversion)
			{
				$addInfo='';
				$result = $this->db->exec_query($sql . "{$tcversion}, {$platform_id})");
				if ($result)
				{
                    $features[$platform_id][$tcversion]=$this->db->insert_id($this->tables['testplan_tcversions']);					
					if( isset($platformInfo[$platform_id]) )
					{
						$addInfo = ' - ' . $platformLabel . ':' . $platformInfo[$platform_id];
					}
					$auditMsg=TLS("audit_tc_added_to_testplan",
								  $auditInfo['tcasePrefix'] . $auditInfo['info'][$tcversion]['tc_external_id'] . 
								  $title_separator . $auditInfo['info'][$tcversion]['name'],
								  $auditInfo['info'][$tcversion]['version'],
								  $auditInfo['tplanInfo']['name'] . $addInfo );
					
					logAuditEvent($auditMsg,"ASSIGN",$id,"testplans");
				}	
			}
		}
		return $features;
	}


	/*
	  function: setExecutionOrder

  	args :
        $id: test plan id
        $executionOrder: assoc array key=tcversion_id value=order
                         passed by reference for speed

  	returns: -
	*/
	function setExecutionOrder($id,&$executionOrder)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		foreach($executionOrder as $tcVersionID => $execOrder)
		{
			$execOrder=intval($execOrder);
			$sql="/* $debugMsg */ UPDATE {$this->tables['testplan_tcversions']} " .
				 "SET node_order={$execOrder} " .
				 "WHERE testplan_id={$id} " .
				 "AND tcversion_id={$tcVersionID}";
			$result = $this->db->exec_query($sql);
		}
	}
	

	/**
	 *
	 * @internal revisions:
	 *   20100722 - asimon - added missing $debugMsg
	 */
	function get_linked_items_id($id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		$sql = " /* $debugMsg */ ". 
			   " SELECT DISTINCT parent_id FROM {$this->tables['nodes_hierarchy']} NHTC " .
			   " JOIN {$this->tables['testplan_tcversions']} TPTCV ON TPTCV.tcversion_id = NHTC.id " .
			   " WHERE TPTCV.testplan_id = {$id} ";
			   
		$linked_items = $this->db->fetchRowsIntoMap($sql,'parent_id');			     
		return $linked_items;
	}


	/*
  	function: get_linked_tcversions
            get information about testcases linked to a testplan.

  	args :
         id: testplan id
         [filters]: map with following keys
         	[tcase_id]: default null => get any testcase
         	            numeric      => just get info for this testcase
         	
         	[keyword_id]: default 0 => do not filter by keyword id
         	              numeric   => filter by keyword id
         	
         	
         	[assigned_to]: default NULL => do not filter by user assign.
         	               array() with user id to be used on filter
         	
         	[exec_status]: default NULL => do not filter by execution status
         	               character or array => filter by execution status=character
         	
         	[build_id]: default 0 => do not filter by build id
         	            numeric   => filter by build id
         	
         	[cf_hash]: default null => do not filter by Custom Fields values
         	
		 	
		 	[urgencyImportance] : filter only Tc's with certain (urgency*importance)-value 
		 	
		 	[tsuites_id]: default null.
		 	              If present only tcversions that are children of this testsuites
		 	              will be included
		 	[exec_type] default null -> all types. 
		 	[platform_id]              
		     		
         [options]: map with following keys
         	[output]: controls data type returned
         	          default: map -> map indexed by test case id (original return type)
         	                          Using this option is (in a certain way) as having
         	                          added DISTINCT on SQL clause.
         	                          YOU WILL GET ONLY LAST EXECUTION (means one record)
         	                          of each test case.
         	                          
         	                   mapOfMap: first key testcase_id, second key platform_id               
         	                            You GET ONLY LAST EXECUTION (means one record)
         	                            of each test case.

         	                   mapOfArray -> indexed by test case id but with an array
         	                                 where each element contains information
         	                                 according to Platform.
         	                                 Be carefull if you have multiple executions
         	                                 for same (testcase,platform) YOU WILL GET
         	                                 MULTIPLE ELEMENTS IN ARRAY
         	                                 
         	                  array -> indexed sequentially. 
         	                  
         	                            
         	          
         	[only_executed]: default false => get executed and NOT executed
         	                                  get only executed tcversions
         
         	[execution_details]: default NULL => by platftorm
         	                     add_build => by build AND platform
         	                             
         	[last_execution]: default false => return all executions
         	                          true => last execution ( MAX(E.id))
         	                          
         	[include_unassigned]: has effects only if [assigned_to] <> null.
         	                      default: false
         	                      true: also testcase not assigned will be retreived
         	
         	[details]: controls columns returned
         	           default 'simple'
         	           'full': add summary, steps and expected_results, and test suite name
         	           'summary': add summary 
		 	[steps_info]: controls if step info has to be added on output    
         	           default true
		 	[user_assignments_per_build]: contains a build ID, for which the
		 	                              assigned user shall get loaded    
		 	    
		 	    
	  returns: changes according options['output'] (see above)

           Notice:
           executed field: will take the following values
                           - NULL if the tc version has not been executed in THIS test plan.
                           - tcversion_id if has executions.

	rev :
		 20100727 - asimon - BUGID 3629: modified statement
		 20100721 - asimon - BUGID 3406: added user_assignments_per_build option
		 20100520 - franciscom - added option steps_info, to try to solve perfomance problems
		 						 allowing caller to ask for NO INFO ABOUT STEPS	
		 20100417 - franciscom - added importance on output data
		 		  				 BUGID 3356: "Failed Test Cases" report is not updated when a test case 
		 		  				 			  has been changed from "Failed" to "Passed"		 
		 		  				 			  
         20090814 - franciscom - interface changes due to platform feature
	*/
	public function get_linked_tcversions($id,$filters=null,$options=null)
	{
		$debugMsg = 'Class: ' . __CLASS__ . ' - Method:' . __FUNCTION__;
		
        $my = array ('filters' => '', 'options' => '');
		$tcversion_exec_type = array('join' => '', 'filter' => '');
		$tc_id = array('join' => '', 'filter' => '');
		$builds = array('join' => '', 'filter' => '');
		$keywords = array('join' => '', 'filter' => '');
		$executions = array('join' => '', 'filter' => '');
		$platforms = array('join' => '', 'filter' => '');
		
        $my['filters'] = array('tcase_id' => null, 'keyword_id' => 0,
                               'assigned_to' => null, 'exec_status' => null,
                               'build_id' => 0, 'cf_hash' => null,
                               'urgencyImportance' => null, 'tsuites_id' => null,
                               'platform_id' => null, 'exec_type' => null);
                               
        $my['options'] = array('only_executed' => false, 'include_unassigned' => false,
                               'output' => 'map', 'details' => 'simple', 'steps_info' => false, 
                               'execution_details' => null, 'last_execution' => false);

 		// Cast to array to handle $options = null
		$my['filters'] = array_merge($my['filters'], (array)$filters);
		$my['options'] = array_merge($my['options'], (array)$options);
		
		// 20100830 - franciscom - bug found by Julian
		$my['filters']['exec_status'] = (array)$my['filters']['exec_status'];

		// new dBug($my['filters']);
		// new dBug($my['options']);

		$groupByPlatform=($my['options']['output']=='mapOfMap' || 
		                  $my['options']['output']=='mapOfMapExecPlatform') ? ',platform_id' : '';
		$groupByBuild=($my['options']['execution_details'] == 'add_build') ? ',build_id' : '';

		// BUGID 3406: user assignments per build 
		$ua_build = isset($my['options']['user_assignments_per_build']) ? 
		            $my['options']['user_assignments_per_build'] : 0;
		
		// BUGID 3629
		$ua_build_sql = $ua_build && is_numeric($ua_build) ? " AND UA.build_id={$ua_build} " : " ";
		
        // @TODO - 20091004 - franciscom
        // Think that this subquery in not good when we add execution filter
		// $last_exec_subquery = " AND E.id IN ( SELECT MAX(id) " .
		// 	 		             "               FROM  {$this->tables['executions']} executions " .
		// 			             "               WHERE testplan_id={$id} %EXECSTATUSFILTER%" .
		// 			             " GROUP BY tcversion_id,testplan_id {$groupByPlatform} {$groupByBuild} )";

		// I've had confirmation of BAD query;
		// BUGID 3356: "Failed Test Cases" report is not updated when a test case 
		// 		  				 			  has been changed from "Failed" to "Passed"		 
		//
        // SUBQUERY CAN NOT HAVE ANY KIND OF FILTERING other that test plan.
        // Adding exec status, means that we will get last exec WITH THIS STATUS, and not THE LATEST EXEC   
		$last_exec_subquery = " AND E.id IN ( SELECT MAX(id) " .
			 		          "               FROM  {$this->tables['executions']} executions " .
					          "               WHERE testplan_id={$id} " .
					          " GROUP BY tcversion_id,testplan_id {$groupByPlatform} {$groupByBuild} )";
           
           
           
		$resultsCfg = config_get('results');
		$status_not_run=$resultsCfg['status_code']['not_run'];
		$sql_subquery='';
		
		// franciscom
		// WARNING: 
		// Order of analisys seems to be critic, because $executions['filter'] is overwritten
		// on some situation below if filtering on execution status is requested
		if( $my['options']['last_execution'] )
		{
			$executions['filter'] = " {$last_exec_subquery} ";
			// 20100417 - franciscom - BUGID 3356
			// $executions['filter'] = str_ireplace("%EXECSTATUSFILTER%", "", $executions['filter']);
		}
		
		if( !is_null($my['filters']['platform_id']) )
		{
			$platforms['filter'] = " AND T.platform_id = {$my['filters']['platform_id']} ";
	    }
		
		// 20100417- Why to use a list ? - must be checked if is on
		if( !is_null($my['filters']['exec_type']) )
		{
			$tcversion_exec_type['filter'] = "AND TCV.execution_type IN (" . 
			                                 implode(",",(array)$my['filters']['exec_type']) . " ) ";     
		}
		
		// Based on work by Eugenia Drosdezki
		if( is_array($my['filters']['keyword_id']) )
		{
			// 0 -> no keyword, remove 
			if( $my['filters']['keyword_id'][0] == 0 )
			{
				array_shift($my['filters']['keyword_id']);
			}
			
			if(count($my['filters']['keyword_id']))
			{
				$keywords['filter'] = " AND TK.keyword_id IN (" . implode(',',$my['filters']['keyword_id']) . ")";          	
			}  
		}
		else if($my['filters']['keyword_id'] > 0)
		{
			$keywords['filter'] = " AND TK.keyword_id = {$my['filters']['keyword_id']} ";
		}
		
		if(trim($keywords['filter']) != "")
		{
			$keywords['join'] = " JOIN {$this->tables['testcase_keywords']} TK ON NHA.parent_id = TK.testcase_id ";
		}
		
		if (!is_null($my['filters']['tcase_id']) )
		{
			if( is_array($my['filters']['tcase_id']) )
			{
				$tc_id['filter'] = " AND NHA.parent_id IN (" . implode(',',$my['filters']['tcase_id']) . ")";          	
			}
			else if ($my['filters']['tcase_id'] > 0 )
			{
				$tc_id['filter'] = " AND NHA.parent_id = {$my['filters']['tcase_id']} ";
			}
		}
		
		// --------------------------------------------------------------
		if(!is_null($my['filters']['exec_status']) )
		{
			$executions['filter'] = '';
			$notrun['filter'] = null;
			$otherexec['filter'] = null;
			
			$notRunPresent = array_search($status_not_run,$my['filters']['exec_status']); 
			if($notRunPresent !== false)
			{
				$notrun['filter'] = " E.status IS NULL ";
				unset($my['filters']['exec_status'][$notRunPresent]);  
			}
			
			if(count($my['filters']['exec_status']) > 0)
			{
				$otherexec['filter']=" E.status IN ('" . implode("','",$my['filters']['exec_status']) . "') ";
				
				// 20100417 - franciscom - BUGID 3356
				// $status_filter=str_ireplace("E.", "executions.", $otherexec['filter']);
			    // $sql_subquery = str_ireplace("%EXECSTATUSFILTER%", "AND {$status_filter}", $last_exec_subquery);

				// code commented before BUGID 3356
			    // $sql_subquery = str_ireplace("E.", "executions.", $sql_subquery);
				// $sql_subquery = $last_exec_subquery;
				
				// 20100417 - franciscom - BUGID 3356
				// $executions['filter'] = " ( {$otherexec['filter']} {$sql_subquery} ) ";  
				$executions['filter'] = " ( {$otherexec['filter']} {$last_exec_subquery} ) ";  
			}
			if( !is_null($notrun['filter']) )
			{
				if($executions['filter'] != "")
				{
					$executions['filter'] .= " OR ";
				}
				$executions['filter'] .= $notrun['filter'];
			}
			
			if($executions['filter'] != "")
			{
                // Just add the AND 
				$executions['filter'] = " AND ({$executions['filter']} )";     
			}
		}
		
		// --------------------------------------------------------------
		if( $my['filters']['build_id'] > 0 )
		{
			$builds['filter'] = " AND E.build_id={$my['filters']['build_id']} ";
		}
		
		// there are several situation where you need to use LEFT OUTER
		if(!$my['options']['only_executed'])
		{
			$executions['join'] = " LEFT OUTER ";
		}
		
		
		// platform feature
		$executions['join'] .= " JOIN {$this->tables['executions']} E ON " .
			                   " (NHA.id = E.tcversion_id AND " .
			                   " E.platform_id=T.platform_id AND " .
			                   " E.testplan_id=T.testplan_id {$builds['filter']}) ";
		// --------------------------------------------------------------
		
		$more_tcase_fields = '';
		$join_for_parent = '';
		$more_parent_fields = '';
		$more_exec_fields='';
		switch($my['options']['details'])
		{
			case 'full':
			$more_tcase_fields .= 'TCV.summary,';
			$join_for_parent .= " JOIN {$this->tables['nodes_hierarchy']} NHC ON NHB.parent_id = NHC.id ";
			$more_parent_fields .= 'NHC.name as tsuite_name,';
			break;
			
			case 'summary':
			$more_tcase_fields .= 'TCV.summary,';
			break;
		
		}
		if($my['options']['execution_details'] == 'add_build')
		{
			$more_exec_fields .= 'E.build_id,B.name AS build_name,';	
			$builds['join']=" LEFT OUTER JOIN {$this->tables['builds']} B ON B.id=E.build_id ";
	    }
		
	    // BUGID 3406 - assignments per build
	    // BUGID 3492 - Added execution notes to sql statement of get_linked_tcversions
		$sql = "/* $debugMsg */ " .
		       " SELECT NHB.parent_id AS testsuite_id, {$more_tcase_fields} {$more_parent_fields}" .
			   " NHA.parent_id AS tc_id, NHB.node_order AS z, NHB.name," .
			   " T.platform_id, PLAT.name as platform_name ,T.id AS feature_id, T.tcversion_id AS tcversion_id,  " .
			   " T.node_order AS execution_order, T.creation_ts AS linked_ts, T.author_id AS linked_by," .
			   " TCV.version AS version, TCV.active," .
			   " TCV.tc_external_id AS external_id, TCV.execution_type,TCV.importance," .
			   " E.id AS exec_id, E.tcversion_number," .
			   " E.tcversion_id AS executed, E.testplan_id AS exec_on_tplan, {$more_exec_fields}" .
			   " E.execution_type AS execution_run_type, E.testplan_id AS exec_on_tplan, " .
			   " E.execution_ts, E.tester_id, E.notes as execution_notes, ".
		       " UA.build_id as assigned_build_id, " . // 3406
			   " UA.user_id,UA.type,UA.status,UA.assigner_id,T.urgency, " .
			   " COALESCE(E.status,'" . $status_not_run . "') AS exec_status, ".
			   " (urgency * importance) AS priority " .
			   " FROM {$this->tables['nodes_hierarchy']} NHA " .
			   " JOIN {$this->tables['nodes_hierarchy']} NHB ON NHA.parent_id = NHB.id " .
			   $join_for_parent .
			   " JOIN {$this->tables['testplan_tcversions']} T ON NHA.id = T.tcversion_id " .
			   " JOIN  {$this->tables['tcversions']} TCV ON NHA.id = TCV.id {$tcversion_exec_type['filter']} " .
			   " {$executions['join']} " .
			   " {$keywords['join']} " .
			   " {$builds['join']} " .
			   " LEFT OUTER JOIN {$this->tables['platforms']} PLAT ON PLAT.id = T.platform_id " .
			   " LEFT OUTER JOIN {$this->tables['user_assignments']} UA ON UA.feature_id = T.id " .
			   " {$ua_build_sql} " . // 3406
			   " WHERE T.testplan_id={$id} {$keywords['filter']} {$tc_id['filter']} {$platforms['filter']}" .
			   " AND (UA.type={$this->assignment_types['testcase_execution']['id']} OR UA.type IS NULL) " . 
			   $executions['filter'];
		
		// 20081220 - franciscom
		// if (!is_null($assigned_to) && $assigned_to > 0)
		// {
		//
		// If special user id TL_USER_ANYBODY is present in set of user id,
		// we will DO NOT FILTER by user ID
		if( !is_null($my['filters']['assigned_to']) && 
		    !in_array(TL_USER_ANYBODY,(array)$my['filters']['assigned_to']) )
		{  
			$sql .= " AND ";
			
			// Warning!!!:
			// If special user id TL_USER_NOBODY is present in set of user id
			// we will ignore any other user id present on set.
			if( in_array(TL_USER_NOBODY,(array)$my['filters']['assigned_to']) )
			{
				$sql .= " UA.user_id IS NULL "; 
			} 
			// BUGID 2455
            // new user filter "somebody" --> all asigned testcases
			else if( in_array(TL_USER_SOMEBODY,(array)$my['filters']['assigned_to']) )
			{
				$sql .= " UA.user_id IS NOT NULL "; 
			}
			else
			{
				$sql_unassigned="";
				if( $my['options']['include_unassigned'] )
				{
					$sql .= "(";
					$sql_unassigned=" OR UA.user_id IS NULL)";
				}
				// BUGID 2500
				$sql .= " UA.user_id IN (" . implode(",",(array)$my['filters']['assigned_to']) . ") " . $sql_unassigned;
			}
		}
		
		if (!is_null($my['filters']['urgencyImportance']))
		{
			$urgencyImportanceCfg = config_get("urgencyImportance");
			if ($my['filters']['urgencyImportance'] == HIGH)
			{
				$sql .= " AND (urgency * importance) >= " . $urgencyImportanceCfg->threshold['high'];
			}
			else if($my['filters']['urgencyImportance'] == LOW)
			{
				$sql .= " AND (urgency * importance) < " . $urgencyImportanceCfg->threshold['low'];
			}
			else
			{
				$sql .= " AND ( ((urgency * importance) >= " . $urgencyImportanceCfg->threshold['low'] . 
					    " AND  ((urgency * importance) < " . $urgencyImportanceCfg->threshold['high']."))) ";
			}		
		}
		
		// test suites filter
		if (!is_null($my['filters']['tsuites_id']))
		{
			$tsuiteSet = is_array($my['filters']['tsuites_id']) ? $my['filters']['tsuites_id'] : array($my['filters']['tsuites_id']);
			$sql .= " AND NHB.parent_id IN (" . implode(',',$tsuiteSet) . ")";
		}
		$sql .= " ORDER BY testsuite_id,NHB.node_order,tc_id,platform_id,E.id ASC";

 		switch($my['options']['output'])
		{ 
			case 'array':
			$recordset = $this->db->get_recordset($sql);
			break;

			case 'mapOfArray':
			$recordset = $this->db->fetchRowsIntoMap($sql,'tc_id',database::CUMULATIVE);
			break;
			
			case 'mapOfMap':
			// with this option we got just one record for each (testcase,platform)
			// no matter how many executions has been done
			
			$recordset = $this->db->fetchMapRowsIntoMap($sql,'tc_id','platform_id');
			break;
			
			case 'mapOfMapExecPlatform':
			// with this option we got just one record for each (platform, build)
			
			$recordset = $this->db->fetchMapRowsIntoMap($sql,'exec_id','platform_id');
			break;
			
			case 'map':
			default:
			$recordset = $this->db->fetchRowsIntoMap($sql,'tc_id');
			
			// 20070913 - jbarchibald
			// here we add functionality to filter out the custom field selections
			//
			// After addition of platform feature, this filtering can not be done
			// always with original filter_cf_selection().
			// Fisrt choice:
			// Enable this feature only if recordset maintains original structured
			//
			if (!is_null($my['filters']['cf_hash'])) {
				$recordset = $this->filter_cf_selection($recordset, $my['filters']['cf_hash']);
			}
			break;
		}
		
        // Multiple Test Case Steps Feature
        // added after Julian mail regarding perf problems building exec tree
        if( !is_null($recordset) && $my['options']['steps_info'])
        {
		    $itemSet = array_keys($recordset);
			switch($my['options']['output'])
			{ 
        	
				case 'mapOfArray':
				case 'mapOfMap':
	  				foreach($itemSet as $itemKey)
	  				{
	  					$keySet = array_keys($recordset[$itemKey]);
	  					$target = &$recordset[$itemKey];
	  					foreach($keySet as $accessKey)
	  					{
	  						$step_set = $this->tcase_mgr->get_steps($target[$accessKey]['tcversion_id']);
	  						$target[$accessKey]['steps'] = $step_set;
	  					}
	  				}
				break;
				
				case 'array':
				case 'map':
				default:
  					foreach($itemSet as $accessKey)
  					{
	  					$step_set = $this->tcase_mgr->get_steps($recordset[$accessKey]['tcversion_id']);
	  					$recordset[$accessKey]['steps'] = $step_set;
	  				} 
				break;
			}
        }

		
		return $recordset;
	}


/*
  function: get_linked_and_newest_tcversions
            returns for every test case in a test plan
            the tc version linked and the newest available version

  args: id: testplan id
        [tcase_id]: default null => all testcases linked to testplan

  returns: map key: testcase internal id
           values: map with following keys:

            [name]
            [tc_id] (internal id)
            [tcversion_id]
            [newest_tcversion_id]
            [tc_external_id]
            [version] (for humans)
            [newest_version] (for humans)

  rev:
      20080614 - franciscom - fixed bug on SQL generated while
                              adding tc_external_id on results.
      20080126 - franciscom - added tc_external_id on results
*/
	function get_linked_and_newest_tcversions($id,$tcase_id=null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		
		$tc_id_filter = " ";
		if (!is_null($tcase_id) )
		{
			if( is_array($tcase_id) )
			{
				// ??? implement as in ?
			}
			else if ($tcase_id > 0 )
			{
				$tc_id_filter = " AND NHA.parent_id = {$tcase_id} ";
			}
		}
		
		// 20080614 - franciscom
		// Peter Rooms found bug due to wrong SQL, accepted by MySQL but not by PostGres
		// Missing column in GROUP BY Clause
		
		$sql = " SELECT MAX(NHB.id) AS newest_tcversion_id, " .
			   " NHA.parent_id AS tc_id, NHC.name, T.tcversion_id AS tcversion_id," .
			   " TCVA.tc_external_id AS tc_external_id, TCVA.version AS version " .
			   " FROM {$this->tables['nodes_hierarchy']} NHA " .
			
			// NHA - will contain ONLY nodes of type testcase_version that are LINKED to test plan
			" JOIN {$this->tables['testplan_tcversions']} T ON NHA.id = T.tcversion_id " . 
			
			// Get testcase_version data for LINKED VERSIONS
			" JOIN {$this->tables['tcversions']} TCVA ON TCVA.id = T.tcversion_id" .
			
			// Work on Sibblings - Start
			// NHB - Needed to get ALL testcase_version sibblings nodes
			" JOIN {$this->tables['nodes_hierarchy']} NHB ON NHB.parent_id = NHA.parent_id " .
			
			// Want only ACTIVE Sibblings
			" JOIN {$this->tables['tcversions']} TCVB ON TCVB.id = NHB.id AND TCVB.active=1 " . 
			// Work on Sibblings - STOP 
			
			// NHC will contain - nodes of type TESTCASE (parent of testcase versions we are working on)
			// we use NHC to get testcase NAME ( testcase version nodes have EMPTY NAME)
			" JOIN {$this->tables['nodes_hierarchy']} NHC ON NHC.id = NHA.parent_id " .
			
			// Want to get only testcase version with id (NHB.id) greater than linked one (NHA.id)
			" WHERE T.testplan_id={$id} AND NHB.id > NHA.id" . $tc_id_filter .
			" GROUP BY NHA.parent_id, NHC.name, T.tcversion_id, TCVA.tc_external_id, TCVA.version  ";
		
		$sql2 = " SELECT SUBQ.name, SUBQ.newest_tcversion_id, SUBQ.tc_id, " .
			    " SUBQ.tcversion_id, SUBQ.version, SUBQ.tc_external_id, " .
			    " TCV.version AS newest_version " .
			    " FROM {$this->tables['tcversions']} TCV, ( $sql ) AS SUBQ " .
			    " WHERE SUBQ.newest_tcversion_id = TCV.id " .
			    " ORDER BY SUBQ.tc_id ";
		
		return $this->db->fetchRowsIntoMap($sql2,'tc_id');
	}


	/**
	 * Remove of records from user_assignments table
	 * @author franciscom
	 * 
	 * @param integer $id   : test plan id
	 * @param array $items: assoc array key=tc_id value=tcversion_id
	 * 
	 * @internal revisions:
	 *    20100725 - asimon - BUGID 3497 and hopefully also 3530
	 */
	function unlink_tcversions($id,&$items)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		if(is_null($items))
		{
			return;
		}
		
		// Get human readeable info for audit
		$gui_cfg = config_get('gui');
		$title_separator = config_get('gui_title_separator_1');
		$auditInfo=$this->tcversionInfoForAudit($id,$items['tcversion']);
		$platformInfo = $this->platform_mgr->getLinkedToTestplanAsMap($id);
		$platformLabel = lang_get('platform');

        $dummy = null;
		foreach($items['items'] as $tcase_id => $elem) 
		{
			foreach($elem as $platform_id => $tcversion_id) 
			{
				$dummy[] = "(tcversion_id = {$tcversion_id} AND platform_id = {$platform_id})";
			}
		}
		$where_clause = implode(" OR ", $dummy);
		
		/*
		 * asimon - BUGID 3497 and hopefully also 3530
		 * A very litte error, missing braces in the $where_clause, was causing this bug. 
		 * When one set of testcases is linked to two testplans, this statement should check 
		 * that the combination of testplan_id, tcversion_id and platform_id was the same, 
		 * but instead it checked for either testplan_id OR tcversion_id and platform_id.
		 * So every linked testcase with fitting tcversion_id and platform_id without execution
		 * was deleted, regardless of testplan_id.
		 * Simply adding braces around the where clause solves this.
		 * So innstead of: 
		 * SELECT id AS link_id FROM testplan_tcversions  
		 * WHERE testplan_id=12 AND (tcversion_id = 5 AND platform_id = 0) 
		 * OR (tcversion_id = 7 AND platform_id = 0) 
		 * OR (tcversion_id = 9 AND platform_id = 0) 
		 * OR (tcversion_id = 11 AND platform_id = 0)
		 * we need this:
		 * SELECT ... WHERE testplan_id=12 AND (... OR ...)
		 */ 
		$where_clause = " ( {$where_clause} ) ";
		
		// First get the executions id if any exist
		$sql=" SELECT id AS execution_id " .
			 " FROM {$this->tables['executions']} " .
			 " WHERE testplan_id = {$id} AND ${where_clause}";
		$exec_ids = $this->db->fetchRowsIntoMap($sql,'execution_id');
		
		if( !is_null($exec_ids) and count($exec_ids) > 0 )
		{
			// has executions
			$exec_ids = array_keys($exec_ids);
			$exec_id_where= " WHERE execution_id IN (" . implode(",",$exec_ids) . ")";
			
			// Remove bugs if any exist
			$sql=" DELETE FROM {$this->tables['execution_bugs']} {$exec_id_where} ";
			$result = $this->db->exec_query($sql);
			
			// now remove executions
			$sql=" DELETE FROM {$this->tables['executions']} " .
				 " WHERE testplan_id = {$id} AND ${where_clause}";
			$result = $this->db->exec_query($sql);
		}
		
		// ----------------------------------------------------------------
		// to remove the assignment to users (if any exists) we need the list of id
		$sql=" SELECT id AS link_id FROM {$this->tables['testplan_tcversions']} " .
			 " WHERE testplan_id={$id} AND {$where_clause} ";
		$link_ids = $this->db->fetchRowsIntoMap($sql,'link_id');
		$features = array_keys($link_ids);
		if( count($features) == 1)
		{
			$features=$features[0];
		}
		$this->assignment_mgr->delete_by_feature_id($features);
		// ----------------------------------------------------------------
		
		// Delete from link table
		$sql=" DELETE FROM {$this->tables['testplan_tcversions']} " .
			 " WHERE testplan_id={$id} AND {$where_clause} ";
		$result = $this->db->exec_query($sql);
		
		foreach($items['items'] as $tcase_id => $elem)
		{
			foreach($elem as $platform_id => $tcversion)
			{
				$addInfo='';
				if( isset($platformInfo[$platform_id]) )
				{
					$addInfo = ' - ' . $platformLabel . ':' . $platformInfo[$platform_id];
				}
				$auditMsg=TLS("audit_tc_removed_from_testplan",
							  $auditInfo['tcasePrefix'] . $auditInfo['info'][$tcversion]['tc_external_id'] . 
							  $title_separator . $auditInfo['info'][$tcversion]['name'],
							  $auditInfo['info'][$tcversion]['version'],
							  $auditInfo['tplanInfo']['name'] . $addInfo );
				
				logAuditEvent($auditMsg,"UNASSIGN",$id,"testplans");
			}
		}
		
	} // end function unlink_tcversions



	/**
	 * 
	 * @internal revisions
	 * 20100505 - franciscom - BUGID 3434
	 */
	function get_keywords_map($id,$order_by_clause='')
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		$map_keywords=null;
		
		// keywords are associated to testcase id, then first
		// we need to get the list of testcases linked to the testplan
		// 
		// 20100505 - according to user report (BUGID 3434) seems that 
		// $linked_items = $this->get_linked_tcversions($id);
		// has performance problems.
		// Then make a choice do simple query here.
		//
		$sql = " /* $debugMsg */ ". 
			   " SELECT DISTINCT parent_id FROM {$this->tables['nodes_hierarchy']} NHTC " .
			   " JOIN {$this->tables['testplan_tcversions']} TPTCV ON TPTCV.tcversion_id = NHTC.id " .
			   " WHERE TPTCV.testplan_id = {$id} ";
			   
		$linked_items = $this->db->fetchRowsIntoMap($sql,'parent_id');			     
		
		if( !is_null($linked_items) )
		{
			$tc_id_list = implode(",",array_keys($linked_items));
			
			$sql = " /* $debugMsg */ " .
				   " SELECT DISTINCT TCKW.keyword_id,KW.keyword " .
				   " FROM {$this->tables['testcase_keywords']} TCKW, " .
				   " {$this->tables['keywords']} KW " .
				   " WHERE TCKW.keyword_id = KW.id " .
				   " AND TCKW.testcase_id IN ( {$tc_id_list} ) " .
				   " {$order_by_clause} ";
			$map_keywords = $this->db->fetchColumnsIntoMap($sql,'keyword_id','keyword');
		}
		return ($map_keywords);
	}
	

/*
  args :
        [$keyword_id]: can be an array
*/
	function get_keywords_tcases($id,$keyword_id=0)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$CUMULATIVE=1;
		$map_keywords=null;
		
		// keywords are associated to testcase id, then first
		// we need to get the list of testcases linked to the testplan
		$linked_items = $this->get_linked_items_id($id);
		if( !is_null($linked_items) )
		{
			$keyword_filter= '' ;
			
			if( is_array($keyword_id) )
			{
				$keyword_filter = " AND keyword_id IN (" . implode(',',$keyword_id) . ")"; 
			}
			else if( $keyword_id > 0 )
			{
				$keyword_filter = " AND keyword_id = {$keyword_id} ";
			}
			
			
			$tc_id_list = implode(",",array_keys($linked_items));
			
			// 20081116 - franciscom -
			// Does DISTINCT is needed ? Humm now I think no.
			$sql = "SELECT DISTINCT testcase_id,keyword_id,keyword
				FROM {$this->tables['testcase_keywords']} testcase_keywords,
				{$this->tables['keywords']} keywords
				WHERE keyword_id = keywords.id
				AND testcase_id IN ( {$tc_id_list} )
				{$keyword_filter}
				ORDER BY keyword ASC ";
			
			// 20081116 - franciscom
			// CUMULATIVE is needed to get all keywords assigned to each testcase linked to testplan	       
			$map_keywords = $this->db->fetchRowsIntoMap($sql,'testcase_id',$CUMULATIVE);
		}
		
		return ($map_keywords);
	} // end function


	/*
  	function: copy_as
            creates a new test plan using an existent one as source.
	Note:	copy_test_urgency is not appropriate to copy


  	args: id: source testplan id
        new_tplan_id: destination
        [tplan_name]: default null.
                      != null => set this as the new name

        [tproject_id]: default null.
                       != null => set this as the new testproject for the testplan
                              this allow us to copy testplans to differents test projects.

        [user_id]
        [options]: default null
                   allowed keys:
                   items2copy: 
                   	          null: do a deep copy => copy following test plan child elements:
                              builds,linked tcversions,milestones,user_roles,priorities,
                              platforms,execution assignment.
                              
                              != null, a map with keys that controls what child elements to copy

				   copy_assigned_to:
				   tcversion_type: 
				                  null/'current' -> use same version present on source testplan
                                  'lastest' -> for every testcase linked to source testplan
                                               use lastest available version
                                               
       [mappings]: need to be documented                                        
  	returns: N/A
	*/
	function copy_as($id,$new_tplan_id,$tplan_name=null,$tproject_id=null,$user_id=null,
                     $options=null,$mappings=null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$cp_methods = array('copy_milestones' => 'copy_milestones',
			                'copy_user_roles' => 'copy_user_roles',
			                'copy_platforms_links' => 'copy_platforms_links',
			                'copy_builds' => 'copy_builds');

		$mapping_methods = array('copy_platforms_links' => 'platforms');

		$my['options'] = array();

		// Configure here only elements that has his own table.
		$my['options']['items2copy']= array('copy_tcases' => 1,'copy_milestones' => 1, 'copy_user_roles' => 1, 
		                                    'copy_builds' => 1, 'copy_platforms_links' => 1);

		$my['options']['copy_assigned_to'] = 0;
		$my['options']['tcversion_type'] = null;

		$my['options'] = array_merge($my['options'], (array)$options);
		
		// get source testplan general info
		$rs_source=$this->get_by_id($id);
		
		if(!is_null($tplan_name))
		{
			$sql="/* $debugMsg */ UPDATE {$this->tables['nodes_hierarchy']} " .
				 "SET name='" . $this->db->prepare_string(trim($tplan_name)) . "' " .
				 "WHERE id={$new_tplan_id}";
			$this->db->exec_query($sql);
		}
		
		if(!is_null($tproject_id))
		{
			$sql="/* $debugMsg */ UPDATE {$this->tables['testplans']} SET testproject_id={$tproject_id} " .
				 "WHERE id={$new_tplan_id}";
			$this->db->exec_query($sql);
		}
		
		
		// Important Notice:
		// Since the addition of Platforms, test case versions are linked to Test Plan AND Platforms
		// this means, that not matter user choice, we will force Platforms COPY.
		// This is a lazy approach, instead of complex one that requires understand what Platforms
		// have been used on SOURCE Test Plan.
		//
		// copy test cases is an special copy
		if( $my['options']['items2copy']['copy_tcases'] )
		{
			$my['options']['items2copy']['copy_platforms_links'] = 1;
			$this->copy_linked_tcversions($id,$new_tplan_id,$user_id,$my['options'],$mappings);
		}
		
		foreach( $my['options']['items2copy'] as $key => $do_copy )
		{
			if( $do_copy )
			{
				if( isset($cp_methods[$key]) )
				{
					$copy_method=$cp_methods[$key];
					if( isset($mapping_methods[$key]) && isset($mappings[$mapping_methods[$key]]))
					{
						$this->$copy_method($id,$new_tplan_id,$mappings[$mapping_methods[$key]]);
					}
					else
					{
						$this->$copy_method($id,$new_tplan_id);
					}	
				}
			}
		}
	} // end function copy_as



	/**
	 * $id: source testplan id
	 * $new_tplan_id: destination
	 */
	private function copy_builds($id,$new_tplan_id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		$rs=$this->get_builds($id);
		
		if(!is_null($rs))
		{
			foreach($rs as $build)
			{
				$sql="INSERT INTO {$this->tables['builds']} (name,notes,testplan_id) " .
					"VALUES ('" . $this->db->prepare_string($build['name']) ."'," .
					"'" . $this->db->prepare_string($build['notes']) ."',{$new_tplan_id})";
				
				$this->db->exec_query($sql);
			}
		}
	}


	/*
  	function: copy_linked_tcversions

  	args: id: source testplan id
        new_tplan_id: destination
        [options]
        	[tcversion_type]: default null -> use same version present on source testplan
                              'lastest' -> for every testcase linked to source testplan
                                      use lastest available version
        	[copy_assigned_to]: 1 -> copy execution assignments without role control                              

		[$mappings] useful when this method is called due to a Test Project COPY AS (yes PROJECT no PLAN)
					
  	returns:
  
 	 Note: test urgency is set to default in the new Test plan (not copied)
	*/
	private function copy_linked_tcversions($id,$new_tplan_id,$user_id=-1, $options=null,$mappings=null)
	
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
    
		$my['options']['tcversion_type'] = null;
	    $my['options']['copy_assigned_to'] = 0;
		$my['options'] = array_merge($my['options'], (array)$options);
        $now_ts = $this->db->db_now();

		$sql="/* $debugMsg */ "; 
		if($my['options']['copy_assigned_to'])
		{
			$sql .= " SELECT TPTCV.*, COALESCE(UA.user_id,-1) AS tester " .
			        " FROM {$this->tables['testplan_tcversions']} TPTCV " .
			        " LEFT OUTER JOIN {$this->tables['user_assignments']} UA ON " .
			        " UA.feature_id = TPTCV.id " .
			        " WHERE testplan_id={$id} ";
		}
		else
		{
			$sql .= " SELECT TPTCV.* FROM {$this->tables['testplan_tcversions']} TPTCV" .
			        " WHERE testplan_id={$id} ";
	    }

		$rs=$this->db->get_recordset($sql);
		if(!is_null($rs))
		{
			$tcase_mgr = new testcase($this->db);
			$doMappings = !is_null($mappings);
			foreach($rs as $elem)
			{
				$tcversion_id = $elem['tcversion_id'];
				
				// Seems useless - 20100204
				$feature_id = $elem['id'];
				if( !is_null($my['options']['tcversion_type']) )
				{
					$sql="/* $debugMsg */ SELECT * FROM {$this->tables['nodes_hierarchy']} WHERE id={$tcversion_id} ";
					$rs2=$this->db->get_recordset($sql);
					$last_version_info = $tcase_mgr->get_last_version_info($rs2[0]['parent_id']);
					$tcversion_id = $last_version_info ? $last_version_info['id'] : $tcversion_id ;
				}
				
				// mapping need to be done with:
				// platforms
				// test case versions
				$platform_id = $elem['platform_id'];
				if( $doMappings )
				{
					if( isset($mappings['platforms'][$platform_id]) )
					{
						$platform_id = $mappings['platforms'][$platform_id]; 
					}
					if( isset($mappings['test_spec'][$tcversion_id]) )
					{
						$tcversion_id = $mappings['test_spec'][$tcversion_id]; 
					}
				}
				
				
				$sql = "/* $debugMsg */ " . 
				       " INSERT INTO {$this->tables['testplan_tcversions']} " .
					   " (testplan_id,tcversion_id,platform_id,node_order,urgency) " .
					   " VALUES({$new_tplan_id},{$tcversion_id},{$platform_id}," .
					   " {$elem['node_order']},{$elem['urgency']})";
					   
 				 //echo "<br>debug - <b><i>" . __FUNCTION__ . "</i></b><br><b>" . $sql . "</b><br>";
	   
				$this->db->exec_query($sql);
				$new_feature_id = $this->db->insert_id($this->tables['testplan_tcversions']);
				
				if($my['options']['copy_assigned_to'] && $elem['tester'] > 0)
				{
					$features_map = array();
					$feature_id=$new_feature_id;
					$features_map[$feature_id]['user_id'] = $elem['tester'];
					$features_map[$feature_id]['type'] = $this->assignment_types['testcase_execution']['id'];
					$features_map[$feature_id]['status']  = $this->assignment_status['open']['id'];
					$features_map[$feature_id]['creation_ts'] = $now_ts;
					$features_map[$feature_id]['assigner_id'] = $user_id;
					
					$this->assignment_mgr->assign($features_map);
				}
				
			}
		}
	}


/*
  function: copy_milestones

  args: id: source testplan id
        new_tplan_id: destination

  returns:

  rev : 
        20090910 - franciscom - added start_date
        
        20070519 - franciscom
        changed date to target_date, because date is an Oracle reverved word.
*/
	private function copy_milestones($tplan_id,$new_tplan_id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		$rs=$this->get_milestones($tplan_id);
		if(!is_null($rs))
		{
			foreach($rs as $mstone)
			{
				// BUGID 3430 - need to check if start date is NOT NULL
				$add2fields = '';
				$add2values = '';
				$use_start_date = strlen(trim($mstone['start_date'])) > 0;
				if( $use_start_date )
				{
					$add2fields = 'start_date,';
					$add2values = "'" . $mstone['start_date'] . "',";
				}

				$sql = "INSERT INTO {$this->tables['milestones']} (name,a,b,c,target_date,{$add2fields} testplan_id)";				
                $sql .= " VALUES ('" . $this->db->prepare_string($mstone['name']) ."'," .
					    $mstone['high_percentage'] . "," . $mstone['medium_percentage'] . "," . 
					    $mstone['low_percentage'] . ",'" . $mstone['target_date'] . "', {$add2values}{$new_tplan_id})";
				$this->db->exec_query($sql);
			}
		}
	}


	/**
	 * Get all milestones for a Test Plan
	 * @param int $tplan_id Test Plan identificator
	 * @return array of arrays TBD fields description 
	 */
	function get_milestones($tplan_id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		
		$sql="SELECT id, name, a AS high_percentage, b AS medium_percentage, c AS low_percentage, " .
		     "target_date, start_date,testplan_id " .       
		     "FROM {$this->tables['milestones']} " .
		     "WHERE testplan_id={$tplan_id} ORDER BY target_date,name";
		return $this->db->get_recordset($sql);
	}


	/**
	 * Copy user roles to a new Test Plan
	 * 
	 * @param int $source_id original Test Plan id
	 * @param int $target_id new Test Plan id
	 */
	private function copy_user_roles($source_id, $target_id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		$sql = "/* $debugMsg */ SELECT user_id,role_id FROM {$this->tables['user_testplan_roles']} " .
		       " WHERE testplan_id={$source_id} ";
		$rs = $this->db->get_recordset($sql);
		if(!is_null($rs))
		{
	    	foreach($rs as $elem)
	    	{
	      		$sql="INSERT INTO {$this->tables['user_testplan_roles']}  " .
	           		"(testplan_id,user_id,role_id) " .
	           		"VALUES({$target_id}," . $elem['user_id'] ."," . $elem['role_id'] . ")";
	      		$this->db->exec_query($sql);
			}
		}
	}


	/**
	 * Gets all testplan related user roles
	 *
	 * @param integer $id the testplan id
	 * @return array assoc map with keys taken from the user_id column
	 **/
	function getUserRoleIDs($id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = "SELECT user_id,role_id FROM {$this->tables['user_testplan_roles']} " .
		       "WHERE testplan_id = {$id}";
		$roles = $this->db->fetchRowsIntoMap($sql,'user_id');
		return $roles;
	}


	/**
	 * Inserts a testplan related role for a given user
	 *
	 * @param int $userID the id of the user
	 * @param int $id the testplan id
	 * @param int $roleID the role id
	 * 
	 * @return integer returns tl::OK on success, tl::ERROR else
	 **/
	
	function addUserRole($userID,$id,$roleID)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$status = tl::ERROR;
		$sql = "INSERT INTO {$this->tables['user_testplan_roles']} (user_id,testplan_id,role_id) VALUES " .
			   " ({$userID},{$id},{$roleID})";
		if ($this->db->exec_query($sql))
		{
			$testPlan = $this->get_by_id($id);
			$role = tlRole::getByID($this->db,$roleID,tlRole::TLOBJ_O_GET_DETAIL_MINIMUM);
			$user = tlUser::getByID($this->db,$userID,tlUser::TLOBJ_O_GET_DETAIL_MINIMUM);
			if ($user && $testPlan && $role)
			{
				logAuditEvent(TLS("audit_users_roles_added_testplan",$user->getDisplayName(),
				              $testPlan['name'],$role->name),"ASSIGN",$id,"testplans");
			}
			$status = tl::OK;
		}
		return $status;
	}


	/**
	 * Deletes all testplan related role assignments for a given testplan
	 *
	 * @param int $id the testplan id
	 * @return tl::OK  on success, tl::FALSE else
	 **/
	function deleteUserRoles($id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$status = tl::ERROR;
		$sql = "DELETE FROM {$this->tables['user_testplan_roles']} " .
		       " WHERE testplan_id = {$id}";
		if ($this->db->exec_query($sql))
		{
			$testPlan = $this->get_by_id($id);
			if ($testPlan)
			{
				logAuditEvent(TLS("audit_all_user_roles_removed_testplan",
				              $testPlan['name']),"ASSIGN",$id,"testplans");
			}
			$status = tl::OK;
		}
		return $status;
	}


	/**
	 * Delete test plan and all related link to other items
	 *
 	 */
	function delete($id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$the_sql=array();
		$main_sql=array();
		
		$this->deleteUserRoles($id);
		$getFeaturesSQL = " SELECT id FROM {$this->tables['testplan_tcversions']} WHERE testplan_id={$id} "; 
		$the_sql[]="DELETE FROM {$this->tables['milestones']} WHERE testplan_id={$id}";
		
		// CF used on testplan_design are linked by testplan_tcversions.id
		$the_sql[]="DELETE FROM {$this->tables['cfield_testplan_design_values']} WHERE link_id ".
			       "IN ({$getFeaturesSQL})";

		// BUGID 3465: Delete Test Project - User Execution Assignment is not deleted
		$the_sql[]="DELETE FROM {$this->tables['user_assignments']} WHERE feature_id ".
			       "IN ({$getFeaturesSQL})";
		
		$the_sql[]="DELETE FROM {$this->tables['risk_assignments']} WHERE testplan_id={$id}";
		$the_sql[]="DELETE FROM {$this->tables['testplan_platforms']} WHERE testplan_id={$id}";

		$the_sql[]="DELETE FROM {$this->tables['testplan_tcversions']} WHERE testplan_id={$id}";
		$the_sql[]="DELETE FROM {$this->tables['builds']} WHERE testplan_id={$id}";
		$the_sql[]="DELETE FROM {$this->tables['cfield_execution_values']} WHERE testplan_id={$id}";
		$the_sql[]="DELETE FROM {$this->tables['user_testplan_roles']} WHERE testplan_id={$id}";
		
		
		// When deleting from executions, we need to clean related tables
		$the_sql[]="DELETE FROM {$this->tables['execution_bugs']} WHERE execution_id ".
				   "IN (SELECT id FROM {$this->tables['executions']} WHERE testplan_id={$id})";
		$the_sql[]="DELETE FROM {$this->tables['executions']} WHERE testplan_id={$id}";
		
		
		foreach($the_sql as $sql)
		{
			$this->db->exec_query($sql);
		}
		
		$this->deleteAttachments($id);
		
		$this->cfield_mgr->remove_all_design_values_from_node($id);
		// ------------------------------------------------------------------------
		
		// Finally delete from main table
		$main_sql[]="DELETE FROM {$this->tables['testplans']} WHERE id={$id}";
		$main_sql[]="DELETE FROM {$this->tables['nodes_hierarchy']} WHERE id={$id}";
		
		foreach($main_sql as $sql)
		{
			$this->db->exec_query($sql);
		}
	} // end delete()



	// --------------------------------------------------------------------------------------
	// Build related methods
	// --------------------------------------------------------------------------------------
	
	/*
	  function: get_builds_for_html_options()
	
	
	  args :
	        $id     : test plan id.
	        [active]: default:null -> all, 1 -> active, 0 -> inactive BUILDS
	        [open]  : default:null -> all, 1 -> open  , 0 -> closed/completed BUILDS
	
	  returns:
	
	  rev :
	        20070129 - franciscom - order to ASC
	        20070120 - franciscom
	        added active, open
	*/
	function get_builds_for_html_options($id,$active=null,$open=null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = " SELECT id, name " .
			" FROM {$this->tables['builds']} WHERE testplan_id = {$id} ";
		
		// 20070120 - franciscom
		if( !is_null($active) )
		{
			$sql .= " AND active=" . intval($active) . " ";
		}
		if( !is_null($open) )
		{
			$sql .= " AND is_open=" . intval($open) . " ";
		}
		
		$sql .= " ORDER BY name ASC";
		
		
		// BUGID
		$recordset=$this->db->fetchColumnsIntoMap($sql,'id','name');
		if( !is_null($recordset) )
		{
			natsort($recordset);
		}
		
		return $recordset;
	}


	/*
	  function: get_max_build_id
	
	  args :
	        $id     : test plan id.
	
	  returns:
	*/
	function get_max_build_id($id,$active = null,$open = null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = " SELECT MAX(id) AS maxbuildid " .
			" FROM {$this->tables['builds']} " .
			" WHERE testplan_id = {$id}";
		
		if(!is_null($active))
		{
			$sql .= " AND active = " . intval($active) . " ";
		}
		if( !is_null($open) )
		{
			$sql .= " AND is_open = " . intval($open) . " ";
		}
		
		$recordset = $this->db->get_recordset($sql);
		$maxBuildID = 0;
		if ($recordset)
		{
			$maxBuildID = intval($recordset[0]['maxbuildid']);
		}
		return $maxBuildID;
	}

	/*
	   function: get_testsuites
	    args :
		$id     : test plan id.
	    returns: returns flat list of names of test suites (including nest test suites)  No particular Order.
	*/
	function get_testsuites($id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		// BUGID 0002776
		$sql = " SELECT NHTSUITE.name, NHTSUITE.id, NHTSUITE.parent_id" . 
			   " FROM {$this->tables['testplan_tcversions']}  TPTCV, {$this->tables['nodes_hierarchy']}  NHTCV, " .
			   " {$this->tables['nodes_hierarchy']} NHTCASE, {$this->tables['nodes_hierarchy']} NHTSUITE " . 
			   " WHERE TPTCV.tcversion_id = NHTCV.id " .
			   " AND NHTCV.parent_id = NHTCASE.id " .
			   " AND NHTCASE.parent_id = NHTSUITE.id " .
			   " AND TPTCV.testplan_id = " . $id . " " .
			   " GROUP BY NHTSUITE.name,NHTSUITE.id,NHTSUITE.parent_id " .
			   " ORDER BY NHTSUITE.name" ;
		
		$recordset = $this->db->get_recordset($sql);
		
		// Now the recordset contains testsuites that have child test cases.
		// However there could potentially be testsuites that only have grandchildren/greatgrandchildren
		// this will iterate through found test suites and check for 
		$superset = $recordset;
		foreach($recordset as $value)
		{
			$superset = array_merge($superset, $this->get_parenttestsuites($value['id']));
		}    
		
		// At this point there may be duplicates
		$dup_track = array();
		foreach($superset as $value)
		{
			if (!array_key_exists($value['id'],$dup_track))
			{
				$dup_track[$value['id']] = true;
				$finalset[] = $value;
			}        
		}    
		
		// Needs to be alphabetical based upon name attribute 
		usort($finalset, array("testplan", "compare_name"));
		return $finalset;
	}


	/*
	 function: compare_name
	Used for sorting a list by nest name attribute
	
	  args :
		$a     : first array to compare
		$b       : second array to compare
	  
	  returns: an integer indicating the result of the comparison
	 */
	private function compare_name($a, $b)
	{
	    return strcasecmp($a['name'], $b['name']);
	}


	/*
	 function: get_parenttestsuites
	
	Used by get_testsuites
	 
	Recursive function used to get all the parent test suites of potentially testcase free testsuites.
	If passed node id isn't the product then it's merged into result set.
	
	  args :
		$id     : $id of potential testsuite
	  
	  returns: an array of all testsuite ancestors of $id
	 */
	private function get_parenttestsuites($id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

	    $sql = "SELECT name, id, parent_id " .
		       "FROM {$this->tables['nodes_hierarchy']}  NH " .
		       "WHERE NH.node_type_id <> {$this->node_types_descr_id['testproject']} " .
		       "AND NH.id = " . $id;
		    
	    $recordset = $this->db->get_recordset($sql);
	    $myarray = array();
	    if (count($recordset) > 0)
	    {        
		    // 20100611 - franciscom
		    // $myarray = array(array('name'=>$recordset[0]['name'], 'id'=>$recordset[0]['id']));
		    $myarray = array($recordset[0]);
		    $myarray = array_merge($myarray, $this->get_parenttestsuites($recordset[0]['parent_id'])); 
	    }
	    
	    return $myarray;            
	}


	/*
	  function: get_builds
	            get info about builds defined for a testlan.
	            Build can be filtered by active and open status.
	
	  args :
	        id: test plan id.
	        [active]: default:null -> all, 1 -> active, 0 -> inactive BUILDS
	        [open]: default:null -> all, 1 -> open  , 0 -> closed/completed BUILDS
	
	  returns: map, where elements are ordered by build name, using variant of nasort php function.
	           key: build id
	           value: map with following keys
	                  id: build id
	                  name: build name
	                  notes: build notes
	                  active: build active status
	                  is_open: build open status
	                  testplan_id
	                  release_date
	
	  rev :
	*/
	function get_builds($id,$active=null,$open=null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = " SELECT id,testplan_id, name, notes, active, is_open,release_date " .
			" FROM {$this->tables['builds']} WHERE testplan_id = {$id} " ;
		
		if( !is_null($active) )
		{
			$sql .= " AND active=" . intval($active) . " ";
		}
		if( !is_null($open) )
		{
			$sql .= " AND is_open=" . intval($open) . " ";
		}
		
		$sql .= "  ORDER BY name ASC";
		
		$recordset = $this->db->fetchRowsIntoMap($sql,'id');
		
		if( !is_null($recordset) )
		{
			$recordset = $this->_natsort_builds($recordset);
		}
		
		return $recordset;
	}


	/**
	 * Get a build belonging to a test plan, using build name as access key
	 *
	 * @param int $id test plan id
	 * @param string $build_name
	 * 
	 * @return array [id,testplan_id, name, notes, active, is_open]
	 */
	function get_build_by_name($id,$build_name)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$safe_build_name=$this->db->prepare_string(trim($build_name));
		
		$sql = " SELECT id,testplan_id, name, notes, active, is_open " .
			" FROM {$this->tables['builds']} " .
			" WHERE testplan_id = {$id} AND name='{$safe_build_name}'";
		
		
		$recordset = $this->db->get_recordset($sql);
		$rs=null;
		if( !is_null($recordset) )
		{
			$rs=$recordset[0];
		}
		return $rs;
	}


	/**
	 * Get a build belonging to a test plan, using build id as access key
	 *
	 * @param int $id test plan id
	 * @param int $build_id
	 *
	 * @return array [id,testplan_id, name, notes, active, is_open]
	 */
	function get_build_by_id($id,$build_id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = " SELECT id,testplan_id, name, notes, active, is_open " .
			" FROM {$this->tables['builds']} BUILDS " .
			" WHERE testplan_id = {$id} AND BUILDS.id={$build_id}";
		
		$recordset = $this->db->get_recordset($sql);
		$rs=null;
		if( !is_null($recordset) )
		{
			$rs=$recordset[0];
		}
		return $rs;
	}


	/**
	 * Get the number of builds of a given Testplan
	 *
	 * @param int tplanID test plan id
	 *
	 * @return int number of builds
	 * 
	 * @internal revisions:
	 * 20100217 - asimon - added parameters active and open to get only number of active/open builds
	 */
	function getNumberOfBuilds($tplanID, $active = null, $open = null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		
		$sql = "/* $debugMsg */ SELECT count(id) AS num_builds FROM {$this->tables['builds']} builds " .
			       "WHERE builds.testplan_id = " . $tplanID;
		
		if( !is_null($active) )
	 	{
	 	   $sql .= " AND builds.active=" . intval($active) . " ";
	 	}
	 	if( !is_null($open) )
	 	{
	 	   $sql .= " AND builds.is_open=" . intval($open) . " ";
	 	}
		
	 	return $this->db->fetchOneValue($sql);
	}

	
	function _natsort_builds($builds_map)
	{
		// BUGID - sort in natural order (see natsort in PHP manual)
		foreach($builds_map as $key => $value)
		{
			$vk[$value['name']]=$key;
			$build_names[$key]=$value['name'];
		}
		
		natsort($build_names);
		$build_num=count($builds_map);
		foreach($build_names as $key => $value)
		{
			$dummy[$key]=$builds_map[$key];
		}
		return $dummy;
	}


	/*
	  function: check_build_name_existence
	
	  args:
	       tplan_id: test plan id.
	       build_name
	      [build_id}: default: null
	                  when is not null we add build_id as filter, this is useful
	                  to understand if is really a duplicate when using this method
	                  while managing update operations via GUI
	
	  returns: 1 => name exists
	
	  rev: 20080217 - franciscom - added build_id argument
	
	*/
	function check_build_name_existence($tplan_id,$build_name,$build_id=null,$case_sensitive=0)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = " SELECT id, name, notes " .
			" FROM {$this->tables['builds']} " .
			" WHERE testplan_id = {$tplan_id} ";
		
		
		if($case_sensitive)
		{
			$sql .= " AND name=";
		}
		else
		{
			$build_name=strtoupper($build_name);
			$sql .= " AND UPPER(name)=";
		}
		$sql .= "'" . $this->db->prepare_string($build_name) . "'";
		
		if( !is_null($build_id) )
		{
			$sql .= " AND id <> " . $this->db->prepare_int($build_id);
		}
		
		
		$result = $this->db->exec_query($sql);
		$status= $this->db->num_rows($result) ? 1 : 0;
		
		return $status;
	}


	/*
	  function: get_build_id_by_name
	
	Ignores case
	
	  args :
		$tplan_id     : test plan id. 
		$build_name   : build name. 
	  
	  returns: 
	  The ID of the build name specified regardless of case.
	
	  rev :
	*/
	//@TODO: schlundus, this is only a special case of get_build_by_name, so it should be refactored
	function get_build_id_by_name($tplan_id,$build_name)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = " SELECT builds.id, builds.name, builds.notes " .
			" FROM {$this->tables['builds']} builds " .
			" WHERE builds.testplan_id = {$tplan_id} ";
		
		$build_name=strtoupper($build_name);        
		$sql .= " AND UPPER(builds.name)=";
		$sql .= "'" . $this->db->prepare_string($build_name) . "'";    
		
		//$result = $this->db->exec_query($sql);
		
		$recordset = $this->db->get_recordset($sql);
		$BuildID = 0;
		if ($recordset)
			$BuildID = intval($recordset[0]['id']);
		
		return $BuildID;  
	}


	/*
	  function: create_build
	
	  args :
	        $tplan_id
	        $name
	        $notes
	        [$active]: default: 1
	        [$open]: default: 1
	
	
	
	  returns:
	
	  rev :
	*/
	function create_build($tplan_id,$name,$notes = '',$active=1,$open=1)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = " INSERT INTO {$this->tables['builds']} (testplan_id,name,notes,active,is_open) " .
			" VALUES ('". $tplan_id . "','" .
			$this->db->prepare_string($name) . "','" .
			$this->db->prepare_string($notes) . "'," .
			"{$active},{$open})";
		
		$new_build_id = 0;
		$result = $this->db->exec_query($sql);
		if ($result)
		{
			$new_build_id = $this->db->insert_id($this->tables['builds']);
		}
		
		return $new_build_id;
	}
	

	// --------------------------------------------------------------------------------------
	// Custom field related methods
	// --------------------------------------------------------------------------------------
	/*
	  function: get_linked_cfields_at_design
	
	  args: $id
	        [$parent_id]: testproject id
	        [$show_on_execution]: default: null
	                              1 -> filter on field show_on_execution=1
	                              0 or null -> don't filter
	
	  returns: hash
	
	  rev :
	        20061231 - franciscom - added $parent_id
	*/
	function get_linked_cfields_at_design($id,$parent_id=null,$show_on_execution=null)
	{
		$path_len=0;
		if( is_null($parent_id) )
		{
			// Need to get testplan parent (testproject id) in order to get custom fields
			// 20081122 - franciscom - need to check when we can call this with ID=NULL
			$the_path = $this->tree_manager->get_path(!is_null($id) ? $id : $parent_id);
			$path_len = count($the_path);
		}
		$tproject_id = ($path_len > 0)? $the_path[$path_len-1]['parent_id'] : $parent_id; 
		
		$cf_map = $this->cfield_mgr->get_linked_cfields_at_design($tproject_id,self::ENABLED,
			                                                      $show_on_execution,'testplan',$id);
		
		return $cf_map;
	}


	/*
	  function: get_linked_cfields_at_execution
	
	  args: $id
	        [$parent_id]: if present is testproject id
	        [$show_on_execution]: default: null
	                              1 -> filter on field show_on_execution=1
	                              0 or null -> don't filter
	
	  returns: hash
	
	  rev :
	        20061231 - franciscom - added $parent_id
	*/
	function get_linked_cfields_at_execution($id,$parent_id=null,$show_on_execution=null)
	{
		$path_len=0;
		if( is_null($parent_id) )
		{
			// Need to get testplan parent (testproject id) in order to get custom fields
			// 20081122 - franciscom - need to check when we can call this with ID=NULL
			$the_path = $this->tree_manager->get_path(!is_null($id) ? $id : $parent_id);
			$path_len = count($the_path);
		}
		$tproject_id = ($path_len > 0)? $the_path[$path_len-1]['parent_id'] : $parent_id; 
		
		// 20081122 - franciscom - humm!! need to look better IMHO this call is done to wrong function
		$cf_map=$this->cfield_mgr->get_linked_cfields_at_execution($tproject_id,self::ENABLED,
			$show_on_execution,'testplan',$id);
		return($cf_map);
	}


	/* Get Custom Fields  Detail which are enabled on Execution of a TestCase/TestProject.
	  function: get_linked_cfields_id
	
	  args: $testproject_id 
	
	  returns: hash map of id : label
	
	  rev :
	
	*/
	
	function get_linked_cfields_id($tproject_id)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$field_map = new stdClass();
		
		$sql = "SELECT field_id,label
			FROM {$this->tables['cfield_testprojects']} cfield_testprojects, 
			{$this->tables['custom_fields']} custom_fields
			WHERE
			custom_fields.id = cfield_testprojects.field_id 
			and cfield_testprojects.active = 1 
			and custom_fields.enable_on_execution = 1 
			and custom_fields.show_on_execution = 1 
			and cfield_testprojects.testproject_id = {$tproject_id}
			order by field_id";
		
		$field_map = $this->db->fetchColumnsIntoMap($sql,'field_id','label');
		return($field_map);
	}


	/*
	  function: html_table_of_custom_field_inputs
	
	  args: $id
	        [$parent_id]: need when you call this method during the creation
	                      of a test suite, because the $id will be 0 or null.
	
	        [$scope]: 'design','execution'
	
	  returns: html string
	*/
	function html_table_of_custom_field_inputs($id,$parent_id=null,$scope='design')
	{
		$cf_smarty='';
		
		if( $scope=='design' )
		{
			$cf_map = $this->get_linked_cfields_at_design($id,$parent_id);
		}
		else
		{
			$cf_map=$this->get_linked_cfields_at_execution($id,$parent_id);
		}
		
		if( !is_null($cf_map) )
		{
			foreach($cf_map as $cf_id => $cf_info)
			{
				$label=str_replace(TL_LOCALIZE_TAG,'',lang_get($cf_info['label'],null,true));
				$cf_smarty .= '<tr><td class="labelHolder">' . htmlspecialchars($label) . "</td><td>" .
					$this->cfield_mgr->string_custom_field_input($cf_info) . "</td></tr>\n";
			} //foreach($cf_map
		}
		
		
		if($cf_smarty != '')
		{
			$cf_smarty = "<table>" . $cf_smarty . "</table>";
		}
		return($cf_smarty);
	}


	/*
	  function: html_table_of_custom_field_values
	
	  args: $id
	        [$scope]: 'design','execution'
	        
	        [$filters]:default: null
	                            
	                           map with keys:
	        
	                           [show_on_execution]: default: null
	                                                1 -> filter on field show_on_execution=1
	                                                     include ONLY custom fields that can be viewed
	                                                     while user is execution testcases.
	                           
	                                                0 or null -> don't filter
	
	  returns: html string
	
	  rev :
	       20080811 - franciscom - BUGID 1650 (REQ)
	       20070701 - franciscom - fixed return string when there are no custom fields.
	*/
	function html_table_of_custom_field_values($id,$scope='design',$filters=null,$formatOptions=null)
	{
		$cf_smarty='';
		$parent_id=null;
		$td_style='class="labelHolder"' ;
		$add_table=true;
		$table_style='';
		if( !is_null($formatOptions) )
		{
			$td_style=isset($formatOptions['td_css_style']) ? $formatOptions['td_css_style'] : $td_style;
			$add_table=isset($formatOptions['add_table']) ? $formatOptions['add_table'] : true;
			$table_style=isset($formatOptions['table_css_style']) ? $formatOptions['table_css_style'] : $table_style;
		} 
		
		
		if( $scope=='design' )
		{
			$cf_map=$this->get_linked_cfields_at_design($id,$parent_id,$filters);
		}
		else
		{
			$cf_map=$this->get_linked_cfields_at_execution($id);
		}
		
		if( !is_null($cf_map) )
		{
			foreach($cf_map as $cf_id => $cf_info)
			{
				// if user has assigned a value, then node_id is not null
				if(isset($cf_info['node_id']) && $cf_info['node_id'])
				{
					// true => do not create input in audit log
					$label=str_replace(TL_LOCALIZE_TAG,'',lang_get($cf_info['label'],null,true));
					$cf_smarty .= "<tr><td {$td_style}>" . htmlspecialchars($label) . "</td><td>" .
						$this->cfield_mgr->string_custom_field_value($cf_info,$id) . "</td></tr>\n";
				}
			}
		}
		
		if($cf_smarty != '' && $add_table)
		{
			$cf_smarty = "<table {$table_style}>" . $cf_smarty . "</table>";
		}
		return($cf_smarty);
	} // function end


	/*
	  function: filter_cf_selection
	
	  args :
	        $tp_tcs - this comes from get_linked_tcversion
	        $cf_hash [cf_id] = value of cfields to filter by.
	
	  returns: array filtered by selected custom fields.
	
	  rev :
	*/
	function filter_cf_selection ($tp_tcs, $cf_hash)
	{
		$new_tp_tcs = null;
		
		foreach ($tp_tcs as $tc_id => $tc_value)
		{
			
			foreach ($cf_hash as $cf_id => $cf_value)
			{
				$passed = 0;
				// there will never be more than one record that has a field_id / node_id combination
				$sql = "SELECT value FROM {$this->tables['cfield_design_values']} " .
					"WHERE field_id = $cf_id " .
					"AND node_id = $tc_id ";
				
				$result = $this->db->exec_query($sql);
				$myrow = $this->db->fetch_array($result);
				
				// push both to arrays so we can compare
				$possibleValues = explode ('|', $myrow['value']);
				$valuesSelected = explode ('|', $cf_value);
				
				// we want to match any selected item from list and checkboxes.
				if ( count($valuesSelected) ) {
					foreach ($valuesSelected as $vs_id => $vs_value) {
						$found = array_search($vs_value, $possibleValues);
						if (is_int($found)) {
							$passed = 1;
						} else {
							$passed = 0;
							break;
						}
					}
				}
				// if we don't match, fall out of the foreach.
				// this gives a "and" search for all cf's, if this is removed then it responds
				// as an "or" search
				// perhaps this could be parameterized.
				if ($passed == 0) {
					break;
				}
			}
			if ($passed) {
				$new_tp_tcs[$tc_id] = $tp_tcs[$tc_id];
			}
		}
		return ($new_tp_tcs);
	}


	/*
	  function: get_estimated_execution_time
	            Created after a contributed code (BUGID 1670)
	
	            Takes all testcases linked to testplan and computes
	            SUM of values assigned AT DESIGN TIME to customa field
	            named CF_ESTIMATED_EXEC_TIME
	
	            IMPORTANT:
	            1. at time of this writting (20080820) this CF can be of type: string,numeric or float.
	            2. YOU NEED TO USE . (dot) as decimal separator (US decimal separator?) or
	               sum will be wrong. 
	         
	            
	            
	  args:id testplan id
	       tcase_set: default null
	
	  returns: sum of CF values for all testcases linked to testplan
	
	  rev: 20080820 - franciscom
	*/
	function get_estimated_execution_time($id,$tcase_set=null)
	{
		// Get list of test cases on test plan
		$estimated=0;
		$cf_info = $this->cfield_mgr->get_by_name('CF_ESTIMATED_EXEC_TIME');
		
		// CF exists ?
		if( ($status_ok=!is_null($cf_info)) )
		{
			$cfield_id=key($cf_info);
		}
		
		if( $status_ok)
		{
			if( is_null($tcase_set) )
			{
				// we will compute time for ALL linked test cases
				// $linked_testcases=$this->get_linked_tcversions($id);  
				// Test done due to BUGID 3434 has shown that:
				// get_linked_items_id($id) has better performance than get_linked_tcversions($id);
				$linked_testcases=$this->get_linked_items_id($id);  
				if( ($status_ok=!is_null($linked_testcases)) )
				{
					$tcase_ids=array_keys($linked_testcases);
				}    
			}
			else
			{
				$tcase_ids=$tcase_set;  
			}
		}  
		
		if($status_ok)
		{
			$sql="SELECT SUM(CAST(value AS NUMERIC)) ";
			if( DB_TYPE == 'mysql')
			{
				$sql="SELECT SUM(value) ";
			} 
			else if ( DB_TYPE == 'postgres')
			{
				$sql="SELECT SUM(CAST(value AS NUMERIC)) ";
			}        
			$sql .= " AS SUM_VALUE FROM {$this->tables['cfield_design_values']} CFDV " .
				" WHERE CFDV.field_id={$cfield_id} " .
				" AND node_id IN (" . implode(',',$tcase_ids) . ")";
			$estimated=$this->db->fetchOneValue($sql);
			$estimated=is_null($estimated) ? 0 :$estimated;
		}
		
		return $estimated;
	}    


	/*
	  function: get_execution_time
	            Takes all testcases (or a subset of executions) linked to testplan 
	            that has been executed and computes SUM of values assigned AT EXECUTION TIME 
	            to customa field named CF_EXEC_TIME
	
	            IMPORTANT:
	            1. at time of this writting (20081207) this CF can be of type: string,numeric or float.
	            2. YOU NEED TO USE . (dot) as decimal separator (US decimal separator?) or
	               sum will be wrong. 
	            
	  args:id testplan id
	       $execution_set: default null
	
	  returns: sum of CF values for all testcases linked to testplan
	
	  rev: 20081207 - franciscom
	*/
	function get_execution_time($id,$execution_set=null)
	{
		$total_time=0;
		$cf_info = $this->cfield_mgr->get_by_name('CF_EXEC_TIME');
		
		// CF exists ?
		if( ($status_ok=!is_null($cf_info)) )
		{
			$cfield_id=key($cf_info);
		}
		
		if( $status_ok)
		{
			if( is_null($execution_set) )
			{
				// we will compute time for ALL linked and executed test cases,
				// just for LAST executed TCVERSION
				// $linked_executed=$this->get_linked_tcversions($id,null,0,'just_executed'); 
				$options = array('only_executed' => true);
				$linked_executed=$this->get_linked_tcversions($id,null,$options); 
				if( ($status_ok=!is_null($linked_executed)) )
				{
					foreach($linked_executed as $tcase_id => $info)
					{
						$execution_ids[]=$info['exec_id'];
					}    
				}    
			}
			else
			{
				$execution_ids=$execution_set;  
			}
		}  
		
		if($status_ok)
		{
			$sql="SELECT SUM(CAST(value AS NUMERIC)) ";
			if( DB_TYPE == 'mysql')
			{
				$sql="SELECT SUM(value) ";
			} 
			else if ( DB_TYPE == 'postgres')
			{
				$sql="SELECT SUM(CAST(value AS NUMERIC)) ";
			}        
			
			$sql .= " AS SUM_VALUE FROM {$this->tables['cfield_execution_values']} CFEV " .
				" WHERE CFEV.field_id={$cfield_id} " .
				" AND testplan_id={$id} " .
				" AND execution_id IN (" . implode(',',$execution_ids) . ")";
			
			$total_time=$this->db->fetchOneValue($sql);
			$total_time=is_null($total_time) ? 0 :$total_time;
		}
		return $total_time;
	}    


	/*
	  function: get_prev_builds() 
	
	  args: id: testplan id
	        build_id: all builds belonging to choosen testplan,
	                  with id < build_id will be retreived.
	        [active]: default null  -> do not filter on active status
	  
	  returns: 
	
	*/
	function get_prev_builds($id,$build_id,$active=null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$sql = " SELECT id,testplan_id, name, notes, active, is_open " .
			" FROM {$this->tables['builds']} " . 
			" WHERE testplan_id = {$id} AND id < {$build_id}" ;
		
		if( !is_null($active) )
		{
			$sql .= " AND active=" . intval($active) . " ";
		}
		
		$recordset = $this->db->fetchRowsIntoMap($sql,'id');
		return $recordset;
	}
	

	/**
	 * returns set of tcversions that has same execution status
	 * in every build present on buildSet.
	 * ATTENTION!!!: this does not work for not_run status
	 */
	 /*           
	  args: id: testplan id
	        buildSet: builds to analise.
	        status: status code
	*/
	function get_same_status_for_build_set($id,$buildSet,$status)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$node_types=$this->tree_manager->get_available_node_types();
		$resultsCfg = config_get('results');
		
		$num_exec = count($buildSet);
		$build_in = implode(",", $buildSet);
		$status_in = implode("',", (array)$status);
		
		if( in_array($resultsCfg['status_code']['not_run'], (array)$status) )
		{
			
			$sql = " SELECT distinct T.tcversion_id,E.build_id,NH.parent_id AS tcase_id " .
				" FROM {$this->tables['testplan_tcversions']}  T " .
				" JOIN {$this->tables['nodes_hierarchy']}  NH ON T.tcversion_id=NH.id " .
				" AND NH.node_type_id={$node_types['testcase_version']} " .
				" LEFT OUTER JOIN {$this->tables['executions']} E ON T.tcversion_id = E.tcversion_id " .
				" AND T.testplan_id=E.testplan_id AND E.build_id IN ({$build_in}) " .
				" WHERE T.testplan_id={$id} AND E.build_id IS NULL ";
		}
		else
		{
			$sql = " SELECT EE.status,SQ1.tcversion_id, NH.parent_id AS tcase_id, COUNT(EE.status) AS exec_qty " .
				" FROM {$this->tables['executions']} EE, {$this->tables['nodes_hierarchy']} NH," .
				" (SELECT E.tcversion_id,E.build_id,MAX(E.id) AS last_exec_id " .
				" FROM {$this->tables['executions']} E " .
				" WHERE E.build_id IN ({$build_in}) " .
				" GROUP BY E.tcversion_id,E.build_id) AS SQ1 " .
				" WHERE EE.build_id IN ({$build_in}) " .
				" AND EE.status IN ('" . $status . "') AND NH.node_type_id={$node_types['testcase_version']} " .
				" AND SQ1.last_exec_id=EE.id AND SQ1.tcversion_id=NH.id " .
				" GROUP BY status,SQ1.tcversion_id,NH.parent_id" .
				" HAVING count(EE.status)= {$num_exec} " ;
		}
		
		$recordset = $this->db->fetchRowsIntoMap($sql,'tcase_id');
		return $recordset;
	}


	/**
	 * BUGID 2455, BUGID 3026
	 * find any builds which have the wanted status in the build set
	 * 
	 * @author asimon
	 * @param integer $id Build ID
	 * @param array $buildSet build set to check
	 * @param array $status status to look for
	 * @return array $recordset set of builds which match the search criterium
	 */
	function get_status_for_any_build($id,$buildSet,$status) 
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$node_types=$this->tree_manager->get_available_node_types();
		$resultsCfg = config_get('results');

		$build_in = implode(",", $buildSet);
		$status_in = implode("','", (array)$status);

		if( in_array($resultsCfg['status_code']['not_run'], (array)$status) ) {
			//not run status
			$sql = "/* $debugMsg */ SELECT distinct T.tcversion_id,E.build_id,NH.parent_id AS tcase_id " .
				   " FROM {$this->tables['testplan_tcversions']}  T " .
				   " JOIN {$this->tables['nodes_hierarchy']}  NH ON T.tcversion_id=NH.id " .
				   " AND NH.node_type_id={$node_types['testcase_version']} " .
				   " LEFT OUTER JOIN {$this->tables['executions']} E ON T.tcversion_id = E.tcversion_id " .
				   " AND T.testplan_id=E.testplan_id AND E.build_id IN ({$build_in}) " .
				   " WHERE T.testplan_id={$id} AND E.build_id IS NULL ";
		} else {
			//anything else
			$sql = "/* $debugMsg */ SELECT EE.status,SQ1.tcversion_id, NH.parent_id AS tcase_id," .
			       " COUNT(EE.status) AS exec_qty " .
				   " FROM {$this->tables['executions']} EE, {$this->tables['nodes_hierarchy']} NH," .
				   " (SELECT E.tcversion_id,E.build_id,MAX(E.id) AS last_exec_id " .
				   "  FROM {$this->tables['executions']} E " .
				   "  WHERE E.build_id IN ({$build_in}) GROUP BY E.tcversion_id,E.build_id) AS SQ1 " .
				   " WHERE EE.build_id IN ({$build_in}) " .
				   " AND EE.status IN ('" . $status_in . "') AND NH.node_type_id={$node_types['testcase_version']} " .
				   " AND SQ1.last_exec_id=EE.id AND SQ1.tcversion_id=NH.id " .
				   " GROUP BY status,SQ1.tcversion_id,NH.parent_id";
		}

		$recordset = $this->db->fetchRowsIntoMap($sql,'tcase_id');
		return $recordset;
	}

	
	/**
	 * BUGID 2455, BUGID 3026
	 * find all builds for which a testcase has not been executed
	 * 
	 * @author asimon
	 * @param integer $id Build ID
	 * @param array $buildSet build set to check
	 * @return array $new_set set of builds which match the search criterium
	 */
	function get_not_run_for_any_build($id,$buildSet) {
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;

		$node_types=$this->tree_manager->get_available_node_types();
		
		$results = array();
		
		foreach ($buildSet as $build) {
			$sql = "/* $debugMsg */ SELECT distinct T.tcversion_id, E.build_id, E.status, NH.parent_id AS tcase_id " .
				   " FROM {$this->tables['testplan_tcversions']} T " .
				   " JOIN {$this->tables['nodes_hierarchy']} NH ON T.tcversion_id=NH.id  AND NH.node_type_id=4 " .
				   " LEFT OUTER JOIN {$this->tables['executions']} E ON T.tcversion_id = E.tcversion_id " .
				   " AND T.testplan_id=E.testplan_id AND E.build_id=$build " .
				   " WHERE T.testplan_id={$id} AND E.status IS NULL ";
			
			$results[] = $this->db->fetchRowsIntoMap($sql,'tcase_id');
		}
		
		$recordset = array();
		foreach ($results as $result) 
		{
			$recordset = array_merge_recursive($recordset, $result);
		} 
		
		$new_set = array();
		foreach ($recordset as $key => $val) {
			$new_set[$val['tcase_id']] = $val;
		}
		
		return $new_set;
	}


	/**
	 * link platforms to a new Test Plan
	 * 
	 * @param int $source_id original Test Plan id
	 * @param int $target_id new Test Plan id
	 * @param array $mappings: key source platform id, target platform id
	 *                         USED when copy is done to a test plan that BELONGS to
	 *                         another Test Project.
	 */
	private function copy_platforms_links($source_id, $target_id, $mappings = null)
	{
    	$sourceLinks = $this->platform_mgr->getLinkedToTestplanAsMap($source_id);
    	if( !is_null($sourceLinks) )
    	{
    		$sourceLinks = array_keys($sourceLinks);
    		if( !is_null($mappings) )
    		{
    			foreach($sourceLinks as $key => $value)
    			{
    				$sourceLinks[$key] = $mappings[$value];
    			}
    		}
    		$this->platform_mgr->linkToTestplan($sourceLinks,$target_id);
    	}
	}

    /**
	 * 
 	 *
 	 * outputFormat: 
 	 *				'array',
 	 *				'map', 
 	 *				'mapAccessByID' => map access key: id
	 *				'mapAccessByName' => map access key: name
	 *
	 * 20100711 - franciscom - BUGID 3564
 	 */
    function getPlatforms($id,$options=null)
    {
        $my['options'] = array('outputFormat' => 'array', 'addIfNull' => false);
	    $my['options'] = array_merge($my['options'], (array)$options);
        switch($my['options']['outputFormat'])
        {
        	case 'map':
        		$platforms = $this->platform_mgr->getLinkedToTestplanAsMap($id);
        	break;
        	
        	default:
        		$opt = array('outputFormat' => $my['options']['outputFormat']);
        		$platforms = $this->platform_mgr->getLinkedToTestplan($id,$opt);
        	break;
        } 	
    	
    	if( $my['options']['addIfNull'] && is_null($platforms) )
		{
			$platforms = array( 0 => '');
		}
    	return $platforms; 
    }

	/**
	 * Logic to determine if platforms should be visible for a given testplan.
	 * @return bool true if the testplan has one or more linked platforms;
	 *              otherwise false.
	 */
    function hasLinkedPlatforms($id)
    {
    	return $this->platform_mgr->platformsActiveForTestplan($id);
    }	



    /**
     * changes platform id on a test plan linked test case versions for
     * a target platform.
     * Corresponding executions information is also updated
     *
	 * @param id: test plan id
	 * @param from: plaftorm id to update (used as filter criteria).
	 * @param to: new plaftorm id value
	 * @param tcversionSet: default null, can be array with tcversion id
	 *                      (used as filter criteria).
	 *
 	 *
 	 */
    function changeLinkedTCVersionsPlatform($id,$from,$to,$tcversionSet=null)
    {
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
    	$sqlFilter = '';
    	if( !is_null($tcversionSet) )
    	{
			$sqlFilter = " AND tcversion_id IN (" . implode(',',(array)$tcversionSet) . " ) ";
    	}
    	$whereClause = " WHERE testplan_id = {$id} AND platform_id = {$from} {$sqlFilter}";

        $sqlStm = array();
		$sqlStm[] = "/* {$debugMsg} */ " . 
		            " UPDATE {$this->tables['testplan_tcversions']} " .
			        " SET platform_id = {$to} " . $whereClause;

		$sqlStm[] = "/* {$debugMsg} */" .
		            " UPDATE {$this->tables['executions']} " .
			        " SET platform_id = {$to} " . $whereClause;

        foreach($sqlStm as $sql)
        {
			$this->db->exec_query($sql);		
		}
    }

    /**
     *
	 * @param id: test plan id
	 * @param platformSet: default null, used as filter criteria.
	 * @return map: key platform id, values count,platform_id
 	 */
	public function countLinkedTCVersionsByPlatform($id,$platformSet=null)
	{
		$sqlFilter = '';
		if( !is_null($platformSet) )
		{
			$sqlFilter = " AND platform_id IN (" . implode(',',(array)$platformSet). ") ";
		}
		$sql = " SELECT COUNT(testplan_id) AS qty,platform_id " .
		       " FROM {$this->tables['testplan_tcversions']} " .
			   " WHERE testplan_id={$id} {$sqlFilter} " .
			   " GROUP BY platform_id ";
		$rs = $this->db->fetchRowsIntoMap($sql,'platform_id');
		return $rs;
	}


    /**
     * get detailed information of test case versions linke to test plan an NOT executed
     * gives detaile for each platform and build combination
     *
     * @deprecated 1.9
     *
	 * @param id: test plan id
	 * @param filters: optional, map with following keys
	 *                 build_id: contains a build id (just one) to be filtered
	 *                 platform_id: contains a platform id (just one) to be filtered
	 *
	 * @param options: optional map with following keys
	 *                 group_by_platform_tcversion: true -> in this way we will get one record
	 *                                              for each platform no matter on how many builds
	 *                                              test case version has not been executed.
	 *                                              when this option is set, filters are ignored
	 * @return map: 
 	 */
	public function getNotExecutedLinkedTCVersionsDetailed($id,$filters=null,$options=null)
	{
		$debugMsg = 'Class:' . __CLASS__ . ' - Method: ' . __FUNCTION__;
		$resultsCfg = config_get('results');
		$status_not_run=$resultsCfg['status_code']['not_run'];
        $executions_join = "";

        $my['filters'] = array('build_id' => 0,'platform_id' => null);
		$my['filters'] = array_merge($my['filters'], (array)$filters);

        $my['options'] = array('group_by_platform_tcversion' => false);
		$my['options'] = array_merge($my['options'], (array)$options);

		$sqlFilter = "";
        foreach($my['filters'] as $key => $value)
        {
        	if( !is_null($value) && $value > 0)
        	{
        		$sqlFilter .= " AND {$key} = {$value} "; 
        	}	
        } 
        
        if($my['options']['group_by_platform_tcversion'])
        {
			$build_fields = " ";
            $build_join = " ";
			$executions_join = " E.tcversion_id=TPTCV.tcversion_id " .
			                   " AND E.testplan_id = TPTCV.testplan_id " .
			                   " AND E.platform_id = TPTCV.platform_id ";
		    $sqlFilter = "";
        }
        else
        {
			$build_fields = " B.id AS build_id, B.name AS build_name, " .
			                " B.release_date AS build_release_date, " .
			                " B.closed_on_date AS build_closed_on_date,";
            $build_join = " JOIN {$this->tables['builds']} B ON  B.testplan_id=TPTCV.testplan_id " ;
			$executions_join = " E.build_id=B.id AND E.tcversion_id=TPTCV.tcversion_id " .
			                   " AND E.testplan_id = TPTCV.testplan_id " .
			                   " AND E.platform_id = TPTCV.platform_id ";
        }

		$sql = "/* {$debugMsg} */ ";
		$sql .= "SELECT COALESCE(E.status,'" . $status_not_run . "') AS exec_status, " .
		        $build_fields .
		        " PLAT.name AS platform_name," . 
		        " NODE_TCASE.parent_id AS testsuite_id, NODE_TCASE.name AS name, NODE_TCASE.id AS tc_id," .
		        " NODE_TCASE.node_order," .
		        " TPTCV.id AS feature_id, TPTCV.testplan_id, TPTCV.tcversion_id, " .
		        " TPTCV.node_order AS exec_node_order, TPTCV.author_id AS linked_by," .
		        " TPTCV.creation_ts AS link_creation_ts, TPTCV.platform_id, " . 
			    " TCV.version AS version, TCV.active, TCV.summary, " .
			    " TCV.tc_external_id AS external_id, TCV.execution_type," .
				" COALESCE(UA.user_id,0) AS assigned_to, " .
				" (urgency * importance) AS priority " .
				" FROM {$this->tables['testplan_tcversions']} TPTCV " .
				$build_join .
				" /* get test case version info */ " .
				" JOIN {$this->tables['tcversions']} TCV ON TCV.id=TPTCV.tcversion_id " .
				" /* get test case name */ " .
				" JOIN {$this->tables['nodes_hierarchy']} NODE_TCV ON NODE_TCV.id=TPTCV.tcversion_id " .
				" JOIN {$this->tables['nodes_hierarchy']} NODE_TCASE ON NODE_TCASE.id=NODE_TCV.parent_id " .
				" /* get platform name */ " .
				" LEFT OUTER JOIN {$this->tables['platforms']} PLAT ON " .
				" PLAT.id=TPTCV.platform_id " .
				" /* get assigned user id */ " .
				" LEFT OUTER JOIN {$this->tables['user_assignments']} UA ON UA.feature_id = TPTCV.id " .
				" LEFT OUTER JOIN {$this->tables['executions']} E ON " .
				$executions_join .
				" WHERE TPTCV.testplan_id={$id} {$sqlFilter} AND E.status IS NULL " .
				" ORDER BY testsuite_id, node_order";

        $result = $this->db->get_recordset($sql);
 		return $result;
	}

    /**
     *
	 * @param tplan_id: test plan id
	 * @return map: 
	 *
	 * @internal revisions
	 * 20100610 - eloff - BUGID 3515 - take platforms into account
 	 */
	public function getStatusTotals($tplan_id)
	{
		$code_verbose = $this->getStatusForReports();
	
		$filters=null;
		$options=array('output' => 'mapOfMap');
		$execResults = $this->get_linked_tcversions($tplan_id,$filters,$options);
	
		$totals = array('total' => 0,'not_run' => 0);
		foreach($code_verbose as $status_code => $status_verbose)
		{
			$totals[$status_verbose]=0;
		}
		foreach($execResults as $key => $testcases)
		{
			foreach($testcases as $testcase)
			{
				$totals['total']++;
				$totals[$code_verbose[$testcase['exec_status']]]++;
			}
		}
		return $totals;
	}


    /**
	 * DocBlock with nested lists
 	 *
 	 */
	public function getStatusForReports()
	{
		// This will be used to create dynamically counters if user add new status
		$resultsCfg = config_get('results');
		foreach( $resultsCfg['status_label_for_exec_ui'] as $tc_status_verbose => $label)
		{
			$code_verbose[$resultsCfg['status_code'][$tc_status_verbose]] = $tc_status_verbose;
		}
		if( !isset($resultsCfg['status_label_for_exec_ui']['not_run']) )
		{
			$code_verbose[$resultsCfg['status_code']['not_run']] = 'not_run';
		}
		return $code_verbose;
	}

    /**
     *
	 * @param tplan_id: test plan id
	 * @return map:
	 *
	 *	'type' => 'platform'
	 *	'total_tc => ZZ
	 *	'details' => array ( 'passed' => array( 'qty' => X)
	 *	                     'failed' => array( 'qty' => Y)
	 *	                     'blocked' => array( 'qty' => U)
	 *                       ....)
	 *
	 * @internal revision
	 * 20100610 - eloff - BUGID 3515 - rewrite inspired by getStatusTotals()
	 * 20100201 - franciscom - BUGID 3121
	 */
	public function getStatusTotalsByPlatform($tplan_id)
	{
		$code_verbose = $this->getStatusForReports();

		$filters=null;
		$options=array('output' => 'mapOfMap');
		$execResults = $this->get_linked_tcversions($tplan_id,$filters,$options);

		$code_verbose = $this->getStatusForReports();
		$platformSet = $this->getPlatforms($tplan_id, array('outputFormat' => 'map'));
		$totals = null;
		$platformIDSet = is_null($platformSet) ? array(0) : array_keys($platformSet);

		foreach($platformIDSet as $platformID)
		{
			$totals[$platformID]=array(
				'type' => 'platform',
				'name' => $platformSet[$platformID],
				'total_tc' => 0,
				'details' => array());
			foreach($code_verbose as $status_code => $status_verbose)
			{
				$totals[$platformID]['details'][$status_verbose]['qty'] = 0;
			}
		}
		foreach($execResults as $key => $testcases)
		{
			foreach($testcases as $platform_id => $testcase)
			{
				$totals[$platform_id]['total_tc']++;
				$totals[$platform_id]['details'][$code_verbose[$testcase['exec_status']]]['qty']++;
			}
		}
		return $totals;
	}

	/**
	 * @param int $tplan_id test plan id
	 * @return map:
	 *	'type' => 'priority'
	 *	'total_tc => ZZ
	 *	'details' => array ( 'passed' => array( 'qty' => X)
	 *	                     'failed' => array( 'qty' => Y)
	 *	                     'blocked' => array( 'qty' => U)
	 *	                      ....)
	 *
	 * @internal revision
	 * 20100614 - eloff - refactor to same style as the other getStatusTotals...()
	 * 20100206 - eloff - BUGID 3060
	 */
	public function getStatusTotalsByPriority($tplan_id)
	{
		$code_verbose = $this->getStatusForReports();
		$urgencyCfg = config_get('urgency');
		$prioSet = array(
			HIGH => lang_get($urgencyCfg['code_label'][HIGH]),
			MEDIUM => lang_get($urgencyCfg['code_label'][MEDIUM]),
			LOW => lang_get($urgencyCfg['code_label'][LOW]));
		$totals = array();

		foreach($prioSet as $prioCode => $prioLabel)
		{
			$totals[$prioCode]=array('type' => 'priority',
				'name' => $prioLabel,
				'total_tc' => 0,
				'details' => null);
			foreach($code_verbose as $status_code => $status_verbose)
			{
				$totals[$prioCode]['details'][$status_verbose]['qty']=0;
			}
		}
		$filters = null;
		$options=array('output' => 'mapOfMap');
		$execResults = $this->get_linked_tcversions($tplan_id,$filters,$options);

		foreach($execResults as $testcases)
		{
			foreach($testcases as $testcase)
			{
				$prio_level = $this->urgencyImportanceToPriorityLevel($testcase['priority']);
				$totals[$prio_level]['total_tc']++;
				$totals[$prio_level]['details'][$code_verbose[$testcase['exec_status']]]['qty']++;
			}
		}
		return $totals;
	}

	/**
	 * get last execution status analised by keyword, used to build reports.
	 *
	 * @param tplan_id: test plan id
	 * @return map: key: keyword id
	 *              value: map with following structure
	 *
	 *
 	 */
	public function getStatusTotalsByKeyword($tplan_id)
	{
		$code_verbose = $this->getStatusForReports();
		$totals = null;
		$filters=null;
		$options=array('output' => 'map');
		$execResults = $this->get_linked_tcversions($tplan_id,$filters,$options);

		if( !is_null($execResults) )
		{
			$tcaseSet = array_keys($execResults);
			$kw=$this->tcase_mgr->getKeywords($tcaseSet,null,'keyword_id',' ORDER BY keyword ASC ');
			if( !is_null($kw) )
			{
				$keywordSet = array_keys($kw);
				foreach($keywordSet as $keywordID)
				{
					$totals[$keywordID]['type'] = 'keyword';
					$totals[$keywordID]['name']=$kw[$keywordID][0]['keyword'];
					$totals[$keywordID]['notes']=$kw[$keywordID][0]['notes'];
					$totals[$keywordID]['total_tc'] = 0;
					foreach($code_verbose as $status_code => $status_verbose)
					{
						$totals[$keywordID]['details'][$status_verbose]['qty']=0;
					}
				}

				foreach($keywordSet as $keywordID)
				{
					foreach($kw[$keywordID] as $kw_tcase)
					{
						$status = $execResults[$kw_tcase['testcase_id']]['exec_status'];
						$totals[$keywordID]['total_tc']++;
						$totals[$keywordID]['details'][$code_verbose[$status]]['qty']++;
					}
				}
			}
		}

		return $totals;
	}

    /**
     * 
	 * @param id: test plan id
	 * @return map: 
 	 *             key: user id
 	 *             value: map with key=platform id
 	 *                             value: map with keys: 'total' and verbose status
 	 *                                             values: test case count.
 	 *                              
 	 */
	public function getStatusTotalsByAssignedTesterPlatform($id)
	{
		$code_verbose = $this->getStatusForReports();
		$filters = null;
		$user_platform = null;
		$options = array('output' => 'mapOfMap');
    	$execResults = $this->get_linked_tcversions($id,$filters,$options);
    	
	    if( !is_null($execResults) )
	    {
	    	$tcaseSet = array_keys($execResults);
            foreach($tcaseSet as $tcaseID)
            {
            	$testcaseInfo=$execResults[$tcaseID];
            	$platformIDSet = array_keys($execResults[$tcaseID]);
            	foreach($platformIDSet as $platformID)
            	{
            		
            		$testedBy = $testcaseInfo[$platformID]['tester_id'];
            		$assignedTo = $testcaseInfo[$platformID]['user_id'];
            		$assignedTo = !is_null($assignedTo) && $assignedTo > 0 ? $assignedTo : TL_USER_NOBODY;
            		$execStatus = $testcaseInfo[$platformID]['exec_status'];
					
            		// to avoid errors due to bad or missing config
            		$verboseStatus = isset($code_verbose[$execStatus]) ? $code_verbose[$execStatus] : $execStatus;
            		
            		// 20100425 - francisco.mancardi@gruppotesi.com
            		if( $assignedTo != TL_USER_NOBODY )
            		{
            			if( !isset($user_platform[$assignedTo][$platformID]) )
            			{
            				$user_platform[$assignedTo][$platformID]['total']=0;
            			}
            			
            			if( !isset($user_platform[$assignedTo][$platformID][$verboseStatus]) )
            			{
            				$user_platform[$assignedTo][$platformID][$verboseStatus]=0;
            			}
            		}   
            		
            		$testerBoy = is_null($testedBy) ? $assignedTo : $testedBy; 
            		if( !isset($user_platform[$testerBoy][$platformID]) )
            		{
            			$user_platform[$testerBoy][$platformID]['total']=0;
            		}
            		
            		if( !isset($user_platform[$testerBoy][$platformID][$verboseStatus]) )
            		{
            			$user_platform[$testerBoy][$platformID][$verboseStatus]=0;
            		}   
                    
            		$user_platform[$testerBoy][$platformID]['total']++;
            		$user_platform[$testerBoy][$platformID][$verboseStatus]++;
                   
				}
            } 
        }
        return $user_platform;
    }

    /**
     * 
	 * @param id: test plan id
	 * @return map: 
 	 *             key: user id
 	 *             value: map with key=platform id
 	 *                             value: map with keys: 'total' and verbose status
 	 *                                             values: test case count.
 	 *                              
 	 */
	public function getStatusTotalsByAssignedTester($id)
	{
		$unassigned = lang_get('unassigned');
		$data_set = $this->getStatusTotalsByAssignedTesterPlatform($id);
	    if( !is_null($data_set) )
	    {
			$code_verbose = $this->getStatusForReports();

	    	$userSet = array_keys($data_set);
	    	// need to find a better way (with less overhead and data movement) to do this
            $userCol=tlUser::getByIDs($this->db,$userSet,tlUser::TLOBJ_O_GET_DETAIL_MINIMUM);
            foreach($userSet as $assignedTo)
            {
            	$user_platform[$assignedTo]['type'] = 'assignedTester';
            	$user_platform[$assignedTo]['name'] = $unassigned; 
            	if( $assignedTo > 0 )
            	{
            		$user_platform[$assignedTo]['name'] = $userCol[$assignedTo]->getDisplayName();;
            	}
            	$user_platform[$assignedTo]['total_tc'] = 0;
            	
   				foreach($code_verbose as $status_code => $status_verbose)
			    {
					$user_platform[$assignedTo]['details'][$status_verbose]['qty']=0;
			    }
            	
            	// this will be removed from final result
            	$user_platform[$assignedTo]['details']['total']['qty'] = 0;
            	
            	$platformIDSet = array_keys($data_set[$assignedTo]);
            	foreach($platformIDSet as $platformID)
            	{
            		foreach( $data_set[$assignedTo][$platformID] as $verboseStatus => $counter)
            		{
            			if( !isset($user_platform[$assignedTo]['details'][$verboseStatus]) )
            			{
            				$user_platform[$assignedTo]['details'][$verboseStatus]['qty']=0;
            			}   
            		    $user_platform[$assignedTo]['details'][$verboseStatus]['qty'] += $counter;
            		}
				}
				$user_platform[$assignedTo]['total_tc']=$user_platform[$assignedTo]['details']['total']['qty'];
				unset($user_platform[$assignedTo]['details']['total']);
            } 
        }
        return $user_platform;
    }


    /**
     * 
	 * @param id: test plan id
	 * @return map: 
 	 */
	public function getStatusByAssignedTesterPlatform($id)
	{
		$filters = null;
		$info = null;
		$options = array('output' => 'mapOfMap');
    	$execResults = $this->get_linked_tcversions($id,$filters,$options);
	    if( !is_null($execResults) )
	    {
	    	$tcaseSet = array_keys($execResults);
            foreach($tcaseSet as $tcaseID)
            {
            	$testcaseInfo=$execResults[$tcaseID];
            	$platformIDSet = array_keys($execResults[$tcaseID]);
            	foreach($platformIDSet as $platformID)
            	{
            		$assignedTo = $testcaseInfo[$platformID]['user_id'];
            		$assignedTo = !is_null($assignedTo) && $assignedTo > 0 ? $assignedTo : TL_USER_NOBODY;   
					$info[$assignedTo][$tcaseID][$platformID] = $testcaseInfo[$platformID]['exec_status'];
				}
            } 
        }

        return $info;
    }

	/**
	 * 
 	 *
     */
	function tallyResultsForReport($results)
	{
		if ($results == null)
		{
			return null;
		}
		$na_string = lang_get('not_aplicable');
		$keySet = array_keys($results);
		foreach($keySet as $keyID)
		{
			$results[$keyID]['percentage_completed'] = 0;
			$totalCases = $results[$keyID]['total_tc'];
			$target = &$results[$keyID]['details']; 
			if ($totalCases != 0)
			{
				$results[$keyID]['percentage_completed'] = 
						number_format((($totalCases - $target['not_run']['qty']) / $totalCases) * 100,2);
						
				foreach($target as $status_verbose => $qty)
				{
					$target[$status_verbose]['percentage']=(($target[$status_verbose]['qty']) / $totalCases) * 100;
					$target[$status_verbose]['percentage']=number_format($target[$status_verbose]['percentage'],2);
				}
			} else {
				// 20100722 - asimon: if $target[$status_verbose]['percentage'] is not set,
				// it causes warnings in the template later, so it has to be set here
				// if $totalCases == 0 to avoid later undefined index warnings in the log
				foreach($target as $status_verbose => $qty) {
					$target[$status_verbose]['percentage'] = $na_string;
				}
			}
		}
		return $results;
	} // end function


	/**
	 * getTestCaseSiblings()
	 *
	 * @internal revisions
	 * 20100520 - franciscom - missed platform_id piece on join
	 */
	function getTestCaseSiblings($id,$tcversion_id,$platform_id)
	{
		$sql = 	" SELECT NHTSET.name as testcase_name,NHTSET.id AS testcase_id , NHTCVSET.id AS tcversion_id," .
        		" NHTC.parent_id AS testsuite_id, " .
        		" TPTCVX.id AS feature_id, TPTCVX.node_order " .
				" from {$this->tables['testplan_tcversions']} TPTCVMAIN " .
				" JOIN {$this->tables['nodes_hierarchy']} NHTCV ON NHTCV.id = TPTCVMAIN.tcversion_id " . 
				" JOIN {$this->tables['nodes_hierarchy']} NHTC ON NHTC.id = NHTCV.parent_id " . 
				" JOIN {$this->tables['nodes_hierarchy']} NHTSET ON NHTSET.parent_id = NHTC.parent_id " .
				" JOIN {$this->tables['nodes_hierarchy']} NHTCVSET ON NHTCVSET.parent_id = NHTSET.id " .
				" JOIN {$this->tables['testplan_tcversions']} TPTCVX " . 
				" ON TPTCVX.tcversion_id = NHTCVSET.id " .
				" AND TPTCVX.testplan_id = TPTCVMAIN.testplan_id " .
				" AND TPTCVX.platform_id = TPTCVMAIN.platform_id " .
				" WHERE TPTCVMAIN.testplan_id = {$id} AND TPTCVMAIN.tcversion_id = {$tcversion_id} " .
				" AND TPTCVMAIN.platform_id = {$platform_id} " .
				" ORDER BY node_order,testcase_name ";
		$siblings = $this->db->fetchRowsIntoMap($sql,'tcversion_id');
		return $siblings;
	}


	/**
	 * getTestCaseNextSibling()
	 *
	 */
	function getTestCaseNextSibling($id,$tcversion_id,$platform_id)
	{
		$sibling = null;
    	$brothers_and_sisters = $this->getTestCaseSiblings($id,$tcversion_id,$platform_id);
    	$tcversionSet = array_keys($brothers_and_sisters);
    	$elemQty = count($tcversionSet);
    	$dummy = array_flip($tcversionSet);
        $pos = $dummy[$tcversion_id]+1;
        $sibling_tcversion = $pos < $elemQty ? $tcversionSet[$pos] : 0;
        if( $sibling_tcversion > 0 )
        {
        	$sibling = array('tcase_id' => $brothers_and_sisters[$sibling_tcversion]['testcase_id'],
        	                 'tcversion_id' => $sibling_tcversion);
        }
        return $sibling;
    }

    /**
     * Convert a given urgency and importance to a priority level using
     * threshold values in $tlCfg->priority_levels.
     *
     * @param mixed $urgency Urgency of the testcase.
     *      If this is the only parameter given then interpret it as
     *      $urgency*$importance.
     * @param mixed $importance Importance of the testcase. (Optional)
     *
     * @return int HIGH, MEDIUM or LOW
     */
    public function urgencyImportanceToPriorityLevel($urgency, $importance=null)
    {
        static $priorityLevelsCfg;
        if ($priorityLevelsCfg == null) {
            $priorityLevelsCfg = config_get('priority_levels');
        }

        $urgencyImportance = intval($urgency) * (is_null($importance) ? 1 : intval($importance)) ;
		
        $levels2check = array(HIGH,MEDIUM,LOW);  // order is important for algorithm
		foreach($levels2check as $level)
		{
			if($urgencyImportance >= $priorityLevelsCfg[$level])
			{
				break;
			}
		}		
        return $level;
        
    }


	// -------------------
    /**
     * 
	 * @param id: test plan id
	 * @return map: 
 	 *             key: user id
 	 *             value: map with key=platform id
 	 *                             value: map with keys: 'total' and verbose status
 	 *                                             values: test case count.
 	 *                              
 	 */
	public function getStatusTotalsByTesterPlatform($id)
	{
		$code_verbose = $this->getStatusForReports();
		$filters = null;
		$user_platform = null;
		$options = array('output' => 'mapOfMap');
    	$execResults = $this->get_linked_tcversions($id,$filters,$options);
    	
	    if( !is_null($execResults) )
	    {
	    	$tcaseSet = array_keys($execResults);
            foreach($tcaseSet as $tcaseID)
            {
            	$testcaseInfo=$execResults[$tcaseID];
            	$platformIDSet = array_keys($execResults[$tcaseID]);
            	foreach($platformIDSet as $platformID)
            	{
            		$testedBy = $testcaseInfo[$platformID]['tester_id'];
            		$testedBy = !is_null($testedBy) && $testedBy > 0 ? $testedBy : TL_USER_NOBODY;
            		$execStatus = $testcaseInfo[$platformID]['exec_status'];
            		
            		// to avoid errors due to bad or missing config
            		$verboseStatus = isset($code_verbose[$execStatus]) ? $code_verbose[$execStatus] : $execStatus;
            		
            		if( !isset($user_platform[$testedBy][$platformID]) )
            		{
            			$user_platform[$testedBy][$platformID]['total']=0;
            		}
            		
            		if( !isset($user_platform[$testedBy][$platformID][$verboseStatus]) )
            		{
            			$user_platform[$testedBy][$platformID][$verboseStatus]=0;
            		}   
            		$user_platform[$testedBy][$platformID]['total']++;
            		$user_platform[$testedBy][$platformID][$verboseStatus]++;
				}
            } 
        }
	    
        return $user_platform;
    }

    /**
     * 
	 * @param id: test plan id
	 * @return map: 
 	 *             key: user id
 	 *             value: map with key=platform id
 	 *                             value: map with keys: 'total' and verbose status
 	 *                                             values: test case count.
 	 *                              
 	 */
	public function getStatusTotalsByTester($id)
	{
		$unassigned = lang_get('unassigned');
		$data_set = $this->getStatusTotalsByAssignedTesterPlatform($id);
	    if( !is_null($data_set) )
	    {
			$code_verbose = $this->getStatusForReports();

	    	$userSet = array_keys($data_set);
	    	// need to find a better way (with less overhead and data movement) to do this
            $userCol=tlUser::getByIDs($this->db,$userSet,tlUser::TLOBJ_O_GET_DETAIL_MINIMUM);
            foreach($userSet as $testedBy)
            {
            	$user_platform[$testedBy]['type'] = 'tester';
            	$user_platform[$testedBy]['name'] = $unassigned; 
            	if( $testedBy > 0 )
            	{
            		$user_platform[$testedBy]['name'] = $userCol[$testedBy]->getDisplayName();;
            	}
            	$user_platform[$testedBy]['total_tc'] = 0;
            	
   				foreach($code_verbose as $status_code => $status_verbose)
			    {
					$user_platform[$testedBy]['details'][$status_verbose]['qty']=0;
			    }
            	
            	// this will be removed from final result
            	$user_platform[$testedBy]['details']['total']['qty'] = 0;
            	
            	$platformIDSet = array_keys($data_set[$assignedTo]);
            	foreach($platformIDSet as $platformID)
            	{
            		foreach( $data_set[$testedBy][$platformID] as $verboseStatus => $counter)
            		{
            			if( !isset($user_platform[$testedBy]['details'][$verboseStatus]) )
            			{
            				$user_platform[$testedBy]['details'][$verboseStatus]['qty']=0;
            			}   
            		    $user_platform[$testedBy]['details'][$verboseStatus]['qty'] += $counter;
            		}
				}
				$user_platform[$testedBy]['total_tc']=$user_platform[$testedBy]['details']['total']['qty'];
				unset($user_platform[$testedBy]['details']['total']);
            } 
        }
        return $user_platform;
    }




} // end class testplan


// ######################################################################################
/** 
 * Build Manager Class 
 * @package TestLink
 **/
class build_mgr extends tlObject
{
	/** @var database handler */
	var $db;

	/** 
	 * class constructor 
	 * 
	 * @param resource &$db reference to database handler
	 **/
	function build_mgr(&$db)
	{
   		parent::__construct();
		$this->db = &$db;
	}


  /*
    function: create

    args :
          $tplan_id
          $name
          $notes
          [$active]: default: 1
          [$open]: default: 1
          [release_date]: YYYY-MM-DD


    returns:

    rev :
  */
	function create($tplan_id,$name,$notes = '',$active=1,$open=1,$release_date='')
	{
		$targetDate=trim($release_date);
		$sql = " INSERT INTO {$this->tables['builds']} " .
			" (testplan_id,name,notes,release_date,active,is_open,creation_ts) " .
			" VALUES ('". $tplan_id . "','" .
			$this->db->prepare_string($name) . "','" .
			$this->db->prepare_string($notes) . "',";
		if($targetDate == '')
		{
			$sql .= "NULL,";
		}       
		else
		{
			$sql .= "'" . $this->db->prepare_string($targetDate) . "',";
		}
		
		
		// Important: MySQL do not support default values on datetime columns that are functions
		// that's why we are using db_now().
		$sql .= "{$active},{$open},{$this->db->db_now()})"; 	                     
		
		
		$new_build_id = 0;
		$result = $this->db->exec_query($sql);
		if ($result)
		{
			$new_build_id = $this->db->insert_id($this->tables['builds']);
		}
		
		return $new_build_id;
	}


  /*
    function: update

    args :
          $id
          $name
          $notes
          [$active]: default: null
          [$open]: default: null
          [$release_date]=''    FORMAT YYYY-MM-DD
          [$closed_on_date]=''  FORMAT YYYY-MM-DD

    returns:

    rev :
  */
	function update($id,$name,$notes,$active=null,$open=null,$release_date='',$closed_on_date='')
	{
		$closure_date = '';
		$targetDate=trim($release_date);
		$sql = " UPDATE {$this->tables['builds']} " .
			" SET name='" . $this->db->prepare_string($name) . "'," .
			"     notes='" . $this->db->prepare_string($notes) . "'";
		
		if($targetDate == '')
		{
			$sql .= ",release_date=NULL";
		}       
		else
		{
			$sql .= ",release_date='" . $this->db->prepare_string($targetDate) . "'";
		}
		if( !is_null($active) )
		{
			$sql .=" , active=" . intval($active);
		}
		
		if( !is_null($open) )
		{
			$open_status=intval($open) ? 1 : 0; 
			$sql .=" , is_open=" . $open_status;
			
			if($open_status == 1)
			{
				$closure_date = ''; 
			}
		}
		
		if($closure_date == '')
		{
			$sql .= ",closed_on_date=NULL";
		}       
		else
		{
			// may be will be useful validate date format
			$sql .= ",closed_on_date='" . $this->db->prepare_string($closure_date) . "'";
		}
		
		$sql .= " WHERE id={$id}";
		$result = $this->db->exec_query($sql);
		return $result ? 1 : 0;
	}

	/**
	 * Delete a build
	 * 
	 * @param integer $id
	 * @return integer status code
	 * 
	 * @internal revisions:
	 *  20100716 - asimon - BUGID 3406: delete user assignments with build
	 */
	function delete($id)
	{
		// 20090611 - franciscom
		// Need to be fixed, because execution bugs are not delete
		
		$sql = " DELETE FROM {$this->tables['executions']}  " .
			" WHERE build_id={$id}";
		
		$result=$this->db->exec_query($sql);
		
		// 3406 - delete user assignments with build
		$sql = " DELETE FROM {$this->tables['user_assignments']}  " .
			" WHERE build_id={$id}";		
		$result=$this->db->exec_query($sql);
		
		$sql = " DELETE FROM {$this->tables['builds']} " .
			" WHERE id={$id}";
		
		$result=$this->db->exec_query($sql);
		return $result ? 1 : 0;
	}


  /*
    function: get_by_id
              get information about a build

    args : id: build id

    returns: map with following keys
             id: build id
             name: build name
             notes: build notes
             active: build active status
             is_open: build open status
             testplan_id

    rev :
  */
	function get_by_id($id)
	{
		$sql = "SELECT * FROM {$this->tables['builds']} WHERE id = {$id}";
		$result = $this->db->exec_query($sql);
		$myrow = $this->db->fetch_array($result);
		return $myrow;
	}

	/**
	 * Set date of closing build
	 * 
	 * @param integer $id Build identifier
	 * @param string $targetDate, format YYYY-MM-DD. can be null
	 * 
	 * @return TBD TBD
	 */
	function setClosedOnDate($id,$targetDate)
	{
		$sql = " UPDATE {$this->tables['builds']} ";
		
		if( is_null($targetDate) )
		{
			$sql .= " SET closed_on_date=NULL ";
		}
		else
		{
			$sql .= " SET closed_on_date='" . $this->db->prepare_string($targetDate) . "'";  	    
		}
		$sql .= " WHERE id={$id} "; 

		$result = $this->db->exec_query($sql);
	}


} // end class build_mgr


// ##################################################################################
/** 
 * Milestone Manager Class 
 * @package TestLink
 **/
class milestone_mgr extends tlObject
{
	/** @var database handler */
	var $db;

	/** 
	 * class constructor 
	 * 
	 * @param resource &$db reference to database handler
	 **/
	function milestone_mgr(&$db)
	{
        parent::__construct();
		$this->db = &$db;
	}

  /*
    function: create()

    args :
            $tplan_id
            $name
            $target_date: string with format: 
            $start_date: 
            $low_priority: percentage
            $medium_priority: percentage
            $high_priority: percentage

    returns:

  */
	function create($tplan_id,$name,$target_date,$start_date,$low_priority,$medium_priority,$high_priority)
	{
		$new_milestone_id=0;
		$dateFields=null;
		$dateValues=null;
		$dateKeys=array('target_date','start_date');
		
		// check dates
		foreach($dateKeys as $varname)
		{
			$value=	trim($$varname);
			if($value != '') 
			{
				if (($time = strtotime($value)) == -1 || $time === false) 
				{
                   die (__FUNCTION__ . ' Abort - Invalid date');
                }
				$dateFields[]=$varname;	
		        $dateValues[]=" '{$this->db->prepare_string($value)}' ";
			}
		}
		$additionalFields='';
		if( !is_null($dateFields) )
		{
			$additionalFields= ',' . implode(',',$dateFields) ;
			$additionalValues= ',' . implode(',',$dateValues) ;
		}
		$sql = "INSERT INTO {$this->tables['milestones']} " .
		       " (testplan_id,name,a,b,c{$additionalFields}) " .
			   " VALUES (" . $tplan_id . ",'{$this->db->prepare_string($name)}'," .
			   $low_priority . "," .  $medium_priority . "," . $high_priority . 
			   $additionalValues . ")";
		$result = $this->db->exec_query($sql);
		
		if ($result)
		{
			$new_milestone_id = $this->db->insert_id($this->tables['milestones']);
		}
		
		return $new_milestone_id;
	}

  /*
    function: update

    args :
          $id
          $name
          $notes
          [$active]: default: 1
          [$open]: default: 1



    returns:

    rev :
  */
	function update($id,$name,$target_date,$start_date,$low_priority,$medium_priority,$high_priority)
	{
		$sql = "UPDATE {$this->tables['milestones']} " . 
		       " SET name='{$this->db->prepare_string($name)}', " .
			   " target_date='{$this->db->prepare_string($target_date)}', " .
			   " start_date='{$this->db->prepare_string($start_date)}', " .
			   " a={$low_priority}, b={$medium_priority}, c={$high_priority} WHERE id={$id}";
		$result = $this->db->exec_query($sql);
		return $result ? 1 : 0;
	}



  /*
    function: delete

    args :
          $id


    returns:

  */
	function delete($id)
	{
		$sql = "DELETE FROM {$this->tables['milestones']} WHERE id={$id}";
		$result=$this->db->exec_query($sql);
		return $result ? 1 : 0;
	}


  /*
    function: get_by_id

    args :
          $id
    returns:

    rev: 20090103 - franciscom - get test plan name.
  */
	function get_by_id($id)
	{
		$sql=" SELECT M.id, M.name, M.a AS high_percentage, M.b AS medium_percentage, M.c AS low_percentage, " .
			 " M.target_date, M.start_date, M.testplan_id, NH.name as testplan_name " .   
			 " FROM {$this->tables['milestones']} M, {$this->tables['nodes_hierarchy']} NH " .
			 " WHERE M.id = {$id} AND NH.id=M.testplan_id";
		$myrow = $this->db->fetchRowsIntoMap($sql,'id');
		return $myrow;
	}

	/**
	 * check existence of milestone name in Test Plan
	 * 
	 * @param integer $tplan_id  test plan id.
	 * @param string $milestone_name milestone name
	 * @param integer $milestone_id default: null
	 *                when is not null we add milestone_id as filter, this is useful
	 *                to understand if is really a duplicate when using this method
	 *                while managing update operations via GUI
	 * 
	 * @return integer 1 => name exists
	 */
	function check_name_existence($tplan_id,$milestone_name,$milestone_id=null,$case_sensitive=0)
	{
		$sql = " SELECT id, name FROM {$this->tables['milestones']} " .
			   " WHERE testplan_id = {$tplan_id} ";
		
		if($case_sensitive)
		{
			$sql .= " AND name=";
		}
		else
		{
			$milestone_name=strtoupper($milestone_name);
			$sql .= " AND UPPER(name)=";
		}
		$sql .= "'{$this->db->prepare_string($milestone_name)}'";
		
		if( !is_null($milestone_id) )
		{
			$sql .= " AND id <> " . $this->db->prepare_int($milestone_id);
		}
		
		$result = $this->db->exec_query($sql);
		$status= $this->db->num_rows($result) ? 1 : 0;
		
		return $status;
	}


  /*
    function: get_all_by_testplan
              get info about all milestones defined for a testlan
    args :
          tplan_id


    returns:

    rev :
  */
	function get_all_by_testplan($tplan_id)
	{
		$sql=" SELECT M.id, M.name, M.a AS high_percentage, M.b AS medium_percentage, M.c AS low_percentage, " .
			 " M.target_date, M.start_date, M.testplan_id, NH.name as testplan_name " .   
			 " FROM {$this->tables['milestones']} M, {$this->tables['nodes_hierarchy']} NH " .
			 " WHERE testplan_id={$tplan_id} AND NH.id = testplan_id " .
			 " ORDER BY M.target_date,M.name";
		$rs=$this->db->get_recordset($sql);
		return $rs;
	}


} // end class milestone_mgr
?>
Back to Top