<?php
/*************************************************************************
this file contains all of the functions used to add and delete bookmarks
************************************************************************/

// checks whether a post has been read
function mar_check_post_is_read($post_id, $user_id) {
	// this line was just for testing purposes
	//delete_user_meta(mar_get_user_id(), 'mar_read_posts');
	$user_meta = mar_get_user_read_posts($user_id);
	if($user_meta) :
		foreach ($user_meta as $read_post) {
            if ($read_post == $post_id) {
				return true;
			}
		}
	endif;
	return false;
}

// gets the current user's ID
function mar_get_user_id() {
    global $current_user;
    get_currentuserinfo();
    return $current_user->ID;
}

// adds a post to the user meta
function mar_add_to_usermeta($post_id) {
	$user_id = mar_get_user_id();
    $mar_read = mar_get_user_read_posts($user_id);
	// add out post id to the end of the array
	$mar_read[] = $post_id;
 	mar_update_user_meta($mar_read, $user_id);
}

// removes a post ID from the user meta
function mar_remove_from_usermeta($post_id) {
	$user_id = mar_get_user_id();
    $mar_read = mar_get_user_read_posts($user_id);
	foreach ($mar_read as $read_post => $id) {
		if ($id == $post_id) {
			unset($mar_read[$read_post]);
		}
	}	
 	mar_update_user_meta($mar_read, $user_id);
}

// updates the current users read IDs
function mar_update_user_meta($arr, $user_id) {
    return update_user_meta($user_id,'mar_read_posts', $arr);
}

// gets all read posts for the current user
function mar_get_user_read_posts($user_id) {
	return get_user_meta($user_id, 'mar_read_posts', true);
}

// processes the ajax mark as read request
function mar_post_mark_as_read() {
  	if ( isset( $_POST["post_read"] ) ) {
		$post_id = intval($_POST["post_read"]);

		$mark_as_read = mar_add_to_usermeta($post_id);
		$update_count = mar_increase_count($post_id);
		
		die();
	}
}
add_action('wp_ajax_mark_post_as_read', 'mar_post_mark_as_read');

// processes the ajax mark as unread request
function mar_post_mark_as_unread() {
  	if ( isset( $_POST["del_post_id"] ) ) {
		$post_id = intval($_POST["del_post_id"]);
		$mark_as_unread = mar_remove_from_usermeta($post_id);
		$update_count = mar_decrease_count($post_id);
		die();
	}
}
add_action('wp_ajax_mark_post_as_unread', 'mar_post_mark_as_unread');

// increases the read posts count for $post_id
function mar_increase_count($post_id) {
	global $wpdb;
	global $mar_db_table;
	
	// add the post ID to the count database if it doesn't already exist
	if(!$wpdb->query("SELECT `read_count` FROM `" . $mar_db_table . "` WHERE id=" . $post_id . ";")) {
		$add_post_id = $wpdb->insert( $mar_db_table, 
			array(
				'id' => $post_id, 
				'read_count' => 1 
			)
		);
	} else {	
		$count = $wpdb->query("UPDATE " . $mar_db_table . " SET read_count = read_count + 1 WHERE id=" . $post_id . ";");
	}
}

// decreases the bookmark count for $post_id
function mar_decrease_count($post_id) {
	global $wpdb;
	global $mar_db_table;
	
	$count = $wpdb->query("UPDATE " . $mar_db_table . " SET read_count = read_count - 1 WHERE id=" . $post_id . ";");
}

// retrieves all report card data for the current user
// this includes unread / read posts with counts
function mar_get_user_report() {
	$user_report = array();
	
	// get all read posts for the current user
	$read_posts = mar_get_user_read_posts( mar_get_user_id() );
	
	// get all unread posts for the current user
	$unread_posts = get_posts(array('exclude' => $read_posts, 'posts_per_page' => -1));
	
	$total_posts = count($unread_posts) + count($read_posts);
	// calculate the percentage of post sthat have been read
	$read_percentage = round( ( count($read_posts) * 100 ) / $total_posts);
	
	$user_report['read_posts'] = $read_posts;
	$user_report['read_posts_count'] = count($read_posts);
	$user_report['unread_posts_count'] = count($unread_posts);
	$user_report['total_posts'] = $total_posts;
	$user_report['read_percentage'] = $read_percentage;
	
	return $user_report;
}

// automarks a post as read after the user has been on the post for the designated length of time
function mar_auto_mark_as_read($content) {
	global $mar_options, $post;
	
	// proceed if the auto option is enabled and we are on a single post
	if($mar_options['auto'] && is_single()) {
		// proceed if the post is not already marked as read
		if(!mar_check_post_is_read($post->ID, mar_get_user_id())) {
			mar_load_auto_scripts();
		}
	}
}
add_action('template_redirect', 'mar_auto_mark_as_read');