implement task editing for Questtype ?Choiceinput? (Issue #36)

This commit is contained in:
coderkun 2014-08-17 20:30:55 +02:00
commit c93f98f8d8
4 changed files with 445 additions and 2 deletions

View file

@ -222,6 +222,10 @@
),
'deadline' => array(
'regex' => '/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})?$/'
),
'choice' => array(
'minlength' => 1,
'maxlength' => 128
)
);

View file

@ -20,6 +20,12 @@
*/
class ChoiceinputQuesttypeController extends \hhu\z\controllers\QuesttypeController
{
/**
* Required components
*
* @var array
*/
public $components = array('validation');
@ -162,7 +168,7 @@
/**
* TODO Action: edittask.
* Action: edittask.
*
* Edit the task of a Quest.
*
@ -172,6 +178,119 @@
*/
public function edittask($seminary, $questgroup, $quest)
{
// Get Task
$task = $this->Choiceinput->getChoiceinputQuest($quest['id']);
$text = $task['text'];
// Get lists
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
foreach($choiceLists as &$list)
{
$list['choices'] = $this->Choiceinput->getChoiceinputChoices($list['id']);
foreach($list['choices'] as $index => &$choice) {
//$choice['correct'] = ($choice['id'] == $list['questtypes_choiceinput_choice_id']);
if($choice['id'] == $list['questtypes_choiceinput_choice_id']) {
$list['answer'] = $index;
}
$choice = $choice['text'];
}
//$list = $list['choices'];
}
// 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('lists'))) {
throw new \nre\exceptions\ParamsNotValidException('lists');
}
$choiceLists = $this->request->getPostParam('lists');
$choiceLists = array_values($choiceLists);
foreach($choiceLists as $listIndex => &$list)
{
// Validate choices
if(!array_key_exists('choices', $list)) {
throw new \nre\exceptions\ParamsNotValidException('choices');
}
$choiceIndex = 0;
$answer = null;
foreach($list['choices'] as $index => &$choice)
{
// Validate choice
$choiceValidation = $this->Validation->validate($choice, \nre\configs\AppConfig::$validation['choice']);
if($choiceValidation !== true)
{
if(!array_key_exists($listIndex, $validations) || !is_array($validations[$listIndex])) {
$validations[$listIndex] = array();
}
if(!array_key_exists('choices', $validations[$listIndex])) {
$validations[$listIndex]['choices'] = array();
}
$validations[$listIndex]['choices'][$choiceIndex] = $choiceValidation;
}
$choiceIndex++;
if(array_key_exists('answer', $list) && $list['answer'] == $index) {
$answer = $choiceIndex;
}
}
// Validate correct answer
if(is_null($answer))
{
if(!array_key_exists($listIndex, $validations) || !is_array($validations[$listIndex])) {
$validations[$listIndex] = array();
}
if(!array_key_exists('answer', $validations[$listIndex])) {
$validations[$listIndex]['answer'] = array();
}
$validations[$listIndex] = $this->Validation->addValidationResult($validations[$listIndex], 'answer', 'exist', true);
}
}
// Save and redirect
if(!is_null($this->request->getPostParam('save')) && empty($validations))
{
// Save text
$this->Choiceinput->setTextForQuest(
$this->Auth->getUserId(),
$quest['id'],
$text
);
// Save lists and choices
foreach($choiceLists as $listIndex => &$list)
{
// Save list
$this->Choiceinput->setListForText(
$quest['id'],
$listIndex + 1,
$list['choices'],
$list['answer'] + 1
);
}
// Redirect
$this->redirect($this->linker->link(array('quest', $seminary['url'], $questgroup['url'], $quest['url']), 1));
}
}
}
// Pass data to view
$this->set('task', $task);
$this->set('text', $text);
$this->set('choiceLists', $choiceLists);
$this->set('validations', $validations);
}
}

View file

