implement task editing for Stationtype ?multiplechoice?
This commit is contained in:
parent
a1e46ff2aa
commit
457a382317
3 changed files with 173 additions and 0 deletions
|
|
@ -105,6 +105,71 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Action: edittask.
|
||||||
|
*
|
||||||
|
* Edit the task of a Station.
|
||||||
|
*
|
||||||
|
* @param array $seminary Current Seminary data
|
||||||
|
* @param array $groupsgroup Current Groups group data
|
||||||
|
* @param array $quest Current Quest data
|
||||||
|
* @param array $station Current Station data
|
||||||
|
*/
|
||||||
|
public function edittask($seminary, $groupsgroup, $quest, $station)
|
||||||
|
{
|
||||||
|
// Get questions
|
||||||
|
$answers = $this->Multiplechoice->getAnswers($station['id']);
|
||||||
|
|
||||||
|
// Save data
|
||||||
|
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('save')))
|
||||||
|
{
|
||||||
|
// Get params
|
||||||
|
$answers = $this->request->getPostParam('answers');
|
||||||
|
if(is_null($answers)) {
|
||||||
|
$answers = array();
|
||||||
|
}
|
||||||
|
$answers = array_values($answers);
|
||||||
|
|
||||||
|
// Save answers
|
||||||
|
foreach($answers as $answerIndex => &$answer)
|
||||||
|
{
|
||||||
|
$this->Multiplechoice->setAnswer(
|
||||||
|
$this->Auth->getUserId(),
|
||||||
|
$station['id'],
|
||||||
|
$answerIndex + 1,
|
||||||
|
$answer['answer'],
|
||||||
|
array_key_exists('tick', $answer)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete deleted answers
|
||||||
|
$this->Multiplechoice->deleteAnswers(
|
||||||
|
$station['id'],
|
||||||
|
count($answers)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Redirect
|
||||||
|
$this->redirect(
|
||||||
|
$this->linker->link(
|
||||||
|
array(
|
||||||
|
'station',
|
||||||
|
$seminary['url'],
|
||||||
|
$groupsgroup['url'],
|
||||||
|
$quest['url'],
|
||||||
|
$station['url']
|
||||||
|
),
|
||||||
|
1
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Pass data to view
|
||||||
|
$this->set('task', $station['task']);
|
||||||
|
$this->set('answers', $answers);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,55 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set an answer for a Station.
|
||||||
|
*
|
||||||
|
* @param int $userId ID of creating user
|
||||||
|
* @param int $stationId ID of Station to set answer for
|
||||||
|
* @param int $pos Position of answer
|
||||||
|
* @param string $answer Answer text
|
||||||
|
* @param int $tick Whether the answer is correct or not
|
||||||
|
*/
|
||||||
|
public function setAnswer($userId, $stationId, $pos, $answer, $tick)
|
||||||
|
{
|
||||||
|
$this->db->query(
|
||||||
|
'INSERT INTO stationtypes_multiplechoice '.
|
||||||
|
'(created_user_id, station_id, pos, answer, tick) '.
|
||||||
|
'VALUES '.
|
||||||
|
'(?, ?, ?, ?, ?) '.
|
||||||
|
'ON DUPLICATE KEY UPDATE '.
|
||||||
|
'answer = ?, tick = ?',
|
||||||
|
'iiisisi',
|
||||||
|
$userId,
|
||||||
|
$stationId,
|
||||||
|
$pos,
|
||||||
|
$answer,
|
||||||
|
$tick,
|
||||||
|
$answer,
|
||||||
|
$tick
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete all answers of a Station above a given index.
|
||||||
|
*
|
||||||
|
* @param int $stationId ID of Station to delete answers for
|
||||||
|
* @param int $offset Offset to delete answers above
|
||||||
|
*/
|
||||||
|
public function deleteAnswers($stationId, $offset)
|
||||||
|
{
|
||||||
|
// Delete answers
|
||||||
|
$this->db->query(
|
||||||
|
'DELETE FROM stationtypes_multiplechoice '.
|
||||||
|
'WHERE station_id = ? AND pos > ?',
|
||||||
|
'ii',
|
||||||
|
$stationId,
|
||||||
|
$offset
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save Character group’s submitted answer for a station.
|
* Save Character group’s submitted answer for a station.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
59
stationtypes/multiplechoice/html/edittask.tpl
Normal file
59
stationtypes/multiplechoice/html/edittask.tpl
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
<form method="post">
|
||||||
|
<fieldset>
|
||||||
|
<legend><?=_('Question')?></legend>
|
||||||
|
<?=$t->t($task)?>
|
||||||
|
</fieldset>
|
||||||
|
<fieldset>
|
||||||
|
<legend><?=_('Answers')?></legend>
|
||||||
|
<ul id="answers">
|
||||||
|
<?php foreach($answers as $answerIndex => &$answer) : ?>
|
||||||
|
<li>
|
||||||
|
<label>
|
||||||
|
<input id="answer-<?=$answerIndex?>-tick" type="checkbox" name="answers[<?=$answerIndex?>][tick]" <?php if(array_key_exists('tick', $answer) && $answer['tick']) : ?>checked="checked"<?php endif ?> />
|
||||||
|
<?=_('correct answer')?>
|
||||||
|
</label>
|
||||||
|
<textarea id="answer-<?=$answerIndex?>" name="answers[<?=$answerIndex?>][answer]" class="answer">
|
||||||
|
<?=$answer['answer']?>
|
||||||
|
</textarea>
|
||||||
|
<button class="remove-answer" type="button">−</button>
|
||||||
|
<hr />
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
<li>
|
||||||
|
<button class="add-answer" type="button">+</button>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</fieldset>
|
||||||
|
<input type="submit" name="save" value="<?=_('save')?>" />
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// Text editors
|
||||||
|
$('textarea.answer').markItUp(mySettings);
|
||||||
|
|
||||||
|
// Answers
|
||||||
|
var answerIndex = <?=count($answers)?>;
|
||||||
|
var answerElement = '<label><input id="answer-ANSWERINDEX-tick" type="checkbox" name="answers[ANSWERINDEX][tick]" /> <?=_('correct answer')?></label>' +
|
||||||
|
'<textarea id="answer-ANSWERINDEX" name="answers[ANSWERINDEX][answer]" class="answer"></textarea>' +
|
||||||
|
'<button class="remove-answer" type="button">−</button>' +
|
||||||
|
'<hr />';
|
||||||
|
$(".add-answer").click(addAnswer);
|
||||||
|
$(".remove-answer").click(removeAnswer);
|
||||||
|
function addAnswer(event)
|
||||||
|
{
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
var parent = $(event.target).parent();
|
||||||
|
var element = '<li>' + answerElement.replace(/ANSWERINDEX/g, answerIndex) + '</li>';
|
||||||
|
parent.before(element);
|
||||||
|
$("#answers li:nth-last-child(2) .remove-answer").click(removeAnswer);
|
||||||
|
$('#answers li:nth-last-child(2) textarea.answer').markItUp(mySettings);
|
||||||
|
|
||||||
|
answerIndex++;
|
||||||
|
}
|
||||||
|
function removeAnswer(event)
|
||||||
|
{
|
||||||
|
event.preventDefault();
|
||||||
|
$(event.target).parent().remove();
|
||||||
|
}
|
||||||
|
</script>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue