/** * Renders the mentor dashboard for the Mentor-Mentee Plugin. */ function mmp_render_mentor_dashboard() { // Ensure the user has the correct role if (!current_user_can('mentor') && !current_user_can('manage_options')) { wp_die(__('You do not have permission to access this page.', 'mentor-mentee-plugin')); } // Get the current mentor's user ID $current_user_id = get_current_user_id(); // Fetch mentees paired with this mentor global $wpdb; $pairings_table = $wpdb->prefix . 'pairings'; $mentees_table = $wpdb->prefix . 'mentees'; $sessions_table = $wpdb->prefix . 'sessions'; $feedback_table = $wpdb->prefix . 'feedback'; $mentors_table = $wpdb->prefix . 'mentors'; // Retrieve mentees assigned to the current mentor $mentees = $wpdb->get_results( $wpdb->prepare( "SELECT m.* FROM $pairings_table p JOIN $mentees_table m ON p.mentee_id = m.user_id WHERE p.mentor_id = %d", $current_user_id ) ); // Handle availability update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_availability']) && check_admin_referer('update_availability_nonce', '_wpnonce')) { $availability = sanitize_text_field($_POST['availability']); $updated = $wpdb->update( $mentors_table, ['availability' => $availability], ['user_id' => $current_user_id] ); if ($updated !== false) { echo '

' . esc_html__('Availability updated successfully.', 'mentor-mentee-plugin') . '

'; } else { echo '

' . esc_html__('Failed to update availability.', 'mentor-mentee-plugin') . '

'; } } // Handle session scheduling if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['schedule_session']) && check_admin_referer('schedule_session_nonce', '_wpnonce')) { $mentee_id = intval($_POST['mentee_id']); $session_date = sanitize_text_field($_POST['session_date']); $inserted = $wpdb->insert($sessions_table, [ 'mentor_id' => $current_user_id, 'mentee_id' => $mentee_id, 'session_date' => $session_date, 'status' => 'upcoming', ]); if ($inserted) { echo '

' . esc_html__('Session scheduled successfully.', 'mentor-mentee-plugin') . '

'; } else { echo '

' . esc_html__('Failed to schedule session.', 'mentor-mentee-plugin') . '

'; } } // Handle feedback submission if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_feedback']) && check_admin_referer('submit_feedback_nonce', '_wpnonce')) { $mentee_id = intval($_POST['mentee_id']); $feedback = sanitize_textarea_field($_POST['feedback']); $inserted = $wpdb->insert($feedback_table, [ 'mentor_id' => $current_user_id, 'mentee_id' => $mentee_id, 'feedback' => $feedback, 'submitted_at' => current_time('mysql'), ]); if ($inserted) { echo '

' . esc_html__('Feedback submitted successfully.', 'mentor-mentee-plugin') . '

'; } else { echo '

' . esc_html__('Failed to submit feedback.', 'mentor-mentee-plugin') . '

'; } } // Fetch sessions $upcoming_sessions = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $sessions_table WHERE mentor_id = %d AND status = 'upcoming' ORDER BY session_date ASC", $current_user_id ) ); $completed_sessions = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $sessions_table WHERE mentor_id = %d AND status = 'completed' ORDER BY session_date DESC", $current_user_id ) ); // Fetch feedback $feedback = []; foreach ($mentees as $mentee) { $feedback[$mentee->user_id] = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $feedback_table WHERE mentee_id = %d AND mentor_id = %d ORDER BY submitted_at DESC", $mentee->user_id, $current_user_id ) ); } // Load the mentor dashboard template from the templates folder mmp_load_template('mentor-dashboard-template', [ 'mentees' => $mentees, 'upcoming_sessions' => $upcoming_sessions, 'completed_sessions' => $completed_sessions, 'feedback' => $feedback, ]); } /** * Renders the mentee dashboard for the Mentor-Mentee Plugin. */ function mmp_render_mentee_dashboard() { // Ensure the user has the correct role if (!current_user_can('mentee') && !current_user_can('manage_options')) { wp_die(__('You do not have permission to access this page.', 'mentor-mentee-plugin')); } global $wpdb; // Fetch current user ID $current_user_id = get_current_user_id(); // Fetch sessions for the current mentee $sessions_table = $wpdb->prefix . 'sessions'; $upcoming_sessions = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $sessions_table WHERE mentee_id = %d AND status = 'upcoming' ORDER BY session_date ASC", $current_user_id ) ); $completed_sessions = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $sessions_table WHERE mentee_id = %d AND status = 'completed' ORDER BY session_date DESC", $current_user_id ) ); // Fetch feedback for the current mentee $feedback_table = $wpdb->prefix . 'feedback'; $feedback = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $feedback_table WHERE mentee_id = %d ORDER BY submitted_at DESC", $current_user_id ) ); // Handle goal updates if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_goals']) && check_admin_referer('update_goals_nonce', '_wpnonce')) { $goals = sanitize_text_field($_POST['mentee_goals']); $preferences = sanitize_text_field($_POST['mentee_preferences']); $mentees_table = $wpdb->prefix . 'mentees'; $updated = $wpdb->update( $mentees_table, [ 'goals' => $goals, 'preferences' => $preferences, ], ['user_id' => $current_user_id] ); if ($updated !== false) { echo '

' . esc_html__('Goals updated successfully.', 'mentor-mentee-plugin') . '

'; } else { echo '

' . esc_html__('Failed to update goals.', 'mentor-mentee-plugin') . '

'; } } // Include the mentee dashboard template from the templates folder mmp_load_template('mentee-dashboard-template', [ 'upcoming_sessions' => $upcoming_sessions, 'completed_sessions' => $completed_sessions, 'feedback' => $feedback, ]); } /** * Renders the admin dashboard for the Mentor-Mentee Plugin. */ function mmp_render_admin_dashboard() { // Ensure the user has the correct permissions if (!current_user_can('manage_options')) { wp_die(__('You do not have permission to access this page.', 'mentor-mentee-plugin')); } global $wpdb; // Fetch data $mentors = mmp_get_all_mentors(); $mentees = mmp_get_all_mentees(); // Handle form submissions if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (isset($_POST['add_mentor']) && check_admin_referer('add_mentor_nonce', '_wpnonce')) { $expertise = sanitize_text_field($_POST['expertise']); $availability = sanitize_text_field($_POST['availability']); $user_id = get_current_user_id(); mmp_add_mentor($user_id, $expertise, $availability); echo '

' . esc_html__('Mentor added successfully.', 'mentor-mentee-plugin') . '

'; } elseif (isset($_POST['add_mentee']) && check_admin_referer('add_mentee_nonce', '_wpnonce')) { $goals = sanitize_text_field($_POST['goals']); $preferences = sanitize_text_field($_POST['preferences']); $user_id = get_current_user_id(); mmp_add_mentee($user_id, $goals, $preferences); echo '

' . esc_html__('Mentee added successfully.', 'mentor-mentee-plugin') . '

'; } } // Include the admin dashboard template from the templates folder mmp_load_template('admin-dashboard-template', [ 'mentors' => $mentors, 'mentees' => $mentees, ]); } /** * Handles role creation for the Mentor-Mentee Plugin. */ // Create custom roles function mmp_create_roles() { // Mentor Role add_role( 'mentor', __('Mentor', 'mentor-mentee-plugin'), [ 'read' => true, // Basic access 'edit_own_data' => true, // Custom capability to edit their own data ] ); // Mentee Role add_role( 'mentee', __('Mentee', 'mentor-mentee-plugin'), [ 'read' => true, // Basic access 'edit_own_data' => true, // Custom capability to edit their own data ] ); // Mentor Admin Role add_role( 'mentor_admin', __('Mentor Admin', 'mentor-mentee-plugin'), [ 'read' => true, 'manage_options' => true, // Admin settings access 'edit_any_data' => true, // Custom capability to edit all data ] ); // Assign capabilities to default administrator role $admin_role = get_role('administrator'); if ($admin_role) { $admin_role->add_cap('edit_any_data'); // Allow admin to edit all data } } // Remove custom roles and capabilities (on plugin deactivation) function mmp_remove_roles() { // Remove custom roles remove_role('mentor'); remove_role('mentee'); remove_role('mentor_admin'); // Remove custom capabilities from administrator role $admin_role = get_role('administrator'); if ($admin_role) { $admin_role->remove_cap('edit_any_data'); } } // Hook role creation and removal register_activation_hook(__FILE__, 'mmp_create_roles'); register_deactivation_hook(__FILE__, 'mmp_remove_roles');