Created
December 6, 2018 18:56
-
-
Save wpmudev-sls/1d1a105bbc8bd1e4a9b11451c69cca18 to your computer and use it in GitHub Desktop.
[CoursePress] - Completion Date Column. Adds a new column for completion date in Assessments page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [CoursePress] - Completion Date Column | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Adds a new column for completion date in Assessments page | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_CP_Completion_Date_Col' ) ) { | |
class WPMUDEV_CP_Completion_Date_Col { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_CP_Completion_Date_Col(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'admin_footer', array( $this, 'js' ) ); | |
add_action( 'wp_ajax_wpmudev_cp_course_completion_date', array( $this, 'course_completion_date_ajax' ) ); | |
} | |
public function course_completion_date_ajax() { | |
check_ajax_referer( 'wpmudev_cp_course_completion_date', 'security' ); | |
$course_id = isset( $_POST['course_id'] ) ? (int) $_POST['course_id'] : false; | |
$student_id = isset( $_POST['student_id'] ) ? (int) $_POST['student_id'] : false; | |
if ( ! $course_id || ! $student_id ) { | |
$return = array( | |
'success' => false, | |
'content' => 'Something is missing' | |
); | |
wp_send_json($return); | |
} | |
$completion_date = $this->get_course_completion_date( $course_id, $student_id ); | |
if ( ! $completion_date ) { | |
$return = array( | |
'success' => true, | |
'content' => '-' | |
); | |
wp_send_json($return); | |
} | |
$completion_date = date( | |
get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), | |
strtotime( $completion_date ) | |
); | |
$return = array( | |
'success' => true, | |
'content' => $completion_date | |
); | |
wp_send_json($return); | |
} | |
public function get_course_completion_date( $course_id, $student_id ) { | |
$completed = CoursePress_Data_Student::is_course_complete( $student_id, $course_id ); | |
if ( ! $completed ) { | |
return false; | |
} | |
$certificate_id = CoursePress_Data_Certificate::get_certificate_id( $student_id, $course_id ); | |
return get_post_field( 'post_date', $certificate_id ); | |
} | |
public function js() { | |
if ( ! is_callable( 'get_current_screen' ) || 'course_page_coursepress_assessments' != get_current_screen()->id ) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
($ => { | |
var WPMUDEV_CP_Comp_Date_Col = { | |
load : function() { | |
$( document ).ajaxComplete(function( event, xhr, settings ) { | |
var action = WPMUDEV_CP_Comp_Date_Col.get_action( 'action', settings.data ); | |
var response = JSON.parse(xhr.responseText); | |
if ( | |
typeof response != "undefined" && | |
typeof response.data != "undefined" && | |
typeof response.data.action != "undefined" && | |
'table' == response.data.action ) { | |
const table = $( '#assessment-table-container table.users' ) | |
course_id = $( '#course-list' ).val(); | |
table.find('tr').each(function(){ | |
$(this).find('th').eq(2).after('<th>Completion date</th>'); | |
let row_id = $(this).attr('id'); | |
if ( typeof row_id != "undefined" ) { | |
let _row_id = row_id.split( '-' ), | |
student = _row_id[1] | |
el_class = 'course-completion-date-' + course_id + '-' + student; | |
$(this).find( '.row-actions .id' ).hide(); | |
$(this).find('td').eq(2).after('<td class="' + el_class + '"></td>' ); | |
WPMUDEV_CP_Comp_Date_Col.get_completion_date( course_id, student, el_class ); | |
} | |
}); | |
} | |
}); | |
}, | |
get_completion_date( course_id, student, el_class ) { | |
var data = { | |
action : 'wpmudev_cp_course_completion_date', | |
security : '<?php echo wp_create_nonce( "wpmudev_cp_course_completion_date" ); ?>', | |
student_id : student, | |
course_id : course_id | |
}; | |
var completion_date = ''; | |
$.post(ajaxurl, data, function(response) { | |
if( response.success ){ | |
$( '.' + el_class ).html( response.content ); | |
} | |
else{ | |
console.log( 'Something went wrong' ); | |
} | |
}); | |
}, | |
get_action: function(name, url){ | |
if (!url){ | |
return ''; | |
} | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return null; | |
if (!results[2]) return ''; | |
return decodeURIComponent(results[2].replace(/\+/g, " ")); | |
} | |
} | |
$( window ).load( () => { | |
WPMUDEV_CP_Comp_Date_Col.load(); | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
} | |
} | |
if ( ! function_exists( 'wpmudev_cp_completion_date_col' ) ) { | |
function wpmudev_cp_completion_date_col(){ | |
return WPMUDEV_CP_Completion_Date_Col::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_cp_completion_date_col', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment