<?php

namespace Modules\Evaluation\Validator;

use Entity\Model\CampQstQ;
use Entity\Model\SigDeleg;
use Helpers\Outil\Date;
use Helpers\Trad\Trad;
use Helpers\Validator\Validator;
use Modules\Evaluation\Repository\BibliothequeDeQuestions;
use Modules\Evaluation\Repository\Evaluation;

class CampaignValidator extends Validator
{
    /**
     * validationCheck
     * vérifie qu'une campagne peut être validée via la procédure stockée
     *
     * @param  string $id
     * @return bool
     */
    public function validationCheck(string $id): bool
    {
        $repo = new BibliothequeDeQuestions($this->pdo);
        $calculatedQuestions = (new CampQstQ($this->pdo))
            ->findAll(['ID_CAMP' => $id, 'TYPE_REP' => 'C'], ['ID_THEME', 'ID_QUEST', 'REPCALC_SELECT', 'REPCALC_REQ', 'LIB_QUEST', 'ID_THEME']);

        if (count($calculatedQuestions) > 0) {
            foreach ($calculatedQuestions as $q) {
                if (!$repo->checkIfSqlRequestWorks($q['REPCALC_SELECT'], $q['REPCALC_REQ'])) {
                    $this->setError(Trad::strBuild(
                        $this->trad->t('La question "@1@" (@2@) issue de la base de données ne renvoie aucun résultat.'),
                        $q['LIB_QUEST'],
                        $q['ID_THEME']
                    ));
                }
            }
        }

        return $this->isValid();
    }
}
