diff --git a/badgeos.php b/badgeos.php index 5760c632..c5953d5e 100644 --- a/badgeos.php +++ b/badgeos.php @@ -4,7 +4,7 @@ * Plugin URI: http://www.badgeos.org/ * Description: BadgeOS lets your site’s users complete tasks and earn badges that recognize their achievement. Define achievements and choose from a range of options that determine when they're complete. Badges are Mozilla Open Badges (OBI) compatible through integration with the “Open Credit” API by Credly, the free web service for issuing, earning and sharing badges for lifelong achievement. * Author: LearningTimes -* Version: 1.4.7 +* Version: 1.4.8 * Author URI: https://credly.com/ * License: GNU AGPL * Text Domain: badgeos @@ -33,7 +33,7 @@ class BadgeOS { * * @var string */ - public static $version = '1.4.7'; + public static $version = '1.4.8'; function __construct() { // Define plugin constants @@ -59,6 +59,9 @@ function __construct() { add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); add_action( 'init', array( $this, 'credly_init' ) ); + //add action for adding ckeditor script + add_action('wp_footer', 'wp_enqueue_scripts'); + } /** @@ -282,6 +285,16 @@ function frontend_scripts() { ); wp_localize_script( 'badgeos-achievements', 'BadgeosCredlyData', $data ); + wp_enqueue_script( + 'ck_editor_cdn', + ('http://cdn.ckeditor.com/4.5.3/full/ckeditor.js'), false, null, false + ); + + wp_enqueue_script( + 'custom_script', + plugins_url( '/js/ckeditor.js' , __FILE__ ), + false,null,true + ); } /** diff --git a/css/badgeos-front.css b/css/badgeos-front.css index 50e9bd38..890dd1ff 100644 --- a/css/badgeos-front.css +++ b/css/badgeos-front.css @@ -201,11 +201,19 @@ ul.badgeos-achievement-earners-list li { border-radius: 5px; margin-bottom:15px; } + +.badgeos-submission-comment-form{ + width:100%; +} +.badgeos-submission-comment-form .badgeos-submission-comment { + width:100% !important; +} .badgeos-submission-comment { padding: 1em 1em 0; margin-bottom: .5em; overflow: hidden; border-bottom: 1px dotted #aaa; + width: 600px !important; } .badgeos-submission-comment:last-child { border-bottom:0; @@ -233,3 +241,21 @@ ul.badgeos-achievement-earners-list li { .badgeos-submission-comment-entry:last-child:after { border: 0; } + +.badgeos-file-submission, .badgeos-submission-file{ + margin: 8px 0; +} + +.badgeos-feedback-filter select#group_id{ + max-width:26% !important; + word-wrap: normal; +} + +.badgeos_submission_message p{ + background: #ffff00; + color: #333; + border-radius: 5px; + padding: 3px 10px; + text-align: center; + +} diff --git a/css/badgeos-widgets.css b/css/badgeos-widgets.css index dc9477c5..66841ca2 100644 --- a/css/badgeos-widgets.css +++ b/css/badgeos-widgets.css @@ -8,19 +8,22 @@ min-height: 25px; } .widget-achievements-listing li.has-thumb { - min-height: 55px; + width: 100%; + float: left; + display: table; } .widget-achievements-listing .badgeos-item-thumb { - position: absolute; - top: 50%; - left: 0; + float: left; + display: table-cell; + vertical-align: middle; } .widget-achievements-listing .widget-badgeos-item-title { font-weight: bold; } .widget-achievements-listing .has-thumb .widget-badgeos-item-title { - display: block; - text-indent: 60px; + width: 75%; + display: table-cell; + vertical-align: middle; } .share-credly { position: relative; @@ -112,3 +115,7 @@ .credly-wrap .success { color: green; } +.widget{ + float:left; + width:100%; +} \ No newline at end of file diff --git a/includes/achievement-functions.php b/includes/achievement-functions.php index 2c9490c6..3013bb73 100644 --- a/includes/achievement-functions.php +++ b/includes/achievement-functions.php @@ -260,10 +260,10 @@ function badgeos_is_achievement_sequential( $achievement_id = 0 ) { */ function badgeos_achievement_user_exceeded_max_earnings( $user_id = 0, $achievement_id = 0 ) { - $max_earnings = absint( get_post_meta( $achievement_id, '_badgeos_maximum_earnings', true ) ); + $max_earnings = get_post_meta( $achievement_id, '_badgeos_maximum_earnings', true); // If the badge has an earning limit, and we've earned it bdfore... - if ( $max_earnings && $user_has_badge = badgeos_get_user_achievements( array( 'user_id' => absint( $user_id ), 'achievement_id' => absint( $achievement_id ) ) ) ) { + if ( $max_earnings != -1 && $user_has_badge = badgeos_get_user_achievements( array( 'user_id' => absint( $user_id ), 'achievement_id' => absint( $achievement_id ) ) ) ) { // If we've earned it as many (or more) times than allowed, // then we have exceeded maximum earnings, thus true if ( count( $user_has_badge ) >= $max_earnings ) { diff --git a/includes/cmb/js/cmb.js b/includes/cmb/js/cmb.js index f7840fdb..b83fa579 100644 --- a/includes/cmb/js/cmb.js +++ b/includes/cmb/js/cmb.js @@ -198,7 +198,7 @@ .on( 'click', '.add-row-button', function(e) { e.preventDefault(); - self = $(this); + var self = $(this); var tableselector = '#'+ self.data('selector'); var $table = $(tableselector); diff --git a/includes/content-filters.php b/includes/content-filters.php index 4b693d18..ad0fc123 100644 --- a/includes/content-filters.php +++ b/includes/content-filters.php @@ -613,7 +613,7 @@ function badgeos_render_submission( $submission = null, $args = array() ) { $output = '
'; // Submission Title - $output .= '

