campusconnect /repository/lib.php

Language PHP Lines 3318
MD5 Hash 9eed1caff850c24826e808293ae2d74c Estimated Cost $52,626 (why?)
Repository https://bitbucket.org/synergylearning/campusconnect.git View Raw File View Project SPDX
   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
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This file contains classes used to manage the repository plugins in Moodle
 *
 * @since 2.0
 * @package   core_repository
 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->libdir . '/formslib.php');

define('FILE_EXTERNAL',  1);
define('FILE_INTERNAL',  2);
define('FILE_REFERENCE', 4);
define('RENAME_SUFFIX', '_2');

/**
 * This class is used to manage repository plugins
 *
 * A repository_type is a repository plug-in. It can be Box.net, Flick-r, ...
 * A repository type can be edited, sorted and hidden. It is mandatory for an
 * administrator to create a repository type in order to be able to create
 * some instances of this type.
 * Coding note:
 * - a repository_type object is mapped to the "repository" database table
 * - "typename" attibut maps the "type" database field. It is unique.
 * - general "options" for a repository type are saved in the config_plugin table
 * - when you delete a repository, all instances are deleted, and general
 *   options are also deleted from database
 * - When you create a type for a plugin that can't have multiple instances, a
 *   instance is automatically created.
 *
 * @package   core_repository
 * @copyright 2009 Jerome Mouneyrac
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class repository_type implements cacheable_object {


    /**
     * Type name (no whitespace) - A type name is unique
     * Note: for a user-friendly type name see get_readablename()
     * @var String
     */
    private $_typename;


    /**
     * Options of this type
     * They are general options that any instance of this type would share
     * e.g. API key
     * These options are saved in config_plugin table
     * @var array
     */
    private $_options;


    /**
     * Is the repository type visible or hidden
     * If false (hidden): no instances can be created, edited, deleted, showned , used...
     * @var boolean
     */
    private $_visible;


    /**
     * 0 => not ordered, 1 => first position, 2 => second position...
     * A not order type would appear in first position (should never happened)
     * @var integer
     */
    private $_sortorder;

    /**
     * Return if the instance is visible in a context
     *
     * @todo check if the context visibility has been overwritten by the plugin creator
     *       (need to create special functions to be overvwritten in repository class)
     * @param stdClass $context context
     * @return bool
     */
    public function get_contextvisibility($context) {
        global $USER;

        if ($context->contextlevel == CONTEXT_COURSE) {
            return $this->_options['enablecourseinstances'];
        }

        if ($context->contextlevel == CONTEXT_USER) {
            return $this->_options['enableuserinstances'];
        }

        //the context is SITE
        return true;
    }



    /**
     * repository_type constructor
     *
     * @param int $typename
     * @param array $typeoptions
     * @param bool $visible
     * @param int $sortorder (don't really need set, it will be during create() call)
     */
    public function __construct($typename = '', $typeoptions = array(), $visible = true, $sortorder = 0) {
        global $CFG;

        //set type attributs
        $this->_typename = $typename;
        $this->_visible = $visible;
        $this->_sortorder = $sortorder;

        //set options attribut
        $this->_options = array();
        $options = repository::static_function($typename, 'get_type_option_names');
        //check that the type can be setup
        if (!empty($options)) {
            //set the type options
            foreach ($options as $config) {
                if (array_key_exists($config, $typeoptions)) {
                    $this->_options[$config] = $typeoptions[$config];
                }
            }
        }

        //retrieve visibility from option
        if (array_key_exists('enablecourseinstances',$typeoptions)) {
            $this->_options['enablecourseinstances'] = $typeoptions['enablecourseinstances'];
        } else {
             $this->_options['enablecourseinstances'] = 0;
        }

        if (array_key_exists('enableuserinstances',$typeoptions)) {
            $this->_options['enableuserinstances'] = $typeoptions['enableuserinstances'];
        } else {
             $this->_options['enableuserinstances'] = 0;
        }

    }

    /**
     * Get the type name (no whitespace)
     * For a human readable name, use get_readablename()
     *
     * @return string the type name
     */
    public function get_typename() {
        return $this->_typename;
    }

    /**
     * Return a human readable and user-friendly type name
     *
     * @return string user-friendly type name
     */
    public function get_readablename() {
        return get_string('pluginname','repository_'.$this->_typename);
    }

    /**
     * Return general options
     *
     * @return array the general options
     */
    public function get_options() {
        return $this->_options;
    }

    /**
     * Return visibility
     *
     * @return bool
     */
    public function get_visible() {
        return $this->_visible;
    }

    /**
     * Return order / position of display in the file picker
     *
     * @return int
     */
    public function get_sortorder() {
        return $this->_sortorder;
    }

    /**
     * Create a repository type (the type name must not already exist)
     * @param bool $silent throw exception?
     * @return mixed return int if create successfully, return false if
     */
    public function create($silent = false) {
        global $DB;

        //check that $type has been set
        $timmedtype = trim($this->_typename);
        if (empty($timmedtype)) {
            throw new repository_exception('emptytype', 'repository');
        }

        //set sortorder as the last position in the list
        if (!isset($this->_sortorder) || $this->_sortorder == 0 ) {
            $sql = "SELECT MAX(sortorder) FROM {repository}";
            $this->_sortorder = 1 + $DB->get_field_sql($sql);
        }

        //only create a new type if it doesn't already exist
        $existingtype = $DB->get_record('repository', array('type'=>$this->_typename));
        if (!$existingtype) {
            //create the type
            $newtype = new stdClass();
            $newtype->type = $this->_typename;
            $newtype->visible = $this->_visible;
            $newtype->sortorder = $this->_sortorder;
            $plugin_id = $DB->insert_record('repository', $newtype);
            //save the options in DB
            $this->update_options();

            $instanceoptionnames = repository::static_function($this->_typename, 'get_instance_option_names');

            //if the plugin type has no multiple instance (e.g. has no instance option name) so it wont
            //be possible for the administrator to create a instance
            //in this case we need to create an instance
            if (empty($instanceoptionnames)) {
                $instanceoptions = array();
                if (empty($this->_options['pluginname'])) {
                    // when moodle trying to install some repo plugin automatically
                    // this option will be empty, get it from language string when display
                    $instanceoptions['name'] = '';
                } else {
                    // when admin trying to add a plugin manually, he will type a name
                    // for it
                    $instanceoptions['name'] = $this->_options['pluginname'];
                }
                repository::static_function($this->_typename, 'create', $this->_typename, 0, context_system::instance(), $instanceoptions);
            }
            //run plugin_init function
            if (!repository::static_function($this->_typename, 'plugin_init')) {
                $this->update_visibility(false);
                if (!$silent) {
                    throw new repository_exception('cannotinitplugin', 'repository');
                }
            }

            cache::make('core', 'repositories')->purge();
            if(!empty($plugin_id)) {
                // return plugin_id if create successfully
                return $plugin_id;
            } else {
                return false;
            }

        } else {
            if (!$silent) {
                throw new repository_exception('existingrepository', 'repository');
            }
            // If plugin existed, return false, tell caller no new plugins were created.
            return false;
        }
    }


    /**
     * Update plugin options into the config_plugin table
     *
     * @param array $options
     * @return bool
     */
    public function update_options($options = null) {
        global $DB;
        $classname = 'repository_' . $this->_typename;
        $instanceoptions = repository::static_function($this->_typename, 'get_instance_option_names');
        if (empty($instanceoptions)) {
            // update repository instance name if this plugin type doesn't have muliti instances
            $params = array();
            $params['type'] = $this->_typename;
            $instances = repository::get_instances($params);
            $instance = array_pop($instances);
            if ($instance) {
                $DB->set_field('repository_instances', 'name', $options['pluginname'], array('id'=>$instance->id));
            }
            unset($options['pluginname']);
        }

        if (!empty($options)) {
            $this->_options = $options;
        }

        foreach ($this->_options as $name => $value) {
            set_config($name, $value, $this->_typename);
        }

        cache::make('core', 'repositories')->purge();
        return true;
    }

    /**
     * Update visible database field with the value given as parameter
     * or with the visible value of this object
     * This function is private.
     * For public access, have a look to switch_and_update_visibility()
     *
     * @param bool $visible
     * @return bool
     */
    private function update_visible($visible = null) {
        global $DB;

        if (!empty($visible)) {
            $this->_visible = $visible;
        }
        else if (!isset($this->_visible)) {
            throw new repository_exception('updateemptyvisible', 'repository');
        }

        cache::make('core', 'repositories')->purge();
        return $DB->set_field('repository', 'visible', $this->_visible, array('type'=>$this->_typename));
    }

    /**
     * Update database sortorder field with the value given as parameter
     * or with the sortorder value of this object
     * This function is private.
     * For public access, have a look to move_order()
     *
     * @param int $sortorder
     * @return bool
     */
    private function update_sortorder($sortorder = null) {
        global $DB;

        if (!empty($sortorder) && $sortorder!=0) {
            $this->_sortorder = $sortorder;
        }
        //if sortorder is not set, we set it as the ;ast position in the list
        else if (!isset($this->_sortorder) || $this->_sortorder == 0 ) {
            $sql = "SELECT MAX(sortorder) FROM {repository}";
            $this->_sortorder = 1 + $DB->get_field_sql($sql);
        }

        cache::make('core', 'repositories')->purge();
        return $DB->set_field('repository', 'sortorder', $this->_sortorder, array('type'=>$this->_typename));
    }

    /**
     * Change order of the type with its adjacent upper or downer type
     * (database fields are updated)
     * Algorithm details:
     * 1. retrieve all types in an array. This array is sorted by sortorder,
     * and the array keys start from 0 to X (incremented by 1)
     * 2. switch sortorder values of this type and its adjacent type
     *
     * @param string $move "up" or "down"
     */
    public function move_order($move) {
        global $DB;

        $types = repository::get_types();    // retrieve all types

        // retrieve this type into the returned array
        $i = 0;
        while (!isset($indice) && $i<count($types)) {
            if ($types[$i]->get_typename() == $this->_typename) {
                $indice = $i;
            }
            $i++;
        }

        // retrieve adjacent indice
        switch ($move) {
            case "up":
                $adjacentindice = $indice - 1;
            break;
            case "down":
                $adjacentindice = $indice + 1;
            break;
            default:
            throw new repository_exception('movenotdefined', 'repository');
        }

        //switch sortorder of this type and the adjacent type
        //TODO: we could reset sortorder for all types. This is not as good in performance term, but
        //that prevent from wrong behaviour on a screwed database. As performance are not important in this particular case
        //it worth to change the algo.
        if ($adjacentindice>=0 && !empty($types[$adjacentindice])) {
            $DB->set_field('repository', 'sortorder', $this->_sortorder, array('type'=>$types[$adjacentindice]->get_typename()));
            $this->update_sortorder($types[$adjacentindice]->get_sortorder());
        }
    }

    /**
     * 1. Change visibility to the value chosen
     * 2. Update the type
     *
     * @param bool $visible
     * @return bool
     */
    public function update_visibility($visible = null) {
        if (is_bool($visible)) {
            $this->_visible = $visible;
        } else {
            $this->_visible = !$this->_visible;
        }
        return $this->update_visible();
    }


    /**
     * Delete a repository_type (general options are removed from config_plugin
     * table, and all instances are deleted)
     *
     * @param bool $downloadcontents download external contents if exist
     * @return bool
     */
    public function delete($downloadcontents = false) {
        global $DB;

        //delete all instances of this type
        $params = array();
        $params['context'] = array();
        $params['onlyvisible'] = false;
        $params['type'] = $this->_typename;
        $instances = repository::get_instances($params);
        foreach ($instances as $instance) {
            $instance->delete($downloadcontents);
        }

        //delete all general options
        foreach ($this->_options as $name => $value) {
            set_config($name, null, $this->_typename);
        }

        cache::make('core', 'repositories')->purge();
        try {
            $DB->delete_records('repository', array('type' => $this->_typename));
        } catch (dml_exception $ex) {
            return false;
        }
        return true;
    }

    /**
     * Prepares the repository type to be cached. Implements method from cacheable_object interface.
     *
     * @return array
     */
    public function prepare_to_cache() {
        return array(
            'typename' => $this->_typename,
            'typeoptions' => $this->_options,
            'visible' => $this->_visible,
            'sortorder' => $this->_sortorder
        );
    }

    /**
     * Restores repository type from cache. Implements method from cacheable_object interface.
     *
     * @return array
     */
    public static function wake_from_cache($data) {
        return new repository_type($data['typename'], $data['typeoptions'], $data['visible'], $data['sortorder']);
    }
}

