/**
 * Filter to create a badgeos-log-entry post
 *
 * @since  1.2.0
 * @param  integer $log_post_id The ID of the log entry (default: 0)
 * @param  array   $args        Available args to use for writing our new post
 * @return integer              The updated log entry ID
 */
function badgeos_log_entry($log_post_id, $args)
{
    // If we weren't explicitly given a title, let's build one
    if (empty($args['title'])) {
        $user = get_userdata($args['user_id']);
        $achievement = get_post($args['object_id']);
        $achievement_types = badgeos_get_achievement_types();
        $achievement_type = $achievement && isset($achievement_types[$achievement->post_type]['single_name']) ? $achievement_types[$achievement->post_type]['single_name'] : '';
        $args['title'] = !empty($title) ? $title : apply_filters('badgeos_log_entry_title', "{$user->user_login} {$args['action']} the \"{$achievement->post_title}\" {$achievement_type}", $args);
    }
    // Insert our entry as a 'badgeos-log-entry' post
    $log_post_id = wp_insert_post(array('post_title' => $args['title'], 'post_author' => absint($args['user_id']), 'post_type' => 'badgeos-log-entry', 'post_status' => 'publish'));
    // Return the ID of the newly created post
    return $log_post_id;
}
    function form($instance)
    {
        $defaults = array('title' => __('My Achievements', 'badgeos'), 'number' => '10', 'point_total' => '', 'set_achievements' => '');
        $instance = wp_parse_args((array) $instance, $defaults);
        $title = $instance['title'];
        $number = $instance['number'];
        $point_total = $instance['point_total'];
        $set_achievements = isset($instance['set_achievements']) ? (array) $instance['set_achievements'] : array();
        ?>
            <p><label><?php 
        _e('Title', 'badgeos');
        ?>
: <input class="widefat" name="<?php 
        echo esc_attr($this->get_field_name('title'));
        ?>
"  type="text" value="<?php 
        echo esc_attr($title);
        ?>
" /></label></p>
+			<p><label><?php 
        _e('Number to display (0 = all)', 'badgeos');
        ?>
: <input class="widefat" name="<?php 
        echo esc_attr($this->get_field_name('number'));
        ?>
"  type="text" value="<?php 
        echo absint($number);
        ?>
" /></label></p>
+			<p><label><input type="checkbox" id="<?php 
        echo esc_attr($this->get_field_name('point_total'));
        ?>
" name="<?php 
        echo esc_attr($this->get_field_name('point_total'));
        ?>
" <?php 
        checked($point_total, 'on');
        ?>
 /> <?php 
        _e('Display user\'s total points', 'badgeos');
        ?>
</label></p>
			<p><?php 
        _e('Display only the following Achievement Types:', 'badgeos');
        ?>
<br />
				<?php 
        //get all registered achievements
        $achievements = badgeos_get_achievement_types();
        //loop through all registered achievements
        foreach ($achievements as $achievement_slug => $achievement) {
            //hide the step CPT
            if ($achievement['single_name'] == 'step') {
                continue;
            }
            //if achievement displaying exists in the saved array it is enabled for display
            $checked = checked(in_array($achievement_slug, $set_achievements), true, false);
            echo '<label for="' . esc_attr($this->get_field_name('set_achievements')) . '_' . esc_attr($achievement_slug) . '">' . '<input type="checkbox" name="' . esc_attr($this->get_field_name('set_achievements')) . '[]" id="' . esc_attr($this->get_field_name('set_achievements')) . '_' . esc_attr($achievement_slug) . '" value="' . esc_attr($achievement_slug) . '" ' . $checked . ' />' . ' ' . esc_html(ucfirst($achievement['plural_name'])) . '</label><br />';
        }
        ?>
			</p>
        <?php 
    }
/**
 * Generate markup for awarding an achievement to a user
 *
 * @since  1.0.0
 * @param  object $user         The current user's $user object
 * @param  array  $achievements array of user-earned achievement IDs
 * @return string               concatenated markup
 */
function badgeos_profile_award_achievement($user = null, $achievement_ids = array())
{
    // Grab our achivement types
    $achievement_types = badgeos_get_achievement_types();
    ?>

	<h2><?php 
    _e('Award an Achievement', 'badgeos');
    ?>
</h2>

	<table class="form-table">
		<tr>
			<th><label for="thechoices"><?php 
    _e('Select an Achievement Type to Award:', 'badgeos');
    ?>
</label></th>
			<td>
				<select id="thechoices">
				<option></option>
				<?php 
    foreach ($achievement_types as $achievement_slug => $achievement_type) {
        echo '<option value="' . $achievement_slug . '">' . ucwords($achievement_type['single_name']) . '</option>';
    }
    ?>
				</select>
			</td>
		</tr>
		<tr><td id="boxes" colspan="2">
			<?php 
    foreach ($achievement_types as $achievement_slug => $achievement_type) {
        ?>
				<table id="<?php 
        echo esc_attr($achievement_slug);
        ?>
" class="widefat badgeos-table">
					<thead><tr>
						<th><?php 
        _e('Image', 'badgeos');
        ?>
</th>
						<th><?php 
        echo ucwords($achievement_type['single_name']);
        ?>
</th>
						<th><?php 
        _e('Action', 'badgeos');
        ?>
</th>
						<th><?php 
        _e('Awarded', 'badgeos');
        ?>
</th>
					</tr></thead>
					<tbody>
					<?php 
        // Load achievement type entries
        $the_query = new WP_Query(array('post_type' => $achievement_slug, 'posts_per_page' => '999', 'post_status' => 'publish'));
        if ($the_query->have_posts()) {
            ?>

						<?php 
            while ($the_query->have_posts()) {
                $the_query->the_post();
                // Setup our award URL
                $award_url = add_query_arg(array('action' => 'award', 'achievement_id' => absint(get_the_ID()), 'user_id' => absint($user->ID)));
                ?>
							<tr>
								<td><?php 
                the_post_thumbnail(array(50, 50));
                ?>
</td>
								<td>
									<?php 
                echo edit_post_link(get_the_title());
                ?>
								</td>
								<td>
									<a href="<?php 
                echo esc_url(wp_nonce_url($award_url, 'badgeos_award_achievement'));
                ?>
"><?php 
                printf(__('Award %s', 'badgeos'), ucwords($achievement_type['single_name']));
                ?>
</a>
									<?php 
                if (in_array(get_the_ID(), (array) $achievement_ids)) {
                    // Setup our revoke URL
                    $revoke_url = add_query_arg(array('action' => 'revoke', 'user_id' => absint($user->ID), 'achievement_id' => absint(get_the_ID())));
                    ?>
										<span class="delete"><a class="error" href="<?php 
                    echo esc_url(wp_nonce_url($revoke_url, 'badgeos_revoke_achievement'));
                    ?>
"><?php 
                    _e('Revoke Award', 'badgeos');
                    ?>
</a></span>
									<?php 
                }
                ?>

								</td>
							</tr>
						<?php 
            }
            ?>

					<?php 
        } else {
            ?>
						<tr>
							<th><?php 
            printf(__('No %s found.', 'badgeos'), $achievement_type['plural_name']);
            ?>
</th>
						</tr>
					<?php 
        }
        wp_reset_postdata();
        ?>

					</tbody>
					</table><!-- #<?php 
        echo esc_attr($achievement_slug);
        ?>
 -->
			<?php 
    }
    ?>
		</td><!-- #boxes --></tr>
	</table>

	<script type="text/javascript">
		jQuery(document).ready(function($){
			<?php 
    foreach ($achievement_types as $achievement_slug => $achievement_type) {
        ?>
				$('#<?php 
        echo $achievement_slug;
        ?>
').hide();
			<?php 
    }
    ?>
			$("#thechoices").change(function(){
				if ( 'all' == this.value )
					$("#boxes").children().show();
				else
					$("#" + this.value).show().siblings().hide();
			}).change();
		});
	</script>
	<?php 
}
        ?>
		<div id="actvity_chart_container" style="width:100%;height:250px;"></div>
		</div>
		<?php 
    }, function () {
        $option_name = '_bai_dashboard_widget_options';
        // Update widget options
        if ('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['bai_dashboard_widget_post'])) {
            update_option($option_name, $_POST['bai_dashboard_widget_options']);
        }
        // Get widget options
        if (!($widget_options = get_option($option_name))) {
            $widget_options = array();
        }
        $achievement_type = $widget_options['achievement_type'];
        $achievement_types = badgeos_get_achievement_types();
        $cur_interval = isset($widget_options['interval']) ? $widget_options['interval'] : 'monthly';
        $cur_display = isset($widget_options['display']) ? $widget_options['display'] : 'chart';
        $intervals = array('monthly', 'weekly', 'daily');
        $displays = array('chart', 'table');
        $preview = $widget_options['preview'];
        ?>

		<p>
			<label for="bai_achievement_type"><?php 
        _e('Choose the achievement type:', 'badgeos-activity-index');
        ?>
</label>
			<select class="widefat" id="bai_achievement_type" name="bai_dashboard_widget_options[achievement_type]">
				<option value="" <?php 
        selected(null, $achievement_type);