<?php

    class QuizAnswer extends Core {

        public function list() {

            $name = $_GET['name'] ?? null;
            $quiz_id = $_GET['quizId'] ?? null;
            $sql = "SELECT qa.*, l.id as user_id, l.firstname, l.lastname, qq.question_text, qq.question_type, qq.options_json, q.title, q.description
                    FROM quiz_answer qa
                    LEFT JOIN quiz_questions qq ON qa.question_id = qq.id                    
                    LEFT JOIN leads l ON qa.candidate_id = l.id
                    LEFT JOIN quizzes q ON qq.quiz_id = q.id
                    WHERE qq.quiz_id = :quiz_id";            

            if($name) {
                $sql .= " AND CONCAT(l.firstname, ' ', l.lastname) LIKE :name";                
            }

            $stmt = parent::$conn->prepare($sql);
            $stmt->bindValue(':quiz_id', $quiz_id, PDO::PARAM_INT);
            
            if($name) {
                $stmt->bindValue(':name', '%' . $name . '%', PDO::PARAM_STR);
            }

            $stmt->execute();
            $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

            return ['type' => 'OK', 'data' => $result];

        }
       
    }