<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('DbIndeedAbstract')) require_once ULP_PATH . 'classes/Abstracts/DbIndeedAbstract.class.php';
if (class_exists('Db_Ulp_Student_Badges')) return;
class Db_Ulp_Student_Badges extends DbIndeedAbstract{
/*
uid BIGINT(20) NOT NULL,
badge_id BIGINT(20),
obtained_date TIMESTAMP NOT NULL DEFAULT 0
*/
  	/**
  	 * @var string
  	 */
  	protected $table = '';
  	/**
  	 * @param none
  	 * @return none
  	 */
  	public function __construct(){
    		global $wpdb;
    		$this->table = $wpdb->prefix . 'ulp_student_badges';
  	}
    public function save($uid=0, $badge_id=0){
        $create_date = date('Y-m-d H:i:s', time() );
        $uid = esc_sql($uid);
        $badge_id = esc_sql($badge_id);
        $create_date = esc_sql($create_date);
        return parent::insert("null, $uid, $badge_id, '$create_date'");
    }
    public function delete($uid=0, $badge_id=0){
        $uid = esc_sql($uid);
        $badge_id = esc_sql($badge_id);
        return parent::delete(" uid=$uid AND badge_id=$badge_id ");
    }
    public function getAllForUser($uid=0){
        global $wpdb;
        $uid = esc_sql($uid);
        $q = "SELECT a.badge_title, a.badge_content, a.badge_image, a.id
                FROM {$wpdb->prefix}ulp_badges a
                INNER JOIN {$wpdb->prefix}ulp_student_badges b ON  a.id=b.badge_id
                WHERE 1=1
                AND b.uid=$uid
        ";
        return $wpdb->get_results($q);
    }
}
