WordPress.org

Ready to get started?Download WordPress

Forums

BuddyPress Docs
[resolved] Unread docs count (11 posts)

  1. laurahreniucu
    Member
    Posted 11 months ago #

    Hello.

    Is it possible to get a list of unread docs, so the user can see better which docs he/she has accessed and which not?

    http://wordpress.org/plugins/buddypress-docs/

  2. Boone Gorges
    Moderator
    Plugin Author

    Posted 11 months ago #

    This is a cool idea, but the concept of "unread" needs a bit of fleshing out. It could mean:
    - Docs that the user has never looked at
    - Docs that have been edited since the last time the user looked at them
    - Docs that have been edited or commented on since the last time the user looked at them

    I'm not sure what makes the most sense from a user's point of view. Do you have thoughts about it?

  3. laurahreniucu
    Member
    Posted 11 months ago #

    By "unread" i mean docs that the user has never looked at.

  4. Boone Gorges
    Moderator
    Plugin Author

    Posted 11 months ago #

    Do you have a sense of where/how this would appear in the UI? Or are you just looking for a function that'll do it?

    FWIW, at the moment, BP Docs does not currently keep track at all of read/unread status. This'd require storing lots of data: potentially, one piece of data for every Doc-user pair. So I'm wary of including it in Docs itself. But I do think you could build a plugin for this purpose, and I'd be happy to point you in the right direction, if that's what you're looking for.

  5. laurahreniucu
    Member
    Posted 11 months ago #

    Ok, i will explain in detail what i want.

    I have a website based on Buddypress and Buddypress Docs. It's a file sharing system.

    Members can create docs that admins / mods will then read/edit. I need to build some kind of counter to show me how many docs per member has the admin not read yet. And this needs to be showed near the group title and the member name (so, one counter for unread docs for members and anotehr for groups).

    I hope you understand what i'm saying :)

    Thank you.

  6. Boone Gorges
    Moderator
    Plugin Author

    Posted 11 months ago #

    Yup, that use case definitely makes sense. I'd say that this needs to be built as a separate plugin, because it's very use-case-specific. Here's the strategy I'd take:

    - Decide on a good storage mechanism for "read/unread" data. I guess postmeta probably makes the most sense, eg update_post_meta( $doc_id, 'read_by_user_' . $user_id, 1 )
    - When loading a Doc, update that 'read_by_user_' key
    - To get your unread counts, do a WP_Query, and use the 'meta_query' param to limit to those items that have/don't have the 'read_by_user_' . $user_id postmeta.
    - Either use hooks or custom templates to display the data

    Good luck! Sounds like a cool project.

  7. laurahreniucu
    Member
    Posted 11 months ago #

    So, when i create the doc, i use update_post_meta to make the key = 1, and then, when i open the doc, use update_post_meta again to make it = 0?

    Or i use it only when loading the Doc.
    I know it may be a dumb question, but, as i said, i'm kind of a noob :D

  8. laurahreniucu
    Member
    Posted 11 months ago #

    Also, unfortunatelly, i don't have time to build a new plugin. I want to edit this plugin's files. It's ok, since i won't be upgrading.

    Could you please tell me where should i edit?

    Thank you :)

  9. Boone Gorges
    Moderator
    Plugin Author

    Posted 11 months ago #

    It really doesn't take any additional time to build a plugin as opposed to editing the Docs core files. But, it's your call.

    I'm afraid I can't give more specific pointers than what I've already given above. You'll have to track how Docs are saved; look into buddypress-docs/includes/query-builder.php to get a preliminary sense.

  10. laurahreniucu
    Member
    Posted 11 months ago #


  11. laurahreniucu
    Member
    Posted 11 months ago #

    Ok, so i managed to get this done. I will post the changes i made, in case anyone needs this.

    1. buddypress-docs\includes\query-builder.php - function "save" - line 556
    ADD

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $user_query = new WP_User_Query( array( 'exclude' => array( $user_id) ) );
    
    if ( ! empty( $user_query->results ) ) {
    	foreach ( $user_query->results as $user ) {
    		update_post_meta( $this->doc_id, 'read_by_user_' . $user->ID, 1 );
    	}
    }

    2. buddypress-docs\includes\templates\docs\docs-header.php - line 9
    ADD

    <?php $current_user = wp_get_current_user();
    	  $user_id = $current_user->ID;
    	  ?>
    <?php update_post_meta( get_the_ID(), 'read_by_user_' . $user_id, 0 ) ?>

    3. buddypress-docs\includes\functions.php - end of file
    ADD

    function bp_docs_get_unread_count($user_id = false, $author_id) {
    	if ( false === $user_id ) {
    		$user_id = bp_loggedin_user_id();
    	}
    
    	$unread_post_count = 0;
    	$unread_key = 'read_by_user_' . $user_id;
    
    	$args = array(
    		'author' => $author_id,
    		'post_type' => 'bp_doc',
    		'meta_query' => array(
    			array(
    				'key' => $unread_key,
    				'value' => '1'
    			)
    		)
    	);
    
    	$query = new WP_Query( $args );
    
    	$unread_post_count = $query->found_posts;
    	wp_reset_postdata(); 
    
    	if( $unread_post_count == 0) {
    		$unread_post_count = '';
    	}
    
    	return $unread_post_count;
    }

    AND ADD

    function get_user_id_by_display_name( $display_name ) {
        global $wpdb;
    
        if ( ! $user = $wpdb->get_row( $wpdb->prepare(
            "SELECT <code>ID</code> FROM $wpdb->users WHERE <code>display_name</code> = %s", $display_name
        ) ) )
            return false;
    
        return $user->ID;
    }

    4. buddypress\bp-themes\bp-default\members\members-loop.php - line 47
    ADD

    <?php
    	$current_user = wp_get_current_user();
    	$user_id = $current_user->ID;
    	$name_id = get_user_id_by_display_name(bp_get_member_name());
    	?>

    AND REPLACE
    <a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
    WITH
    <a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?><?php echo bp_docs_get_unread_count($user_id, $name)?></a>

    This will result in the total number of unread docs for each user to be listed next to the user name on the "Members" page.

    Thank you Boone for all your answers and especially for your patience with me :)

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.