<?php

namespace App\Models;

use CodeIgniter\Model;

class RespuestaModel extends Model
{
    protected $table = 'answers';
    protected $primaryKey = 'id';
    protected $useTimestamps = true;

    protected $allowedFields = ['survey_id', 'user_id', 'answer', 'question_id'];

    protected $validationRules = [
        'survey_id' => 'required',
        'user_id' => 'required',
        'answer' => 'required',
        'question_id' => 'required',
    ];

    public function getRespuestas($survey_id, $user_id)
    {
        $db = db_connect();
        $where = 'answers.survey_id = ' . $db->escape($survey_id) . ' AND answers.user_id = ' . $db->escape($user_id);
        return $this
            ->select('answers.id, answers.survey_id, answers.user_id, answers.answer,answers.question_id, questions.question, questions.type')
            ->join('questions', 'answers.question_id = questions.id')
            ->where($where)
            ->findAll();
    }
}
