<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('DbIndeedAbstract')) require_once ULP_PATH . 'classes/Abstracts/DbIndeedAbstract.class.php';
if (class_exists('DbActivityUlp')) return;
class DbActivityUlp extends DbIndeedAbstract{
	/*
	 	============ WP_ULP_ACTIVITY ==============
		id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
		uid INT(11) NOT NULL,
		entity_id INT(11) DEFAULT 0,
		entity_type VARCHAR(200),
		action VARCHAR(200),
		description TEXT,
		event_time TIMESTAMP NOT NULL DEFAULT 0,
		status TINYINT(1)
	 */
	/**
	 * @var string
	 */
	protected $table = '';
	/**
	 * @param none
	 * @return none
	 */
	public function __construct(){
		global $wpdb;
		$this->table = $wpdb->prefix . 'ulp_activity';
	}
	/**
	 * @param int (user id)
	 * @param int (entity id)
	 * @param string (action)
	 * @return mixed
	 */
	public function getItem($uid=0, $entity_id=0, $action=''){
			$uid = esc_sql($uid);
			$entity_id = esc_sql($entity_id);
			$action = esc_sql($action);
			return $this->getVar('id', "uid=$uid AND entity_id='$entity_id' AND action='$action'");
	}
	public function getAllByUid($uid=0, $limit=0, $offset=0){
			global $wpdb;
			$uid = esc_sql($uid);
			$q = "SELECT `id`,`uid`,`entity_id`,`entity_type`,`action`,`description`,`event_time`,`status` FROM {$wpdb->prefix}ulp_activity
							WHERE 1=1
							AND uid=$uid
							ORDER BY id DESC
			";
			if ($limit){
					$limit = esc_sql($limit);
					$offset = esc_sql($offset);
					$q .= " LIMIT $limit OFFSET $offset";
			}
			return $wpdb->get_results($q);
	}
	public function getCountAll($uid=0){
			global $wpdb;
			$uid = esc_sql($uid);
			$q = "SELECT COUNT(id) as v FROM {$wpdb->prefix}ulp_activity
							WHERE 1=1
							AND uid=$uid ";
			return $wpdb->get_var($q);
	}
	/**
	 * @param int (user id)
	 * @param int (entity id)
	 * @param string (entity type)
	 * @param string (action)
	 * @param string (description)
	 * @param int (status)
	 * @return int
	 */
	public function saveItem($uid=0, $entity_id=0, $entity_type='', $action='', $description='', $time='', $status=1){
		global $wpdb;
		$uid = esc_sql($uid);
		$entity_id = esc_sql($entity_id);
		$entity_type = esc_sql($entity_type);
		$action = esc_sql($action);
		$description = esc_sql($description);
		$time = esc_sql($time);
		$status = esc_sql($status);
		if ($id=$this->getItem($uid, $entity_id, $action)){
			/// update
			$this->update("uid=$uid, entity_id=$entity_id, entity_type='$entity_type', action='$action', description='$description', event_time='$time', status='$status'", "id=$id");
			return $id;
		} else {
			/// create
			return $this->insert("null, $uid, $entity_id, '$entity_type', '$action', '$description', '$time', $status");
		}
	}
	/**
	 * @param int (user id)
	 * @param int (entity id)
	 * @param string (action type)
	 * @return string (date)
	 */
	public function getItemTime($uid=0, $entity_id=0, $action=''){
		$uid = esc_sql($uid);
		$entity_id = esc_sql($entity_id);
		$action = esc_sql($action);
		return $this->getVar('event_time', "uid=$uid AND entity_id='$entity_id' AND action='$action'");
	}
	/**
	 * @param int
	 * @param int
	 * @return boolean
	 */
	public function is_lesson_completed($uid=0, $lesson_id=0){
		if ($uid && $lesson_id){
			$uid = esc_sql($uid);
			$lesson_id = esc_sql($lesson_id);
			$data = $this->getVar('id', "uid=$uid AND entity_id=$lesson_id AND action='complete_lesson' ");
			if ($data){
				return TRUE;
			}
		}
		return FALSE;
	}
	public function delete($id=0){
			$id = esc_sql($id);
			return parent::delete(" id=$id ");
	}
}
