Questtype ?Crossword?: add basic edit form

This commit is contained in:
oliver 2015-05-14 18:49:46 +02:00
parent fc9c4d9ea1
commit 8c79cb617e
2 changed files with 126 additions and 1 deletions

View file

@ -373,6 +373,84 @@
*/
public function edittask($seminary, $questgroup, $quest)
{
// Get words
$words = $this->Crossword->getWordsForQuest($quest['id']);
// Create 2D-matrix
$matrix = array();
$maxX = 0;
$maxY = 0;
foreach($words as $index => &$word)
{
// Insert word
if($word['vertical'])
{
$x = $word['pos_x'];
$startY = $word['pos_y'];
$endY = $startY + mb_strlen($word['word'], 'UTF-8') - 1;
$matrix = array_pad($matrix, $x+1, array());
$matrix[$x] = array_pad($matrix[$x], $endY+1, null);
$maxX = max($maxX, $x);
$maxY = max($maxY, $endY);
foreach(range($startY, $endY) as $y)
{
$matrix[$x][$y] = array(
'char' => mb_substr($word['word'], $y-$startY, 1, 'UTF-8'),
'indices' => (array_key_exists($x, $matrix) && array_key_exists($y, $matrix[$x]) && !is_null($matrix[$x][$y]) && array_key_exists('indices', $matrix[$x][$y])) ? $matrix[$x][$y]['indices'] : array()
);
if($y == $startY) {
$matrix[$x][$y]['indices'][] = $index;
}
if(array_key_exists('answer', $word))
{
$answer = mb_substr($word['answer'], $y-$startY, 1, 'UTF-8');
if($answer != ' ') {
$matrix[$x][$y]['answer'] = $answer;
}
}
}
}
else
{
$startX = $word['pos_x'];
$endX = $startX + mb_strlen($word['word'], 'UTF-8') - 1;
$y = $word['pos_y'];
$matrix = array_pad($matrix, $endX+1, array());
$maxX = max($maxX, $endX);
$maxY = max($maxY, $y);
foreach(range($startX, $endX) as $x)
{
$matrix[$x] = array_pad($matrix[$x], $y+1, null);
$matrix[$x][$y] = array(
'char' => mb_substr($word['word'], $x-$startX, 1, 'UTF-8'),
'indices' => (array_key_exists($x, $matrix) && array_key_exists($y, $matrix[$x]) && !is_null($matrix[$x][$y]) && array_key_exists('indices', $matrix[$x][$y])) ? $matrix[$x][$y]['indices'] : array(),
'answer' => null
);
if($x == $startX) {
$matrix[$x][$y]['indices'][] = $index;
}
if(array_key_exists('answer', $word))
{
$answer = mb_substr($word['answer'], $x-$startX, 1, 'UTF-8');
if($answer != ' ') {
$matrix[$x][$y]['answer'] = $answer;
}
}
}
}
}
// Pass data to view
$this->set('words', $words);
$this->set('maxX', $maxX);
$this->set('maxY', $maxY);
$this->set('matrix', $matrix);
}
}

View file

@ -1 +1,48 @@
<p>TODO</p>
<form method="post" class="crossword">
<fieldset>
<legend><?=_('Preview')?></legend>
<table id="matrix">
<tbody>
<?php foreach(range(0, $maxY) as $y) : ?>
<tr>
<?php foreach(range(0, $maxX) as $x) : ?>
<td>
<?php if(array_key_exists($x, $matrix) && array_key_exists($y, $matrix[$x]) && !is_null($matrix[$x][$y])) : ?>
<?php if(count($matrix[$x][$y]['indices']) > 0) : ?><span class="index"><?=implode('/',array_map(function($e) { return $e+1; }, $matrix[$x][$y]['indices']))?></span><?php endif ?>
<input type="text" maxlength="1" size="1" disabled="disabled" value="<?=$matrix[$x][$y]['char']?>" />
<?php endif ?>
</td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
</fieldset>
<fieldset class="dev">
<legend><?=_('Questions')?></legend>
<ol>
<?php foreach($words as &$word) : ?>
<li>
<?=_('Position')?>:
<input type="number" value="<?=$word['pos_x']+1?>" />
<input type="number" value="<?=$word['pos_y']+1?>" />
<select>
<option <?php if(!$word['vertical']) : ?>selected="selected"<?php endif ?>><?=_('horizontal')?></option>
<option <?php if($word['vertical']) : ?>selected="selected"<?php endif ?>><?=_('vertical')?></option>
</select>
<br />
<?=_('Question')?>:<br />
<textarea><?=$word['question']?></textarea><br />
<?=_('Answer')?>:<br />
<input type="text" value="<?=$word['word']?>" />
<br />
<input type="button" class="remove-question" value="" />
</li>
<?php endforeach ?>
<li>
<input type="button" class="add-question" value="+" />
</li>
</ol>
</fieldset>
<input type="submit" name="save" value="<?=_('save')?>" />
</form>