/**
 * This is the base class of the repository class.
 *
 * To create repository plugin, see: {@link http://docs.moodle.org/dev/Repository_plugins}
 * See an example: {@link repository_boxnet}
 *
 * @package   core_repository
 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
abstract class repository implements cacheable_object {
    /** Timeout in seconds for downloading the external file into moodle */
    const GETFILE_TIMEOUT = 30;
    /** Timeout in seconds for syncronising the external file size */
    const SYNCFILE_TIMEOUT = 1;
    /** Timeout in seconds for downloading an image file from external repository during syncronisation */
    const SYNCIMAGE_TIMEOUT = 3;

    // $disabled can be set to true to disable a plugin by force
    // example: self::$disabled = true
    /** @var bool force disable repository instance */
    public $disabled = false;
    /** @var int repository instance id */
    public $id;
    /** @var stdClass current context */
    public $context;
    /** @var array repository options */
    public $options;
    /** @var bool Whether or not the repository instance is editable */
    public $readonly;
    /** @var int return types */
    public $returntypes;
    /** @var stdClass repository instance database record */
    public $instance;
    /** @var string Type of repository (webdav, google_docs, dropbox, ...). Read from $this->get_typename(). */
    protected $typename;

    /**
     * Constructor
     *
     * @param int $repositoryid repository instance id
     * @param int|stdClass $context a context id or context object
     * @param array $options repository options
     * @param int $readonly indicate this repo is readonly or not
     */
    public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array(), $readonly = 0) {
        global $DB;
        $this->id = $repositoryid;
        if (is_object($context)) {
            $this->context = $context;
        } else {
            $this->context = context::instance_by_id($context);
        }
        $cache = cache::make('core', 'repositories');
        if (($this->instance = $cache->get('i:'. $this->id)) === false) {
            $this->instance = $DB->get_record_sql("SELECT i.*, r.type AS repositorytype, r.sortorder, r.visible
                      FROM {repository} r, {repository_instances} i
                     WHERE i.typeid = r.id and i.id = ?", array('id' => $this->id));
            $cache->set('i:'. $this->id, $this->instance);
        }
        $this->readonly = $readonly;
        $this->options = array();

        if (is_array($options)) {
            // The get_option() method will get stored options in database.
            $options = array_merge($this->get_option(), $options);
        } else {
            $options = $this->get_option();
        }
        foreach ($options as $n => $v) {
            $this->options[$n] = $v;
        }
        $this->name = $this->get_name();
        $this->returntypes = $this->supported_returntypes();
        $this->super_called = true;
    }

    /**
     * Magic method for non-existing (usually deprecated) class methods.
     *
     * @param string $name
     * @param array $arguments
     * @return mixed
     * @throws coding_exception
     */
    public function __call($name, $arguments) {
        if ($name === 'sync_individual_file') {
            // Method repository::sync_individual_file() was deprecated in Moodle 2.6.
            // See repository::sync_reference().
            debugging('Function repository::sync_individual_file() is deprecated.', DEBUG_DEVELOPER);
            return true;
        } else if ($name === 'get_file_by_reference') {
            // Method repository::get_file_by_reference() was deprecated in Moodle 2.6.
            // See repository::sync_reference().
            debugging('Function repository::get_file_by_reference() is deprecated.', DEBUG_DEVELOPER);
            return null;
        } else if ($name === 'get_reference_file_lifetime') {
            // Method repository::get_file_by_reference() was deprecated in Moodle 2.6.
            // See repository::sync_reference().
            debugging('Function repository::get_reference_file_lifetime() is deprecated.', DEBUG_DEVELOPER);
            return 24 * 60 * 60;
        } else {
            throw new coding_exception('Tried to call unknown method '.get_class($this).'::'.$name);
        }
    }

    /**
     * Get repository instance using repository id
     *
     * Note that this function does not check permission to access repository contents
     *
     * @throws repository_exception
     *
     * @param int $repositoryid repository instance ID
     * @param context|int $context context instance or context ID where this repository will be used
     * @param array $options additional repository options
     * @return repository
     */
    public static function get_repository_by_id($repositoryid, $context, $options = array()) {
        global $CFG, $DB;
        $cache = cache::make('core', 'repositories');
        if (!is_object($context)) {
            $context = context::instance_by_id($context);
        }
        $cachekey = 'rep:'. $repositoryid. ':'. $context->id. ':'. serialize($options);
        if ($repository = $cache->get($cachekey)) {
            return $repository;
        }

        if (!$record = $cache->get('i:'. $repositoryid)) {
            $sql = "SELECT i.*, r.type AS repositorytype, r.visible, r.sortorder
                      FROM {repository_instances} i
                      JOIN {repository} r ON r.id = i.typeid
                     WHERE i.id = ?";
            if (!$record = $DB->get_record_sql($sql, array($repositoryid))) {
                throw new repository_exception('invalidrepositoryid', 'repository');
            }
            $cache->set('i:'. $record->id, $record);
        }

        $type = $record->repositorytype;
        if (file_exists($CFG->dirroot . "/repository/$type/lib.php")) {
            require_once($CFG->dirroot . "/repository/$type/lib.php");
            $classname = 'repository_' . $type;
            $options['type'] = $type;
            $options['typeid'] = $record->typeid;
            $options['visible'] = $record->visible;
            if (empty($options['name'])) {
                $options['name'] = $record->name;
            }
            $repository = new $classname($repositoryid, $context, $options, $record->readonly);
            if (empty($repository->super_called)) {
                // to make sure the super construct is called
                debugging('parent::__construct must be called by '.$type.' plugin.');
            }
            $cache->set($cachekey, $repository);
            return $repository;
        } else {
            throw new repository_exception('invalidplugin', 'repository');
        }
    }

    /**
     * Returns the type name of the repository.
     *
     * @return string type name of the repository.
     * @since  2.5
     */
    public function get_typename() {
        if (empty($this->typename)) {
            $matches = array();
            if (!preg_match("/^repository_(.*)$/", get_class($this), $matches)) {
                throw new coding_exception('The class name of a repository should be repository_<typeofrepository>, '.
                        'e.g. repository_dropbox');
            }
            $this->typename = $matches[1];
        }
        return $this->typename;
    }

    /**
     * Get a repository type object by a given type name.
     *
     * @static
     * @param string $typename the repository type name
     * @return repository_type|bool
     */
    public static function get_type_by_typename($typename) {
        global $DB;
        $cache = cache::make('core', 'repositories');
        if (($repositorytype = $cache->get('typename:'. $typename)) === false) {
            $repositorytype = null;
            if ($record = $DB->get_record('repository', array('type' => $typename))) {
                $repositorytype = new repository_type($record->type, (array)get_config($record->type), $record->visible, $record->sortorder);
                $cache->set('typeid:'. $record->id, $repositorytype);
            }
            $cache->set('typename:'. $typename, $repositorytype);
        }
        return $repositorytype;
    }

    /**
     * Get the repository type by a given repository type id.
     *
     * @static
     * @param int $id the type id
     * @return object
     */
    public static function get_type_by_id($id) {
        global $DB;
        $cache = cache::make('core', 'repositories');
        if (($repositorytype = $cache->get('typeid:'. $id)) === false) {
            $repositorytype = null;
            if ($record = $DB->get_record('repository', array('id' => $id))) {
                $repositorytype = new repository_type($record->type, (array)get_config($record->type), $record->visible, $record->sortorder);
                $cache->set('typename:'. $record->type, $repositorytype);
            }
            $cache->set('typeid:'. $id, $repositorytype);
        }
        return $repositorytype;
    }

    /**
     * Return all repository types ordered by sortorder field
     * first repository type in returnedarray[0], second repository type in returnedarray[1], ...
     *
     * @static
     * @param bool $visible can return types by visiblity, return all types if null
     * @return array Repository types
     */
    public static function get_types($visible=null) {
        global $DB, $CFG;
        $cache = cache::make('core', 'repositories');
        if (!$visible) {
            $typesnames = $cache->get('types');
        } else {
            $typesnames = $cache->get('typesvis');
        }
        $types = array();
        if ($typesnames === false) {
            $typesnames = array();
            $vistypesnames = array();
            if ($records = $DB->get_records('repository', null ,'sortorder')) {
                foreach($records as $type) {
                    if (($repositorytype = $cache->get('typename:'. $type->type)) === false) {
                        // Create new instance of repository_type.
                        if (file_exists($CFG->dirroot . '/repository/'. $type->type .'/lib.php')) {
                            $repositorytype = new repository_type($type->type, (array)get_config($type->type), $type->visible, $type->sortorder);
                            $cache->set('typeid:'. $type->id, $repositorytype);
                            $cache->set('typename:'. $type->type, $repositorytype);
                        }
                    }
                    if ($repositorytype) {
                        if (empty($visible) || $repositorytype->get_visible()) {
                            $types[] = $repositorytype;
                            $vistypesnames[] = $repositorytype->get_typename();
                        }
                        $typesnames[] = $repositorytype->get_typename();
                    }
                }
            }
            $cache->set('types', $typesnames);
            $cache->set('typesvis', $vistypesnames);
        } else {
            foreach ($typesnames as $typename) {
                $types[] = self::get_type_by_typename($typename);
            }
        }
        return $types;
    }

    /**
     * Checks if user has a capability to view the current repository.
     *
     * @return bool true when the user can, otherwise throws an exception.
     * @throws repository_exception when the user does not meet the requirements.
     */
    public final function check_capability() {
        global $USER;

        // The context we are on.
        $currentcontext = $this->context;

        // Ensure that the user can view the repository in the current context.
        $can = has_capability('repository/'.$this->get_typename().':view', $currentcontext);

        // Context in which the repository has been created.
        $repocontext = context::instance_by_id($this->instance->contextid);

        // Prevent access to private repositories when logged in as.
        if ($can && \core\session\manager::is_loggedinas()) {
            if ($this->contains_private_data() || $repocontext->contextlevel == CONTEXT_USER) {
                $can = false;
            }
        }

        // We are going to ensure that the current context was legit, and reliable to check
        // the capability against. (No need to do that if we already cannot).
        if ($can) {
            if ($repocontext->contextlevel == CONTEXT_USER) {
                // The repository is a user instance, ensure we're the right user to access it!
                if ($repocontext->instanceid != $USER->id) {
                    $can = false;
                }
            } else if ($repocontext->contextlevel == CONTEXT_COURSE) {
                // The repository is a course one. Let's check that we are on the right course.
                if (in_array($currentcontext->contextlevel, array(CONTEXT_COURSE, CONTEXT_MODULE, CONTEXT_BLOCK))) {
                    $coursecontext = $currentcontext->get_course_context();
                    if ($coursecontext->instanceid != $repocontext->instanceid) {
                        $can = false;
                    }
                } else {
                    // We are on a parent context, therefore it's legit to check the permissions
                    // in the current context.
                }
            } else {
                // Nothing to check here, system instances can have different permissions on different
                // levels. We do not want to prevent URL hack here, because it does not make sense to
                // prevent a user to access a repository in a context if it's accessible in another one.
            }
        }

        if ($can) {
            return true;
        }

        throw new repository_exception('nopermissiontoaccess', 'repository');
    }

    /**
     * Check if file already exists in draft area.
     *
     * @static
     * @param int $itemid of the draft area.
     * @param string $filepath path to the file.
     * @param string $filename file name.
     * @return bool
     */
    public static function draftfile_exists($itemid, $filepath, $filename) {
        global $USER;
        $fs = get_file_storage();
        $usercontext = context_user::instance($USER->id);
        return $fs->file_exists($usercontext->id, 'user', 'draft', $itemid, $filepath, $filename);
    }

    /**
     * Parses the moodle file reference and returns an instance of stored_file
     *
     * @param string $reference reference to the moodle internal file as retruned by
     *        {@link repository::get_file_reference()} or {@link file_storage::pack_reference()}
     * @return stored_file|null
     */
    public static function get_moodle_file($reference) {
        $params = file_storage::unpack_reference($reference, true);
        $fs = get_file_storage();
        return $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
                    $params['itemid'], $params['filepath'], $params['filename']);
    }

    /**
     * Repository method to make sure that user can access particular file.
     *
     * This is checked when user tries to pick the file from repository to deal with
     * potential parameter substitutions is request
     *
     * @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
     * @return bool whether the file is accessible by current user
     */
    public function file_is_accessible($source) {
        if ($this->has_moodle_files()) {
            $reference = $this->get_file_reference($source);
            try {
                $params = file_storage::unpack_reference($reference, true);
            } catch (file_reference_exception $e) {
                return false;
            }
            $browser = get_file_browser();
            $context = context::instance_by_id($params['contextid']);
            $file_info = $browser->get_file_info($context, $params['component'], $params['filearea'],
                    $params['itemid'], $params['filepath'], $params['filename']);
            return !empty($file_info);
        }
        return true;
    }

    /**
     * This function is used to copy a moodle file to draft area.
     *
     * It DOES NOT check if the user is allowed to access this file because the actual file
     * can be located in the area where user does not have access to but there is an alias
     * to this file in the area where user CAN access it.
     * {@link file_is_accessible} should be called for alias location before calling this function.
     *
     * @param string $source The metainfo of file, it is base64 encoded php serialized data
     * @param stdClass|array $filerecord contains itemid, filepath, filename and optionally other
     *      attributes of the new file
     * @param int $maxbytes maximum allowed size of file, -1 if unlimited. If size of file exceeds
     *      the limit, the file_exception is thrown.
     * @param int $areamaxbytes the maximum size of the area. A file_exception is thrown if the
     *      new file will reach the limit.
     * @return array The information about the created file
     */
    public function copy_to_area($source, $filerecord, $maxbytes = -1, $areamaxbytes = FILE_AREA_MAX_BYTES_UNLIMITED) {
        global $USER;
        $fs = get_file_storage();

        if ($this->has_moodle_files() == false) {
            throw new coding_exception('Only repository used to browse moodle files can use repository::copy_to_area()');
        }

        $user_context = context_user::instance($USER->id);

        $filerecord = (array)$filerecord;
        // make sure the new file will be created in user draft area
        $filerecord['component'] = 'user';
        $filerecord['filearea'] = 'draft';
        $filerecord['contextid'] = $user_context->id;
        $draftitemid = $filerecord['itemid'];
        $new_filepath = $filerecord['filepath'];
        $new_filename = $filerecord['filename'];

        // the file needs to copied to draft area
        $stored_file = self::get_moodle_file($source);
        if ($maxbytes != -1 && $stored_file->get_filesize() > $maxbytes) {
            throw new file_exception('maxbytes');
        }
        // Validate the size of the draft area.
        if (file_is_draft_area_limit_reached($draftitemid, $areamaxbytes, $stored_file->get_filesize())) {
            throw new file_exception('maxareabytes');
        }

        if (repository::draftfile_exists($draftitemid, $new_filepath, $new_filename)) {
            // create new file
            $unused_filename = repository::get_unused_filename($draftitemid, $new_filepath, $new_filename);
            $filerecord['filename'] = $unused_filename;
            $fs->create_file_from_storedfile($filerecord, $stored_file);
            $event = array();
            $event['event'] = 'fileexists';
            $event['newfile'] = new stdClass;
            $event['newfile']->filepath = $new_filepath;
            $event['newfile']->filename = $unused_filename;
            $event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $unused_filename)->out();
            $event['existingfile'] = new stdClass;
            $event['existingfile']->filepath = $new_filepath;
            $event['existingfile']->filename = $new_filename;
            $event['existingfile']->url = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();
            return $event;
        } else {
            $fs->create_file_from_storedfile($filerecord, $stored_file);
            $info = array();
            $info['itemid'] = $draftitemid;
            $info['file'] = $new_filename;
            $info['title'] = $new_filename;
            $info['contextid'] = $user_context->id;
            $info['url'] = moodle_url::make_draftfile_url($draftitemid, $new_filepath, $new_filename)->out();
            $info['filesize'] = $stored_file->get_filesize();
            return $info;
        }
    }

    /**
     * Get an unused filename from the current draft area.
     *
     * Will check if the file ends with ([0-9]) and increase the number.
     *
     * @static
     * @param int $itemid draft item ID.
     * @param string $filepath path to the file.
     * @param string $filename name of the file.
     * @return string an unused file name.
     */
    public static function get_unused_filename($itemid, $filepath, $filename) {
        global $USER;
        $contextid = context_user::instance($USER->id)->id;
        $fs = get_file_storage();
        return $fs->get_unused_filename($contextid, 'user', 'draft', $itemid, $filepath, $filename);
    }

    /**
     * Append a suffix to filename.
     *
     * @static
     * @param string $filename
     * @return string
     * @deprecated since 2.5
     */
    public static function append_suffix($filename) {
        debugging('The function repository::append_suffix() has been deprecated. Use repository::get_unused_filename() instead.',
            DEBUG_DEVELOPER);
        $pathinfo = pathinfo($filename);
        if (empty($pathinfo['extension'])) {
            return $filename . RENAME_SUFFIX;
        } else {
            return $pathinfo['filename'] . RENAME_SUFFIX . '.' . $pathinfo['extension'];
        }
    }

    /**
     * Return all types that you a user can create/edit and which are also visible
     * Note: Mostly used in order to know if at least one editable type can be set
     *
     * @static
     * @param stdClass $context the context for which we want the editable types
     * @return array types
     */
    public static function get_editable_types($context = null) {

        if (empty($context)) {
            $context = context_system::instance();
        }

        $types= repository::get_types(true);
        $editabletypes = array();
        foreach ($types as $type) {
            $instanceoptionnames = repository::static_function($type->get_typename(), 'get_instance_option_names');
            if (!empty($instanceoptionnames)) {
                if ($type->get_contextvisibility($context)) {
                    $editabletypes[]=$type;
                }
             }
        }
        return $editabletypes;
    }

    /**
     * Return repository instances
     *
     * @static
     * @param array $args Array containing the following keys:
     *           currentcontext : instance of context (default system context)
     *           context : array of instances of context (default empty array)
     *           onlyvisible : bool (default true)
     *           type : string return instances of this type only
     *           accepted_types : string|array return instances that contain files of those types (*, web_image, .pdf, ...)
     *           return_types : int combination of FILE_INTERNAL & FILE_EXTERNAL & FILE_REFERENCE.
     *                          0 means every type. The default is FILE_INTERNAL | FILE_EXTERNAL.
     *           userid : int if specified, instances belonging to other users will not be returned
     *
     * @return array repository instances
     */
    public static function get_instances($args = array()) {
        global $DB, $CFG, $USER;

        // Fill $args attributes with default values unless specified
        if (!isset($args['currentcontext']) || !($args['currentcontext'] instanceof context)) {
            $current_context = context_system::instance();
        } else {
            $current_context = $args['currentcontext'];
        }
        $args['currentcontext'] = $current_context->id;
        $contextids = array();
        if (!empty($args['context'])) {
            foreach ($args['context'] as $context) {
                $contextids[] = $context->id;
            }
        }
        $args['context'] = $contextids;
        if (!isset($args['onlyvisible'])) {
            $args['onlyvisible'] = true;
        }
        if (!isset($args['return_types'])) {
            $args['return_types'] = FILE_INTERNAL | FILE_EXTERNAL;
        }
        if (!isset($args['type'])) {
            $args['type'] = null;
        }
        if (empty($args['disable_types']) || !is_array($args['disable_types'])) {
            $args['disable_types'] = null;
        }
        if (empty($args['userid']) || !is_numeric($args['userid'])) {
            $args['userid'] = null;
        }
        if (!isset($args['accepted_types']) || (is_array($args['accepted_types']) && in_array('*', $args['accepted_types']))) {
            $args['accepted_types'] = '*';
        }
        ksort($args);
        $cachekey = 'all:'. serialize($args);

        // Check if we have cached list of repositories with the same query
        $cache = cache::make('core', 'repositories');
        if (($cachedrepositories = $cache->get($cachekey)) !== false) {
            // convert from cacheable_object_array to array
            $repositories = array();
            foreach ($cachedrepositories as $repository) {
                $repositories[$repository->id] = $repository;
            }
            return $repositories;
        }

        // Prepare DB SQL query to retrieve repositories
        $params = array();
        $sql = "SELECT i.*, r.type AS repositorytype, r.sortorder, r.visible
                  FROM {repository} r, {repository_instances} i
                 WHERE i.typeid = r.id ";

        if ($args['disable_types']) {
            list($types, $p) = $DB->get_in_or_equal($args['disable_types'], SQL_PARAMS_NAMED, 'distype', false);
            $sql .= " AND r.type $types";
            $params = array_merge($params, $p);
        }

        if ($args['userid']) {
            $sql .= " AND (i.userid = 0 or i.userid = :userid)";
            $params['userid'] = $args['userid'];
        }

        if ($args['context']) {
            list($ctxsql, $p2) = $DB->get_in_or_equal($args['context'], SQL_PARAMS_NAMED, 'ctx');
            $sql .= " AND i.contextid $ctxsql";
            $params = array_merge($params, $p2);
        }

        if ($args['onlyvisible'] == true) {
            $sql .= " AND r.visible = 1";
        }

        if ($args['type'] !== null) {
            $sql .= " AND r.type = :type";
            $params['type'] = $args['type'];
        }
        $sql .= " ORDER BY r.sortorder, i.name";

        if (!$records = $DB->get_records_sql($sql, $params)) {
            $records = array();
        }

        $repositories = array();
        // Sortorder should be unique, which is not true if we use $record->sortorder
        // and there are multiple instances of any repository type
        $sortorder = 1;
        foreach ($records as $record) {
            $cache->set('i:'. $record->id, $record);
            if (!file_exists($CFG->dirroot . '/repository/'. $record->repositorytype.'/lib.php')) {
                continue;
            }
            $repository = self::get_repository_by_id($record->id, $current_context);
            $repository->options['sortorder'] = $sortorder++;

            $is_supported = true;

            // check mimetypes
            if ($args['accepted_types'] !== '*' and $repository->supported_filetypes() !== '*') {
                $accepted_ext = file_get_typegroup('extension', $args['accepted_types']);
                $supported_ext = file_get_typegroup('extension', $repository->supported_filetypes());
                $valid_ext = array_intersect($accepted_ext, $supported_ext);
                $is_supported = !empty($valid_ext);
            }
            // Check return values.
            if (!empty($args['return_types']) && !($repository->supported_returntypes() & $args['return_types'])) {
                $is_supported = false;
            }

            if (!$args['onlyvisible'] || ($repository->is_visible() && !$repository->disabled)) {
                // check capability in current context
                $capability = has_capability('repository/'.$record->repositorytype.':view', $current_context);
                if ($record->repositorytype == 'coursefiles') {
                    // coursefiles plugin needs managefiles permission
                    $capability = $capability && has_capability('moodle/course:managefiles', $current_context);
                }
                if ($is_supported && $capability) {
                    $repositories[$repository->id] = $repository;
                }
            }
        }
        $cache->set($cachekey, new cacheable_object_array($repositories));
        return $repositories;
    }

    /**
     * Get single repository instance for administrative actions
     *
     * Do not use this function to access repository contents, because it
     * does not set the current context
     *
     * @see repository::get_repository_by_id()
     *
     * @static
     * @param integer $id repository instance id
     * @return repository
     */
    public static function get_instance($id) {
        return self::get_repository_by_id($id, context_system::instance());
    }

    /**
     * Call a static function. Any additional arguments than plugin and function will be passed through.
     *
     * @static
     * @param string $plugin repository plugin name
     * @param string $function function name
     * @return mixed
     */
    public static function static_function($plugin, $function) {
        global $CFG;

        //check that the plugin exists
        $typedirectory = $CFG->dirroot . '/repository/'. $plugin . '/lib.php';
        if (!file_exists($typedirectory)) {
            //throw new repository_exception('invalidplugin', 'repository');
            return false;
        }

        $args = func_get_args();
        if (count($args) <= 2) {
            $args = array();
        } else {
            array_shift($args);
            array_shift($args);
        }

        require_once($typedirectory);
        return call_user_func_array(array('repository_' . $plugin, $function), $args);
    }

    /**
     * Scan file, throws exception in case of infected file.
     *
     * Please note that the scanning engine must be able to access the file,
     * permissions of the file are not modified here!
     *
     * @static
     * @param string $thefile
     * @param string $filename name of the file
     * @param bool $deleteinfected
     */
    public static function antivir_scan_file($thefile, $filename, $deleteinfected) {
        global $CFG;

        if (!is_readable($thefile)) {
            // this should not happen
            return;
        }

        if (empty($CFG->runclamonupload) or empty($CFG->pathtoclam)) {
            // clam not enabled
            return;
        }

        $CFG->pathtoclam = trim($CFG->pathtoclam);

        if (!file_exists($CFG->pathtoclam) or !is_executable($CFG->pathtoclam)) {
            // misconfigured clam - use the old notification for now
            require("$CFG->libdir/uploadlib.php");
            $notice = get_string('clamlost', 'moodle', $CFG->pathtoclam);
            clam_message_admins($notice);
            return;
        }

        $clamparam = ' --stdout ';
        // If we are dealing with clamdscan, clamd is likely run as a different user
        // that might not have permissions to access your file.
        // To make clamdscan work, we use --fdpass parameter that passes the file
        // descriptor permissions to clamd, which allows it to scan given file
        // irrespective of directory and file permissions.
        if (basename($CFG->pathtoclam) == 'clamdscan') {
            $clamparam .= '--fdpass ';
        }
        // execute test
        $cmd = escapeshellcmd($CFG->pathtoclam).$clamparam.escapeshellarg($thefile);
        exec($cmd, $output, $return);

        if ($return == 0) {
            // perfect, no problem found
            return;

        } else if ($return == 1) {
            // infection found
            if ($deleteinfected) {
                unlink($thefile);
            }
            throw new moodle_exception('virusfounduser', 'moodle', '', array('filename'=>$filename));

        } else {
            //unknown problem
            require("$CFG->libdir/uploadlib.php");
            $notice = get_string('clamfailed', 'moodle', get_clam_error_code($return));
            $notice .= "\n\n". implode("\n", $output);
            clam_message_admins($notice);
            if ($CFG->clamfailureonupload === 'actlikevirus') {
                if ($deleteinfected) {
                    unlink($thefile);
                }
                throw new moodle_exception('virusfounduser', 'moodle', '', array('filename'=>$filename));
            } else {
                return;
            }
        }
    }

    /**
     * Repository method to serve the referenced file
     *
     * @see send_stored_file
     *
     * @param stored_file $storedfile the file that contains the reference
     * @param int $lifetime Number of seconds before the file should expire from caches (null means $CFG->filelifetime)
     * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
     * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
     * @param array $options additional options affecting the file serving
     */
    public function send_file($storedfile, $lifetime=null , $filter=0, $forcedownload=false, array $options = null) {
        if ($this->has_moodle_files()) {
            $fs = get_file_storage();
            $params = file_storage::unpack_reference($storedfile->get_reference(), true);
            $srcfile = null;
            if (is_array($params)) {
                $srcfile = $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
                        $params['itemid'], $params['filepath'], $params['filename']);
            }
            if (empty($options)) {
                $options = array();
            }
            if (!isset($options['filename'])) {
                $options['filename'] = $storedfile->get_filename();
            }
            if (!$srcfile) {
                send_file_not_found();
            } else {
                send_stored_file($srcfile, $lifetime, $filter, $forcedownload, $options);
            }
        } else {
            throw new coding_exception("Repository plugin must implement send_file() method.");
        }
    }

    /**
     * Return human readable reference information
     *
     * @param string $reference value of DB field files_reference.reference
     * @param int $filestatus status of the file, 0 - ok, 666 - source missing
     * @return string
     */
    public function get_reference_details($reference, $filestatus = 0) {
        if ($this->has_moodle_files()) {
            $fileinfo = null;
            $params = file_storage::unpack_reference($reference, true);
            if (is_array($params)) {
                $context = context::instance_by_id($params['contextid'], IGNORE_MISSING);
                if ($context) {
                    $browser = get_file_browser();
                    $fileinfo = $browser->get_file_info($context, $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']);
                }
            }
            if (empty($fileinfo)) {
                if ($filestatus == 666) {
                    if (is_siteadmin() || ($context && has_capability('moodle/course:managefiles', $context))) {
                        return get_string('lostsource', 'repository',
                                $params['contextid']. '/'. $params['component']. '/'. $params['filearea']. '/'. $params['itemid']. $params['filepath']. $params['filename']);
                    } else {
                        return get_string('lostsource', 'repository', '');
                    }
                }
                return get_string('undisclosedsource', 'repository');
            } else {
                return $fileinfo->get_readable_fullname();
            }
        }
        return '';
    }

    /**
     * Cache file from external repository by reference
     * {@link repository::get_file_reference()}
     * {@link repository::get_file()}
     * Invoked at MOODLE/repository/repository_ajax.php
     *
     * @param string $reference this reference is generated by
     *                          repository::get_file_reference()
     * @param stored_file $storedfile created file reference
     */
    public function cache_file_by_reference($reference, $storedfile) {
    }

    /**
     * Return the source information
     *
     * The result of the function is stored in files.source field. It may be analysed
     * when the source file is lost or repository may use it to display human-readable
     * location of reference original.
     *
     * This method is called when file is picked for the first time only. When file
     * (either copy or a reference) is already in moodle and it is being picked
     * again to another file area (also as a copy or as a reference), the value of
     * files.source is copied.
     *
     * @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
     * @return string|null
     */
    public function get_file_source_info($source) {
        if ($this->has_moodle_files()) {
            $reference = $this->get_file_reference($source);
            return $this->get_reference_details($reference, 0);
        }
        return $source;
    }

    /**
     * Move file from download folder to file pool using FILE API
     *
     * @todo MDL-28637
     * @static
     * @param string $thefile file path in download folder
     * @param stdClass $record
     * @return array containing the following keys:
     *           icon
     *           file
     *           id
     *           url
     */
    public static function move_to_filepool($thefile, $record) {
        global $DB, $CFG, $USER, $OUTPUT;

        // scan for viruses if possible, throws exception if problem found
        self::antivir_scan_file($thefile, $record->filename, empty($CFG->repository_no_delete)); //TODO: MDL-28637 this repository_no_delete is a bloody hack!

        $fs = get_file_storage();
        // If file name being used.
        if (repository::draftfile_exists($record->itemid, $record->filepath, $record->filename)) {
            $draftitemid = $record->itemid;
            $new_filename = repository::get_unused_filename($draftitemid, $record->filepath, $record->filename);
            $old_filename = $record->filename;
            // Create a tmp file.
            $record->filename = $new_filename;
            $newfile = $fs->create_file_from_pathname($record, $thefile);
            $event = array();
            $event['event'] = 'fileexists';
            $event['newfile'] = new stdClass;
            $event['newfile']->filepath = $record->filepath;
            $event['newfile']->filename = $new_filename;
            $event['newfile']->url = moodle_url::make_draftfile_url($draftitemid, $record->filepath, $new_filename)->out();

            $event['existingfile'] = new stdClass;
            $event['existingfile']->filepath = $record->filepath;
            $event['existingfile']->filename = $old_filename;
            $event['existingfile']->url      = moodle_url::make_draftfile_url($draftitemid, $record->filepath, $old_filename)->out();
            return $event;
        }
        if ($file = $fs->create_file_from_pathname($record, $thefile)) {
            if (empty($CFG->repository_no_delete)) {
                $delete = unlink($thefile);
                unset($CFG->repository_no_delete);
            }
            return array(
                'url'=>moodle_url::make_draftfile_url($file->get_itemid(), $file->get_filepath(), $file->get_filename())->out(),
                'id'=>$file->get_itemid(),
                'file'=>$file->get_filename(),
                'icon' => $OUTPUT->pix_url(file_extension_icon($thefile, 32))->out(),
            );
        } else {
            return null;
        }
    }

    /**
     * Builds a tree of files This function is then called recursively.
     *
     * @static
     * @todo take $search into account, and respect a threshold for dynamic loading
     * @param file_info $fileinfo an object returned by file_browser::get_file_info()
     * @param string $search searched string
     * @param bool $dynamicmode no recursive call is done when in dynamic mode
     * @param array $list the array containing the files under the passed $fileinfo
     * @return int the number of files found
     */
    public static function build_tree($fileinfo, $search, $dynamicmode, &$list) {
        global $CFG, $OUTPUT;

        $filecount = 0;
        $children = $fileinfo->get_children();

        foreach ($children as $child) {
            $filename = $child->get_visible_name();
            $filesize = $child->get_filesize();
            $filesize = $filesize ? display_size($filesize) : '';
            $filedate = $child->get_timemodified();
            $filedate = $filedate ? userdate($filedate) : '';
            $filetype = $child->get_mimetype();

            if ($child->is_directory()) {
                $path = array();
                $level = $child->get_parent();
                while ($level) {
                    $params = $level->get_params();
                    $path[] = array($params['filepath'], $level->get_visible_name());
                    $level = $level->get_parent();
                }

                $tmp = array(
                    'title' => $child->get_visible_name(),
                    'size' => 0,
                    'date' => $filedate,
                    'path' => array_reverse($path),
                    'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)
                );

                //if ($dynamicmode && $child->is_writable()) {
                //    $tmp['children'] = array();
                //} else {
                    // if folder name matches search, we send back all files contained.
                $_search = $search;
                if ($search && stristr($tmp['title'], $search) !== false) {
                    $_search = false;
                }
                $tmp['children'] = array();
                $_filecount = repository::build_tree($child, $_search, $dynamicmode, $tmp['children']);
                if ($search && $_filecount) {
                    $tmp['expanded'] = 1;
                }

                //}

                if (!$search || $_filecount || (stristr($tmp['title'], $search) !== false)) {
                    $filecount += $_filecount;
                    $list[] = $tmp;
                }

            } else { // not a directory
                // skip the file, if we're in search mode and it's not a match
                if ($search && (stristr($filename, $search) === false)) {
                    continue;
                }
                $params = $child->get_params();
                $source = serialize(array($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']));
                $list[] = array(
                    'title' => $filename,
                    'size' => $filesize,
                    'date' => $filedate,
                    //'source' => $child->get_url(),
                    'source' => base64_encode($source),
                    'icon'=>$OUTPUT->pix_url(file_file_icon($child, 24))->out(false),
                    'thumbnail'=>$OUTPUT->pix_url(file_file_icon($child, 90))->out(false),
                );
                $filecount++;
            }
        }

        return $filecount;
    }

    /**
     * Display a repository instance list (with edit/delete/create links)
     *
     * @static
     * @param stdClass $context the context for which we display the instance
     * @param string $typename if set, we display only one type of instance
     */
    public static function display_instances_list($context, $typename = null) {
        global $CFG, $USER, $OUTPUT;

        $output = $OUTPUT->box_start('generalbox');
        //if the context is SYSTEM, so we call it from administration page
        $admin = ($context->id == SYSCONTEXTID) ? true : false;
        if ($admin) {
            $baseurl = new moodle_url('/'.$CFG->admin.'/repositoryinstance.php', array('sesskey'=>sesskey()));
            $output .= $OUTPUT->heading(get_string('siteinstances', 'repository'));
        } else {
            $baseurl = new moodle_url('/repository/manage_instances.php', array('contextid'=>$context->id, 'sesskey'=>sesskey()));
        }

        $namestr = get_string('name');
        $pluginstr = get_string('plugin', 'repository');
        $settingsstr = get_string('settings');
        $deletestr = get_string('delete');
        // Retrieve list of instances. In administration context we want to display all
        // instances of a type, even if this type is not visible. In course/user context we
        // want to display only visible instances, but for every type types. The repository::get_instances()
        // third parameter displays only visible type.
        $params = array();
        $params['context'] = array($context);
        $params['currentcontext'] = $context;
        $params['return_types'] = 0;
        $params['onlyvisible'] = !$admin;
        $params['type']        = $typename;
        $instances = repository::get_instances($params);
        $instancesnumber = count($instances);
        $alreadyplugins = array();

        $table = new html_table();
        $table->head = array($namestr, $pluginstr, $settingsstr, $deletestr);
        $table->align = array('left', 'left', 'center','center');
        $table->data = array();

        $updowncount = 1;

        foreach ($instances as $i) {
            $settings = '';
            $delete = '';

            $type = repository::get_type_by_id($i->options['typeid']);

            if ($type->get_contextvisibility($context)) {
                if (!$i->readonly) {

                    $settingurl = new moodle_url($baseurl);
                    $settingurl->param('type', $i->options['type']);
                    $settingurl->param('edit', $i->id);
                    $settings .= html_writer::link($settingurl, $settingsstr);

                    $deleteurl = new moodle_url($baseurl);
                    $deleteurl->param('delete', $i->id);
                    $deleteurl->param('type', $i->options['type']);
                    $delete .= html_writer::link($deleteurl, $deletestr);
                }
            }

            $type = repository::get_type_by_id($i->options['typeid']);
            $table->data[] = array(format_string($i->name), $type->get_readablename(), $settings, $delete);

            //display a grey row if the type is defined as not visible
            if (isset($type) && !$type->get_visible()) {
                $table->rowclasses[] = 'dimmed_text';
            } else {
                $table->rowclasses[] = '';
            }

            if (!in_array($i->name, $alreadyplugins)) {
                $alreadyplugins[] = $i->name;
            }
        }
        $output .= html_writer::table($table);
        $instancehtml = '<div>';
        $addable = 0;

        //if no type is set, we can create all type of instance
        if (!$typename) {
            $instancehtml .= '<h3>';
            $instancehtml .= get_string('createrepository', 'repository');
            $instancehtml .= '</h3><ul>';
            $types = repository::get_editable_types($context);
            foreach ($types as $type) {
                if (!empty($type) && $type->get_visible()) {
                    // If the user does not have the permission to view the repository, it won't be displayed in
                    // the list of instances. Hiding the link to create new instances will prevent the
                    // user from creating them without being able to find them afterwards, which looks like a bug.
                    if (!has_capability('repository/'.$type->get_typename().':view', $context)) {
                        continue;
                    }
                    $instanceoptionnames = repository::static_function($type->get_typename(), 'get_instance_option_names');
                    if (!empty($instanceoptionnames)) {
                        $baseurl->param('new', $type->get_typename());
                        $instancehtml .= '<li><a href="'.$baseurl->out().'">'.get_string('createxxinstance', 'repository', get_string('pluginname', 'repository_'.$type->get_typename())).  '</a></li>';
                        $baseurl->remove_params('new');
                        $addable++;
                    }
                }
            }
            $instancehtml .= '</ul>';

        } else {
            $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
            if (!empty($instanceoptionnames)) {   //create a unique type of instance
                $addable = 1;
                $baseurl->param('new', $typename);
                $output .= $OUTPUT->single_button($baseurl, get_string('createinstance', 'repository'), 'get');
                $baseurl->remove_params('new');
            }
        }

        if ($addable) {
            $instancehtml .= '</div>';
            $output .= $instancehtml;
        }

        $output .= $OUTPUT->box_end();

        //print the list + creation links
        print($output);
    }

    /**
     * Prepare file reference information
     *
     * @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
     * @return string file reference, ready to be stored
     */
    public function get_file_reference($source) {
        if ($source && $this->has_moodle_files()) {
            $params = @json_decode(base64_decode($source), true);
            if (!$params && !in_array($this->get_typename(), array('recent', 'user', 'local', 'coursefiles'))) {
                // IMPORTANT! Since default format for moodle files was changed in the minor release as a security fix
                // we maintain an old code here in order not to break 3rd party repositories that deal
                // with moodle files. Repositories are strongly encouraged to be upgraded, see MDL-45616.
                // In Moodle 2.8 this fallback will be removed.
                $params = file_storage::unpack_reference($source, true);
                return file_storage::pack_reference($params);
            }
            if (!is_array($params) || empty($params['contextid'])) {
                throw new repository_exception('invalidparams', 'repository');
            }
            $params = array(
                'component' => empty($params['component']) ? ''   : clean_param($params['component'], PARAM_COMPONENT),
                'filearea'  => empty($params['filearea'])  ? ''   : clean_param($params['filearea'], PARAM_AREA),
                'itemid'    => empty($params['itemid'])    ? 0    : clean_param($params['itemid'], PARAM_INT),
                'filename'  => empty($params['filename'])  ? null : clean_param($params['filename'], PARAM_FILE),
                'filepath'  => empty($params['filepath'])  ? null : clean_param($params['filepath'], PARAM_PATH),
                'contextid' => clean_param($params['contextid'], PARAM_INT)
            );
            // Check if context exists.
            if (!context::instance_by_id($params['contextid'], IGNORE_MISSING)) {
                throw new repository_exception('invalidparams', 'repository');
            }
            return file_storage::pack_reference($params);
        }
        return $source;
    }

    /**
     * Decide where to save the file, can be overwriten by subclass
     *
     * @param string $filename file name
     * @return file path
     */
    public function prepare_file($filename) {
        global $CFG;
        $dir = make_temp_directory('download/'.get_class($this).'/');
        while (empty($filename) || file_exists($dir.$filename)) {
            $filename = uniqid('', true).'_'.time().'.tmp';
        }
        return $dir.$filename;
    }

    /**
     * Does this repository used to browse moodle files?
     *
     * @return bool
     */
    public function has_moodle_files() {
        return false;
    }

    /**
     * Return file URL, for most plugins, the parameter is the original
     * url, but some plugins use a file id, so we need this function to
     * convert file id to original url.
     *
     * @param string $url the url of file
     * @return string
     */
    public function get_link($url) {
        return $url;
    }

    /**
     * Downloads a file from external repository and saves it in temp dir
     *
     * Function get_file() must be implemented by repositories that support returntypes
     * FILE_INTERNAL or FILE_REFERENCE. It is invoked to pick up the file and copy it
     * to moodle. This function is not called for moodle repositories, the function
     * {@link repository::copy_to_area()} is used instead.
     *
     * This function can be overridden by subclass if the files.reference field contains
     * not just URL or if request should be done differently.
     *
     * @see curl
     * @throws file_exception when error occured
     *
     * @param string $url the content of files.reference field, in this implementaion
     * it is asssumed that it contains the string with URL of the file
     * @param string $filename filename (without path) to save the downloaded file in the
     * temporary directory, if omitted or file already exists the new filename will be generated
     * @return array with elements:
     *   path: internal location of the file
     *   url: URL to the source (from parameters)
     */
    public function get_file($url, $filename = '') {
        $path = $this->prepare_file($filename);
        $c = new curl;
        $result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => self::GETFILE_TIMEOUT));
        if ($result !== true) {
            throw new moodle_exception('errorwhiledownload', 'repository', '', $result);
        }
        return array('path'=>$path, 'url'=>$url);
    }

    /**
     * Downloads the file from external repository and saves it in moodle filepool.
     * This function is different from {@link repository::sync_reference()} because it has
     * bigger request timeout and always downloads the content.
     *
     * This function is invoked when we try to unlink the file from the source and convert
     * a reference into a true copy.
     *
     * @throws exception when file could not be imported
     *
     * @param stored_file $file
     * @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit)
     */
    public function import_external_file_contents(stored_file $file, $maxbytes = 0) {
        if (!$file->is_external_file()) {
            // nothing to import if the file is not a reference
            return;
        } else if ($file->get_repository_id() != $this->id) {
            // error
            debugging('Repository instance id does not match');
            return;
        } else if ($this->has_moodle_files()) {
            // files that are references to local files are already in moodle filepool
            // just validate the size
            if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) {
                throw new file_exception('maxbytes');
            }
            return;
        } else {
            if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) {
                // note that stored_file::get_filesize() also calls synchronisation
                throw new file_exception('maxbytes');
            }
            $fs = get_file_storage();
            $contentexists = $fs->content_exists($file->get_contenthash());
            if ($contentexists && $file->get_filesize() && $file->get_contenthash() === sha1('')) {
                // even when 'file_storage::content_exists()' returns true this may be an empty
                // content for the file that was not actually downloaded
                $contentexists = false;
            }
            if (!$file->get_status() && $contentexists) {
                // we already have the content in moodle filepool and it was synchronised recently.
                // Repositories may overwrite it if they want to force synchronisation anyway!
                return;
            } else {
                // attempt to get a file
                try {
                    $fileinfo = $this->get_file($file->get_reference());
                    if (isset($fileinfo['path'])) {
                        list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo['path']);
                        // set this file and other similar aliases synchronised
                        $file->set_synchronized($contenthash, $filesize);
                    } else {
                        throw new moodle_exception('errorwhiledownload', 'repository', '', '');
                    }
                } catch (Exception $e) {
                    if ($contentexists) {
                        // better something than nothing. We have a copy of file. It's sync time
                        // has expired but it is still very likely that it is the last version
                    } else {
                        throw($e);
                    }
                }
            }
        }
    }

    /**
     * Return size of a file in bytes.
     *
     * @param string $source encoded and serialized data of file
     * @return int file size in bytes
     */
    public function get_file_size($source) {
        // TODO MDL-33297 remove this function completely?
        $browser    = get_file_browser();
        $params     = unserialize(base64_decode($source));
        $contextid  = clean_param($params['contextid'], PARAM_INT);
        $fileitemid = clean_param($params['itemid'], PARAM_INT);
        $filename   = clean_param($params['filename'], PARAM_FILE);
        $filepath   = clean_param($params['filepath'], PARAM_PATH);
        $filearea   = clean_param($params['filearea'], PARAM_AREA);
        $component  = clean_param($params['component'], PARAM_COMPONENT);
        $context    = context::instance_by_id($contextid);
        $file_info  = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename);
        if (!empty($file_info)) {
            $filesize = $file_info->get_filesize();
        } else {
            $filesize = null;
        }
        return $filesize;
    }

    /**
     * Return is the instance is visible
     * (is the type visible ? is the context enable ?)
     *
     * @return bool
     */
    public function is_visible() {
        $type = repository::get_type_by_id($this->options['typeid']);
        $instanceoptions = repository::static_function($type->get_typename(), 'get_instance_option_names');

        if ($type->get_visible()) {
            //if the instance is unique so it's visible, otherwise check if the instance has a enabled context
            if (empty($instanceoptions) || $type->get_contextvisibility(context::instance_by_id($this->instance->contextid))) {
                return true;
            }
        }

        return false;
    }

    /**
     * Can the instance be edited by the current user?
     *
     * The property $readonly must not be used within this method because
     * it only controls if the options from self::get_instance_option_names()
     * can be edited.
     *
     * @return bool true if the user can edit the instance.
     * @since 2.5
     */
    public final function can_be_edited_by_user() {
        global $USER;

        // We need to be able to explore the repository.
        try {
            $this->check_capability();
        } catch (repository_exception $e) {
            return false;
        }

        $repocontext = context::instance_by_id($this->instance->contextid);
        if ($repocontext->contextlevel == CONTEXT_USER && $repocontext->instanceid != $USER->id) {
            // If the context of this instance is a user context, we need to be this user.
            return false;
        } else if ($repocontext->contextlevel == CONTEXT_MODULE && !has_capability('moodle/course:update', $repocontext)) {
            // We need to have permissions on the course to edit the instance.
            return false;
        } else if ($repocontext->contextlevel == CONTEXT_SYSTEM && !has_capability('moodle/site:config', $repocontext)) {
            // Do not meet the requirements for the context system.
            return false;
        }

        return true;
    }

    /**
     * Return the name of this instance, can be overridden.
     *
     * @return string
     */
    public function get_name() {
        if ($name = $this->instance->name) {
            return $name;
        } else {
            return get_string('pluginname', 'repository_' . $this->get_typename());
        }
    }

    /**
     * Is this repository accessing private data?
     *
     * This function should return true for the repositories which access external private
     * data from a user. This is the case for repositories such as Dropbox, Google Docs or Box.net
     * which authenticate the user and then store the auth token.
     *
     * Of course, many repositories store 'private data', but we only want to set
     * contains_private_data() to repositories which are external to Moodle and shouldn't be accessed
     * to by the users having the capability to 'login as' someone else. For instance, the repository
     * 'Private files' is not considered as private because it's part of Moodle.
     *
     * You should not set contains_private_data() to true on repositories which allow different types
     * of instances as the levels other than 'user' are, by definition, not private. Also
     * the user instances will be protected when they need to.
     *
     * @return boolean True when the repository accesses private external data.
     * @since  2.5
     */
    public function contains_private_data() {
        return true;
    }

    /**
     * What kind of files will be in this repository?
     *
     * @return array return '*' means this repository support any files, otherwise
     *               return mimetypes of files, it can be an array
     */
    public function supported_filetypes() {
        // return array('text/plain', 'image/gif');
        return '*';
    }

    /**
     * Tells how the file can be picked from this repository
     *
     * Maximum value is FILE_INTERNAL | FILE_EXTERNAL | FILE_REFERENCE
     *
     * @return int
     */
    public function supported_returntypes() {
        return (FILE_INTERNAL | FILE_EXTERNAL);
    }

    /**
     * Provide repository instance information for Ajax
     *
     * @return stdClass
     */
    final public function get_meta() {
        global $CFG, $OUTPUT;
        $meta = new stdClass();
        $meta->id   = $this->id;
        $meta->name = format_string($this->get_name());
        $meta->type = $this->get_typename();
        $meta->icon = $OUTPUT->pix_url('icon', 'repository_'.$meta->type)->out(false);
        $meta->supported_types = file_get_typegroup('extension', $this->supported_filetypes());
        $meta->return_types = $this->supported_returntypes();
        $meta->sortorder = $this->options['sortorder'];
        return $meta;
    }

    /**
     * Create an instance for this plug-in
     *
     * @static
     * @param string $type the type of the repository
     * @param int $userid the user id
     * @param stdClass $context the context
     * @param array $params the options for this instance
     * @param int $readonly whether to create it readonly or not (defaults to not)
     * @return mixed
     */
    public static function create($type, $userid, $context, $params, $readonly=0) {
        global $CFG, $DB;
        $params = (array)$params;
        require_once($CFG->dirroot . '/repository/'. $type . '/lib.php');
        $classname = 'repository_' . $type;
        if ($repo = $DB->get_record('repository', array('type'=>$type))) {
            $record = new stdClass();
            $record->name = $params['name'];
            $record->typeid = $repo->id;
            $record->timecreated  = time();
            $record->timemodified = time();
            $record->contextid = $context->id;
            $record->readonly = $readonly;
            $record->userid    = $userid;
            $id = $DB->insert_record('repository_instances', $record);
            cache::make('core', 'repositories')->purge();
            $options = array();
            $configs = call_user_func($classname . '::get_instance_option_names');
            if (!empty($configs)) {
                foreach ($configs as $config) {
                    if (isset($params[$config])) {
                        $options[$config] = $params[$config];
                    } else {
                        $options[$config] = null;
                    }
                }
            }

            if (!empty($id)) {
                unset($options['name']);
                $instance = repository::get_instance($id);
                $instance->set_option($options);
                return $id;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

    /**
     * delete a repository instance
     *
     * @param bool $downloadcontents
     * @return bool
     */
    final public function delete($downloadcontents = false) {
        global $DB;
        if ($downloadcontents) {
            $this->convert_references_to_local();
        }
        cache::make('core', 'repositories')->purge();
        try {
            $DB->delete_records('repository_instances', array('id'=>$this->id));
            $DB->delete_records('repository_instance_config', array('instanceid'=>$this->id));
        } catch (dml_exception $ex) {
            return false;
        }
        return true;
    }

    /**
     * Delete all the instances associated to a context.
     *
     * This method is intended to be a callback when deleting
     * a course or a user to delete all the instances associated
     * to their context. The usual way to delete a single instance
     * is to use {@link self::delete()}.
     *
     * @param int $contextid context ID.
     * @param boolean $downloadcontents true to convert references to hard copies.
     * @return void
     */
    final public static function delete_all_for_context($contextid, $downloadcontents = true) {
        global $DB;
        $repoids = $DB->get_fieldset_select('repository_instances', 'id', 'contextid = :contextid', array('contextid' => $contextid));
        if ($downloadcontents) {
            foreach ($repoids as $repoid) {
                $repo = repository::get_repository_by_id($repoid, $contextid);
                $repo->convert_references_to_local();
            }
        }
        cache::make('core', 'repositories')->purge();
        $DB->delete_records_list('repository_instances', 'id', $repoids);
        $DB->delete_records_list('repository_instance_config', 'instanceid', $repoids);
    }

    /**
     * Hide/Show a repository
     *
     * @param string $hide
     * @return bool
     */
    final public function hide($hide = 'toggle') {
        global $DB;
        if ($entry = $DB->get_record('repository', array('id'=>$this->id))) {
            if ($hide === 'toggle' ) {
                if (!empty($entry->visible)) {
                    $entry->visible = 0;
                } else {
                    $entry->visible = 1;
                }
            } else {
                if (!empty($hide)) {
                    $entry->visible = 0;
                } else {
                    $entry->visible = 1;
                }
            }
            return $DB->update_record('repository', $entry);
        }
        return false;
    }

    /**
     * Save settings for repository instance
     * $repo->set_option(array('api_key'=>'f2188bde132', 'name'=>'dongsheng'));
     *
     * @param array $options settings
     * @return bool
     */
    public function set_option($options = array()) {
        global $DB;

        if (!empty($options['name'])) {
            $r = new stdClass();
            $r->id   = $this->id;
            $r->name = $options['name'];
            $DB->update_record('repository_instances', $r);
            unset($options['name']);
        }
        foreach ($options as $name=>$value) {
            if ($id = $DB->get_field('repository_instance_config', 'id', array('name'=>$name, 'instanceid'=>$this->id))) {
                $DB->set_field('repository_instance_config', 'value', $value, array('id'=>$id));
            } else {
                $config = new stdClass();
                $config->instanceid = $this->id;
                $config->name   = $name;
                $config->value  = $value;
                $DB->insert_record('repository_instance_config', $config);
            }
        }
        cache::make('core', 'repositories')->purge();
        return true;
    }

    /**
     * Get settings for repository instance.
     *
     * @param string $config a specific option to get.
     * @return mixed returns an array of options. If $config is not empty, then it returns that option,
     *               or null if the option does not exist.
     */
    public function get_option($config = '') {
        global $DB;
        $cache = cache::make('core', 'repositories');
        if (($entries = $cache->get('ops:'. $this->id)) === false) {
            $entries = $DB->get_records('repository_instance_config', array('instanceid' => $this->id));
            $cache->set('ops:'. $this->id, $entries);
        }

        $ret = array();
        foreach($entries as $entry) {
            $ret[$entry->name] = $entry->value;
        }

        if (!empty($config)) {
            if (isset($ret[$config])) {
                return $ret[$config];
            } else {
                return null;
            }
        } else {
            return $ret;
        }
    }

    /**
     * Filter file listing to display specific types
     *
     * @param array $value
     * @return bool
     */
    public function filter(&$value) {
        $accepted_types = optional_param_array('accepted_types', '', PARAM_RAW);
        if (isset($value['children'])) {
            if (!empty($value['children'])) {
                $value['children'] = array_filter($value['children'], array($this, 'filter'));
            }
            return true; // always return directories
        } else {
            if ($accepted_types == '*' or empty($accepted_types)
                or (is_array($accepted_types) and in_array('*', $accepted_types))) {
                return true;
            } else {
                foreach ($accepted_types as $ext) {
                    if (preg_match('#'.$ext.'$#i', $value['title'])) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Given a path, and perhaps a search, get a list of files.
     *
     * See details on {@link http://docs.moodle.org/dev/Repository_plugins}
     *
     * @param string $path this parameter can a folder name, or a identification of folder
     * @param string $page the page number of file list
     * @return array the list of files, including meta infomation, containing the following keys
     *           manage, url to manage url
     *           client_id
     *           login, login form
     *           repo_id, active repository id
     *           login_btn_action, the login button action
     *           login_btn_label, the login button label
     *           total, number of results
     *           perpage, items per page
     *           page
     *           pages, total pages
     *           issearchresult, is it a search result?
     *           list, file list
     *           path, current path and parent path
     */
    public function get_listing($path = '', $page = '') {
    }


    /**
     * Prepare the breadcrumb.
     *
     * @param array $breadcrumb contains each element of the breadcrumb.
     * @return array of breadcrumb elements.
     * @since 2.3.3
     */
    protected static function prepare_breadcrumb($breadcrumb) {
        global $OUTPUT;
        $foldericon = $OUTPUT->pix_url(file_folder_icon(24))->out(false);
        $len = count($breadcrumb);
        for ($i = 0; $i < $len; $i++) {
            if (is_array($breadcrumb[$i]) && !isset($breadcrumb[$i]['icon'])) {
                $breadcrumb[$i]['icon'] = $foldericon;
            } else if (is_object($breadcrumb[$i]) && !isset($breadcrumb[$i]->icon)) {
                $breadcrumb[$i]->icon = $foldericon;
            }
        }
        return $breadcrumb;
    }

    /**
     * Prepare the file/folder listing.
     *
     * @param array $list of files and folders.
     * @return array of files and folders.
     * @since 2.3.3
     */
    protected static function prepare_list($list) {
        global $OUTPUT;
        $foldericon = $OUTPUT->pix_url(file_folder_icon(24))->out(false);

        // Reset the array keys because non-numeric keys will create an object when converted to JSON.
        $list = array_values($list);

        $len = count($list);
        for ($i = 0; $i < $len; $i++) {
            if (is_object($list[$i])) {
                $file = (array)$list[$i];
                $converttoobject = true;
            } else {
                $file =& $list[$i];
                $converttoobject = false;
            }
            if (isset($file['size'])) {
                $file['size'] = (int)$file['size'];
                $file['size_f'] = display_size($file['size']);
            }
            if (isset($file['license']) && get_string_manager()->string_exists($file['license'], 'license')) {
                $file['license_f'] = get_string($file['license'], 'license');
            }
            if (isset($file['image_width']) && isset($file['image_height'])) {
                $a = array('width' => $file['image_width'], 'height' => $file['image_height']);
                $file['dimensions'] = get_string('imagesize', 'repository', (object)$a);
            }
            foreach (array('date', 'datemodified', 'datecreated') as $key) {
                if (!isset($file[$key]) && isset($file['date'])) {
                    $file[$key] = $file['date'];
                }
                if (isset($file[$key])) {
                    // must be UNIX timestamp
                    $file[$key] = (int)$file[$key];
                    if (!$file[$key]) {
                        unset($file[$key]);
                    } else {
                        $file[$key.'_f'] = userdate($file[$key], get_string('strftimedatetime', 'langconfig'));
                        $file[$key.'_f_s'] = userdate($file[$key], get_string('strftimedatetimeshort', 'langconfig'));
                    }
                }
            }
            $isfolder = (array_key_exists('children', $file) || (isset($file['type']) && $file['type'] == 'folder'));
            $filename = null;
            if (isset($file['title'])) {
                $filename = $file['title'];
            }
            else if (isset($file['fullname'])) {
                $filename = $file['fullname'];
            }
            if (!isset($file['mimetype']) && !$isfolder && $filename) {
                $file['mimetype'] = get_mimetype_description(array('filename' => $filename));
            }
            if (!isset($file['icon'])) {
                if ($isfolder) {
                    $file['icon'] = $foldericon;
                } else if ($filename) {
                    $file['icon'] = $OUTPUT->pix_url(file_extension_icon($filename, 24))->out(false);
                }
            }

            // Recursively loop over children.
            if (isset($file['children'])) {
                $file['children'] = self::prepare_list($file['children']);
            }

            // Convert the array back to an object.
            if ($converttoobject) {
                $list[$i] = (object)$file;
            }
        }
        return $list;
    }

    /**
     * Prepares list of files before passing it to AJAX, makes sure data is in the correct
     * format and stores formatted values.
     *
     * @param array|stdClass $listing result of get_listing() or search() or file_get_drafarea_files()
     * @return array
     */
    public static function prepare_listing($listing) {
        $wasobject = false;
        if (is_object($listing)) {
            $listing = (array) $listing;
            $wasobject = true;
        }

        // Prepare the breadcrumb, passed as 'path'.
        if (isset($listing['path']) && is_array($listing['path'])) {
            $listing['path'] = self::prepare_breadcrumb($listing['path']);
        }

        // Prepare the listing of objects.
        if (isset($listing['list']) && is_array($listing['list'])) {
            $listing['list'] = self::prepare_list($listing['list']);
        }

        // Convert back to an object.
        if ($wasobject) {
            $listing = (object) $listing;
        }
        return $listing;
    }

    /**
     * Search files in repository
     * When doing global search, $search_text will be used as
     * keyword.
     *
     * @param string $search_text search key word
     * @param int $page page
     * @return mixed see {@link repository::get_listing()}
     */
    public function search($search_text, $page = 0) {
        $list = array();
        $list['list'] = array();
        return false;
    }

    /**
     * Logout from repository instance
     * By default, this function will return a login form
     *
     * @return string
     */
    public function logout(){
        return $this->print_login();
    }

    /**
     * To check whether the user is logged in.
     *
     * @return bool
     */
    public function check_login(){
        return true;
    }


    /**
     * Show the login screen, if required
     *
     * @return string
     */
    public function print_login(){
        return $this->get_listing();
    }

    /**
     * Show the search screen, if required
     *
     * @return string
     */
    public function print_search() {
        global $PAGE;
        $renderer = $PAGE->get_renderer('core', 'files');
        return $renderer->repository_default_searchform();
    }

    /**
     * For oauth like external authentication, when external repository direct user back to moodle,
     * this function will be called to set up token and token_secret
     */
    public function callback() {
    }

    /**
     * is it possible to do glboal search?
     *
     * @return bool
     */
    public function global_search() {
        return false;
    }

    /**
     * Defines operations that happen occasionally on cron
     *
     * @return bool
     */
    public function cron() {
        return true;
    }

    /**
     * function which is run when the type is created (moodle administrator add the plugin)
     *
     * @return bool success or fail?
     */
    public static function plugin_init() {
        return true;
    }

    /**
     * Edit/Create Admin Settings Moodle form
     *
     * @param moodleform $mform Moodle form (passed by reference)
     * @param string $classname repository class name
     */
    public static function type_config_form($mform, $classname = 'repository') {
        $instnaceoptions = call_user_func(array($classname, 'get_instance_option_names'), $mform, $classname);
        if (empty($instnaceoptions)) {
            // this plugin has only one instance
            // so we need to give it a name
            // it can be empty, then moodle will look for instance name from language string
            $mform->addElement('text', 'pluginname', get_string('pluginname', 'repository'), array('size' => '40'));
            $mform->addElement('static', 'pluginnamehelp', '', get_string('pluginnamehelp', 'repository'));
            $mform->setType('pluginname', PARAM_TEXT);
        }
    }

    /**
     * Validate Admin Settings Moodle form
     *
     * @static
     * @param moodleform $mform Moodle form (passed by reference)
     * @param array $data array of ("fieldname"=>value) of submitted data
     * @param array $errors array of ("fieldname"=>errormessage) of errors
     * @return array array of errors
     */
    public static function type_form_validation($mform, $data, $errors) {
        return $errors;
    }


    /**
     * Edit/Create Instance Settings Moodle form
     *
     * @param moodleform $mform Moodle form (passed by reference)
     */
    public static function instance_config_form($mform) {
    }

    /**
     * Return names of the general options.
     * By default: no general option name
     *
     * @return array
     */
    public static function get_type_option_names() {
        return array('pluginname');
    }

    /**
     * Return names of the instance options.
     * By default: no instance option name
     *
     * @return array
     */
    public static function get_instance_option_names() {
        return array();
    }

    /**
     * Validate repository plugin instance form
     *
     * @param moodleform $mform moodle form
     * @param array $data form data
     * @param array $errors errors
     * @return array errors
     */
    public static function instance_form_validation($mform, $data, $errors) {
        return $errors;
    }

    /**
     * Create a shorten filename
     *
     * @param string $str filename
     * @param int $maxlength max file name length
     * @return string short filename
     */
    public function get_short_filename($str, $maxlength) {
        if (core_text::strlen($str) >= $maxlength) {
            return trim(core_text::substr($str, 0, $maxlength)).'...';
        } else {
            return $str;
        }
    }

    /**
     * Overwrite an existing file
     *
     * @param int $itemid
     * @param string $filepath
     * @param string $filename
     * @param string $newfilepath
     * @param string $newfilename
     * @return bool
     */
    public static function overwrite_existing_draftfile($itemid, $filepath, $filename, $newfilepath, $newfilename) {
        global $USER;
        $fs = get_file_storage();
        $user_context = context_user::instance($USER->id);
        if ($file = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $filepath, $filename)) {
            if ($tempfile = $fs->get_file($user_context->id, 'user', 'draft', $itemid, $newfilepath, $newfilename)) {
                // Remember original file source field.
                $source = @unserialize($file->get_source());
                // Remember the original sortorder.
                $sortorder = $file->get_sortorder();
                if ($tempfile->is_external_file()) {
                    // New file is a reference. Check that existing file does not have any other files referencing to it
                    if (isset($source->original) && $fs->search_references_count($source->original)) {
                        return (object)array('error' => get_string('errordoublereference', 'repository'));
                    }
                }
                // delete existing file to release filename
                $file->delete();
                // create new file
                $newfile = $fs->create_file_from_storedfile(array('filepath'=>$filepath, 'filename'=>$filename), $tempfile);
                // Preserve original file location (stored in source field) for handling references
                if (isset($source->original)) {
                    if (!($newfilesource = @unserialize($newfile->get_source()))) {
                        $newfilesource = new stdClass();
                    }
                    $newfilesource->original = $source->original;
                    $newfile->set_source(serialize($newfilesource));
                }
                $newfile->set_sortorder($sortorder);
                // remove temp file
                $tempfile->delete();
                return true;
            }
        }
        return false;
    }

    /**
     * Updates a file in draft filearea.
     *
     * This function can only update fields filepath, filename, author, license.
     * If anything (except filepath) is updated, timemodified is set to current time.
     * If filename or filepath is updated the file unconnects from it's origin
     * and therefore all references to it will be converted to copies when
     * filearea is saved.
     *
     * @param int $draftid
     * @param string $filepath path to the directory containing the file, or full path in case of directory
     * @param string $filename name of the file, or '.' in case of directory
     * @param array $updatedata array of fields to change (only filename, filepath, license and/or author can be updated)
     * @throws moodle_exception if for any reason file can not be updated (file does not exist, target already exists, etc.)
     */
    public static function update_draftfile($draftid, $filepath, $filename, $updatedata) {
        global $USER;
        $fs = get_file_storage();
        $usercontext = context_user::instance($USER->id);
        // make sure filename and filepath are present in $updatedata
        $updatedata = $updatedata + array('filepath' => $filepath, 'filename' => $filename);
        $filemodified = false;
        if (!$file = $fs->get_file($usercontext->id, 'user', 'draft', $draftid, $filepath, $filename)) {
            if ($filename === '.') {
                throw new moodle_exception('foldernotfound', 'repository');
            } else {
                throw new moodle_exception('filenotfound', 'error');
            }
        }
        if (!$file->is_directory()) {
            // This is a file
            if ($updatedata['filepath'] !== $filepath || $updatedata['filename'] !== $filename) {
                // Rename/move file: check that target file name does not exist.
                if ($fs->file_exists($usercontext->id, 'user', 'draft', $draftid, $updatedata['filepath'], $updatedata['filename'])) {
                    throw new moodle_exception('fileexists', 'repository');
                }
                if (($filesource = @unserialize($file->get_source())) && isset($filesource->original)) {
                    unset($filesource->original);
                    $file->set_source(serialize($filesource));
                }
                $file->rename($updatedata['filepath'], $updatedata['filename']);
                // timemodified is updated only when file is renamed and not updated when file is moved.
                $filemodified = $filemodified || ($updatedata['filename'] !== $filename);
            }
            if (array_key_exists('license', $updatedata) && $updatedata['license'] !== $file->get_license()) {
                // Update license and timemodified.
                $file->set_license($updatedata['license']);
                $filemodified = true;
            }
            if (array_key_exists('author', $updatedata) && $updatedata['author'] !== $file->get_author()) {
                // Update author and timemodified.
                $file->set_author($updatedata['author']);
                $filemodified = true;
            }
            // Update timemodified:
            if ($filemodified) {
                $file->set_timemodified(time());
            }
        } else {
            // This is a directory - only filepath can be updated for a directory (it was moved).
            if ($updatedata['filepath'] === $filepath) {
                // nothing to update
                return;
            }
            if ($fs->file_exists($usercontext->id, 'user', 'draft', $draftid, $updatedata['filepath'], '.')) {
                // bad luck, we can not rename if something already exists there
                throw new moodle_exception('folderexists', 'repository');
            }
            $xfilepath = preg_quote($filepath, '|');
            if (preg_match("|^$xfilepath|", $updatedata['filepath'])) {
                // we can not move folder to it's own subfolder
                throw new moodle_exception('folderrecurse', 'repository');
            }

            // If directory changed the name, update timemodified.
            $filemodified = (basename(rtrim($file->get_filepath(), '/')) !== basename(rtrim($updatedata['filepath'], '/')));

            // Now update directory and all children.
            $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftid);
            foreach ($files as $f) {
                if (preg_match("|^$xfilepath|", $f->get_filepath())) {
                    $path = preg_replace("|^$xfilepath|", $updatedata['filepath'], $f->get_filepath());
                    if (($filesource = @unserialize($f->get_source())) && isset($filesource->original)) {
                        // unset original so the references are not shown any more
                        unset($filesource->original);
                        $f->set_source(serialize($filesource));
                    }
                    $f->rename($path, $f->get_filename());
                    if ($filemodified && $f->get_filepath() === $updatedata['filepath'] && $f->get_filename() === $filename) {
                        $f->set_timemodified(time());
                    }
                }
            }
        }
    }

    /**
     * Delete a temp file from draft area
     *
     * @param int $draftitemid
     * @param string $filepath
     * @param string $filename
     * @return bool
     */
    public static function delete_tempfile_from_draft($draftitemid, $filepath, $filename) {
        global $USER;
        $fs = get_file_storage();
        $user_context = context_user::instance($USER->id);
        if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $filepath, $filename)) {
            $file->delete();
            return true;
        } else {
            return false;
        }
    }

    /**
     * Find all external files in this repo and import them
     */
    public function convert_references_to_local() {
        $fs = get_file_storage();
        $files = $fs->get_external_files($this->id);
        foreach ($files as $storedfile) {
            $fs->import_external_file($storedfile);
        }
    }

    /**
     * Method deprecated, cache is handled by MUC now.
     * @deprecated since 2.6
     */
    public static function reset_caches() {
        debugging('Function repository::reset_caches() is deprecated.', DEBUG_DEVELOPER);
    }

    /**
     * Method deprecated
     * @deprecated since 2.6
     * @see repository::sync_reference()
     */
    public static function sync_external_file($file, $resetsynchistory = false) {
        debugging('Function repository::sync_external_file() is deprecated.',
                DEBUG_DEVELOPER);
        if ($resetsynchistory || !$file || !$file->get_repository_id() ||
                !($repository = self::get_repository_by_id($file->get_repository_id(), SYSCONTEXTID))) {
            return false;
        }
        return $repository->sync_reference($file);
    }

    /**
     * Performs synchronisation of an external file if the previous one has expired.
     *
     * This function must be implemented for external repositories supporting
     * FILE_REFERENCE, it is called for existing aliases when their filesize,
     * contenthash or timemodified are requested. It is not called for internal
     * repositories (see {@link repository::has_moodle_files()}), references to
     * internal files are updated immediately when source is modified.
     *
     * Referenced files may optionally keep their content in Moodle filepool (for
     * thumbnail generation or to be able to serve cached copy). In this
     * case both contenthash and filesize need to be synchronized. Otherwise repositories
     * should use contenthash of empty file and correct filesize in bytes.
     *
     * Note that this function may be run for EACH file that needs to be synchronised at the
     * moment. If anything is being downloaded or requested from external sources there
     * should be a small timeout. The synchronisation is performed to update the size of
     * the file and/or to update image and re-generated image preview. There is nothing
     * fatal if syncronisation fails but it is fatal if syncronisation takes too long
     * and hangs the script generating a page.
     *
     * Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or
     * $file->get_timemodified() make sure that recursion does not happen.
     *
     * Called from {@link stored_file::sync_external_file()}
     *
     * @uses stored_file::set_missingsource()
     * @uses stored_file::set_synchronized()
     * @param stored_file $file
     * @return bool false when file does not need synchronisation, true if it was synchronised
     */
    public function sync_reference(stored_file $file) {
        if ($file->get_repository_id() != $this->id) {
            // This should not really happen because the function can be called from stored_file only.
            return false;
        }

        if ($this->has_moodle_files()) {
            // References to local files need to be synchronised only once.
            // Later they will be synchronised automatically when the source is changed.
            if ($file->get_referencelastsync()) {
                return false;
            }
            $fs = get_file_storage();
            $params = file_storage::unpack_reference($file->get_reference(), true);
            if (!is_array($params) || !($storedfile = $fs->get_file($params['contextid'],
                    $params['component'], $params['filearea'], $params['itemid'], $params['filepath'],
                    $params['filename']))) {
                $file->set_missingsource();
            } else {
                $file->set_synchronized($storedfile->get_contenthash(), $storedfile->get_filesize());
            }
            return true;
        }

        // Backward compatibility (Moodle 2.3-2.5) implementation that calls
        // methods repository::get_reference_file_lifetime(), repository::sync_individual_file()
        // and repository::get_file_by_reference(). These methods are removed from the
        // base repository class but may still be implemented by the child classes.

        // THIS IS NOT A GOOD EXAMPLE of implementation. For good examples see the overwriting methods.

        if (!method_exists($this, 'get_file_by_reference')) {
            // Function get_file_by_reference() is not implemented. No synchronisation.
            return false;
        }

        // Check if the previous sync result is still valid.
        if (method_exists($this, 'get_reference_file_lifetime')) {
            $lifetime = $this->get_reference_file_lifetime($file->get_reference());
        } else {
            // Default value that was hardcoded in Moodle 2.3 - 2.5.
            $lifetime =  60 * 60 * 24;
        }
        if (($lastsynced = $file->get_referencelastsync()) && $lastsynced + $lifetime >= time()) {
            return false;
        }

        $cache = cache::make('core', 'repositories');
        if (($lastsyncresult = $cache->get('sync:'.$file->get_referencefileid())) !== false) {
            if ($lastsyncresult === true) {
                // We are in the process of synchronizing this reference.
                // Avoid recursion when calling $file->get_filesize() and $file->get_contenthash().
                return false;
            } else {
                // We have synchronised the same reference inside this request already.
                // It looks like the object $file was created before the synchronisation and contains old data.
                if (!empty($lastsyncresult['missing'])) {
                    $file->set_missingsource();
                } else {
                    $cache->set('sync:'.$file->get_referencefileid(), true);
                    if ($file->get_contenthash() != $lastsyncresult['contenthash'] ||
                            $file->get_filesize() != $lastsyncresult['filesize']) {
                        $file->set_synchronized($lastsyncresult['contenthash'], $lastsyncresult['filesize']);
                    }
                    $cache->set('sync:'.$file->get_referencefileid(), $lastsyncresult);
                }
                return true;
            }
        }

        // Weird function sync_individual_file() that was present in API in 2.3 - 2.5, default value was true.
        if (method_exists($this, 'sync_individual_file') && !$this->sync_individual_file($file)) {
            return false;
        }

        // Set 'true' into the cache to indicate that file is in the process of synchronisation.
        $cache->set('sync:'.$file->get_referencefileid(), true);

        // Create object with the structure that repository::get_file_by_reference() expects.
        $reference = new stdClass();
        $reference->id = $file->get_referencefileid();
        $reference->reference = $file->get_reference();
        $reference->referencehash = sha1($file->get_reference());
        $reference->lastsync = $file->get_referencelastsync();
        $reference->lifetime = $lifetime;

        $fileinfo = $this->get_file_by_reference($reference);

        $contenthash = null;
        $filesize = null;
        $fs = get_file_storage();
        if (!empty($fileinfo->filesize)) {
            // filesize returned
            if (!empty($fileinfo->contenthash) && $fs->content_exists($fileinfo->contenthash)) {
                // contenthash is specified and valid
                $contenthash = $fileinfo->contenthash;
            } else if ($fileinfo->filesize == $file->get_filesize()) {
                // we don't know the new contenthash but the filesize did not change,
                // assume the contenthash did not change either
                $contenthash = $file->get_contenthash();
            } else {
                // we can't save empty contenthash so generate contenthash from empty string
                list($contenthash, $unused1, $unused2) = $fs->add_string_to_pool('');
            }
            $filesize = $fileinfo->filesize;
        } else if (!empty($fileinfo->filepath)) {
            // File path returned
            list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo->filepath);
        } else if (!empty($fileinfo->handle) && is_resource($fileinfo->handle)) {
            // File handle returned
            $contents = '';
            while (!feof($fileinfo->handle)) {
                $contents .= fread($fileinfo->handle, 8192);
            }
            fclose($fileinfo->handle);
            list($contenthash, $filesize, $newfile) = $fs->add_string_to_pool($contents);
        } else if (isset($fileinfo->content)) {
            // File content returned
            list($contenthash, $filesize, $newfile) = $fs->add_string_to_pool($fileinfo->content);
        }

        if (!isset($contenthash) or !isset($filesize)) {
            $file->set_missingsource(null);
            $cache->set('sync:'.$file->get_referencefileid(), array('missing' => true));
        } else {
            // update files table
            $file->set_synchronized($contenthash, $filesize);
            $cache->set('sync:'.$file->get_referencefileid(),
                    array('contenthash' => $contenthash, 'filesize' => $filesize));
        }

        return true;
    }

    /**
     * Build draft file's source field
     *
     * {@link file_restore_source_field_from_draft_file()}
     * XXX: This is a hack for file manager (MDL-28666)
     * For newly created  draft files we have to construct
     * source filed in php serialized data format.
     * File manager needs to know the original file information before copying
     * to draft area, so we append these information in mdl_files.source field
     *
     * @param string $source
     * @return string serialised source field
     */
    public static function build_source_field($source) {
        $sourcefield = new stdClass;
        $sourcefield->source = $source;
        return serialize($sourcefield);
    }

    /**
     * Prepares the repository to be cached. Implements method from cacheable_object interface.
     *
     * @return array
     */
    public function prepare_to_cache() {
        return array(
            'class' => get_class($this),
            'id' => $this->id,
            'ctxid' => $this->context->id,
            'options' => $this->options,
            'readonly' => $this->readonly
        );
    }

    /**
     * Restores the repository from cache. Implements method from cacheable_object interface.
     *
     * @return array
     */
    public static function wake_from_cache($data) {
        $classname = $data['class'];
        return new $classname($data['id'], $data['ctxid'], $data['options'], $data['readonly']);
    }

    /**
     * Gets a file relative to this file in the repository and sends it to the browser.
     * Used to allow relative file linking within a repository without creating file records
     * for linked files
     *
     * Repositories that overwrite this must be very careful - see filesystem repository for example.
     *
     * @param stored_file $mainfile The main file we are trying to access relative files for.
     * @param string $relativepath the relative path to the file we are trying to access.
     *
     */
    public function send_relative_file(stored_file $mainfile, $relativepath) {
        // This repository hasn't implemented this so send_file_not_found.
        send_file_not_found();
    }

    /**
     * helper function to check if the repository supports send_relative_file.
     *
     * @return true|false
     */
    public function supports_relative_file() {
        return false;
    }
}

/**
 * Exception class for repository api
 *
 * @since 2.0
 * @package   core_repository
 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class repository_exception extends moodle_exception {
}

/**
 * This is a class used to define a repository instance form
 *
 * @since 2.0
 * @package   core_repository
 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
final class repository_instance_form extends moodleform {
    /** @var stdClass repository instance */
    protected $instance;
    /** @var string repository plugin type */
    protected $plugin;

    /**
     * Added defaults to moodle form
     */
    protected function add_defaults() {
        $mform =& $this->_form;
        $strrequired = get_string('required');

        $mform->addElement('hidden', 'edit',  ($this->instance) ? $this->instance->id : 0);
        $mform->setType('edit', PARAM_INT);
        $mform->addElement('hidden', 'new',   $this->plugin);
        $mform->setType('new', PARAM_ALPHANUMEXT);
        $mform->addElement('hidden', 'plugin', $this->plugin);
        $mform->setType('plugin', PARAM_PLUGIN);
        $mform->addElement('hidden', 'typeid', $this->typeid);
        $mform->setType('typeid', PARAM_INT);
        $mform->addElement('hidden', 'contextid', $this->contextid);
        $mform->setType('contextid', PARAM_INT);

        $mform->addElement('text', 'name', get_string('name'), 'maxlength="100" size="30"');
        $mform->addRule('name', $strrequired, 'required', null, 'client');
        $mform->setType('name', PARAM_TEXT);
    }

    /**
     * Define moodle form elements
     */
    public function definition() {
        global $CFG;
        // type of plugin, string
        $this->plugin = $this->_customdata['plugin'];
        $this->typeid = $this->_customdata['typeid'];
        $this->contextid = $this->_customdata['contextid'];
        $this->instance = (isset($this->_customdata['instance'])
                && is_subclass_of($this->_customdata['instance'], 'repository'))
            ? $this->_customdata['instance'] : null;

        $mform =& $this->_form;

        $this->add_defaults();

        // Add instance config options.
        $result = repository::static_function($this->plugin, 'instance_config_form', $mform);
        if ($result === false) {
            // Remove the name element if no other config options.
            $mform->removeElement('name');
        }
        if ($this->instance) {
            $data = array();
            $data['name'] = $this->instance->name;
            if (!$this->instance->readonly) {
                // and set the data if we have some.
                foreach ($this->instance->get_instance_option_names() as $config) {
                    if (!empty($this->instance->options[$config])) {
                        $data[$config] = $this->instance->options[$config];
                     } else {
                        $data[$config] = '';
                     }
                }
            }
            $this->set_data($data);
        }

        if ($result === false) {
            $mform->addElement('cancel');
        } else {
            $this->add_action_buttons(true, get_string('save','repository'));
        }
    }

    /**
     * Validate moodle form data
     *
     * @param array $data form data
     * @param array $files files in form
     * @return array errors
     */
    public function validation($data, $files) {
        global $DB;
        $errors = array();
        $plugin = $this->_customdata['plugin'];
        $instance = (isset($this->_customdata['instance'])
                && is_subclass_of($this->_customdata['instance'], 'repository'))
            ? $this->_customdata['instance'] : null;

        if (!$instance) {
            $errors = repository::static_function($plugin, 'instance_form_validation', $this, $data, $errors);
        } else {
            $errors = $instance->instance_form_validation($this, $data, $errors);
        }

        $sql = "SELECT count('x')
                  FROM {repository_instances} i, {repository} r
                 WHERE r.type=:plugin AND r.id=i.typeid AND i.name=:name AND i.contextid=:contextid";
        $params = array('name' => $data['name'], 'plugin' => $this->plugin, 'contextid' => $this->contextid);
        if ($instance) {
            $sql .= ' AND i.id != :instanceid';
            $params['instanceid'] = $instance->id;
        }
        if ($DB->count_records_sql($sql, $params) > 0) {
            $errors['name'] = get_string('erroruniquename', 'repository');
        }

        return $errors;
    }
}

/**
 * This is a class used to define a repository type setting form
 *
 * @since 2.0
 * @package   core_repository
 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
final class repository_type_form extends moodleform {
    /** @var stdClass repository instance */
    protected $instance;
    /** @var string repository plugin name */
    protected $plugin;
    /** @var string action */
    protected $action;

    /**
     * Definition of the moodleform
     */
    public function definition() {
        global $CFG;
        // type of plugin, string
        $this->plugin = $this->_customdata['plugin'];
        $this->instance = (isset($this->_customdata['instance'])
                && is_a($this->_customdata['instance'], 'repository_type'))
            ? $this->_customdata['instance'] : null;

        $this->action = $this->_customdata['action'];
        $this->pluginname = $this->_customdata['pluginname'];
        $mform =& $this->_form;
        $strrequired = get_string('required');

        $mform->addElement('hidden', 'action', $this->action);
        $mform->setType('action', PARAM_TEXT);
        $mform->addElement('hidden', 'repos', $this->plugin);
        $mform->setType('repos', PARAM_PLUGIN);

        // let the plugin add its specific fields
        $classname = 'repository_' . $this->plugin;
        require_once($CFG->dirroot . '/repository/' . $this->plugin . '/lib.php');
        //add "enable course/user instances" checkboxes if multiple instances are allowed
        $instanceoptionnames = repository::static_function($this->plugin, 'get_instance_option_names');

        $result = call_user_func(array($classname, 'type_config_form'), $mform, $classname);

        if (!empty($instanceoptionnames)) {
            $sm = get_string_manager();
            $component = 'repository';
            if ($sm->string_exists('enablecourseinstances', 'repository_' . $this->plugin)) {
                $component .= ('_' . $this->plugin);
            }
            $mform->addElement('checkbox', 'enablecourseinstances', get_string('enablecourseinstances', $component));
            $mform->setType('enablecourseinstances', PARAM_BOOL);

            $component = 'repository';
            if ($sm->string_exists('enableuserinstances', 'repository_' . $this->plugin)) {
                $component .= ('_' . $this->plugin);
            }
            $mform->addElement('checkbox', 'enableuserinstances', get_string('enableuserinstances', $component));
            $mform->setType('enableuserinstances', PARAM_BOOL);
        }

        // set the data if we have some.
        if ($this->instance) {
            $data = array();
            $option_names = call_user_func(array($classname,'get_type_option_names'));
            if (!empty($instanceoptionnames)){
                $option_names[] = 'enablecourseinstances';
                $option_names[] = 'enableuserinstances';
            }

            $instanceoptions = $this->instance->get_options();
            foreach ($option_names as $config) {
                if (!empty($instanceoptions[$config])) {
                    $data[$config] = $instanceoptions[$config];
                } else {
                    $data[$config] = '';
                }
            }
            // XXX: set plugin name for plugins which doesn't have muliti instances
            if (empty($instanceoptionnames)){
                $data['pluginname'] = $this->pluginname;
            }
            $this->set_data($data);
        }

        $this->add_action_buttons(true, get_string('save','repository'));
    }

    /**
     * Validate moodle form data
     *
     * @param array $data moodle form data
     * @param array $files
     * @return array errors
     */
    public function validation($data, $files) {
        $errors = array();
        $plugin = $this->_customdata['plugin'];
        $instance = (isset($this->_customdata['instance'])
                && is_subclass_of($this->_customdata['instance'], 'repository'))
            ? $this->_customdata['instance'] : null;
        if (!$instance) {
            $errors = repository::static_function($plugin, 'type_form_validation', $this, $data, $errors);
        } else {
            $errors = $instance->type_form_validation($this, $data, $errors);
        }

        return $errors;
    }
}

