hide map for Character groups Quest Stations when there are no stations
This commit is contained in:
commit
df14dfafc3
4371 changed files with 1220224 additions and 0 deletions
24
questtypes/choiceinput/ChoiceinputQuesttypeAgent.inc
Normal file
24
questtypes/choiceinput/ChoiceinputQuesttypeAgent.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 choosing between predefined input values.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class ChoiceinputQuesttypeAgent extends \hhu\z\agents\QuesttypeAgent
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
||||
299
questtypes/choiceinput/ChoiceinputQuesttypeController.inc
Normal file
299
questtypes/choiceinput/ChoiceinputQuesttypeController.inc
Normal file
|
|
@ -0,0 +1,299 @@
|
|||
<?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 ChoiceinputQuesttypeAgent for choosing between
|
||||
* predefined input values.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class ChoiceinputQuesttypeController extends \hhu\z\controllers\QuesttypeController
|
||||
{
|
||||
/**
|
||||
* Required components
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $components = array('validation');
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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 lists
|
||||
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
|
||||
|
||||
// Save answers
|
||||
foreach($choiceLists as &$list)
|
||||
{
|
||||
$pos = intval($list['number']) - 1;
|
||||
$answer = (array_key_exists($pos, $answers)) ? $answers[$pos] : null;
|
||||
$this->Choiceinput->setCharacterSubmission($list['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 lists
|
||||
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
|
||||
|
||||
// Match lists with user answers
|
||||
foreach($choiceLists as &$list)
|
||||
{
|
||||
$pos = intval($list['number']) - 1;
|
||||
if(!array_key_exists($pos, $answers)) {
|
||||
return false;
|
||||
}
|
||||
if($list['questtypes_choiceinput_choice_id'] != $answers[$pos]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// All answers right
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Action: quest.
|
||||
*
|
||||
* Display a text with lists with predefined values.
|
||||
*
|
||||
* @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->Choiceinput->getChoiceinputQuest($quest['id']);
|
||||
|
||||
// Get lists
|
||||
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
|
||||
foreach($choiceLists as &$list) {
|
||||
$list['values'] = $this->Choiceinput->getChoiceinputChoices($list['id']);
|
||||
}
|
||||
|
||||
// Get Character answers
|
||||
if($this->request->getGetParam('show-answer') == 'true' || !$this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']) || $this->request->getGetParam('status') == 'solved') {
|
||||
foreach($choiceLists as &$list) {
|
||||
$list['answer'] = $this->Choiceinput->getCharacterSubmission($list['id'], $character['id']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('task', $task);
|
||||
$this->set('choiceLists', $choiceLists);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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->Choiceinput->getChoiceinputQuest($quest['id']);
|
||||
|
||||
// Get lists
|
||||
$choiceLists = $this->Choiceinput->getChoiceinputLists($quest['id']);
|
||||
foreach($choiceLists as &$list)
|
||||
{
|
||||
$list['values'] = $this->Choiceinput->getChoiceinputChoices($list['id']);
|
||||
$list['answer'] = $this->Choiceinput->getCharacterSubmission($list['id'], $character['id']);
|
||||
$list['right'] = ($list['questtypes_choiceinput_choice_id'] == $list['answer']);
|
||||
}
|
||||
|
||||
|
||||
// Pass data to view
|
||||
$this->set('task', $task);
|
||||
$this->set('choiceLists', $choiceLists);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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->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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
379
questtypes/choiceinput/ChoiceinputQuesttypeModel.inc
Normal file
379
questtypes/choiceinput/ChoiceinputQuesttypeModel.inc
Normal file
|
|
@ -0,0 +1,379 @@
|
|||
<?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 ChoiceinputQuesttypeAgent for choosing between
|
||||
* predefined input values.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class ChoiceinputQuesttypeModel 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 choiceinput
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_choiceinput '.
|
||||
'(quest_id, created_user_id, text) '.
|
||||
'SELECT ?, ?, text '.
|
||||
'FROM questtypes_choiceinput '.
|
||||
'WHERE quest_id = ?',
|
||||
'iii',
|
||||
$targetQuestId, $userId,
|
||||
$sourceQuestId
|
||||
);
|
||||
|
||||
// Copy lists
|
||||
$lists = $this->getChoiceinputLists($sourceQuestId);
|
||||
foreach($lists as &$list)
|
||||
{
|
||||
// Copy list
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_choiceinput_lists '.
|
||||
'(questtypes_choiceinput_quest_id, number) '.
|
||||
'SELECT ?, number '.
|
||||
'FROM questtypes_choiceinput_lists '.
|
||||
'WHERE id = ?',
|
||||
'ii',
|
||||
$targetQuestId,
|
||||
$list['id']
|
||||
);
|
||||
$targetListId = $this->db->getInsertId();
|
||||
|
||||
// Copy choices
|
||||
$choiceIds = array();
|
||||
$choices = $this->getChoiceinputChoices($list['id']);
|
||||
foreach($choices as $choice)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_choiceinput_choices '.
|
||||
'(questtypes_choiceinput_list_id, pos, text) '.
|
||||
'SELECT ?, pos, text '.
|
||||
'FROM questtypes_choiceinput_choices '.
|
||||
'WHERE id = ?',
|
||||
'ii',
|
||||
$targetListId,
|
||||
$choice['id']
|
||||
);
|
||||
$choiceIds[$choice['id']] = $this->db->getInsertId();
|
||||
}
|
||||
|
||||
// Set correct choice
|
||||
if(!is_null($list['questtypes_choiceinput_choice_id']))
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE questtypes_choiceinput_lists '.
|
||||
'SET questtypes_choiceinput_choice_id = ? '.
|
||||
'WHERE id = ?',
|
||||
'ii',
|
||||
$choiceIds[$list['questtypes_choiceinput_choice_id']],
|
||||
$targetListId
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to delete
|
||||
*/
|
||||
public function deleteQuest($questId)
|
||||
{
|
||||
$this->db->query('DELETE FROM questtypes_choiceinput WHERE quest_id = ?', 'i', $questId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get choiceinput-text for a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest
|
||||
* @return array Choiceinput-text
|
||||
*/
|
||||
public function getChoiceinputQuest($questId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT text '.
|
||||
'FROM questtypes_choiceinput '.
|
||||
'WHERE quest_id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all lists of input values for a choiceinput-text.
|
||||
*
|
||||
* @param int $questId ID of Quest
|
||||
* @return array List
|
||||
*/
|
||||
public function getChoiceinputLists($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, number, questtypes_choiceinput_choice_id '.
|
||||
'FROM questtypes_choiceinput_lists '.
|
||||
'WHERE questtypes_choiceinput_quest_id = ? '.
|
||||
'ORDER BY number ASC',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the list of values for a choiceinput-list.
|
||||
*
|
||||
* @param int $listId ID of list
|
||||
* @return array Input values
|
||||
*/
|
||||
public function getChoiceinputChoices($listId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, pos, text '.
|
||||
'FROM questtypes_choiceinput_choices '.
|
||||
'WHERE questtypes_choiceinput_list_id = ? '.
|
||||
'ORDER BY pos ASC',
|
||||
'i',
|
||||
$listId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save Character’s submitted answer for one choiceinput-list.
|
||||
*
|
||||
* @param int $regexId ID of list
|
||||
* @param int $characterId ID of Character
|
||||
* @param string $answer Submitted answer for this list
|
||||
*/
|
||||
public function setCharacterSubmission($listId, $characterId, $answer)
|
||||
{
|
||||
if(is_null($answer))
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_choiceinput_lists_characters '.
|
||||
'(questtypes_choiceinput_list_id, character_id, questtypes_choiceinput_choice_id) '.
|
||||
'VALUES '.
|
||||
'(?, ?, NULL) '.
|
||||
'ON DUPLICATE KEY UPDATE '.
|
||||
'questtypes_choiceinput_choice_id = NULL',
|
||||
'ii',
|
||||
$listId, $characterId
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO questtypes_choiceinput_lists_characters '.
|
||||
'(questtypes_choiceinput_list_id, character_id, questtypes_choiceinput_choice_id) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) '.
|
||||
'ON DUPLICATE KEY UPDATE '.
|
||||
'questtypes_choiceinput_choice_id = ?',
|
||||
'iiii',
|
||||
$listId, $characterId, $answer, $answer
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get answer of one choiceinput-list submitted by Character.
|
||||
*
|
||||
* @param int $regexId ID of list
|
||||
* @param int $characterId ID of Character
|
||||
* @return int Submitted answer for this list or null
|
||||
*/
|
||||
public function getCharacterSubmission($listId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT questtypes_choiceinput_choice_id '.
|
||||
'FROM questtypes_choiceinput_lists_characters '.
|
||||
'WHERE questtypes_choiceinput_list_id = ? AND character_id = ? ',
|
||||
'ii',
|
||||
$listId, $characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['questtypes_choiceinput_choice_id'];
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
182
questtypes/choiceinput/html/edittask.tpl
Normal file
182
questtypes/choiceinput/html/edittask.tpl
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
<?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) : ?>
|
||||
<li id="list-<?=$listIndex?>">
|
||||
<?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 ?>
|
||||
<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>
|
||||
$(function() {
|
||||
$("#text").markItUp(mySettings);
|
||||
});
|
||||
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>
|
||||
17
questtypes/choiceinput/html/quest.tpl
Normal file
17
questtypes/choiceinput/html/quest.tpl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<form method="post">
|
||||
<p>
|
||||
<?php $posStart = 0; ?>
|
||||
<?php foreach($choiceLists as &$list) : ?>
|
||||
<?php $posEnd = mb_strpos($task['text'], '[choiceinput]', $posStart, 'UTF-8'); ?>
|
||||
<?=$t->t(mb_substr($task['text'], $posStart, $posEnd-$posStart, 'UTF-8'))?>
|
||||
<select name="answers[]">
|
||||
<?php foreach($list['values'] as &$choice) : ?>
|
||||
<option value="<?=$choice['id']?>" <?php if(array_key_exists('answer', $list) && $list['answer'] == $choice['id']) : ?>selected="selected"<?php endif ?>><?=$choice['text']?></option>
|
||||
<?php endforeach ?>
|
||||
</select>
|
||||
<?php $posStart = $posEnd + mb_strlen('[choiceinput]', 'UTF-8'); ?>
|
||||
<?php endforeach ?>
|
||||
<?=$t->t(mb_substr($task['text'], $posStart, mb_strlen($task['text'], 'UTF-8')-$posStart, 'UTF-8'))?>
|
||||
</p>
|
||||
<input type="submit" name="submit" value="<?=_('solve')?>" />
|
||||
</form>
|
||||
14
questtypes/choiceinput/html/submission.tpl
Normal file
14
questtypes/choiceinput/html/submission.tpl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<p>
|
||||
<?php $posStart = 0; ?>
|
||||
<?php foreach($choiceLists as &$list) : ?>
|
||||
<?php $posEnd = mb_strpos($task['text'], '[choiceinput]', $posStart, 'UTF-8'); ?>
|
||||
<?=$t->t(mb_substr($task['text'], $posStart, $posEnd-$posStart, 'UTF-8'))?>
|
||||
<select name="answers[]" disabled="disabled">
|
||||
<?php foreach($list['values'] as &$choice) : ?>
|
||||
<option value="<?=$choice['id']?>" <?php if(array_key_exists('answer', $list) && $list['answer'] == $choice['id']) : ?>selected="selected"<?php endif ?>><?=$choice['text']?></option>
|
||||
<?php endforeach ?>
|
||||
</select>
|
||||
<?php $posStart = $posEnd + mb_strlen('[choiceinput]', '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