-
Notifications
You must be signed in to change notification settings - Fork 198
/
includes_class-sensei-teacher.php.html
1847 lines (1456 loc) · 59.2 KB
/
includes_class-sensei-teacher.php.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Source: includes/class-sensei-teacher.php - Sensei LMS Hook Docs</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Mono|IBM+Plex+Sans:300,400|Playfair+Display:900&display=swap" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="styles.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: includes/class-sensei-teacher.php</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code><?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Sensei Teacher class
*
* All functionality pertaining to the teacher role.
*
* @package Users
* @author Automattic
* @since 1.0.0
*/
class Sensei_Teacher {
/**
* $teacher_role
*
* Keeps a reference to the teacher role object
*
* @access protected
* @since 1.8.0
*/
protected $teacher_role;
/**
* $token
*
* Keeps a reference to the global sensei token
*
* @access protected
* @since 1.8.0
*/
public $token;
/**
* The nonce name when submitting a new message.
*
* @var string
*/
const NONCE_FIELD_NAME = 'sensei_meta_nonce';
/**
* The nonce action name when submitting a new message.
*/
const NONCE_ACTION_NAME = 'sensei_save_data';
/**
* Sensei_Teacher::__constructor
*
* Constructor Function
*
* @since 1.8.0
* @access public
*/
public function __construct() {
add_action( 'add_meta_boxes', [ $this, 'add_teacher_meta_boxes' ], 10, 2 );
add_action( 'save_post', [ $this, 'save_teacher_meta_box' ] );
add_filter( 'parse_query', array( $this, 'limit_teacher_edit_screen_post_types' ) );
add_filter( 'pre_get_posts', array( $this, 'course_analysis_teacher_access_limit' ) );
add_filter( 'wp_count_posts', array( $this, 'list_table_counts' ), 10, 3 );
add_action( 'pre_get_posts', array( $this, 'filter_queries' ) );
// filter the quiz submissions
add_filter( 'sensei_check_for_activity', array( $this, 'filter_grading_activity_queries' ) );
// grading totals count only those belonging to the teacher
add_filter( 'sensei_count_statuses_args', array( $this, 'limit_grading_totals' ) );
// show the courses owned by a user on his author archive page
add_filter( 'pre_get_posts', array( $this, 'add_courses_to_author_archive' ) );
// notify admin when a teacher creates a course
add_action( 'transition_post_status', array( $this, 'notify_admin_teacher_course_creation' ), 10, 3 );
// limit the analysis view to only the users taking courses belong to this teacher
add_filter( 'sensei_analysis_overview_filter_users', array( $this, 'limit_analysis_learners' ), 5, 1 );
// give teacher access to question post type
add_filter( 'sensei_lesson_quiz_questions', array( $this, 'allow_teacher_access_to_questions' ), 20, 2 );
// Teacher column on the courses list on the admin edit screen
add_filter( 'manage_course_posts_columns', array( $this, 'course_column_heading' ), 10, 1 );
add_filter( 'manage_course_posts_custom_column', array( $this, 'course_column_data' ), 10, 2 );
// admin edit messages query limit teacher
add_filter( 'pre_get_posts', array( $this, 'limit_edit_messages_query' ) );
// add filter by teacher on courses list
add_action( 'restrict_manage_posts', array( $this, 'course_teacher_filter_options' ) );
add_filter( 'request', array( $this, 'teacher_filter_query_modify' ) );
// Handle media library restrictions
add_filter( 'request', array( $this, 'restrict_media_library' ), 10, 1 );
add_filter( 'ajax_query_attachments_args', array( $this, 'restrict_media_library_modal' ), 10, 1 );
// update lesson owner to course teacher before insert
add_filter( 'wp_insert_post_data', array( $this, 'update_lesson_teacher' ), 99, 2 );
// If a Teacher logs in, redirect to /wp-admin/
add_filter( 'wp_login', array( $this, 'teacher_login_redirect' ), 10, 2 );
add_action( 'admin_menu', array( $this, 'restrict_posts_menu_page' ), 10 );
add_filter( 'pre_get_comments', array( $this, 'restrict_comment_moderation' ), 10, 1 );
// If slug changed to custom, try to extract and save teacher id.
add_action( 'edit_module', [ $this, 'extract_and_save_teacher_to_meta_from_slug' ] );
add_action( 'sensei_course_new_teacher_assigned', [ $this, 'teacher_course_assigned_notification' ], 10, 2 );
}
/**
* Sensei_Teacher::create_teacher_role
*
* This function checks if the role exist, if not it creates it.
* for the teacher role
*
* @since 1.8.0
* @access public
* @return void
*/
public function create_role() {
// check if the role exists
$this->teacher_role = get_role( 'teacher' );
// if the the teacher is not a valid WordPress role create it
if ( ! is_a( $this->teacher_role, 'WP_Role' ) ) {
// create the role
$this->teacher_role = add_role( 'teacher', __( 'Teacher', 'sensei-lms' ) );
}
// add the capabilities before returning
$this->add_capabilities();
}
/**
* Sensei_Teacher::add_capabilities
*
* @since 1.8.0
* @access protected
*/
protected function add_capabilities() {
// if this is not a valid WP_Role object exit without adding anything
if ( ! is_a( $this->teacher_role, 'WP_Role' ) || empty( $this->teacher_role ) ) {
return;
}
/**
* Sensei teachers capabilities array filter
*
* These capabilities will be applied to the teacher role
*
* @param array $capabilities
* keys: (string) $cap_name => (bool) $grant
*/
$caps = apply_filters(
'sensei_teacher_role_capabilities',
array(
// General access rules
'read' => true,
'manage_sensei_grades' => true,
'moderate_comments' => true,
'upload_files' => true,
'edit_files' => true,
// Lessons
'publish_lessons' => true,
'manage_lesson_categories' => true,
'edit_lessons' => true,
'edit_published_lessons' => true,
'edit_private_lessons' => true,
'read_private_lessons' => true,
'delete_published_lessons' => true,
// Courses
'create_courses' => true,
'publish_courses' => false,
'manage_course_categories' => true,
'edit_courses' => true,
'edit_published_courses' => true,
'edit_private_courses' => true,
'read_private_courses' => true,
'delete_published_courses' => true,
// Quiz
'publish_quizzes' => true,
'edit_quizzes' => true,
'edit_published_quizzes' => true,
'edit_private_quizzes' => true,
'read_private_quizzes' => true,
// Questions
'publish_questions' => true,
'edit_questions' => true,
'edit_published_questions' => true,
'edit_private_questions' => true,
'read_private_questions' => true,
// messages
'publish_sensei_messages' => true,
'edit_sensei_messages' => true,
'edit_published_sensei_messages' => true,
'edit_private_sensei_messages' => true,
'read_private_sensei_messages' => true,
// Comments -
// Necessary cap so Teachers can moderate comments
// on their own lessons. We restrict access to other
// post types in $this->restrict_posts_menu_page()
'edit_posts' => true,
)
);
foreach ( $caps as $cap => $grant ) {
// load the capability on to the teacher role
$this->teacher_role->add_cap( $cap, $grant );
}
}
/**
* Sensei_Teacher::teacher_meta_box
*
* Add the teacher meta_box to the course post type edit screen
*
* @since 1.8.0
* @access public
* @parameter string $post_type
* @parameter WP_Post $post
* @return void
*/
public function add_teacher_meta_boxes( $post ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
add_meta_box(
'sensei-teacher',
__( 'Teacher', 'sensei-lms' ),
array( $this, 'teacher_meta_box_content' ),
'course',
'side',
'core',
[
'__block_editor_compatible_meta_box' => true,
'__back_compat_meta_box' => true,
]
);
}
/**
* Sensei_Teacher::teacher_meta_box_content
*
* Render the teacher meta box markup
*
* @since 1.8.0
* @access public
* @parameters
*/
public function teacher_meta_box_content( $post ) {
wp_nonce_field( self::NONCE_ACTION_NAME, self::NONCE_FIELD_NAME );
// get the current author
$current_author = $post->post_author;
// get the users authorised to author courses
$users = $this->get_teachers_and_authors_with_fields( [ 'ID', 'display_name' ] );
?>
<input type="hidden" name="post_author_override" value="<?php echo intval( $current_author ); ?>" />
<input type="hidden" name="course_module_custom_slugs" />
<select name="sensei-course-teacher-author" class="sensei course teacher">
<?php foreach ( $users as $user_data ) { ?>
<?php
$user_id = $user_data->ID;
$user_display_name = $user_data->display_name;
?>
<option <?php selected( $current_author, $user_id, true ); ?> value="<?php echo esc_attr( $user_id ); ?>" >
<?php echo esc_html( $user_display_name ); ?>
</option>
<?php
}
?>
</select>
<?php
}
/**
* Sensei_Teacher::get_teachers_and_authors
*
* Get a list of users who can author courses, lessons and quizes.
*
* @since 1.8.0
* @access private
* @parameters
* @return array $users user id array
*/
public function get_teachers_and_authors() {
$edit_course_roles = [];
foreach ( wp_roles()->roles as $role_slug => $role ) {
if ( ! empty( $role['capabilities']['edit_courses'] ) ) {
$edit_course_roles[] = $role_slug;
}
}
return get_users(
[
'blog_id' => $GLOBALS['blog_id'],
'fields' => 'any',
'role__in' => $edit_course_roles,
]
);
}
/**
* Get list of users who can author courses, lessons, and quizzes.
* Optionally specify the fields to return from the DB.
*
* @since 3.13.4
* @access private
*
* @param string|array $fields Fields to return from DB. Defaults to 'ID'.
* @return array
*/
public function get_teachers_and_authors_with_fields( $fields = 'ID' ) {
$ids = $this->get_teachers_and_authors();
return get_users(
[
'blog_id' => $GLOBALS['blog_id'],
'include' => $ids,
'fields' => $fields,
]
);
}
/**
* Sensei_Teacher::save_teacher_meta_box
*
* Save the new teacher / author from the meta box form.
*
* Hooked into admin_init
*
* @since 1.8.0
* @access public
* @param int $course_id Course ID.
* @return void
*/
public function save_teacher_meta_box( $course_id ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Do not change the nonce.
if ( empty( $_POST[ self::NONCE_FIELD_NAME ] ) || ! wp_verify_nonce( wp_unslash( $_POST[ self::NONCE_FIELD_NAME ] ), self::NONCE_ACTION_NAME ) ) {
return;
}
// check if this is a post from saving the teacher, if not exit early
if ( ! isset( $_POST['sensei-course-teacher-author'] ) || ! isset( $_POST['post_ID'] ) ) {
return;
}
// If a custom slug is of a module that belongs to another teacher from another course, don't process farther.
if ( isset( $_POST['course_module_custom_slugs'] ) ) {
$module_custom_slugs = json_decode( sanitize_text_field( wp_unslash( $_POST['course_module_custom_slugs'] ) ) );
foreach ( $module_custom_slugs as $module_custom_slug ) {
$course_name = self::is_module_in_use_by_different_course_and_teacher( $module_custom_slug, $course_id, absint( $_POST['sensei-course-teacher-author'] ) );
if ( $course_name ) {
return;
}
}
}
// don't fire this hook again
remove_action( 'save_post', array( $this, 'save_teacher_meta_box' ) );
$new_teacher = absint( $_POST['sensei-course-teacher-author'] );
$this->save_teacher( $course_id, $new_teacher );
}
/**
* Sensei_Teacher::save_teacher
*
* Save the new teacher / author to course and all lessons
*
* @access public
* @param int $course_id Course ID.
* @param int $new_teacher Course ID.
* @return void
*/
public function save_teacher( $course_id, $new_teacher ) {
$post = get_post( $course_id );
// get the current teacher/author
$current_teacher = absint( $post->post_author );
// loop through all post lessons to update their authors as well
$this->update_course_lessons_author( $course_id, $new_teacher );
// do not do any processing if the selected author is the same as the current author
if ( $current_teacher == $new_teacher ) {
return;
}
// save the course author
$post_updates = array(
'ID' => $post->ID,
'post_author' => $new_teacher,
);
wp_update_post( $post_updates );
// ensure the modules are update so that then new teacher has access to them.
self::update_course_modules_author( $course_id, $new_teacher );
/**
* Fires when a new teacher is assigned to a course.
*
* @since $$next-version$$
*
* @hook sensei_course_new_teacher_assigned
*
* @param {int} $new_teacher The ID of the new teacher.
* @param {int} $course_id The ID of the course.
*/
do_action( 'sensei_course_new_teacher_assigned', $new_teacher, $course_id );
}
/**
* Check if the module with the slug provided is
* a part of any other course taught by a different teacher.
*
* @since 4.6.0
* @access private
*
* @param string $module_slug Slug of the module to check for.
* @param int $course_id Slugs of the modules to check for.
* @param int $teacher_id Slugs of the modules to check for.
*
* @return string|boolean Returns the name of the first course it finds a match for, false otherwise.
*/
public static function is_module_in_use_by_different_course_and_teacher( $module_slug, $course_id, $teacher_id ) {
$existing_module_by_slug = get_term_by( 'slug', $module_slug, 'module' );
if ( $existing_module_by_slug ) {
$args = array(
'post_type' => 'course',
'post_status' => [ 'publish', 'draft', 'private' ],
'posts_per_page' => -1,
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery
array(
'taxonomy' => 'module',
'field' => 'id',
'terms' => $existing_module_by_slug->term_id,
),
),
);
$module_courses = get_posts( $args );
foreach ( $module_courses as $module_course ) {
if ( intval( $module_course->post_author ) !== intval( $teacher_id ) &&
intval( $course_id ) !== $module_course->ID ) {
if ( user_can( $module_course->post_author, 'manage_options' ) && user_can( $teacher_id, 'manage_options' ) ) {
continue;
}
return $module_course->post_title;
}
}
}
return false;
}
/**
* Update all the course terms set(selected) on the given course. Moving course term ownership to
* the new author. Making sure the course terms are maintained.
*
* This function also checks if terms are shared, with other courses
*
* @param $course_id
* @param $new_teacher_id
* @return void
*/
public static function update_course_modules_author( $course_id, $new_teacher_id ) {
if ( empty( $course_id ) || empty( $new_teacher_id ) ) {
return;
}
if ( ! taxonomy_exists( 'module' ) ) {
// If for some reason the `module` taxonomy is not present (normally it is), register it now.
// This mitigates a Jetpack-introduced bug.
Sensei()->modules->setup_modules_taxonomy();
}
remove_filter( 'get_terms', array( Sensei()->modules, 'append_teacher_name_to_module' ), 70 );
$modules = Sensei()->modules->get_course_modules( $course_id );
add_filter( 'get_terms', array( Sensei()->modules, 'append_teacher_name_to_module' ), 70, 3 );
if ( empty( $modules ) ) {
return;
}
$lessons = Sensei()->course->course_lessons( $course_id, null );
$module_order = [];
foreach ( $modules as $term ) {
$term_author = Sensei_Core_Modules::get_term_author( $term->slug );
if ( ! $term_author || intval( $new_teacher_id ) !== intval( $term_author->ID ) ) {
$new_slug = $new_teacher_id . '-' . sanitize_title( trim( $term->name ) );
$search_slugs = array();
$search_slugs[] = $new_slug;
// First, try to recycle an existing module.
if ( user_can( $new_teacher_id, 'manage_options' ) ) {
$admin_slug = sanitize_title( trim( $term->name ) );
array_unshift( $search_slugs, $admin_slug );
$new_slug = $admin_slug;
}
// Search for term to recycle.
$new_term = false;
foreach ( $search_slugs as $search_slug ) {
$search_term = get_term_by( 'slug', $search_slug, 'module', ARRAY_A );
if ( $search_term && ! is_wp_error( $search_term ) ) {
$new_term = $search_term;
break;
}
}
if ( empty( $new_term ) ) {
// Create new term and set it.
$new_term = wp_insert_term(
$term->name,
'module',
array(
'slug' => $new_slug,
)
);
}
if ( is_wp_error( $new_term ) ) {
// Something happened. Let's leave the module alone.
continue;
}
$term_id = $new_term['term_id'];
$module_order[] = $term_id;
Sensei_Core_Modules::update_module_teacher_meta( $term_id, $new_teacher_id );
// Set the terms selected on the course.
wp_set_object_terms( $course_id, $term_id, 'module', true );
// Remove old term.
if ( intval( $term_id ) !== intval( $term->term_id ) ) {
wp_remove_object_terms( $course_id, $term->term_id, 'module' );
}
foreach ( $lessons as $lesson ) {
if ( has_term( $term->term_id, 'module', $lesson ) ) {
// Add the new term, the false at the end says to replace all terms on this module
// with the new term.
wp_set_object_terms( $lesson->ID, $term_id, 'module', false );
$order_module = 0;
$old_order_module = get_post_meta( $lesson->ID, '_order_module_' . intval( $term->term_id ), true );
if ( $old_order_module ) {
$order_module = $old_order_module;
if ( intval( $term->term_id ) !== intval( $term_id ) ) {
delete_post_meta( $lesson->ID, '_order_module_' . intval( $term->term_id ) );
}
}
update_post_meta( $lesson->ID, '_order_module_' . intval( $term_id ), $order_module );
}
}
// Clean up module if no longer used.
Sensei()->modules->remove_if_unused( $term->term_id );
}
}
Sensei_Course_Structure::instance( $course_id )->save_module_order( $module_order );
}
/**
* Update all course lessons and their quiz with a new author.
*
* @since 1.8.0
* @access public
*
* @param int $course_id The course id.
* @param int $new_author The new authors id.
*
* @return bool True when the author is updated.
*/
public function update_course_lessons_author( $course_id, $new_author ) {
if ( empty( $course_id ) || empty( $new_author ) ) {
return false;
}
// Get a list of course lessons.
$lessons = Sensei()->course->course_lessons( $course_id, 'any' );
if ( empty( $lessons ) || ! is_array( $lessons ) ) {
return false;
}
// Temporarily remove the author match filter.
remove_filter( 'wp_insert_post_data', array( $this, 'update_lesson_teacher' ), 99 );
// Update each lesson and quiz author.
foreach ( $lessons as $lesson ) {
// Don't update if the author is tha same as the new author.
if ( $new_author == $lesson->post_author ) {
continue;
}
// Update lesson author.
wp_update_post(
array(
'ID' => $lesson->ID,
'post_author' => $new_author,
)
);
// Update quiz author.
$lesson_quiz_id = Sensei()->lesson->lesson_quizzes( $lesson->ID );
if ( ! empty( $lesson_quiz_id ) ) {
Sensei()->quiz->update_quiz_author( (int) $lesson_quiz_id, (int) $new_author );
}
}
// Reinstate the author match filter.
add_filter( 'wp_insert_post_data', array( $this, 'update_lesson_teacher' ), 99, 2 );
return true;
}
/**
* Sensei_Teacher::course_analysis_teacher_access_limit
*
* Alter the query so that users can only see their courses on the analysis page
*
* @since 1.8.0
* @access public
* @parameters $query
* @return array $users user id array
*/
public function course_analysis_teacher_access_limit( $query ) {
if ( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return $query;
}
if ( ! function_exists( 'get_current_screen' ) ) {
return $query;
}
$screen = get_current_screen();
$sensei_post_types = array( 'course', 'lesson', 'question' );
// exit early for the following conditions.
$limit_screen_ids = array( 'sensei-lms_page_' . Sensei_Analysis::PAGE_SLUG, 'sensei-lms_page_module-order' );
if ( ! $this->is_admin_teacher() || empty( $screen ) || ! in_array( $screen->id, $limit_screen_ids )
|| ! in_array( $query->query['post_type'], $sensei_post_types ) ) {
return $query;
}
global $current_user;
// set the query author to the current user to only show those those posts
$query->set( 'author', $current_user->ID );
return $query;
}
/**
* Sensei_Teacher::limit_teacher_edit_screen_post_types
*
* Determine if we're in admin and the current logged in use is a teacher
*
* @since 1.8.0
* @access public
* @parameters array $wp_query
* @return bool $is_admin_teacher
*/
public function is_admin_teacher() {
if ( ! function_exists( 'is_user_logged_in' ) ) {
return false;
}
if ( ! is_user_logged_in() ) {
return false;
}
$is_admin_teacher = false;
if ( is_admin() && self::is_a_teacher( get_current_user_id() ) ) {
$is_admin_teacher = true;
}
return $is_admin_teacher;
}
/**
* Show correct post counts on list table for Sensei post types
*
* @since 1.8.0
*
* @param object $counts Default status counts
* @param string $type Current post type
* @param string $perm User permission level
* @return object Modified status counts
*/
public function list_table_counts( $counts, $type, $perm ) {
global $current_user;
if ( ! in_array( $type, array( 'course', 'lesson', 'question' ) ) ) {
return $counts;
}
if ( ! $this->is_admin_teacher() ) {
return $counts;
}
$args = array(
'post_type' => $type,
'author' => $current_user->ID,
'posts_per_page' => -1,
);
// Get all available statuses
$stati = get_post_stati();
// Update count object
foreach ( $stati as $status ) {
$args['post_status'] = $status;
$posts = get_posts( $args );
$counts->$status = count( $posts );
}
return $counts;
}
/**
* Filter the post queries to show
* only lesson /course and users that belong
* to the current logged teacher.
*
* @since 1.8.0
*/
public function filter_queries( $query ) {
global $current_user;
if ( ! $this->is_admin_teacher() ) {
return;
}
if ( ! function_exists( 'get_current_screen' ) ) {
return;
}
$screen = get_current_screen();
if ( empty( $screen ) ) {
return $query;
}
switch ( $screen->id ) {
case 'sensei-lms_page_sensei_grading':
case 'sensei-lms_page_' . Sensei_Analysis::PAGE_SLUG:
case 'sensei-lms_page_sensei_learners':
case 'lesson':
case 'course':
case 'question':
case 'lesson_page_module-order':
/**
* sensei_filter_queries_set_author
* Filter the author Sensei set for queries
*
* @since 1.8.0
*
* @param int $user_id
* @param string $screen_id
*/
$query->set( 'author', apply_filters( 'sensei_filter_queries_set_author', $current_user->ID, $screen->id ) );
break;
}
}
/**
* Limit grading quizzes to only those within courses belonging to the current teacher
* . This excludes the admin user.
*
* @since 1.8.0
* @hooked into the_comments
* @param array $comments
*
* @return array $comments
*/
public function filter_grading_activity_queries( $comments ) {
if ( ! is_admin() || ! $this->is_admin_teacher() || is_numeric( $comments ) || ! is_array( $comments ) ) {
return $comments;
}
// check if we're on the grading screen
$screen = get_current_screen();
if ( empty( $screen ) || 'sensei-lms_page_sensei_grading' != $screen->id ) {
return $comments;
}
// get the course and determine if the current teacher is the owner
// if not remove it from the list of comments to be returned
foreach ( $comments as $key => $comment ) {
$lesson = get_post( $comment->comment_post_ID );
$course_id = Sensei()->lesson->get_course_id( $lesson->ID );
$course = get_post( $course_id );
/**
* Allows to change the list of teacher IDs with grading access allowed for a given course ID.
*
* @hook sensei_grading_allowed_user_ids
* @since 4.9.0
*
* @param {int[]} $user_ids The list of user IDs with access granted. By default the course author.
* @param {int} $course_id The course ID.
*/
$allowed_user_ids = apply_filters( 'sensei_grading_allowed_user_ids', [ intval( $course->post_author ) ], $course_id );
if ( ! isset( $course->post_author ) || ! in_array( intval( get_current_user_id() ), $allowed_user_ids, true ) ) {
// remove this as the teacher should see this.
unset( $comments[ $key ] );
}
}
return $comments;
}
/**
* Limit the grading screen totals to only show lessons in the course
* belonging to the currently logged in teacher. This only applies to
* the teacher role.
*
* @since 1.8.0
*
* @hooked into sensei_count_statuses_args
* @param array $args
*
* @return array $args
*/
public function limit_grading_totals( $args ) {
if ( ! is_admin() || ! $this->is_admin_teacher() || ! is_array( $args ) ) {
return $args;
}
// get the teachers courses
// the query is already filtered to only the teacher
$courses = Sensei()->course->get_all_courses();
if ( empty( $courses ) || ! is_array( $courses ) ) {
return $args;
}
// setup the lessons quizzes to limit the grading totals to
$quiz_scope = array();
foreach ( $courses as $course ) {
$course_lessons = Sensei()->course->course_lessons( $course->ID );
if ( ! empty( $course_lessons ) && is_array( $course_lessons ) ) {
foreach ( $course_lessons as $lesson ) {
array_push( $quiz_scope, $lesson->ID );
}
}
}
$args['post__in'] = $quiz_scope;
return $args;
}
/**
* It ensures that the author archive shows course by the current user.
*
* This function is hooked into the pre_get_posts filter
*
* @param WP_Query $query
* @return WP_Query $query
*/
public function add_courses_to_author_archive( $query ) {
if ( is_admin() || ! $query->is_author() ) {
return $query;
}
// this should only apply to users with the teacher role
$current_page_user = get_user_by( 'login', $query->get( 'author_name' ) );
if ( ! $current_page_user || ! in_array( 'teacher', $current_page_user->roles ) ) {
return $query;
}
// Change post types depending on what is set already
$current_post_types = $query->get( 'post_type' );
if ( empty( $current_post_types ) ) {
// if empty it means post by default, so add post so that it also includes that for now
$new_post_types = array( 'post', 'course' );
} elseif ( is_array( $current_post_types ) ) {
// merge the post types instead of overwriting it
$new_post_types = array_merge( $current_post_types, array( 'course' ) );
} else {
// in this instance it is probably just one post type in string format
$new_post_types = array( $current_post_types, 'course' );
}
// change the query before returning it
$query->set( 'post_type', $new_post_types );
/**
* Change the query on the teacher author archive template
*
* @since 1.8.4
* @param WP_Query $query
*/
return apply_filters( 'sensei_teacher_archive_query', $query );
}
/**
* Notify teacher when someone assigns a course to their account.
*
* @since 1.8.0
*
* @param $teacher_id
* @param $course_id