<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('DbIndeedAbstract')) require_once ULP_PATH . 'classes/Abstracts/DbIndeedAbstract.class.php';
if (class_exists('DbModuleItems')) return;
class DbModuleItems extends DbIndeedAbstract{
	/**
	 * @var string
	 */
	protected $table = '';
	/*
	protected static $all_quizes = [];
	protected static $all_lessons = [];
	*/
	protected static $all_items = array(); /// will contain all lessons and quizes
	/**
	 * @param none
	 * @return none
	 */
	public function __construct(){
		global $wpdb;
		$this->table = $wpdb->prefix . 'ulp_course_modules_items';
	}
	/**
	 * @param int (module id)
	 * @param string (meta name)
	 * @return mixed
	 */
	public function getItem($module_id=0, $item_id=0){
		$module_id = esc_sql($module_id);
		$item_id = esc_sql($item_id);
		return $this->getVar('id', "module_id=$module_id AND item_id='$item_id'");
	}
	/**
	 * @param int (module)
	 * @param int (item id)
	 * @param string (course or quiz)
	 * @param int (the order)
	 * @param int (status 1 or 0)
	 * @return int
	 */
	public function saveItem($module_id=0, $course_id=0, $item_id=0, $item_type='', $item_order=0, $status=0){
		global $wpdb;
		$module_id = esc_sql($module_id);
		$course_id = esc_sql($course_id);
		$item_id = esc_sql($item_id);
		$item_type = esc_sql($item_type);
		$item_order = esc_sql($item_order);
		$status = esc_sql($status);
		if ($id=$this->getItem($module_id, $item_id)){
			/// update
			$this->update("module_id=$module_id, course_id='$course_id', item_id=$item_id, item_type='$item_type', item_order=$item_order, status=$status", "id=$id");
			return $id;
		} else {
			/// create
			return $this->insert("null, $module_id, $course_id, $item_id, '$item_type', $item_order, $status");
		}
	}
	/**
	 * @param int (module id)
	 * @param int (item id)
	 * @return bool
	 */
	public function deleteItem($module_id=0, $item_id=0){
		$module_id = esc_sql($module_id);
		$item_id = esc_sql($item_id);
		return $this->delete("module_id=$module_id AND item_id=$item_id");
	}
	public function updateOrder($module_id=0, $item_id=0, $order=0){
			global $wpdb;
			$module_id = esc_sql($module_id);
			$item_id = esc_sql($item_id);
			$entry_id = $this->getItem($module_id, $item_id);
			if ($entry_id){
					$order = esc_sql($order);
					return $wpdb->query("UPDATE {$this->table} SET item_order=$order WHERE id=$entry_id;");
			}
	}
	/**
	 * Return an array with all items for a module
	 * @param int (module id)
	 * @param string
	 * @return array
	 */
	public function getAllModuleItemsByModuleId($module_id=0, $type='', $onlyPublish=false){
			global $wpdb;
			$module_id = esc_sql($module_id);
			$q = "SELECT a.item_id FROM
								{$this->table} a
								INNER JOIN {$wpdb->posts} b
								ON a.item_id=b.ID
								WHERE module_id=$module_id ";
			if ($type){
				$type = esc_sql($type);
				$q .= " AND item_type='$type'";
			}
			if ($onlyPublish){
					$q .= " AND b.post_status='publish' ";
			}
			$q .= " ORDER BY item_order ASC";
			$data = $wpdb->get_results($q);
			return $data ? indeed_convert_to_array($data) : array();
	}
	/**
	 * @param int (module id)
	 * @return bool
	 */
	public function deleteAllModuleItemsByModuleId($module_id=0){
		$module_id = esc_sql($module_id);
		return $this->delete("module_id=$module_id");
	}
	/**
	 * @param int (module id)
	 * @param string
	 * @return int
	 */
	public function getCountModuleItems($module_id=0, $type='', $onlyPublish=false){
		$module_id = esc_sql($module_id);
		$type = esc_sql($type);
		global $wpdb;
		$q = "SELECT COUNT(a.id) FROM {$this->table} a
							INNER JOIN {$wpdb->posts} b
							ON a.item_id=b.ID
							WHERE
							a.module_id=$module_id
							AND
							a.item_type='$type'
		";
		if ($onlyPublish){
				$q .= " AND b.post_status='publish' ";
		}
		return $wpdb->get_var($q);
	}
	/**
	 * @param int
	 * @return int
	 */
	public function getLastQuizForCourse($module_id=0){
		$module_id = esc_sql($module_id);
		return $this->getVar("item_id", "module_id=$module_id AND item_type='ulp_quiz' ORDER BY item_order DESC LIMIT 1"); ///or quiz
	}
	/*
	public function getAllQuizes(){
			if (empty(self::$all_quizes)){
					self::$all_quizes =  DbUlp::getAllQuizes();
			}
			return self::$all_quizes;
	}
	public function getAllLessons(){
			if (empty(self::$all_lessons)){
					self::$all_lessons =  DbUlp::getAllLessons();
			}
			return self::$all_lessons;
	}
	*/
	public function getAllItems(){
			if (empty(self::$all_items)){
					self::$all_items = DbUlp::getAllQuizesAndLessons();
			}
			return self::$all_items;
	}
}
