<?php
if (!defined('ABSPATH')) exit();
if (!class_exists('DbIndeedAbstract')) require_once ULP_PATH . 'classes/Abstracts/DbIndeedAbstract.class.php';
if (class_exists('Db_Ulp_Notes')) return;
/*
id BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
uid BIGINT(20) NOT NULL,
course_id BIGINT(20),
note_title VARCHAR (400),
note_content LONGTEXT,
obtained_date TIMESTAMP NOT NULL DEFAULT 0
*/
class Db_Ulp_Notes extends DbIndeedAbstract{
    /**
     * @var string
     */
    protected $table = '';
    /**
     * @param none
     * @return none
     */
    public function __construct(){
        global $wpdb;
        $this->table = $wpdb->prefix . 'ulp_notes';
    }
    public function save($uid=0, $course_id=0, $title='', $content=''){
        $insert_date = date('Y-m-d H:i:s', time() );
        $uid = esc_sql($uid);
        $course_id = esc_sql($course_id);
        $title = esc_sql($title);
        $content = esc_sql($content);
        return $this->insert("null, $uid, $course_id, '$title', '$content', '$insert_date'");
    }
    public function delete($id=0){
        $id = esc_sql($id);
        return parent::delete(" id=$id ");
    }
    public function selectAll($uid=0, $course_id=0, $limit=0, $offset=0){
        global $wpdb;
        $uid = esc_sql($uid);
        $course_id = esc_sql($course_id);
        $limit = esc_sql($limit);
        $offset = esc_sql($offset);
        $q = "SELECT a.id, a.note_title, a.note_content, a.obtained_date, b.post_title as course FROM {$wpdb->prefix}ulp_notes a
                INNER JOIN {$wpdb->posts} b ON a.course_id=b.ID
                WHERE
                uid=$uid
        ";
        if ($course_id){
            $q .= " AND course_id=$course_id ";
        }
        if ($limit){
            $q .= " LIMIT $limit OFFSET $offset ";
        }
        return $wpdb->get_results($q);
    }
}
