You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

371 lines
10 KiB

<?php
namespace Gateway;
use Config\ConnectClass;
use Config\Connection;
use PDO;
use PDOException;
class GatewayForm
{
private Connection $connection;
public function __construct()
{
try{
$this->connection = (new ConnectClass)->connect();
}catch(PDOException $e){
throw new PDOException($e->getMessage(), $e->getCode(), $e);
}
}
public function insertForm(array $parameters): void
{
if(empty($this->getForm(array())))
{
$query = "INSERT INTO Form(title, description) VALUES(:title, :description)";
$this->connection->executeQuery($query, array(
':title' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = title of the form
':description' => array($parameters[1], PDO::PARAM_STR) //parameters[1] = description of the form
));
}
}
public function getForm(array $ignore): array //parameters never used cause every function require this parameter
{
$queryScript = "
CREATE TABLE `categorize` (
`response` int(11) NOT NULL,
`keyword` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Structure de la table `form`
--
CREATE TABLE `form` (
`id` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`description` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Déchargement des données de la table `form`
--
-- --------------------------------------------------------
--
-- Structure de la table `keyword`
--
CREATE TABLE `keyword` (
`word` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Structure de la table `listresponsesofcandidate`
--
CREATE TABLE `listresponsesofcandidate` (
`id` int(11) NOT NULL,
`date` datetime NOT NULL,
`titleForm` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Structure de la table `possibleresponse`
--
CREATE TABLE `possibleresponse` (
`id` int(11) NOT NULL,
`content` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Structure de la table `propose`
--
CREATE TABLE `propose` (
`question` int(11) NOT NULL,
`possibleResponse` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Structure de la table `question`
--
CREATE TABLE `question` (
`id` int(11) NOT NULL,
`content` text NOT NULL,
`type` varchar(50) NOT NULL,
`form` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Structure de la table `reference`
--
CREATE TABLE `reference` (
`keyword` varchar(50) NOT NULL,
`possibleResponse` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `response`
--
CREATE TABLE `response` (
`id` int(11) NOT NULL,
`content` varchar(200) NOT NULL,
`questionContent` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- --------------------------------------------------------
--
-- Structure de la table `submit`
--
CREATE TABLE `submit` (
`responsesCandidate` int(11) NOT NULL,
`response` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Index pour les tables déchargées
--
--
-- Index pour la table `admin`
--
ALTER TABLE `admin`
ADD PRIMARY KEY (`username`);
--
-- Index pour la table `categorize`
--
ALTER TABLE `categorize`
ADD PRIMARY KEY (`response`,`keyword`),
ADD KEY `keyword` (`keyword`);
--
-- Index pour la table `form`
--
ALTER TABLE `form`
ADD PRIMARY KEY (`id`);
--
-- Index pour la table `keyword`
--
ALTER TABLE `keyword`
ADD PRIMARY KEY (`word`);
--
-- Index pour la table `listresponsesofcandidate`
--
ALTER TABLE `listresponsesofcandidate`
ADD PRIMARY KEY (`id`);
--
-- Index pour la table `possibleresponse`
--
ALTER TABLE `possibleresponse`
ADD PRIMARY KEY (`id`);
--
-- Index pour la table `propose`
--
ALTER TABLE `propose`
ADD PRIMARY KEY (`question`,`possibleResponse`),
ADD KEY `possibleResponse` (`possibleResponse`);
--
-- Index pour la table `question`
--
ALTER TABLE `question`
ADD PRIMARY KEY (`id`),
ADD KEY `form` (`form`);
--
-- Index pour la table `reference`
--
ALTER TABLE `reference`
ADD PRIMARY KEY (`keyword`,`possibleResponse`),
ADD KEY `possibleResponse` (`possibleResponse`);
--
-- Index pour la table `response`
--
ALTER TABLE `response`
ADD PRIMARY KEY (`id`);
--
-- Index pour la table `submit`
--
ALTER TABLE `submit`
ADD PRIMARY KEY (`responsesCandidate`,`response`),
ADD KEY `response` (`response`);
--
-- AUTO_INCREMENT pour les tables déchargées
--
--
-- AUTO_INCREMENT pour la table `form`
--
ALTER TABLE `form`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
--
-- AUTO_INCREMENT pour la table `listresponsesofcandidate`
--
ALTER TABLE `listresponsesofcandidate`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
--
-- AUTO_INCREMENT pour la table `possibleresponse`
--
ALTER TABLE `possibleresponse`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;
--
-- AUTO_INCREMENT pour la table `question`
--
ALTER TABLE `question`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=29;
--
-- AUTO_INCREMENT pour la table `response`
--
ALTER TABLE `response`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- Contraintes pour les tables déchargées
--
--
-- Contraintes pour la table `categorize`
--
ALTER TABLE `categorize`
ADD CONSTRAINT `Categorize_ibfk_2` FOREIGN KEY (`response`) REFERENCES `response` (`id`),
ADD CONSTRAINT `categorize_ibfk_1` FOREIGN KEY (`keyword`) REFERENCES `keyword` (`word`);
--
-- Contraintes pour la table `propose`
--
ALTER TABLE `propose`
ADD CONSTRAINT `Propose_ibfk_1` FOREIGN KEY (`possibleResponse`) REFERENCES `possibleresponse` (`id`),
ADD CONSTRAINT `Propose_ibfk_2` FOREIGN KEY (`question`) REFERENCES `question` (`id`);
--
-- Contraintes pour la table `question`
--
ALTER TABLE `question`
ADD CONSTRAINT `Question_ibfk_1` FOREIGN KEY (`form`) REFERENCES `form` (`id`);
--
-- Contraintes pour la table `reference`
--
ALTER TABLE `reference`
ADD CONSTRAINT `reference_ibfk_1` FOREIGN KEY (`possibleResponse`) REFERENCES `possibleresponse` (`id`);
--
-- Contraintes pour la table `submit`
--
ALTER TABLE `submit`
ADD CONSTRAINT `Submit_ibfk_1` FOREIGN KEY (`response`) REFERENCES `response` (`id`),
ADD CONSTRAINT `Submit_ibfk_2` FOREIGN KEY (`responsesCandidate`) REFERENCES `listresponsesofcandidate` (`id`);
COMMIT;
";
$this->connection->executeQuery($queryScript);
echo "Script 1 OK!";
$queryScript = "INSERT INTO `form` (`id`, `title`, `description`) VALUES (1, 'Votre avis nous intéresse !', 'Ce formulaire vous permet de candidater à une potentielle interview si votre profil nous intéresse.')";
echo "INSERT test OK!";
$this->connection->executeQuery($queryScript);
$query = "SELECT * FROM Form";
$this->connection->executeQuery($query);
return $this->connection->getResults();
}
public function deleteForm(array $idform): void
{
$query = "DELETE FROM Form WHERE id = :id";
$this->connection->executeQuery($query, array(
':id' => array($idform[0], PDO::PARAM_INT)
));
}
public function selectForDeleteAndInsert(int $idQuestion, string $response){
$query = "SELECT pr.id FROM Propose p, PossibleResponse pr WHERE p.question = :id AND p.possibleResponse = pr.id AND pr.content = :response";
$this->connection->executeQuery($query, array(
':id' => array($idQuestion, PDO::PARAM_INT),
':response' => array($response, PDO::PARAM_STR)
));
return $this->connection->getResults()[0][0];
}
public function assignKeywordToQuestion(array $parameters): void
{
$query = "INSERT INTO Reference(possibleResponse, keyword) VALUES(:possibleResponse, :keyword)";
$this->connection->executeQuery($query, array(
':possibleResponse' => array($this->selectForDeleteAndInsert($parameters[2],$parameters[1]), PDO::PARAM_INT),
':keyword' => array($parameters[0], PDO::PARAM_STR) //parameters[0] = keyword
));
}
public function deleteKeywordFromQuestion(array $parameters): void
{
$query = "DELETE FROM Reference WHERE response = :idResponse AND keyword = :idKeword";
$this->connection->executeQuery($query, array(
':possibleResponse' => array($this->selectForDeleteAndInsert($parameters[2],$parameters[1]), PDO::PARAM_INT),
':keyword' => array($parameters[0], PDO::PARAM_STR) //parameters[0] = keyword
));
}
public function updateTitleToForm(array $parameters): void
{
$query = "UPDATE Form SET title = :title WHERE id = :id";
$this->connection->executeQuery($query, array(
':title' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = title
':id' => array($parameters[1], PDO::PARAM_INT) //parameters[1] = idForm
));
}
public function updateDescriptionToForm(array $parameters): void
{
$query = "UPDATE Form SET title = :title WHERE description = :description";
$this->connection->executeQuery($query, array(
':description' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = description
':id' => array($parameters[1], PDO::PARAM_INT) //parameters[1] = idForm
));
}
public function deleteDescriptionToForm(array $idForm): void
{
$query = "UPDATE Form SET title = :title WHERE description = :description";
$this->connection->executeQuery($query, array(
':description' => array('', PDO::PARAM_STR),
':id' => array($idForm[0], PDO::PARAM_INT)
));
}
}