@ -147,6 +147,148 @@
return null;
}
/**
* Set the text for a Quest and correct choice input lists count.
*
* @param int $userId ID of user setting text
* @param int $questId ID of Quest to set text for
* @param string $text Text for Quest
*/
public function setTextForQuest($userId, $questId, $text)
{
$this->db->setAutocommit(false);
try {
// Set text
$this->db->query(
'INSERT INTO questtypes_choiceinput '.
'(quest_id, created_user_id, text) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'text = ?',
'iiss',
$questId,
$userId,
$text,
$text
);
// Count fields
$listCount = substr_count($text, '[choiceinput]');
// Remove fields
$this->db->query(
'DELETE FROM questtypes_choiceinput_lists '.
'WHERE questtypes_choiceinput_quest_id = ? AND number > ?',
'ii',
$questId,
$listCount
);
// Add fields
for($i=1; $i<=$listCount; $i++)
{
$this->db->query(
'INSERT IGNORE INTO questtypes_choiceinput_lists '.
'(questtypes_choiceinput_quest_id, number, questtypes_choiceinput_choice_id) '.
'VALUES '.
'(?, ?, NULL) ',
'ii',
$questId,
$i
);
}
$this->db->commit();
}
catch(\Exception $e) {
$this->db->rollback();
$this->db->setAutocommit(true);
throw $e;
}
$this->db->setAutocommit(true);
}
/**
* Set list of choices for a text.
*
* @param int $questId ID of Quest to set choices for
* @param int $number List number
* @param array $choices List of choices
* @param int $correctPos Position of correct answer
*/
public function setListForText($questId, $number, $choices, $correctPos)
{
// Get ID of list
$listId = $this->db->query(
'SELECT id '.
'FROM questtypes_choiceinput_lists '.
'WHERE questtypes_choiceinput_quest_id = ? AND number = ?',
'ii',
$questId,
$number
);
$listId = $listId[0]['id'];
// Manage choices
$this->db->setAutocommit(false);
try {
// Remove choices
$this->db->query(
'DELETE FROM questtypes_choiceinput_choices '.
'WHERE questtypes_choiceinput_list_id = ? AND pos > ?',
'ii',
$listId,
count($choices)
);
// Add choices
foreach($choices as $index => &$choice)
{
$this->db->query(
'INSERT INTO questtypes_choiceinput_choices '.
'(questtypes_choiceinput_list_id, pos, text) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'text = ?',
'iiss',
$listId,
$index + 1,
$choice,
$choice
);
}
// Set correct choice for list
$this->db->query(
'UPDATE questtypes_choiceinput_lists '.
'SET questtypes_choiceinput_choice_id = ('.
'SELECT id '.
'FROM questtypes_choiceinput_choices '.
'WHERE questtypes_choiceinput_list_id = ? AND pos = ?'.
') '.
'WHERE id = ?',
'iii',
$listId,
$correctPos,
$listId
);
$this->db->commit();
}
catch(\Exception $e) {
$this->db->rollback();
$this->db->setAutocommit(true);
throw $e;
}
$this->db->setAutocommit(true);
}
}

View file

