}
if (isset($_GET['instructor_id']) && is_numeric($_GET['instructor_id'])) {
    $instructor = new Instructor($_GET['instructor_id']);
}
if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['instructor_id']) && is_numeric($_GET['instructor_id'])) {
    if (!isset($_GET['cp_nonce']) || !wp_verify_nonce($_GET['cp_nonce'], 'delete_instructor_' . $_GET['instructor_id'])) {
        die(__('Cheating huh?', 'cp'));
    }
    $instructor->delete_instructor();
    $message = __('Selected instructor has been removed successfully.', 'cp');
}
if (isset($_GET['action']) && ($_GET['action'] == 'edit' || $_GET['action'] == 'view') && isset($_GET['instructor_id']) && is_numeric($_GET['instructor_id'])) {
    include 'instructors-profile.php';
} else {
    // Query the users
    $wp_user_search = new Instructor_Search($usersearch, $page_num);
    ?>

	<div class="wrap nosubsub instructors cp-wrap">

		<div class="icon32 " id="icon-users"><br></div>
		<h2><?php 
    _e('Instructors', 'cp');
    if (current_user_can('manage_options')) {
        ?>
				<a class="add-new-h2" href="user-new.php"><?php 
        _e('Add New', 'cp');
        ?>
</a><?php 
    }
    ?>
<?php

global $wp_roles;
if (isset($_POST['submit']) && current_user_can('manage_options') && isset($_POST['instructor_capability'])) {
    /* Set capabilities for each instructor user */
    $wp_user_search = new Instructor_Search();
    // $wp_user_search = new Instructor_Search( $usersearch, $page_num );
    foreach ($wp_user_search->get_results() as $user) {
        CoursePress_Capabilities::grant_private_caps($user->ID);
        // Don't remove capabilities from administrators
        /* if( user_can( $user->ID, 'manage_options' ) ){
        	  continue;
        	  } */
        $role = new WP_User($user->ID);
        $user_capabilities = $role->wp_capabilities;
        // More than the hidden field needs to be present to add roles.
        if (isset($_POST['instructor_capability']) && 1 < count($_POST['instructor_capability'])) {
            foreach ($user_capabilities as $key => $old_cap) {
                // Make sure to only remove CoursePress instructor capabilities
                if (!in_array($key, $_POST['instructor_capability']) && in_array($key, array_keys(CoursePress_Capabilities::$capabilities['instructor']))) {
                    //making the operation less expensive
                    if (!user_can($user->ID, 'manage_options')) {
                        $role->remove_cap($key);
                    }
                }
            }
            foreach ($_POST['instructor_capability'] as $new_cap) {
                $role->add_cap($new_cap);
            }
        } else {
            //all unchecked, remove all instructor capabilities