Course Visibility based on Student department

In this tip we’ll learn how we can force course visibility to students of a certain department. For example, Students in Chemistry section should only see courses from course category Chemistry.

Or Users form Corporation A should only see courses from Course category – Corporation A

 

Steps :

  1. Create a custom profile field (radio buttons) with name department (case sensitive)
  2. Create course category for every department.
  3. Add below code in Child theme – functions.php
add_filter('wplms_carousel_course_filters','wplms_exclude_course_category_student_department');
add_filter('wplms_grid_course_filters','wplms_exclude_course_category_student_department');
add_filter('vibe_editor_filterable_type','wplms_exclude_course_category_student_department');
add_filter('bp_course_wplms_filters','wplms_exclude_course_category_student_department');

function wplms_exclude_course_category_student_department($args){

  	if($args['post_type'] != 'course') // Bail out if post type is not course
  		return $args; 

  	if(!is_user_logged_in() || current_user_can('edit_posts')) // Bail out if user not logged in. OR administrator & instructors
  		return $args;


  	// Bail out if viewing "My Courses" section	
  	if(isset($args['meta_query']) && is_array($args['meta_query'])){
       foreach($args['meta_query'] as $query){
         if(isset($query) && is_array($query)){
            if($query['key'] == $user_id){ // If in my courses, do not apply this
              return $args;
            }
         }
       }
    }


    $user_id=get_current_user_id();
    $custom_profile_field_name = 'Department'; //Case sensitive , Custom profile field name

    $profile_field_value_category_map =array(  // Profile field value => Course Category slug , Generate this map based on your requirements
    	'chemistry' =>	'chemistry',
    	'physics' =>	'physics', 
	);
    // Get profile field value 
    if(function_exists('bp_get_profile_field_data')){
    	$value = bp_get_profile_field_data('field='.$custom_profile_field_name.'&user_id='.$user_id);
    }

    if(empty($value)) //No department value found for user
    	return $args; 
  	
  	
  	if(isset($profile_field_value_category_map[$value])){ // Value exists in profile field - category map
  		if(empty($args['tax_query'])){
  			$args['tax_query'] = array(array(
  				'taxonomy' => 'course-cat',
  				'field'    => 'slug',
  				'terms'	   =>$profile_field_value_category_map[$value],
  				'operator' => 'IN'
			)); 
  		}else{
  			$args['tax_query'][] = array(
  				'taxonomy' => 'course-cat',
  				'field'    => 'slug',
  				'terms'	   =>$profile_field_value_category_map[$value],
  				'operator' => 'IN'
			); 
  		}
  	}

  	return $args;    
}

 

 

Important Points :

  1. This tip only works for Students, not for administrators or Instructors
  2. This tip only works when student has set a course department, in case of no department all the courses are visible.
  3. This tip does not apply on My Courses section. So if student subscribes to the course, or instructor manually adds her to the course, the course will be visible in my courses section.

Leave a Reply

Your email address will not be published. Required fields are marked *