vapvarun icon

LearnDash Custom Dashboard Tab for Student Assignments

vapvarun | PRO | 11/28/23 08:45:19 AM UTC | 0 ⭐ | 1282 👁️ | Never ⏰ | [learndash, learndash-dashboard]
PHP |

3.4 KB

|

Software

|

0 👍

/

0 👎

/**
 * LearnDash Custom Dashboard Tab for Student Assignments
 *
 * This code adds a custom "Assignment" tab to WordPress's LearnDash dashboard navigation menu. 
 * The tab links to a page with the slug 'student-assignment' and displays an icon and label.
 */
 
function ld_dashboard_add_nav_menu( $menu_items ) {
    $menu_items['all']['custom'] = array(
        'url'   => get_the_permalink() . '?tab=student-assignment', // tab=custom set the slug of your custom tab, which will later be used to display content in this tab.
        'icon'  => '<img src="https://demos.wbcomdesigns.com/wp-content/uploads/2023/11/report.svg">',
        'label' => 'Assignment',
    );
    return $menu_items;
}
add_filter( 'learndash_dashboard_nav_menu', 'ld_dashboard_add_nav_menu', 10, 1 );
 
function ld_dashboard_after_content_callback() {
    if ( isset( $_GET['tab'] ) && 'student-assignment' === $_GET['tab'] ) {
        echo do_shortcode('[sfwd_assignment_list]');
    }
}
 
add_action( 'ld_dashboard_after_content', 'ld_dashboard_after_content_callback' );
 
 
function sfwd_assignment_list_shortcode() {
    ob_start();
 
    $args = array(
        'post_type'      => 'sfwd-assignment',
        'posts_per_page' => -1, // Retrieve all assignments
        // Add any additional query parameters as needed
    );
 
    // Check if the current user is an administrator
    $is_admin = current_user_can('manage_options');
 
    // If the user is an admin, show all posts; otherwise, show only their own posts
    if (!$is_admin) {
        $args['author'] = get_current_user_id();
    }
 
    $assignments = get_posts($args);
 
    if ($assignments) {
        echo '<table>';
        echo '<thead>';
        echo '<tr>';
        echo '<th>Title</th>';
        echo '<th>Author</th>';
        echo '<th>Publish Date</th>';
        echo '<th>Assignment Name</th>';
        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';
 
        foreach ($assignments as $assignment) {
            $author_name = get_the_author_meta('display_name', $assignment->post_author);
            $publish_date = get_the_date('Y-m-d', $assignment->ID);
            $file_name = get_post_meta($assignment->ID, 'file_name', true);
            $file_link = get_post_meta($assignment->ID, 'file_link', true);
 
            // Trim the title to a maximum of 16 characters
            $trimmed_title = mb_strimwidth($assignment->post_title, 0, 19, '...');
 
            // Trim the filename to a maximum of 25 characters
            $trimmed_filename = mb_strimwidth($file_name, 0, 25, '...');
 
            echo '<tr>';
            echo '<td>' . esc_html($trimmed_title) . '</td>';
            echo '<td>' . esc_html($author_name) . '</td>';
            echo '<td>' . esc_html($publish_date) . '</td>';
 
            // Check if both file_name and file_link are available
            if ($file_name && $file_link) {
                $file_link = esc_url($file_link);
                echo '<td><a href="' . $file_link . '" target="_blank">' . esc_html($trimmed_filename) . '</a></td>';
            } else {
                echo '<td></td>'; // Empty cell if file information is not available
            }
 
            echo '</tr>';
        }
 
        echo '</tbody>';
        echo '</table>';
    } else {
        echo '<p>No SFWD assignments found.</p>';
    }
 
    return ob_get_clean();
}
 
add_shortcode('sfwd_assignment_list', 'sfwd_assignment_list_shortcode');
 
 

Comments