implement task editing for Questtype ?Textinput? (issue #36)
This commit is contained in:
parent
d955fd0c72
commit
3b1bed540f
7 changed files with 449 additions and 19 deletions
|
|
@ -19,6 +19,12 @@
|
|||
*/
|
||||
class TextinputQuesttypeController extends \hhu\z\controllers\QuesttypeController
|
||||
{
|
||||
/**
|
||||
* Required components
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $components = array('validation');
|
||||
|
||||
|
||||
|
||||
|
|
@ -163,6 +169,125 @@
|
|||
$this->set('task', $task);
|
||||
$this->set('fields', $fields);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Action: edittask.
|
||||
*
|
||||
* Edit the task of a Quest.
|
||||
*
|
||||
* @param array $seminary Current Seminary data
|
||||
* @param array $questgroup Current Questgroup data
|
||||
* @param array $quest Current Quest data
|
||||
*/
|
||||
public function edittask($seminary, $questgroup, $quest)
|
||||
{
|
||||
// Get Task
|
||||
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
||||
$text = $task['text'];
|
||||
|
||||
// Get fields
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Get field sizes
|
||||
$fieldSizes = $this->Textinput->getFieldSizes();
|
||||
|
||||
// Values
|
||||
$validations = array();
|
||||
|
||||
// Save data
|
||||
if($this->request->getRequestMethod() == 'POST')
|
||||
{
|
||||
if(!is_null($this->request->getPostParam('preview')) || !is_null($this->request->getPostParam('save')))
|
||||
{
|
||||
// Get params and validate them
|
||||
if(is_null($this->request->getPostParam('text'))) {
|
||||
throw new \nre\exceptions\ParamsNotValidException('text');
|
||||
}
|
||||
$text = $this->request->getPostParam('text');
|
||||
if(is_null($this->request->getPostParam('fields'))) {
|
||||
throw new \nre\exceptions\ParamsNotValidException('fields');
|
||||
}
|
||||
$fields = $this->request->getPostParam('fields');
|
||||
$fields = array_values($fields);
|
||||
$fieldIndex = 0;
|
||||
foreach($fields as &$field)
|
||||
{
|
||||
// Validate regex
|
||||
$regex = $field['regex'];
|
||||
//$fieldValidation = $this->Validation->validate($regex, \nre\configs\AppConfig::$validation['regex']);
|
||||
$fieldValidation = @preg_match($regex, '') !== false;
|
||||
if($fieldValidation !== true)
|
||||
{
|
||||
if(!array_key_exists($fieldIndex, $validations) || !is_array($validations[$fieldIndex])) {
|
||||
$validations[$fieldIndex] = array();
|
||||
}
|
||||
$validations[$fieldIndex] = $this->Validation->addValidationResults($validations[$fieldIndex], 'regex', $fieldValidation);
|
||||
}
|
||||
|
||||
// Validate size
|
||||
foreach($fieldSizes as $sizeIndex => &$size)
|
||||
{
|
||||
if($size['size'] == $field['size'])
|
||||
{
|
||||
$field['sizeIndex'] = $sizeIndex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!array_key_exists('sizeIndex', $field)) {
|
||||
throw new \nre\exceptions\ParamsNotValidException($fieldIndex);
|
||||
}
|
||||
|
||||
$fieldIndex++;
|
||||
}
|
||||
|
||||
// Save and redirect
|
||||
if(!is_null($this->request->getPostParam('save')) && empty($validations))
|
||||
{
|
||||
// Save text
|
||||
$this->Textinput->setTextForQuest(
|
||||
$this->Auth->getUserId(),
|
||||
$quest['id'],
|
||||
$text
|
||||
);
|
||||
|
||||
// Save field
|
||||
foreach($fields as $index => &$field)
|
||||
{
|
||||
// Add regex modifiers
|
||||
$modifiers = substr($field['regex'], strrpos($field['regex'], $field['regex'][0]) + 1);
|
||||
if(strpos($modifiers, 'u') === false) {
|
||||
$field['regex'] .= 'u';
|
||||
}
|
||||
|
||||
// Save field
|
||||
$this->Textinput->setFieldForText(
|
||||
$quest['id'],
|
||||
$index + 1,
|
||||
$fieldSizes[$field['sizeIndex']]['id'],
|
||||
$field['regex']
|
||||
);
|
||||
}
|
||||
|
||||
// Redirect
|
||||
$this->redirect($this->linker->link(array('quest', $seminary['url'], $questgroup['url'], $quest['url']), 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set colors for fields
|
||||
foreach($fields as &$field) {
|
||||
$field['color'] = str_pad(sprintf('%x%x%x', rand(0,255), rand(0,255), rand(0,255)), 6, 9);
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('task', $task);
|
||||
$this->set('text', $text);
|
||||
$this->set('fields', $fields);
|
||||
$this->set('fieldSizes', $fieldSizes);
|
||||
$this->set('validations', $validations);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -112,6 +112,109 @@
|
|||
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the text for a Quest and correct fields count.
|
||||
*
|
||||
* @param int $userId ID of user setting text
|
||||
* @param int $questId ID of Quest to set text for
|
||||
* @param string $text Text for Quset
|
||||
*/
|
||||
public function setTextForQuest($userId, $questId, $text)
|
||||
{
|
||||
$this->db->setAutocommit(false);
|
||||
try {
|
||||
// Set text
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_textinput '.
|
||||
'(quest_id, created_user_id, text) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) '.
|
||||
'ON DUPLICATE KEY UPDATE '.
|
||||
'text = ?',
|
||||
'iiss',
|
||||
$questId,
|
||||
$userId,
|
||||
$text,
|
||||
$text
|
||||
);
|
||||
|
||||
// Count fields
|
||||
$fieldCount = substr_count($text, '[textinput]');
|
||||
|
||||
// Remove fields
|
||||
$this->db->query(
|
||||
'DELETE FROM questtypes_textinput_fields '.
|
||||
'WHERE questtypes_textinput_quest_id = ? AND number > ?',
|
||||
'ii',
|
||||
$questId,
|
||||
$fieldCount
|
||||
);
|
||||
|
||||
// Add fields
|
||||
for($i=1; $i<=$fieldCount; $i++)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT IGNORE INTO questtypes_textinput_fields '.
|
||||
'(questtypes_textinput_quest_id, number, regex) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) ',
|
||||
'iis',
|
||||
$questId,
|
||||
$i,
|
||||
''
|
||||
);
|
||||
}
|
||||
|
||||
$this->db->commit();
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
$this->db->rollback();
|
||||
$this->db->setAutocommit(true);
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->db->setAutocommit(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set values for a field of a text.
|
||||
*
|
||||
* @param int $questId ID of Quest to set field for
|
||||
* @param int $number Field number
|
||||
* @param int $sizeId ID of field size
|
||||
* @param string $regex RegEx for field
|
||||
*/
|
||||
public function setFieldForText($questId, $number, $sizeId, $regex)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE questtypes_textinput_fields '.
|
||||
'SET questtypes_textinput_fieldsize_id = ?, regex = ? '.
|
||||
'WHERE questtypes_textinput_quest_id = ? AND number = ?',
|
||||
'isii',
|
||||
$sizeId,
|
||||
$regex,
|
||||
$questId,
|
||||
$number
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all registered field sizes.
|
||||
*
|
||||
* @return List of field sizes
|
||||
*/
|
||||
public function getFieldSizes()
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, size '.
|
||||
'FROM questtypes_textinput_fieldsizes '.
|
||||
'ORDER BY size'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
105
questtypes/textinput/html/edittask.tpl
Normal file
105
questtypes/textinput/html/edittask.tpl
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
<?php if(!empty($validations)) : ?>
|
||||
<ul>
|
||||
<?php foreach($validations as $field => &$settings) : ?>
|
||||
<li>
|
||||
<ul>
|
||||
<?php foreach($settings as $setting => $value) : ?>
|
||||
<li>
|
||||
<?php
|
||||
switch($setting) {
|
||||
case 'regex': echo _('Regex invalid');
|
||||
break;
|
||||
default: echo _('Regex invalid');
|
||||
}
|
||||
?>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ul>
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ul>
|
||||
<?php endif ?>
|
||||
<form method="post">
|
||||
<fieldset>
|
||||
<legend><?=_('Text')?></legend>
|
||||
<button id="add-field"><?=_('Add field')?></button><br />
|
||||
<textarea id="text" name="text"><?=$text?></textarea>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend><?=_('Fields')?></legend>
|
||||
<ul id="fields">
|
||||
<?php foreach($fields as $index => &$field) : ?>
|
||||
<li>
|
||||
<select name="fields[<?=$index?>][size]">
|
||||
<?php foreach($fieldSizes as &$size) : ?>
|
||||
<option value="<?=$size['size']?>" <?php if($fields[$index]['size'] == $size['size']) : ?>selected="selected"<?php endif ?>><?=$size['size']?></option>
|
||||
<?php endforeach ?>
|
||||
</select>
|
||||
<input type="text" name="fields[<?=$index?>][regex]" value="<?=$field['regex']?>" style="border:1px solid #<?=$field['color']?>" <?php if(!empty($validations) && array_key_exists($index, $validations)) : ?>class="invalid"<?php endif ?> />
|
||||
</li>
|
||||
<?php endforeach ?>
|
||||
</ul>
|
||||
</fieldset>
|
||||
<input type="submit" name="preview" value="<?=_('Preview')?>" />
|
||||
<input type="submit" name="save" value="<?=_('save')?>" />
|
||||
</form>
|
||||
<h2><?=_('Preview')?></h2>
|
||||
<p>
|
||||
<?php $posStart = 0; ?>
|
||||
<?php foreach($fields as &$field) : ?>
|
||||
<?php $posEnd = mb_strpos($text, '[textinput]', $posStart, 'UTF-8'); ?>
|
||||
<?=$t->t(mb_substr($text, $posStart, $posEnd-$posStart, 'UTF-8'))?>
|
||||
<input type="text" name="answers[]" value="<?=(array_key_exists('answer', $field)) ? $field['answer'] : ''?>" <?php if($field['size'] != 'default') : ?>class="<?=$field['size']?>"<?php endif ?> />
|
||||
<?php $posStart = $posEnd + mb_strlen('[textinput]', 'UTF-8'); ?>
|
||||
<?php endforeach ?>
|
||||
<?=$t->t(mb_substr($text, $posStart, mb_strlen($text, 'UTF-8')-$posStart, 'UTF-8'))?>
|
||||
</p>
|
||||
|
||||
<script>
|
||||
var index = <?=count($fields)?>;
|
||||
var selectElement = '<select name="fields[INDEX][size]">' +
|
||||
<?php foreach($fieldSizes as &$size) : ?>
|
||||
'<option value="<?=$size['size']?>"><?=$size['size']?></option>' +
|
||||
<?php endforeach ?>
|
||||
'</select>';
|
||||
var inputElement = '<input type="text" name="fields[INDEX][regex]" />';
|
||||
$("#add-field").click(function(event) {
|
||||
event.preventDefault();
|
||||
var caret = getCaret("text");
|
||||
insertAtCaret("text", caret, "[textinput]");
|
||||
updateFields();
|
||||
});
|
||||
$("#text").on('change keyup paste', function(event) {
|
||||
updateFields();
|
||||
});
|
||||
|
||||
function updateFields()
|
||||
{
|
||||
var newCount = $("#text").val().split("[textinput]").length - 1;
|
||||
var oldCount = $("#fields li").length;
|
||||
var caret = getCaret("text");
|
||||
var pos = $("#text").val().substring(0, caret).split("[textinput]").length - 1;
|
||||
if(newCount > oldCount)
|
||||
{
|
||||
// Add fields
|
||||
for(var i=oldCount; i<newCount; i++)
|
||||
{
|
||||
index++;
|
||||
var element = '<li>' + selectElement.replace('INDEX', index) + inputElement.replace('INDEX', index) + '</li>';
|
||||
if($("#fields li").length > pos-1) {
|
||||
$($("#fields li")[pos-1]).before(element);
|
||||
}
|
||||
else {
|
||||
$($("#fields li")[pos-2]).after(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(newCount < oldCount)
|
||||
{
|
||||
// Remove fields
|
||||
for(var i=oldCount; i>newCount; i--) {
|
||||
$($("#fields li")[pos]).remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Loading…
Add table
Add a link
Reference in a new issue