<?php 
if(!is_admin() && $GLOBALS['pagenow'] !== 'post.php'){	
	add_shortcode('DAPCourseProgressBar', 'DAPCourseProgressBarShortcode');
}
function DAPCourseProgressBarShortcode($atts, $content = null) {
	if(isset($_REQUEST['tve']) && $_REQUEST['tve'] == true){return;}
	
	extract(shortcode_atts(array(
	'id' => '',
	), $atts));
	
	$loging_status = false;
	
	if(Dap_Session::isLoggedIn()){ 
		$loging_status = true;
		
	}
	
	$siteurl = SITE_URL_DAP;
	$remove = array("http:","https:");
    $siteurl =  str_replace($remove,"",$siteurl);
	$css_link =  '<link rel="stylesheet" href="'.$siteurl.'/dap/admin/templates/courseprogressbar/template1.css" type="text/css" media="all">'; 
	$template_new_html = '';
	if($loging_status){
		$session = Dap_Session::getSession();
		$user = $session->getUser();
		$userId = $user->getId();
		
		$obj = Dap_OnlineCourse::loadById($id);
		$html  = '';
		
		if(isset($obj)){
			
			
			$pb_c_id = $obj->getCourseId();
			$pb_c_ids = explode(',',$pb_c_id);
			foreach($pb_c_ids as $pb_c_id){
				$template_html  = stripslashes($obj->getHtml());
					
				$pb_product_info = Dap_Product::loadProduct($pb_c_id);
				$user_obj = new Dap_User();
				$user_obj->setId($userId);
				$hasAccess = $user_obj->hasAccessTo($pb_c_id);
				
				if(!$hasAccess){
					continue;
				}
				
				$cname = '';
				if(isset($pb_product_info)){
					$cname =  $pb_product_info->getName();
				}
				$product_structure = $pb_product_info->getProductStructure(); 
				 $pro_all_courses_ids = array();
				if($product_structure == "module_lesson"){ 
					$moduledata = DAP_CourseModule::loadByCourseId($pb_c_id) ;
					$pro_all_courses_total = 0;
					foreach($moduledata as $mod){
						$lessoncounts  =  Dap_Product::displayFileResourcesWithModuleIdCourseId($pb_c_id, $mod->getId());
						
						//echo "<pre>"; print_r($lessoncounts); 
						foreach($lessoncounts as $lessoncount){
							$pro_all_courses_ids[] =  $lessoncount['id'];
							$pro_all_courses_total += 1;	
						}
					}
				}else if($product_structure == "lesson"){
					
					$pro_all_courses_obj = DAP_Product::displayFileResourcesCount($pb_c_id);
					
					foreach($pro_all_courses_obj as $pro_all_courses_ob){
						$pro_all_courses_ids[] =  $pro_all_courses_ob['id'];
					}
					
					$pro_all_courses_total = count($pro_all_courses_obj);						
				}
				

				$CP_objs_new = DAP_UserCourseProgress::getCourseProgressDetailsByUserIdAndProductId($userId,$pb_c_id);
				//applied if data is in progress table, but deleted from resource table
				$CP_objs = array();
				foreach($CP_objs_new as $CP_obj){
					$unit_id =  $CP_obj['unit_id'];
					if(!in_array($unit_id,$pro_all_courses_ids)){
						continue;
					}
					$CP_objs[] = $CP_obj;
				}
				//echo "<pre>"; print_r($CP_objs); 
				$user_courses_complete = count($CP_objs);			
				 
				$total_complete_course_percentage = ($user_courses_complete*100)/$pro_all_courses_total;
				$user_total_complete_course = (int)$total_complete_course_percentage;
				$total_complete_course_percentage = number_format((float)$total_complete_course_percentage, 2, '.', '');
				
				$pb_customize_data = $obj->getCustomizeData();
				$pb_customize_data = explode('|||',$pb_customize_data);
				
				$pb_display_cn = '';
				if(isset($pb_customize_data[0])){
					$pb_display_cn = $pb_customize_data[0];
				}
				$class = "pb_wrapper_".$id."_".$pb_c_id;  
				$template_html = "<div class='".$class."'>".$template_html."</div>";
				$template_html = str_replace('%%COURSENAME%%',$cname,$template_html);
				$template_html = str_replace('%%PERCENT%%',$user_total_complete_course.'%',$template_html);
				$template_html = str_replace('contenteditable="true"','contenteditable="false"',$template_html);
				
				if($pb_display_cn == 'no'){
					$template_html .=  "<style> .".$class." #pb_customize_template .course_name_text{display:none;}</style>";
				} 
				
				$template_html .=  "<style> .".$class." #pb_customize_template .porgress_bar_green{width:".$user_total_complete_course."%;}</style>";
				$template_new_html = $template_new_html.$template_html;
				
			}// foreach loop closed 
			
			
			
		}	
		
	}else{
		
		//$template_html = "Please login";
		
	}
	
	$html  .= "<div class='dap_course_progress_bar_wrapper'>";
	$html  .= $css_link;
	$html  .= $template_new_html;
	$html  .= "</div>";
	
	return $html; 
	
}	


