Generování addRadioList()
- Allconius
- Člen | 317
Ahoj, generuji z databáze náhodných 30 otázek testu, ale při ukládání se mi to znova jakoby přegeneruje, tzn. ukládají se mi jiné otázky než ty vyplněné. Nevíte kde mám chybu? Nemůžu na to přijít.
<?php
namespace App\Presenters;
use Nette;
use App\Model\DbManager;
use Nette\Application\UI\Form;
use Nette\Utils\DateTime;
use Nette\Utils\Html;
class TestPresenter extends Nette\Application\UI\Presenter
{
/** @var DbManager */
private $dbManager;
public function __construct(DbManager $dbManager)
{
$this->dbManager = $dbManager;
}
protected function createComponentTestForm()
{
$form = new Form;
$form->getElementPrototype()->name = 'test';
$form->addProtection();
$result = $this->dbManager->zobrazTest(30); //pocet RAND otazek
$cislo = 1;
$numbers = range(1, 3);
foreach ($result as $row) {
if (($row->obrazek)<>'') {
$form->addGroup($cislo.'. '.$row->otazka.' ('.$row->id.')')
->setOption('description',Html::el()->setHtml('<img src="../obrazky/'.$row->obrazek.'.png" alt="'.$row->otazka.'" height="380" />'));
}else{
$form->addGroup($cislo.'. '.$row->otazka.' ('.$row->id.')');
}
//namixuj to nahodne
shuffle($numbers);
foreach ($numbers as $number) {
$m = 'm'.$number;
$odpovedi[$number] = ' '.$row->$m.'';
}
// pro vypsání možností do 1 řádku
$form->addRadioList('otazka'.$row->id.'', '', $odpovedi);
//->getSeparatorPrototype()->setName(null); //hodnoty v 1 radku
$cislo++;
}
$form->addSubmit('submit', 'Odeslat test');
// setup form rendering
$renderer = $form->getRenderer();
$renderer->wrappers['controls']['container'] = NULL;
$renderer->wrappers['pair']['container'] = 'div class=form-group';
$renderer->wrappers['pair']['.error'] = 'has-error';
$renderer->wrappers['control']['container'] = 'div class=col-sm-9';
$renderer->wrappers['label']['container'] = 'div class="col-sm-3 control-label"';
$renderer->wrappers['control']['description'] = 'span class=help-block';
$renderer->wrappers['control']['errorcontainer'] = 'span class=help-block';
// make form and controls compatible with Twitter Bootstrap
$form->getElementPrototype()->class('form-horizontal');
foreach ($form->getControls() as $control) {
if ($control instanceof Controls\Button) {
$control->getControlPrototype()->addClass(empty($usedPrimary) ? 'btn btn-primary' : 'btn btn-default');
$usedPrimary = TRUE;
} elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) {
$control->getControlPrototype()->addClass('form-control');
} elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) {
$control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type);
}
}
$form->onSuccess[] = [$this, 'testFormSucceeded'];
return $form;
}
public function testFormSucceeded(Form $form, $values)
{
print_r($values); // ???? tady už jsou jiné otázky než ty původně vygenerované ????
exit;
$user = $this->getUser();
$username = $user->getIdentity();
//$user->setAuthenticator($this->authenticator);
$id = $username->id;
$this->dbManager->ulozTest($values, $id);
$this->redirect('Homepage:');
exit;
}
public function renderDefault()
{
$castestu = $this->dbManager->castestu;
$user = $this->getUser();
$username = $user->getIdentity();
//$user->setAuthenticator($this->authenticator);
$id = $username->id;
$start = $this->dbManager->testStart($id);
//nastav datum z DB kvuli hackerum
$this->template->rok = date("Y", $start);
$this->template->mesic = date("n", $start);
$this->template->den = date("j", $start);
$this->template->hodina = date("H", $start);
$this->template->minuta = date("i", $start);
$this->template->sekunda = date("s", $start);
//uprav cas odeslani
$ubehlo = time()-$start;
$this->template->konectestu = (($castestu*60)-$ubehlo)*1000;
$this->template->castestu = $castestu;
}
public function actionDefault()
{
if (!$this->getUser()->isLoggedIn()) {
$this->redirect('Homepage:');
}
}
}
?>
- lightteka
- Backer | 17
Rekl by, ze si budes muset to
$result = $this->dbManager->zobrazTest(30); //pocet RAND otazek
Ulozit do session a pokud uz to v session mas, tak to nacitat z ni a ve
formSuccess po uspesnem ulozeni tu session smazat.
Pripadne v metode zobrazTest(30) si to nejak cachovat, aby do urcite doby nebo
nez ji reknes, ze to ma zapomenout, vracela stale stejne otazky.
Takhle se ti pri kazdym nacteni stranky vola createComponent a teda i ta metoda
zobrazTest()
, ktera znova vybere nahodny otazky.
Editoval lightteka (5. 2. 2019 9:46)