Skip to content

Commit

Permalink
Merge pull request #648 from themeum/shewa-v2
Browse files Browse the repository at this point in the history
Utility Function Added for The Course Entry Box Buttons
  • Loading branch information
harunollyo authored Jan 11, 2024
2 parents e54a85f + 0677c49 commit 0209687
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
84 changes: 84 additions & 0 deletions includes/tutor-general-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Tutor\Cache\FlashMessage;
use TUTOR\Input;
use Tutor\Models\CourseModel;

if ( ! defined( 'ABSPATH' ) ) {
exit;
Expand Down Expand Up @@ -1177,3 +1178,86 @@ function tutor_getallheaders() {
return $headers;
}
}

if ( ! function_exists( 'tutor_entry_box_buttons' ) ) {
/**
* Tutor conditional buttons for the enrollment box
*
* @since 2.6.0
*
* @param int $course_id course id.
* @param int $user_id user id.
*
* @return object
*/
function tutor_entry_box_buttons( int $course_id = 0, int $user_id = 0 ) {
$conditional_buttons = (object) array(
'show_enroll_btn' => false,
'show_add_to_cart_btn' => false,
'show_start_learning_btn' => false,
'show_continue_learning_btn' => false,
'show_complete_course_btn' => false,
'show_retake_course_btn' => false,
'show_certificate_view_btn' => false,
'show_course_fully_booked_btn' => false,
);

$course_id = tutor_utils()->get_post_id( $course_id );
$user_id = tutor_utils()->get_user_id( $user_id );

$has_course_access = tutor_utils()->has_user_course_content_access( $user_id, $course_id );

$is_public_course = get_post_meta( $course_id, '_tutor_is_public_course', true );

$is_enabled_retake = tutor_utils()->get_option( 'course_retake_feature' );

$is_enrolled = tutor_utils()->is_enrolled( $course_id, $user_id );

if ( 'yes' === $is_public_course ) {
$conditional_buttons->show_start_learning_btn = true;
} else {
// Admin & instructor can manage posts.
if ( $is_enrolled || $has_course_access ) {
$can_complete_course = CourseModel::can_complete_course( $course_id, $user_id );
$is_completed_course = tutor_utils()->is_completed_course( $course_id, $user_id );
$course_progress = (int) tutor_utils()->get_course_completed_percent( $course_id, $user_id );

if ( $course_progress > 0 || $course_progress < 100) {
$conditional_buttons->show_continue_learning_btn = true;
}

if ( $course_progress === 0 ) {
$conditional_buttons->show_start_learning_btn = true;
}

if ( $can_complete_course ) {
$conditional_buttons->show_complete_course_btn = true;
}

if ( $is_completed_course ) {
$conditional_buttons->show_certificate_view_btn = true;
}

if ( $is_enabled_retake && $is_completed_course ) {
$conditional_buttons->show_retake_course_btn = true;
}
} else {
$is_paid_course = tutor_utils()->is_course_purchasable( $course_id );
if ( $is_paid_course ) {
$conditional_buttons->show_add_to_cart_btn = true;
} else {
$conditional_buttons->show_enroll_btn = true;
}
}
}

if ( ! $is_public_course && ! ( $is_enrolled || $has_course_access ) ) {
$is_fully_booked = tutor_utils()->is_course_fully_booked( $course_id );
if ( $is_fully_booked ) {
$conditional_buttons->show_course_fully_booked_btn = true;
}
}

return apply_filters( 'tutor_enrollment_buttons', $conditional_buttons );
}
}
4 changes: 0 additions & 4 deletions models/CourseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,6 @@ public static function get_paid_courses( string $meta_key, array $args = array()
* @return boolean
*/
public static function can_complete_course( $course_id, $user_id ) {
if ( ! tutor_utils()->is_enrolled( $course_id, $user_id ) ) {
return false;
}

$mode = tutor_utils()->get_option( 'course_completion_process' );
if ( self::MODE_FLEXIBLE === $mode ) {
return true;
Expand Down

0 comments on commit 0209687

Please sign in to comment.