/**
 * Create a new nomination entry
 *
 * @since  1.0.0
 * @param  integer $achievement_id  The associated achievement's post ID
 * @param  string  $title           The title of the new nomination
 * @param  string  $content         The content for the new nomination
 * @param  integer $user_nominated  The user ID of the person nominated
 * @param  integer $user_nominating The user ID of the person who did the nominating
 * @return bool                     True on succesful post creation, false otherwise
 */
function badgeos_create_nomination($achievement_id = 0, $title = '', $content = '', $user_nominated = 0, $user_nominating = 0)
{
    if (!badgeos_check_if_user_has_nomination(absint($user_nominated), absint($achievement_id))) {
        $submission_data = array('post_title' => esc_html($title), 'post_content' => esc_textarea($content), 'post_status' => 'publish', 'post_author' => absint($user_nominated), 'post_type' => 'nomination');
        //insert the post into the database
        if ($new_post_id = wp_insert_post($submission_data)) {
            //save the achievement id metadata
            add_post_meta($new_post_id, '_badgeos_nomination_achievement_id', absint($achievement_id));
            //save the user being nominated
            add_post_meta($new_post_id, '_badgeos_nomination_user_id', absint($user_nominated));
            //save the user doing the nomination
            add_post_meta($new_post_id, '_badgeos_nominating_user_id', absint($user_nominating));
            do_action('badgeos_save_nomination', $new_post_id);
            // Submission status workflow
            $status_args = array('achievement_id' => $achievement_id, 'user_id' => $user_nominated, 'from_user_id' => $user_nominating, 'submission_type' => 'nomination');
            badgeos_set_submission_status($new_post_id, 'pending', $status_args);
            return true;
        } else {
            return false;
        }
    }
}
/**
 * Nomination Form Shortcode.
 *
 * @since  1.0.0
 *
 * @param  array $atts Shortcode attributes.
 * @return string 	   HTML markup.
 */
function badgeos_nomination_form($atts = array())
{
    // Parse our attributes
    $atts = shortcode_atts(array('achievement_id' => get_the_ID()), $atts, 'badgeos_nomination');
    $output = '';
    // Verify user is logged in to view any submission data
    if (is_user_logged_in()) {
        // If we've just saved nomination data
        if (badgeos_save_nomination_data()) {
            $output .= sprintf('<p>%s</p>', __('Nomination saved successfully.', 'badgeos'));
        }
        // Return the user's nominations
        if (badgeos_check_if_user_has_nomination(get_current_user_id(), $atts['achievement_id'])) {
            $output .= badgeos_get_user_nominations('', $atts['achievement_id']);
        }
        // Include the nomination form
        $output .= badgeos_get_nomination_form(array('user_id' => get_current_user_id(), 'achievement_id' => $atts['achievement_id']));
    } else {
        $output = sprintf('<p><em>%s</em></p>', __('You must be logged in to post a nomination.', 'badgeos'));
    }
    return $output;
}