/**
 * Generate all options needed by filepicker
 *
 * @param array $args including following keys
 *          context
 *          accepted_types
 *          return_types
 *
 * @return array the list of repository instances, including meta infomation, containing the following keys
 *          externallink
 *          repositories
 *          accepted_types
 */
function initialise_filepicker($args) {
    global $CFG, $USER, $PAGE, $OUTPUT;
    static $templatesinitialized = array();
    require_once($CFG->libdir . '/licenselib.php');

    $return = new stdClass();
    $licenses = array();
    if (!empty($CFG->licenses)) {
        $array = explode(',', $CFG->licenses);
        foreach ($array as $license) {
            $l = new stdClass();
            $l->shortname = $license;
            $l->fullname = get_string($license, 'license');
            $licenses[] = $l;
        }
    }
    if (!empty($CFG->sitedefaultlicense)) {
        $return->defaultlicense = $CFG->sitedefaultlicense;
    }

    $return->licenses = $licenses;

    $return->author = fullname($USER);

    if (empty($args->context)) {
        $context = $PAGE->context;
    } else {
        $context = $args->context;
    }
    $disable_types = array();
    if (!empty($args->disable_types)) {
        $disable_types = $args->disable_types;
    }

    $user_context = context_user::instance($USER->id);

    list($context, $course, $cm) = get_context_info_array($context->id);
    $contexts = array($user_context, context_system::instance());
    if (!empty($course)) {
        // adding course context
        $contexts[] = context_course::instance($course->id);
    }
    $externallink = (int)get_config(null, 'repositoryallowexternallinks');
    $repositories = repository::get_instances(array(
        'context'=>$contexts,
        'currentcontext'=> $context,
        'accepted_types'=>$args->accepted_types,
        'return_types'=>$args->return_types,
        'disable_types'=>$disable_types
    ));

    $return->repositories = array();

    if (empty($externallink)) {
        $return->externallink = false;
    } else {
        $return->externallink = true;
    }

    $return->userprefs = array();
    $return->userprefs['recentrepository'] = get_user_preferences('filepicker_recentrepository', '');
    $return->userprefs['recentlicense'] = get_user_preferences('filepicker_recentlicense', '');
    $return->userprefs['recentviewmode'] = get_user_preferences('filepicker_recentviewmode', '');

    user_preference_allow_ajax_update('filepicker_recentrepository', PARAM_INT);
    user_preference_allow_ajax_update('filepicker_recentlicense', PARAM_SAFEDIR);
    user_preference_allow_ajax_update('filepicker_recentviewmode', PARAM_INT);


    // provided by form element
    $return->accepted_types = file_get_typegroup('extension', $args->accepted_types);
    $return->return_types = $args->return_types;
    $templates = array();
    foreach ($repositories as $repository) {
        $meta = $repository->get_meta();
        // Please note that the array keys for repositories are used within
        // JavaScript a lot, the key NEEDS to be the repository id.
        $return->repositories[$repository->id] = $meta;
        // Register custom repository template if it has one
        if(method_exists($repository, 'get_upload_template') && !array_key_exists('uploadform_' . $meta->type, $templatesinitialized)) {
            $templates['uploadform_' . $meta->type] = $repository->get_upload_template();
            $templatesinitialized['uploadform_' . $meta->type] = true;
        }
    }
    if (!array_key_exists('core', $templatesinitialized)) {
        // we need to send each filepicker template to the browser just once
        $fprenderer = $PAGE->get_renderer('core', 'files');
        $templates = array_merge($templates, $fprenderer->filepicker_js_templates());
        $templatesinitialized['core'] = true;
    }
    if (sizeof($templates)) {
        $PAGE->requires->js_init_call('M.core_filepicker.set_templates', array($templates), true);
    }
    return $return;
}
Back to Top