Commit 4beddbd7 authored by Martin Gregor's avatar Martin Gregor
Browse files

pridanie spravy korekcie pre pouzivatelov

parent 6ec043cd
......@@ -17,44 +17,158 @@ class HomepagePresenter extends BasePresenter
public function renderDefault()
{
$count = $this->db->table('sentences')->count("*");
$this->sentence_id = (time() % $count) + 1;
$current_sentence_id = $this->sentence_id;
if (!$_POST['sentence_id'])
{
$count = $this->db->table('sentences')->count("*");
$this->sentence_id = (time() % $count) + 1;
$current_sentence_id = $this->sentence_id;
$sentenceIdSession = $this->getSession('sentenceIdSession');
$sentenceIdSession = $this->getSession('sentenceIdSession');
// inicializacia session
if (!is_array($sentenceIdSession->ids) || count($sentenceIdSession->ids) == $count)
{
$sentenceIdSession->ids = [];
// inicializacia session
if (!is_array($sentenceIdSession->ids) || count($sentenceIdSession->ids) == $count)
{
$sentenceIdSession->ids = [];
}
// vyhladanie nezobrazenej vety
while (in_array($current_sentence_id,$sentenceIdSession->ids))
{
$current_sentence_id = mt_rand(1, $count);
}
// ulozenie id vybranej vety a ziskanie vety z db
$sentenceIdSession->ids[] = $current_sentence_id;
// else
// {
// $current_sentence_id = $this->sentence_id = $_POST['sentence_id'];
// }
$sentence = $this->db->table('sentences')->get($current_sentence_id);
// rozdelenie vety na slova a inicializacia pola duplicit
$this->words = explode(' ', $sentence->sentence);
// lematizacia skolskou sluzbou
// $this->lematize($sentence);
// preklad kazdeho slova z vety
foreach ($this->words as $word)
{
$this->en_words[] = $this->translate($word);
}
$this->genTranslations();
$this->template->sentence = $sentence->sentence;
}
}
// vyhladanie nezobrazenej vety
while (in_array($current_sentence_id,$sentenceIdSession->ids))
protected function createComponentTranslationForm()
{
$form = new Nette\Application\UI\Form();
foreach ($this->sentences as $id => $sentence)
{
$current_sentence_id = mt_rand(1, $count);
$form->addRadioList('sentence'.$id,$sentence[0],['correct' => 'Správne','incorrect' => 'Nesprávne']);
foreach ($sentence[1] as $position => $word)
{
$form->addText($id.'word'.$position,'Preklad '.$word,40,100);
}
$form->addText('note'.$id,'Poznámka',60,500);
}
// ulozenie id vybranej vety a ziskanie vety z db
$sentenceIdSession->ids[] = $current_sentence_id;
$sentence = $this->db->table('sentences')->get($current_sentence_id);
$form->addHidden('sentence_id',$this->sentence_id);
$form->addSubmit('send','Ďalej');
$form->onSuccess[] = $this->translationFormSucceeded;
return $form;
}
// rozdelenie vety na slova a inicializacia pola duplicit
$this->words = explode(' ', $sentence->sentence);
public function translationFormSucceeded(Nette\Application\UI\Form $form)
{
$values = $_POST;//$form->getValues(TRUE);
$sentences = [];
// lematizacia skolskou sluzbou
// $this->lematize($sentence);
foreach ($values as $id => $value)
{
if (strstr($id,'sentence'))
{
$s_id = intval(str_replace('sentence','',$id));
$sentences[$s_id]['type'] = $value;
if ($value)
$this->correctTranslation($s_id,$value);
if ($_POST['note'.$s_id])
$sentences[$s_id]['note'] = $_POST['note'.$s_id];
}
else if (strstr($id,'word'))
{
$tmp = explode('word',$id);
if ($value)
$sentences[intval($tmp[0])]['words'][$tmp[1]] = $value;
}
}
// preklad kazdeho slova z vety
foreach ($this->words as $word)
foreach ($sentences as $id => $values)
{
$this->en_words[] = $this->translate($word);
if ($values['type'] == 'incorrect' && (isset($values['words']) || isset($values['note'])))
{
$tmp = $this->db->table('translations')->get($id);
$tmp = explode(' ',$tmp->translation);
foreach ($values['words'] as $position => $word)
{
$tmp[$position] = $word;
}
$correction = implode(' ',$tmp);
$this->storeCorrection($id,$correction,$values['words'],$values['note']);
}
}
}
$this->genTranslations();
private function correctTranslation($id, $value)
{
$translation = $this->db->table('translations')->get($id);
if ($value == 'correct')
{
$translation->update(['correct_count' => 'correct_count + 1']);
}
else
{
$translation->update(['incorrect_count' => 'incorrect_count + 1']);
}
}
$this->template->sentence = $sentence->sentence;
$this->template->sentences = $this->sentences;
private function storeCorrection($trans_id,$correction,$words,$comment)
{
$correction_id = $this->db->table('corrections')->select('id')->where(['translation_id' => $trans_id,'correction' => $correction])->fetch();
if ($correction_id)
{
$correction->update(['use_count' => 'use_count + 1','updated_at' => date('Y-m-d H:i:s',time())]);
}
else
{
$this->db->table('corrections')->insert(['use_count' => 1, 'translation_id' => $trans_id,
'correction' => $correction,'correction_count' => count($words)]);
$correction_id = $this->db->lastInsertId();
foreach ($words as $position => $word)
{
$word_en_id = $this->db->table('word_en')->select('id')->where(['word_en' => $word])->fetch();
if (!$word_en_id)
{
$this->db->table('word_en')->insert(['word_en' => $word]);
$word_en_id = $this->db->lastInsertId();
}
$word_sk_id = $this->db->table('word_translation')->select('word_sk_id')
->where(['translation_id' => $trans_id,'word_position' => $position])->fetch();
if ($word_sk_id)
$this->db->table('word_correction')->insert(['correction_id' => $correction_id,'word_sk_id' => $word_sk_id, 'word_en_id' => $word_en_id]);
}
}
if ($comment)
{
$this->db->table('comments')->insert(['correction_id' => $correction_id,'comment' => $comment]);
}
}
private function translate($word)
......@@ -102,12 +216,11 @@ class HomepagePresenter extends BasePresenter
}
$this->storeTranslation($sentences);
$this->sentences = $sentences;
}
private function storeTranslation($sentences)
{
$tmp_sentences = [];
foreach ($sentences as $sentence)
{
$translation_id = $this->db->table('translations')->where(['sentence_id' => $this->sentence_id,'translation' => $sentence])->select('id')->fetch();
......@@ -121,10 +234,15 @@ class HomepagePresenter extends BasePresenter
else
{
$this->db->table('translations')->insert(['sentence_id' => $this->sentence_id,'translation' => $sentence,
'correct_count' => 0, 'incorrect_count' => 0, 'view_count' => 1, 'translated_word_count' => count($this->sentences[$sentence])]);
$this->storeWords($this->sentences[$sentence],$this->db->lastInsertId());
'correct_count' => 0, 'incorrect_count' => 0, 'view_count' => 1,
'translated_word_count' => count($this->sentences[$sentence])]);
$translation_id = $this->db->lastInsertId();
$this->storeWords($this->sentences[$sentence],$translation_id);
}
$tmp_sentences[$translation_id] = [$sentence,$this->sentences[$sentence]];
}
$this->sentences = $tmp_sentences;
}
private function storeWords($words, $translation_id)
......
......@@ -5,7 +5,5 @@
<hgroup>
<h2>Veta:</h2><h3>{$sentence}</h3>
</hgroup>
<section class="en-sentences" n:foreach="$sentences as $item">
<p>{$item}</p>
</section>
{control translationForm}
{/block}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment