merge
This commit is contained in:
commit
046a724272
4209 changed files with 1186656 additions and 0 deletions
24
questtypes/textinput/TextinputQuesttypeAgent.inc
Normal file
24
questtypes/textinput/TextinputQuesttypeAgent.inc
Normal 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\agents\QuesttypeAgent
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
||||
334
questtypes/textinput/TextinputQuesttypeController.inc
Normal file
334
questtypes/textinput/TextinputQuesttypeController.inc
Normal file
|
|
@ -0,0 +1,334 @@
|
|||
<?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\controllers\QuesttypeController
|
||||
{
|
||||
/**
|
||||
* Required components
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $components = array('validation', 'questtypedata');
|
||||
/**
|
||||
* Count of correct answers
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
const KEY_QUESTTYPEDATA_N_CORRECT_ANSWRES = 'nCorrectAnswers';
|
||||
|
||||
private $nCorrectAnswers = null;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Save the answers of a Character for a Quest.
|
||||
*
|
||||
* @param array $seminary Current Seminary data
|
||||
* @param array $questgroup Current Questgroup data
|
||||
* @param array $quest Current Quest data
|
||||
* @param array $character Current Character data
|
||||
* @param array $answers Character answers for the Quest
|
||||
*/
|
||||
public function saveAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers)
|
||||
{
|
||||
// Get fields
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Save answers
|
||||
foreach($fields as &$field)
|
||||
{
|
||||
$pos = intval($field['number']) - 1;
|
||||
$answer = (array_key_exists($pos, $answers)) ? $answers[$pos] : '';
|
||||
$this->Textinput->setCharacterSubmission($field['id'], $character['id'], $answer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save additional data for the answers of a Character for a Quest.
|
||||
*
|
||||
* @param array $seminary Current Seminary data
|
||||
* @param array $questgroup Current Questgroup data
|
||||
* @param array $quest Current Quest data
|
||||
* @param array $character Current Character data
|
||||
* @param array $data Additional (POST-) data
|
||||
*/
|
||||
public function saveDataForCharacterAnswers($seminary, $questgroup, $quest, $character, $data)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if answers of a Character for a Quest match the correct ones.
|
||||
*
|
||||
* @param array $seminary Current Seminary data
|
||||
* @param array $questgroup Current Questgroup data
|
||||
* @param array $quest Current Quest data
|
||||
* @param array $character Current Character data
|
||||
* @param array $answers Character answers for the Quest
|
||||
* @return boolean True/false for a right/wrong answer or null for moderator evaluation
|
||||
*/
|
||||
public function matchAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers)
|
||||
{
|
||||
// Get right answers
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Match regexs with user answers
|
||||
$nCorrectAnswers = 0;
|
||||
foreach($fields as &$field)
|
||||
{
|
||||
$pos = intval($field['number']) - 1;
|
||||
if(!array_key_exists($pos, $answers)) {
|
||||
continue;
|
||||
}
|
||||
if(!$this->isMatching($field['regex'], $answers[$pos])) {
|
||||
continue;
|
||||
}
|
||||
$nCorrectAnswers++;
|
||||
}
|
||||
|
||||
// Save count of correct answers
|
||||
$this->Questtypedata->set(
|
||||
$quest['id'],
|
||||
self::KEY_QUESTTYPEDATA_N_CORRECT_ANSWRES,
|
||||
$nCorrectAnswers
|
||||
);
|
||||
|
||||
|
||||
// All answers right
|
||||
return ($nCorrectAnswers == count($fields));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Action: quest.
|
||||
*
|
||||
* Display a text with input fields and evaluate if user input
|
||||
* matches with stored regular expressions.
|
||||
*
|
||||
* @param array $seminary Current Seminary data
|
||||
* @param array $questgroup Current Questgroup data
|
||||
* @param array $quest Current Quest data
|
||||
* @param array $character Current Character data
|
||||
* @param \Exception $exception Character submission exception
|
||||
*/
|
||||
public function quest($seminary, $questgroup, $quest, $character, $exception)
|
||||
{
|
||||
// Get Task
|
||||
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
||||
|
||||
// Get fields
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Has Character already solved Quest?
|
||||
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
|
||||
|
||||
// Get Character answers
|
||||
if(!$solved || $this->request->getGetParam('show-answer') == 'true' || $this->request->getGetParam('status') == 'solved')
|
||||
{
|
||||
foreach($fields as &$field) {
|
||||
$field['answer'] = $this->Textinput->getCharacterSubmission($field['id'], $character['id']);
|
||||
}
|
||||
}
|
||||
|
||||
// Get count of last correct answers
|
||||
$nCorrectAnswers = $this->Questtypedata->get($quest['id'], self::KEY_QUESTTYPEDATA_N_CORRECT_ANSWRES);
|
||||
$this->Questtypedata->set(
|
||||
$quest['id'],
|
||||
self::KEY_QUESTTYPEDATA_N_CORRECT_ANSWRES,
|
||||
null
|
||||
);
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('task', $task);
|
||||
$this->set('fields', $fields);
|
||||
$this->set('nCorrectAnswers', $nCorrectAnswers);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Action: submission.
|
||||
*
|
||||
* Show the submission of a Character for a Quest.
|
||||
*
|
||||
* @param array $seminary Current Seminary data
|
||||
* @param array $questgroup Current Questgroup data
|
||||
* @param array $quest Current Quest data
|
||||
* @param array $character Current Character data
|
||||
*/
|
||||
public function submission($seminary, $questgroup, $quest, $character)
|
||||
{
|
||||
// Get Task
|
||||
$task = $this->Textinput->getTextinputQuest($quest['id']);
|
||||
|
||||
// Process text
|
||||
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ');
|
||||
|
||||
// Get fields
|
||||
$fields = $this->Textinput->getTextinputFields($quest['id']);
|
||||
|
||||
// Get Character answers
|
||||
foreach($fields as &$field)
|
||||
{
|
||||
$field['answer'] = $this->Textinput->getCharacterSubmission($field['id'], $character['id']);
|
||||
$field['right'] = $this->isMatching($field['regex'], $field['answer']);
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$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 = @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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('task', $task);
|
||||
$this->set('text', $text);
|
||||
$this->set('fields', $fields);
|
||||
$this->set('fieldSizes', $fieldSizes);
|
||||
$this->set('validations', $validations);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Check if an Character answer matches a Regex.
|
||||
*
|
||||
* @param string $regex Regex to match against
|
||||
* @param string $answer Character answer to match
|
||||
* @return boolean Whether answer matches Regex or not
|
||||
*/
|
||||
private function isMatching($regex, $answer)
|
||||
{
|
||||
$score = preg_match($regex, trim($answer));
|
||||
|
||||
|
||||
return ($score !== false && $score > 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
257
questtypes/textinput/TextinputQuesttypeModel.inc
Normal file
257
questtypes/textinput/TextinputQuesttypeModel.inc
Normal file
|
|
@ -0,0 +1,257 @@
|
|||
<?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\models\QuesttypeModel
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Copy a Quest
|
||||
*
|
||||
* @param int $userId ID of creating user
|
||||
* @param int $sourceQuestId ID of Quest to copy from
|
||||
* @param int $targetQuestId ID of Quest to copy to
|
||||
* @param int $seminaryMediaIds Mapping of SeminaryMedia-IDs from source Seminary to targetSeminary
|
||||
*/
|
||||
public function copyQuest($userId, $sourceQuestId, $targetQuestId, $seminaryMediaIds)
|
||||
{
|
||||
// Copy textinput
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_textinput '.
|
||||
'(quest_id, created_user_id, text) '.
|
||||
'SELECT ?, ?, text '.
|
||||
'FROM questtypes_textinput '.
|
||||
'WHERE quest_id = ?',
|
||||
'iii',
|
||||
$targetQuestId, $userId,
|
||||
$sourceQuestId
|
||||
);
|
||||
|
||||
// Copy fields
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_textinput_fields '.
|
||||
'(questtypes_textinput_quest_id, number, questtypes_textinput_fieldsize_id, regex) '.
|
||||
'SELECT ?, number, questtypes_textinput_fieldsize_id, regex '.
|
||||
'FROM questtypes_textinput_fields '.
|
||||
'WHERE questtypes_textinput_quest_id = ?',
|
||||
'ii',
|
||||
$targetQuestId,
|
||||
$sourceQuestId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get fields for a textinput-text.
|
||||
*
|
||||
* @param int $questId ID of Quest
|
||||
* @return array Fields
|
||||
*/
|
||||
public function getTextinputFields($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT fields.id, fields.number, fields.regex, fieldsizes.id AS fieldsize_id, fieldsizes.size '.
|
||||
'FROM questtypes_textinput_fields AS fields '.
|
||||
'LEFT JOIN questtypes_textinput_fieldsizes AS fieldsizes ON fieldsizes.id = fields.questtypes_textinput_fieldsize_id '.
|
||||
'WHERE fields.questtypes_textinput_quest_id = ? '.
|
||||
'ORDER BY fields.number ASC',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save Character’s submitted answer for one textinput field.
|
||||
*
|
||||
* @param int $fieldId ID of field
|
||||
* @param int $characterId ID of Character
|
||||
* @param string $answer Submitted answer for this field
|
||||
*/
|
||||
public function setCharacterSubmission($fieldId, $characterId, $answer)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_textinput_fields_characters '.
|
||||
'(questtypes_textinput_field_id, character_id, value) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) '.
|
||||
'ON DUPLICATE KEY UPDATE '.
|
||||
'value = ?',
|
||||
'iiss',
|
||||
$fieldId, $characterId, $answer, $answer
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get answer of one input field submitted by Character.
|
||||
*
|
||||
* @param int $fieldId ID of field
|
||||
* @param int $characterId ID of Character
|
||||
* @return string Submitted answer for this field or empty string
|
||||
*/
|
||||
public function getCharacterSubmission($fieldId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT value '.
|
||||
'FROM questtypes_textinput_fields_characters '.
|
||||
'WHERE questtypes_textinput_field_id = ? AND character_id = ? ',
|
||||
'ii',
|
||||
$fieldId, $characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['value'];
|
||||
}
|
||||
|
||||
|
||||
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 Quest
|
||||
*/
|
||||
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'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
111
questtypes/textinput/html/edittask.tpl
Normal file
111
questtypes/textinput/html/edittask.tpl
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
<?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" type="button"><?=_('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]" required="required" placeholder="/regex/i" value="<?=$field['regex']?>" <?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]" required="required" placeholder="/regex/i" />';
|
||||
$("#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 > 0)
|
||||
{
|
||||
if($("#fields > li").length > pos-1) {
|
||||
$($("#fields > li")[pos-1]).before(element);
|
||||
}
|
||||
else {
|
||||
$($("#fields > li")[pos-2]).after(element);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$("#fields").append(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(newCount < oldCount)
|
||||
{
|
||||
// Remove fields
|
||||
for(var i=oldCount; i>newCount; i--) {
|
||||
$($("#fields > li")[pos]).remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
16
questtypes/textinput/html/quest.tpl
Normal file
16
questtypes/textinput/html/quest.tpl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<form method="post" class="textinput">
|
||||
<p>
|
||||
<?php $posStart = 0; ?>
|
||||
<?php foreach($fields as &$field) : ?>
|
||||
<?php $posEnd = mb_strpos($task['text'], '[textinput]', $posStart, 'UTF-8'); ?>
|
||||
<?=$t->t(mb_substr($task['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($task['text'], $posStart, mb_strlen($task['text'], 'UTF-8')-$posStart, 'UTF-8'))?>
|
||||
</p>
|
||||
<?php if(!is_null($nCorrectAnswers)) : ?>
|
||||
<p><?=sprintf(_('You filled %d of %d fields correctly'), $nCorrectAnswers, count($fields))?>.</p>
|
||||
<?php endif ?>
|
||||
<input type="submit" name="submit" value="<?=_('solve')?>" />
|
||||
</form>
|
||||
10
questtypes/textinput/html/submission.tpl
Normal file
10
questtypes/textinput/html/submission.tpl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<p>
|
||||
<?php $posStart = 0; ?>
|
||||
<?php foreach($fields as &$field) : ?>
|
||||
<?php $posEnd = mb_strpos($task['text'], '[textinput]', $posStart, 'UTF-8'); ?>
|
||||
<?=$t->t(mb_substr($task['text'], $posStart, $posEnd-$posStart, 'UTF-8'))?>
|
||||
<span style="background-color:grey"><?=$field['answer']?></span><?=($field['right']) ? '✓' : '✕'?>
|
||||
<?php $posStart = $posEnd + mb_strlen('[textinput]', 'UTF-8'); ?>
|
||||
<?php endforeach ?>
|
||||
<?=$t->t(mb_substr($task['text'], $posStart, mb_strlen($task['text'], 'UTF-8')-$posStart, 'UTF-8'))?>
|
||||
</p>
|
||||
Loading…
Add table
Add a link
Reference in a new issue