<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('DbIndeedAbstract')) require_once ULP_PATH . 'classes/Abstracts/DbIndeedAbstract.class.php';
if (class_exists('DbUserEntitiesRelations')) return;
class DbUserEntitiesRelations extends DbIndeedAbstract{
	/**
	 * @var string
	 */
	protected $table = '';
	/**
	 * @param none
	 * @return none
	 */
	public function __construct(){
		global $wpdb;
		$this->table = $wpdb->prefix . 'ulp_user_entities_relations';
	}
	/**
	 * @param int (user id)
	 * @param int (entity id)
	 * @return mixed
	 */
	public function getRelationId($uid=0, $entity_id=0){
		$uid = esc_sql($uid);
		$entity_id = esc_sql($entity_id);
		return $this->getVar('id', "user_id=$uid AND entity_id=$entity_id");
	}
	/**
	 * @param int
	 * @param int
	 * @param int
	 * @param string
	 * @param string
	 * @param int
	 * @return int
	 */
	public function saveRelation($uid=0, $entity_id=0, $entity_type=0, $start_time='', $end_time='', $status=0){
		global $wpdb;
		$uid = esc_sql($uid);
		$entity_id = esc_sql($entity_id);
		$entity_type = esc_sql($entity_type);
		$start_time = esc_sql($start_time);
		$end_time = esc_sql($end_time);
		$status = esc_sql($status);
		if ($id = $this->getRelationId($uid, $entity_id)){
			/// update
			$q = "entity_type='$entity_type', status=$status";
			if ($start_time!=''){
				$q .= ", start_time='$start_time'";
			}
			if ($end_time!=''){
				$q .= ", end_time='$end_time'";
			}
			$this->update($q, "id=$id");
			return $id;
		} else {
			/// save
			//$this->insert("null, $uid, $entity_id, '$entity_type', '$start_time', '$end_time', $status");
			//return $wpdb->insert_id;
			return $this->do_Insert($uid, $entity_id, $entity_type, $start_time, $end_time, $status);
		}
	}
	/**
	 * @param int
	 * @param int
	 * @param int
	 * @param string
	 * @param string
	 * @param int
	 * @return int
	 */
	public function do_Insert($uid=0, $entity_id=0, $entity_type=0, $start_time='', $end_time='', $status=0){
			global $wpdb;
			$uid = esc_sql($uid);
			$entity_id = esc_sql($entity_id);
			$entity_type = esc_sql($entity_type);
			$start_time = esc_sql($start_time);
			$end_time = esc_sql($end_time);
			$status = esc_sql($status);
			$this->insert("null, $uid, $entity_id, '$entity_type', '$start_time', '$end_time', $status");
			return $wpdb->insert_id;
	}
	/**
	 * @param int (user id)
	 * @param int (entity id)
	 * @return bool
	 */
	public function deleteRelation($uid=0, $entity_id=0){
		$uid = esc_sql($uid);
		$entity_id = esc_sql($entity_id);
		$relaton_id = $this->getRelationId($uid,$entity_id);

		require_once ULP_PATH . 'classes/Db/DbUserEntitiesRelationMetas.class.php';
		$DbUserEntitiesRelationMetas = new DbUserEntitiesRelationMetas();

		$DbUserEntitiesRelationMetas->deleteAllRelation($relaton_id);

		return $this->delete("user_id=$uid AND entity_id=$entity_id");
	}
	/**
	 * @param int (user id)
	 * @param int (course id)
	 * @return bool
	 */
	public function userCanSeeCourse($uid=0, $course_id=0){
		/// do not forget to add the status
		$returnValue = false;
		$uid = esc_sql($uid);
		$course_id = esc_sql($course_id);
		$temp = $this->getRow('start_time, end_time', "user_id=$uid AND entity_id=$course_id AND entity_type='ulp_course' ORDER BY id DESC LIMIT 1 "); ///  AND status=1

		if ($temp){
			$now = time();
			if (strtotime($temp['start_time'])<$now && strtotime($temp['end_time'])>$now){
				$returnValue = true;
			}
		}
		$returnValue = apply_filters('ulp_filter_does_user_can_access_course', $returnValue, $uid, $course_id);
		return $returnValue;
	}
	/**
	 * This will select the last relation on the table to see if user can access the course
	 * @param int (user id)
	 * @param int (course id)
	 * @return bool
	 */
	public function isUserEnrolledOnCourse($uid=0, $course_id=0){
		$uid = esc_sql($uid);
		$course_id = esc_sql($course_id);
		$temp = $this->getVar('id', "user_id=$uid AND entity_id=$course_id AND entity_type='ulp_course' ORDER BY id DESC LIMIT 1"); ///  AND status=1
		if ($temp){
			if ($this->userCanSeeCourse($uid, $course_id)) return TRUE;
		}
		return FALSE;
	}
	public function how_many_times_user_enroll_course($uid=0, $course_id=0){
			global $wpdb;
			$uid = esc_sql($uid);
			$course_id = esc_sql($course_id);
			$q = "SELECT IFNULL(COUNT(id), 0) as v FROM {$wpdb->prefix}ulp_user_entities_relations
			 					WHERE user_id=$uid
								AND entity_id=$course_id
								AND entity_type='ulp_course'
			";
			return $wpdb->get_var($q);
	}
	public function get_enroll_date($uid=0, $cid=0){
			global $wpdb;
			$uid = esc_sql($uid);
			$cid = esc_sql($cid);
			$q = "SELECT start_time FROM  {$wpdb->prefix}ulp_user_entities_relations WHERE user_id=$uid AND entity_id=$cid;";
			return $wpdb->get_var($q);
	}
	/**
	 * @param int (user id)
	 * @param int (entity)
	 * @param string (what to select)
	 * @return value
	 */
	public function getRelationColValue($uid=0, $entity_id=0, $col=''){
		$uid = esc_sql($uid);
		$entity_id = esc_sql($entity_id);
		$col = esc_sql($col);
		return $this->getVar("$col", "user_id=$uid AND entity_id=$entity_id");
	}
	/**
	 * @param int (course id)
	 * @return int
	 */
	public function getCountUsersForCourse($course_id=0){
		$course_id = esc_sql($course_id);
		return $this->getVar('COUNT(DISTINCT user_id)', " entity_id='$course_id' AND entity_type='ulp_course' ");
	}
	/**
	 * @param int
	 * @return bool
	 */
	public function is_user_student($uid=0){
		$uid = esc_sql($uid);
		return $this->getVar('id', " user_id=$uid AND entity_type='ulp_course' ");
	}
	/**
	 * @param int
	 * @return array
	 */
	public function get_user_courses($uid=0){
		$array = array();
		$uid = esc_sql($uid);
		$temp_array = $this->getResults('entity_id', " user_id=$uid AND entity_type='ulp_course' ");
		if ($temp_array){
			foreach ($temp_array as $temp){
				$array[] = $temp['entity_id'];
			}
		}
		return $array;
	}

	public function deleteAllEntriesByEntity( $entityId=0 )
	{
			global $wpdb;
			if ( !$entityId ){
					return false;
			}
			$query = $wpdb->prepare( "DELETE FROM {$this->table} WHERE entity_id=%d", $entityId );
			return $wpdb->query( $query );
	}
	
}