' . sprintf( __( 'Submission: "%1$s" (#%2$d)', 'badgeos' ), get_the_title( $achievement_id ), $submission->ID ) . '

'; + $output .= '

' . sprintf( __( 'Submission: "%1$s"', 'badgeos' ), get_the_title( $achievement_id )) . '

'; // Submission Meta $output .= '

'; $output .= sprintf( '%1$s %2$s
', __( 'Author:', 'badgeos' ), $display_name ); @@ -626,7 +626,7 @@ function badgeos_render_submission( $submission = null, $args = array() ) { // Submission Content $output .= '

'; - $output .= wpautop( $submission->post_content ); + $output .= html_entity_decode( $submission->post_content , ENT_QUOTES, 'UTF-8' ); $output .= '
'; // Include any attachments @@ -705,7 +705,7 @@ function badgeos_render_submission_comment( $comment = null, $odd_even = 'odd' ) // Content $output .= '
'; - $output .= wpautop( $comment->comment_content ); + $output .= html_entity_decode( wpautop( $comment->comment_content ), ENT_QUOTES, 'UTF-8' ); $output .= '
'; $output .= ''; diff --git a/includes/meta-boxes.php b/includes/meta-boxes.php index 6c587585..2a1fc6c8 100644 --- a/includes/meta-boxes.php +++ b/includes/meta-boxes.php @@ -125,7 +125,7 @@ function badgeos_custom_metaboxes( array $meta_boxes ) { ), array( 'name' => __( 'Maximum Earnings', 'badgeos' ), - 'desc' => ' '.__( 'Number of times a user can earn this badge (set to 0 for no maximum).', 'badgeos' ), + 'desc' => ' '.__( 'Number of times a user can earn this badge (set to -1 for no maximum).', 'badgeos' ), 'id' => $prefix . 'maximum_earnings', 'type' => 'text_small', 'std' => '1', @@ -140,6 +140,18 @@ function badgeos_custom_metaboxes( array $meta_boxes ) { array( 'name' => __( 'Hidden to User', 'badgeos' ), 'value' => 'hidden', ), ), ), + array( + 'name' => __( 'Allow Attachment for Submission', 'badgeos' ), + 'desc' => ' '.__( 'Yes, allow to display submission attachment.', 'badgeos' ), + 'id' => $prefix . 'all_attachment_submission', + 'type' => 'checkbox', + ), + array( + 'name' => __( 'Allow Attachment for Submission Comment', 'badgeos' ), + 'desc' => ' '.__( 'Yes, allow to display submission comment attachment.', 'badgeos' ), + 'id' => $prefix . 'all_attachment_submission_comment', + 'type' => 'checkbox', + ), ), $prefix, $achievement_types ) ), $achievement_types ); diff --git a/includes/post-types.php b/includes/post-types.php index 8ae65d82..f55e651c 100644 --- a/includes/post-types.php +++ b/includes/post-types.php @@ -225,6 +225,14 @@ function badgeos_register_achievement_type_cpt() { // Determine whether this achievement type should be visible in the menu $show_in_menu = get_post_meta( $achievement_type->ID, '_badgeos_show_in_menu', true ) ? 'badgeos_badgeos' : false; + //filter school admin menu badgeOS start + if(class_exists("BadgeOS_Group_Management") && function_exists('badgeos_get_user_role')){ + $role = badgeos_get_user_role(get_current_user_id()); + if(!empty($role) && ($role =="school_admin" || $role =="author")){ + $show_in_menu=false; + } + } + // Register the post type register_post_type( sanitize_title( substr( strtolower( $achievement_name_singular ), 0, 20 ) ), array( 'labels' => array( diff --git a/includes/rules-engine.php b/includes/rules-engine.php index ef35cfc4..91d052e5 100644 --- a/includes/rules-engine.php +++ b/includes/rules-engine.php @@ -234,12 +234,13 @@ function badgeos_maybe_award_additional_achievements_to_user( $user_id = 0, $ach // Get achievements that can be earned from completing this achievement $dependent_achievements = badgeos_get_dependent_achievements( $achievement_id ); + // See if a user has unlocked all achievements of a given type + badgeos_maybe_trigger_unlock_all( $user_id, $achievement_id ); + // Loop through each dependent achievement and see if it can be awarded foreach ( $dependent_achievements as $achievement ) badgeos_maybe_award_achievement_to_user( $achievement->ID, $user_id ); - // See if a user has unlocked all achievements of a given type - badgeos_maybe_trigger_unlock_all( $user_id, $achievement_id ); } add_action( 'badgeos_award_achievement', 'badgeos_maybe_award_additional_achievements_to_user', 10, 2 ); diff --git a/includes/shortcodes/badgeos_submission.php b/includes/shortcodes/badgeos_submission.php index 5595bd4c..acbb8ccd 100644 --- a/includes/shortcodes/badgeos_submission.php +++ b/includes/shortcodes/badgeos_submission.php @@ -48,13 +48,34 @@ function badgeos_submission_form( $atts = array() ) { if ( isset( $_REQUEST['achievement_id'] ) && $_REQUEST['achievement_id'] == $atts['achievement_id'] ) { // Don't award if this achievement was earned in the past 30 seconds $recently_earned = badgeos_get_user_achievements( array( 'user_id' => get_current_user_id(), 'achievement_id' => $_REQUEST['achievement_id'], 'since' => ( time() - 30 ) ) ); - if ( empty( $recently_earned ) ) { - if ( badgeos_save_submission_data() ) { - $output .= sprintf( '

%s

', __( 'Submission saved successfully.', 'badgeos' ) ); + + global $wp; + if ( empty( $recently_earned )) { + if ( badgeos_save_submission_data() ) { + $recent_submission_id = $_SESSION['new_added_submission_id']; + unset($_SESSION['new_added_submission_id']); + wp_redirect(home_url($wp->request.'?new_submission_id='.$recent_submission_id)); } - } + }else{ + wp_redirect(home_url($wp->request)); + } } + if(!empty($_GET['new_submission_id']) && $new_submission_id = $_GET['new_submission_id']){ + $posts = get_post( absint($new_submission_id) ); + // Checking the type of submission and display the success message based on submission type + if($posts->post_status == 'draft'){ + $output .= sprintf( '

%s

', __( 'Thank you! Your draft has been saved.', 'badgeos' ) ); + }else { + if (get_post_meta($atts['achievement_id'], '_badgeos_earned_by', true) == 'submission') { + + if(get_post_meta($new_submission_id , '_badgeos_submission_status', true) == 'pending') + $output .= sprintf('

%s

', __('Thank you! Your submission has been received.', 'badgeos')); + + } + } + } + // If user has already submitted something, show their submissions if ( badgeos_check_if_user_has_submission( get_current_user_id(), $atts['achievement_id'] ) ) { $output .= badgeos_get_user_submissions( '', $atts['achievement_id'] ); diff --git a/includes/submission-actions.php b/includes/submission-actions.php index a3c24e6c..9af5ca6f 100644 --- a/includes/submission-actions.php +++ b/includes/submission-actions.php @@ -192,7 +192,8 @@ function badgeos_submission_column_action( $column = '' ) { case 'content': - echo substr( $post->post_content, 0, 250 ) .'...'; + $content = substr( $post->post_content, 0, 250 ) .'...'; + echo html_entity_decode( wpautop( $content ), ENT_QUOTES, 'UTF-8' ); break; case 'status': @@ -227,7 +228,7 @@ function badgeos_add_submission_dropdown_filters() { if ( $typenow == 'submission' ) { //array of current status values available $submission_statuses = array( - 'approve' => __( 'Approve', 'badgeos' ), + 'approved' => __( 'Approve', 'badgeos' ), 'denied' => __( 'Deny', 'badgeos' ), 'pending' => __( 'Pending', 'badgeos' ), ); @@ -317,18 +318,21 @@ function badgeos_process_submission_review( $post_id = 0 ) { function badgeos_save_submission_data() { // If form items don't exist, bail. - if ( ! isset( $_POST['badgeos_submission_submit'] ) || ! isset( $_POST['badgeos_submission_content'] ) ) + if ( ( ! isset( $_POST['badgeos_submission_submit'] ) && ! isset($_POST['badgeos_submission_draft']) ) || ! isset( $_POST['badgeos_submission_content'] ) ) return; // Nonce check for security check_admin_referer( 'badgeos_submission_form', 'submit_submission' ); + $action = isset( $_POST['badgeos_submission_submit'] ) ? $_POST['badgeos_submission_submit'] : $_POST['badgeos_submission_draft']; + // Publish the submission return badgeos_create_submission( absint( $_POST['achievement_id'] ), sprintf( '%1$s: %2$s', get_post_type( absint( $_POST['achievement_id'] ) ), get_the_title( absint( $_POST['achievement_id'] ) ) ), esc_textarea( $_POST['badgeos_submission_content'] ), - absint( $_POST['user_id'] ) + absint( $_POST['user_id'] ), + $action ); } /** @@ -340,49 +344,79 @@ function badgeos_save_submission_data() { * @param integer $user_id The user ID * @return boolean Returns true if able to create form */ -function badgeos_create_submission( $achievement_id = 0, $title = '', $content = '', $user_id = 0 ) { +function badgeos_create_submission( $achievement_id = 0, $title = '', $content = '', $user_id = 0 , $action = '' ) { + + global $wpdb; + + $post_status = 'publish'; + + if(!empty($action) && $action == 'Save Draft'){ + $post_status = 'draft'; + } $submission_data = array( 'post_title' => $title, 'post_content' => $content, - 'post_status' => 'publish', + 'post_status' => $post_status, 'post_author' => $user_id, 'post_type' => 'submission', ); - //insert the post into the database - if ( $submission_id = wp_insert_post( $submission_data ) ) { - // save the achievement ID related to the submission - add_post_meta( $submission_id, '_badgeos_submission_achievement_id', $achievement_id ); + $submission_data_draft = ''; - //process attachment upload if a file was submitted - if( ! empty($_FILES['document_file'] ) ) { + //Check exists submission data + if(isset($_POST['post_id']) && ($_POST['post_id'] != 0)){ - if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' ); + $submission_data_draft = get_post(absint( $_POST['post_id'] )); + } - $file = $_FILES['document_file']; - $upload = wp_handle_upload( $file, array( 'test_form' => false ) ); + if(!empty($submission_data_draft)){ - if( ! isset( $upload['error'] ) && isset($upload['file'] ) ) { + $update_submission_data = array( + 'ID' => absint( $_POST['post_id'] ), + 'post_content' => $content, + 'post_status' => $post_status + ); - $filetype = wp_check_filetype( basename( $upload['file'] ), null ); - $title = $file['name']; - $ext = strrchr( $title, '.' ); - $title = ( $ext !== false ) ? substr( $title, 0, -strlen( $ext ) ) : $title; + if($title){ + $update_submission_data['post_title'] = $title; + } - $attachment = array( - 'post_mime_type' => $filetype['type'], - 'post_title' => addslashes( $title ), - 'post_content' => '', - 'post_status' => 'inherit', - 'post_parent' => $submission_id - ); + // Update the submission data into the database + wp_update_post( $update_submission_data ); - $attach_id = wp_insert_attachment( $attachment, $upload['file'] ); + if($post_status == 'publish'){ + // save the achievement ID related to the submission + add_post_meta( absint( $_POST['post_id'] ), '_badgeos_submission_achievement_id', $achievement_id ); + + // Available action for other processes + do_action( 'badgeos_save_submission', absint( $_POST['post_id'] ) ); + + // Submission status workflow + $status_args = array( + 'achievement_id' => $achievement_id, + 'user_id' => $user_id + ); + + $status = 'pending'; + + // Check if submission is auto approved or not + if ( badgeos_is_submission_auto_approved( absint( $_POST['post_id'] ) ) ) { + $status = 'approved'; + $status_args[ 'auto' ] = true; + } + + badgeos_set_submission_status( absint( $_POST['post_id'] ) , $status, $status_args ); - } } + }else if ( $submission_id = wp_insert_post( $submission_data ) ) { + + if($post_status == 'publish'){ + // save the achievement ID related to the submission + add_post_meta( $submission_id, '_badgeos_submission_achievement_id', $achievement_id ); + } + // Available action for other processes do_action( 'badgeos_save_submission', $submission_id ); @@ -403,14 +437,98 @@ function badgeos_create_submission( $achievement_id = 0, $title = '', $content badgeos_set_submission_status( $submission_id, $status, $status_args ); - return true; + } + //Check this submission already has file attachment + $args = array( + 'post_type' => 'attachment', + 'post_parent' => absint( $_POST['post_id'] ), + 'post_status' => 'draft', + 'post_author' => $user_id, + 'suppress_filters' => true + ); - } else { + $draft_file = get_posts( $args ); - return false; + //process attachment upload if a file was submitted + if( ! empty($_FILES['document_file'] ) && !empty($_FILES['document_file']['name'])) { + + if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' ); + + $file = $_FILES['document_file']; + $upload = wp_handle_upload( $file, array( 'test_form' => false ) ); + + if( ! isset( $upload['error'] ) && isset($upload['file'] ) ) { + + $filetype = wp_check_filetype( basename( $upload['file'] ), null ); + $title = $file['name']; + $ext = strrchr( $title, '.' ); + $title = ( $ext !== false ) ? substr( $title, 0, -strlen( $ext ) ) : $title; + + $post_status = ($post_status == 'draft')?$post_status:'inherit'; + + $attachment = array( + 'post_mime_type' => $filetype['type'], + 'post_title' => addslashes( $title ), + 'post_content' => '', + 'post_status' => $post_status, + 'post_parent' => absint( $_POST['post_id'] ) + ); + + if($draft_file){ + + $attachment_file = get_post_meta( $draft_file[0]->ID, '_wp_attached_file', true ); + //Delete file from directory for new file attachment with same submission + $upload_dir = wp_upload_dir(); + $path = $upload_dir['basedir'].'/'.$attachment_file; + unlink($path); + + //Update post meta to exists value + update_attached_file(absint( $draft_file[0]->ID ), $upload['file']); + + //Update post attachment + $attachment['ID'] = absint( $draft_file[0]->ID ); + wp_update_post($attachment); + + + $data_array = array('post_status' => $post_status); + $where = array('ID' => absint( $draft_file[0]->ID )); + $wpdb->update( $wpdb->posts , $data_array, $where ); + + }else{ + + $attachment['post_parent'] = absint($submission_id); + //Insert file attachment to draft submission + $attach_id = wp_insert_attachment( $attachment, $upload['file'] ); + + $data_array = array('post_status' => $post_status); + $where = array('ID' => absint( $attach_id )); + $wpdb->update( $wpdb->posts , $data_array, $where ); } } + }else{ + + if($draft_file){ + + $post_status = ($post_status == 'draft')?$post_status:'inherit'; + + $data_array = array('post_status' => $post_status); + $where = array('ID' => absint( $draft_file[0]->ID )); + $wpdb->update( $wpdb->posts , $data_array, $where ); + } + + } + + if(isset($_POST['post_id']) && ($_POST['post_id'] != 0)){ + $new_submission_id = $_POST['post_id']; + }else{ + $new_submission_id = $submission_id; + } + + $_SESSION['new_added_submission_id'] = $new_submission_id; + + return true; +} /** * Set submission / nominiation status @@ -838,17 +956,71 @@ function badgeos_set_submission_status_nomination_pending( $messages, $args ) { */ function badgeos_get_comment_form( $post_id = 0 ) { + global $post; + if ( ! is_user_logged_in() ) return ''; $defaults = array( - 'heading' => '

' . sprintf( __( 'Comment on Submission (#%1$d):', 'badgeos' ), $post_id ) . '

', + 'heading' => '

' . sprintf( __( 'Comment on Submission:', 'badgeos' )) . '

', 'attachment' => __( 'Attachment:', 'badgeos' ), 'submit' => __( 'Submit Comment', 'badgeos' ), 'toggle' => __( 'Show/Add Comments', 'badgeos' ), ); $language = wp_parse_args( apply_filters( 'badgeos_comment_form_language', $defaults ), $defaults ); + + //check draft comment + $comment_filter = array( + 'type' => 'draft', + 'user_id' => get_current_user_id(), + 'post_id' => absint( $post_id ), + 'orderby' => 'comment_ID', + 'order' => 'DESC', + 'offset' => 1 + ); + + $comments = get_comments($comment_filter); + + $comment_data = null; + + if($comments){ + + foreach($comments as $comment){ + $comment_data = $comment; + } + } + + //Check enable/disable attachment for submission comment + $id = $post->ID; + + if($post->post_type == 'page'){ + + $post_data = get_post($post_id); + + $pos = strpos($post_data->post_title,":"); + $post_type = trim(substr($post_data->post_title,0,$pos)); + $post_title = trim(substr(strstr($post_data->post_title,':'),1)); + + global $wpdb; + + $table = $wpdb->posts; + + $query = "SELECT ID FROM ".$table." WHERE post_title LIKE '$post_title%' AND post_type LIKE '$post_type%' ORDER BY ID LIMIT 1"; + + $id = $wpdb->get_var($query); + + } + + //check attachment in draft comment submission data + $attachment_data = get_attachment_from_draft_submission($comment_data->comment_post_ID, get_current_user_id()); + + $attachment = null; + + if(!empty($attachment_data)){ + $attachment = render_attachment_from_draft_submission($attachment_data); + } + $sub_form = '
'; // comment form heading @@ -856,26 +1028,37 @@ function badgeos_get_comment_form( $post_id = 0 ) { // submission comment $sub_form .= '
'; - $sub_form .= '

'; + $sub_form .= '

'; $sub_form .= '
'; + if(get_post_meta($id, '_badgeos_all_attachment_submission_comment', true)){ // submission file upload $sub_form .= '
'; $sub_form .= '

'; + if($attachment){ + $sub_form .= $attachment; + } $sub_form .= '
'; + } // submit button - $sub_form .= '

'; + $sub_form .= '

+ +

'; // Hidden Fields $sub_form .= wp_nonce_field( 'submit_comment', 'badgeos_comment_nonce', true, false ); $sub_form .= ''; $sub_form .= ''; + if($comments){ + $sub_form .= ''; + } + $sub_form .= '
'; // Toggle button for showing comment form - $sub_form .= ''; + $sub_form .= '' . esc_html( $language['toggle'] ) . ''; return apply_filters( 'badgeos_get_comment_form', $sub_form, $post_id ); @@ -897,41 +1080,141 @@ function badgeos_save_comment_data() { if ( ! wp_verify_nonce( $_POST['badgeos_comment_nonce'], 'submit_comment' ) ) return; + global $wpdb; + + //Check comment type + $comment_type = ''; + $comment_approved = 1; //Approve + + if(isset($_POST['badgeos_comment_draft'])){ + + $comment_type = 'draft'; + $comment_approved = 0; //Unapprove + + } + // Process comment data $comment_data = array( 'user_id' => absint( $_POST['user_id'] ), 'comment_post_ID' => absint( $_POST['submission_id'] ), 'comment_content' => esc_textarea( $_POST['badgeos_comment'] ), + 'comment_type' => $comment_type, + 'comment_approved' => $comment_approved ); - if ( $comment_id = wp_insert_comment( $comment_data ) ) { + $comment_count = ''; + + //Check exists comment data + if(isset($_POST['comment_id']) && ($_POST['comment_id'] != 0)){ + + $comment_filter = array( + 'ID' => absint( $_POST['comment_id'] ) + ); + + $comment_count = get_comments($comment_filter); + } + + if(!empty($comment_count)){ + + //update comment data + $where = array( 'comment_ID' => absint( $_POST['comment_id'] ) ); - // Process attachment upload if a file was submitted - if( ! empty($_FILES['document_file'] ) ) { + $wpdb->update( $wpdb->comments, $comment_data, $where ); + + }else if( $comment_id = wp_insert_comment( $comment_data ) ){ + } + + //Check this submission already has file attachment + $args = array( + 'post_type' => 'attachment', + 'post_parent' => absint( $_POST['submission_id'] ), + 'post_status' => 'draft', + 'post_author' => absint( $_POST['user_id'] ), + 'suppress_filters' => true + ); + + $draft_file = get_posts( $args ); + + //process attachment upload if a file was submitted + if( ! empty($_FILES['document_file'] ) && !empty($_FILES['document_file']['name'])) { if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' ); $file = $_FILES['document_file']; $upload = wp_handle_upload( $file, array( 'test_form' => false ) ); - if( ! isset( $upload['error'] ) && isset( $upload['file'] ) ) { + if( ! isset( $upload['error'] ) && isset($upload['file'] ) ) { + + $filetype = wp_check_filetype( basename( $upload['file'] ), null ); + $title = $file['name']; + $ext = strrchr( $title, '.' ); + $title = ( $ext !== false ) ? substr( $title, 0, -strlen( $ext ) ) : $title; - $filetype = wp_check_filetype( basename( $upload['file'] ), null ); - $title = $file['name']; - $ext = strrchr( $title, '.' ); - $title = ( $ext !== false ) ? substr( $title, 0, -strlen( $ext ) ) : $title; + $comment_type = ($comment_type == 'draft')?$comment_type:'inherit'; $attachment = array( 'post_mime_type' => $filetype['type'], 'post_title' => addslashes( $title ), 'post_content' => '', - 'post_status' => 'inherit', + 'post_status' => 'draft', + /*'post_status' => ! isset ($comment_type) ? 'inherit' : $comment_type ,*/ 'post_parent' => absint( $_REQUEST['submission_id'] ), 'post_author' => absint( $_REQUEST['user_id'] ) ); - wp_insert_attachment( $attachment, $upload['file'] ); + + if(!empty($draft_file) && !empty($comment_count)){ + + $attachment_file = get_post_meta( $draft_file[0]->ID, '_wp_attached_file', true ); + + //Delete file from directory for new file attachment with same submission + $upload_dir = wp_upload_dir(); + $path = $upload_dir['basedir'].'/'.$attachment_file; + unlink($path); + + //Update post meta to exists value + update_attached_file(absint( $draft_file[0]->ID ), $upload['file']); + + + //Update post attachment + $attachment['ID'] = absint( $draft_file[0]->ID ); + wp_update_post($attachment); + + $data_array = array('post_status' => $comment_type); + $where = array('ID' => absint( $draft_file[0]->ID )); + $wpdb->update( $wpdb->posts , $data_array, $where ); + + }else{ + //Insert file attachment to draft submission + $attachment_id = wp_insert_attachment( $attachment, $upload['file'] ); + + $data_array = array('post_status' => $comment_type); + $where = array('ID' => absint( $attachment_id )); + $wpdb->update( $wpdb->posts , $data_array, $where ); + } } + }else{ + + if($draft_file){ + + $comment_type = ($comment_type == 'draft')?$comment_type:'inherit'; + + $data_array = array('post_status' => $comment_type); + $where = array('ID' => absint( $draft_file[0]->ID )); + $wpdb->update( $wpdb->posts , $data_array, $where ); + } + + } + + if ( isset( $_POST['badgeos_comment_nonce'] ) ) { + global $wp; + if(empty($wp->request)){ + $url = array_filter(explode('/', $_SERVER['REQUEST_URI'])); + $url = $url[count($url)-1].'/'.$url[count($url)]; + }else{ + $url = $wp->request; + } + wp_redirect( home_url($url) ); exit; } } add_action( 'init', 'badgeos_save_comment_data' ); @@ -950,6 +1233,7 @@ function badgeos_get_comments_for_submission( $submission_id = 0 ) { 'post_id' => absint( $submission_id ), 'orderby' => 'date', 'order' => 'ASC', + 'type__not_in' => 'draft' ) ); // If we have no comments, bail @@ -1137,7 +1421,7 @@ function badgeos_get_nomination_form( $args = array() ) { // nomination content $sub_form .= ''; $sub_form .= '
'; - $sub_form .= '

'; + $sub_form .= '

'; $sub_form .= '
'; // submit button $sub_form .= '

'; @@ -1160,7 +1444,7 @@ function badgeos_get_submission_form( $args = array() ) { // Setup our defaults $defaults = array( - 'heading' => sprintf( '

%s

', __( 'Create a New Submission', 'badgeos' ) ), + 'heading' => sprintf( '

%s

', __( 'Submission', 'badgeos' ) ), 'attachment' => __( 'Attachment:', 'badgeos' ), 'submit' => __( 'Submit', 'badgeos' ) ); @@ -1175,23 +1459,51 @@ function badgeos_get_submission_form( $args = array() ) { // Merge our defaults with the passed args $args = wp_parse_args( $args, $defaults ); + + //check draft submission data + $submission_data = get_submission_data_from_draft(); + //end check - draft submission data + + //check attachment in draft submission data + $attachment_data = get_attachment_from_draft_submission($submission_data->ID, $user_ID); + + $attachment = null; + + if(!empty($attachment_data)){ + $attachment = render_attachment_from_draft_submission($attachment_data); + } + $sub_form = '
'; // submission form heading $sub_form .= ''. $args['heading'] .''; + + // submission comment + $sub_form .= '
'; + $sub_form .= '

'; + $sub_form .= '
'; + + if(get_post_meta($post->ID, '_badgeos_all_attachment_submission', true)){ // submission file upload $sub_form .= '
'; $sub_form .= '

'; + if($attachment){ + $sub_form .= $attachment; + } $sub_form .= '
'; - // submission comment - $sub_form .= '
'; - $sub_form .= '

'; - $sub_form .= '
'; + } // submit button - $sub_form .= '

'; + $sub_form .= '

+ +

'; // hidden fields $sub_form .= wp_nonce_field( 'badgeos_submission_form', 'submit_submission', true, false ); $sub_form .= ''; $sub_form .= ''; + + if($submission_data){ + $sub_form .= ''; + } + $sub_form .= '
'; return apply_filters( 'badgeos_get_submission_form', $sub_form ); @@ -1425,3 +1737,85 @@ function badgeos_get_submission_attachments( $submission_id = 0 ) { // Return out filterable output return apply_filters( 'badgeos_get_submission_attachments', $output, $submission_id, $attachments ); } + +/** + * Get the data saved from the draft + * @return array + */ +function get_submission_data_from_draft(){ + + global $post, $user_ID; + + global $wpdb; + + $post_title = sprintf( '%1$s: %2$s', get_post_type( absint( $post->ID ) ), get_the_title( absint( $post->ID ) ) ); + + $table = $wpdb->posts; + + $query = "SELECT ID FROM ".$table." WHERE post_status='draft' AND post_type='submission' AND post_author = $user_ID AND post_title LIKE '%$post_title%' ORDER BY ID DESC LIMIT 1"; + + $post_id = $wpdb->get_var($query); + + $submission_data = array(); + + if ( $post_id ){ + $submission_data = get_post($post_id); + } + + return $submission_data; + +} + +/** + * Get the data from the submission form to be saved as draft + * @param $post_id + * @param $user_ID + * @return array + */ +function get_attachment_from_draft_submission($post_id, $user_ID){ + + global $wpdb; + + $table = $wpdb->posts; + + $query = "SELECT ID FROM ".$table." WHERE post_status='draft' AND post_type='attachment' AND post_author = $user_ID AND post_parent = ".$post_id." ORDER BY ID DESC LIMIT 1"; + + $attachment_post_id = $wpdb->get_var($query); + + $attachment_data = array(); + + if ( $attachment_post_id ){ + $attachment_data = get_post($attachment_post_id); + } + + return $attachment_data; +} + +/** + * Function to render the attachment that is saved in draft + * @param array $attachment + * @return null|string + */ +function render_attachment_from_draft_submission($attachment = array()){ + + $sub_form = null; + + if(!empty($attachment)){ + + $userdata = get_userdata( $attachment->post_author ); + $display_name = is_object( $userdata ) ? $userdata->display_name : ''; + + // Concatenate the markup + $sub_form .= '
' . sprintf( __( 'Submitted Draft Attachment:', 'badgeos' )) . '
'; + $sub_form .= sprintf( __( '%1$s - uploaded %2$s by %3$s', 'badgeos' ), + wp_get_attachment_link( $attachment->ID, 'full', false, null, $attachment->post_title ), + get_the_time( 'F j, Y g:i a', $attachment ), + $display_name + ); + } + + return $sub_form; +} + + + diff --git a/includes/widgets/earned-user-achievements-widget.php b/includes/widgets/earned-user-achievements-widget.php index 75d6abcd..b7ea6cb8 100644 --- a/includes/widgets/earned-user-achievements-widget.php +++ b/includes/widgets/earned-user-achievements-widget.php @@ -33,7 +33,7 @@ function form( $instance ) { //hide the step CPT if ( $achievement['single_name'] == 'step' ) { - continue; + continue; } //if achievement displaying exists in the saved array it is enabled for display @@ -109,7 +109,7 @@ function widget( $args, $instance ) { $permalink = get_permalink( $achievement->ID ); $title = get_the_title( $achievement->ID ); $img = badgeos_get_achievement_post_thumbnail( $achievement->ID, array( 50, 50 ), 'wp-post-image' ); - $thumb = $img ? '' . $img .'' : ''; + $thumb = $img ? '' . $img .'' : ''; $class = 'widget-badgeos-item-title'; $item_class = $thumb ? ' has-thumb' : ''; @@ -126,7 +126,7 @@ function widget( $args, $instance ) { $thecount++; if ( $thecount == $number_to_show && $number_to_show != 0 ) { - break; + break; } } diff --git a/js/badgeos-achievements.js b/js/badgeos-achievements.js index b95bf402..85e2a0c0 100644 --- a/js/badgeos-achievements.js +++ b/js/badgeos-achievements.js @@ -12,11 +12,17 @@ jQuery( function( $ ) { function badgeos_hide_submission_comments( submissions_wrapper ) { submissions_wrapper.find( '.badgeos-submission-comments-wrap' ).hide(); submissions_wrapper.find( '.badgeos-comment-form' ).hide(); - submissions_wrapper.find( '.submission-comment-toggle' ).show(); + //submissions_wrapper.find( '.submission-comment-toggle' ).show(); } // Hide comment form on feedback posts with toggle var $submissions_wrapper = $('.badgeos-feedback-container'); + if($('div.badgeos-feedback-container .badgeos-submissions').length){ + $submissions_wrapper = $('.badgeos-feedback-container'); + }else{ + $submissions_wrapper = $('.badgeos-submissions'); + } + badgeos_hide_submission_comments( $submissions_wrapper ); $submissions_wrapper.on( 'click', '.submission-comment-toggle', function( event ) { event.preventDefault(); @@ -25,7 +31,12 @@ jQuery( function( $ ) { $button.siblings( '.badgeos-submission-comments-wrap' ).fadeIn('fast'); $button.siblings( '.badgeos-comment-form' ).fadeIn('fast'); $button.hide(); - } ); + + var value = $button.siblings('.badgeos-comment-form').find('.badgeos_comment').val(); + if(value){ + alert('Comment is saved in draft mode.'); + } + }); // Get feedback posts function badgeos_get_feedback() { @@ -84,6 +95,14 @@ jQuery( function( $ ) { $( '.badgeos-feedback-' + $button.data( 'feedback-id' ) + ' .badgeos-feedback-status' ).html( response.data.status ); $( '.cmb_id__badgeos_submission_current td, .cmb_id__badgeos_nomination_current td' ).html( response.data.status ); $( '.badgeos-comment-date-by' ).html( 'Status: '+ response.data.status ); + + var feed_back_id = $button.data( 'feedback-id' ); + + if(response.data.status != 'Approved'){ + $('.comment-toggle-'+feed_back_id+'').show(); + }else{ + $('.comment-toggle-'+feed_back_id+'').hide(); + } } } ); } ); diff --git a/readme.txt b/readme.txt index 8e4aeaec..f9a7111c 100644 --- a/readme.txt +++ b/readme.txt @@ -1,5 +1,5 @@ === BadgeOS === -Contributors: badgeos, learningtimes, credly +Contributors: badgeos, learningtimes, credly, asahitechnologies Donate link: http://badgeos.org/contribute/donate/ Tags: badge, badges, openbadges, learningtimes, credly, OBI, mozilla, open badges, achievement, award, reward, engagement, submission, nomination, API, open credit, credit Requires at least: 3.6