-
Notifications
You must be signed in to change notification settings - Fork 199
/
class-sensei-utils.php
3172 lines (2717 loc) · 92 KB
/
class-sensei-utils.php
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
<?php
use Sensei\Internal\Student_Progress\Course_Progress\Models\Course_Progress_Interface;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Sensei Utilities Class
*
* Common utility functions for Sensei.
*
* @package Core
* @author Automattic
*
* @since 1.0.0
*/
class Sensei_Utils {
const WC_INFORMATION_TRANSIENT = 'sensei_woocommerce_plugin_information';
/**
* Get the placeholder thumbnail image.
*
* @access public
* @since 1.0.0
* @return string The URL to the placeholder thumbnail image.
*/
public static function get_placeholder_image() {
/**
* Filter the placeholder thumbnail image.
*
* @hook sensei_placeholder_thumbnail
*
* @param {string} $placeholder_image_url The URL to the placeholder thumbnail image.
* @return {string} The URL to the placeholder thumbnail image.
*/
return esc_url( apply_filters( 'sensei_placeholder_thumbnail', Sensei()->plugin_url . 'assets/images/placeholder.png' ) );
}
/**
* Log an activity item.
*
* @access public
* @since 1.0.0
* @param array $args (default: array())
* @return bool | int
*/
public static function sensei_log_activity( $args = array() ) {
global $wpdb;
// Args, minimum data required for WP
$data = array(
'comment_post_ID' => intval( $args['post_id'] ),
'comment_author' => '', // Not needed
'comment_author_email' => '', // Not needed
'comment_author_url' => '', // Not needed
'comment_content' => ! empty( $args['data'] ) ? esc_html( $args['data'] ) : '',
'comment_type' => esc_attr( $args['type'] ),
'user_id' => intval( $args['user_id'] ),
'comment_approved' => ! empty( $args['status'] ) ? esc_html( $args['status'] ) : 'log',
);
// Allow extra data
if ( ! empty( $args['username'] ) ) {
$data['comment_author'] = sanitize_user( $args['username'] );
}
if ( ! empty( $args['user_email'] ) ) {
$data['comment_author_email'] = sanitize_email( $args['user_email'] );
}
if ( ! empty( $args['user_url'] ) ) {
$data['comment_author_url'] = esc_url( $args['user_url'] );
}
if ( ! empty( $args['parent'] ) ) {
$data['comment_parent'] = $args['parent'];
}
// Sanity check
if ( empty( $args['user_id'] ) ) {
_deprecated_argument( __FUNCTION__, '1.0', esc_html__( 'At no point should user_id be equal to 0.', 'sensei-lms' ) );
return false;
}
/**
* This action runs before logging the activity.
*
* @hook sensei_log_activity_before
*
* @param {array} $args Initial arguments.
* @param {array} $data Processed data to log.
*/
do_action( 'sensei_log_activity_before', $args, $data );
// Custom Logic
// Check if comment exists first
$comment_id = $wpdb->get_var( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND user_id = %d AND comment_type = %s ", $args['post_id'], $args['user_id'], $args['type'] ) );
if ( ! $comment_id ) {
// Add the comment
$comment_id = wp_insert_comment( $data );
} elseif ( isset( $args['action'] ) && 'update' == $args['action'] ) {
// Update the comment if an update was requested
$data['comment_ID'] = $comment_id;
// By default update the timestamp of the comment
if ( empty( $args['keep_time'] ) ) {
$data['comment_date'] = current_time( 'mysql' );
}
wp_update_comment( $data );
}
/**
* Fires after logging the activity.
*
* @hook sensei_log_activity_after
*
* @param {array} $args Initial arguments.
* @param {array} $data Processed data to log.
* @param {int} $comment_id The comment ID of the logged activity.
*/
do_action( 'sensei_log_activity_after', $args, $data, $comment_id );
Sensei()->flush_comment_counts_cache( $args['post_id'] );
if ( 0 < $comment_id ) {
// Return the ID so that it can be used for meta data storage
return $comment_id;
} else {
return false;
}
}
/**
* Check for Sensei activity.
*
* @access public
* @since 1.0.0
* @param array $args (default: array())
* @param bool $return_comments (default: false)
* @return mixed | int
*/
public static function sensei_check_for_activity( $args = array(), $return_comments = false ) {
if ( ! $return_comments ) {
$args['count'] = true;
}
// A user ID of 0 is invalid, so shortcut this.
if ( isset( $args['user_id'] ) && 0 === intval( $args['user_id'] ) ) {
_deprecated_argument( __FUNCTION__, '1.0', esc_html__( 'At no point should user_id be equal to 0.', 'sensei-lms' ) );
return false;
}
if ( ! isset( $args['status'] ) ) {
$args['status'] = 'any';
}
/**
* This action runs before getting the comments for the given request.
*
* @hook sensei_utils_check_for_activity_before_get_comments
*
* @param {array} $args Search arguments.
*/
do_action( 'sensei_utils_check_for_activity_before_get_comments', $args );
/**
* This filter runs inside Sensei_Utils::sensei_check_for_activity
*
* It runs while getting the comments for the given request.
*
* @hook sensei_check_for_activity
*
* @param {int|array} $comments Activity to filter.
* @param {array} $args Search arguments.
* @return {int|array} Filtered activity.
*/
$comments = apply_filters( 'sensei_check_for_activity', get_comments( $args ), $args );
/**
* This action runs after getting the comments for the given request.
*
* @hook sensei_utils_check_for_activity_after_get_comments
*
* @param {array} $args Search arguments.
* @param {int|array} $comments Activity.
*/
do_action( 'sensei_utils_check_for_activity_after_get_comments', $args, $comments );
// Return comments.
if ( $return_comments ) {
// Could check for array of 1 and just return the 1 item?
if ( is_array( $comments ) && 1 == count( $comments ) ) {
$comments = array_shift( $comments );
}
return $comments;
}
// Count comments.
return intval( $comments ); // This is the count, check the return from WP_Comment_Query.
}
/**
* Get IDs of Sensei activity items.
*
* @access public
* @since 1.0.0
* @param array $args (default: array())
* @return array
*/
public static function sensei_activity_ids( $args = array() ) {
$comments = self::sensei_check_for_activity( $args, true );
// Need to always use an array, even with only 1 item
if ( ! is_array( $comments ) ) {
$comments = array( $comments );
}
$post_ids = array();
// Count comments
if ( is_array( $comments ) && ( 0 < intval( count( $comments ) ) ) ) {
foreach ( $comments as $key => $value ) {
// Add matches to id array
if ( isset( $args['field'] ) && 'comment' == $args['field'] ) {
array_push( $post_ids, $value->comment_ID );
} elseif ( isset( $args['field'] ) && 'user_id' == $args['field'] ) {
array_push( $post_ids, $value->user_id );
} else {
array_push( $post_ids, $value->comment_post_ID );
}
}
// Reset array indexes
$post_ids = array_unique( $post_ids );
$post_ids = array_values( $post_ids );
}
return $post_ids;
}
/**
* Delete Sensei activities.
*
* @access public
* @since 1.0.0
* @param array $args (default: array())
* @return boolean
*/
public static function sensei_delete_activities( $args = array() ) {
$dataset_changes = false;
// If activity exists remove activity from log
$comments = self::sensei_check_for_activity(
array(
'post_id' => intval( $args['post_id'] ),
'user_id' => intval( $args['user_id'] ),
'type' => esc_attr( $args['type'] ),
),
true
);
if ( $comments ) {
// Need to always return an array, even with only 1 item
if ( ! is_array( $comments ) ) {
$comments = array( $comments );
}
foreach ( $comments as $key => $value ) {
if ( isset( $value->comment_ID ) && 0 < $value->comment_ID ) {
$dataset_changes = wp_delete_comment( intval( $value->comment_ID ), true );
}
}
}
Sensei()->flush_comment_counts_cache( $args['post_id'] );
return $dataset_changes;
}
/**
* Delete all activity for specified user.
*
* @access public
* @since 1.5.0
*
* @deprecated 3.0.0 Use `\Sensei_Learner::delete_all_user_activity` instead.
*
* @param integer $user_id User ID.
* @return boolean
*/
public static function delete_all_user_activity( $user_id = 0 ) {
_deprecated_function( __METHOD__, '3.0.0', 'Sensei_Learner::delete_all_user_activity' );
return \Sensei_Learner::instance()->delete_all_user_activity( $user_id );
}
/**
* Get value for a specified activity.
*
* @access public
* @since 1.0.0
* @param array $args (default: array())
* @return string
*/
public static function sensei_get_activity_value( $args = array() ) {
$activity_value = false;
if ( ! empty( $args['field'] ) ) {
$comment = self::sensei_check_for_activity( $args, true );
if ( isset( $comment->{$args['field']} ) && '' != $comment->{$args['field']} ) {
$activity_value = $comment->{$args['field']};
}
}
return $activity_value;
}
/**
* Load the WordPress rich text editor
*
* @param string $content Initial content for editor
* @param string $editor_id ID of editor (only lower case characters - no spaces, underscores, hyphens, etc.)
* @param string $input_name Name for text area form element
* @return void
*/
public static function sensei_text_editor( $content = '', $editor_id = 'senseitexteditor', $input_name = '' ) {
if ( ! $input_name ) {
$input_name = $editor_id;
}
$buttons = 'bold,italic,underline,strikethrough,blockquote,bullist,numlist,justifyleft,justifycenter,justifyright,undo,redo,pastetext';
$settings = array(
'media_buttons' => false,
'wpautop' => true,
'textarea_name' => $input_name,
'editor_class' => 'sensei_text_editor',
'teeny' => false,
'dfw' => false,
'editor_css' => '<style> .mce-top-part button { background-color: rgba(0,0,0,0); } </style>',
'tinymce' => array(
'theme_advanced_buttons1' => $buttons,
'theme_advanced_buttons2' => '',
'setup' => 'function (editor) {
tinymce.dom.ScriptLoader.ScriptLoader.add("' . Sensei()->assets->asset_url( 'js/question-answer-tinymce-editor.js' ) . '");
tinymce.dom.ScriptLoader.ScriptLoader.loadQueue(function() {
window.addPlaceholderInTinymceEditor(editor);
});
}
',
),
'quicktags' => false,
);
if ( false !== strpos( $input_name, 'sensei_question[' ) ) {
// Only pick the global style variables. TinyMCE loads in an iFrame, so none of our global
// variables are available inside it. We add them here manually.
$global_variables = str_replace( '"', "'", wp_get_global_stylesheet( [ 'variables' ] ) );
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, Squiz.Strings.DoubleQuoteUsage.NotRequired -- Using local file and need double quote for newline.
$question_editor_styles = str_replace( "\n", "", file_get_contents( Sensei()->assets->dist_path( 'css/question-answer-tinymce-editor.css' ) ) );
$settings['tinymce']['content_style'] = $global_variables . ' ' . $question_editor_styles;
}
wp_editor( $content, $editor_id, $settings );
}
public static function upload_file( $file = array() ) {
require_once ABSPATH . 'wp-admin/includes/admin.php';
/**
* Filter the data array for the Sensei wp_handle_upload function call
*
* This filter was mainly added for Unit Testing purposes.
*
* @since 1.7.4
*
* @hook sensei_file_upload_args
*
* @param {array} $file_upload_args Array of current values.
* @property {string} `$file_upload_args['test_form']` Set to false by default.
* @return {array} Filtered data array.
*/
$file_upload_args = apply_filters( 'sensei_file_upload_args', array( 'test_form' => false ) );
/**
* Customize the prefix prepended onto files uploaded in Sensei.
*
* @since 3.9.0
*
* @hook sensei_file_upload_file_prefix
*
* @param {string} $prefix Prefix to prepend to uploaded files.
* @param {array} $file Arguments with uploaded file information.
* @return {string} Filtered prefix.
*/
$file_prefix = apply_filters( 'sensei_file_upload_file_prefix', substr( md5( uniqid() ), 0, 7 ) . '_', $file );
$file['name'] = $file_prefix . $file['name'];
$file_return = wp_handle_upload( $file, $file_upload_args );
if ( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url'],
);
$attachment_id = wp_insert_attachment( $attachment, $filename );
require_once ABSPATH . 'wp-admin/includes/image.php';
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
if ( 0 < intval( $attachment_id ) ) {
return $attachment_id;
}
}
return false;
}
/**
* Grade quiz
*
* @param integer $quiz_id ID of quiz.
* @param float $grade Grade received.
* @param integer $user_id ID of user being graded.
* @param string $quiz_grade_type default 'auto'.
*
* @return boolean
*/
public static function sensei_grade_quiz( $quiz_id = 0, $grade = 0, $user_id = 0, $quiz_grade_type = 'auto' ): bool {
$user_id = $user_id ? $user_id : get_current_user_id();
if ( ! $quiz_id || ! $user_id ) {
return false;
}
$quiz_submission = Sensei()->quiz_submission_repository->get( $quiz_id, $user_id );
if ( ! $quiz_submission ) {
return false;
}
$quiz_submission->set_final_grade( $grade );
Sensei()->quiz_submission_repository->save( $quiz_submission );
$quiz_passmark = absint( get_post_meta( $quiz_id, '_quiz_passmark', true ) );
/**
* Fires when a user quiz is graded.
*
* @hook sensei_user_quiz_grade
*
* @param {int} $user_id ID of user being graded.
* @param {int} $quiz_id ID of quiz.
* @param {float} $grade Grade received.
* @param {int} $quiz_passmark Passmark for quiz.
* @param {string} $quiz_grade_type Type of grading.
*/
do_action( 'sensei_user_quiz_grade', $user_id, $quiz_id, $grade, $quiz_passmark, $quiz_grade_type );
return true;
}
/**
* Grade question
*
* @deprecated 4.19.2
*
* @param integer $question_id ID of question
* @param integer $grade Grade received
* @param int $user_id
* @return boolean
*/
public static function sensei_grade_question( $question_id = 0, $grade = 0, $user_id = 0 ) {
_deprecated_function( __METHOD__, '4.19.2', 'Sensei_Quiz::set_user_grades' );
if ( intval( $user_id ) == 0 ) {
$user_id = get_current_user_id();
}
$activity_logged = false;
if ( intval( $question_id ) > 0 && intval( $user_id ) > 0 ) {
$user_answer_id = self::sensei_get_activity_value(
array(
'post_id' => $question_id,
'user_id' => $user_id,
'type' => 'sensei_user_answer',
'field' => 'comment_ID',
)
);
$activity_logged = update_comment_meta( $user_answer_id, 'user_grade', $grade );
$answer_notes = get_post_meta( $question_id, '_answer_feedback', true );
if ( ! empty( $answer_notes ) ) {
update_comment_meta( $user_answer_id, 'answer_note', base64_encode( $answer_notes ) );
}
}
return $activity_logged;
}
/**
* Delete the question grade.
*
* @deprecated 4.19.2
*
* @param int $question_id The question ID.
* @param int $user_id The user ID. Defaults to the current user ID.
*
* @return bool
*/
public static function sensei_delete_question_grade( $question_id = 0, $user_id = 0 ) {
_deprecated_function( __METHOD__, '4.19.2', 'Sensei_Quiz::set_user_grades' );
if ( intval( $user_id ) == 0 ) {
$user_id = get_current_user_id();
}
$activity_logged = false;
if ( intval( $question_id ) > 0 ) {
$user_answer_id = self::sensei_get_activity_value(
array(
'post_id' => $question_id,
'user_id' => $user_id,
'type' => 'sensei_user_answer',
'field' => 'comment_ID',
)
);
$activity_logged = delete_comment_meta( $user_answer_id, 'user_grade' );
}
return $activity_logged;
}
/**
* Alias to Woothemes_Sensei_Utils::sensei_start_lesson
*
* @since 1.7.4
*
* @param integer $user_id
* @param integer $lesson_id
* @param bool $complete
*
* @return mixed boolean or comment_ID
*/
public static function user_start_lesson( $user_id = 0, $lesson_id = 0, $complete = false ) {
return self::sensei_start_lesson( $lesson_id, $user_id, $complete );
}
/**
* Mark a lesson as started for user
*
* Will also start the lesson course for the user if the user hasn't started taking it already.
*
* @since 1.6.0
*
* @param integer $lesson_id ID of lesson
* @param int| string $user_id default 0
* @param bool $complete default false
*
* @return mixed boolean or comment_ID
*/
public static function sensei_start_lesson( $lesson_id = 0, $user_id = 0, $complete = false ) {
if ( 0 === (int) $user_id ) {
$user_id = get_current_user_id();
}
if ( 0 >= (int) $lesson_id ) {
return false;
}
$course_id = get_post_meta( $lesson_id, '_lesson_course', true );
if ( $course_id ) {
$is_user_taking_course = self::has_started_course( $course_id, $user_id );
if ( ! $is_user_taking_course ) {
self::user_start_course( $user_id, $course_id );
}
}
/**
* Fires when a user starts a lesson.
* When this action runs the lesson status may not yet exist.
*
* @hook sensei_user_lesson_start
*
* @param {int} $user_id ID of user starting lesson.
* @param {int} $lesson_id ID of lesson being started.
*/
do_action( 'sensei_user_lesson_start', $user_id, $lesson_id );
$lesson_progress = Sensei()->lesson_progress_repository->get( $lesson_id, $user_id );
if ( ! $lesson_progress ) {
$lesson_progress = Sensei()->lesson_progress_repository->create( $lesson_id, $user_id );
$has_questions = Sensei_Lesson::lesson_quiz_has_questions( $lesson_id );
if ( $complete && $has_questions ) {
update_comment_meta( $lesson_progress->get_id(), 'grade', 0 );
}
}
if ( $complete && ! $lesson_progress->is_complete() ) {
$lesson_progress->complete();
Sensei()->lesson_progress_repository->save( $lesson_progress );
}
if ( $complete ) {
/**
* Fires when a user completes a lesson.
*
* This hook is fired when a user completes a lesson, passes a quiz or their quiz submission was graded.
* Therefore the corresponding lesson is marked as complete.
*
* @since 1.7.0
*
* @hook sensei_user_lesson_end
*
* @param {int} $user_id The user ID.
* @param {int} $lesson_id The lesson ID.
*/
do_action( 'sensei_user_lesson_end', $user_id, $lesson_id );
}
return $lesson_progress->get_id();
}
/**
* Remove user from lesson, deleting all data from the corresponding quiz
*
* @param int $lesson_id The lesson ID.
* @param int $user_id The user ID.
* @param bool $from_course Whether the user is being removed from a course.
* @return boolean
*/
public static function sensei_remove_user_from_lesson( $lesson_id = 0, $user_id = 0, $from_course = false ) {
if ( ! $lesson_id ) {
return false;
}
if ( intval( $user_id ) == 0 ) {
$user_id = get_current_user_id();
}
// Process quiz
$lesson_quiz_id = Sensei()->lesson->lesson_quizzes( $lesson_id );
// Delete quiz answers, this auto deletes the corresponding meta data, such as the question/answer grade
self::sensei_delete_quiz_answers( $lesson_quiz_id, $user_id );
// Delete quiz saved answers
Sensei()->quiz->reset_user_lesson_data( $lesson_id, $user_id );
// Delete lesson progress.
$lesson_progress = Sensei()->lesson_progress_repository->get( $lesson_id, $user_id );
if ( $lesson_progress ) {
Sensei()->lesson_progress_repository->delete( $lesson_progress );
}
if ( ! $from_course ) {
/**
* Fires when user progress is reset in a lesson.
*
* @hook sensei_user_lesson_reset
*
* @param {int} $user_id The user ID.
* @param {int} $lesson_id The lesson ID.
*/
do_action( 'sensei_user_lesson_reset', $user_id, $lesson_id );
}
return true;
}
/**
* Remove a user from a course, deleting all activities across all lessons
*
* @param int $course_id
* @param int $user_id
* @return boolean
*/
public static function sensei_remove_user_from_course( $course_id = 0, $user_id = 0 ) {
if ( ! $course_id ) {
return false;
}
if ( intval( $user_id ) == 0 ) {
$user_id = get_current_user_id();
}
$lesson_ids = Sensei()->course->course_lessons( $course_id, 'any', 'ids' );
foreach ( $lesson_ids as $lesson_id ) {
self::sensei_remove_user_from_lesson( $lesson_id, $user_id, true );
}
// Delete course progress.
$course_progress = Sensei()->course_progress_repository->get( $course_id, $user_id );
if ( $course_progress ) {
Sensei()->course_progress_repository->delete( $course_progress );
}
/**
* Fires when user progress is reset in a course.
*
* @hook sensei_user_course_reset
*
* @param {int} $user_id The user ID.
* @param {int} $course_id The course ID.
*/
do_action( 'sensei_user_course_reset', $user_id, $course_id );
return true;
}
public static function sensei_get_quiz_questions( $quiz_id = 0 ) {
$questions = array();
if ( intval( $quiz_id ) > 0 ) {
$questions = Sensei()->lesson->lesson_quiz_questions( $quiz_id );
$questions = self::array_sort_reorder( $questions );
}
return $questions;
}
public static function sensei_get_quiz_total( $quiz_id = 0 ) {
$quiz_total = 0;
if ( $quiz_id > 0 ) {
$questions = self::sensei_get_quiz_questions( $quiz_id );
$question_grade = 0;
foreach ( $questions as $question ) {
$question_grade = Sensei()->question->get_question_grade( $question->ID );
$quiz_total += $question_grade;
}
}
return $quiz_total;
}
/**
* Returns the user_grade for a specific question and user, or sensei_user_answer entry
*
* @deprecated 4.19.2
*
* @param mixed $question
* @param int $user_id
* @return string
*/
public static function sensei_get_user_question_grade( $question = 0, $user_id = 0 ) {
_deprecated_function( __METHOD__, '4.19.2', 'Sensei_Quiz::get_user_grades' );
$question_grade = false;
if ( $question ) {
if ( is_object( $question ) ) {
$user_answer_id = $question->comment_ID;
} else {
if ( intval( $user_id ) == 0 ) {
$user_id = get_current_user_id();
}
$user_answer_id = self::sensei_get_activity_value(
array(
'post_id' => intval( $question ),
'user_id' => $user_id,
'type' => 'sensei_user_answer',
'field' => 'comment_ID',
)
);
}
if ( $user_answer_id ) {
$question_grade = get_comment_meta( $user_answer_id, 'user_grade', true );
}
}
return $question_grade;
}
/**
* Delete the quiz answers and all related data including the grades.
*
* @param int $quiz_id The quiz ID.
* @param int $user_id The user ID.
*
* @return bool
*/
public static function sensei_delete_quiz_answers( $quiz_id = 0, $user_id = 0 ): bool {
if ( intval( $user_id ) === 0 ) {
$user_id = get_current_user_id();
}
if ( ! $quiz_id || ! $user_id ) {
return false;
}
$deleted = false;
$questions = self::sensei_get_quiz_questions( $quiz_id );
foreach ( $questions as $question ) {
// Fallback for pre 1.7.4 data.
$deleted = self::sensei_delete_activities(
array(
'post_id' => $question->ID,
'user_id' => $user_id,
'type' => 'sensei_user_answer',
)
);
}
$quiz_submission = Sensei()->quiz_submission_repository->get( $quiz_id, $user_id );
if ( $quiz_submission ) {
Sensei()->quiz_submission_repository->delete( $quiz_submission );
Sensei()->quiz_answer_repository->delete_all( $quiz_submission );
Sensei()->quiz_grade_repository->delete_all( $quiz_submission );
$deleted = true;
}
return $deleted;
}
/**
* Delete the quiz submission grade.
*
* @param int $quiz_id The quiz ID.
* @param int $user_id The user ID. Defaults to the current user ID.
*
* @return bool
*/
public static function sensei_delete_quiz_grade( $quiz_id = 0, $user_id = 0 ): bool {
if ( intval( $user_id ) === 0 ) {
$user_id = get_current_user_id();
}
if ( ! $quiz_id || ! $user_id ) {
return false;
}
$quiz_submission = Sensei()->quiz_submission_repository->get( $quiz_id, $user_id );
if ( ! $quiz_submission ) {
return false;
}
$quiz_submission->set_final_grade( null );
Sensei()->quiz_submission_repository->save( $quiz_submission );
return true;
}
/**
* Add answer notes to question
*
* @deprecated 4.19.2
*
* @param integer $question_id ID of question
* @param integer $user_id ID of user
* @param string $notes
* @return boolean
*/
public static function sensei_add_answer_notes( $question_id = 0, $user_id = 0, $notes = '' ) {
_deprecated_function( __METHOD__, '4.19.2', 'Sensei_Quiz::save_user_answers_feedback' );
if ( intval( $user_id ) == 0 ) {
$user_id = get_current_user_id();
}
$activity_logged = false;
if ( intval( $question_id ) > 0 ) {
$notes = base64_encode( $notes );
// Don't store empty values, no point
if ( ! empty( $notes ) ) {
$user_lesson_id = self::sensei_get_activity_value(
array(
'post_id' => $question_id,
'user_id' => $user_id,
'type' => 'sensei_user_answer',
'field' => 'comment_ID',
)
);
$activity_logged = update_comment_meta( $user_lesson_id, 'answer_note', $notes );
} else {
$activity_logged = true;
}
}
return $activity_logged;
}
/**
* array_sort_reorder handle sorting of table data
*
* @since 1.3.0
* @param array $return_array data to be ordered
* @return array $return_array ordered data
*/
public static function array_sort_reorder( $return_array ) {
if ( isset( $_GET['orderby'] ) && '' != esc_html( $_GET['orderby'] ) ) {
$sort_key = '';
if ( '' != $sort_key ) {
self::sort_array_by_key( $return_array, $sort_key );
if ( isset( $_GET['order'] ) && 'desc' == esc_html( $_GET['order'] ) ) {
$return_array = array_reverse( $return_array, true );
}
}
return $return_array;
} else {
return $return_array;
}
}
/**
* sort_array_by_key sorts array by key
*
* @since 1.3.0
* @param array $array by ref
* @param $key string column name in array
* @return void
*/
public static function sort_array_by_key( $array, $key ) {
$sorter = array();
$ret = array();
reset( $array );
foreach ( $array as $ii => $va ) {
$sorter[ $ii ] = $va[ $key ];
}
asort( $sorter );
foreach ( $sorter as $ii => $va ) {
$ret[ $ii ] = $array[ $ii ];
}
$array = $ret;
}
/**
* This function returns an array of lesson quiz questions
*
* @since 1.3.2
* @since 3.5.0 Added $query_args.
*
* @param integer $quiz_id
* @param array $query_args Additional args for the query.
* @return array of quiz questions
*/
public static function lesson_quiz_questions( $quiz_id = 0, $query_args = [] ) {
$questions_array = array();
if ( 0 < $quiz_id ) {
$defaults = array(
'post_type' => 'question',
'posts_per_page' => -1,
'orderby' => 'ID',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_quiz_id',
'value' => $quiz_id,
),
),
'post_status' => 'any',
'suppress_filters' => 0,
);
$question_args = wp_parse_args( $query_args, $defaults );
$questions_array = get_posts( $question_args );
}
return $questions_array;
}
/**
* Complete this course forcefully for this user by passing all the lessons.
*
* @param int $user_id User ID.
* @param int $course_id Course ID
*/
public static function force_complete_user_course( $user_id, $course_id ) {
$user = get_user_by( 'id', $user_id );
if ( false === $user ) {
return;
}
$lesson_ids = Sensei()->course->course_lessons( $course_id, 'any', 'ids' );
foreach ( $lesson_ids as $id ) {
self::sensei_start_lesson( $id, $user_id, true );
}
}
/**