implement Questtype ?Multiple choice?

This commit is contained in:
coderkun 2014-03-12 01:29:11 +01:00
commit 5461a4d937
4 changed files with 187 additions and 0 deletions

View file

@ -0,0 +1,24 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\questtypes;
/**
* QuesttypeAgent for multiple choice.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class MultiplechoiceQuesttypeAgent extends \hhu\z\QuesttypeAgent
{
}
?>

View file

@ -0,0 +1,88 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\questtypes;
/**
* Controller of the MultiplechoiceQuesttypeAgent multiple choice.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class MultiplechoiceQuesttypeController extends \hhu\z\QuesttypeController
{
/**
* Action: index.
*
* Display a text with input fields and evaluate if user input
* matches with stored regular expressions.
*/
public function index($questId)
{
// Answers
if(!array_key_exists('answers', $_SESSION)) {
$_SESSION['answers'] = array();
}
$answers = array_key_exists($questId, $_SESSION['answers']) ? $_SESSION['answers'][$questId] : array();
// Check for submission
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit')))
{
// Get answers
$answers = $this->request->getPostParam('answers');
// Store answers in session
$_SESSION['answers'][$questId] = $answers;
// Get right answers
$tickQuestions = $this->Multiplechoice->getTickQuestionsOfQuest($questId);
// Match tick questions with user answers
$allSolved = true;
foreach($tickQuestions as &$tickQuestion)
{
$pos = intval($tickQuestion['pos'])-1;
if(!array_key_exists($pos, $answers) && $answers[$pos] == 'true')
{
$allSolved = false;
break;
}
else {
unset($answers[$pos]);
}
}
// Set status
if($allSolved && count($answers) == 0) {
$this->setQuestSolved();
}
else {
$this->setQuestUnsolved();
}
}
// Get questions
$questions = $this->Multiplechoice->getQuestionsOfQuest($questId);
// Pass data to view
$this->set('questions', $questions);
$this->set('answers', $answers);
}
}
?>

View file

@ -0,0 +1,64 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @copyright 2014 Heinrich-Heine-Universität Düsseldorf
* @license http://www.gnu.org/licenses/gpl.html
* @link https://bitbucket.org/coderkun/the-legend-of-z
*/
namespace hhu\z\questtypes;
/**
* Model of the MultiplechoiceQuesttypeAgent for multiple choice.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class MultiplechoiceQuesttypeModel extends \hhu\z\QuesttypeModel
{
/**
* Get all multiple choice questions of a Quest.
*
* @param int $questId ID of Quest
* @return array Multiple choice questions
*/
public function getQuestionsOfQuest($questId)
{
return $this->db->query(
'SELECT question, tick '.
'FROM questtypes_multiplechoice '.
'WHERE quest_id = ?',
'i',
$questId
);
}
/**
* Get all multiple choice questions of a Quest that should be
* ticked.
*
* @param int $questId ID of Quest
* @return array Multiple choice questions that should be ticked
*/
public function getTickQuestionsOfQuest($questId)
{
return $this->db->query(
'SELECT question, tick, pos '.
'FROM questtypes_multiplechoice '.
'WHERE quest_id = ? AND tick = True',
'i',
$questId
);
}
}
?>

View file

@ -0,0 +1,11 @@
<form method="post">
<ol>
<?php foreach($questions as $i => &$question) : ?>
<li>
<input type="checkbox" id="answers[<?=$i?>]" name="answers[<?=$i?>]" value="true" <?=(array_key_exists($i, $answers)) ? 'checked="checked' : '' ?> />
<label for="answers[<?=$i?>]"><?=\hhu\z\Utils::t($question['question'])?></label>
</li>
<?php endforeach ?>
</ol>
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>