ld-course-progress.php
91.4 KB
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
<?php
/**
* Course Progress Functions
*
* @since 2.1.0
*
* @package LearnDash\Course
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// cspell:ignore accessable .
/**
* Outputs the HTML output to mark a course complete.
*
* Must meet requirements of course to mark the course as complete.
*
* @since 2.1.0
*
* @param WP_Post $post The `WP_Post` lesson or topic object.
* @param array $atts Optional. An array of attributes to mark course complete. Default empty array.
*
* @return string HTML output to mark course complete
*/
function learndash_mark_complete( $post, $atts = array() ) {
if ( ! is_user_logged_in() ) {
return '';
}
$user_id = get_current_user_id();
if ( isset( $_POST['sfwd_mark_complete'] ) && isset( $_POST['post'] ) && intval( $_POST['post'] ) == $post->ID ) {
return '';
}
$bypass_course_limits_admin_users = learndash_can_user_bypass( $user_id, 'learndash_course_progression', $post->ID, $post );
/**
* Bypass prerequisites.
*
* @since 2.4.0
*
* @param boolean $bypass True/False is user is allowed to bypass.
* @param integer $user_id User ID.
* @param integer $post_id Post ID.
* @param object $post WP_Post instance
*/
$bypass_course_limits_admin_users = apply_filters( 'learndash_prerequities_bypass', $bypass_course_limits_admin_users, $user_id, $post->ID, $post ); // cspell:disable-line -- prerequities are prerequisites...
$course_id = learndash_get_course_id( $post->ID );
if ( ( learndash_lesson_progression_enabled() ) && ( ! $bypass_course_limits_admin_users ) ) {
if ( ! sfwd_lms_has_access( $course_id, $user_id ) ) {
return '';
}
// Check Course Prerequisites.
if ( ! learndash_is_course_prerequities_completed( $course_id, $user_id ) ) { // cspell:disable-line -- prerequities are prerequisites...
return '';
}
if ( ! learndash_check_user_course_points_access( $course_id, $user_id ) ) {
return '';
}
$step_quiz_list = learndash_get_lesson_quiz_list( $post->ID, $user_id, $course_id );
if ( ! empty( $step_quiz_list ) ) {
foreach ( $step_quiz_list as $quiz ) {
if ( 'notcompleted' === $quiz['status'] ) {
return '';
}
}
}
if ( 'sfwd-lessons' === $post->post_type ) {
$progress = learndash_get_course_progress( $user_id, $post->ID );
if ( ! empty( $progress['this']->completed ) ) {
/** This filter is documented in includes/class-ld-cpt-instance.php */
if ( ! apply_filters( 'learndash_previous_step_completed', false, $progress['this']->ID, $user_id ) ) {
return learndash_show_mark_incomplete( $post, $atts );
}
}
if ( ! empty( $progress['prev'] ) && empty( $progress['prev']->completed ) && learndash_lesson_progression_enabled() ) {
/** This filter is documented in includes/class-ld-cpt-instance.php */
if ( ! apply_filters( 'learndash_previous_step_completed', false, $progress['prev']->ID, $user_id ) ) {
return '';
}
}
if ( ! learndash_lesson_topics_completed( $post->ID ) ) {
/** This filter is documented in includes/class-ld-cpt-instance.php */
if ( ! apply_filters( 'learndash_previous_step_completed', false, $post->ID, $user_id ) ) {
return '';
}
}
}
if ( 'sfwd-topic' === $post->post_type ) {
$progress = learndash_get_course_progress( $user_id, $post->ID );
if ( ! empty( $progress['this']->completed ) ) {
/** This filter is documented in includes/class-ld-cpt-instance.php */
if ( ! apply_filters( 'learndash_previous_step_completed', false, $progress['this']->ID, $user_id ) ) {
return learndash_show_mark_incomplete( $post, $atts );
}
}
if ( ! empty( $progress['prev'] ) && empty( $progress['prev']->completed ) && learndash_lesson_progression_enabled() ) {
/** This filter is documented in includes/class-ld-cpt-instance.php */
if ( ! apply_filters( 'learndash_previous_step_completed', false, $progress['prev']->ID, $user_id ) ) {
return '';
}
}
if ( learndash_lesson_progression_enabled() ) {
if ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' ) == 'yes' ) {
$lesson_id = learndash_course_get_single_parent_step( $course_id, $post->ID );
} else {
$lesson_id = learndash_get_setting( $post, 'lesson' );
}
$lesson = get_post( $lesson_id );
if ( ! learndash_is_previous_complete( $lesson ) ) {
/** This filter is documented in includes/class-ld-cpt-instance.php */
if ( ! apply_filters( 'learndash_previous_step_completed', false, $lesson->ID, $user_id ) ) {
return '';
}
}
}
}
$previous_item_id = learndash_user_progress_get_previous_incomplete_step( $user_id, $course_id, $post->ID );
if ( ! empty( $previous_item_id ) && ( $previous_item_id !== $post->ID ) ) {
return '';
}
} else {
$progress = learndash_get_course_progress( $user_id, $post->ID );
if ( ! empty( $progress['this']->completed ) ) {
return '';
}
}
if ( learndash_lesson_hasassignments( $post ) ) { // cspell:disable-line.
global $learndash_assignment_upload_message;
$ret = SFWD_LMS::get_template(
'learndash_lesson_assignment_upload_form.php',
array(
'course_step_post' => $post,
'user_id' => $user_id,
'assignment_upload_error_message' => $learndash_assignment_upload_message,
)
);
if ( ! is_null( $ret ) ) {
return $ret;
}
if ( ( ! learndash_is_admin_user( $user_id ) ) || ( ! $bypass_course_limits_admin_users ) ) {
$assignments = learndash_get_user_assignments( $post->ID, $user_id, $course_id );
if ( empty( $assignments ) ) {
return false;
} else {
foreach ( $assignments as $assignment ) {
if ( ! learndash_is_assignment_approved_by_meta( $assignment->ID ) ) {
return false;
}
}
}
}
}
$return = '';
$button_disabled = '';
$time = 0;
$time_value = learndash_forced_lesson_time( $post );
if ( ! empty( $time_value ) ) {
$time = learndash_convert_lesson_time_time( $time_value );
}
if ( ( ! learndash_is_admin_user( $user_id ) ) || ( ! $bypass_course_limits_admin_users ) ) {
if ( ! empty( $time ) ) {
$time_cookie_key = learndash_forced_lesson_time_cookie_key( $post );
if ( ! empty( $time_cookie_key ) ) {
/**
* Note this is not a 100% check. We are only checking if the cookie
* key exists and is zero. But this cookie could have been set from
* external.
*/
if ( ( isset( $_COOKIE[ 'learndash_timer_cookie_' . $time_cookie_key ] ) ) && ( '0' === $_COOKIE[ 'learndash_timer_cookie_' . $time_cookie_key ] ) ) {
$time = 0;
}
}
if ( ! empty( $time ) ) {
// Set the mark complete button disabled.
$button_disabled = " disabled='disabled' ";
wp_enqueue_script(
'jquery-cookie',
plugins_url( 'js/jquery.cookie' . learndash_min_asset() . '.js', WPPROQUIZ_FILE ),
array( 'jquery' ),
'1.4.0',
true
);
global $learndash_assets_loaded;
$learndash_assets_loaded['scripts']['jquery-cookie'] = __FUNCTION__;
}
}
}
/**
* Filters attributes for mark a course complete form.
*
* @since 3.0.0
*
* @param array $attributes An array of form, button, and timer attributes to override id and class.
* @param WP_Post $post WP_Post object being displayed.
*/
$atts = apply_filters( 'learndash_mark_complete_form_atts', $atts, $post );
if ( isset( $atts['form']['id'] ) ) {
$form_id = ' id="' . esc_attr( $atts['form']['id'] ) . '" ';
} else {
$form_id = '';
}
if ( isset( $atts['form']['class'] ) ) {
$form_class = ' class="sfwd-mark-complete ' . esc_attr( $atts['form']['class'] ) . '" ';
} else {
$form_class = ' class="sfwd-mark-complete" ';
}
if ( isset( $atts['button']['id'] ) ) {
$button_id = ' id="' . esc_attr( $atts['button']['id'] ) . '" ';
} else {
$button_id = '';
}
if ( isset( $atts['button']['class'] ) ) {
$button_class = ' class="learndash_mark_complete_button ' . esc_attr( $atts['button']['class'] ) . '" ';
} else {
$button_class = ' class="learndash_mark_complete_button" ';
}
$form_fields = '<input type="hidden" value="' . $post->ID . '" name="post" />
<input type="hidden" value="' . learndash_get_course_id( $post->ID ) . '" name="course_id" />
<input type="hidden" value="' . wp_create_nonce( 'sfwd_mark_complete_' . get_current_user_id() . '_' . $post->ID ) . '" name="sfwd_mark_complete" />
<input type="submit" ' . $button_id . ' value="' . LearnDash_Custom_Label::get_label( 'button_mark_complete' ) . '" ' . $button_class . ' ' . $button_disabled . '/>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Method escapes output
/**
* Filters form fields to mark a course complete.
*
* @since 3.0.0
*
* @param string $form_fields HTML output for course mark complete form fields.
* @param WP_Post $post WP_Post object being displayed.
*/
$form_fields = apply_filters( 'learndash_mark_complete_form_fields', $form_fields, $post );
$return .= '<form ' . $form_id . ' ' . $form_class . ' method="post" action="">' . $form_fields . '</form>';
if ( ( ! learndash_is_admin_user( $user_id ) ) || ( ! $bypass_course_limits_admin_users ) ) {
if ( ! empty( $time ) ) {
if ( isset( $atts['timer']['id'] ) ) {
$timer_id = ' id="' . esc_attr( $atts['timer']['id'] ) . '" ';
} else {
$timer_id = '';
}
$timer_class = ' class="learndash_timer';
if ( isset( $atts['timer']['class'] ) ) {
$timer_class .= ' ' . esc_attr( $atts['timer']['class'] );
}
$timer_class .= '" ';
$return .= '<span ' . $timer_id . ' ' . $timer_class . ' data-timer-seconds="' . $time . '" data-button="input.learndash_mark_complete_button" data-cookie-key="' . $time_cookie_key . '"></span>';
}
}
/**
* Filters HTML output to mark a course completion.
*
* @since 2.1.0
*
* @param string $return HTML output to mark course complete.
* @param WP_Post $post WP_Post object being displayed.
*/
return apply_filters( 'learndash_mark_complete', $return, $post );
}
/**
* Handles the AJAX output to mark a quiz complete.
*
* @since 2.1.0
*
* @global WP_Post $post Global post object.
*
* @param int|null $quiz_id Optional. Quiz ID. Default null.
* @param int|null $lesson_id Optional. Lesson ID. Default null.
*/
function learndash_ajax_mark_complete( $quiz_id = null, $lesson_id = null ) {
if ( empty( $quiz_id ) || empty( $lesson_id ) ) {
return;
}
global $post;
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$can_attempt_again = learndash_can_attempt_again( $user_id, $quiz_id );
if ( $can_attempt_again ) {
$link = learndash_next_lesson_quiz( false, $user_id, $lesson_id, null );
} else {
$link = learndash_next_lesson_quiz( false, $user_id, $lesson_id, array( $quiz_id ) );
}
}
/**
* Checks if the lesson topics are completed.
*
* @since 2.1.0
*
* @param int $lesson_id Lesson ID.
* @param boolean $mark_lesson_complete Optional. Whether to mark the lesson complete. Default false.
*
* @return boolean Returns true if the lesson is completed otherwise false.
*/
function learndash_lesson_topics_completed( $lesson_id, $mark_lesson_complete = false ) {
$topics = learndash_get_topic_list( $lesson_id );
if ( empty( $topics[0]->ID ) ) {
return true;
}
$progress = learndash_get_course_progress( null, $topics[0]->ID );
if ( empty( $progress['posts'] ) || ! is_array( $progress['posts'] ) ) {
return false;
}
foreach ( $progress['posts'] as $topic ) {
if ( empty( $topic->completed ) ) {
return false;
}
}
if ( $mark_lesson_complete ) {
$user_id = get_current_user_id();
learndash_process_mark_complete( null, $lesson_id );
}
return true;
}
/**
* Processes a request to mark a course complete.
*
* @global WP_Post $post Global post object.
*
* @since 2.1.0
*
* @param WP_Post|null $post Optional. The `WP_Post` object. Defaults to global post object.
*/
function learndash_mark_complete_process( $post = null ) {
// This is wrong. This function hooks into the 'wp' action. That action doesn't pass a post object or post_id.
// The $post object set were is not even used. We only need the _POST[post] (post_id) variable from the form.
if ( empty( $post ) ) {
global $post;
}
if ( ( isset( $_POST['sfwd_mark_complete'] ) ) && ( ! empty( $_POST['sfwd_mark_complete'] ) ) && ( isset( $_POST['post'] ) ) && ( ! empty( $_POST['post'] ) ) ) {
if ( empty( $post ) || empty( $post->ID ) ) {
$post = get_post(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- I suppose it's what they wanted.
if ( empty( $post ) || empty( $post->ID ) ) {
return;
}
}
$post_id = intval( $_POST['post'] );
if ( isset( $_POST['course_id'] ) ) {
$course_id = intval( $_POST['course_id'] );
} else {
$course_id = learndash_get_course_id( $post_id );
}
if ( isset( $_POST['userid'] ) ) {
$userid = intval( $_POST['userid'] );
} else {
if ( ! is_user_logged_in() ) {
return;
}
$userid = get_current_user_id();
}
/**
* Verify the form is valid
*
* @since 2.2.1.2
*/
if ( ! wp_verify_nonce( $_POST['sfwd_mark_complete'], 'sfwd_mark_complete_' . $userid . '_' . $post_id ) ) {
return;
}
$return = learndash_process_mark_complete( $userid, $post_id, false, $course_id );
if ( $return ) {
// Remove the lesson/topic timer cookie once the step is completed.
$timer_cookie_key = learndash_forced_lesson_time_cookie_key( $post_id );
if ( ! empty( $timer_cookie_key ) ) {
if ( isset( $_COOKIE[ 'learndash_timer_cookie_' . $timer_cookie_key ] ) ) {
unset( $_COOKIE[ 'learndash_timer_cookie_' . $timer_cookie_key ] );
}
// empty value and expiration one hour before.
$res = setcookie( 'learndash_timer_cookie_' . $timer_cookie_key, '', time() - 3600 );
}
// Remove the lesson/topic video progress cookie once the step is completed.
learndash_video_delete_cookie_for_step( $post_id, $course_id, $userid );
$next_lesson_redirect = learndash_get_next_lesson_redirect();
} else {
$next_lesson_redirect = get_permalink();
}
if ( ! empty( $next_lesson_redirect ) ) {
/**
* Filters URL to redirect to after marking a course complete.
*
* @param string $redirect_url Next lesson redirect URL.
* @param int $post_id Post ID.
*/
$next_lesson_redirect = apply_filters( 'learndash_completion_redirect', $next_lesson_redirect, $post_id );
if ( ! empty( $next_lesson_redirect ) ) {
learndash_safe_redirect( $next_lesson_redirect );
}
}
}
}
add_action( 'wp', 'learndash_mark_complete_process' );
/**
* Gets the course permalink.
*
* @since 2.1.0
*
* @param int|null $id Optional. The ID of the resource like course, topic, lesson, quiz, etc. Default null.
*
* @return string The course permalink.
*/
function learndash_get_course_url( $id = null ) {
if ( empty( $id ) ) {
$id = learndash_get_course_id();
}
return get_permalink( $id );
}
/**
* Redirects the user to next lesson.
*
* @global WP_Post $post Global post object.
*
* @since 2.1.0
*
* @param WP_Post|null $post Optional. The `WP_Post` object. Defaults to global post object. Default null.
*
* @return string Returns empty string if the next lesson redirect link empty.
*/
function learndash_get_next_lesson_redirect( $post = null ) {
if ( empty( $post->ID ) ) {
global $post;
}
$next = learndash_next_post_link( '', true, $post );
if ( ! empty( $next ) ) {
$link = $next;
} else {
if ( 'sfwd-topic' === $post->post_type ) {
if ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' ) == 'yes' ) {
$course_id = learndash_get_course_id( $post->ID );
$lesson_id = learndash_course_get_single_parent_step( $course_id, $post->ID );
} else {
$lesson_id = learndash_get_setting( $post, 'lesson' );
}
$link = get_permalink( $lesson_id );
} else {
$course_id = learndash_get_course_id( $post );
$link = learndash_next_global_quiz( true, null, $course_id );
}
}
if ( ! empty( $link ) ) {
/** This filter is documented in includes/course/ld-course-progress.php */
$link = apply_filters( 'learndash_completion_redirect', $link, $post->ID );
if ( ! empty( $link ) ) {
learndash_safe_redirect( $link );
}
}
return '';
}
/**
* Redirects the user after quiz completion.
*
* Fires on `wp` hook.
*
* @global WP_Post $post Global post object.
*
* @since 2.1.0
*/
function learndash_quiz_redirect() {
global $post;
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
if ( ! empty( $_GET['quiz_redirect'] ) && ! empty( $_GET['quiz_id'] ) && ! empty( $_GET['quiz_type'] ) && ! empty( $_GET['course_id'] ) && 'global' == $_GET['quiz_type'] ) {
$quiz_id = intval( $_GET['quiz_id'] );
$can_attempt_again = learndash_can_attempt_again( $user_id, $quiz_id );
if ( $can_attempt_again ) {
$link = learndash_next_global_quiz();
} else {
$link = learndash_next_global_quiz( true, null, null, array( $quiz_id ) );
}
/** This filter is documented in includes/course/ld-course-progress.php */
$link = apply_filters( 'learndash_completion_redirect', $link, $quiz_id );
if ( ! empty( $link ) ) {
learndash_safe_redirect( $link );
}
} else {
if ( ! empty( $_GET['quiz_redirect'] ) && ! empty( $_GET['quiz_id'] ) && ! empty( $_GET['quiz_type'] ) && ! empty( $_GET['lesson_id'] ) && 'lesson' == $_GET['quiz_type'] ) {
$quiz_id = intval( $_GET['quiz_id'] );
$lesson_id = intval( $_GET['lesson_id'] );
if ( isset( $_GET['course_id'] ) ) {
$course_id = absint( $_GET['course_id'] );
}
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id();
}
$link = '';
$next_step = 0;
if ( isset( $_GET['next_step'] ) ) {
$next_step = absint( $_GET['next_step'] );
}
if ( ( 1 === $next_step ) && ( ! empty( $user_id ) ) && ( ! empty( $course_id ) ) && ( ! empty( $quiz_id ) ) ) {
$next_incomplete_step_id = learndash_user_progress_get_next_incomplete_step( $user_id, $course_id, $quiz_id );
if ( ! empty( $next_incomplete_step_id ) ) {
$link = learndash_get_step_permalink( $next_incomplete_step_id, $course_id );
}
if ( empty( $link ) ) {
$link = get_permalink( $course_id );
}
} else {
$link = learndash_next_lesson_quiz( true, $user_id, $lesson_id, null );
if ( empty( $link ) ) {
$link = learndash_next_post_link( '', true );
}
if ( empty( $link ) ) {
$lesson_post = get_post( $lesson_id );
if ( 'sfwd-topic' === $lesson_post->post_type ) {
if ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' ) == 'yes' ) {
$course_id = learndash_get_course_id( $lesson_post->ID );
$lesson = learndash_course_get_single_parent_step( $course_id, $lesson_post->ID );
} else {
$lesson = learndash_get_setting( $lesson_post, 'lesson' );
}
$link = learndash_get_step_permalink( $lesson, $course_id );
} else {
$link = learndash_next_global_quiz();
}
}
}
if ( ! empty( $link ) ) {
/** This filter is documented in includes/course/ld-course-progress.php */
$link = apply_filters( 'learndash_completion_redirect', $link, $quiz_id );
if ( ! empty( $link ) ) {
learndash_safe_redirect( $link );
}
}
}
}
}
add_action( 'wp', 'learndash_quiz_redirect' );
/**
* Checks whether a user can attempt the quiz again.
*
* @since 2.1.0
*
* @param int $user_id User ID.
* @param int $quiz_id Quiz ID.
*
* @return boolean Returns true if the user can attempt quiz again otherwise false.
*/
function learndash_can_attempt_again( $user_id, $quiz_id ) {
$quizmeta = get_post_meta( $quiz_id, '_sfwd-quiz', true );
if ( isset( $quizmeta['sfwd-quiz_repeats'] ) ) {
$repeats = $quizmeta['sfwd-quiz_repeats'];
} else {
$repeats = '';
}
/**
* Filters number of quiz attempts allowed for a user.
*
* @param int $repeats Number of quiz attempts allowed.
* @param int $user_id User ID.
* @param int $quiz_id Quiz ID.
*/
$repeats = apply_filters( 'learndash_allowed_repeats', $repeats, $user_id, $quiz_id );
if ( '' == $repeats ) {
return true;
}
$quiz_results = get_user_meta( $user_id, '_sfwd-quizzes', true );
$count = 0;
if ( ! empty( $quiz_results ) ) {
foreach ( $quiz_results as $quiz ) {
if ( $quiz['quiz'] == $quiz_id ) {
$count++;
}
}
}
if ( $repeats > $count - 1 ) {
return true;
} else {
return false;
}
}
/**
* Checks if the previous topic or lesson is complete.
*
* @since 3.4.0
*
* @param WP_Post $post The `WP_Post` object of lesson or topic.
*
* @return int Returns 1 if the previous lesson or topic is completed otherwise 0.
*/
function learndash_is_previous_complete( $post ) {
$progress = learndash_get_course_progress( null, $post->ID );
if ( empty( $progress ) ) {
return 1;
}
if ( ! empty( $progress['prev'] ) && empty( $progress['prev']->completed ) ) {
return 0;
} else {
return 1;
}
}
/**
* Returns the previous lesson/topic to be completed.
*
* @since 2.3.0
*
* @param WP_Post $post The `WP_Post` object.
*
* @return WP_Post|null The `WP_Post` object of lesson/topic to be completed.
*/
function learndash_get_previous( $post ) {
$progress = learndash_get_course_progress( null, $post->ID );
if ( ! empty( $progress['prev'] ) ) {
return $progress['prev'];
}
return null;
}
/**
* Updates the user meta with completion status for any resource.
*
* @since 2.1.0
*
* @param int|null $user_id Optional. User ID. Default null.
* @param int|null $postid Optional. The ID of the resource like course, lesson, topic, etc. Default null.
* @param boolean $onlycalculate Optional. Whether to mark the resource as complete. Default false.
* @param int $course_id Optional. Course ID. Default 0.
*
* @return boolean Returns true if the meta is updated successfully otherwise false.
*/
function learndash_process_mark_complete( $user_id = null, $postid = null, $onlycalculate = false, $course_id = 0 ) {
if ( ( defined( 'LEARNDASH_COURSE_FUNCTIONS_LEGACY' ) ) && ( true === LEARNDASH_COURSE_FUNCTIONS_LEGACY ) ) {
return learndash_process_mark_complete_legacy( $user_id, $postid, $onlycalculate, $course_id );
}
if ( empty( $user_id ) ) {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
} else {
return false;
}
} else {
$current_user = get_user_by( 'id', $user_id );
}
$post = get_post( $postid );
if ( ! ( $post instanceof WP_Post ) ) {
return false;
}
if ( ! $onlycalculate ) {
/**
* Filters whether to mark a process complete.
*
* @since 2.1.0
*
* @param boolean $mark_complete Whether to mark a process complete.
* @param WP_Post $post WP_Post object to be checked.
* @param WP_User $current_user Current logged in WP_User object.
*/
$process_completion = apply_filters( 'learndash_process_mark_complete', true, $post, $current_user );
if ( ! $process_completion ) {
return false;
}
}
if ( 'sfwd-topic' === $post->post_type ) {
if ( learndash_is_course_builder_enabled() ) {
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id( $post->ID );
}
$lesson_id = learndash_course_get_single_parent_step( $course_id, $post->ID );
} else {
$lesson_id = learndash_get_setting( $post, 'lesson' );
}
}
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id( $postid );
}
if ( empty( $course_id ) ) {
return false;
}
$course_progress = learndash_user_get_course_progress( $user_id, $course_id, 'legacy' );
if ( ( empty( $course_progress ) ) || ( ! is_array( $course_progress ) ) ) {
$course_progress = array(
'lessons' => array(),
'topics' => array(),
);
}
if ( ( ! isset( $course_progress['lessons'] ) ) || ( empty( $course_progress['lessons'] ) ) ) {
$course_progress['lessons'] = array();
}
if ( ( ! isset( $course_progress['topics'] ) ) || ( empty( $course_progress['topics'] ) ) ) {
$course_progress['topics'] = array();
}
if ( 'sfwd-topic' === $post->post_type && empty( $course_progress['topics'][ $lesson_id ] ) ) {
$course_progress['topics'][ $lesson_id ] = array();
}
$lesson_completed = false;
$topic_completed = false;
if ( ! $onlycalculate && 'sfwd-lessons' === $post->post_type && empty( $course_progress['lessons'][ $postid ] ) ) {
$course_progress['lessons'][ $postid ] = 1;
$lesson_completed = true;
}
if ( ! $onlycalculate && 'sfwd-topic' === $post->post_type && empty( $course_progress['topics'][ $lesson_id ][ $postid ] ) ) {
$course_progress['topics'][ $lesson_id ][ $postid ] = 1;
$topic_completed = true;
}
$completed_old = isset( $course_progress['completed'] ) ? $course_progress['completed'] : 0;
$completed = learndash_course_get_completed_steps( $user_id, $course_id, $course_progress );
$course_progress['completed'] = $completed;
$course_progress['total'] = learndash_get_course_steps_count( $course_id );
/**
* Track the last post_id (Lesson, Topic, Quiz) seen by user.
*
* @since 2.1.0
*/
if ( in_array( $post->post_type, learndash_get_post_types( 'course_steps' ), true ) ) {
$course_progress['last_id'] = $post->ID;
}
$course_completed_time = time();
$course_earliest_completed_time = learndash_activity_course_get_earliest_started( $current_user->ID, $course_id, $course_completed_time );
// If course is completed.
if ( ( $course_progress['completed'] >= $completed_old ) && ( $course_progress['completed'] >= $course_progress['total'] ) ) {
/**
* Fires before the course is marked completed.
*
* @since 2.1.0
*
* @param array $course_data An array of course complete data.
*/
do_action(
'learndash_before_course_completed',
array(
'user' => $current_user,
'course' => get_post( $course_id ),
'progress' => array( $course_id => $course_progress ),
'completed_time' => $course_completed_time,
)
);
add_user_meta( $current_user->ID, 'course_completed_' . $course_id, $course_completed_time, true );
} else {
delete_user_meta( $current_user->ID, 'course_completed_' . $course_id );
}
learndash_user_set_course_progress( $user_id, $course_id, $course_progress );
if ( ! empty( $topic_completed ) ) {
$course_activity = learndash_activity_start_course( $current_user->ID, $course_id, $course_earliest_completed_time );
if ( $course_activity ) {
learndash_activity_update_meta_set(
$course_activity->activity_id,
array(
'steps_completed' => learndash_course_get_completed_steps( $current_user->ID, $course_id ),
'steps_last_id' => $post->ID,
)
);
}
learndash_activity_start_lesson( $current_user->ID, $course_id, $lesson_id, $course_completed_time );
learndash_activity_complete_topic( $current_user->ID, $course_id, $post->ID, $course_completed_time );
}
if ( ! empty( $lesson_completed ) ) {
$course_activity = learndash_activity_start_course( $current_user->ID, $course_id, $course_earliest_completed_time );
if ( $course_activity ) {
learndash_activity_update_meta_set(
$course_activity->activity_id,
array(
'steps_completed' => learndash_course_get_completed_steps( $current_user->ID, $course_id ),
'steps_last_id' => $post->ID,
)
);
}
learndash_activity_complete_lesson( $current_user->ID, $course_id, $post->ID, $course_completed_time );
}
$course_args = array(
'course_id' => $course_id,
'user_id' => $current_user->ID,
'post_id' => $course_id,
'activity_type' => 'course',
);
$course_activity = learndash_get_user_activity( $course_args );
if ( ! empty( $course_activity ) ) {
$course_activity = json_decode( wp_json_encode( $course_activity ), true );
} else {
$course_activity = $course_args;
}
if ( in_array( $post->post_type, learndash_get_post_types( 'course_steps' ), true ) ) {
$course_activity['activity_meta'] = array(
'steps_last_id' => $post->ID,
);
}
$do_course_complete_action = false;
if ( $course_progress['completed'] >= $completed_old && $course_progress['total'] == $course_progress['completed'] ) {
if ( ! $course_activity['activity_status'] ) {
$course_activity['activity_status'] = true;
$course_activity['activity_completed'] = $course_completed_time;
$course_activity['activity_updated'] = $course_completed_time;
if ( empty( $course_activity['activity_started'] ) ) {
$course_activity['activity_started'] = $course_earliest_completed_time;
}
$do_course_complete_action = true;
}
} else {
$course_activity['activity_completed'] = 0;
$course_activity['activity_status'] = false;
$course_activity['activity_updated'] = $course_completed_time;
}
learndash_update_user_activity( $course_activity );
$return = false;
if ( ! empty( $lesson_completed ) ) {
/**
* Fires after the lesson is marked completed.
*
* @since 2.1.0
*
* @param array $lesson_data An array of lesson complete data.
*/
do_action(
'learndash_lesson_completed',
array(
'user' => $current_user,
'course' => get_post( $course_id ),
'lesson' => $post,
'progress' => $course_progress,
)
);
$return = true;
}
if ( ! empty( $topic_completed ) ) {
/**
* Fires after the topic is marked completed.
*
* @since 2.1.0
*
* @param array $topic_data An array of topic complete data.
*/
do_action(
'learndash_topic_completed',
array(
'user' => $current_user,
'course' => get_post( $course_id ),
'lesson' => get_post( $lesson_id ),
'topic' => $post,
'progress' => $course_progress,
)
);
$return = true;
}
if ( true == $do_course_complete_action ) {
/**
* Fires after the course is marked completed.
*
* @since 2.1.0
*
* @param array $course_data An array of course complete data.
*/
do_action(
'learndash_course_completed',
array(
'user' => $current_user,
'course' => get_post( $course_id ),
'progress' => array( $course_id => $course_progress ),
'course_completed' => $course_completed_time,
)
);
$return = true;
}
/**
* LEARNDASH-5883 - Always return true if we've made it this far.
*/
$return = true;
return $return;
}
/**
* Marks a resource complete.
*
* @todo seems redundant, function already exists
*
* @since 2.1.0
*
* @param int $user_id Optional. User ID. Default null.
* @param int $postid Optional. The ID of the resource. Default null.
*/
function learndash_update_completion( $user_id = null, $postid = null ) {
if ( empty( $postid ) ) {
global $post;
if ( ( $post ) && ( is_a( $post, 'WP_Post' ) ) ) {
$postid = $post->ID;
}
}
if ( ! empty( $postid ) ) {
learndash_process_mark_complete( $user_id, $postid, true );
}
}
/**
* Checks whether a quiz is complete for a user.
*
* @since 2.1.0
*
* @param int|null $user_id Optional. User ID. Default null.
* @param int $quiz_id Quiz ID.
* @param int $course_id Optional. Course ID. Default 0.
*
* @return boolean Returns true if the quiz is completed for a user otherwise false.
*/
function learndash_is_quiz_complete( $user_id = null, $quiz_id = 0, $course_id = 0 ) {
return ! learndash_is_quiz_notcomplete( $user_id, array( $quiz_id => 1 ), false, $course_id );
}
/**
* Checks whether a quiz is not completed for a user.
*
* Checks against quizzes in user meta and passing percentage of the quiz itself
*
* @since 2.1.0
* @since 2.3.1 Added $return_incomplete_quiz_ids parameter.
*
* @param int|null $user_id Optional. User ID for quizzes. Default null.
* @param array|null $quizzes Optional. Quiz ID to search user quizzes. Default null.
* @param boolean $return_incomplete_quiz_ids Optional. If true will return the array of incomplete quizzes. Default is false. Default false.
* @param int $course_id Optional. The Course ID to match. If -1 is passed then course match is not performed. Default 0.
*
* @return bool Returns true if the quiz(es) NOT complete otherwise false.
*/
function learndash_is_quiz_notcomplete( $user_id = null, $quizzes = null, $return_incomplete_quiz_ids = false, $course_id = 0 ) {
$user_id = (int) $user_id;
$course_id = (int) $course_id;
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
if ( empty( $user_id ) ) {
return true;
}
}
if ( ( is_null( $quizzes ) ) || ( ! is_array( $quizzes ) ) || ( empty( $quizzes ) ) ) {
return;
}
$quiz_results = get_user_meta( $user_id, '_sfwd-quizzes', true );
if ( ( ! empty( $quiz_results ) ) && ( is_array( $quiz_results ) ) ) {
foreach ( $quiz_results as $quiz ) {
if ( ( ! isset( $quiz['quiz'] ) ) || ( empty( $quiz['quiz'] ) ) ) {
continue;
}
if ( ! isset( $quizzes[ $quiz['quiz'] ] ) ) {
continue;
}
// Because we don't want to alter the original $course_id function param.
$quiz_course_id = $course_id;
if ( ( -1 !== $quiz_course_id ) && ( empty( $quiz_course_id ) ) ) {
$quiz_course_id = (int) learndash_get_course_id( intval( $quiz['quiz'] ) );
}
if ( ! isset( $quiz['course'] ) ) {
if ( ! learndash_is_course_shared_steps_enabled() ) {
/**
* If shared steps is not enabled we can determine the related
* course from the quiz post meta.
*/
$quiz['course'] = (int) learndash_get_setting( $quiz['quiz'], 'course' );
} else {
$quiz['course'] = learndash_get_course_id( $quiz['quiz'] );
}
}
$quiz['course'] = intval( $quiz['course'] );
$pass = false;
if ( ( -1 === $course_id ) || ( $course_id === $quiz['course'] ) ) {
if ( isset( $quiz['pass'] ) ) {
$pass = ( 1 == $quiz['pass'] ) ? 1 : 0;
} else {
$passingpercentage = (int) learndash_get_setting( $quiz['quiz'], 'passingpercentage' );
$pass = ( ! empty( $quiz['count'] ) && $quiz['score'] * 100 / $quiz['count'] >= $passingpercentage ) ? 1 : 0;
}
}
if ( $pass ) {
unset( $quizzes[ $quiz['quiz'] ] );
}
// Break if empty. No need to loop through the rest of user quiz progress.
if ( empty( $quizzes ) ) {
break;
}
}
}
if ( empty( $quizzes ) ) {
return 0;
} else {
if ( true == $return_incomplete_quiz_ids ) {
return $quizzes;
} else {
return 1;
}
}
}
/**
* Gets the user's current course progress.
*
* @since 2.1.0
*
* @param int|null $user_id Optional. User ID. Default null.
* @param int|null $postid Optional. Post ID. Default null.
* @param int|null $course_id Optional. Course ID. Default null.
*
* @return array An array of user's current course progress.
*/
function learndash_get_course_progress( $user_id = null, $postid = null, $course_id = null ) {
if ( ( defined( 'LEARNDASH_COURSE_FUNCTIONS_LEGACY' ) ) && ( true === LEARNDASH_COURSE_FUNCTIONS_LEGACY ) ) {
return learndash_get_course_progress_legacy( $user_id, $postid, $course_id );
}
$user_id = absint( $user_id );
$course_id = absint( $course_id );
if ( empty( $user_id ) ) {
$current_user = wp_get_current_user();
if ( empty( $current_user->ID ) ) {
return null;
}
$user_id = $current_user->ID;
}
$posts = array();
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id( $postid );
}
$this_post = get_post( $postid );
if ( ( ! $this_post ) || ( ! is_a( $this_post, 'WP_Post' ) ) ) {
return null;
}
$ld_course_object = LDLMS_Factory_Post::course( intval( $course_id ) );
if ( ! $ld_course_object ) {
return null;
}
$course_progress = learndash_user_get_course_progress( $user_id, $course_id, 'legacy' );
if ( empty( $course_progress ) ) {
$course_progress = array();
}
if ( 'sfwd-lessons' === $this_post->post_type ) {
$posts = $ld_course_object->get_lessons( array( 'nopaging' => true ) );
if ( empty( $course_progress ) || empty( $course_progress['lessons'] ) ) {
$completed_posts = array();
} else {
$completed_posts = $course_progress['lessons'];
}
} elseif ( 'sfwd-topic' === $this_post->post_type ) {
$lesson_id = learndash_course_get_single_parent_step( $course_id, $this_post->ID );
if ( ! $lesson_id ) {
return null;
}
$posts = $ld_course_object->get_topics( $lesson_id, array( 'nopaging' => true ) );
if ( empty( $course_progress ) || empty( $course_progress['topics'][ $lesson_id ] ) ) {
$completed_posts = array();
} else {
$completed_posts = $course_progress['topics'][ $lesson_id ];
}
}
$temp = '';
$prev_p = '';
$next_p = '';
$this_p = '';
if ( ! empty( $posts ) ) {
foreach ( $posts as $k => $post ) {
if ( $post instanceof WP_Post ) {
if ( ! empty( $completed_posts[ $post->ID ] ) ) {
$posts[ $k ]->completed = 1;
} else {
$posts[ $k ]->completed = 0;
}
if ( $post->ID == $postid ) {
$this_p = $post;
$prev_p = $temp;
}
if ( ! empty( $temp->ID ) && $temp->ID == $postid ) {
$next_p = $post;
}
$temp = $post;
}
}
} else {
$posts = array();
}
return array(
'posts' => $posts,
'this' => $this_p,
'prev' => $prev_p,
'next' => $next_p,
);
}
/**
* Checks if a lesson is complete.
*
* @since 2.1.0
*
* @param int|null $user_id Optional. User ID. Defaults to the current logged-in user. Default null.
* @param int $lesson_id Lesson ID.
* @param int $course_id Optional. Course ID. Default 0.
*
* @return boolean Return true if the lesson is complete otherwise false.
*/
function learndash_is_lesson_complete( $user_id = null, $lesson_id = 0, $course_id = 0 ) {
return ! learndash_is_lesson_notcomplete( $user_id, array( $lesson_id => 1 ), $course_id );
}
/**
* Checks if a lesson is not complete.
*
* @since 2.1.0
*
* @param int|null $user_id Optional. User ID. Defaults to the current logged-in user. Default null.
* @param array $lessons An array of lesson IDs.
* @param int $course_id Optional. Course ID. Default 0.
*
* @return boolean Returns true if the lesson is not complete otherwise false.
*/
function learndash_is_lesson_notcomplete( $user_id = null, $lessons = array(), $course_id = 0 ) {
if ( ( defined( 'LEARNDASH_COURSE_FUNCTIONS_LEGACY' ) ) && ( true === LEARNDASH_COURSE_FUNCTIONS_LEGACY ) ) {
return learndash_is_lesson_notcomplete_legacy( $user_id, $lessons, $course_id );
}
if ( empty( $user_id ) ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
}
$course_id = absint( $course_id );
if ( ! empty( $lessons ) ) {
foreach ( $lessons as $lesson => $v ) {
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id( $lesson );
}
if ( ! empty( $course_id ) ) {
$course_progress = learndash_user_get_course_progress( $user_id, $course_id, 'legacy' );
if ( ( isset( $course_progress['lessons'][ $lesson ] ) ) && ( ! empty( $course_progress['lessons'][ $lesson ] ) ) ) {
unset( $lessons[ $lesson ] );
}
}
}
}
if ( empty( $lessons ) ) {
return 0;
} else {
return 1;
}
}
/**
* Checks if a topic is complete.
*
* @since 2.3.1
* @since 3.2.0 Added $course_id
*
* @param int $user_id Optional. User ID. Defaults to the current logged-in user. Default null.
* @param int $topic_id Topic ID.
* @param int $course_id Course ID.
*
* @return boolean Returns true if the topic is completed otherwise false.
*/
function learndash_is_topic_complete( $user_id = null, $topic_id = 0, $course_id = 0 ) {
return ! learndash_is_topic_notcomplete( $user_id, array( $topic_id => 1 ), $course_id );
}
/**
* Checks if a topic is not complete.
*
* @since 2.3.1
* @since 3.2.0 Added $course_id
*
* @param int|null $user_id Optional. User ID. Defaults to the current logged-in user. Default null.
* @param array $topics An array of topic IDs.
* @param int $course_id Course ID.
*
* @return boolean Returns true if the topic is not completed otherwise false.
*/
function learndash_is_topic_notcomplete( $user_id = null, $topics = array(), $course_id = 0 ) {
if ( ( defined( 'LEARNDASH_COURSE_FUNCTIONS_LEGACY' ) ) && ( true === LEARNDASH_COURSE_FUNCTIONS_LEGACY ) ) {
return learndash_is_topic_notcomplete_legacy( $user_id, $topics, $course_id );
}
if ( empty( $user_id ) ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
}
$user_id = absint( $user_id );
$course_id = absint( $course_id );
if ( ! empty( $topics ) ) {
foreach ( $topics as $topic_id => $v ) {
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id( $topic_id );
}
if ( ! empty( $course_id ) ) {
$course_progress = learndash_user_get_course_progress( $user_id, $course_id, 'legacy' );
$lesson_id = learndash_course_get_single_parent_step( $course_id, $topic_id );
if ( ! empty( $lesson_id ) ) {
if ( ( isset( $course_progress['topics'] ) )
&& ( ! empty( $course_progress['topics'] ) )
&& ( isset( $course_progress['topics'][ $lesson_id ][ $topic_id ] ) )
&& ( ! empty( $course_progress['topics'][ $lesson_id ][ $topic_id ] ) ) ) {
unset( $topics[ $topic_id ] );
}
}
}
}
}
if ( empty( $topics ) ) {
return 0;
} else {
return 1;
}
}
/**
* Outputs the current status of the course.
*
* @since 2.1.0
* @since 2.5.8 Added $return_slug parameter.
*
* @param int $course_id Course ID to get status.
* @param int|null $user_id Optional. User ID. Default null.
* @param boolean $return_slug Optional. If false will return translatable string otherwise the status slug. Default false.
*
* @return string The current status of the course.
*/
function learndash_course_status( $course_id, $user_id = null, $return_slug = false ) {
global $learndash_course_statuses;
if ( ( defined( 'LEARNDASH_COURSE_FUNCTIONS_LEGACY' ) ) && ( true === LEARNDASH_COURSE_FUNCTIONS_LEGACY ) ) {
return learndash_course_status_legacy( $course_id, $user_id, $return_slug );
}
$course_status_slug = '';
$course_id = absint( $course_id );
$user_id = absint( $user_id );
if ( empty( $user_id ) ) {
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
}
}
if ( ( empty( $course_id ) ) || ( empty( $user_id ) ) ) {
return $course_status_slug;
}
$course_progress = learndash_user_get_course_progress( $user_id, $course_id, 'legacy' );
if ( isset( $course_progress['status'] ) ) {
$course_status_slug = $course_progress['status'];
}
/**
* Sort of a kludge for now. If the course is not complete and the 'total' steps is
* not zero. Call the the mark complete function.
*
* This logic should be within User Course Progression class.
*/
if ( in_array( $course_status_slug, array( 'in_progress', 'not_started' ), true ) ) {
if ( ( $course_progress['total'] > 0 ) && ( $course_progress['completed'] === $course_progress['total'] ) ) {
if ( learndash_process_mark_complete( $user_id, $course_id ) ) {
// If the Mark Complete was success we call to get the progress again.
$course_progress = learndash_user_get_course_progress( $user_id, $course_id, 'legacy' );
if ( isset( $course_progress['status'] ) ) {
$course_status_slug = $course_progress['status'];
}
}
}
}
if ( true === $return_slug ) {
return $course_status_slug;
} else {
if ( isset( $learndash_course_statuses[ $course_status_slug ] ) ) {
/**
* Filters the current status of the course.
*
* @param string $course_status_str The translatable current course status string.
* @param int $course_id Course ID.
* @param int $user_id User ID.
* @param array $course_progress Current course progress.
*/
return apply_filters(
'learndash_course_status',
learndash_course_status_label( $course_status_slug ),
$course_id,
$user_id,
isset( $course_progress ) ? $course_progress : array()
);
}
}
return $course_status_slug;
}
/**
* Gets the course status index from the course status label.
*
* In various places with LD the course status is expressed as a string as in 'Not Started', 'In Progress' or 'Complete'.
* the problem with using this string is it will be translated depending on the locale(). This means comparative logic can
* possible fails.
* The purpose of this function is to help use an internal key to keep track of the course status value.
*
* @global array $learndash_course_statuses An array of course status.
*
* @since 2.3.0
*
* @param string $course_status_label Optional. The current translatable text for course status. Default empty.
*
* @return string The index/key of the course status string if found in the `$learndash_course_statuses` global array.
*/
function learndash_course_status_idx( $course_status_label = '' ) {
global $learndash_course_statuses;
return array_search( $course_status_label, $learndash_course_statuses, true );
}
/**
* Get the Course Status label from slug.
*
* @since 3.4.0
*
* @param string $course_status_slug Course Status slug.
*
* @return string|null.
*/
function learndash_course_status_label( $course_status_slug = '' ) {
global $learndash_course_statuses;
if ( isset( $learndash_course_statuses[ $course_status_slug ] ) ) {
return $learndash_course_statuses[ $course_status_slug ];
}
}
/**
* Checks if the quiz is accessible to the user.
*
* @since 3.4.0
*
* @param int|null $user_id $user_id Optional. The ID of User to check. Defaults to the current logged-in user. Default null.
* @param WP_Post|null $post Optional. The `WP_Post` quiz object. Default null.
* @param boolean $return_incomplete Optional. Whether to return last incomplete step. Default false.
* @param int $course_id Optional. Course ID. Default 0.
*
* @return int|WP_Post|void Returns 1 if the quiz is accessible by user otherwise 0. If the `$return_incomplete`
* parameter is set to true it may return `WP_Post` object for incomplete step.
*/
function learndash_is_quiz_accessable( $user_id = null, $post = null, $return_incomplete = false, $course_id = 0 ) {
// Allow using the legacy function in case of issues with new logic.
if ( ( defined( 'LEARNDASH_IS_QUIZ_ACCESSABLE_LEGACY' ) && ( LEARNDASH_IS_QUIZ_ACCESSABLE_LEGACY === true ) ) ) {
return learndash_is_quiz_accessable_legacy( $user_id, $post, $course_id );
}
if ( empty( $user_id ) ) {
$current_user = wp_get_current_user();
if ( empty( $current_user->ID ) ) {
return 1;
}
$user_id = $current_user->ID;
}
if ( ( empty( $post ) ) || ( ! is_a( $post, 'WP_Post' ) ) ) {
return;
}
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id( $post );
}
$course_id = absint( $course_id );
// If we have a Quiz but the Quiz is not part of a course then return 1 for valid.
if ( empty( $course_id ) ) {
return 1;
}
$bypass_course_limits_admin_users = learndash_can_user_bypass( $user_id, 'learndash_quiz_accessable', $post, $course_id );
if ( true === $bypass_course_limits_admin_users ) {
return 1;
}
$course_progress_co = learndash_user_get_course_progress( $user_id, $course_id, 'co' );
if ( learndash_is_course_builder_enabled() ) {
$quiz_parent_id = learndash_course_get_single_parent_step( $course_id, $post->ID );
} else {
$quiz_parent_id = learndash_get_setting( $post, 'lesson' );
}
$quiz_parent_id = absint( $quiz_parent_id );
if ( ! empty( $quiz_parent_id ) ) {
$quiz_parent_post = get_post( $quiz_parent_id );
if ( is_a( $quiz_parent_post, 'WP_Post' ) ) {
if ( learndash_get_post_type_slug( 'topic' ) === $quiz_parent_post->post_type ) {
$quiz_parent_topic_post = $quiz_parent_post;
$quiz_parent_lesson_id = learndash_get_setting( $quiz_parent_topic_post, 'lesson' );
$quiz_parent_lesson_post = get_post( $quiz_parent_lesson_id );
$parent_topic_quizzes = learndash_get_lesson_quiz_list( $quiz_parent_topic_post, $user_id, $course_id );
if ( ! empty( $parent_topic_quizzes ) ) {
// loop until we get to the first status == 'notcompleted'.
foreach ( $parent_topic_quizzes as $quiz ) {
if ( $quiz['post']->ID === $post->ID ) {
break;
} elseif ( 'completed' !== $quiz['status'] ) {
if ( true === $return_incomplete ) {
return $quiz['post'];
} else {
return 0;
}
}
}
}
$lesson_topics_progress = learndash_get_course_progress( $user_id, $quiz_parent_topic_post->ID );
if ( ( isset( $lesson_topics_progress['posts'] ) ) && ( ! empty( $lesson_topics_progress['posts'] ) ) ) {
foreach ( $lesson_topics_progress['posts'] as $topic ) {
if ( $topic->ID === $quiz_parent_topic_post->ID ) {
if ( ! empty( $topic->completed ) ) {
return 1;
}
break;
}
if ( empty( $topic->completed ) ) {
if ( true === $return_incomplete ) {
return $topic;
} else {
return 0;
}
break;
}
}
}
if ( 'on' === learndash_get_setting( $quiz_parent_topic_post->ID, 'lesson_video_enabled' ) ) {
if ( ! empty( learndash_get_setting( $quiz_parent_topic_post->ID, 'lesson_video_url' ) ) ) {
if ( 'BEFORE' === learndash_get_setting( $quiz_parent_topic_post->ID, 'lesson_video_shown' ) ) {
if ( ! learndash_video_complete_for_step( $quiz_parent_topic_post->ID, $course_id, $user_id ) ) {
if ( true === $return_incomplete ) {
return $quiz_parent_topic_post;
} else {
return 0;
}
}
}
}
}
$lesson_progress = learndash_get_course_progress( $user_id, $quiz_parent_lesson_post->ID, $course_id );
if ( ( isset( $lesson_progress['posts'] ) ) && ( ! empty( $lesson_progress['posts'] ) ) ) {
foreach ( $lesson_progress['posts'] as $lesson ) {
if ( $lesson->ID === $quiz_parent_lesson_post->ID ) {
break;
}
if ( empty( $lesson->completed ) ) {
if ( true === $return_incomplete ) {
return $lesson;
} else {
return 0;
}
break;
}
}
}
return 1;
} elseif ( learndash_get_post_type_slug( 'lesson' ) === $quiz_parent_post->post_type ) {
$quiz_parent_lesson_post = $quiz_parent_post;
$sibling_completed_steps = 0;
$lesson_topics = learndash_get_topic_list( $quiz_parent_lesson_post->ID );
if ( ! empty( $lesson_topics ) ) {
$lesson_topics_progress = learndash_get_course_progress( $user_id, $lesson_topics[0]->ID );
if ( ( isset( $lesson_topics_progress['posts'] ) ) && ( ! empty( $lesson_topics_progress['posts'] ) ) ) {
foreach ( $lesson_topics_progress['posts'] as $topic ) {
if ( empty( $topic->completed ) ) {
if ( true === $return_incomplete ) {
return $topic;
} else {
return 0;
}
break;
} else {
++$sibling_completed_steps;
}
}
}
}
$quizzes = learndash_get_lesson_quiz_list( $quiz_parent_lesson_post, $user_id, $course_id );
if ( ! empty( $quizzes ) ) {
// loop until we get to the first status == 'notcompleted'.
foreach ( $quizzes as $quiz ) {
if ( $quiz['post']->ID === $post->ID ) {
break;
}
if ( 'completed' !== $quiz['status'] ) {
if ( true === $return_incomplete ) {
return $quiz['post'];
} else {
return 0;
}
} else {
++$sibling_completed_steps;
}
}
}
$lesson_progress = learndash_get_course_progress( $user_id, $quiz_parent_lesson_post->ID );
if ( ( isset( $lesson_progress['posts'] ) ) && ( ! empty( $lesson_progress['posts'] ) ) ) {
foreach ( $lesson_progress['posts'] as $lesson ) {
if ( $lesson->ID === $quiz_parent_lesson_post->ID ) {
if ( ! empty( $lesson->completed ) ) {
return 1;
}
break;
}
if ( empty( $lesson->completed ) ) {
if ( true === $return_incomplete ) {
return $lesson;
} else {
return 0;
}
break;
}
}
}
if ( empty( $sibling_completed_steps ) ) {
if ( 'on' === learndash_get_setting( $quiz_parent_lesson_post->ID, 'lesson_video_enabled' ) ) {
if ( ! empty( learndash_get_setting( $quiz_parent_lesson_post->ID, 'lesson_video_url' ) ) ) {
if ( 'BEFORE' === learndash_get_setting( $quiz_parent_lesson_post->ID, 'lesson_video_shown' ) ) {
if ( ! learndash_video_complete_for_step( $quiz_parent_lesson_post->ID, $course_id, $user_id ) ) {
if ( true === $return_incomplete ) {
return $quiz_parent_lesson_post;
} else {
return 0;
}
}
}
}
}
}
return 1;
}
}
} else {
// First we check if all course lessons are completed.
$lessons = learndash_get_course_lessons_list( $course_id, $user_id, array( 'num' => 0 ) );
if ( ! empty( $lessons ) ) {
foreach ( $lessons as $lesson ) {
if ( 'completed' !== $lesson['status'] ) {
if ( true === $return_incomplete ) {
return $lesson['post'];
} else {
return 0;
}
}
}
}
// Next we check if other global quizzes are completed.
$quizzes = learndash_get_global_quiz_list( $course_id );
if ( ! empty( $quizzes ) ) {
// loop until we get to the first status == 'notcompleted'.
foreach ( $quizzes as $quiz ) {
if ( $quiz->ID === $post->ID ) {
return 1;
} elseif ( ! learndash_is_quiz_complete( $user_id, $quiz->ID, $course_id ) ) {
if ( true === $return_incomplete ) {
return $quiz;
} else {
return 0;
}
}
}
}
}
return 0;
}
/**
* Checks if all quizzes for a course are complete for the user.
*
* @since 3.4.0
*
* @param int|null $user_id Optional. User ID. Default null.
* @param int|null $id Optional. The ID of the resource. Default null.
*
* @return boolean
*/
function learndash_is_all_global_quizzes_complete( $user_id = null, $id = null ) {
$quizzes = learndash_get_global_quiz_list( $id );
$return = true;
if ( ! empty( $quizzes ) ) {
foreach ( $quizzes as $quiz ) {
if ( learndash_is_quiz_notcomplete( $user_id, array( $quiz->ID => 1 ), false, $id ) ) {
$return = false;
}
}
}
return $return;
}
/**
* Gets the next quiz for a course.
*
* @since 2.1.0
*
* @param boolean $url Optional. Whether to return URL for the next quiz. Default true.
* @param int|null $user_id Optional. User ID. Defaults to the current logged-in user. Default null.
* @param int|null $id Optional. The ID of the resource. Default null.
* @param array $exclude Optional. An array of quiz IDs to exclude. Default empty array.
*
* @return int|string The ID or the URL of the quiz.
*/
function learndash_next_global_quiz( $url = true, $user_id = null, $id = null, $exclude = array() ) {
if ( empty( $id ) ) {
$id = learndash_get_course_id();
}
if ( empty( $user_id ) ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
}
$quizzes = learndash_get_global_quiz_list( $id );
$return = get_permalink( $id );
if ( ! empty( $quizzes ) ) {
foreach ( $quizzes as $quiz ) {
if ( ! in_array( $quiz->ID, $exclude, true ) && learndash_is_quiz_notcomplete( $user_id, array( $quiz->ID => 1 ), false, $id ) && learndash_can_attempt_again( $user_id, $quiz->ID ) ) {
if ( $url ) {
return get_permalink( $quiz->ID );
} else {
return $quiz->ID;
}
}
}
}
/**
* Filters ID or URL of the next quiz of the course.
*
* @todo filter name does not seem correct
* in context of function
*
* @since 2.1.0
*
* @param int|string $next_quiz ID or URL of next quiz of the course.
* @param int $course_id Course ID.
*/
$return = apply_filters( 'learndash_course_completion_url', $return, $id );
return $return;
}
/**
* Gets the next quiz for current lesson for a user.
*
* @global WP_Post $post Global post object.
*
* @since 2.1.0
*
* @param boolean $url Optional. Whether to return URL for the next quiz. Default true.
* @param int|null $user_id Optional. User ID. Defaults to the current logged-in user. Default null.
* @param int|null $lesson_id Optional. Lesson ID. Default null.
* @param array $exclude Optional. An array of quiz IDs to exclude. Default empty array.
*
* @return int|string The ID or the URL of the quiz.
*/
function learndash_next_lesson_quiz( $url = true, $user_id = null, $lesson_id = null, $exclude = array() ) {
global $post;
$return = false;
if ( empty( $lesson_id ) ) {
$lesson_id = $post->ID;
}
if ( empty( $exclude ) ) {
$exclude = array();
}
if ( empty( $user_id ) ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
}
$course_id = learndash_get_course_id();
// Assumption here is the learndash_get_lesson_quiz_list returns the quizzes in the order they should be taken.
$quizzes = learndash_get_lesson_quiz_list( $lesson_id, $user_id );
if ( ( ! empty( $quizzes ) ) && ( is_array( $quizzes ) ) ) {
foreach ( $quizzes as $quiz ) {
// The logic here is we need to check all the quizzes in this lesson. If all the quizzes are complete
// (including the current one) then we set the parent (lesson) to complete.
if ( 'completed' == $quiz['status'] ) {
continue;
}
// If not complete AND the user CAN take the quiz again...
if ( learndash_can_attempt_again( $user_id, $quiz['post']->ID ) ) {
$return = ( $url ) ? get_permalink( $quiz['post']->ID ) : $quiz['post']->ID;
break;
}
$return = ( $url ) ? get_permalink( $quiz['post']->ID ) : $quiz['post']->ID;
break;
}
}
if ( empty( $return ) ) {
if ( ( learndash_lesson_progression_enabled( $course_id ) ) && ( ! learndash_can_complete_step( $user_id, $lesson_id, $course_id ) ) ) {
$return = learndash_get_step_permalink( $lesson_id, $course_id );
} elseif ( learndash_user_is_course_children_progress_complete( $user_id, $course_id, $lesson_id ) ) {
learndash_process_mark_complete( $user_id, $lesson_id, false, $course_id );
}
}
return $return;
}
/**
* Check if the user can complete the current step.
*
* This is mostly used before auto-completing a parent step like a quiz
* parent lesson.
*
* @since 3.2.3
* @since 4.0.3 Added `$ignore_lesson_timer` parameter.
*
* @param int $user_id User ID.
* @param int $step_id Course Step ID.
* @param int $course_id Course ID.
* @param bool $ignore_lesson_timer Whether to ignore the lesson timer. Default false. @since 4.0.3.
*
* @return bool True if can complete.
*/
function learndash_can_complete_step( $user_id = 0, $step_id = 0, $course_id = 0, $ignore_lesson_timer = false ) {
$user_id = absint( $user_id );
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$step_id = absint( $step_id );
$course_id = absint( $course_id );
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id( $step_id );
}
if ( ( ! empty( $user_id ) ) && ( ! empty( $step_id ) ) && ( ! empty( $course_id ) ) ) {
// If we have ANY previous incomplete steps then we can't complete this step.
if ( ! learndash_lesson_progression_enabled( $course_id ) ) {
$incomplete_child_steps = learndash_user_progression_get_incomplete_child_steps( $user_id, $course_id, $step_id );
if ( empty( ! $incomplete_child_steps ) ) {
return false;
}
} else {
$incomplete_step_id = learndash_user_progress_get_previous_incomplete_step( $user_id, $course_id, $step_id );
if ( ! empty( $incomplete_step_id ) && ( $incomplete_step_id !== $step_id ) ) {
return false;
}
}
if ( in_array( get_post_type( $step_id ), learndash_get_post_type_slug( array( 'lesson', 'topic' ) ), true ) ) {
// Check the Lesson Timer...
if ( false === $ignore_lesson_timer ) {
$step_timer_time = learndash_forced_lesson_time( $step_id );
if ( ! empty( $step_timer_time ) ) {
$time_cookie_key = learndash_forced_lesson_time_cookie_key( $step_id );
if ( ! empty( $time_cookie_key ) ) {
/**
* Note this is not a 100% check. We are only checking if the cookie
* key exists and is zero. But this cookie could have been set from
* external.
*/
if ( ( ! isset( $_COOKIE[ 'learndash_timer_cookie_' . $time_cookie_key ] ) ) || ( '0' !== $_COOKIE[ 'learndash_timer_cookie_' . $time_cookie_key ] ) ) {
return false;
}
}
}
}
// Next check the Lesson Assignment.
if ( 'on' === learndash_get_setting( $step_id, 'lesson_assignment_upload' ) ) {
$assignments = learndash_get_user_assignments( $step_id, $user_id, $course_id );
if ( empty( $assignments ) ) {
return false;
} else {
foreach ( $assignments as $assignment ) {
if ( ! learndash_is_assignment_approved_by_meta( $assignment->ID ) ) {
return false;
}
}
}
}
// Next check if all child steps are completed.
if ( ! learndash_user_is_course_children_progress_complete( $user_id, $course_id, $step_id ) ) {
return false;
}
}
return true;
}
return false;
}
/**
* Checks if the resource has any quizzes.
*
* @since 3.4.0
*
* @param int|null $id Optional. The ID of the resource like course, lesson, topic, etc. Default null.
*
* @return boolean Returns true if the resource has quizzes otherwise false.
*/
function learndash_has_global_quizzes( $id = null ) {
$quizzes = learndash_get_global_quiz_list( $id );
return ! empty( $quizzes );
}
/**
* Outputs the course progress HTML for the user.
*
* @todo consider for deprecation, not in use
*
* @since 2.1.0
*
* @param array $atts An array of course progress attributes.
*/
function learndash_course_progress_widget( $atts ) {
echo learndash_course_progress( $atts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Need to output HTML
}
/**
* Checks whether the lesson progression is enabled or not.
*
* @since 2.1.0
*
* @param int $course_id Optional. Course ID to check. Default 0.
*
* @return boolean Returns true if the lesson progression is enabled otherwise false.
*/
function learndash_lesson_progression_enabled( $course_id = 0 ) {
$course_id = intval( $course_id );
if ( empty( $course_id ) ) {
$course_id = learndash_get_course_id();
}
if ( ! empty( $course_id ) ) {
$setting = learndash_get_setting( $course_id, 'course_disable_lesson_progression' );
/**
* Filter for Course Progression Enabled.
*
* @since 3.4.0
*
* @param bool $setting true if course progression is enabled.
* @param int $course_id Course ID.
*/
return apply_filters( 'learndash_course_progression_enabled', empty( $setting ), $course_id );
}
return false;
}
/**
* Gets the lesson time for a lesson if it exists.
*
* @global WP_Post $post Global post object.
*
* @since 2.1.0
*
* @param string|int|WP_Post $lesson_topic_post Optional. The `WP_Post` lesson topic post object or ID. Defaults to global post object. Default empty.
*
* @return int|string Returns lesson time if it exists otherwise 0.
*/
function learndash_forced_lesson_time( $lesson_topic_post = '' ) {
if ( empty( $lesson_topic_post ) ) {
global $post;
$lesson_topic_post = $post;
}
if ( ! is_a( $lesson_topic_post, 'WP_Post' ) ) {
$post_id = absint( $lesson_topic_post );
if ( empty( $post_id ) ) {
return 0;
}
$lesson_topic_post = get_post( $post_id );
if ( ( ! $lesson_topic_post ) || ( ! is_a( $lesson_topic_post, 'WP_Post' ) ) ) {
return 0;
}
}
if ( ! in_array( $lesson_topic_post->post_type, array( learndash_get_post_type_slug( 'lesson' ), learndash_get_post_type_slug( 'topic' ) ), true ) ) {
return 0;
}
$meta = get_post_meta( $lesson_topic_post->ID, '_' . $lesson_topic_post->post_type, true );
if ( ! is_array( $meta ) ) {
$meta = array();
}
if ( ! isset( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time_enabled' ] ) ) {
if ( ( isset( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ] ) ) && ( ! empty( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ] ) ) ) {
$meta[ $lesson_topic_post->post_type . '_forced_lesson_time_enabled' ] = 'on';
} else {
$meta[ $lesson_topic_post->post_type . '_forced_lesson_time_enabled' ] = '';
}
}
if ( 'on' === $meta[ $lesson_topic_post->post_type . '_forced_lesson_time_enabled' ] ) {
if ( ( isset( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ] ) ) && ( ! empty( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ] ) ) ) {
return $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ];
}
}
return 0;
}
/**
* Gets the lesson time cookie key for lesson/topic.
*
* @global WP_Post $post Global post object.
*
* @since 3.0.0
*
* @param string|int|WP_Post $lesson_topic_post Optional. The `WP_Post` lesson topic post object or ID. Defaults to global post object.
*
* @return string The cookie key value or empty string.
*/
function learndash_forced_lesson_time_cookie_key( $lesson_topic_post = '' ) {
if ( empty( $lesson_topic_post ) ) {
global $post;
$lesson_topic_post = $post;
}
if ( ! is_a( $lesson_topic_post, 'WP_Post' ) ) {
$post_id = absint( $lesson_topic_post );
if ( empty( $post_id ) ) {
return 0;
}
$lesson_topic_post = get_post( $post_id );
if ( ( ! $lesson_topic_post ) || ( ! is_a( $lesson_topic_post, 'WP_Post' ) ) ) {
return 0;
}
}
if ( ! in_array( $lesson_topic_post->post_type, array( learndash_get_post_type_slug( 'lesson' ), learndash_get_post_type_slug( 'topic' ) ), true ) ) {
return 0;
}
$meta = get_post_meta( $lesson_topic_post->ID, '_' . $lesson_topic_post->post_type, true );
if ( ! is_array( $meta ) ) {
$meta = array();
}
if ( ! isset( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time_enabled' ] ) ) {
if ( ( isset( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ] ) ) && ( ! empty( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ] ) ) ) {
$meta[ $lesson_topic_post->post_type . '_forced_lesson_time_enabled' ] = 'on';
} else {
$meta[ $lesson_topic_post->post_type . '_forced_lesson_time_enabled' ] = '';
}
}
if ( 'on' === $meta[ $lesson_topic_post->post_type . '_forced_lesson_time_enabled' ] ) {
if ( ( isset( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ] ) ) && ( ! empty( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time' ] ) ) ) {
$cookie_key = get_current_user_id() . '_' . learndash_get_course_id( $lesson_topic_post ) . '_' . $lesson_topic_post->ID;
if ( ( isset( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time_cookie_key' ] ) ) && ( ! empty( $meta[ $lesson_topic_post->post_type . '_forced_lesson_time_cookie_key' ] ) ) ) {
$cookie_key .= '_' . $meta[ $lesson_topic_post->post_type . '_forced_lesson_time_cookie_key' ];
}
return $cookie_key;
}
}
return '';
}
/**
* Checks if a course is completed for a user.
*
* @since 2.1.0
*
* @param int $user_id User ID.
* @param int $course_id Course ID.
*
* @return boolean Returns true if the course is completed otherwise false.
*/
function learndash_course_completed( $user_id, $course_id ) {
if ( learndash_course_status( $course_id, $user_id ) == esc_html__( 'Completed', 'learndash' ) ) {
return true;
} else {
return false;
}
}
/**
* Adds the course completion date to user meta.
*
* @since 2.1.0
*
* @param array $data An array of course completion data.
*/
function learndash_course_completed_store_time( $data ) {
$user_id = $data['user']->ID;
$course_id = $data['course']->ID;
$meta_key = 'course_completed_' . $course_id;
$meta_value = time();
$course_completed = get_user_meta( $user_id, $meta_key );
if ( empty( $course_completed ) ) {
update_user_meta( $user_id, $meta_key, $meta_value );
}
}
add_action( 'learndash_before_course_completed', 'learndash_course_completed_store_time', 10, 1 );
/**
* Deletes the course progress for a user.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @since 2.1.0
*
* @param int $course_id Course ID.
* @param int $user_id User ID.
*/
function learndash_delete_course_progress( $course_id, $user_id ) {
global $wpdb;
$usermeta = get_user_meta( $user_id, '_sfwd-course_progress', true );
if ( isset( $usermeta[ $course_id ] ) ) {
unset( $usermeta[ $course_id ] );
update_user_meta( $user_id, '_sfwd-course_progress', $usermeta );
}
delete_user_meta( $user_id, 'course_completed_' . $course_id );
// The reason we don't use the methods above is we want to ensure all old data is removed
// from the quiz attempt history not just for quizzes currently associated with the course.
$quizzes = array();
$usermeta_quizzes = get_user_meta( $user_id, '_sfwd-quizzes', true );
if ( ! is_array( $usermeta_quizzes ) ) {
$usermeta_quizzes = array();
}
if ( ! empty( $usermeta_quizzes ) ) {
foreach ( $usermeta_quizzes as $quiz_item ) {
if ( ( isset( $quiz_item['course'] ) ) && ( intval( $course_id ) == intval( $quiz_item['course'] ) ) ) {
if ( isset( $quiz_item['quiz'] ) ) {
$quiz_id = intval( $quiz_item['quiz'] );
$quizzes[ $quiz_id ] = $quiz_id;
}
}
}
}
if ( ! empty( $quizzes ) ) {
foreach ( $quizzes as $quiz_id ) {
learndash_delete_quiz_progress( $user_id, $quiz_id );
}
}
}
/**
* Deletes the quiz progress for a user.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @since 2.1.0
*
* @param int $user_id User ID.
* @param int $quiz_id Quiz ID.
*/
function learndash_delete_quiz_progress( $user_id, $quiz_id ) {
global $wpdb;
// Clear User Meta.
$usermeta = get_user_meta( $user_id, '_sfwd-quizzes', true );
if ( ! empty( $usermeta ) && is_array( $usermeta ) ) {
$usermeta_new = array();
foreach ( $usermeta as $key => $quizmeta ) {
if ( $quizmeta['quiz'] != $quiz_id ) {
$usermeta_new[] = $quizmeta;
}
}
update_user_meta( $user_id, '_sfwd-quizzes', $usermeta_new );
}
// ProQuiz Data.
$pro_quiz_id = learndash_get_setting( $quiz_id, 'quiz_pro' );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$ref_ids = $wpdb->get_col(
$wpdb->prepare( 'SELECT statistic_ref_id FROM ' . esc_sql( LDLMS_DB::get_table_name( 'quiz_statistic_ref' ) ) . ' WHERE user_id = %d AND quiz_id = %d ', $user_id, $pro_quiz_id )
);
if ( ! empty( $ref_ids[0] ) ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->delete(
LDLMS_DB::get_table_name( 'quiz_statistic_ref' ),
array(
'user_id' => $user_id,
'quiz_id' => $pro_quiz_id,
)
);
$ref_ids = array_map( 'absint', $ref_ids );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQL.NotPrepared -- IN clause
$wpdb->prepare( 'DELETE FROM ' . esc_sql( LDLMS_DB::get_table_name( 'quiz_statistic' ) ) . ' WHERE statistic_ref_id IN (' . LDLMS_DB::escape_IN_clause_placeholders( $ref_ids ) . ')', LDLMS_DB::escape_IN_clause_values( $ref_ids ) )
);
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->delete(
LDLMS_DB::get_table_name( 'quiz_toplist' ),
array(
'user_id' => $user_id,
'quiz_id' => $pro_quiz_id,
)
);
}
/**
* Removes the user quiz statistics by the reference ID.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @since 2.5.0
*
* @param int $ref_id Optional. Quiz statistic reference ID. Default 0.
*/
function learndash_quiz_remove_user_statistics_by_ref( $ref_id = 0 ) {
global $wpdb;
if ( ! empty( $ref_id ) ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->delete( LDLMS_DB::get_table_name( 'quiz_statistic_ref' ), array( 'statistic_ref_id' => $ref_id ) );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
$wpdb->prepare( 'DELETE FROM ' . esc_sql( LDLMS_DB::get_table_name( 'quiz_statistic' ) ) . ' WHERE statistic_ref_id = %d', $ref_id )
);
}
}
/**
* Removes the quiz user toplist.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @since 3.1.0
*
* @param int $user_id Optional. User ID. Default 0.
* @param int $quiz_time Optional. Quiz time. Default 0.
* @param int $pro_quizid Optional. Pro quiz ID. Default 0.
*
* @return void|int|false The number of rows updated, or false on error.
*/
function learndash_quiz_remove_user_toplist( $user_id = 0, $quiz_time = 0, $pro_quizid = 0 ) {
global $wpdb;
if ( ( ! empty( $user_id ) ) && ( ! empty( $quiz_time ) ) && ( ! empty( $pro_quizid ) ) ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
return $wpdb->delete(
LDLMS_DB::get_table_name( 'quiz_toplist' ),
array(
'user_id' => $user_id,
'date' => $quiz_time,
'quiz_id' => $pro_quizid,
),
array( '%d', '%d', '%d' )
);
}
}
/**
* Marks a course step incomplete for a course.
*
* Used to set a course step ( lesson or topic only ) back to not complete status.
*
* @global WP_Post $post Global post object.
*
* @since 2.5.0
*
* @param int $user_id Optional. User ID. Default 0.
* @param int $course_id Optional. Course ID. Default 0.
* @param int $step_id Optional. Step ID. Default 0.
* @param boolean $step_complete Optional. Unused. Default false.
*
* @return int|boolean Returns true if the update is successful otherwise false or meta ID
* if the meta does not exist.
*/
function learndash_process_mark_incomplete( $user_id = 0, $course_id = 0, $step_id = 0, $step_complete = false ) {
$user_id = absint( $user_id );
if ( empty( $user_id ) ) {
return;
}
$course_id = absint( $course_id );
if ( empty( $course_id ) ) {
return;
}
$step_id = absint( $step_id );
if ( empty( $step_id ) ) {
global $post;
if ( ( isset( $post ) ) && ( $post instanceof WP_Post ) && ( ( in_array( $post->post_type, array( 'sfwd-lessons', 'sfwd-topic', 'sfwd-quiz' ), true ) ) ) ) {
$step_id = $post->ID;
} else {
return;
}
}
$subtracted_completed_steps = 0;
$course_step_parents = learndash_course_get_all_parent_step_ids( $course_id, $step_id );
$user_course_progress = get_user_meta( $user_id, '_sfwd-course_progress', true );
if ( ! isset( $user_course_progress[ $course_id ] ) ) {
return;
}
$step_post_type = get_post_type( $step_id );
if ( 'sfwd-quiz' === $step_post_type ) {
if ( ! empty( $course_step_parents ) ) {
if ( ( 2 == count( $course_step_parents ) ) && ( 'sfwd-lessons' == get_post_type( $course_step_parents[0] ) ) && ( 'sfwd-topic' == get_post_type( $course_step_parents[1] ) ) ) {
$lesson_id = $course_step_parents[0];
$topic_id = $course_step_parents[1];
if ( ( isset( $user_course_progress[ $course_id ]['topics'][ $lesson_id ][ $topic_id ] ) ) && ( true == $user_course_progress[ $course_id ]['topics'][ $lesson_id ][ $topic_id ] ) ) {
$user_course_progress[ $course_id ]['topics'][ $lesson_id ][ $topic_id ] = 0;
$user_course_progress[ $course_id ]['completed'] -= 1;
/**
* Fires on marking a course incomplete.
*
* @param int $user_id User ID.
* @param int $course_id Course ID.
* @param int $topic_id Topic ID.
*/
do_action( 'learndash_mark_incomplete_process', $user_id, $course_id, $topic_id );
$topic_args = array(
'course_id' => $course_id,
'user_id' => $user_id,
'post_id' => $topic_id,
'activity_type' => 'topic',
);
$topic_activity = learndash_get_user_activity( $topic_args );
if ( $topic_activity ) {
$topic_activity = (array) $topic_activity;
$topic_activity['activity_status'] = false;
$topic_activity['activity_completed'] = false;
learndash_update_user_activity( $topic_activity );
}
}
if ( ( isset( $user_course_progress[ $course_id ]['lessons'][ $lesson_id ] ) ) && ( true == $user_course_progress[ $course_id ]['lessons'][ $lesson_id ] ) ) {
$user_course_progress[ $course_id ]['lessons'][ $lesson_id ] = 0;
$user_course_progress[ $course_id ]['completed'] -= 1;
/** This filter is documented in includes/course/ld-course-progress.php */
do_action( 'learndash_mark_incomplete_process', $user_id, $course_id, $lesson_id );
$lesson_args = array(
'course_id' => $course_id,
'user_id' => $user_id,
'post_id' => $lesson_id,
'activity_type' => 'lesson',
);
$lesson_activity = learndash_get_user_activity( $lesson_args );
if ( $lesson_args ) {
$lesson_args = (array) $lesson_args;
$lesson_args['activity_status'] = false;
$lesson_args['activity_completed'] = false;
learndash_update_user_activity( $lesson_args );
}
}
} elseif ( ( 1 == count( $course_step_parents ) ) && ( 'sfwd-lessons' == get_post_type( $course_step_parents[0] ) ) ) {
$lesson_id = $course_step_parents[0];
if ( ( isset( $user_course_progress[ $course_id ]['lessons'][ $lesson_id ] ) ) && ( true == $user_course_progress[ $course_id ]['lessons'][ $lesson_id ] ) ) {
$user_course_progress[ $course_id ]['lessons'][ $lesson_id ] = 0;
$user_course_progress[ $course_id ]['completed'] -= 1;
/** This filter is documented in includes/course/ld-course-progress.php */
do_action( 'learndash_mark_incomplete_process', $user_id, $course_id, $lesson_id );
$lesson_args = array(
'course_id' => $course_id,
'user_id' => $user_id,
'post_id' => $lesson_id,
'activity_type' => 'lesson',
);
$lesson_activity = learndash_get_user_activity( $lesson_args );
if ( $lesson_args ) {
$lesson_args = (array) $lesson_args;
$lesson_args['activity_status'] = false;
$lesson_args['activity_completed'] = false;
learndash_update_user_activity( $lesson_args );
}
}
}
}
} elseif ( 'sfwd-topic' === $step_post_type ) {
$step_parent_id = learndash_course_get_single_parent_step( $course_id, $step_id );
if ( ! empty( $step_parent_id ) ) {
if ( ( isset( $user_course_progress[ $course_id ]['topics'][ $step_parent_id ][ $step_id ] ) ) && ( true == $user_course_progress[ $course_id ]['topics'][ $step_parent_id ][ $step_id ] ) ) {
$user_course_progress[ $course_id ]['topics'][ $step_parent_id ][ $step_id ] = 0;
$user_course_progress[ $course_id ]['completed'] -= 1;
/** This filter is documented in includes/course/ld-course-progress.php */
do_action( 'learndash_mark_incomplete_process', $user_id, $course_id, $step_id );
$topic_args = array(
'course_id' => $course_id,
'user_id' => $user_id,
'post_id' => $step_id,
'activity_type' => 'topic',
);
$topic_activity = learndash_get_user_activity( $topic_args );
if ( $topic_activity ) {
$topic_activity = (array) $topic_activity;
$topic_activity['activity_status'] = false;
$topic_activity['activity_completed'] = false;
learndash_update_user_activity( $topic_activity );
}
}
if ( ( isset( $user_course_progress[ $course_id ]['lessons'][ $step_parent_id ] ) ) && ( true == $user_course_progress[ $course_id ]['lessons'][ $step_parent_id ] ) ) {
$user_course_progress[ $course_id ]['lessons'][ $step_parent_id ] = 0;
$user_course_progress[ $course_id ]['completed'] -= 1;
/** This filter is documented in includes/course/ld-course-progress.php */
do_action( 'learndash_mark_incomplete_process', $user_id, $course_id, $step_parent_id );
$lesson_args = array(
'course_id' => $course_id,
'user_id' => $user_id,
'post_id' => $step_parent_id,
'activity_type' => 'lesson',
);
$lesson_activity = learndash_get_user_activity( $lesson_args );
if ( $lesson_args ) {
$lesson_args = (array) $lesson_args;
$lesson_args['activity_status'] = false;
$lesson_args['activity_completed'] = false;
learndash_update_user_activity( $lesson_args );
}
}
}
} elseif ( 'sfwd-lessons' === $step_post_type ) {
if ( ( isset( $user_course_progress[ $course_id ]['lessons'][ $step_id ] ) ) && ( true == $user_course_progress[ $course_id ]['lessons'][ $step_id ] ) ) {
$user_course_progress[ $course_id ]['lessons'][ $step_id ] = 0;
$user_course_progress[ $course_id ]['completed'] -= 1;
/** This filter is documented in includes/course/ld-course-progress.php */
do_action( 'learndash_mark_incomplete_process', $user_id, $course_id, $step_id );
$lesson_args = array(
'course_id' => $course_id,
'user_id' => $user_id,
'post_id' => $step_id,
'activity_type' => 'lesson',
);
$lesson_activity = learndash_get_user_activity( $lesson_args );
if ( $lesson_args ) {
$lesson_args = (array) $lesson_args;
$lesson_args['activity_status'] = false;
$lesson_args['activity_completed'] = false;
learndash_update_user_activity( $lesson_args );
}
}
}
if ( ! isset( $user_course_progress[ $course_id ] ) ) {
return;
}
if ( isset( $user_course_progress[ $course_id ]['completed'] ) ) {
$user_course_progress[ $course_id ]['completed'] = absint( $user_course_progress[ $course_id ]['completed'] );
} else {
$user_course_progress[ $course_id ]['completed'] = 0;
}
if ( isset( $user_course_progress[ $course_id ]['total'] ) ) {
$user_course_progress[ $course_id ]['total'] = absint( $user_course_progress[ $course_id ]['total'] );
} else {
$user_course_progress[ $course_id ]['total'] = 0;
}
if ( $user_course_progress[ $course_id ]['completed'] !== $user_course_progress[ $course_id ]['total'] ) {
delete_user_meta( $user_id, 'course_completed_' . $course_id );
/** This filter is documented in includes/course/ld-course-progress.php */
do_action( 'learndash_mark_incomplete_process', $user_id, $course_id, $course_id );
$course_args = array(
'course_id' => $course_id,
'user_id' => $user_id,
'post_id' => $course_id,
'activity_type' => 'course',
);
$course_activity = learndash_get_user_activity( $course_args );
if ( $course_args ) {
$course_args = (array) $course_args;
$course_args['activity_status'] = false;
$course_args['activity_completed'] = false;
learndash_update_user_activity( $course_args );
}
}
return update_user_meta( $user_id, '_sfwd-course_progress', $user_course_progress );
}
/**
* Gets the quiz attempt meta for a given user.
*
* @since 2.3.0
*
* @param int $user_id Optional. User ID. Default 0.
* @param array $args Optional. An array of items to match. Default empty array.
*
* @return array An array of user quiz attempt meta.
*/
function learndash_get_user_quiz_attempt( $user_id = 0, $args = array() ) {
if ( ( ! empty( $user_id ) ) && ( ! empty( $args ) ) ) {
$user_quizzes = get_user_meta( $user_id, '_sfwd-quizzes', true );
if ( ! empty( $user_quizzes ) ) {
foreach ( $user_quizzes as $idx => $user_quiz ) {
foreach ( $args as $arg_key => $arg_val ) {
if ( ( ! isset( $user_quiz[ $arg_key ] ) ) || ( $user_quiz[ $arg_key ] != $arg_val ) ) {
unset( $user_quizzes[ $idx ] );
}
}
}
}
return $user_quizzes;
}
return array();
}
/**
* Removes the quiz attempt meta for a given user.
*
* @since 2.5.0
*
* @param int $user_id Optional. User ID. Default 0.
* @param array $args Optional. An array of items to match. Default empty array.
*
* @return array An array of updated quiz attempt meta.
*/
function learndash_remove_user_quiz_attempt( $user_id = 0, $args = array() ) {
if ( ( ! empty( $user_id ) ) && ( ! empty( $args ) ) ) {
$changes = false;
$user_quizzes = get_user_meta( $user_id, '_sfwd-quizzes', true );
if ( ! empty( $user_quizzes ) ) {
$changed_user_quizzes = array();
foreach ( $user_quizzes as $idx => $user_quiz ) {
$match_found = true;
foreach ( $args as $arg_key => $arg_val ) {
if ( ( ! isset( $user_quiz[ $arg_key ] ) ) || ( $user_quiz[ $arg_key ] != $arg_val ) ) {
$match_found = false;
break;
}
}
if ( true === $match_found ) {
/**
* Fires before user single quiz attempt has been removed.
*
* @since 3.2.3
*
* @param int $user_id User ID for the Quiz.
* @param array $user_quiz User meta quiz item to be deleted.
*/
do_action( 'learndash_user_quiz_delete_single_before', $user_id, $user_quiz );
if ( ( isset( $user_quiz['time'] ) ) && ( ! empty( $user_quiz['time'] ) ) ) {
if ( ( isset( $user_quiz['pro_quizid'] ) ) && ( ! empty( $user_quiz['pro_quizid'] ) ) ) {
learndash_quiz_remove_user_toplist( $user_id, $user_quiz['time'], $user_quiz['pro_quizid'] );
}
}
// If we have a statistics reference we need to remove this set of records.
if ( ( isset( $user_quiz['statistic_ref_id'] ) ) && ( ! empty( $user_quiz['statistic_ref_id'] ) ) ) {
learndash_quiz_remove_user_statistics_by_ref( $user_quiz['statistic_ref_id'] );
}
if ( ( ! isset( $user_quiz['course'] ) ) || ( empty( $user_quiz['course'] ) ) ) {
$user_quiz['course'] = learndash_get_course_id( $user_quiz['quiz'] );
}
// If this quiz has graded items they all need to be moved to trash or deleted.
if ( ( isset( $user_quiz['graded'] ) ) && ( ! empty( $user_quiz['graded'] ) ) ) {
foreach ( $user_quiz['graded'] as $question_id => $graded_set ) {
if ( ( isset( $graded_set['post_id'] ) ) && ( ! empty( $graded_set['post_id'] ) ) ) {
wp_delete_post( $graded_set['post_id'], true );
}
}
}
// Remove the user activity record.
$quiz_args = array(
'course_id' => isset( $user_quiz['course'] ) ? absint( $user_quiz['course'] ) : 0,
'user_id' => $user_id,
'post_id' => $user_quiz['quiz'],
'activity_type' => 'quiz',
'activity_completed' => isset( $user_quiz['completed'] ) ? absint( $user_quiz['completed'] ) : 0,
);
$quiz_activity = learndash_get_user_activity( $quiz_args );
if ( ! empty( $quiz_activity ) ) {
learndash_delete_user_activity( $quiz_activity->activity_id );
}
$changed_user_quizzes[] = $user_quiz;
unset( $user_quizzes[ $idx ] );
$changes = true;
/**
* Fires after user single quiz attempt has been removed.
*
* @since 3.2.3
*
* @param int $user_id User ID for the Quiz.
* @param array $user_quiz User meta quiz item to be deleted.
*/
do_action( 'learndash_user_quiz_delete_single_after', $user_id, $user_quiz );
}
}
if ( true === $changes ) {
// If not empty then we reset the keys.
if ( ! empty( $user_quizzes ) ) {
$user_quizzes = array_values( $user_quizzes );
}
update_user_meta( $user_id, '_sfwd-quizzes', $user_quizzes );
if ( ! empty( $changed_user_quizzes ) ) {
/**
* Fires after user all quiz attempts have been removed.
*
* @since 3.2.3
*
* @param int $user_id User ID for the Quiz.
* @param array $changed_user_quizzes Array of all quiz items deleted.
*/
do_action( 'learndash_user_quiz_delete_all_after', $user_id, $changed_user_quizzes );
foreach ( $changed_user_quizzes as $user_quiz ) {
if ( ! learndash_is_quiz_complete( $user_id, $user_quiz['quiz'], $user_quiz['course'] ) ) {
learndash_process_mark_incomplete( $user_id, $user_quiz['course'], $user_quiz['quiz'], false );
}
/**
* Legacy: Call the `learndash_process_mark_complete` function after the quiz entry is removed.
*
* The call to this function was removed as of LD 3.5.1 because it should not be needed. But
* retaining for legacy purposes.
*
* @since 3.5.1
*
* @param bool $call_function Default is false.
* @param array $user_quiz User meta quiz item to be deleted.
* @param int $user_id User ID for the Quiz.
*/
if ( true === apply_filters( 'learndash_quiz_call_mark_complete_after_remove', false, $user_quiz, $user_id ) ) {
learndash_process_mark_complete( $user_id, $user_quiz['quiz'], false, $user_quiz['course'] );
}
}
}
}
}
return $user_quizzes;
}
return array();
}
/**
* Outputs HTML output to mark a step incomplete.
*
* Must meet requirements of course to mark incomplete.
*
* @since 3.1.4
*
* @param WP_Post $post The `WP_Post` for lesson, topic.
* @param array $atts Optional. An array of attributes for mark incomplete output. Default empty array.
*
* @return string The HTML output to mark course incomplete.
*/
function learndash_show_mark_incomplete( $post, $atts = array() ) {
$course_mark_incomplete_enabled = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Management_Display', 'course_mark_incomplete_enabled', false );
if ( empty( $course_mark_incomplete_enabled ) ) {
return '';
}
if ( ! is_user_logged_in() ) {
return '';
}
$user_id = get_current_user_id();
$course_id = learndash_get_course_id( $post->ID );
/**
* Filters whether to show mark course incomplete form.
*
* @param boolean $show_form Whether to show mark incomplete form.
* @param int $course_id Course ID.
* @param WP_Post $post `WP_Post` object being displayed.
* @param array $atts An array of attributes to mark a course incomplete.
*/
if ( ! apply_filters( 'learndash_show_mark_incomplete_form', true, $course_id, $post, $atts ) ) {
return '';
}
/**
* Filters attributes to mark a course incomplete.
*
* @param array $atts An array of attributes to mark a course incomplete.
* @param WP_Post $post `WP_Post` object being displayed.
*/
$atts = apply_filters( 'learndash_mark_incomplete_form_atts', $atts, $post );
if ( isset( $atts['form']['id'] ) ) {
$form_id = ' id="' . esc_attr( $atts['form']['id'] ) . '" ';
} else {
$form_id = '';
}
if ( isset( $atts['form']['class'] ) ) {
$form_class = ' class="sfwd-mark-incomplete sfwd-mark-complete ' . esc_attr( $atts['form']['class'] ) . '" ';
} else {
$form_class = ' class="sfwd-mark-incomplete sfwd-mark-complete" ';
}
if ( isset( $atts['button']['id'] ) ) {
$button_id = ' id="' . esc_attr( $atts['button']['id'] ) . '" ';
} else {
$button_id = '';
}
$button_disabled = '';
if ( isset( $atts['button']['class'] ) ) {
$button_class = ' class="learndash_mark_incomplete_button learndash_mark_complete_button ' . esc_attr( $atts['button']['class'] ) . '" ';
} else {
$button_class = ' class="learndash_mark_incomplete_button learndash_mark_complete_button" ';
}
$button_label = LearnDash_Custom_Label::get_label( 'button_mark_incomplete' );
if ( empty( $button_label ) ) {
$button_label = esc_html__( 'Mark Incomplete', 'learndash' );
}
$form_fields = '<input type="hidden" value="' . $post->ID . '" name="post" />
<input type="hidden" value="' . learndash_get_course_id( $post->ID ) . '" name="course_id" />
<input type="hidden" value="' . wp_create_nonce( 'sfwd_mark_incomplete_' . $user_id . '_' . $post->ID ) . '" name="sfwd_mark_incomplete" />
<input type="submit" ' . $button_id . ' value="' . esc_attr( $button_label ) . '" ' . $button_class . ' ' . $button_disabled . '/>';
/** This filter is documented in includes/course/ld-course-progress.php */
$form_fields = apply_filters( 'learndash_mark_complete_form_fields', $form_fields, $post );
$return = '<form ' . $form_id . ' ' . $form_class . ' method="post" action="">' . $form_fields . '</form>';
return $return;
}
/**
* Processes the request to mark a course or step incomplete.
*
* @since 3.1.4
*
* @global WP_Post $post Global post object.
*/
function learndash_mark_incomplete_process() {
if ( ( isset( $_POST['sfwd_mark_incomplete'] ) ) && ( ! empty( $_POST['sfwd_mark_incomplete'] ) ) && ( isset( $_POST['post'] ) ) && ( ! empty( $_POST['post'] ) ) ) {
$post_id = intval( $_POST['post'] );
if ( isset( $_POST['course_id'] ) ) {
$course_id = intval( $_POST['course_id'] );
} else {
$course_id = learndash_get_course_id( $post_id );
}
if ( isset( $_POST['userid'] ) ) {
$user_id = intval( $_POST['userid'] );
} else {
if ( ! is_user_logged_in() ) {
return;
}
$user_id = get_current_user_id();
}
/**
* Verify the form is valid
*
* @since 3.1.4
*/
if ( ! wp_verify_nonce( $_POST['sfwd_mark_incomplete'], 'sfwd_mark_incomplete_' . $user_id . '_' . $post_id ) ) {
return;
}
return learndash_process_mark_incomplete( $user_id, $course_id, $post_id, false );
}
return false;
}
add_action( 'wp', 'learndash_mark_incomplete_process' );