@ -1 +1,179 @@
<p>TODO</p>
<?php if(!empty($validations)) : ?>
<ul>
<?php foreach($validations as &$list) : ?>
<?php foreach($list as $part => &$choices) : ?>
<li>
<?php if($part == 'choices') : ?>
<?php foreach($choices as $field => &$settings) : ?>
<ul>
<?php foreach($settings as $setting => $value) : ?>
<li>
<?php
switch($setting) {
case 'minlength': printf(_('Choice input is too short (min. %d chars)'), $value);
break;
case 'maxlength': printf(_('Choice input is too long (max. %d chars)'), $value);
break;
default: echo _('Choice input invalid');
}
?>
</li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
<?php elseif($part == 'answer') : ?>
<?php foreach($choices as $setting => &$value) : ?>
<li>
<?php
switch($setting) {
case 'exist': printf(_('Please select correct choice'));
break;
}
?>
</li>
<?php endforeach ?>
<?php endif ?>
</li>
<?php endforeach ?>
<?php endforeach ?>
</ul>
<?php endif ?>
<form method="post">
<fieldset>
<legend><?=_('Text')?></legend>
<button id="add-field" type="button"><?=_('Add field')?></button><br />
<textarea id="text" name="text"><?=$text?></textarea>
</fieldset>
<fieldset>
<legend><?=_('Choice inputs')?></legend>
<ul id="lists">
<?php foreach($choiceLists as $listIndex => &$list) : ?>
<?php if(!empty($validations) && array_key_exists($listIndex, $validations) && !empty($validations[$listIndex]) && array_key_exists('answer', $validations[$listIndex]) && $validations[$listIndex]['answer'] !== true) : ?>
<ul>
<?php foreach($validations[$listIndex]['answer'] as $setting => $value) : ?>
<li>
<?php
switch($setting) {
case 'exist': printf(_('Please select correct choice'));
break;
}
?>
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
<li id="list-<?=$listIndex?>">
<ul>
<?php foreach($list['choices'] as $choiceIndex => &$choice) : ?>
<li>
<input type="radio" name="lists[<?=$listIndex?>][answer]" value="<?=$choiceIndex?>" <?php if($choiceIndex == $list['answer']) : ?>checked="checked"<?php endif ?> />
<input type="text" name="lists[<?=$listIndex?>][choices][<?=$choiceIndex?>]" required="required" value="<?=$choice?>" <?php if(!empty($validations) && array_key_exists($listIndex, $validations) && !empty($validations[$listIndex]) && array_key_exists($choiceIndex, $validations[$listIndex]['choices']) && $validations[$listIndex]['choices'][$choiceInput] !== true) : ?>class="invalid"<?php endif ?> />
<button class="remove-choice" type="button"></button>
</li>
<?php endforeach ?>
<li>
<button class="add-choice" type="button">+</button>
</li>
</ul>
</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($choiceLists as &$list) : ?>
<?php $posEnd = mb_strpos($text, '[choiceinput]', $posStart, 'UTF-8'); ?>
<?=$t->t(mb_substr($text, $posStart, $posEnd-$posStart, 'UTF-8'))?>
<select name="answers[]">
<?php foreach($list['choices'] as &$choice) : ?>
<option><?=$choice?></option>
<?php endforeach ?>
</select>
<?php $posStart = $posEnd + mb_strlen('[choiceinput]', 'UTF-8'); ?>
<?php endforeach ?>
<?=$t->t(mb_substr($text, $posStart, mb_strlen($text, 'UTF-8')-$posStart, 'UTF-8'))?>
</p>
<script>
var listIndex = <?=count($choiceLists)?>;
var choiceIndices = new Array(<?=count($choiceLists)?>);
<?php foreach($choiceLists as $index => &$list) : ?>
choiceIndices[<?=$index?>] = <?=count($list)?>;
<?php endforeach?>
var listElement = '<ul><li><button class="add-choice" type="button">+</button></li></ul>';
var inputElement = '<input type="radio" name="lists[LISTINDEX][answer]" value="CHOICEINDEX" />' +
'<input type="text" name="lists[LISTINDEX][choices][CHOICEINDEX]" required="required" />' +
'<button class="remove-choice" type="button"></button>';
$("#add-field").click(function(event) {
event.preventDefault();
var caret = getCaret("text");
insertAtCaret("text", caret, "[choiceinput]");
updateFields();
});
$("#text").on('change keyup paste', function(event) {
updateFields();
});
$(".add-choice").click(addChoice);
$(".remove-choice").click(removeChoice);
function updateFields()
{
var newCount = $("#text").val().split("[choiceinput]").length - 1;
var oldCount = $("#lists > li").length;
var caret = getCaret("text");
var pos = $("#text").val().substring(0, caret).split("[choiceinput]").length - 1;
if(newCount > oldCount)
{
// Add lists
for(var i=oldCount; i<newCount; i++)
{
choiceIndices.push(0);
var element = '<li id="list-'+listIndex+'">' + listElement + '</li>';
if($("#lists > li").length > 0)
{
if($("#lists > li").length > pos-1) {
$($("#lists > li")[pos-1]).before(element);
}
else {
$($("#lists > li")[pos-2]).after(element);
}
}
else {
$("#lists").append(element);
}
$("#list-"+listIndex+" .add-choice").click(addChoice);
listIndex++;
}
}
else if(newCount < oldCount)
{
// Remove lists
for(var i=oldCount; i>newCount; i--) {
$($("#lists > li")[pos]).remove();
}
}
}
function addChoice(event)
{
event.preventDefault();
var parent = $(event.target).parent();
var index = parent.parent().parent().attr('id').substring(5);
var element = '<li>' + inputElement.replace(/LISTINDEX/g, index).replace(/CHOICEINDEX/g, choiceIndices[index]) + '</li>';
$(event.target).parent().before(element);
$(".remove-choice").click(removeChoice);
choiceIndices[index]++;
}
function removeChoice(event)
{
event.preventDefault();
$(event.target).parent().remove();
}
</script>