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.
Scripted/WEB/Model/AdminModel.php

89 lines
3.0 KiB

<?php
class AdminModel
{
private EnigmeGateway $enigme_gateway;
private Nettoyage $nettoyage;
private Validation $validation;
function __construct()
{
global $error, $view, $rep;
$this->enigme_gateway = new EnigmeGateway();
$this->nettoyage = new Nettoyage();
$this->validation = new Validation();
}
public function addNewEnigmeSolo(string $nom,string $enonce,string $aide,string $rappel,string $exemple,string $test,string $solution, string $prompt) : Enigme
{
$last = $this->enigme_gateway->findLastEnigma();
if ($last != null){
$ordre = $last[0]->getOrdre() + 1;
} else {
$ordre = 1;
}
$enigme = new Enigme(1,$nom, $enonce, $aide, $rappel, $exemple, $solution, $test, $ordre, 0, 0, $prompt);
$this->enigme_gateway->insert($enigme);
$tabEnigme = $this->enigme_gateway->findLastEnigma();
$js = fopen("View/src/JS/$nom.js", "w");
if (is_resource($js)) {
fwrite($js, "//~ Function that test the user code
async function submit(){
var test = editor.getValue()+`\\n\n". $solution . "\n". $test . "\n`;
exec(\"print ('True')\", \"code\");
exec(test, \"solution\");
result.innerHTML = \"Test en cours...\";
await new Promise(r => setTimeout(r, 1500));
check();
}");
fclose($js);
} else {
throw new Exception("Impossible d'ouvrir le fichier");
}
return $tabEnigme[0];
}
public function deleteEnigme(int $id) : void
{
$nom = $this->enigme_gateway->findById($id)[0]->getNom();
$this->enigme_gateway->delete($id);
unlink('View/src/JS/'.$nom.'.js');
}
public function getEnigmesSolo() : array
{
return $this->enigme_gateway->findSoloEnigma();
}
public function getEnigmeById (int $id) : Enigme
{
return $this->enigme_gateway->findById($id)[0];
}
public function editEnigme(int $id, string $nom,string $enonce,string $aide,string $rappel,string $exemple,string $test,string $solution, string $prompt) : Enigme
{
$old = $this->enigme_gateway->findById($id)[0];
$ordre = $old->getOrdre();
$enigme = new Enigme($id,$nom, $enonce, $aide, $rappel, $exemple, $solution, $test, $ordre, 0, 0, $prompt);
$this->enigme_gateway->update($enigme);
$js = fopen("View/src/JS/$nom.js", "w");
if (is_resource($js)) {
fwrite($js, "//~ Function that test the user code
async function submit(){
var test = editor.getValue()+`\\n\n". $solution . "\n". $test . "\n`;
exec(\"print ('True')\", \"code\");
exec(test, \"solution\");
result.innerHTML = \"Test en cours...\";
await new Promise(r => setTimeout(r, 1500));
check();
}");
fclose($js);
if ($old->getNom() != $nom){
unlink('View/src/JS/'.$old->getNom().'.js');
}
} else {
throw new Exception("Impossible d'ouvrir le fichier");
}
return $enigme;
}
}