Created
September 23, 2018 06:21
-
-
Save wpmudev-sls/c37112707bb3003ffb0f12dfe2ff1fbf to your computer and use it in GitHub Desktop.
[CoursePress] - Enrollment Expire - Course Duration
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] - Enrollment Expire | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Withdtaws students from course after a certain period has passed since enrollement date | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_CP_Course_Duration' ) ) { | |
class WPMUDEV_CP_Course_Duration { | |
private static $_instance = null; | |
private $post_type; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_CP_Course_Duration(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
if ( ! class_exists( 'CoursePress_Data_Course' ) ) { | |
return; | |
} | |
$this->post_type = CoursePress_Data_Course::get_post_type_name(); | |
// Admin | |
add_filter( "coursepress_allowed_meta_boxes", array( $this, 'allow_meta_box' ), 20 ); | |
add_action( "add_meta_boxes_{$this->post_type}", array( $this, 'add_meta_box' ), 10 ); | |
add_action( "save_post_{$this->post_type}", array( $this, 'save_meta' ), 10, 2 ); | |
add_action( "admin_footer", array( $this, 'js' ) ); | |
//Front | |
add_filter( "coursepress_template_dashboard_page", array( $this, 'dashboard_template' ), 10, 3 ); | |
add_shortcode( "course_list_ext", array( $this, 'course_list' ) ); | |
// Set cron with custom url. Needs to be set only once | |
// Set the cron by visiting : http://site.com/wp-admin/tools.php?_cp_schedule_enrollment_expire=yes | |
add_action( "load-tools.php", array( $this, 'register_cron' ) ); | |
add_action( "_cp_schedule_enrollment_expire", array( $this, 'scheduled_expiration_check' ) ); | |
// Shortcode | |
add_shortcode( 'cp_enrollment_expire', array( $this, 'shortcode' ) ); | |
} | |
/** | |
* Add our meta_box to the allowed Course meta_boxes | |
*/ | |
public function allow_meta_box( $allowed ) { | |
$allowed[] = 'cp_course_duration'; | |
return $allowed; | |
} | |
public function add_meta_box() { | |
add_meta_box( | |
'cp_course_duration', | |
__( 'Course duration' ), | |
array( $this, 'meta_box_content' ), | |
$this->post_type, | |
'side', | |
'low' | |
); | |
} | |
public function meta_box_content( $post ) { | |
$duration = get_post_meta( $post->ID, '_wpmudev_cp_course_duration', true ); | |
$unit = isset( $duration['unit'] ) ? $duration['unit'] : ''; | |
$amount = isset( $duration['amount'] ) ? $duration['amount'] : ''; | |
?> | |
<h3><?php _e( 'Course duration after enrollment' ); ?></h3> | |
<em><?php _e( 'Leave the Duration field to 0 so it won\'t add any time limit' ) ?></em> | |
<p> | |
<label><?php _e( 'Duration unit' ); ?></label> | |
<select | |
id="_wpmudev_cp_course_duration_unit"> | |
<option value="d" <?php selected( $unit, 'd' ); ?> ><?php _e( 'Days' ); ?></option> | |
<option value="m" <?php selected( $unit, 'm' ); ?> ><?php _e( 'Months' ); ?></option> | |
</select> | |
</p> | |
<p> | |
<label><?php _e( 'Duration amount' ); ?></label> | |
<select | |
id="_wpmudev_cp_course_duration_amount"> | |
<?php for ( $a=0; $a <= 50; $a++ ): ?> | |
<option value="<?php echo $a; ?>" <?php selected( $amount, $a ); ?> ><?php echo $a; ?></option> | |
<?php endfor; ?> | |
</select> | |
</p> | |
<?php | |
} | |
public function js() { | |
if ( | |
! function_exists( 'get_current_screen' ) || | |
'course' != get_current_screen()->id || | |
( isset( $_GET['tab'] ) && 'setup' != $_GET['tab'] ) | |
) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
( function( $ ) { | |
var _wpmudev_cp_course_duration_saved = false; | |
var _wpmudev_cp_getUrlParameter = function getUrlParameter( sParam, url ) { | |
if ( url.indexOf( '?' ) === -1 ) { | |
return false; | |
} | |
var sURLAtts = url.split( '?' ), | |
sURLVariables = sURLAtts[1].split( '&' ), | |
sParameterName, | |
i; | |
for ( i = 0; i < sURLVariables.length; i++ ) { | |
sParameterName = sURLVariables[i].split( '=' ); | |
if (sParameterName[0] === sParam) { | |
return sParameterName[1] === undefined ? true : sParameterName[1]; | |
} | |
} | |
}; | |
$.ajaxSetup( { | |
beforeSend: function( jqXHR, settings ) { | |
if ( typeof settings.url == "undefined" || 'update_course' != _wpmudev_cp_getUrlParameter( 'action', settings.url ) ) { | |
return true; | |
} | |
if ( _wpmudev_cp_course_duration_saved ) { | |
return true; | |
} | |
_wpmudev_cp_course_duration_saved = true; | |
var data = JSON.parse( settings.data ), | |
duration_unit = $( '#_wpmudev_cp_course_duration_unit' ).val(), | |
duration_amount = $( '#_wpmudev_cp_course_duration_amount' ).val(); | |
data.data["_wpmudev_cp_course_duration_unit"] = duration_unit; | |
data.data["_wpmudev_cp_course_duration_amount"] = duration_amount; | |
settings.data = JSON.stringify( data ); | |
return true; | |
} | |
}); | |
$( document ).ready(function(){ | |
$( '#_wpmudev_cp_course_duration_unit' ).on( 'change', function(){ _wpmudev_cp_course_duration_saved = false; } ); | |
$( '#_wpmudev_cp_course_duration_amount' ).on( 'change', function(){ _wpmudev_cp_course_duration_saved = false; } ); | |
}); | |
} ) ( jQuery ); | |
</script> | |
<?php | |
} | |
public function save_meta( $post_id, $post ) { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return $post_id; | |
} | |
$_data = json_decode( file_get_contents( 'php://input' ) ); | |
$data = isset( $_data->data ) ? $_data->data : false; | |
if ( ! $data ) { | |
return; | |
} | |
$unit = ( isset( $data->_wpmudev_cp_course_duration_unit ) && in_array( $data->_wpmudev_cp_course_duration_unit, array('d','m') ) ) ? $data->_wpmudev_cp_course_duration_unit : false; | |
$amount = isset( $data->_wpmudev_cp_course_duration_amount ) ? (int) $data->_wpmudev_cp_course_duration_amount : false; | |
// If not set, or soemthing went wrong, better do nothing | |
if ( ! $unit || ! $amount ) { | |
return; | |
} | |
$duration = array( | |
'unit' => $unit, | |
'amount' => $amount | |
); | |
update_post_meta( $post_id, '_wpmudev_cp_course_duration', $duration ); | |
} | |
public function dashboard_template( $template, $user_id, $a ) { | |
$template = '<div class="coursepress-dashboard-wrapper"> | |
[course_list instructor="%1$s" dashboard="true"] | |
[course_list facilitator="%1$s" dashboard="true"] | |
[course_list_ext student="%1$s" dashboard="true" current_label="%2$s" show_labels="true"] | |
</div>'; | |
$template = sprintf( $template, $user_id, __( 'Enrolled Courses', 'coursepress' ) ); | |
return $template; | |
} | |
public function course_list( $atts, $content ) { | |
CoursePress_Core::$is_cp_page = true; | |
$atts = CoursePress_Helper_Utility::sanitize_recursive( | |
shortcode_atts( | |
array( | |
'completed_label' => __( 'Completed courses', 'coursepress' ), | |
'context' => 'all', // <blank>, enrolled, completed | |
'current_label' => __( 'Current Courses', 'coursepress' ), | |
'dashboard' => false, | |
'facilitator_label' => __( 'Facilitated Courses', 'coursepress' ), | |
'facilitator' => '', | |
'future_label' => __( 'Starting soon', 'coursepress' ), | |
'incomplete_label' => __( 'Incomplete courses', 'coursepress' ), | |
'instructor_msg' => __( 'The Instructor does not have any courses assigned yet.', 'coursepress' ), | |
'instructor' => '', // Note, one or the other | |
'limit' => - 1, | |
'manage_label' => __( 'Manage Courses', 'coursepress' ), | |
'order' => 'ASC', | |
'orderby' => 'meta', /// possible values: meta, title | |
'past_label' => __( 'Past courses', 'coursepress' ), | |
'show_labels' => false, | |
'status' => 'publish', | |
'student_msg' => sprintf( __( 'You are not enrolled in any courses. <a href="%s">See available courses.</a>', 'coursepress' ), CoursePress_Core::get_slug( 'course/', true ) ), | |
'student' => '', // If both student and instructor is specified only student will be used | |
'suggested_label' => __( 'Suggested courses', 'coursepress' ), | |
'suggested_msg' => __( 'You are not enrolled in any courses.<br />Here are a few you might like, or <a href="%s">see all available courses.</a>', 'coursepress' ), | |
'show_withdraw_link' => false, | |
'categories' => '', | |
), | |
$atts, | |
'course_page' | |
) | |
); | |
$instructor_list = false; | |
$student_list = false; | |
$atts['dashboard'] = cp_is_true( $atts['dashboard'] ); | |
$courses = array(); | |
$content = ''; | |
$student = 0; | |
$include_ids = array(); | |
/** | |
* Sanitize show_withdraw_link | |
*/ | |
if ( empty( $atts['student'] ) || 'incomplete' != $atts['status'] ) { | |
$atts['show_withdraw_link'] = false; | |
} | |
if ( ! empty( $atts['instructor'] ) ) { | |
$include_ids = array(); | |
$instructors = explode( ',', $atts['instructor'] ); | |
if ( ! empty( $instructors ) ) { | |
foreach ( $instructors as $ins ) { | |
$ins = (int) $ins; | |
if ( $ins ) { | |
$course_ids = CoursePress_Data_Instructor::get_assigned_courses_ids( $ins, $atts['status'] ); | |
if ( $course_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $course_ids ) ); | |
} | |
} | |
} | |
} else { | |
$instructor = (int) $atts['instructor']; | |
if ( $instructor ) { | |
$course_ids = CoursePress_Data_Instructor::get_assigned_courses_ids( $instructor, $atts['status'] ); | |
if ( $course_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $course_ids ) ); | |
} | |
} | |
} | |
$instructor_list = true; | |
if ( empty( $include_ids ) ) { return ''; } | |
} | |
if ( ! empty( $atts['facilitator'] ) ) { | |
$facilitator = $atts['facilitator']; | |
$atts['context'] = 'facilitator'; | |
$include_ids = CoursePress_Data_Facilitator::get_facilitated_courses( $facilitator, 'publish', true ); | |
if ( empty( $include_ids ) ) { | |
return ''; | |
} | |
} | |
if ( ! empty( $atts['student'] ) ) { | |
$include_ids = array(); | |
$students = explode( ',', $atts['student'] ); | |
foreach ( $students as $student ) { | |
$student = (int) $student; | |
if ( $student ) { | |
$courses_ids = array(); | |
$courses_to_add = CoursePress_Data_Student::get_enrolled_courses_ids( $student ); | |
if ( isset( $atts['status'] ) ) { | |
foreach ( $courses_to_add as $course_id ) { | |
$status = get_post_status( $course_id ); | |
if ( 'publish' != $status ) { | |
continue; | |
} | |
$add = true; | |
if ( 'publish' != $atts['status'] ) { | |
$status = CoursePress_Data_Student::get_course_status( $course_id, $student, false ); | |
if ( 'completed' == $atts['status'] ) { | |
$add = false; | |
if ( 'certified' == $status ) { | |
$add = true; | |
} | |
} else { | |
if ( 'certified' == $status ) { | |
$add = false; | |
} | |
} | |
} | |
if ( $add ) { | |
$courses_ids[] = $course_id; | |
} | |
} | |
} else { | |
$courses_ids = $courses_to_add; | |
} | |
if ( $courses_ids ) { | |
$include_ids = array_unique( array_merge( $include_ids, $courses_ids ) ); | |
} | |
} | |
} | |
$student_list = true; | |
} | |
$post_args = array( | |
'order' => $atts['order'], | |
'post_type' => CoursePress_Data_Course::get_post_type_name(), | |
'post_status' => $atts['status'], | |
'posts_per_page' => (int) $atts['limit'], | |
'suppress_filters' => true, | |
'meta_key' => 'cp_course_start_date', | |
'orderby' => 'meta_value_num', | |
); | |
/** | |
* categories | |
*/ | |
if ( ! empty( $atts['categories'] ) ) { | |
$post_args['tax_query'] = array( | |
array( | |
'taxonomy' => CoursePress_Data_Course::get_post_category_name(), | |
'field' => 'slug', | |
'terms' => preg_split( '/[, ]+/', $atts['categories'] ), | |
), | |
); | |
} | |
$test_empty_courses_ids = false; | |
switch ( $atts['context'] ) { | |
case 'enrolled': | |
$test_empty_courses_ids = true; | |
$user_id = get_current_user_id(); | |
$include_ids = CoursePress_Data_Student::get_enrolled_courses_ids( $user_id ); | |
break; | |
case 'incomplete': | |
$test_empty_courses_ids = true; | |
$user_id = get_current_user_id(); | |
$ids = CoursePress_Data_Student::get_enrolled_courses_ids( $user_id ); | |
foreach ( $ids as $course_id ) { | |
$status = CoursePress_Data_Student::get_course_status( $course_id, $user_id, false ); | |
if ( 'certified' != $status ) { | |
$include_ids[] = $course_id; | |
} | |
} | |
break; | |
case 'completed': | |
$test_empty_courses_ids = true; | |
$user_id = get_current_user_id(); | |
$ids = CoursePress_Data_Student::get_enrolled_courses_ids( $user_id ); | |
foreach ( $ids as $course_id ) { | |
$status = CoursePress_Data_Student::get_course_status( $course_id, $user_id, false ); | |
if ( 'certified' == $status ) { | |
$include_ids[] = $course_id; | |
} | |
} | |
break; | |
case 'future': | |
unset( $post_args['meta_key'] ); | |
$post_args['meta_query'] = array( | |
array( | |
'key' => 'cp_course_start_date', | |
'value' => time(), | |
'type' => 'NUMERIC', | |
'compare' => '>', | |
), | |
); | |
break; | |
case 'past': | |
unset( $post_args['meta_key'] ); | |
$post_args['meta_query'] = array( | |
'relation' => 'AND', | |
array( | |
'key' => 'cp_course_end_date', | |
'compare' => 'EXISTS', | |
), | |
array( | |
'key' => 'cp_course_end_date', | |
'value' => 0, | |
'type' => 'NUMERIC', | |
'compare' => '>', | |
), | |
array( | |
'key' => 'cp_course_end_date', | |
'value' => time(), | |
'type' => 'NUMERIC', | |
'compare' => '<', | |
), | |
); | |
break; | |
case 'manage': | |
$user_id = get_current_user_id(); | |
$test_empty_courses_ids = true; | |
if ( CoursePress_Data_Capabilities::can_manage_courses( $user_id ) ) { | |
$local_args = array( | |
'post_type' => CoursePress_Data_Course::get_post_type_name(), | |
'nopaging' => true, | |
'fields' => 'ids', | |
); | |
$include_ids = get_posts( $local_args ); | |
} else { | |
$include_ids = CoursePress_Data_Instructor::get_assigned_courses_ids( $user_id ); | |
if ( empty( $include_ids ) ) { | |
$include_ids = CoursePress_Data_Facilitator::get_facilitated_courses( $user_id, array( 'all' ), true, 0, -1 ); | |
} | |
} | |
break; | |
case 'all': | |
$atts['orderby'] = strtolower( $atts['orderby'] ); | |
switch ( $atts['orderby'] ) { | |
case 'title': | |
case 'post_title': | |
$post_args['orderby'] = 'title'; | |
break; | |
default: | |
$post_args['orderby'] = 'meta_value_num'; | |
break; | |
} | |
break; | |
} | |
if ( $test_empty_courses_ids && empty( $include_ids ) ) { | |
/** | |
* do nothing if we have empty list | |
*/ | |
$courses = array(); | |
} else if ( ( ( $student_list || $instructor_list ) && ! empty( $include_ids ) ) || ( ! $student_list && ! $instructor_list ) ) { | |
if ( ! empty( $include_ids ) ) { | |
$post_args = wp_parse_args( array( 'post__in' => $include_ids ), $post_args ); | |
} | |
$courses = get_posts( $post_args ); | |
} | |
$counter = 0; | |
if ( ! $atts['dashboard'] ) { | |
foreach ( $courses as $course ) { | |
$shortcode_attributes = array( | |
'course_id' => $course->ID, | |
'show_withdraw_link' => $atts['show_withdraw_link'], | |
); | |
$shortcode_attributes = CoursePress_Helper_Utility::convert_array_to_params( $shortcode_attributes ); | |
$content .= do_shortcode( '[course_list_box ' . $shortcode_attributes . ']' ); | |
$counter += 1; | |
} | |
} else { | |
if ( $student_list ) { | |
$my_courses = CoursePress_Data_Student::my_courses( $student, $courses ); | |
$context = $atts['context']; | |
if ( isset( $my_courses[ $context ] ) ) { | |
$courses = $my_courses[ $context ]; | |
} | |
$courses = array_filter( $courses ); | |
if ( empty( $courses ) ) { | |
if ( $atts['dashboard'] ) { | |
$content .= sprintf( '<p class="message">%s</p>', esc_html__( 'You are not enrolled to any course.', 'coursepress' ) ); | |
} | |
} else { | |
$counter += count( $courses ); | |
$content .= $this->course_list_table( $courses ); | |
} | |
} else { | |
foreach ( $courses as $course ) { | |
$course_url = get_edit_post_link( $course->ID ); | |
$content .= do_shortcode( '[course_list_box course_id="' . $course->ID . '" override_button_text="' . esc_attr__( 'Manage Course', 'coursepress' ) . '" override_button_link="' . esc_url( $course_url ) . '"]' ); | |
$counter += 1; | |
} | |
} | |
} | |
$context = $atts['dashboard'] && $instructor_list ? 'manage' : $atts['context']; | |
if ( ( $atts['dashboard'] && ! empty( $counter ) ) || ! empty( $atts['show_labels'] ) ) { | |
$label = ''; | |
$show_empty = false; | |
switch ( $context ) { | |
case 'enrolled': | |
case 'current': | |
case 'all': | |
$label = $atts['current_label']; | |
if ( 0 == $counter ) { | |
$show_empty = true; | |
$content = sprintf( | |
'<p class="message">%s</p>', | |
sprintf( | |
$atts['student_msg'], | |
esc_attr( '/'.CoursePress_Core::get_setting( 'slugs/course', 'courses' ) ) | |
) | |
); | |
} | |
break; | |
case 'future': | |
$label = $atts['future_label']; | |
break; | |
case 'incomplete': | |
$label = $atts['incomplete_label']; | |
break; | |
case 'completed': | |
$label = $atts['completed_label']; | |
break; | |
case 'past': | |
$label = $atts['past_label']; | |
break; | |
case 'manage': | |
$label = $atts['manage_label']; | |
break; | |
case 'facilitator': | |
$label = $atts['facilitator_label']; | |
break; | |
} | |
if ( $counter || ( 0 === $counter && $show_empty ) ) { | |
$content = '<div class="dashboard-course-list ' . esc_attr( $context ) . '">' . | |
'<h3 class="section-title">' . esc_html( $label ) . '</h3>' . | |
$content . | |
'</div>'; | |
} | |
} elseif ( $atts['dashboard'] && 'enrolled' === $context ) { | |
$label = $atts['suggested_label']; | |
$message = sprintf( $atts['suggested_msg'], esc_url( CoursePress_Core::get_slug( 'courses', true ) ) ); | |
$content = '<div class="dashboard-course-list suggested">' . | |
'<h3 class="section-title">' . esc_html( $label ) . '</h3>' . | |
'<p>' . $message . '</p>' . | |
do_shortcode( '[course_random featured_title="" media_type="image" media_priority="image"]' ) . | |
'</div>'; | |
} | |
return $content; | |
} | |
public function course_list_table( $courses = array() ) { | |
if ( ! is_array( $courses ) || empty( $courses ) ) { | |
return ''; | |
} | |
$content = ''; | |
$student_id = get_current_user_id(); | |
$courses = array_filter( $courses ); | |
if ( ! empty( $courses ) ) { | |
$date_format = get_option( 'date_format' ); | |
$time_format = get_option( 'time_format' ); | |
$table_header = ''; | |
$table_body = ''; | |
$certificated = CoursePress_Data_Certificate::is_enabled(); | |
$table_columns = array( | |
'name' => __( 'Course', 'coursepress' ), | |
'date_enrolled' => __( 'Date Enrolled', 'coursepress' ), | |
'date_expire' => __( 'Expiration date', 'coursepress' ), | |
'average' => __( 'Average', 'coursepress' ), | |
'status' => __( 'Status', 'coursepress' ), | |
); | |
if ( $certificated ) { | |
$table_columns['certificate'] = __( 'Certificate', 'coursepress' ); | |
} | |
foreach ( $table_columns as $column => $column_label ) { | |
$table_header .= sprintf( '<th class="column-%s">%s</th>', $column, $column_label ); | |
} | |
$table_header .= '<th> </th>'; | |
$column_keys = array_keys( $table_columns ); | |
foreach ( $courses as $course ) { | |
$course_url = CoursePress_Data_Course::get_course_url( $course->ID ); | |
$completion_status = CoursePress_Data_Student::get_course_status( $course->ID, $student_id ); | |
$course_completed = CoursePress_Data_Student::is_course_complete( $student_id, $course->ID ); | |
$table_body .= '<tr>'; | |
foreach ( $column_keys as $column_key ) { | |
switch ( $column_key ) { | |
case 'name': | |
$table_body .= sprintf( '<td><a href="%s">%s</a></td>', esc_url( $course_url ), $course->post_title ); | |
break; | |
case 'date_enrolled': | |
$date_enrolled = get_user_meta( $student_id, 'enrolled_course_date_' . $course->ID ); | |
if ( is_array( $date_enrolled ) ) { | |
$date_enrolled = array_pop( $date_enrolled ); | |
} | |
if ( empty( $date_enrolled ) ) { | |
$date_enrolled = sprintf( | |
'<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>', | |
__( 'Unknown enrolled date.', 'coursepress' ) | |
); | |
} else { | |
$date_enrolled = date_i18n( $date_format, CoursePress_Data_Course::strtotime( $date_enrolled ) ); | |
} | |
$table_body .= sprintf( '<td>%s</td>', $date_enrolled ); | |
break; | |
case 'date_expire': | |
$expiration_date = $this->get_course_expiration_date( $course->ID, $student_id ); | |
$table_body .= sprintf( '<td>%s</td>', $expiration_date ); | |
break; | |
case 'average': | |
$statuses = array( 'Ongoing', 'Awaiting Review' ); | |
if ( in_array( $completion_status, $statuses ) ) { | |
$average = '—'; | |
} else { | |
$average = CoursePress_Data_Student::average_course_responses( $student_id, $course->ID ); | |
$average .= '%'; | |
} | |
$table_body .= sprintf( '<td>%s</td>', $average ); | |
break; | |
case 'status': | |
$table_body .= sprintf( '<td class="column-status">%s</td>', $completion_status ); | |
break; | |
case 'certificate': | |
$download_certificate = __( 'Not available', 'coursepress' ); | |
if ( $course_completed ) { | |
$certificate_link = CoursePress_Data_Certificate::get_encoded_url( $course->ID, $student_id ); | |
$download_certificate = sprintf( '<a href="%s" class="button-primary">%s</a>', $certificate_link, __( 'Download', 'coursepress' ) ); | |
} | |
$table_body .= sprintf( '<td>%s</td>', $download_certificate ); | |
break; | |
} | |
} | |
// Row actions | |
$row_actions = array(); | |
$allow_workbook = CoursePress_Data_Course::get_setting( $course->ID, 'allow_workbook' ); | |
if ( $allow_workbook ) { | |
$workbook_url = CoursePress_Data_Student::get_workbook_url( $course->ID ); | |
$workbook_link = sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( $workbook_url ), __( 'Workbook', 'coursepress' ) ); | |
$row_actions['workbook'] = $workbook_link; | |
} | |
$withdraw_link = add_query_arg( array( | |
'_wpnonce' => wp_create_nonce( 'coursepress_student_withdraw' ), | |
'course_id' => $course->ID, | |
'student_id' => $student_id, | |
) ); | |
$withdraw_link = sprintf( '<a href="%s" class="cp-withdraw-student">%s</a>', esc_url( $withdraw_link ), __( 'Withdraw', 'coursepress' ) ); | |
$row_actions['withdraw'] = $withdraw_link; | |
$table_body .= sprintf( '<td class="row-actions">%s</td>', implode( ' | ', $row_actions ) ); | |
$table_body .= '</tr>'; | |
} | |
$table_format = '<table class="cp-dashboard-table"><thead><tr>%s</tr></thead><tbody>%s</tbody></table>'; | |
$content .= sprintf( $table_format, $table_header, $table_body ); | |
} | |
return $content; | |
} | |
public function get_course_expiration_date( $course_id = null, $student_id = null, $format = null ) { | |
if ( is_null( $course_id ) || ! is_numeric( $course_id ) ) { | |
return false; | |
} | |
if ( is_null( $student_id ) ) { | |
$student_id = get_current_user_id(); | |
} | |
$duration = get_post_meta( $course_id, '_wpmudev_cp_course_duration', true ); | |
if ( ! $duration || ! is_array( $duration ) ) { | |
return false; | |
} | |
$date_enrolled = new DateTime( get_user_meta( $student_id, 'enrolled_course_date_' . $course_id )[0] ); | |
$current_date = new DateTime( date( 'Y-m-d' ) ); | |
$diff = $current_date->diff( $date_enrolled ); | |
$unit = $duration['unit']; | |
$amount = $duration['amount']; | |
$unit = 'd' == $unit ? 'D' : 'M'; | |
$expiration_date = $date_enrolled->add( new DateInterval( "P{$amount}{$unit}" ) ); | |
if ( is_null( $format ) ) { | |
$date_format = get_option('date_format'); | |
$time_format = get_option('time_format'); | |
$format = "{$date_format} {$time_format}"; | |
} | |
return $expiration_date->format( $format ); | |
} | |
public function register_cron() { | |
if ( ! isset( $_GET['_cp_schedule_enrollment_expire'] ) || 'yes' != $_GET['_cp_schedule_enrollment_expire'] ) { | |
return; | |
} | |
if (! wp_next_scheduled ( '_cp_schedule_enrollment_expire' )) { | |
wp_schedule_event( time(), 'daily', '_cp_schedule_enrollment_expire' ); | |
} | |
} | |
public function scheduled_expiration_check() { | |
$courses = $this->courses(); | |
foreach ( $courses as $course ) { | |
$student_ids = CoursePress_Data_Course::get_student_ids( $course->ID ); | |
foreach ( $student_ids as $student_id ) { | |
$this->maybe_expire_course( $course->ID, $student_id ); | |
} | |
} | |
} | |
public function maybe_expire_course( $course_id = null, $student_id = null ) { | |
if ( is_null( $course_id ) || is_null( $student_id ) ) { | |
return; | |
} | |
$duration = get_post_meta( $course_id, '_wpmudev_cp_course_duration', true ); | |
if ( ! $duration || ! is_array( $duration ) ) { | |
return false; | |
} | |
$unit = $duration['unit']; | |
$amount = $duration['amount']; | |
$date_enrolled = new DateTime( get_user_meta( ( int )$student_id, 'enrolled_course_date_' . ( int )$course_id )[0] ); | |
$current_date = new DateTime( date( 'Y-m-d' ) ); | |
$diff = $current_date->diff( $date_enrolled ); | |
$diff_amount = 0; | |
if ( 'm' == $unit ) { | |
$diff_amount = ( $diff->format('%y') * 12 ) + $diff->format('%m'); | |
} | |
else { | |
$diff_amount = $diff->format( '%d' ); | |
} | |
if ( $diff_amount >= $amount ) { | |
CoursePress_Data_Course::withdraw_student( $student_id, $course_id ); | |
} | |
} | |
public function courses() { | |
$post_args = array( | |
'post_type' => CoursePress_Data_Course::get_post_type_name(), | |
'post_status' => 'publish', | |
'posts_per_page' => -1 | |
); | |
$courses = new WP_Query( $post_args ); | |
return $courses->posts; | |
} | |
public function shortcode( $atts, $content ) { | |
global $post; | |
$atts = shortcode_atts( | |
array( | |
'course_id' => ( $post instanceof WP_Post && $this->post_type == $post->post_type ) ? $post->ID : false, | |
'student_id' => is_user_logged_in() ? get_current_user_id() : false, | |
'format' => null | |
), $atts, 'cp_course_duration_sc_atts' ); | |
if ( ! $atts['course_id'] || ! $atts['student_id'] ){ | |
return false; | |
} | |
return $this->get_course_expiration_date( $atts['course_id'], $atts['student_id'], $atts['format'] ); | |
} | |
} | |
if ( ! function_exists( 'wpmudev_cp_course_duration' ) ) { | |
function wpmudev_cp_course_duration(){ | |
return WPMUDEV_CP_Course_Duration::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_cp_course_duration', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment