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
|
<?php
/**
* Resource.php - model class for a resource
*
* The Resource class is the base class of the new
* Room and Resource management system in Stud.IP.
* It provides core functionality for handling general resources
* and can be derived for handling special resources.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Moritz Strohm <strohm@data-quest.de>
* @copyright 2017-2019
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package resources
* @since 4.5
*
* @property string $id database column
* @property string $parent_id database column
* @property string $category_id database column
* @property int|null $level database column
* @property string $name database column
* @property I18NString|null $description database column
* @property int $requestable database column
* @property int $lockable database column
* @property int $mkdate database column
* @property int $chdate database column
* @property int $sort_position database column
* @property SimpleORMapCollection|ResourceProperty[] $properties has_many ResourceProperty
* @property SimpleORMapCollection|ResourcePermission[] $permissions has_many ResourcePermission
* @property SimpleORMapCollection|ResourceRequest[] $requests has_many ResourceRequest
* @property SimpleORMapCollection|ResourceBooking[] $bookings has_many ResourceBooking
* @property SimpleORMapCollection|Resource[] $children has_many Resource
* @property ResourceCategory $category belongs_to ResourceCategory
* @property Resource $parent belongs_to Resource
* @property mixed $class_name additional field
*/
class Resource extends SimpleORMap implements StudipItem
{
protected static function configure($config = [])
{
$config['db_table'] = 'resources';
$config['belongs_to']['category'] = [
'class_name' => ResourceCategory::class,
'foreign_key' => 'category_id',
'assoc_func' => 'find'
];
$config['has_many']['properties'] = [
'class_name' => ResourceProperty::class,
'assoc_foreign_key' => 'resource_id',
'on_delete' => 'delete',
'on_store' => 'store'
];
$config['has_many']['permissions'] = [
'class_name' => ResourcePermission::class,
'assoc_foreign_key' => 'resource_id',
'on_delete' => 'delete',
'on_store' => 'store'
];
$config['has_many']['requests'] = [
'class_name' => ResourceRequest::class,
'assoc_foreign_key' => 'resource_id',
'on_delete' => 'delete',
'on_store' => 'store'
];
$config['has_many']['bookings'] = [
'class_name' => ResourceBooking::class,
'assoc_foreign_key' => 'resource_id',
'on_delete' => 'delete',
'on_store' => 'store'
];
$config['has_many']['children'] = [
'class_name' => Resource::class,
'assoc_func' => 'findChildren',
'on_delete' => 'delete',
'on_store' => 'store'
];
$config['belongs_to']['parent'] = [
'class_name' => Resource::class,
'foreign_key' => 'parent_id'
];
$config['i18n_fields']['description'] = true;
$config['additional_fields']['class_name'] = ['category', 'class_name'];
$config['registered_callbacks']['before_store'][] = 'cbValidate';
parent::configure($config);
}
/**
* This is a cache for permissions that users have on resources.
* It is meant to reduce the database requests in cases where the
* same permission is queried a lot of times.
*/
protected static $permission_cache;
/**
* Returns the children of a resource.
* The children are converted to an instance of the derived class,
* if they are not instances of the default Resource class.
*/
public static function findChildren($resource_id)
{
$children = self::findBySql(
'parent_id = :parent_id ORDER BY name ASC',
['parent_id' => $resource_id]
);
if (!$children) {
return [];
}
foreach ($children as &$child) {
$child = $child->getDerivedClassInstance();
}
return $children;
}
/**
* Returns a translation of the resource class name.
* The translated name can be singular or plural, depending
* on the value of the parameter $item_count.
*
* @param int $item_count The amount of items the translation shall be
* made for. This is only used to determine, if a singular or a
* plural form shall be returned.
*
* @return string The translated form of the class name, either in
* singular or plural.
*
*/
public static function getTranslatedClassName($item_count = 1)
{
return ngettext(
'Ressource',
'Ressourcen',
$item_count
);
}
/**
* Retrieves all resources which don't have a parent resource assigned.
* Such resources are called root resources since they are roots of
* a resource hierarchy (or a resource tree).
*
* @return Resource[] An array of Resource objects
* which are root resources.
*/
public static function getRootResources()
{
return self::findBySql("parent_id = '' ORDER BY name");
}
/**
* A method for overloaded classes so that they can define properties
* that are required for that resource class.
*
* @return string[] An array with the names of the required properties.
* Example: The properties with the names "foo", "bar" and "baz"
* are required properties. The array would have the following content:
* [
* 'foo',
* 'bar',
* 'baz'
* ]
*/
public static function getRequiredProperties()
{
return [];
}
/**
* Returns the part of the URL for getLink and getURL which will be
* placed inside the calls to URLHelper::getLink and URLHelper::getURL
* in these methods.
*
* @param string $action The action for the resource.
* @param string $id The ID of the resource.
*
* @return string The URL path for the specified action.
* @throws InvalidArgumentException If $resource_id is empty.
*
*/
protected static function buildPathForAction($action = 'show', $id = null)
{
$actions_without_id = ['add'];
if (!$id && !in_array($action, $actions_without_id)) {
throw new InvalidArgumentException(
_('Zur Erstellung der URL fehlt eine Ressourcen-ID!')
);
}
switch ($action) {
case 'show':
return 'dispatch.php/resources/resource/index/' . $id;
case 'add':
return 'dispatch.php/resources/resource/add';
case 'edit':
return 'dispatch.php/resources/resource/edit/' . $id;
case 'files':
return 'dispatch.php/resources/resource/files/' . $id . '/';
case 'permissions':
return 'dispatch.php/resources/resource/permissions/' . $id;
case 'temporary_permissions':
return 'dispatch.php/resources/resource/temporary_permissions/' . $id;
case 'booking_plan':
return 'dispatch.php/resources/room_planning/booking_plan/' . $id;
case 'request_plan':
return 'dispatch.php/resources/room_planning/request_plan/' . $id;
case 'semester_plan':
return 'dispatch.php/resources/room_planning/semester_plan/' . $id;
case 'assign-undecided':
return 'dispatch.php/resources/booking/add/' . $id;
case 'assign':
return 'dispatch.php/resources/booking/add/' . $id . '/0';
case 'reserve':
return 'dispatch.php/resources/booking/add/' . $id . '/1';
case 'lock':
return 'dispatch.php/resources/booking/add/' . $id . '/2';
case 'delete_bookings':
return 'dispatch.php/resources/resource/delete_bookings/' . $id;
case 'export_bookings':
return 'dispatch.php/resources/export/resource_bookings/' . $id;
case 'delete':
return 'dispatch.php/resources/resource/delete/' . $id;
default:
return 'dispatch.php/resources/resource/show/' . $id;
}
}
/**
* Returns the appropriate link for the resource action that shall be
* executed on a resource.
*
* @param string $action The action which shall be executed.
* For default Resources the actions 'show', 'add', 'edit' and 'delete'
* are defined.
* @param string $id The ID of the resource on which the specified
* action shall be executed.
* @param array $link_parameters Optional parameters for the link.
*
* @return string The Link for the resource action.
* @throws InvalidArgumentException If $resource_id is empty.
*
*/
public static function getLinkForAction(
$action = 'show',
$id = null,
$link_parameters = []
)
{
return URLHelper::getLink(
self::buildPathForAction($action, $id),
$link_parameters
);
}
/**
* Returns the appropriate URL for the resource action that shall be
* executed on a resource.
*
* @param string $action The action which shall be executed.
* For default Resources the actions 'show', 'add', 'edit' and 'delete'
* are defined.
* @param string $id The ID of the resource on which the specified
* action shall be executed.
* @param array $url_parameters Optional parameters for the URL.
*
* @return string The URL for the resource action.
* @throws InvalidArgumentException If $resource_id is empty.
*
*/
public static function getURLForAction(
$action = 'show',
$id = null,
$url_parameters = []
)
{
return URLHelper::getURL(
self::buildPathForAction($action, $id),
$url_parameters
);
}
/**
* The SORM store method is overloaded to assure that the right level
* attribute is stored.
*/
public function store()
{
//Set the level attribute according to the parent's
//level attribute. If no parents are defined
//set the level to zero.
if ($this->parent_id && $this->parent) {
$this->level = $this->parent->level + 1;
} else {
$this->level = 0;
}
//Store the folder, if it hasn't been stored before:
$folder = $this->getFolder();
if ($folder) {
$folder->store();
}
return parent::store();
}
public function delete()
{
//Delete the folder:
$folder = $this->getFolder(false);
if ($folder) {
$folder->delete();
}
return parent::delete();
}
public function cbValidate()
{
if (!$this->category_id) {
throw new InvalidResourceException(
sprintf(
_('Die Ressource %s ist keiner Ressourcenkategorie zugeordnet!'),
$this->name
)
);
}
return true;
}
/**
* @see StudipItem::__toString
*/
public function __toString()
{
return $this->getFullName();
}
/**
* Retrieves the folder for this resource.
*
* @param bool $create_if_missing Whether to create a folder (true) or
* not (false) in case no folder exists for this resource.
* Defaults to true.
*
* @returns ResourceFolder|null Either a ResourceFolder instance or null
* in case no such instance can be retrieved or created.
*/
public function getFolder($create_if_missing = true)
{
$folder = Folder::findOneByRange_id($this->id);
if ($folder) {
$folder = $folder->getTypedFolder();
if ($folder instanceof ResourceFolder) {
//Only return ResourceFolder instances.
return $folder;
}
} elseif ($create_if_missing) {
$folder = $this->createFolder();
if ($folder instanceof ResourceFolder) {
return $folder;
}
}
//In all other cases return null:
return null;
}
public function setFolder(ResourceFolder $folder)
{
if ($this->isNew()) {
$this->store();
}
$folder->range_id = $this->id;
$folder->range_type = 'Resource';
return $folder->store();
}
public function createFolder()
{
if ($this->isNew()) {
$this->id = $this->getNewId();
}
$folder = Folder::createTopFolder(
$this->id,
'Resource',
'ResourceFolder'
);
if ($folder) {
$folder = $folder->getTypedFolder();
if ($folder) {
$folder->store();
return $folder;
}
}
return null;
}
/**
* Returns a list of property names that are required
* for the resource class.
*
* @return string[] An array with the property names.
*/
public function getRequiredPropertyNames()
{
return [];
}
/**
* This is a simplified version of the createBooking method.
* @param User $user
* @param DateTime $begin
* @param DateTime $end
* @param int $preparation_time
* @param string $description
* @param string $internal_comment
* @param int $booking_type
* @return ResourceBooking
* @see Resource::createBooking
*/
public function createSimpleBooking(
User $user,
DateTime $begin,
DateTime $end,
$preparation_time = 0,
$description = '',
$internal_comment = '',
$booking_type = ResourceBooking::TYPE_NORMAL
)
{
return $this->createBooking(
$user,
$user->id,
[
[
'begin' => $begin,
'end' => $end
]
],
null,
0,
null,
$preparation_time,
$description,
$internal_comment,
$booking_type
);
}
/**
* Creates bookings from a request.
* @param User $user
* @param ResourceRequest $request The request from which
* a resource booking shall be built.
* @param int $preparation_time
* @param string $description
* @param string $internal_comment
* @param int $booking_type
* @param bool $prepend_preparation_time . If this is set to true,
* the preparation time will end before the start of the
* requested time. If $prepend_preparation_time is set to false
* (the default) the preparation time starts with the start of the
* requested time.
* @param bool $notify_lecturers
* @return ResourceBooking[] A list of resource bookings
* matching the request.
* @throws ResourceRequestException if the request could not be marked
* as resolved.
*
* @throws ResourceUnavailableException if the resource cannot be assigned
* in at least one of the time ranges specified by the resource request.
*/
public function createBookingFromRequest(
User $user,
ResourceRequest $request,
$preparation_time = 0,
$description = '',
$internal_comment = '',
$booking_type = ResourceBooking::TYPE_NORMAL,
$prepend_preparation_time = false,
$notify_lecturers = false
)
{
$course_dates = $request->getAffectedDates();
$bookings = [];
if ($course_dates) {
foreach ($course_dates as $course_date) {
$booking = $this->createBooking(
$user,
$course_date->id,
[
[
'begin' => (
$prepend_preparation_time
? $course_date->date - $preparation_time
: $course_date->date
),
'end' => $course_date->end_time
]
],
null,
0,
$course_date->end_time,
$preparation_time,
$description,
$internal_comment,
$booking_type
);
if ($booking instanceof ResourceBooking) {
$bookings[] = $booking;
}
}
} elseif (count($request->appointments)) {
//It is a request for multiple single dates.
//Such requests are resolved into multiple bookings.
foreach ($request->appointments as $appointment) {
$begin = (
$prepend_preparation_time
? $appointment->appointment->date - $preparation_time
: $appointment->appointment->date
);
$end = $appointment->appointment->end_time;
$booking = $this->createBooking(
$user,
$appointment->appointment_id,
[
[
'begin' => $begin,
'end' => $end
]
],
null,
0,
$end,
$preparation_time,
$description,
$internal_comment,
$booking_type
);
if ($booking instanceof ResourceBooking) {
$bookings[] = $booking;
}
}
} else {
//No date objects for the request.
//It is a simple request:
$booking = $this->createBooking(
$user,
$request->user->id,
[
[
'begin' => (
$prepend_preparation_time
? $request->begin - $preparation_time
: $request->begin
),
'end' => $request->end
]
],
null,
0,
$request->end,
$preparation_time,
$description,
$internal_comment,
$booking_type
);
if ($booking instanceof ResourceBooking) {
$bookings[] = $booking;
}
}
if (!$request->closeRequest($notify_lecturers)) {
throw new ResourceRequestException(
_('Die Anfrage konnte nicht als bearbeitet markiert werden!')
);
}
return $bookings;
}
/**
* A factory method for creating a ResourceBooking object
* for this resource.
*
* @param User $user The user who wishes to create a resource booking.
* @param string $range_id The ID of the user (or the Stud.IP object)
* which owns the ResourceBooking.
* @param array[][] $time_ranges The time ranges for the booking.
* At least one time range has to be specified using unix timestamps
* or DateTime objects.
* This array has the following structure:
* [
* [
* 'begin' => The begin timestamp or DateTime object.
* 'end' => The end timestamp or DateTime object.
* ]
* ]
* @param DateInterval|null $repetition_interval The repetition interval
* for the new booking. This must be a DateInterval object if
* repetitions shall be stored.
* Otherwise this parameter must be set to null.
* @param int $repeat_amount The amount of repetitions.
* This parameter is only regarded if $repetition_interval contains
* a DateInterval object.
* In case repetitions are specified by their end date set this
* parameter to 0.
* @param DateTime|string|null $repetition_end_date The end date of the
* repetition. This can either be an unix timestamp or a DateTime object
* and will only be regarded if $repetition_interval contains a
* DateInterval object.
* In case repetitions are specified by their amount set this
* parameter to null.
* @param int $repetition_amount (obsolete, has no effect)
* @param int $preparation_time The preparation time which is needed before
* the real start time. This will be substracted
* from the begin timestamp and stored in an extra column of the
* resource_bookings table.
* @param string $description An optional description for the booking.
* This fields was previously known as "user_free_name".
* @param string $internal_comment An optional comment for the
* booking which is intended to be used internally
* in the room and resource administration staff.
* @param int $booking_type The booking type.
* 0 = normal booking
* 1 = reservation
* 2 = lock booking
* @param bool $force_booking If this parameter is set to true,
* overlapping bookings are removed before storing this booking.
*
* @param string $weekdays The weekdays (1 - 7) on which the booking
* shall take place. This is only used when a booking with repetitions
* shall only take place on some weekdays.
*
* @return ResourceBooking object.
* @throws InvalidArgumentException If no time ranges are specified
* or if there is an error regarding the time ranges.
* @throws ResourceBookingRangeException If $range_id is not set.
* @throws ResourceBookingOverlapException If the booking overlaps
* with another booking or a resource lock.
* @throws ResourcePermissionException If the specified user does not
* have sufficient permissions to create a resource booking.
* @throws ResourceBookingException If the repetition interval
* is invalid or if the resource booking cannot be stored.
*
*/
public function createBooking(
User $user,
$range_id = null,
$time_ranges = [],
$repetition_interval = null,
$repetition_amount = 0,
$repetition_end_date = null,
$preparation_time = 0,
$description = '',
$internal_comment = '',
$booking_type = ResourceBooking::TYPE_NORMAL,
$force_booking = false,
string $weekdays = ''
)
{
if (!is_array($time_ranges)) {
throw new InvalidArgumentException(
_('Es wurden keine Zeitbereiche fĂĽr die Buchung angegeben!')
);
}
$booking_begin = null;
$booking_end = null;
//Check if each entry of the $time_intervals array is in the right
//format and if it contains either timestamps or DateTime objects.
//After that the time ranges are checked for validity (begin > end)
//and if there are locks or bookings in one of the time ranges.
//Furthermore all reservations that are affected by this booking
//are collected so that the persons who made the reservations
//can be informed about the new booking.
$affected_reservations = [];
foreach ($time_ranges as $index => $time_range) {
$begin = $time_range['begin'];
$end = $time_range['end'];
if ($begin === null || $end === null) {
throw new InvalidArgumentException(
_('Mindestens eines der Zeitintervalls ist im falschen Format!')
);
}
if (!($begin instanceof DateTime)) {
$b = new DateTime();
$b->setTimestamp($begin);
$begin = $b;
}
if (!($end instanceof DateTime)) {
$e = new DateTime();
$e->setTimestamp($end);
$end = $e;
}
$real_begin = clone $begin;
if ($preparation_time > 0) {
$real_begin = $real_begin->sub(
new DateInterval('PT' . $preparation_time . 'S')
);
}
if ($real_begin > $end) {
throw new InvalidArgumentException(
_('Der Startzeitpunkt darf nicht hinter dem Endzeitpunkt liegen!')
);
}
$duration = $end->getTimestamp() - $begin->getTimestamp();
$min_duration = Config::get()->RESOURCES_MIN_BOOKING_TIME;
if ($min_duration && ($duration < ($min_duration * 60))) {
throw new InvalidArgumentException(
sprintf(
_('Die minimale Buchungsdauer von %1$d Minuten wurde unterschritten!'),
$min_duration
)
);
}
if ($index == array_keys($time_ranges)[0]) {
$booking_begin = clone $begin;
$booking_end = clone $end;
}
if ($repetition_interval instanceof DateInterval) {
//We must calculate the end of the repetition interval
//by using $repetition_amount or $repetition_end_date.
$repetition_end = null;
if ($repetition_end_date instanceof DateTime) {
$repetition_end = $repetition_end_date;
} elseif ($repetition_end_date) {
//convert $repetition_end_date to a DateTime object:
$red = new DateTime();
$red->setTimestamp($repetition_end_date);
$repetition_end = $red;
} else {
//$repetition_end_date is not set: Use $repetition_amount.
//Add the repetition interval $repetition_amount times
//to the $real_begin DateTime object to get the end date
//of the repetition:
$repetition = clone $real_begin;
for ($i = 0; $i < $repetition_amount; $i++) {
$repetition = $repetition->add($repetition_interval);
}
$repetition_end = $repetition;
}
$current_date = clone $real_begin;
//Check for each repetition if the resource is available
//or locked:
while ($current_date <= $repetition_end) {
$current_begin = clone $current_date;
$current_end = clone $current_date;
$current_end->setTime(
intval($end->format('H')),
intval($end->format('i')),
intval($end->format('s'))
);
if ($current_begin < $current_end) {
$affected_reservations = array_merge(
ResourceBooking::findByResourceAndTimeRanges(
$this,
[
[
'begin' => $current_begin->getTimestamp(),
'end' => $current_end->getTimestamp(),
]
],
[1, 3]
),
$affected_reservations
);
}
$current_date = $current_date->add($repetition_interval);
}
} else {
$affected_reservations = array_merge(
ResourceBooking::findByResourceAndTimeRanges(
$this,
[
[
'begin' => $real_begin->getTimestamp(),
'end' => $end->getTimestamp(),
]
],
[1, 3]
),
$affected_reservations
);
}
}
$booking = new ResourceBooking();
$booking->resource_id = $this->id;
$booking->booking_user_id = $user->id;
$booking->range_id = $range_id;
$booking->description = $description;
$booking->begin = $booking_begin->getTimestamp();
$booking->end = $booking_end->getTimestamp();
if ($repetition_interval instanceof DateInterval) {
if ($repetition_end_date) {
if ($repetition_end_date instanceof DateTime) {
$booking->repeat_end = $repetition_end_date->getTimestamp();
} else {
$booking->repeat_end = $repetition_end_date;
}
}
$booking->repetition_interval = $repetition_interval->format('P%YY%MM%DD');
$booking->weekdays = $weekdays;
}
if ($preparation_time) {
$booking->preparation_time = $preparation_time;
} else {
$booking->preparation_time = '0';
}
$booking->internal_comment = $internal_comment;
$booking->booking_type = (int)$booking_type;
//We can finally store the new booking.
try {
$booking->store($force_booking);
} catch (ResourceBookingOverlapException $e) {
if ($begin->format('Ymd') == $end->format('Ymd')) {
throw new ResourceBookingOverlapException(
sprintf(
_('%1$s: Die Buchung vom %2$s bis %3$s konnte wegen Ăśberlappungen nicht gespeichert werden: %4$s'),
$this->getFullName(),
$begin->format('d.m.Y H:i'),
$end->format('H:i'),
$e->getMessage()
),
$e->getCode(),
$e->getRange()
);
} else {
throw new ResourceBookingOverlapException(
sprintf(
_('%1$s: Die Buchung vom %2$s bis %3$s konnte wegen Ăśberlappungen nicht gespeichert werden: %4$s'),
$this->getFullName(),
$begin->format('d.m.Y H:i'),
$end->format('d.m.Y H:i'),
$e->getMessage()
),
$e->getCode(),
$e->getRange()
);
}
} catch (Exception $e) {
if ($begin->format('Ymd') == $end->format('Ymd')) {
throw new ResourceBookingException(
sprintf(
_('%1$s: Die Buchung vom %2$s bis %3$s konnte aus folgendem Grund nicht gespeichert werden: %4$s'),
$this->getFullName(),
$begin->format('d.m.Y H:i'),
$end->format('H:i'),
$e->getMessage()
)
);
} else {
throw new ResourceBookingException(
sprintf(
_('%1$s: Die Buchung vom %2$s bis %3$s konnte aus folgendem Grund nicht gespeichert werden: %4$s'),
$this->getFullName(),
$begin->format('d.m.Y H:i'),
$end->format('d.m.Y H:i'),
$e->getMessage()
)
);
}
}
return $booking;
}
/**
* This method creates a simple request for this resource.
* A simple request is not bound to a date, metadate
* or course object and its time ranges. Instead the time
* range is specified directly.
* Note that simple resource requests do not support recurrence.
*
* @param User $user The user who wishes to create a simple request.
* @param DateTime $begin The begin timestamp of the request.
* @param DateTime $end The end timestamp of the request.
* @param string $comment A comment for the resource request.
* @param int $preparation_time The requested preparation time before
* the begin of the requested time range. This parameter must be
* specified in seconds. Only positive values are accepted.
*
* @return ResourceRequest A resource request object.
* @throws AccessDeniedException If the user is not permitted
* to request this resource.
* @throws InvalidArgumentException If the the timestamps provided by
* $begin and $end are invalid or if $begin is greater than or equal
* to $end which results in an invalid time range.
* @throws ResourceUnavailableException If the resource is not available
* in the selected time range.
* @throws ResourceRequestException If the resource request
* cannot be stored.
*
*/
public function createSimpleRequest(
User $user,
DateTime $begin,
DateTime $end,
$comment = '',
$preparation_time = 0
)
{
//All users are permitted to create a request,
//if the resource is requestable.
if (!$this->requestable) {
throw new InvalidArgumentException(
_('Diese Ressource kann nicht angefragt werden!')
);
}
if ($begin > $end) {
throw new InvalidArgumentException(
_('Der Startzeitpunkt darf nicht hinter dem Endzeitpunkt liegen!')
);
} elseif ($begin == $end) {
throw new InvalidArgumentException(
_('Startzeitpunkt und Endzeitpunkt sind identisch!')
);
}
if (!$this->isAvailable($begin, $end)) {
throw new ResourceUnavailableException(
sprintf(
_('Die Ressource %1$s ist im Zeitraum von %2$s bis %3$s nicht verfĂĽgbar!'),
$this->name,
$begin->format('d.m.Y H:i'),
$end->format('d.m.Y H:i')
)
);
}
$request = new ResourceRequest();
$request->resource_id = $this->id;
$request->category_id = $this->category_id;
$request->user_id = $user->id;
$request->begin = $begin->getTimestamp();
$request->end = $end->getTimestamp();
$request->preparation_time = (
$preparation_time > 0
? $preparation_time
: 0
);
$request->closed = '0';
$request->comment = $comment;
if (!$request->store()) {
throw new ResourceRequestException(
sprintf(
_('Die Anfrage zur Ressource %s konnte nicht gespeichert werden!'),
$this->name
)
);
}
return $request;
}
/**
* This method creates a resource request for this resource.
*
* @param User $user The user who wishes to create a request.
* @param string|array $date_range_ids One or more IDs of Stud.IP objects
* which can provide at least one time range.
* Objects which fulfill this requirement are
* course dates (CourseDate objects),
* cycle dates (SeminarCycleDate objects)
* and courses (Course objects).
* If only one ID is provided it can be passed as string.
* If multiple IDs are provided they have to be passed as array.
* @param string $comment A comment for the resource request.
* @param mixed[] $properties The wishable properties
* for the resource request. The format of the array is as follows:
* [
* 'property name' => 'property state'
* ]
* @param int $preparation_time The requested preparation time before
* the begin of the requested time range. This parameter must be
* specified in seconds. Only positive values are accepted.
*
* @return ResourceRequest A resource request object.
* @throws InvalidArgumentException If $date_range_id is not set.
* or no object which can provide at least one time range
* can be found with the specified ID.
* @throws ResourceNoTimeRangeException If no time range can be found
* by looking at the object, specified by its ID in $date_range_id.
* @throws ResourceUnavailableException If the resource is not available
* in the selected time range.
* @throws ResourceRequestException If the resource request
* cannot be stored.
*
*/
public function createRequest(
User $user,
$date_range_ids = null,
$comment = '',
$properties = [],
$preparation_time = 0
)
{
if (!$date_range_ids) {
throw new InvalidArgumentException(
_('Es wurde keine ID eines Objektes angegeben, welches Zeiträume für eine Ressourcenanfrage liefern kann!')
);
}
if (!$this->requestable) {
throw new InvalidArgumentException(
_('Diese Ressource kann nicht angefragt werden!')
);
}
//We must get the date ranges by looking at $date_range_id
//and the object which lies behind that ID.
if (!is_array($date_range_ids)) {
$date_range_ids = [$date_range_ids];
}
$time_ranges = [];
foreach ($date_range_ids as $date_range_id) {
$time_ranges = array_merge(
$time_ranges,
ResourceManager::getTimeRangesFromRangeId(
$date_range_id
)
);
}
if (!$time_ranges) {
//We couldn't find any time range.
throw new ResourceNoTimeRangeException(
sprintf(
_('Es konnte kein Zeitbereich fĂĽr die Anfrage der Ressource %s gefunden werden.'),
$this->name
)
);
}
//Default resource request handling:
//Check if the resource is available in all requested time ranges.
foreach ($time_ranges as $time_range) {
if (!$this->isAvailable($time_range[0], $time_range[1])) {
throw new ResourceUnavailableException(
sprintf(
_('Die Ressource %1$s ist im Zeitraum vom %2$s bis %3$s nicht verfĂĽgbar!'),
$this->name,
$time_range[0]->format('d.m.Y H:i'),
$time_range[1]->format('d.m.Y H:i')
)
);
}
}
//We must check, if all the properties exist:
if ($properties and is_array($properties)) {
foreach ($properties as $property_name => $property_state) {
$property_object = ResourcePropertyDefinition::findByName(
$property_name
);
if (!$property_object) {
throw new ResourcePropertyException(
sprintf(
_('Die Ressourceneigenschaft %s ist nicht definiert!'),
$property_name
)
);
} elseif (count($property_object) > 1) {
throw new ResourcePropertyException(
sprintf(
_('Es gibt mehrere Ressourceneigenschaften mit dem Namen %s!'),
$property_name
)
);
}
//$property_object is an array of ResourcePropertyDefinition objects:
$property_data[] = [
'object' => $property_object[0],
'state' => $property_state
];
}
}
$request = new ResourceRequest();
$request->resource_id = $this->id;
$request->category_id = $this->category_id;
$request->user_id = $user->id;
$request->comment = $comment;
$request->preparation_time = (
$preparation_time > 0
? $preparation_time
: 0
);
$request->closed = '0';
//Resolve the date range ID and set the
//appropriate field in the request object:
if (count($date_range_ids) <= 1) {
$course_date = CourseDate::find($date_range_ids[0]);
if ($course_date) {
$request->termin_id = $course_date->id;
} else {
$cycle_date = SeminarCycleDate::find($date_range_ids[0]);
if ($cycle_date) {
$request->metadate_id = $cycle_date->id;
} else {
$course = Course::find($date_range_ids[0]);
if ($course) {
$request->course_id = $course->id;
}
}
}
if (!$request->store()) {
throw new ResourceRequestException(
sprintf(
_('Die Anfrage zur Ressource %s konnte nicht gespeichert werden!'),
$this->name
)
);
}
} else {
if (!$request->store()) {
throw new ResourceRequestException(
sprintf(
_('Die Anfrage zur Ressource %s konnte nicht gespeichert werden!'),
$this->name
)
);
}
//More than one entry:
//We must use ResourceBookingAppointment objects.
foreach ($date_range_ids as $date_range_id) {
$appointment_id = null;
$course_date = CourseDate::find($date_range_id);
if ($course_date) {
$appointment_id = $course_date->id;
} else {
$cycle_date = SeminarCycleDate::find($date_range_id);
if ($cycle_date) {
$appointment_id = $cycle_date->id;
} else {
$course = Course::find($date_range_id);
if ($course) {
$appointment_id = $course->id;
}
}
}
if ($appointment_id) {
$rra = new ResourceRequestAppointment();
$rra->request_id = $request->id;
$rra->appointment_id = $appointment_id;
if (!$rra->store()) {
throw new ResourceRequestException(
_('Die Terminzuordnungen zur Anfrage konnten nicht gespeichert werden!')
);
}
}
}
}
//The request has been created: Now we need to link the properties:
if(!empty($property_data)) {
foreach ($property_data as $property) {
$rrp = new ResourceRequestProperty();
$rrp->request_id = $request->id;
$rrp->property_id = $property['object']->id;
$rrp->state = intval($property['state']);
if (!$rrp->store()) {
throw new InvalidResourceRequestException(
sprintf(
_('%1$s: Die Eigenschaft %2$s zur Anfrage konnte nicht gespeichert werden!'),
$this->getFullName(),
$property['object']->name
)
);
}
}
}
return $request;
}
/**
* Creates a lock booking for this resource.
*
* @param User $user The user who wishes to create a lock booking.
* @param DateTime $begin The begin of the lock time range.
* @param DateTime $end The end of the lock time range.
* @param string $internal_comment An optional comment for the
* lock booking which is intended to be used internally
* in the room and resource administration staff.
*
* @return ResourceBooking A ResourceBooking object.
* @throws ResourceUnavailableException If a lock booking already
* exists in the specified time range.
*
* @throws AccessDeniedException If the user does not have sufficient
* permissions to lock this resource.
*/
public function createLock(
User $user,
DateTime $begin,
DateTime $end,
$internal_comment = ''
)
{
if (!$this->userHasPermission($user, 'admin', [$begin, $end])) {
throw new AccessDeniedException(
sprintf(
_('%s: Unzureichende Berechtigungen zum Erstellen einer Sperrbuchung!'),
$this->getFullName()
)
);
}
if ($this->isLocked($begin, $end)) {
throw new ResourceUnavailableException(
sprintf(
_('%1$s: Im Zeitbereich von %2$s bis %3$s gibt es bereits Sperrbuchungen!'),
$this->getFullName(),
$begin->format('d.m.Y H:i'),
$end->format('d.m.Y H:i')
)
);
}
$lock = new ResourceBooking();
$lock->booking_type = ResourceBooking::TYPE_LOCK;
$lock->range_id = $user->id;
$lock->resource_id = $this->id;
$lock->begin = $begin->getTimestamp();
$lock->end = $end->getTimestamp();
$lock->internal_comment = $internal_comment;
if (!$lock->store()) {
throw new ResourceBookingException(
sprintf(
_('%1$s: Fehler beim Speichern der Sperrbuchung fĂĽr den Zeitbereich von %2$s bis %3$s!'),
$begin->format('d.m.Y H:i'),
$end->format('d.m.Y H:i')
)
);
}
return $lock;
}
/**
* Retrieves the properties grouped by their property groups
* and in the order specified in that group.
*
* @param string[] excluded_properties An array with the names
* of the properties that shall be excluded from the result set.
*
* @return array An array with the group names as keys and the properties
* in the second array dimension. The structure of the array
* is as follows:
* [
* group1 name => [
* property1,
* property2,
* ...
* ],
* group2 name => [
* ...
* ]
* ]
*/
public function getGroupedProperties($excluded_properties = [])
{
if (is_array($excluded_properties) && count($excluded_properties)) {
$properties = ResourceProperty::findBySql(
"INNER JOIN resource_property_definitions rpd
USING (property_id)
LEFT JOIN resource_property_groups rpg
ON rpd.property_group_id = rpg.id
WHERE
resource_properties.resource_id = :resource_id
AND
rpd.name NOT IN ( :excluded_properties )
ORDER BY
rpg.position ASC, rpg.name ASC,
rpd.property_group_pos ASC, rpd.name ASC",
[
'resource_id' => $this->id,
'excluded_properties' => $excluded_properties
]
);
} else {
$properties = ResourceProperty::findBySql(
"INNER JOIN resource_property_definitions rpd
USING (property_id)
LEFT JOIN resource_property_groups rpg
ON rpd.property_group_id = rpg.id
WHERE
resource_properties.resource_id = :resource_id
ORDER BY
rpg.position ASC, rpg.name ASC,
rpd.property_group_pos ASC, rpd.name ASC",
[
'resource_id' => $this->id
]
);
}
if (!$properties) {
return [];
}
$property_groups = [];
foreach ($properties as $property) {
if (!$property->state) {
continue;
}
$group_name = '';
if (!empty($property->definition->group->name)) {
$group_name = $property->definition->group->name;
}
if (empty($property_groups[$group_name]) || !is_array($property_groups[$group_name])) {
$property_groups[$group_name] = [];
}
$property_groups[$group_name][] = $property;
}
return $property_groups;
}
/**
* Determines wheter this resource has a property
* with the specified name.
*
* @param string $name The name of the resource property.
*
* @return bool True, if this resource has a property with
* the specified name, false otherwise.
*/
public function propertyExists($name = '')
{
if (!$name) {
return false;
}
$db = DBManager::get();
$exists_stmt = $db->prepare(
"SELECT TRUE FROM resource_properties
INNER JOIN resource_property_definitions rpd
ON resource_properties.property_id = rpd.property_id
WHERE resource_properties.resource_id = :resource_id
AND rpd.name = :name");
$exists_stmt->execute(
[
'resource_id' => $this->id,
'name' => $name
]
);
$exists = $exists_stmt->fetchColumn(0);
return (bool)$exists;
}
/**
* Retrieves a ResourceProperty object for a property of this resource
* which has the specified name. If the property has not been set for this
* resource, but is defined for this resource's category, a new
* ResourceProperty object will be created, stored and returned.
*
* @param string $name The name of the resource property.
*
* @return ResourceProperty|null Either a ResourceProperty object for
* the resource property matching the specified name or null,
* if no resource property with the specified name can be found.
* @throws InvalidResourceCategoryException If this resource category
* doesn't match the category of the resource object.
*
*/
public function getPropertyObject(string $name)
{
if (!$this->propertyExists($name)) {
if ($name === 'geo_coordinates') {
return null;
}
//A property with the name $name does not exist for this
//resource object. If it is a defined property
//we can still try to create it:
if ($this->category->hasProperty($name)) {
$property = $this->category->createDefinedResourceProperty(
$this,
$name
);
$property->store();
return $property;
} else {
return null;
}
}
return ResourceProperty::findOneBySql(
"INNER JOIN resource_property_definitions rpd
ON resource_properties.property_id = rpd.property_id
WHERE resource_properties.resource_id = :resource_id
AND rpd.name = :name",
[
'resource_id' => $this->id,
'name' => $name
]
);
}
/**
* Returns all info-label properties
*
* @return SimpleCollection
*/
public function getInfolabelProperties()
{
return SimpleCollection::createFromArray(
ResourceProperty::findBySQL('INNER JOIN `resource_property_definitions` USING (`property_id`)
WHERE `info_label` = 1 AND `state` != "" AND `resource_id` = ?', [$this->id]
)
);
}
/**
* Returns the state of the property specified by $name.
* If the property has not been set for this resource, but is defined
* for this resource's category, a new ResourceProperty object
* will be created, stored and its state will be returned.
*
* @param string $name The name of the resource property.
*
* @return string|null The state of the specified property or null
* if the propery can't be found.
*/
public function getProperty(string $name)
{
if (!$this->propertyExists($name)) {
//A property with the name $name does not exist for this
//resource object. If it is a defined property
//we can still try to create it:
if ($this->category->hasProperty($name)) {
$property = $this->category->createDefinedResourceProperty(
$this,
$name,
''
);
$property->store();
return $property->state;
} else {
return null;
}
}
$db = DBManager::get();
$value_stmt = $db->prepare(
"SELECT resource_properties.state FROM resource_properties
INNER JOIN resource_property_definitions rpd
ON resource_properties.property_id = rpd.property_id
WHERE resource_properties.resource_id = :resource_id
AND rpd.name = :name");
$value_stmt->execute(
[
'resource_id' => $this->id,
'name' => $name
]
);
$value = $value_stmt->fetchColumn(0);
if (!$value) {
return null;
}
return $value;
}
/**
* Retrieves an object by the state of a property of this resource,
* specified by the property's name.
* This method is useful for properties of type user, institute
* or fileref. Those properties store IDs of User, Institute
* or FileRef objects. Therefore the IDs can be resolved directly
* to get the corresponding User, Institute or FileRef object directly.
*
* @param string $name The name of the resource property.
*
* @return SimpleORMap|null A SimpleORMap-based object or null,
* if no such object can be retrieved from the property's state.
*/
public function getPropertyRelatedObject(string $name)
{
//Get the property state first:
$property = $this->getPropertyObject($name);
//Now we return the object which is referenced by the property's state:
if ($property) {
switch ($property->definition->type) {
case 'user':
return User::find($property->state);
case 'institute':
return Institute::find($property->state);
case 'fileref' :
return FileRef::find($property->state);
default:
//For all other property types where we cannot create an object
//we return the raw state value:
return $property->state;
}
}
return null;
}
/**
* Sets a specified property of this resource to the specified state.
* If the property has not been set for this resource, but is defined
* for this resource's category, a new ResourceProperty object
* will be created, stored and its state will be returned.
*
* @param string $name The name of the resource property.
* @param mixed $state The state of the resource property.
* @param User|null $user The user who wishes to set the property.
*
* @return bool True, if the property state could be set, false otherwise.
*/
public function setProperty(string $name, $state = '', $user = null)
{
if (!($user instanceof User)) {
$user = User::findCurrent();
if (!$user) {
//We cannot continue without a user object!
return false;
}
}
//Get the minimum permission level required for modifying the property:
if (!$this->userHasPermission($user, 'admin')) {
throw new AccessDeniedException(
sprintf(
_('Unzureichende Berechtigungen zum Ändern der Ressource %s!'),
$this->name
)
);
}
if (!$this->category->userHasPropertyWritePermissions($name, $user, $this)) {
throw new AccessDeniedException(
sprintf(
_('Unzureichende Berechtigungen zum Ändern der Eigenschaft %s!'),
$name
)
);
}
if (!$this->propertyExists($name)) {
//A property with the name $name does not exist for this
//resource object. If it is a defined property
//we can still try to create it:
if ($this->category->hasProperty($name)) {
$property = $this->category->createDefinedResourceProperty(
$this,
$name,
$state
);
return $property->store();
} else {
return false;
}
}
$property = $this->getPropertyObject($name);
if ($property) {
$property->state = $state;
if ($property->isDirty()) {
return $property->store();
}
return true;
}
return false;
}
/**
* Sets the properties (specified by their names) to the specified values.
*
* @param array $properties The properties array in the format "key-value".
* The array keys must contain the property name while the
* items of the array contain the values.
* Example:
* ['bar' => 'foo']: Sets the value 'foo' for the property
* with the name 'bar'.
*
* @param User|null $user The user who wishes to set the properties.
* If this is left empty, the current user will be used.
*
* @return array If properties cannot be set, their names (as key) and the
* error messages (if any) are returned.
* The array has the following structure:
* [
* (property name) => (error message or empty string)
* ]
*/
public function setPropertiesByName(array $properties, User $user)
{
$failed_properties = [];
if (!($user instanceof User)) {
$user = User::findCurrent();
if (!$user) {
//No property can be set.
foreach ($properties as $name => $state) {
$failed_properties[$name] = '';
}
return $failed_properties;
}
}
foreach ($properties as $name => $state) {
try {
$this->setProperty($name, $state, $user);
} catch (Exception $e) {
$this->failed_properties[$name] = $e->getMessage();
}
}
return $failed_properties;
}
/**
* Sets the properties (specified by their IDs) to the specified values.
*
* @param array $properties The properties array in the format "key-value".
* The array keys must contain the property-ID while the
* items of the array contain the values.
* Example:
* ['1' => 'foo']: Sets the value 'foo' for the property
* with the ID '1'.
*
* @param User|null $user The user who wishes to set the properties.
* If this is left empty, the current user will be used.
*
* @return array If properties cannot be set, their ids (as key) and the
* error messages (if any) are returned.
* The array has the following structure:
* [
* (property-ID) => (error message or empty string)
* ]
*/
public function setPropertiesById(array $properties, User $user = null)
{
$failed_properties = [];
if (!($user instanceof User)) {
$user = User::findCurrent();
if (!$user) {
//No property can be set.
foreach ($properties as $id => $state) {
$failed_properties[$id] = '';
}
return $failed_properties;
}
}
foreach ($properties as $id => $state) {
$property = ResourcePropertyDefinition::find($id);
if (!$property) {
//Invalid property:
$this->failed_properties[$id] =
_('Die Eigenschaft wurde nicht gefunden!');
continue;
}
try {
$this->setProperty($property->name, $state, $user);
} catch (Exception $e) {
$failed_properties[$id] = $e->getMessage();
}
}
return $failed_properties;
}
/**
* Determines if the specified user has sufficient permissions to edit
* the property specified by its name.
*
* @param string $name The name of the resource property.
* @param user $user The user whose edit permissions shall be checked.
*
* @return bool True, if the user has edit permissions for the property,
* false otherwise.
*/
public function isPropertyEditable(string $name, User $user)
{
return $this->category->userHasPropertyWritePermissions($name, $user, $this);
}
/**
* Sets the state of a property by its definition_id rather than its name.
*
* @param string $property_definition_id The definition-ID of the property.
* @param string $state The state of the property.
*
* @return bool True, if the property state can be stored, false otherwise.
* @throws ResourcePropertyStateException If the provided state is invalid
* for the specified resource property.
*
*/
public function setPropertyByDefinitionId($property_definition_id = null, $state = null)
{
if (!$property_definition_id and !$state) {
return false;
}
//Get property definition:
$definition = ResourcePropertyDefinition::find($property_definition_id);
if (!$definition) {
return false;
}
//Check if the state matches the property definition's rules:
$definition->validateState($state);
//Check if the property for this resource already exists.
//If so, update it. Otherwise create it.
$property = ResourceProperty::findOneBySql(
'(property_id = :property_id) AND (resource_id = :resource_id)',
[
'property_id' => $definition->id,
'resource_id' => $this->id
]
);
if (!$property) {
$property = new ResourceProperty();
$property->property_id = $definition->id;
$property->resource_id = $this->id;
}
$property->state = $state;
return $property->store();
}
/**
* Sets the property state by specifying an SimpleORMap object.
* This method is meant for resource properties of type user,
* institute or fileref.
*
* @param string $name The name of the resource property.
* @param SimpleORMap $object The object for the resource property.
*
* @return bool True, if the property has been saved, false otherwise.
*/
public function setPropertyRelatedObject(string $name, SimpleORMap $object)
{
//Get the property state first:
$property = $this->getPropertyObject($name);
if (!$property) {
return false;
}
//Now we return the object which is referenced by the property's state:
switch ($property->definition->type) {
case 'user':
if (!($object instanceof User)) {
throw new ResourcePropertyException(
_("Eine Ressourceneigenschaft vom Typ 'user' benötigt ein Nutzer-Objekt zur Wertzuweisung!")
);
}
break;
case 'institute':
if (!($object instanceof Institute)) {
throw new ResourcePropertyException(
_("Eine Ressourceneigenschaft vom Typ 'institute' benötigt ein Institut-Objekt zur Wertzuweisung!")
);
}
break;
case 'fileref':
if (!($object instanceof FileRef)) {
throw new ResourcePropertyException(
_("Eine Ressourceneigenschaft vom Typ 'fileref' benötigt ein FileRef-Objekt zur Wertzuweisung!")
);
}
break;
default:
break;
}
//When no exception is thrown above we can set the object's ID
//as the property's state:
$property->state = $object->id;
return $property->store();
}
/**
* Deletes a property for a resource.
*
* @param string $name The name of the property to be deleted.
*
* @param User $user The user who wishes to delete the property.
* @return number
*/
public function deleteProperty(string $name, User $user)
{
//Get the user object and the minimum permission level
//required for modifying the property:
if (!$this->userHasPermission($user, 'admin')) {
throw new AccessDeniedException(
sprintf(
_('Unzureichende Berechtigungen zum Ändern der Ressource %s!'),
$this->name
)
);
}
if (!$this->category->userHasPropertyWritePermissions($name, $user)) {
throw new AccessDeniedException(
sprintf(
_('Unzureichende Berechtigungen zum Löschen der Eigenschaft %s!'),
$name
)
);
}
return ResourceProperty::deleteBySql(
"INNER JOIN resource_property_definitions rpd
ON resource_properties.property_id = rpd.property_id
WHERE
rpd.name = :name AND resource_properties.resource_id = :resource_id",
[
'name' => $name,
'resource_id' => $this->id
]
);
}
/**
* Returns the path for the resource's image.
* If the resource has no image the path for a general
* resource icon will be returned.
*
* Classes derived from the Resource class should only re-implement
* this method if they have an alternative storage method for
* resource pictures than the Stud.IP file system.
*
* @return string The URL to the resource picture.
*/
public function getPictureUrl()
{
return '';
}
/**
* Returns the default picture for the resource class.
*
* Classes derived from Resource should re-implement this method
* if they want to get a different default picture than the resource icon.
* The call to getPictureUrl will call the getDefaultPictureUrl method
* from the derived class.
*
* @return string The URL to the picture.
*/
public function getDefaultPictureUrl()
{
return $this->getIcon()->asImagePath();
}
/**
* Returns the Icon for the resource class.
*
* Classes derived from Resource should re-implement this method
* if they want to get a different icon than the resource icon.
* @param string $role
* @return Icon The icon for the resource.
*/
public function getIcon($role = Icon::ROLE_INFO)
{
return Icon::create('resources', $role);
}
/**
* Returns all properties in a two-dimensional array with the following
* property data inside of the second dimension:
* [
* 'name' => (the property's name)
* 'display_name' => (the display name of the property)
* 'type' => (the property's type)
* 'state' => (the property's state)
* 'requestable' => (if the property is requestable or not (true or false))
* ]
*
* @param bool $only_requestable_properties If only requestable properties
* shall be returned set this to true. If all properties shall be
* returned, set this to false.
*
* @return array[] A two-dimensional array containing property data.
*/
public function getPropertyArray($only_requestable_properties = false)
{
$property_array = [];
if ($this->properties) {
foreach ($this->properties as $property) {
if ($only_requestable_properties) {
$category_property = ResourceCategoryProperty::findByNameAndCategoryId(
$property->name,
$this->category_id
);
if ($category_property) {
if ($category_property->requestable) {
$property_array[] = [
'name' => $property->name,
'display_name' => $property->display_name,
'type' => $property->type,
'state' => $property->state,
'requestable' => $property->isRequestable()
];
}
}
} else {
$property_array[] = [
'name' => $property->name,
'display_name' => $property->display_name,
'type' => $property->type,
'state' => $property->state,
'requestable' => $property->isRequestable()
];
}
}
}
return $property_array;
}
/**
* Shortcut method for ResourceBooking::countByResourceAndTimeRanges.
* Determines whether normal resource bookings exist
* in the specified time range.
*
* @param DateTime $begin Time range start timestamp.
*
* @param DateTime $end Time range end timestamp.
*
* @param array $excluded_booking_ids The IDs of bookings that shall
* be excluded from the determination of the "assigned" status.
*
* @return bool True, if the resource is assigned in the specified
* time range, false otherwise.
*/
public function isAssigned(
DateTime $begin,
DateTime $end,
$excluded_booking_ids = []
)
{
return ResourceBooking::countByResourceAndTimeRanges(
$this,
[
[
'begin' => $begin->getTimestamp(),
'end' => $end->getTimestamp()
]
],
[0],
$excluded_booking_ids
) > 0;
}
/**
* Shortcut method for ResourceBooking::countByResourceAndTimeRanges.
* Determines whether resource reservations exist
* in the specified time range.
*
* @param DateTime $begin Time range start timestamp.
*
* @param DateTime $end Time range end timestamp.
*
* @param array $excluded_reservation_ids The IDs of reservation bookings that shall
* be excluded from the determination of the "reserved" status.
*
* @return bool True, if the resource is reserved in the specified
* time range, false otherwise.
*/
public function isReserved(
DateTime $begin,
DateTime $end,
$excluded_reservation_ids = []
)
{
//One second is added to the begin timestamp to avoid
//getting "false" overlaps where another booking ends on exactly
//the begin timestamp.
return ResourceBooking::countByResourceAndTimeRanges(
$this,
[
[
'begin' => $begin->getTimestamp(),
'end' => $end->getTimestamp()
]
],
[1, 3],
$excluded_reservation_ids
) > 0;
}
/**
* Shortcut method for ResourceBooking::countByResourceAndTimeRanges.
* Determines whether resource locks exist
* in the specified time range.
*
* @param DateTime $begin Time range start timestamp.
*
* @param DateTime $end Time range end timestamp.
*
* @param array $excluded_lock_ids The IDs of lock bookings that shall
* be excluded from the determination of the "locked" status.
*
* @return bool True, if the resource is locked in the specified
* time range, false otherwise.
*/
public function isLocked(
DateTime $begin,
DateTime $end,
$excluded_lock_ids = []
)
{
//One second is added to the begin timestamp to avoid
//getting "false" overlaps where another booking ends on exactly
//the begin timestamp.
return ResourceBooking::countByResourceAndTimeRanges(
$this,
[
[
'begin' => $begin->getTimestamp(),
'end' => $end->getTimestamp()
]
],
[2],
$excluded_lock_ids
) > 0;
}
/**
* Determines, if the resource is available (not assigned or locked)
* in a specified time range.
*
* @param DateTime $begin Time range start timestamp.
* @param DateTime $end Time range end timestamp.
*
* @param array $excluded_booking_ids The IDs of available bookings that shall
* be excluded from the determination of the "available" status.
*
* @return bool True, if the resource is available in the specified
* time range, false otherwise.
*/
public function isAvailable(
DateTime $begin,
DateTime $end,
$excluded_booking_ids = []
)
{
return ResourceBooking::countByResourceAndTimeRanges(
$this,
[
[
'begin' => $begin->getTimestamp(),
'end' => $end->getTimestamp()
]
],
[0, 2],
$excluded_booking_ids
) == 0;
}
/**
* Determines, if the resource is available (not assigned or locked)
* in the time ranges specified by a resource request.
*
* @param ResourceRequest $request A resource request object.
*
* @return bool True, if the resource is available in the
* time ranges of the resource request, false otherwise.
*/
public function isAvailableForRequest(ResourceRequest $request)
{
$time_intervals = $request->getTimeIntervals(true);
if (!$time_intervals) {
//Without a single time interval we cannot check
//if the resource is available.
return false;
}
foreach ($time_intervals as $time_interval) {
$begin = new DateTime();
$end = new DateTime();
$begin->setTimestamp($time_interval['begin']);
$end->setTimestamp($time_interval['end']);
if (!$this->isAvailable($begin, $end)) {
//The resource is not available in the time interval.
//We can stop here and return false.
return false;
}
//If code execution reaches this point the resource is
//available in all time intervals of the resource request:
return true;
}
}
/**
* Returns the full (localised) name of the resource.
*
* @return string The full name of the resource.
*/
public function getFullName()
{
return sprintf(
_('Ressource %s'),
$this->name
);
}
/**
* Sets the permission for one user for this resource.
*
* @param User $user The user whose permission shall be set.
* @param string $perm The permission level for the specified user.
* The levels 'user', 'autor', 'tutor' and 'admin' are allowed.
*
* @return bool True, if the permission has been stored successfully,
* false otherwise.
*/
public function setUserPermission(User $user, $perm = 'autor')
{
if (!in_array($perm, ['user', 'autor', 'tutor', 'admin'])) {
return false;
}
$perm_object = ResourcePermission::findOneBySql(
'(user_id = :user_id) AND (resource_id = :resource_id)',
[
'user_id' => $user->id,
'resource_id' => $this->id
]
);
if (!$perm_object) {
$perm_object = new ResourcePermission();
$perm_object->user_id = $user->id;
$perm_object->resource_id = $this->id;
}
$perm_object->perms = $perm;
$stored = (bool)$perm_object->store();
if ($stored) {
if (!isset(self::$permission_cache[$this->id])) {
self::$permission_cache[$this->id] = [];
}
//Update the permission cache.
self::$permission_cache[$this->id][$user->id] = $perm;
}
return $stored;
}
/**
* Deletes the permission a specified user has on this resource.
*
* @param User $user The user whose permission shall be deleted.
*
* @return bool True
*/
public function deleteUserPermission(User $user)
{
$deleted = ResourcePermission::deleteBySql(
'(user_id = :user_id) AND (resource_id = :resource_id)',
[
'user_id' => $user->id,
'resource_id' => $this->id
]
);
if ($deleted && is_array(self::$permission_cache[$this->id])) {
//Update the permission cache.
self::$permission_cache[$this->id][$user->id] = null;
}
return true;
}
/**
* Deletes all permissions of all users for this resource.
*
* @return bool True
*/
public function deleteAllPermissions()
{
ResourcePermission::deleteBySql(
'resource_id = :resource_id',
[
'resource_id' => $this->id
]
);
//Update the permission cache:
self::$permission_cache[$this->id] = [];
return true;
}
/**
* Retrieves the permission level a specified user
* has on this resource.
*
* Setting the optional $time_range parameter will also enable checks for
* temporary global permissions.
*
* @param User $user The user whose permission shall be retrieved.
*
* @param array $time_range (DateTime) This is an optional parameter that can
* be used to pass two DateTime objects to this method. The first object
* will be treated as the begin timestamp and the second one as the
* end timestamp.
*
* @param bool $permanent_only Whether to retrieve only permanent permissions
* (true) or permanent and temporary permissions (false).
* Defaults to false.
*
* @return string The permission level, expressed as string.
* The level can be 'user', 'autor', 'tutor' or 'admin'.
*/
public function getUserPermission(User $user, $time_range = [], $permanent_only = false)
{
if (ResourceManager::getGlobalResourcePermission($user) === 'admin') {
return 'admin';
}
$perm_string = '';
$temp_perm = null;
$begin = time();
$end = $begin;
//Check for a temporary permission first:
//check only against current timestamp
if (!$permanent_only) {
$temp_perm = ResourceTemporaryPermission::findOneBySql(
'(resource_id = :resource_id) AND (user_id = :user_id)
AND (begin <= :begin) AND (end >= :end)',
[
'resource_id' => $this->id,
'user_id' => $user->id,
'begin' => $begin,
'end' => $end
]
);
}
if ($temp_perm) {
$perm_string = $temp_perm->perms;
} else {
//No temporary permission exist or has been retrieved.
//Check for a "normal" permission.
$cached_perms = self::$permission_cache[$this->id][$user->id] ?? null;
if ($cached_perms === null) {
//The permission of the specified user is not in the
//permission cache. Load it from the database and store
//it in the permission cache before returning it.
$perms = ResourcePermission::findOneBySql(
'(resource_id = :resource_id) AND (user_id = :user_id)',
[
'resource_id' => $this->id,
'user_id' => $user->id
]
);
if ($perms) {
if (!isset(self::$permission_cache[$this->id])) {
self::$permission_cache[$this->id] = [];
}
self::$permission_cache[$this->id][$user->id] = $perms->perms;
$perm_string = $perms->perms;
}
} else {
$perm_string = $cached_perms;
}
}
if (!$perm_string) {
//A user which doesn't have special permissions for this resource
//can have global resource permissions:
$global_perm = ResourceManager::getGlobalResourcePermission($user);
if ($global_perm) {
//Set the permission cache:
if (!isset(self::$permission_cache[$this->id])) {
self::$permission_cache[$this->id] = [];
}
self::$permission_cache[$this->id][$user->id] = $global_perm;
}
$perm_string = $global_perm;
}
//Now we must check for global resource locks:
if ($perm_string && $time_range && $this->lockable) {
if ($time_range[0] instanceof DateTime) {
$begin = $time_range[0]->getTimestamp();
} else {
$begin = $time_range[0];
}
if ($time_range[1] instanceof DateTime) {
$end = $time_range[1]->getTimestamp();
} else {
$end = $time_range[1];
}
if (GlobalResourceLock::isLocked($begin, $end)) {
//A permission level exists for the user.
//The user gets "user" permissions in case
//a global lock is active.
$perm_string = 'user';
}
}
//No global resource lock exists. We must return
//the permission string if it is set:
if ($perm_string) {
return $perm_string;
}
return '';
}
/**
* Determines if a user has the specified permission.
*
* @param ?User $user The user whose permissions shall be checked on this
* resource object. May be null.
* @param string $permission The permission level.
* @param $time_range @TODO
*
* @return bool True, if the specified user has the specified permission,
* false otherwise.
*/
public function userHasPermission(
?User $user,
string $permission = 'user',
array $time_range = []
)
{
if (!in_array($permission, ['user', 'autor', 'tutor', 'admin']) || $user === null) {
return false;
}
if (ResourceManager::getGlobalResourcePermission($user) === 'admin') {
return true;
}
$perm_level = $this->getUserPermission($user, $time_range);
if ($permission === 'user') {
//No check for global resource locks here:
//If only user permissions are requested we can safely grant them
//since 'user' users may only perform reading actions but
//no writing actions.
if (in_array($perm_level, ['user', 'autor', 'tutor', 'admin'])) {
return true;
} else {
return false;
}
} elseif ($permission === 'autor') {
if (in_array($perm_level, ['autor', 'tutor', 'admin'])) {
return true;
} else {
return false;
}
} elseif ($permission === 'tutor') {
if (in_array($perm_level, ['tutor', 'admin'])) {
return true;
} else {
return false;
}
} elseif ($permission === 'admin') {
if ($perm_level == 'admin') {
return true;
} else {
return false;
}
}
//Code execution should be finished at this point.
//If this point is reached the user has no permissions for the
//resource management system at all.
return false;
}
/**
* Determines whether the user may create a child resource
* on this resource.
*
* @param User $user The user whose permission to create a child
* resource shall be checked.
*
* @return bool True, if the user may create a child resource
* on this resource, false otherwise.
*/
public function userMayCreateChild(User $user)
{
return $this->userHasPermission($user, 'admin');
}
/**
* Checks if the specified user has sufficient permissions to make resource
* requests, according to the setting RESOURCES_MIN_REQUEST_PERMISSION.
* This permission check is only relevant for creating requests that are not
* bound to a course.
*
* @param User $user The user whose request permissions shall be checked.
*
* @return bool True, if the user has request permissions, false otherwise.
*/
public function userHasRequestRights(User $user)
{
if (!Config::get()->RESOURCES_ALLOW_ROOM_REQUESTS || !$this->booking_plan_request) {
return false;
}
$min_perm = Config::get()->RESOURCES_MIN_REQUEST_PERMISSION;
if (!in_array($min_perm, ['', 'user', 'autor', 'tutor', 'admin'])) {
//Invalid permission level!
return false;
}
if (!$min_perm) {
//No minimum permission set: Every logged-in user
//can create requests.
return true;
}
return $this->userHasPermission($user, $min_perm);
}
/**
* Determines whether the user may book the resource or not.
* An optional time range can be set to check the user's
* temporary permissions on another date than the current date.
*
* @param User $user The user whose booking permissions shall be checked.
*
* @param int|string|DateTime $begin The begin timestamp of the
* optional time range.
*
* @param int|string|DateTime $end The end timestamp of the
* optional time range.
*
* @return bool True, if the user may book the resource, false otherwise.
*/
public function userHasBookingRights(
User $user,
$begin = null,
$end = null
)
{
if ($begin && $end) {
$time_range = [$begin, $end];
} else {
$time_range = [];
}
//Check the permissions on this resource and the global permissions:
return $this->userHasPermission($user, 'autor', $time_range);
}
/**
* Determines if the booking plan of the resource is visible for a
* specified user.
*
* @param ?User $user The user whose permission to view the booking plan
* shall be determined. May be null.
*
* @param DateTime[] $time_range An optional time range for the
* permission check.
* @return bool True, if the user can see the resource booking plan,
* false otherwise.
* @see Resource::getUserPermission
*
*/
public function bookingPlanVisibleForUser(?User $user, $time_range = [])
{
return $this->userHasPermission($user, 'user', $time_range);
}
/**
* Retrieves a parent resource object that matches the specified
* class name. The search stops when either a parent resource
* with the class name is found or when the root resource object
* is reached.
*
* @param string $class_name The class name of the parent.
*
* @return Resource|null Either a resource object or null
* in case a matching parent resource cannot be found.
*/
public function findParentByClassName($class_name = 'Resource')
{
$resource_ids = [$this->id];
$resource = $this->parent;
while ($resource) {
//We should check for circular hierarchies first
//to avoid an endless while loop:
if (in_array($resource->id, $resource_ids)) {
//We have a circular hierarchy: this resource is
//the parent of itself which is an invalid state!
throw new InvalidResourceException(
sprintf(
_('Zirkuläre Hierarchie: Die Ressource %1$s ist ein Elternknoten von sich selbst!'),
$resource->name
)
);
}
if (is_a($resource->class_name, $class_name, true)) {
//We have found a parent node which has the
//specified class name: return that parent.
return $resource;
}
//The current parent was not the one we were looking for.
//Therefore we must go one layer up in the resource
//hierarchy and continue search:
$resource_ids[] = $resource->id;
$resource = $resource->parent;
}
//The search was not successful:
//We have reached the root resource (whose parent_id field
//is set to an equivalend of NULL) and we haven't found a
//resource matching the specified class name.
return null;
}
/**
* This method searches the hierarchy below this resource
* to find resources matching the specified class name.
* Via the optional parameter $depth the search can be limited
* to a specific amount of layers.
*
* @param string $class_name The name of the resource class
* where resources shall be found to.
* @param int $depth The (optional) maximum depth below this resource
* which shall be searched.
* @param bool $convert_objects True, if objects shall be converted to
* $class_name (default), false otherwise.
* @param bool $order_by_name Order the children by name.
* Defaults to true.
*
* @return Resource[] An array of resource objects or an empty array
* if no matching resources can be found.
*/
public function findChildrenByClassName(
$class_name = 'Resource',
$depth = 0,
$convert_objects = true,
$order_by_name = true
)
{
$result = [];
if ($this->children) {
//this resource has children: iterate over them and
//check if they match the search criteria.
foreach ($this->children as $child) {
if (is_a($child->class_name, $class_name, true)) {
if ($convert_objects) {
$result[] = $child->getDerivedClassInstance();
} else {
$result[] = $child;
}
}
if (($depth > 1) || ($depth == 0)) {
//Search the child and lower depth by one when calling this
//method on the child.
$result = array_merge(
$result,
$child->findChildrenByClassName(
$class_name,
(($depth > 1) ? $depth - 1 : 0),
$convert_objects
)
);
}
}
if ($order_by_name) {
usort(
$result,
function ($a, $b) {
if ($a->name == $b->name) {
return 0;
} elseif ($a->name < $b->name) {
return -1;
} else {
return 1;
}
}
);
}
}
return $result;
}
/**
* Adds a resource as child resource to this resource.
*
* @param Resource $resource The child resource.
*
* @return bool True on success, false on failure.
*/
public function addChild(Resource $resource)
{
$old_parent = $resource->parent;
$old_parent_id = $resource->parent_id;
$resource->parent = $this;
$resource->parent_id = $this->id;
if (!$resource->checkHierarchy()) {
//We must revert the parent fields since $resource
//may be used in other code pieces afterwards.
$resource->parent = $old_parent;
$resource->parent_id = $old_parent_id;
throw new InvalidArgumentException(
sprintf(
_('Die Ressource %1$s (Typ %2$s) kann nicht unterhalb der Ressource %3$s (Typ %4$s) platziert werden!'),
$resource->name,
$resource->class_name,
$this->name,
$this->class_name
)
);
}
if ($resource->isDirty()) {
//Only store the resource object if setting the parent_id field
//did change it:
return $resource->store();
}
//The resource object hasn't changed by setting the parent_id field:
//We can return true.
return true;
}
/**
* Get all resource requests for the resource in a given timeframe.
*
* @param DateTime $begin Begin of timeframe.
* @param DateTime $end End of timeframe.
*
* @return ResourceRequest[] An array of ResourceRequest objects.
*/
public function getOpenResourceRequests(DateTime $begin, DateTime $end)
{
//We must get all requests that either have a start and end date
//set or that have a start date, repeate end, repeat interval and
//repeat quantity set.
return ResourceRequest::findByResourceAndTimeRanges(
$this,
[
[
'begin' => $begin->getTimestamp(),
'end' => $end->getTimestamp()
]
],
0
);
}
/**
* Get all resource bookings for the resource in a given timeframe.
*
* @param DateTime $begin Begin of timeframe.
* @param DateTime $end End of timeframe.
* @param array $booking_types
*
* @return ResourceBooking[] An array of ResourceBooking objects.
*/
public function getResourceBookings(DateTime $begin, DateTime $end, array $booking_types = [0])
{
return ResourceBooking::findByResourceAndTimeRanges(
$this,
[
[
'begin' => $begin->getTimestamp(),
'end' => $end->getTimestamp()
]
],
$booking_types
);
}
/**
* Get all resource locks for the resource in a given timeframe.
*
* @param DateTime $begin Begin of timeframe.
* @param DateTime $end End of timeframe.
*
* @return ResourceBooking[] An array of ResourceBooking objects.
*/
public function getResourceLocks(DateTime $begin, DateTime $end)
{
return ResourceBooking::findByResourceAndTimeRanges(
$this,
[
[
$begin->getTimestamp(),
$end->getTimestamp()
]
],
[2]
);
}
/**
* Determines if files are attached to this resource.
* If a folder exists for this resource its files are counted.
* Depending on whether the folder has files in it or not
* this method returns true or false.
*
* @return bool True, if there are files attached to this resource,
* false otherwise.
*/
public function hasFiles()
{
$folder = Folder::findOneBySql(
'range_id = :range_id',
[
'range_id' => $this->id
]
);
if (!$folder) {
return false;
}
//Since files from resources shall always be stored in the
//Stud.IP file system we can skip the conversion from Folder
//to FolderType and count the FileRef-objects for this resource
//directly in the database. Since resource folders do not
//have subfolders we will count any file of the resource:
return FileRef::countBySql(
'folder_id = :folder_id',
[
'folder_id' => $folder->id
]
) > 0;
}
/**
* Converts a Resource object to an object of a specialised resource class.
*
* @return Resource An object of a specialised resource class
* or a Resource object, if the resource is a standard resource
* with the class_name 'Resource' in its resource category.
* If the derived resource class is not available, an instance of
* BrokenResource is returned.
*/
public function getDerivedClassInstance()
{
$class_name = $this->class_name;
if ($class_name == 'Resource') {
//It is a standard resource which is managed by this class.
return $this;
}
if (is_subclass_of($class_name, 'Resource')) {
$converted_resource = $class_name::buildExisting(
$this->toRawArray()
);
return $converted_resource;
} else {
//$class_name does not contain the name of a subclass
//of Resource. That's an error!
$broken_resource = BrokenResource::buildExisting(
$this->toRawArray()
);
return $broken_resource;
}
}
/**
* Checks if the place in the resource hierarchy (resource tree)
* is correct for this resource.
* This method has no function in this class but can be filled
* with logic in one of the classes derived from Resource.
*
* @return bool True, if this resource is correctly placed,
* false otherwise.
* @throws NoResourceClassException
* if the class name of this resource is not a derived class
* of the Resource class.
*
*/
public function checkHierarchy()
{
if ($this->class_name == 'Resource') {
//Objects of the Resource class are always in the right
//place of the resource hierarchy.
return true;
}
//The object does not use the Resource class name and uses
//a derived class instead. We must check the hierarchy
//using the checkHierarchy method of the derived class.
$converted_resource = $this->getDerivedClassInstance();
return $converted_resource->checkHierarchy();
}
/**
* Returns the link for an action for this resource.
* This is the non-static variant of Resource::getLinkForAction.
*
* @param string $action The action which shall be executed.
* For default Resources the actions 'show', 'add', 'edit' and 'delete'
* are defined.
* @param array $link_parameters Optional parameters for the link.
* @return string @TODO
*/
public function getActionLink($action = 'show', $link_parameters = [])
{
//We must check the class name and call the appropriate
//getLinkForAction method for derived classes:
$class_name = $this->class_name;
if (is_subclass_of($class_name, 'Resource')) {
return $class_name::getLinkForAction(
$action,
$this->id,
$link_parameters
);
} else {
return self::getLinkForAction(
$action,
$this->id,
$link_parameters
);
}
}
/**
* Returns the URL for an action for this resource.
* This is the non-static variant of Resource::getURLForAction.
*
* @param string $action The action which shall be executed.
* For default Resources the actions 'show', 'add', 'edit' and 'delete'
* are defined.
* @param array $url_parameters Optional parameters for the URL.
* @return string @TODO
*/
public function getActionURL($action = 'show', $url_parameters = [])
{
//We must check the class name and call the appropriate
//getURLForAction method for derived classes:
$class_name = $this->class_name;
if (is_subclass_of($class_name, 'Resource')) {
return $class_name::getURLForAction(
$action,
$this->id,
$url_parameters
);
} else {
return self::getURLForAction(
$action,
$this->id,
$url_parameters
);
}
}
public function getItemName($long_format = true)
{
if ($long_format) {
//In some cases the general Resource class may be used
//when the resource objects are in fact instances
//of derived classes. To make sure that the correct prefix
//is always displayed, we retrieve the derived class first
//before returning the name:
$derived_class = $this->getDerivedClassInstance();
return $derived_class->getFullName();
} else {
return $this->name;
}
}
public function getItemURL()
{
return $this->getActionURL('show');
}
public function getItemAvatarURL()
{
return Icon::create('resources', Icon::ROLE_INFO)->asImagePath();
}
public function getLink() : StudipLink
{
return new StudipLink($this->getActionURL(), $this->name, Icon::create('resources'));
}
}
|