<?php

namespace app\models;
if(!defined('ACCESS_CONTROL')) {header($_SERVER['SERVER_PROTOCOL']. ' 404'); exit('404 not found');}
use PDO;

class TaskLogs extends Model {
    function __construct() {
        parent::__construct();
    }

    public function logs_for($task_id)
    {
        return $this->core->dbh->query('SELECT * FROM `task_logs` WHERE `task_id` = '.$task_id)->fetchAll(PDO::FETCH_ASSOC);
    }

    public function insertLog($task_id, $log, $error=0)
    {
        $sql = "INSERT INTO `task_logs` SET `task_id` = :task_id, `log` = :log, `error` = :error, `created_at` = :now ";
        $stmt = $this->core->dbh->prepare($sql);
        /** @var $stmt \PDOStatement */
        $stmt->execute(array(
            'task_id'   =>  $task_id,
            'log'       =>  $log,
            'error'     =>  $error ? 1 : 0,
            'now'       =>  date("Y-m-d H:i:s")
        ));
    }

    public function is_valid()
    {
        // TODO: Implement is_valid() method.
    }
}