implement Questtype ?textinput?

This commit is contained in:
coderkun 2014-03-09 14:35:13 +01:00
commit e8c0035418
4 changed files with 176 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 inserting text.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class TextinputQuesttypeAgent extends \hhu\z\QuesttypeAgent
{
}
?>

View file

@ -0,0 +1,74 @@
<?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 TextinputQuesttypeAgent for for inserting text.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class TextinputQuesttypeController 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)
{
// Check for submission
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit')))
{
// Get right answers
$regexs = $this->Textinput->getTextinputRegexs($questId);
// Match regexs with user answers
$allSolved = true;
for($i=0; $i<count($regexs); $i++)
{
$answer = $this->request->getPostParam('answer-'.strval($i+1));
$score = preg_match($regexs[$i]['regex'], $answer);
if($score === 0 || $score === false) {
$allSolved = false;
}
}
// Set status
if($allSolved) {
$this->setQuestSolved();
}
else {
$this->setQuestUnsolved();
}
}
// Get Task
$task = $this->Textinput->getTextinputQuest($questId);
// Process text
$textParts = preg_split('/(\$\$)/', $task['text'], -1, PREG_SPLIT_NO_EMPTY);
// Pass data to view
$this->set('text', $textParts);
}
}
?>

View file

@ -0,0 +1,67 @@
<?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 TextinputQuesttypeAgent for inserting text.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class TextinputQuesttypeModel extends \hhu\z\QuesttypeModel
{
/**
* Get textinput-text for a Quest.
*
* @param int $questId ID of Quest
* @return array Textinput-text
*/
public function getTextinputQuest($questId)
{
$data = $this->db->query(
'SELECT text '.
'FROM questtypes_textinput '.
'WHERE quest_id = ?',
'i',
$questId
);
return $data[0];
}
/**
* Get regular expressions for a textinput-text.
*
* @param int $questId ID of Quest
* @return array Regexs
*/
public function getTextinputRegexs($questId)
{
return $this->db->query(
'SELECT number, regex '.
'FROM questtypes_textinput_regexs '.
'WHERE questtypes_textinput_quest_id = ? '.
'ORDER BY number ASC',
'i',
$questId
);
}
}
?>

View file

@ -0,0 +1,11 @@
<form method="post">
<?php for($i=0; $i<count($text); $i++) : ?>
<?php if($i > 0) : ?>
<input type="text" name="answer-<?=$i?>" value="" />
<?php endif ?>
<?=\hhu\z\Utils::t($text[$i])?>
<?php endfor ?>
<br /><br />
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>