<?php

/**
 * edumodo learndash course ribbon text
 */
if(!function_exists('edumodo_learndash_course_ribbon_text')){
	function edumodo_learndash_course_ribbon_text($course_id){

		$user_id   = get_current_user_id();
		$options = get_option('sfwd_cpt_options');
		$course_options = get_post_meta($course_id, "_sfwd-courses", true);
		$currency = null;
		$class       = '';
		$ribbon_text = '';
		/**
		 * currency
		 */
		if ( ! is_null( $options ) ) {
			if ( isset($options['modules'] ) && isset( $options['modules']['sfwd-courses_options'] ) && isset( $options['modules']['sfwd-courses_options']['sfwd-courses_paypal_currency'] ) )
				$currency = $options['modules']['sfwd-courses_options']['sfwd-courses_paypal_currency'];
		}
		if( is_null( $currency ) ){
			$currency = 'USD';
		}
		/**
		 * price
		 */
		$price = $course_options && isset($course_options['sfwd-courses_course_price']) ? $course_options['sfwd-courses_course_price'] : __( 'Free', 'edumodo' );
		if( $price == '' ){
			$price .= __( 'Free', 'edumodo' );
		}
		if ( is_numeric( $price ) ) {
			if ( $currency == "USD" ){
				$price = learndash_30_get_currency_symbol()	 . $price;
			}else{
				$price .= ' ' . $currency;
			}
		}
		/**
		 * access control
		 */
		$has_access   = sfwd_lms_has_access( $course_id, $user_id );
		$is_completed = learndash_course_completed( $user_id, $course_id );

		if ( $has_access && ! $is_completed ) {
			$class = 'ld_course_grid_price ribbon-enrolled';
			$ribbon_text = __( 'Enrolled', 'edumodo' );
		} elseif ( $has_access && $is_completed ) {
			$class = 'ld_course_grid_price';
			$ribbon_text = __( 'Completed', 'edumodo' );
		} else {
			$class = ! empty( $course_options['sfwd-courses_course_price'] ) ? 'ld_course_grid_price price_' . $currency : 'ld_course_grid_price free';
			$ribbon_text = $price;
		}

		return $ribbon_text;

	}
}

/**
 * Display related courses for sidebar
 */
if (!function_exists('edumodo_learndash_related_courses')) {
	function edumodo_learndash_related_courses($post_id='', $course_count=3) { 

		$button_text  = get_post_meta( $post_id, '_learndash_course_grid_custom_button_text', true );
		$button_text = isset( $button_text ) && ! empty( $button_text ) ? $button_text : __( 'See more...', 'edumodo' );
		$button_text = apply_filters( 'learndash_course_grid_custom_button_text', $button_text, $post_id );

		$tags = wp_get_post_terms($post_id, 'post_tag'); ?>

		<aside id="secondary" class="widget-area">
			<div class="widget ld-related-course-widget">
				<div class="leaarndash-releted-course-two">
					<h3 class="widget-title rl-course-title ld"><?php esc_html_e( 'Related Courses', 'edumodo' ); ?> </h3>
					<div class="lp-related-course">

						<?php 
						$tag_ids = array();
						foreach ( $tags as $individual_tag ) array_push($tag_ids,$individual_tag->term_id);
						// need to fix query
						$query_args_arr = array(
							'post_type'        => 'sfwd-courses',
							'tax_query' => array( 
								array( 'taxonomy' => 'post_tag', 
									'field' => 'id',
									'terms' => $tag_ids
								)
							),
							'post__not_in'     => array( $post_id ),
							'posts_per_page'   => $course_count,
						);
						$course_query = new WP_Query( $query_args_arr );

						if ( $course_query->have_posts() ) {
							while ( $course_query->have_posts() ) {
								$course_query->the_post(); ?>       

								<div class="single-course">
									<div class="ld-course-thumbnail">
										<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
											<?php the_post_thumbnail('small'); ?>
										</a>
									</div>
									<div class="content-body">
										<a href="<?php the_permalink()?>"> 
											<h4 class="title"><?php the_title(); ?></h4> 
										</a>
									</div>
									<div class="course-footer">
										<div class="lp-enroll"><?php echo edumodo_learndash_course_ribbon_text(get_the_ID()); ?></div>
									</div>
								</div>
							<?php }
						}
						wp_reset_query(); ?>

					</div> 
				</div>
			</div>
		</aside>
		

	<?php }
}

/**
 * Display related courses for sidebar
 */
if (!function_exists('edumodo_learndash_related_courses_grid')) {
	function edumodo_learndash_related_courses_grid($post_id='', $course_column='3', $course_count=3) { ?>

		<?php 
		$button_text  = get_post_meta( $post_id, '_learndash_course_grid_custom_button_text', true );
		$button_text = isset( $button_text ) && ! empty( $button_text ) ? $button_text : __( 'See more...', 'edumodo' );
		$button_text = apply_filters( 'learndash_course_grid_custom_button_text', $button_text, $post_id );

		$tags = wp_get_post_terms($post_id, 'post_tag');?>

		<div class="leaarndash-releted-course">
			<h2 class="rl-course-title ld"><?php esc_html_e( 'Related Courses', 'edumodo' ); ?> </h2>
			<div class="row">
				<div class="lp-related-course clearfix">

					<?php 
					$tag_ids = array();
					foreach ( $tags as $individual_tag ) array_push($tag_ids,$individual_tag->term_id);

					$args2 = array(
						'post_type'        => 'sfwd-courses',
						'tax_query' => array( 
							array( 'taxonomy' => 'post_tag', 
								'field' => 'id',
								'terms' => $tag_ids
							)
						),
						'post__not_in'     => array( $post_id ),
						'posts_per_page'   => $course_count, // Number of related posts that will be shown.

					);
					$my_query = new WP_Query( $args2 );

					if ( $my_query->have_posts() ) {
						while ( $my_query->have_posts() ) {
							$my_query->the_post(); ?>       

							<div class="course-content-wrapper col-md-<?php echo 12/$course_column; ?>">
								<article class="lp-course-1">

									<figure class="lp-course-thumbnail">
										<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
											<?php the_post_thumbnail('small'); ?>
										</a>
									</figure>

									<div class="course-content-body">

										<header class="entry-header">
											<h4 class="course-entry-title">
												<a href="<?php the_permalink()?>">
													<?php the_title(); ?>
												</a>
											</h4>       
										</header>

										<footer class="lp-entry-footer">
											<div class="footer-body">
												<span class="lp-enroll">
													<?php echo edumodo_learndash_course_ribbon_text(get_the_ID()); ?>
												</span>
											</div>
										</footer>

									</div>
								</article>
							</div>
						<?php }
					}
					wp_reset_query(); ?>

				</div> 
			</div>
		</div>

	<?php }
}
