Completed
Push — master ( d51ce1...80c0ca )
by Dwain
05:52
created

Sensei_Teacher::create_role()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4286
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4
/**
5
 * Sensei Teacher class
6
 *
7
 * All functionality pertaining to the teacher role.
8
 *
9
 * @package WordPress
10
 * @subpackage Sensei
11
 * @category Core
12
 * @author WooThemes
13
 * @since 1.0.0
14
 */
15
class Sensei_Teacher {
16
17
    /**
18
     * $teacher_role
19
     *
20
     * Keeps a reference to the teacher role object
21
     *
22
     * @access protected
23
     * @since 1.8.0
24
     */
25
    protected $teacher_role;
26
27
    /**
28
     * $token
29
     *
30
     * Keeps a reference to the global sensei token
31
     *
32
     * @access protected
33
     * @since 1.8.0
34
     */
35
    public  $token;
36
37
    /**
38
     * Sensei_Teacher::__constructor
39
     *
40
     * Constructor Function
41
     *
42
     * @since 1.8.0
43
     * @access public
44
     */
45
    public function __construct ( ) {
46
47
        add_action( 'add_meta_boxes', array( $this , 'add_teacher_meta_boxes' ) , 10, 2 );
48
        add_action( 'save_post',  array( $this, 'save_teacher_meta_box' ) );
49
        add_filter( 'parse_query', array( $this, 'limit_teacher_edit_screen_post_types' ));
50
        add_filter( 'pre_get_posts', array( $this, 'course_analysis_teacher_access_limit' ) );
51
        add_filter( 'wp_count_posts', array( $this, 'list_table_counts' ), 10, 3 );
52
53
        add_action( 'pre_get_posts', array( $this, 'filter_queries' ) );
54
55
        //filter the quiz submissions
56
        add_filter( 'sensei_check_for_activity' , array( $this, 'filter_grading_activity_queries') );
57
58
        //grading totals count only those belonging to the teacher
59
        add_filter('sensei_count_statuses_args', array( $this, 'limit_grading_totals' ) );
60
61
        // show the courses owned by a user on his author archive page
62
        add_filter( 'pre_get_posts', array( $this, 'add_courses_to_author_archive' ) );
63
64
        // notify admin when a teacher creates a course
65
        add_action( 'transition_post_status',array( $this, 'notify_admin_teacher_course_creation' ), 10, 3 );
66
67
        // limit the analysis view to only the users taking courses belong to this teacher
68
        add_filter( 'sensei_analysis_overview_filter_users',array( $this, 'limit_analysis_learners' ) , 5, 1 );
69
70
        // give teacher access to question post type
71
        add_filter( 'sensei_lesson_quiz_questions', array( $this, 'allow_teacher_access_to_questions' ), 20, 2 );
72
73
        // Teacher column on the courses list on the admin edit screen
74
        add_filter('manage_edit-course_columns' , array( $this, 'course_column_heading'), 10,1 );
75
        add_filter('manage_course_posts_custom_column' , array( $this, 'course_column_data'), 10,2 );
76
77
        //admin edit messages query limit teacher
78
        add_filter( 'pre_get_posts', array( $this, 'limit_edit_messages_query' ) );
79
80
        //add filter by teacher on courses list
81
        add_action( 'restrict_manage_posts', array( $this, 'course_teacher_filter_options' ) );
82
        add_filter( 'request', array( $this, 'teacher_filter_query_modify' ) );
83
84
        // Handle media library restrictions
85
        add_filter( 'request', array( $this, 'restrict_media_library' ), 10, 1 );
86
        add_filter( 'ajax_query_attachments_args', array( $this, 'restrict_media_library_modal' ), 10, 1 );
87
88
        // update lesson owner to course teacher when saved
89
        add_action( 'save_post',  array( $this, 'update_lesson_teacher' ) );
90
91
        // If a Teacher logs in, redirect to /wp-admin/
92
        add_filter( 'wp_login', array( $this, 'teacher_login_redirect') , 10, 2 );
93
94
95
        add_action( 'admin_menu', array( $this, 'restrict_posts_menu_page'), 10);
96
        add_filter('pre_get_comments',  array ($this, 'restrict_comment_moderation'), 10, 1);
97
98
99
    } // end __constructor()
100
101
    /**
102
     * Sensei_Teacher::create_teacher_role
103
     *
104
     * This function checks if the role exist, if not it creates it.
105
     * for the teacher role
106
     *
107
     * @since 1.8.0
108
     * @access public
109
     * @return void
110
     */
111
    public function create_role ( ) {
112
113
        // check if the role exists
114
        $this->teacher_role = get_role( 'teacher' );
115
116
        // if the the teacher is not a valid WordPress role create it
117
       if ( ! is_a( $this->teacher_role, 'WP_Role' ) ) {
118
           // create the role
119
           $this->teacher_role = add_role( 'teacher', __( 'Teacher', 'woothemes-sensei' ) );
120
       }
121
122
       // add the capabilities before returning
123
        $this->add_capabilities();
124
125
    }// end create_teacher_role
126
127
    /**
128
     * Sensei_Teacher::add_capabilities
129
     *
130
     * @since 1.8.0
131
     * @access protected
132
     */
133
    protected function add_capabilities ( ) {
134
135
        // if this is not a valid WP_Role object exit without adding anything
136
        if(  ! is_a( $this->teacher_role, 'WP_Role' ) || empty( $this->teacher_role ) ) {
137
            return;
138
        }
139
140
        /**
141
         * Sensei teachers capabilities array filter
142
         *
143
         * These capabilities will be applied to the teacher role
144
         * @param array $capabilities
145
         * keys: (string) $cap_name => (bool) $grant
146
         */
147
        $caps = apply_filters( 'sensei_teacher_role_capabilities', array(
148
            // General access rules
149
            'read' => true,
150
            'manage_sensei_grades' => true,
151
            'moderate_comments'=> true,
152
            'upload_files'	=> true,
153
            'edit_files'	=> true,
154
155
            //Lessons
156
            'publish_lessons'	 => true,
157
            'manage_lesson_categories'	 => true,
158
            'edit_lessons'	 => true,
159
            'edit_published_lessons'  => true,
160
            'edit_private_lessons' => true,
161
            'read_private_lessons' => true,
162
            'delete_published_lessons' => true,
163
164
            // Courses
165
            'create_courses' => true,
166
            'publish_courses'	 => false,
167
            'manage_course_categories'	 => true,
168
            'edit_courses'	 => true,
169
            'edit_published_courses'  => true,
170
            'edit_private_courses' => true,
171
            'read_private_courses' => true,
172
            'delete_published_courses' => true,
173
174
            // Quiz
175
            'publish_quizzes'	 => true,
176
            'edit_quizzes'	 => true,
177
            'edit_published_quizzes'  => true,
178
            'edit_private_quizzes' => true,
179
            'read_private_quizzes' => true,
180
181
            // Questions
182
            'publish_questions'	 => true,
183
            'edit_questions'	 => true,
184
            'edit_published_questions'  => true,
185
            'edit_private_questions' => true,
186
            'read_private_questions' => true,
187
188
            //messages
189
            'publish_sensei_messages'	 => true,
190
            'edit_sensei_messages'	 => true,
191
            'edit_published_sensei_messages'  => true,
192
            'edit_private_sensei_messages' => true,
193
            'read_private_sensei_messages' => true,
194
195
            // Comments -
196
            // Necessary cap so Teachers can moderate comments
197
            // on their own lessons. We restrict access to other
198
            // post types in $this->restrict_posts_menu_page()
199
200
            'edit_posts' => true,
201
202
        ));
203
204
        foreach ( $caps as $cap => $grant ) {
205
206
            // load the capability on to the teacher role
207
            $this->teacher_role->add_cap($cap, $grant);
208
209
        } // end for each
210
211
    }// end add_cap
212
213
    /**
214
     * Sensei_Teacher::teacher_meta_box
215
     *
216
     * Add the teacher metabox to the course post type edit screen
217
     *
218
     * @since 1.8.0
219
     * @access public
220
     * @parameter string $post_type
221
     * @parameter WP_Post $post
222
     * @return void
223
     */
224
    public function add_teacher_meta_boxes ( $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
225
226
        if( !current_user_can('manage_options') ){
227
            return;
228
        }
229
        add_meta_box( 'sensei-teacher',  __( 'Teacher' , 'woothemes-sensei'),  array( $this , 'teacher_meta_box_content' ),
230
            'course',
231
            'side',
232
            'core'
233
        );
234
235
    } // end teacher_meta_box()
236
237
    /**
238
     * Sensei_Teacher::teacher_meta_box_content
239
     *
240
     * Render the teacher meta box markup
241
     *
242
     * @since 1.8.0
243
     * @access public
244
     * @parameters
245
     */
246
    public function teacher_meta_box_content ( $post ) {
247
248
        // get the current author
249
        $current_author = $post->post_author;
250
251
        //get the users authorised to author courses
252
        $users = $this->get_teachers_and_authors();
253
254
    ?>
255
        <select name="sensei-course-teacher-author" class="sensei course teacher">
256
257
            <?php foreach ( $users as $user_id ) { ?>
258
259
                    <?php
260
                        $user = get_user_by('id', $user_id);
261
                    ?>
262
                    <option <?php selected(  $current_author , $user_id , true ); ?> value="<?php echo $user_id; ?>" >
263
                        <?php echo  $user->display_name; ?>
264
                    </option>
265
266
            <?php }// end for each ?>
267
268
        </select>
269
270
        <?php
271
272
    } // end render_teacher_meta_box()
273
274
    /**
275
     * Sensei_Teacher::get_teachers_and_authors
276
     *
277
     * Get a list of users who can author courses, lessons and quizes.
278
     *
279
     * @since 1.8.0
280
     * @access public
281
     * @parameters
282
     * @return array $users user id array
283
     */
284
    public function get_teachers_and_authors ( ){
285
286
        $author_query_args = array(
287
            'blog_id'      => $GLOBALS['blog_id'],
288
            'fields'       => 'any',
289
            'who'          => 'authors'
290
        );
291
292
        $authors = get_users( $author_query_args );
293
294
        $teacher_query_args = array(
295
            'blog_id'      => $GLOBALS['blog_id'],
296
            'fields'       => 'any',
297
            'role'         => 'teacher',
298
        );
299
300
        $teachers = get_users( $teacher_query_args );
301
302
        return  array_unique( array_merge( $teachers, $authors ) );
303
304
    }// end get_teachers_and_authors
305
306
    /**
307
     * Sensei_Teacher::save_teacher_meta_box
308
     *
309
     * Save the new teacher / author to course and all lessons
310
     *
311
     * Hooked into admin_init
312
     *
313
     * @since 1.8.0
314
     * @access public
315
     * @parameters
316
     * @return array $users user id array
317
     */
318
    public function save_teacher_meta_box ( $course_id ){
319
320
        // check if this is a post from saving the teacher, if not exit early
321
        if(! isset( $_POST[ 'sensei-course-teacher-author' ] ) || ! isset( $_POST['post_ID'] )  ){
322
            return;
323
        }
324
325
        //don't fire this hook again
326
        remove_action('save_post', array( $this, 'save_teacher_meta_box' ) );
327
328
        // get the current post object
329
        $post = get_post( $course_id );
330
331
        // get the current teacher/author
332
        $current_author = absint( $post->post_author );
333
        $new_author = absint( $_POST[ 'sensei-course-teacher-author' ] );
334
335
        // loop through all post lessons to update their authors as well
336
        $this->update_course_lessons_author( $course_id , $new_author );
337
338
        // do not do any processing if the selected author is the same as the current author
339
        if( $current_author == $new_author ){
340
            return;
341
        }
342
343
        // save the course  author
344
        $post_updates = array(
345
            'ID' => $post->ID ,
346
            'post_author' => $new_author
347
        );
348
        wp_update_post( $post_updates );
349
350
        // ensure the the modules are update so that then new teacher has access to them
351
        Sensei_Teacher::update_course_modules_author( $course_id, $new_author );
352
353
        // notify the new teacher
354
        $this->teacher_course_assigned_notification( $new_author, $course_id );
355
356
    } // end save_teacher_meta_box
357
358
    /**
359
     * Update all the course terms set(selected) on the given course. Moving course term ownership to
360
     * the new author. Making sure the course terms are maintained.
361
     *
362
     * This function also checks if terms are shared, with other courses
363
     *
364
     * @param $course_id
365
     * @param $new_teacher_id
366
     * @return void
367
     */
368
    public static function update_course_modules_author( $course_id ,$new_teacher_id ){
369
370
        if( empty( $course_id ) || empty( $new_teacher_id ) ){
371
            return false;
372
        }
373
374
        $terms_selected_on_course = wp_get_object_terms( $course_id, 'module' );
375
376
        if( empty( $terms_selected_on_course ) ){
377
            return;
378
        }
379
380
        foreach( $terms_selected_on_course as $term ){
381
382
            $term_author = Sensei_Core_Modules::get_term_author( $term->slug );
383
            if( $new_teacher_id != $term_author->ID  ){
384
385
                $new_term = '';
386
387
                //if the new teacher is admin first check to see if the term with this name already exists
388
                if( user_can( $new_teacher_id, 'manage_options' ) ){
389
390
                    $slug_without_teacher_id = str_ireplace(' ', '-', trim( $term->name ) );
391
                    $term_args = array( 'slug'=> $slug_without_teacher_id, 'hide_empty' => false, );
392
                    $existing_admin_terms = get_terms( 'module', $term_args );
393
                    if( !empty( $existing_admin_terms ) ){
394
                        // insert it even if it exists
395
                        $new_term = get_term( $existing_admin_terms[0]->term_id, 'module', ARRAY_A );
396
                    }
397
                }
398
399
                if( empty ( $new_term ) ){
400
401
                   //setup the new slug
402
                   $new_author_term_slug =  $new_teacher_id . '-' . str_ireplace(' ', '-', trim( $term->name ) );
403
404
                   // create new term and set it
405
                   $new_term = wp_insert_term( $term->name,'module', array('slug'=> $new_author_term_slug )  );
406
407
                }
408
409
410
411
                // if term exists
412
                if( is_wp_error( $new_term ) && isset( $new_term->errors['term_exists'] ) ){
413
414
                    $existing_term = get_term_by( 'slug', $new_author_term_slug, 'module');
0 ignored issues
show
Bug introduced by
The variable $new_author_term_slug does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
415
                    $term_id = $existing_term->term_id;
416
417
                }else{
418
419
                    // for a new term simply get the term from the returned value
420
                    $term_id = $new_term['term_id'];
421
422
                } // end if term exist
423
424
                // set the terms selected on the course
425
                wp_set_object_terms( $course_id, $term_id , 'module', true );
426
427
                // remove old term
428
                wp_remove_object_terms( $course_id, $term->term_id, 'module' );
429
430
                // update the lessons within the current module term
431
                $lessons = Sensei()->course->course_lessons( $course_id );
432
                foreach( $lessons as $lesson  ){
433
434
                    if( has_term( $term->slug, 'module', $lesson ) ){
435
436
                        // add the new term, the false at the end says to replace all terms on this module
437
                        // with the new term.
438
                        wp_set_object_terms( $lesson->ID, $term_id , 'module', false );
439
                        update_post_meta( $lesson->ID, '_order_module_' . intval( $term_id ), 0 );
440
                    }
441
442
                }// end for each
443
444
            }
445
        }
446
447
    }// end update_course_module_terms_author
448
449
    /**
450
     * Sensei_Teacher::update_course_lessons_author
451
     *
452
     * Update all course lessons and their quiz with a new author
453
     *
454
     * @since 1.8.0
455
     * @access public
456
     * @parameters
457
     * @return array $users user id array
458
     */
459
    public function update_course_lessons_author ( $course_id, $new_author  ){
460
461
462
        if( empty( $course_id ) || empty( $new_author ) ){
463
            return false;
464
        }
465
466
        //get a list of course lessons
467
        $lessons = Sensei()->course->course_lessons( $course_id );
468
469
        if( empty( $lessons )  ||  ! is_array( $lessons )  ){
470
            return false;
471
        }
472
473
        // update each lesson and quiz author
474
        foreach( $lessons as $lesson ){
475
476
            // don't update if the author is tha same as the new author
477
            if( $new_author == $lesson->post_author ){
478
                continue;
479
            }
480
481
            // update lesson author
482
            wp_update_post( array(
483
                'ID'=> $lesson->ID,
484
                'post_author' => $new_author
485
                ) );
486
487
            // update quiz author
488
            //get the lessons quiz
489
            $lesson_quizzes = Sensei()->lesson->lesson_quizzes( $lesson->ID );
490
            if( is_array( $lesson_quizzes ) ){
491
                foreach ( $lesson_quizzes as $quiz_id ) {
492
                    // update quiz with new author
493
                    wp_update_post( array(
494
                        'ID'           => $quiz_id,
495
                        'post_author' =>  $new_author
496
                    ) );
497
                }
498
            }else{
499
                wp_update_post( array(
500
                    'ID'           => $lesson_quizzes,
501
                    'post_author' =>  $new_author
502
                ) );
503
            }
504
505
        } // end for each lessons
506
507
        return true;
508
509
    }// end update_course_lessons_author
510
511
512
513
    /**
514
     * Sensei_Teacher::course_analysis_teacher_access_limit
515
     *
516
     * Alter the query so that users can only see their courses on the analysis page
517
     *
518
     * @since 1.8.0
519
     * @access public
520
     * @parameters $query
521
     * @return array $users user id array
522
     */
523
    public function course_analysis_teacher_access_limit ( $query ) {
524
525
        if( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
526
            return $query;
527
        }
528
529
        if ( ! function_exists( 'get_current_screen' ) ) {
530
            return $query;
531
        }
532
533
        $screen = get_current_screen();
534
        $sensei_post_types = array('course', 'lesson', 'question' );
535
536
        // exit early for the following conditions
537
        $limit_screen_ids = array( 'sensei_page_sensei_analysis', 'course_page_module-order' );
538
539
        if( ! $this->is_admin_teacher() || empty( $screen ) || ! in_array( $screen->id ,$limit_screen_ids )
540
            || ! in_array( $query->query['post_type'], $sensei_post_types ) ){
541
            return $query;
542
        }
543
544
        global $current_user;
545
        // set the query author to the current user to only show those those posts
546
        $query->set( 'author', $current_user->ID );
547
        return $query;
548
549
    }// end course_analysis_teacher_access_limit
550
551
552
    /**
553
     * Sensei_Teacher::limit_teacher_edit_screen_post_types
554
     *
555
     * Determine if we're in admin and the current logged in use is a teacher
556
     *
557
     * @since 1.8.0
558
     * @access public
559
     * @parameters array $wp_query
560
     * @return bool $is_admin_teacher
561
     */
562
    public function is_admin_teacher ( ){
563
564
        if( ! is_user_logged_in()){
565
            return false;
566
        }
567
        $is_admin_teacher = false;
568
569
        if( is_admin() && Sensei_Teacher::is_a_teacher( get_current_user_id() )  ){
570
571
            $is_admin_teacher = true;
572
573
        }
574
575
        return $is_admin_teacher;
576
577
    } // end is_admin_teacher
578
579
    /**
580
     * Show correct post counts on list table for Sensei post types
581
     *
582
     * @since 1.8.0
583
     *
584
     * @param  object $counts Default status counts
585
     * @param  string $type   Current post type
586
     * @param  string $perm   User permission level
587
     * @return object         Modified status counts
588
     */
589
    public function list_table_counts( $counts, $type, $perm ) {
0 ignored issues
show
Unused Code introduced by
The parameter $perm is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
590
        global $current_user;
591
592
        if( ! in_array( $type, array( 'course', 'lesson', 'question' ) ) ) {
593
            return $counts;
594
        }
595
596
        if( ! $this->is_admin_teacher() ) {
597
            return $counts;
598
        }
599
600
        $args = array(
601
            'post_type' => $type,
602
            'author' => $current_user->ID,
603
            'posts_per_page' => -1
604
        );
605
606
         // Get all available statuses
607
        $stati = get_post_stati();
608
609
        // Update count object
610
        foreach( $stati as $status ) {
611
            $args['post_status'] = $status;
612
            $posts = get_posts( $args );
613
            $counts->$status = count( $posts );
614
        }
615
616
        return $counts;
617
    }
618
619
    /**
620
     * Filter the post queries to show
621
     * only lesson /course and users that belong
622
     * to the current logged teacher.
623
     *
624
     * @since 1.8.0
625
     *
626
     */
627
    public function filter_queries ( $query ) {
628
        global $current_user;
629
630
        if( ! $this->is_admin_teacher() ) {
631
            return;
632
        }
633
634
        if ( ! function_exists( 'get_current_screen' ) ) {
635
            return;
636
        }
637
638
        $screen = get_current_screen();
639
        if( empty( $screen ) ) {
640
            return $query;
641
        }
642
        switch( $screen->id ) {
643
            case 'sensei_page_sensei_grading':
644
            case 'sensei_page_sensei_analysis':
645
            case 'sensei_page_sensei_learners':
646
            case 'lesson':
647
            case 'course':
648
            case 'question':
649
            case 'lesson_page_module-order':
650
651
            /**
652
             * sensei_filter_queries_set_author
653
             * Filter the author Sensei set for queries
654
             *
655
             * @since 1.8.0
656
             *
657
             * @param int $user_id
658
             * @param string $screen_id
659
             *
660
             */
661
            $query->set( 'author', apply_filters( 'sensei_filter_queries_set_author', $current_user->ID, $screen->id ) );
662
            break;
663
        }
664
    }
665
666
    /**
667
     * Limit grading quizzes to only those within courses belonging to the current teacher
668
     * . This excludes the admin user.
669
     *
670
     * @since 1.8.0
671
     * @hooked into the_comments
672
     * @param array  $comments
673
     *
674
     * @return array $comments
675
     */
676
    public function filter_grading_activity_queries( $comments ){
677
678
        if( !is_admin() || ! $this->is_admin_teacher() || is_numeric( $comments ) || ! is_array( $comments ) ){
679
            return $comments ;
680
        }
681
682
        //check if we're on the grading screen
683
        $screen = get_current_screen();
684
685
        if( empty( $screen ) || 'sensei_page_sensei_grading' != $screen->id ){
686
            return $comments;
687
        }
688
689
        // get the course and determine if the current teacher is the owner
690
        // if not remove it from the list of comments to be returned
691
        foreach( $comments as $key => $comment){
692
            $lesson = get_post( $comment->comment_post_ID );
693
            $course_id = Sensei()->lesson->get_course_id( $lesson->ID );
694
            $course = get_post( $course_id );
695
            if( ! isset( $course->post_author ) || intval( $course->post_author) != intval( get_current_user_id() ) ){
696
                //remove this as the teacher should see this.
697
                unset( $comments[ $key ] );
698
            }
699
        }
700
        return $comments ;
701
702
    }// end function filter grading
703
704
    /**
705
     * Limit the grading screen totals to only show lessons in the course
706
     * belonging to the currently logged in teacher. This only applies to
707
     * the teacher role.
708
     *
709
     * @since 1.8.0
710
     *
711
     * @hooked into sensei_count_statuses_args
712
     * @param array $args
713
     *
714
     * @return array  $args
715
     */
716
    public function limit_grading_totals( $args ){
717
718
        if( !is_admin() || ! $this->is_admin_teacher() || ! is_array( $args ) ){
719
            return $args ;
720
        }
721
722
        //get the teachers courses
723
        // the query is already filtered to only the teacher
724
        $courses =  Sensei()->course->get_all_courses();
725
726
        if( empty(  $courses ) || ! is_array( $courses ) ){
727
            return $args;
728
        }
729
730
        //setup the lessons quizzes  to limit the grading totals to
731
        $quiz_scope = array();
732
        foreach( $courses as $course ){
733
734
            $course_lessons = Sensei()->course->course_lessons( $course->ID );
735
736
            if( ! empty( $course_lessons ) && is_array( $course_lessons  ) ){
737
738
                foreach(  $course_lessons as $lesson ){
739
740
                    $quiz_id = Sensei()->lesson->lesson_quizzes( $lesson->ID );
741
                    if( !empty( $quiz_id ) ) {
742
743
                        array_push( $quiz_scope, $quiz_id );
744
745
                    }
746
747
                }
748
749
            }
750
751
        }
752
753
        $args['post__in'] = $quiz_scope;
754
755
        return $args;
756
    }
757
758
    /**
759
     * It ensures that the author archive shows course by the current user.
760
     *
761
     * This function is hooked into the pre_get_posts filter
762
     *
763
     * @param WP_Query $query
764
     * @return WP_Query $query
765
     */
766
    public function add_courses_to_author_archive( $query ) {
767
768
        if ( is_admin() || ! $query->is_author() ){
769
            return $query;
770
        }
771
772
        // this should only apply to users with the teacher role
773
        $current_page_user = get_user_by('login', $query->get('author_name') );
774
        if( ! $current_page_user || ! in_array('teacher', $current_page_user->roles ) )     {
775
776
            return $query;
777
778
        }
779
780
        // Change post types depending on what is set already
781
        $current_post_types = $query->get( 'post_type' );
782
        if( empty( $current_post_types  ) ){
783
784
            // if empty it means post by default, so add post so that it also includes that for now
785
            $new_post_types = array( 'post', 'course' );
786
787
        } elseif( is_array( $current_post_types  ) ) {
788
789
            // merge the post types instead of overwriting it
790
            $new_post_types = array_merge( $current_post_types, array( 'course' ) );
791
792
        }else{
793
794
            // in this instance it is probably just one post type in string format
795
            $new_post_types =  array( $current_post_types , 'course');
796
797
        }
798
799
        // change the query before returning it
800
        $query->set('post_type', $new_post_types );
801
802
        /**
803
         * Change the query on the teacher author archive template
804
         *
805
         * @since 1.8.4
806
         * @param WP_Query $query
807
         */
808
        return apply_filters( 'sensei_teacher_archive_query', $query );
809
810
    }
811
812
    /**
813
     * Notify teacher when someone assigns a course to their account.
814
     *
815
     * @since 1.8.0
816
     *
817
     * @param $teacher_id
818
     * @param $course_id
819
     * @return bool
820
     */
821
    public function teacher_course_assigned_notification( $teacher_id, $course_id ){
822
823
        if( 'course' != get_post_type( $course_id ) || ! get_userdata( $teacher_id ) ){
824
            return false;
825
        }
826
827
        // if new user is the same as the current logged user, they don't need an email
828
        if( $teacher_id == get_current_user_id() ){
829
            return true;
830
        }
831
832
        // load the email class
833
        include('emails/class-woothemes-sensei-teacher-new-course-assignment.php');
834
        $email = new Teacher_New_Course_Assignment();
835
        $email->trigger( $teacher_id, $course_id );
836
837
        return true;
838
    } // end  teacher_course_assigned_notification
839
840
    /**
841
     * Email the admin when a teacher creates a new course
842
     *
843
     * This function hooks into wp_insert_post
844
     *
845
     * @since 1.8.0
846
     * @param int $course_id
0 ignored issues
show
Bug introduced by
There is no parameter named $course_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
847
     * @return bool
848
     */
849
    public function notify_admin_teacher_course_creation( $new_status, $old_status, $post ){
850
851
        $course_id = $post->ID;
852
853
        if( 'course' != get_post_type( $course_id ) || 'auto-draft' == get_post_status( $course_id )
854
            || 'trash' == get_post_status( $course_id ) || 'draft' == get_post_status( $course_id ) ) {
855
856
            return false;
857
858
        }
859
860
        /**
861
         * Filter the option to send admin notification emails when teachers creation
862
         * course.
863
         *
864
         * @since 1.8.0
865
         *
866
         * @param bool $on default true
867
         */
868
        if( ! apply_filters('sensei_notify_admin_new_course_creation', true ) ){
869
            return false;
870
        }
871
872
        // setting up the data needed by the email template
873
        global $sensei_email_data;
874
        $template = 'admin-teacher-new-course-created';
875
        $course = get_post( $course_id );
876
        $teacher = new WP_User( $course->post_author );
877
        $recipient = get_option('admin_email', true);
878
879
        // don't send if the course is created by admin
880
        if( $recipient == $teacher->user_email ){
881
            return;
882
        }
883
884
        /**
885
         * Filter the email Header for the admin-teacher-new-course-created template
886
         *
887
         * @since 1.8.0
888
         * @param string $template
889
         */
890
        $heading = apply_filters( 'sensei_email_heading', __( 'New course created.', 'woothemes-sensei' ), $template );
891
892
        /**
893
         * Filter the email subject for the the
894
         * admin-teacher-new-course-created template
895
         *
896
         * @since 1.8.0
897
         * @param string $subject default New course assigned to you
898
         * @param string $template
899
         */
900
        $subject = apply_filters('sensei_email_subject',
901
                                '['. get_bloginfo( 'name', 'display' ) .'] '. __( 'New course created by', 'woothemes-sensei' ) . ' ' . $teacher->display_name ,
902
                                $template );
903
904
        //course edit link
905
        $course_edit_link = admin_url('post.php?post=' . $course_id . '&action=edit' );
906
907
        // Construct data array
908
        $email_data = array(
909
            'template'			=> $template,
910
            'heading' =>  $heading,
911
            'teacher'		=> $teacher,
912
            'course_id'			=> $course_id,
913
            'course_name'			=> $course->post_title,
914
            'course_edit_link' => $course_edit_link,
915
        );
916
917
        /**
918
         * Filter the sensei email data for the admin-teacher-new-course-created template
919
         *
920
         * @since 1.8.0
921
         * @param array $email_data
922
         * @param string $template
923
         */
924
        $sensei_email_data = apply_filters( 'sensei_email_data', $email_data , $template );
925
926
        // Send mail
927
        Sensei()->emails->send( $recipient, $subject , Sensei()->emails->get_content( $template ) );
928
929
    }// end notify admin of course creation
930
931
    /**
932
     * Limit the analysis view to only the users taking courses belong to this teacher
933
     *
934
     * Hooked into sensei_analysis_get_learners
935
     * @param array $args WP_User_Query arguments
936
     * @return array $learners_query_results
937
     */
938
    public function limit_analysis_learners( $args ){
939
940
        // show default for none teachers
941
        if( ! Sensei()->teacher->is_admin_teacher() ) {
942
                return $args;
943
        }
944
945
        // for teachers all courses only return those which belong to the teacher
946
        // as they don't have access to course belonging to other users
947
        $teacher_courses = Sensei()->course->get_all_courses();
948
949
        // if the user has no courses they should see no users
950
        if( empty( $teacher_courses ) ||  ! is_array( $teacher_courses ) ){
951
            // tell the query to return 0 students
952
            $args[ 'include'] = array( 0 );
953
            return $args;
954
955
        }
956
957
        $learner_ids_for_teacher_courses = array();
958
        foreach( $teacher_courses as $course ){
959
960
            $course_learner_ids = array();
961
            $activity_comments =  Sensei_Utils::sensei_check_for_activity( array( 'post_id' => $course->ID, 'type' => 'sensei_course_status', 'field' => 'user_id' ), true );
962
963
            if( empty( $activity_comments ) ||  ( is_array( $activity_comments  ) && ! ( count( $activity_comments ) > 0 ) ) ){
964
                continue; // skip to the next course as there are no users on this course
965
            }
966
967
            // it could be an array of comments or a single comment
968
            if( is_array( $activity_comments ) ){
969
970
                foreach( $activity_comments as $comment ){
971
972
                    $user = get_userdata( $comment->user_id );
973
974
                    if( empty( $user ) ){
975
                        // next comment in this array
976
                        continue;
977
                    }
978
979
                    $course_learner_ids[] = $user->ID;
980
                }
981
982
            }else{
983
984
                $user = get_userdata( $activity_comments->user_id );
985
                $course_learner_ids[] = $user->ID;
986
987
            }
988
989
            // add learners on this course to the all courses learner list
990
            $learner_ids_for_teacher_courses = array_merge( $learner_ids_for_teacher_courses, $course_learner_ids );
991
992
        }
993
994
        // if there are no students taking the courses by this teacher don't show them any of the other users
995
        if( empty( $learner_ids_for_teacher_courses ) ){
996
997
            $args[ 'include'] = array( 0 );
998
999
        }else{
1000
1001
            $args[ 'include'] = $learner_ids_for_teacher_courses;
1002
1003
        }
1004
1005
        // return the WP_Use_Query arguments
1006
        return $args;
1007
1008
    }// end limit_analysis_learners
1009
1010
    /**
1011
     * Give teacher full admin access to the question post type
1012
     * in certain cases.
1013
     *
1014
     * @since 1.8.0
1015
     * @param $questions
1016
     * @return mixed
1017
     */
1018
    public function allow_teacher_access_to_questions( $questions, $quiz_id ){
1019
1020
        if( ! $this->is_admin_teacher() ){
1021
            return $questions;
1022
        }
1023
1024
        $screen = get_current_screen();
1025
1026
        // don't run this filter within this functions call to Sensei()->lesson->lesson_quiz_questions
1027
        remove_filter( 'sensei_lesson_quiz_questions', array( $this, 'allow_teacher_access_to_questions' ), 20 );
1028
1029
        if( ! empty( $screen ) && 'lesson'== $screen->post_type ){
1030
1031
            $admin_user = get_user_by('email', get_bloginfo('admin_email'));
1032
            if( ! empty($admin_user) ){
1033
1034
                $current_teacher_id = get_current_user_id();
1035
1036
                // set current user to admin so teacher can view all questions
1037
                wp_set_current_user( $admin_user->ID  );
1038
                $questions = Sensei()->lesson->lesson_quiz_questions( $quiz_id  );
1039
1040
                // set the teacher as the current use again
1041
                wp_set_current_user( $current_teacher_id );
1042
            }
1043
1044
        }
1045
        // attach the filter again for other funtion calls to Sensei()->lesson->lesson_quiz_questions
1046
        add_filter( 'sensei_lesson_quiz_questions', array( $this, 'allow_teacher_access_to_questions' ), 20,2 );
1047
1048
        return $questions;
1049
    }
1050
1051
    /**
1052
     * Give the teacher role access to questions from the question bank
1053
     *
1054
     * @since 1.8.0
1055
     * @param $wp_query
1056
     * @return mixed
1057
     */
1058
    public function give_access_to_all_questions( $wp_query ){
1059
1060
        if( ! $this->is_admin_teacher() || !function_exists( 'get_current_screen') || 'question' != $wp_query->get('post_type') ){
1061
1062
            return $wp_query;
1063
        }
1064
1065
        $screen = get_current_screen();
1066
        if( ( isset($screen->id) && 'lesson' == $screen->id )
1067
            || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ){
1068
1069
            $admin_user = get_user_by('email', get_bloginfo('admin_email'));
1070
            if( ! empty($admin_user) ){
1071
1072
                $current_teacher_id = get_current_user_id();
1073
1074
                // set current user to admin so teacher can view all questions
1075
                wp_set_current_user( $admin_user->ID  );
1076
1077
                //run new query as admin
1078
                $wp_query = new WP_Query( $wp_query->query );
1079
1080
                //set the teache as current use again
1081
                wp_set_current_user( $current_teacher_id );
1082
1083
            }
1084
        }
1085
1086
        return $wp_query;
1087
    }// end give_access_to_all_questions
1088
1089
    /**
1090
     * Add new column heading to the course admin edit list
1091
     *
1092
     * @since 1.8.0
1093
     * @param $columns
1094
     * @return array
1095
     */
1096
    public function course_column_heading($columns) {
1097
1098
        if( $this->is_admin_teacher() ){
1099
            return $columns;
1100
        }
1101
        $new_columns = array(
1102
            'teacher' => __('Teacher', 'woothemes-sensei'),
1103
        );
1104
        return array_merge($columns, $new_columns);
1105
1106
    }// end teacher column add
1107
1108
    /**
1109
     * Print out  teacher column data
1110
     *
1111
     * @since 1.8.0
1112
     * @param $column
1113
     * @param $course_id
1114
     */
1115
    public function course_column_data( $column, $course_id  ){
1116
1117
        if( $this->is_admin_teacher() || 'teacher' != $column  ){
1118
            return;
1119
        }
1120
1121
        $course = get_post( $course_id );
1122
        $teacher = get_userdata( $course->post_author );
1123
1124
        if( !$teacher ){
1125
            return;
1126
        }
1127
1128
        echo '<a href="'. get_edit_user_link( $teacher->ID ) .'" >'. $teacher->display_name.'</a>';
1129
1130
    }// end course_column_ data
1131
1132
    /**
1133
     * Return only courses belonging to the given teacher.
1134
     *
1135
     *
1136
     * @since 1.8.0
1137
     *
1138
     * @param int $teacher_id
1139
     * @param bool $return_ids_only
1140
     *
1141
     * @return array $teachers_courses
1142
     */
1143
    public function get_teacher_courses( $teacher_id, $return_ids_only= false){
1144
1145
        $teachers_courses = array();
1146
1147
        if( empty( $teacher_id  ) ){
1148
            $teacher_id = get_current_user_id();
1149
        }
1150
1151
        $all_courses = Sensei()->course->get_all_courses();
1152
1153
        if( empty( $all_courses ) ){
1154
            return $all_courses;
1155
        }
1156
1157
        foreach( $all_courses as $course ){
1158
1159
            if( $course->post_author != $teacher_id  ){
1160
                continue;
1161
            }
1162
1163
            if( $return_ids_only ){
1164
1165
                $teachers_courses[] = $course->ID;
1166
1167
            }else{
1168
1169
                $teachers_courses[] = $course;
1170
1171
            }
1172
1173
        }
1174
1175
        return $teachers_courses;
1176
1177
    }
1178
1179
    /**
1180
     * Limit the message display to only those sent to the current teacher
1181
     *
1182
     * @since 1.8.0
1183
     *
1184
     * @param $query
1185
     * @return mixed
1186
     */
1187
    public function limit_edit_messages_query( $query ){
1188
        if( ! $this->is_admin_teacher() || 'sensei_message' != $query->get('post_type') ){
1189
            return $query;
1190
        }
1191
1192
        $teacher = wp_get_current_user();
1193
1194
        $query->set( 'meta_key', '_receiver' );
1195
        $meta_query_args = array(
1196
            'key'     => '_receiver',
1197
            'value'   => $teacher->get('user_login') ,
1198
            'compare' => '='
1199
        );
1200
1201
        $query->set('meta_query', $meta_query_args  );
1202
1203
        return $query;
1204
    }
1205
1206
1207
    /**
1208
     * Add options to filter courses by teacher
1209
     *
1210
     * @since 1.8.0
1211
     *
1212
     * @return void
1213
     */
1214
    public function course_teacher_filter_options() {
1215
        global $typenow;
1216
1217
        if( ! is_admin() || 'course' != $typenow || ! current_user_can('manage_sensei') ) {
1218
            return;
1219
        }
1220
1221
        // get all roles
1222
        $roles = get_editable_roles();
1223
1224
        // get roles with the course edit capability
1225
        // and then get the users with those roles
1226
        $users_who_can_edit_courses = array();
1227
        foreach( $roles as $role_item ){
1228
1229
            $role = get_role( strtolower( $role_item['name'] ) );
1230
1231
            if( is_a( $role, 'WP_Role' ) && $role->has_cap('edit_courses') ){
1232
1233
                $user_query_args = array( 'role' => $role->name, 'fields' => array( 'ID', 'display_name' ) );
1234
                $role_users_who_can_edit_courses = get_users( $user_query_args );
1235
1236
                // add user from the current $user_role to all users
1237
                $users_who_can_edit_courses = array_merge( $users_who_can_edit_courses, $role_users_who_can_edit_courses );
1238
1239
            }
1240
1241
        }
1242
1243
        // Create the select element with the given users who can edit course
1244
        $selected = isset( $_GET['course_teacher'] ) ? $_GET['course_teacher'] : '';
1245
        $course_options = '';
1246
        foreach( $users_who_can_edit_courses as $user ) {
1247
            $course_options .= '<option value="' . esc_attr( $user->ID ) . '" ' . selected( $selected, $user->ID, false ) . '>' .  $user->display_name . '</option>';
1248
        }
1249
1250
        $output = '<select name="course_teacher" id="dropdown_course_teachers">';
1251
        $output .= '<option value="">'.__( 'Show all teachers', 'woothemes-sensei' ).'</option>';
1252
        $output .= $course_options;
1253
        $output .= '</select>';
1254
1255
        echo $output;
1256
    }
1257
1258
    /**
1259
     * Modify the main query on the admin course list screen
1260
     *
1261
     * @since 1.8.0
1262
     *
1263
     * @param $query
1264
     * @return $query
0 ignored issues
show
Documentation introduced by
The doc-type $query could not be parsed: Unknown type name "$query" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
1265
     */
1266
    public function teacher_filter_query_modify( $query ){
1267
        global $typenow;
1268
1269
        if( ! is_admin() && 'course' != $typenow  || ! current_user_can('manage_sensei')  ) {
1270
            return $query;
1271
        }
1272
        $course_teacher = isset( $_GET['course_teacher'] ) ? $_GET['course_teacher'] : '';
1273
1274
        if( empty( $course_teacher ) ) {
1275
            return $query;
1276
        }
1277
1278
        $query['author'] = $course_teacher;
1279
        return $query;
1280
    }
1281
1282
    /**
1283
     * Only show current teacher's media in the media library
1284
     * @param  array $request Default request arguments
1285
     * @return array          Modified request arguments
1286
     */
1287
    public function restrict_media_library( $request = array() ) {
1288
1289
        if( ! is_admin() ) {
1290
            return $request;
1291
        }
1292
1293
        if( ! $this->is_admin_teacher() ) {
1294
            return $request;
1295
        }
1296
1297
        $screen = get_current_screen();
1298
1299
        if( in_array( $screen->id, array( 'upload', 'course', 'lesson', 'question' ) ) ) {
1300
            $teacher = intval( get_current_user_id() );
1301
1302
            if( $teacher ) {
1303
                $request['author__in'] = array( $teacher );
1304
            }
1305
        }
1306
1307
        return $request;
1308
    } // End restrict_media_library()
1309
1310
    /**
1311
     * Only show current teacher's media in the media library modal on the course/lesson/quesion edit screen
1312
     * @param  array $query Default query arguments
1313
     * @return array        Modified query arguments
1314
     */
1315
    public function restrict_media_library_modal( $query = array() ) {
1316
1317
        if( ! is_admin() ) {
1318
            return $query;
1319
        }
1320
1321
        if( ! $this->is_admin_teacher() ) {
1322
            return $query;
1323
        }
1324
1325
        $teacher = intval( get_current_user_id() );
1326
1327
        if( $teacher ) {
1328
            $query['author__in'] = array( $teacher );
1329
        }
1330
1331
        return $query;
1332
    } // End restrict_media_library_modal()
1333
1334
    /**
1335
     * When saving the lesson, update the teacher if the lesson belongs to a course
1336
     *
1337
     * @since 1.8.0
1338
     *
1339
     * @param int $lesson_id
1340
     */
1341
    public function update_lesson_teacher( $lesson_id ){
1342
1343
        if( 'lesson'!= get_post_type() ){
1344
            return;
1345
        }
1346
1347
        // this should only run once per request cycle
1348
        remove_action( 'save_post',  array( $this, 'update_lesson_teacher' ) );
1349
1350
        $course_id = Sensei()->lesson->get_course_id( $lesson_id );
1351
1352
        if(  empty( $course_id ) || ! $course_id ){
1353
            return;
1354
        }
1355
1356
        $course = get_post( $course_id );
1357
1358
        $lesson_update_args= array(
1359
            'ID' => $lesson_id ,
1360
            'post_author' => $course->post_author
1361
        );
1362
        wp_update_post( $lesson_update_args );
1363
1364
    } // end update_lesson_teacher
1365
1366
    /**
1367
     * Sensei_Teacher::limit_teacher_edit_screen_post_types
1368
     *
1369
     * Limit teachers to only see their courses, lessons and questions
1370
     *
1371
     * @since 1.8.0
1372
     * @access public
1373
     * @parameters array $wp_query
1374
     * @return WP_Query $wp_query
1375
     */
1376
    public function limit_teacher_edit_screen_post_types( $wp_query ) {
1377
        global $current_user;
1378
1379
        //exit early
1380
        if( ! $this->is_admin_teacher() ){
1381
            return $wp_query;
1382
        }
1383
1384
        if ( ! function_exists( 'get_current_screen' ) ) {
1385
            return $wp_query;
1386
        }
1387
1388
        $screen = get_current_screen();
1389
1390
        if( empty( $screen ) ){
1391
            return $wp_query;
1392
        }
1393
1394
        // for any of these conditions limit what the teacher will see
1395
        $limit_screens = array(
1396
            'edit-lesson',
1397
            'edit-course',
1398
            'edit-question',
1399
            'course_page_course-order',
1400
            'lesson_page_lesson-order',
1401
        );
1402
1403
        if(  in_array($screen->id  , $limit_screens ) ) {
1404
1405
            // set the query author to the current user to only show those those posts
1406
            $wp_query->set( 'author', $current_user->ID );
1407
        }
1408
1409
        return $wp_query;
1410
1411
    } // end limit_teacher_edit_screen_post_types()
1412
1413
1414
    /**
1415
     * Sensei_Teacher::teacher_login_redirect
1416
     *
1417
     * Redirect teachers to /wp-admin/ after login
1418
     *
1419
     * @since 1.8.7
1420
     * @access public
1421
     * @param string $user_login
1422
     * @param object $user
1423
     * @return void
1424
     */
1425
1426
    public function teacher_login_redirect( $user_login, $user  ) {
1427
1428
        if (user_can($user, 'edit_courses')) {
1429
1430
            if (isset($_POST['redirect_to'])) {
1431
1432
                wp_redirect($_POST['redirect_to'], 303);
1433
1434
                exit;
1435
1436
            } else {
1437
1438
                wp_redirect(admin_url(), 303);
1439
1440
                exit;
1441
1442
            }
1443
        }
1444
1445
    } // end teacher_login_redirect()
1446
1447
1448
1449
    /**
1450
     * Sensei_Teacher::restrict_posts_menu_page()
1451
     *
1452
     * Remove the Posts menu page for teachers and restrict access to it.
1453
     * We have to do this because we give teachers the 'edit_posts' cap
1454
     * so they can 'moderate_comments' as well.
1455
     *
1456
     * @since 1.8.7
1457
     * @access public
1458
     * @parameters void
1459
     * @return void
1460
     */
1461
1462
    public function restrict_posts_menu_page() {
1463
1464
        global $pagenow, $typenow;
1465
1466
        $user = wp_get_current_user();
1467
1468
        /**
1469
         * Filter the option to hide the Posts menu page.
1470
         *
1471
         * @since 1.8.7
1472
         *
1473
         * @param bool $restrict default true
1474
         */
1475
1476
        $restrict = apply_filters('sensei_restrict_posts_menu_page', true );
1477
1478
        if ( in_array( 'teacher', (array) $user->roles ) && !current_user_can('delete_posts') && $restrict) {
1479
1480
            remove_menu_page('edit.php');
1481
1482
            if ($pagenow == "edit.php" || $pagenow == "post-new.php") {
1483
1484
                if ($typenow == '' || $typenow == 'post' || $typenow == 'page') {
1485
1486
                    wp_die('You do not have sufficient permissions to access this page.');
1487
1488
                }
1489
1490
            }
1491
1492
        }
1493
1494
    } // end restrict_posts_menu_page()
1495
1496
    /**
1497
     * Sensei_Teacher::restrict_comment_moderation()
1498
     *
1499
     * Restrict commendation moderation for teachers
1500
     * so they can only moderate comments made to posts they own.
1501
     *
1502
     * @since 1.8.7
1503
     * @access public
1504
     * @parameters obj $clauses
1505
     * @return obj $clauses
1506
     */
1507
1508
    public function restrict_comment_moderation($clauses) {
1509
1510
        global $pagenow;
1511
1512
        if( self::is_a_teacher( get_current_user_id() ) && $pagenow == "edit-comments.php") {
1513
1514
            $clauses->query_vars['post_author'] = get_current_user_id();
1515
1516
        }
1517
1518
        return $clauses;
1519
1520
    }   // end restrict_comment_moderation()
1521
1522
    /**
1523
     * Determine if a user is a teacher by ID
1524
     *
1525
     * @param int $user_id
1526
     *
1527
     * @return bool
1528
     */
1529
    public static function is_a_teacher( $user_id ){
1530
1531
        $user = get_user_by('id', $user_id);
1532
1533
        if( isset( $user->roles ) && in_array(  'teacher',  $user->roles )   ){
1534
1535
            return true;
1536
1537
        }else{
1538
1539
            return false;
1540
1541
        }
1542
1543
    }// end is_a_teacher
1544
1545
    /**
1546
     * The archive title on the teacher archive filter
1547
     *
1548
     * @since 1.9.0
1549
     */
1550
    public static function archive_title(){
1551
1552
        $author = get_user_by( 'id', get_query_var( 'author' ) );
1553
        $author_name = $author->display_name;
1554
        ?>
1555
            <h2 class="teacher-archive-title">
1556
1557
                <?php echo sprintf( __( 'All courses by %s', 'woothemes-sensei') , $author_name ); ?>
1558
1559
            </h2>
1560
        <?php
1561
1562
    }// archive title
1563
1564
    /**
1565
     * Removing course meta on the teacher archive page
1566
     *
1567
     * @since 1.9.0
1568
     */
1569
    public static function remove_course_meta_on_teacher_archive(){
1570
1571
        remove_action('sensei_course_content_inside_before', array( Sensei()->course, 'the_course_meta' ) );
1572
1573
    }
1574
1575
} // End Class
1576