/** * Check if a user has completed a course or not * * @param int | WP_Post | WP_Comment $course course_id or sensei_course_status entry * * @param int $user_id * @return boolean */ public static function user_completed_course($course, $user_id = 0) { if ($course) { if (is_object($course) && is_a($course, 'WP_Comment')) { $user_course_status = $course->comment_approved; } elseif (!is_numeric($course) && !is_a($course, 'WP_Post')) { $user_course_status = $course; } else { if (!$user_id) { $user_id = get_current_user_id(); } if (is_a($course, 'WP_Post')) { $course = $course->ID; } $user_course_status = Sensei_Utils::user_course_status($course, $user_id); if (isset($user_course_status->comment_approved)) { $user_course_status = $user_course_status->comment_approved; } } if ($user_course_status && 'complete' == $user_course_status) { return true; } } return false; }
/** * Activate single course if already purchases * @return void */ public function activate_purchased_single_course() { global $post, $current_user; if (Sensei_WC::is_woocommerce_active()) { if (!is_user_logged_in()) { return; } if (!isset($post->ID)) { return; } $user_id = $current_user->ID; $course_id = $post->ID; $course_product_id = (int) get_post_meta($course_id, '_course_woocommerce_product', true); if (!$course_product_id) { return; } $user_course_status = Sensei_Utils::user_course_status(intval($course_id), $user_id); // Ignore course if already completed if (Sensei_Utils::user_completed_course($user_course_status)) { return; } // Ignore course if already started if ($user_course_status) { return; } // Get all user's orders $order_args = array('post_type' => 'shop_order', 'posts_per_page' => -1, 'post_status' => array('wc-processing', 'wc-completed'), 'meta_query' => array(array('key' => '_customer_user', 'value' => $user_id)), 'fields' => 'ids'); $orders = get_posts($order_args); foreach ($orders as $order_post_id) { // Get course product IDs from order $order = new WC_Order($order_post_id); $items = $order->get_items(); foreach ($items as $item) { $product = wc_get_product($item['product_id']); // handle product bundles if (is_object($product) && $product->is_type('bundle')) { $bundled_product = new WC_Product_Bundle($product->id); $bundled_items = $bundled_product->get_bundled_items(); foreach ($bundled_items as $bundled_item) { if ($bundled_item->product_id == $course_product_id) { Sensei_Utils::user_start_course($user_id, $course_id); return; } } } else { // handle regular products if ($item['product_id'] == $course_product_id) { Sensei_Utils::user_start_course($user_id, $course_id); return; } } } } } }
/** * Output the course actions like start taking course, register, add to cart etc. * * @since 1.9.0 */ public static function the_course_enrolment_actions() { ?> <section class="course-meta course-enrolment"> <?php global $post, $current_user; $is_user_taking_course = Sensei_Utils::user_started_course($post->ID, $current_user->ID); if (is_user_logged_in() && !$is_user_taking_course) { // Get the product ID $wc_post_id = absint(get_post_meta($post->ID, '_course_woocommerce_product', true)); // Check for woocommerce if (Sensei_WC::is_woocommerce_active() && 0 < intval($wc_post_id)) { sensei_wc_add_to_cart($post->ID); } else { sensei_start_course_form($post->ID); } // End If Statement } elseif (is_user_logged_in()) { // Check if course is completed $user_course_status = Sensei_Utils::user_course_status($post->ID, $current_user->ID); $completed_course = Sensei_Utils::user_completed_course($user_course_status); // Success message if ($completed_course) { ?> <div class="status completed"><?php _e('Completed', 'woothemes-sensei'); ?> </div> <?php $has_quizzes = Sensei()->course->course_quizzes($post->ID, true); if (has_filter('sensei_results_links') || $has_quizzes) { ?> <p class="sensei-results-links"> <?php $results_link = ''; if ($has_quizzes) { $results_link = '<a class="view-results" href="' . Sensei()->course_results->get_permalink($post->ID) . '">' . __('View results', 'woothemes-sensei') . '</a>'; } /** * Filter documented in Sensei_Course::the_course_action_buttons */ $results_link = apply_filters('sensei_results_links', $results_link, $post->ID); echo $results_link; ?> </p> <?php } ?> <?php } else { ?> <div class="status in-progress"><?php echo __('In Progress', 'woothemes-sensei'); ?> </div> <?php } } else { // Get the product ID $wc_post_id = absint(get_post_meta($post->ID, '_course_woocommerce_product', true)); // Check for woocommerce if (Sensei_WC::is_woocommerce_active() && 0 < intval($wc_post_id)) { sensei_wc_add_to_cart($post->ID); } else { if (get_option('users_can_register')) { $my_courses_page_id = ''; /** * Filter to force Sensei to output the default WordPress user * registration link. * * @since 1.9.0 * @param bool $wp_register_link default false */ $wp_register_link = apply_filters('sensei_use_wp_register_link', false); $settings = Sensei()->settings->get_settings(); if (isset($settings['my_course_page']) && 0 < intval($settings['my_course_page'])) { $my_courses_page_id = $settings['my_course_page']; } // If a My Courses page was set in Settings, and 'sensei_use_wp_register_link' // is false, link to My Courses. If not, link to default WordPress registration page. if (!empty($my_courses_page_id) && $my_courses_page_id && !$wp_register_link) { $my_courses_url = get_permalink($my_courses_page_id); $register_link = '<a href="' . $my_courses_url . '">' . __('Register', 'woothemes-sensei') . '</a>'; echo '<div class="status register">' . $register_link . '</div>'; } else { wp_register('<div class="status register">', '</div>'); } } // end if user can register } // End If Statement } // End If Statement ?> </section><?php }
/** * Compare the user's subscriptions end date with the date * the user was added to the course. If the user was added after * the subscription ended they were manually added and this will return * true. * * Important to note that all subscriptions for the user is compared. * * @since 1.9.0 * * @param $user_id * @param $product_id * @param $course_id * * @return bool */ public static function was_user_added_without_subscription($user_id, $product_id, $course_id) { $course_start_date = ''; $subscription_start_date = ''; $is_a_subscription = ''; $was_user_added_without_subscription = true; // if user is not on the course they were not added if (!Sensei_Utils::user_started_course($course_id, $user_id)) { return false; } // if user doesn't have a subscription and is taking the course // they were added manually if (!wcs_user_has_subscription($user_id, $product_id) && Sensei_Utils::user_started_course($course_id, get_current_user_id())) { return true; } $course_status = Sensei_Utils::user_course_status($course_id, $user_id); // comparing dates setup data $course_start_date = date_create($course_status->comment_date); $subscriptions = wcs_get_users_subscriptions($user_id); // comparing every subscription foreach ($subscriptions as $subscription) { // for the following statuses we know the user was not added // manually $status = $subscription->get_status(); if (in_array($status, array('pending-canceled', 'active', 'on-hold', 'pending'))) { continue; } $current_subscription_start_date = date_create($subscription->modified_date); // is the last updated subscription date newer than course start date if ($current_subscription_start_date > $course_start_date) { return false; } } return $was_user_added_without_subscription; }