sub menu design update

This commit is contained in:
Daniel 2014-04-08 19:02:34 +02:00
commit a557703ce7
207 changed files with 18919 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 Drag&Drop.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class DragndropQuesttypeAgent extends \hhu\z\QuesttypeAgent
{
}
?>

View file

@ -0,0 +1,199 @@
<?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 DragndropQuesttypeAgent for Drag&Drop.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class DragndropQuesttypeController extends \hhu\z\QuesttypeController
{
/**
* Required models
*
* @var array
*/
public $models = array('media');
/**
* 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 Drag&Drop field
$dndField = $this->Dragndrop->getDragndrop($quest['id']);
// Get Drops
$drops = $this->Dragndrop->getDrops($dndField['quest_id']);
// Save user answers
foreach($drops as &$drop)
{
// Determine user answer
$answer = null;
if(array_key_exists($drop['id'], $answers) && !empty($answers[$drop['id']]))
{
$a = intval(substr($answers[$drop['id']], 4));
if($a !== false && $a > 0) {
$answer = $a;
}
}
// Update database record
$this->Dragndrop->setCharacterSubmission($drop['id'], $character['id'], $answer);
}
}
/**
* 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
* @return boolean True/false for a right/wrong answer or null for moderator evaluation
*/
public function matchAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers)
{
// Get Drag&Drop field
$dndField = $this->Dragndrop->getDragndrop($quest['id']);
// Get Drops
$drops = $this->Dragndrop->getDrops($dndField['quest_id']);
// Match drops with user answers
foreach($drops as &$drop)
{
if(!array_key_exists($drop['id'], $answers) || intval(substr($answers[$drop['id']], 4)) !== $drop['questtypes_dragndrop_drag_id']) {
return false;
}
}
// Set status
return true;
}
/**
* 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 Drag&Drop field
$dndField = $this->Dragndrop->getDragndrop($quest['id']);
$dndField['media'] = $this->Media->getSeminaryMediaById($dndField['questmedia_id']);
// Get Drags
$drags = array();
$dragsByIndex = $this->Dragndrop->getDrags($dndField['quest_id']);
foreach($dragsByIndex as &$drag) {
$drag['media'] = $this->Media->getSeminaryMediaById($drag['questmedia_id']);
$drags[$drag['id']] = $drag;
}
// Get Drops
$drops = $this->Dragndrop->getDrops($dndField['quest_id']);
// Get Character answers
if($this->request->getGetParam('show-answer') == 'true')
{
foreach($drops as &$drop)
{
$drop['answer'] = $this->Dragndrop->getCharacterSubmission($drop['id'], $character['id']);
if(!is_null($drop['answer']))
{
$drop['answer'] = $drags[$drop['answer']];
unset($drags[$drop['answer']['id']]);
}
}
}
// Pass data to view
$this->set('seminary', $seminary);
$this->set('field', $dndField);
$this->set('drops', $drops);
$this->set('drags', $drags);
}
/**
* 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 Drag&Drop field
$dndField = $this->Dragndrop->getDragndrop($quest['id']);
$dndField['media'] = $this->Media->getSeminaryMediaById($dndField['questmedia_id']);
// Get Drags
$drags = array();
$dragsByIndex = $this->Dragndrop->getDrags($dndField['quest_id']);
foreach($dragsByIndex as &$drag) {
$drag['media'] = $this->Media->getSeminaryMediaById($drag['questmedia_id']);
$drags[$drag['id']] = $drag;
}
// Get Drops
$drops = $this->Dragndrop->getDrops($dndField['quest_id']);
// Get Character answers
foreach($drops as &$drop)
{
$drop['answer'] = $this->Dragndrop->getCharacterSubmission($drop['id'], $character['id']);
if(!is_null($drop['answer']))
{
$drop['answer'] = $drags[$drop['answer']];
unset($drags[$drop['answer']['id']]);
}
}
// Pass data to view
$this->set('seminary', $seminary);
$this->set('field', $dndField);
$this->set('drops', $drops);
$this->set('drags', $drags);
}
}
?>

View file

@ -0,0 +1,146 @@
<?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 DragndropQuesttypeAgent for Drag&Drop.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class DragndropQuesttypeModel extends \hhu\z\QuesttypeModel
{
/**
* Get Drag&Drop-field.
*
* @param int $questId ID of Quest
* @return array Drag&Drop-field
*/
public function getDragndrop($questId)
{
$data = $this->db->query(
'SELECT quest_id, questmedia_id, width, height '.
'FROM questtypes_dragndrop '.
'WHERE quest_id = ?',
'i',
$questId
);
if(!empty($data)) {
return $data[0];
}
return null;
}
/**
* Get Drop-items.
*
* @param int $dragndropId ID of Drag&Drop-field
* @return array Drop-items
*/
public function getDrops($dragndropId)
{
return $this->db->query(
'SELECT id, top, `left`, width, height, questtypes_dragndrop_drag_id '.
'FROM questtypes_dragndrop_drops '.
'WHERE questtypes_dragndrop_id = ?',
'i',
$dragndropId
);
}
/**
* Get Drag-items.
*
* @param int $dragndropId ID of Drag&Drop-field
* @return array Drag-items
*/
public function getDrags($dragndropId)
{
return $this->db->query(
'SELECT id, questmedia_id '.
'FROM questtypes_dragndrop_drags '.
'WHERE questtypes_dragndrop_id = ?',
'i',
$dragndropId
);
}
/**
* Save Characters submitted answer for one Drop-field.
*
* @param int $dropId ID of Drop-field
* @param int $characterId ID of Character
* @param string $answer Submitted Drag-field-ID for this field
*/
public function setCharacterSubmission($dropId, $characterId, $answer)
{
if(is_null($answer))
{
$this->db->query(
'DELETE FROM questtypes_dragndrop_drops_characters '.
'WHERE questtypes_dragndrop_drop_id = ? AND character_id = ?',
'ii',
$dropId, $characterId
);
}
else
{
$this->db->query(
'INSERT INTO questtypes_dragndrop_drops_characters '.
'(questtypes_dragndrop_drop_id, character_id, questtypes_dragndrop_drag_id) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'questtypes_dragndrop_drag_id = ?',
'iiii',
$dropId, $characterId, $answer, $answer
);
}
}
/**
* Get Characters saved answer for one Drop-field.
*
* @param int $dropId ID of Drop-field
* @param int $characterId ID of Character
* @return int ID of Drag-field or null
*/
public function getCharacterSubmission($dropId, $characterId)
{
$data = $this->db->query(
'SELECT questtypes_dragndrop_drag_id '.
'FROM questtypes_dragndrop_drops_characters '.
'WHERE questtypes_dragndrop_drop_id = ? AND character_id = ?',
'ii',
$dropId, $characterId
);
if(!empty($data)) {
return $data[0]['questtypes_dragndrop_drag_id'];
}
return null;
}
}
?>

View file

@ -0,0 +1,17 @@
<form method="post">
<div id="dropZone" style="width:<?=$field['width']?>px; height:<?=$field['height']?>px; background-image:url('<?=$linker->link(array('media','seminary',$seminary['url'],$field['media']['url']))?>')">
<?php foreach($drops as &$drop) : ?>
<div id="drop<?=$drop['id']?>" ondragenter="onDragEnter(event)" ondragover="onDragOver(event)" ondragleave="onDragLeave(event)" ondrop="onDrop(event)" style="position:absolute; width:<?=$drop['width']?>px; height:<?=$drop['height']?>px; margin:<?=$drop['top']?>px 0 0 <?=$drop['left']?>px;"><?php if(array_key_exists('answer', $drop) && !is_null($drop['answer'])) : ?><img id="drag<?=$drop['answer']['id']?>" draggable="true" ondragstart="onDragStart(event)" ondragend="onDragEnd(event)" src="<?=$linker->link(array('media','index',$seminary['url'],$drop['answer']['media']['url']))?>" /><?php endif ?></div>
<input type="hidden" id="dnd_drop<?=$drop['id']?>" name="answers[<?=$drop['id']?>]" value="<?=(!is_null($drop['answer'])) ? 'drag'.$drop['answer']['id'] : null ?>" />
<?php endforeach ?>
</div>
<div id="dragZone" ondragenter="onDragEnter(event)" ondragover="onDragOver(event)" ondragleave="onDragLeave(event)" ondrop="onDrop(event, false)" style="width:100%; min-height:5em;">
<?php foreach($drags as &$drag) : ?>
<img id="drag<?=$drag['id']?>" draggable="true" ondragstart="onDragStart(event)" ondragend="onDragEnd(event)" src="<?=$linker->link(array('media','seminary',$seminary['url'],$drag['media']['url']))?>" />
<?php endforeach ?>
</div>
<br />
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>

View file

@ -0,0 +1,15 @@
<div style="width:<?=$field['width']?>px; height:<?=$field['height']?>px; background-image:url('<?=$linker->link(array('media','seminary',$seminary['url'],$field['media']['url']))?>')">
<?php foreach($drops as &$drop) : ?>
<div id="drop<?=$drop['id']?>" style="position:absolute; width:<?=$drop['width']?>px; height:<?=$drop['height']?>px; margin:<?=$drop['top']?>px 0 0 <?=$drop['left']?>px;">
<?php if(!is_null($drop['answer'])) : ?>
<img id="drag<?=$drop['answer']['id']?>" src="<?=$linker->link(array('media','seminary',$seminary['url'],$drop['answer']['media']['url']))?>" />
<?php endif ?>
</div>
<?php endforeach ?>
</div>
<div style="width:100%; min-height:5em;">
<?php foreach($drags as &$drag) : ?>
<img id="drag<?=$drag['id']?>" src="<?=$linker->link(array('media','seminary',$seminary['url'],$drag['media']['url']))?>" />
<?php endforeach ?>
</div>

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;
/**
* Dummy-QuesttypeAgent for testing basic QuesttypeAgent-functionality.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class DummyQuesttypeAgent extends \hhu\z\QuesttypeAgent
{
}
?>

View file

@ -0,0 +1,93 @@
<?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 Dummy-QuesttypeAgent for testing basic
* QuesttypeAgent-functionality.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class DummyQuesttypeController extends \hhu\z\QuesttypeController
{
/**
* 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)
{
// Do nothing
}
/**
* 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)
{
// Set status
return true;
}
/**
* Action: quest.
*
* Show the task of 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 Exception $exception Character submission exception
*/
public function quest($seminary, $questgroup, $quest, $character, $exception)
{
// Nothing to do
}
/**
* 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)
{
// Nothing to do
}
}
?>

View file

@ -0,0 +1,25 @@
<?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 Dummy-QuesttypeAgent for testing basic
* QuesttypeAgent-functionality.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class DummyQuesttypeModel extends \hhu\z\QuesttypeModel
{
}
?>

View file

@ -0,0 +1,4 @@
<form method="post">
<input type="submit" name="submit" value="richtige Antwort" />
<input type="submit" name="wrong" value="falsche Antwort" />
</form>

View file

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,261 @@
<?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
{
/**
* 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)
{
// Save temporary user answer of last question
$answers = (!is_array($answers)) ? array() : $answers;
$pos = $this->Multiplechoice->getQuestionsCountOfQuest($quest['id']);
$question = $this->Multiplechoice->getQuestionOfQuest($quest['id'], $pos);
$this->saveUserAnswers($quest['id'], $question['id'], $answers);
// Save answers
$questions = $this->Multiplechoice->getQuestionsOfQuest($quest['id']);
foreach($questions as &$question)
{
$userAnswers = $this->getUserAnswers($quest['id'], $question['id']);
$answers = $this->Multiplechoice->getAnswersOfQuestion($question['id']);
foreach($answers as &$answer)
{
$userAnswer = (array_key_exists($answer['pos']-1, $userAnswers)) ? true : false;
$this->Multiplechoice->setCharacterSubmission($answer['id'], $character['id'], $userAnswer);
}
}
}
/**
* 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
* @return boolean True/false for a right/wrong answer or null for moderator evaluation
*/
public function matchAnswersOfCharacter($seminary, $questgroup, $quest, $character, $answers)
{
// Save temporary user answer of last question
$answers = (!is_array($answers)) ? array() : $answers;
$pos = $this->Multiplechoice->getQuestionsCountOfQuest($quest['id']);
$question = $this->Multiplechoice->getQuestionOfQuest($quest['id'], $pos);
$this->saveUserAnswers($quest['id'], $question['id'], $answers);
// Get questions
$questions = $this->Multiplechoice->getQuestionsOfQuest($quest['id']);
// Iterate questions
foreach($questions as &$question)
{
// Get answers
$userAnswers = $this->getUserAnswers($quest['id'], $question['id']);
$answers = $this->Multiplechoice->getAnswersOfQuestion($question['id']);
var_dump($userAnswers);
var_dump($answers);
// Match answers with user answers
foreach($answers as &$answer)
{
if($answer['tick']) {
if(!array_key_exists($answer['pos']-1, $userAnswers)) {
return false;
}
}
else {
if(array_key_exists($answer['pos']-1, $userAnswers)) {
return false;
}
}
}
}
// All questions correct answerd
return true;
}
/**
* Action: quest.
*
* Display questions with a checkbox to let the user choose the
* right 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 Exception $exception Character submission exception
*/
public function quest($seminary, $questgroup, $quest, $character, $exception)
{
// Get count of questions
$count = $this->Multiplechoice->getQuestionsCountOfQuest($quest['id']);
// Get position
$pos = 1;
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit-answer')))
{
if(!is_null($this->request->getPostParam('question')))
{
// Get current position
$pos = intval($this->request->getPostParam('question'));
if($pos < 0 || $pos > $count) {
throw new \nre\exceptions\ParamsNotValidException($pos);
}
// Save temporary answer of user
$question = $this->Multiplechoice->getQuestionOfQuest($quest['id'], $pos);
$answers = ($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('answers'))) ? $this->request->getPostParam('answers') : array();
$this->saveUserAnswers($quest['id'], $question['id'], $answers);
// Go to next position
$pos++;
}
else {
throw new \nre\exceptions\ParamsNotValidException('pos');
}
}
// Get current question
$question = $this->Multiplechoice->getQuestionOfQuest($quest['id'], $pos);
// Get answers
$question['answers'] = $this->Multiplechoice->getAnswersOfQuestion($question['id']);
// Get previous user answers
if($this->request->getGetParam('show-answer') == 'true')
{
foreach($question['answers'] as &$answer) {
$answer['useranswer'] = $this->Multiplechoice->getCharacterSubmission($answer['id'], $character['id']);
}
}
// Pass data to view
$this->set('question', $question);
$this->set('pos', $pos);
$this->set('count', $count);
}
/**
* 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 questions
$questions = $this->Multiplechoice->getQuestionsOfQuest($quest['id']);
// Get answers
foreach($questions as &$question)
{
$question['answers'] = $this->Multiplechoice->getAnswersOfQuestion($question['id']);
// Get user answers
foreach($question['answers'] as &$answer) {
$answer['useranswer'] = $this->Multiplechoice->getCharacterSubmission($answer['id'], $character['id']);
}
}
// Pass data to view
$this->set('questions', $questions);
}
/**
* Save the answers of a user for a question temporary in the
* session.
*
* @param int $questId ID of Quest
* @param int $questionId ID of multiple choice question
* @param array $userAnswers Answers of user for the question
*/
private function saveUserAnswers($questId, $questionId, $userAnswers)
{
// Ensure session structure
if(!array_key_exists('answers', $_SESSION)) {
$_SESSION['answers'] = array();
}
if(!array_key_exists($questId, $_SESSION['answers'])) {
$_SESSION['answers'][$questId] = array();
}
$_SESSION['answers'][$questId][$questionId] = array();
// Save answres
foreach($userAnswers as $pos => &$answer) {
$_SESSION['answers'][$questId][$questionId][$pos] = $answer;
}
}
/**
* Get the temporary saved answers of a user for a question.
*
* @param int $questId ID of Quest
* @param int $questionId ID of multiple choice question
* @return array Answers of user for the question
*/
private function getUserAnswers($questId, $questionId)
{
// Ensure session structure
if(!array_key_exists('answers', $_SESSION)) {
$_SESSION['answers'] = array();
}
if(!array_key_exists($questId, $_SESSION['answers'])) {
$_SESSION['answers'][$questId] = array();
}
if(!array_key_exists($questionId, $_SESSION['answers'][$questId])) {
$_SESSION['answers'][$questId][$questionId] = array();
}
// Return answers
return $_SESSION['answers'][$questId][$questionId];
}
}
?>

View file

@ -0,0 +1,159 @@
<?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 the count of multiple choice questions for a Quest.
*
* @param int $questId ID of Quest to get count for
* @return int Conut of questions
*/
public function getQuestionsCountOfQuest($questId)
{
$data = $this->db->query(
'SELECT count(id) AS c '.
'FROM questtypes_multiplechoice '.
'WHERE quest_id = ?',
'i',
$questId
);
if(!empty($data)) {
return $data[0]['c'];
}
return 0;
}
/**
* 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 id, pos, question '.
'FROM questtypes_multiplechoice '.
'WHERE quest_id = ?',
'i',
$questId
);
}
/**
* Get one multiple choice question of a Quest.
*
* @param int $questId ID of Quest
* @param int $pos Position of question
* @return array Question data
*/
public function getQuestionOfQuest($questId, $pos)
{
$data = $this->db->query(
'SELECT id, pos, question '.
'FROM questtypes_multiplechoice '.
'WHERE quest_id = ? AND pos = ?',
'ii',
$questId, $pos
);
if(!empty($data)) {
return $data[0];
}
return null;
}
/**
* Get all answers of a multiple choice question.
*
* @param int $questionId ID of multiple choice question
* @return array Answers of question
*/
public function getAnswersOfQuestion($questionId)
{
return $this->db->query(
'SELECT id, pos, answer, tick '.
'FROM questtypes_multiplechoice_answers '.
'WHERE questtypes_multiplechoice_id = ?',
'i',
$questionId
);
}
/**
* Save Characters submitted answer for one option.
*
* @param int $answerId ID of multiple choice answer
* @param int $characterId ID of Character
* @param boolean $answer Submitted answer for this option
*/
public function setCharacterSubmission($answerId, $characterId, $answer)
{
$this->db->query(
'INSERT INTO questtypes_multiplechoice_characters '.
'(questtypes_multiplechoice_answer_id, character_id, ticked) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'ticked = ?',
'iiii',
$answerId, $characterId, $answer, $answer
);
}
/**
* Get answer of one option submitted by Character.
*
* @param int $answerId ID of multiple choice answer
* @param int $characterId ID of Character
* @return boolean Submitted answer of Character or false
*/
public function getCharacterSubmission($answerId, $characterId)
{
$data = $this->db->query(
'SELECT ticked '.
'FROM questtypes_multiplechoice_characters '.
'WHERE questtypes_multiplechoice_answer_id = ? AND character_id = ? ',
'ii',
$answerId, $characterId
);
if(!empty($data)) {
return $data[0]['ticked'];
}
return false;
}
}
?>

View file

@ -0,0 +1,21 @@
<form method="post">
<fieldset>
<legend><?=sprintf(_('Question %d of %d'), $pos, $count)?></legend>
<p><?=\hhu\z\Utils::t($question['question'])?></p>
<ol>
<?php foreach($question['answers'] as $i => &$answer) : ?>
<li>
<input type="checkbox" id="answers[<?=$i?>]" name="answers[<?=$i?>]" value="true" <?=(array_key_exists('useranswer', $answer) && $answer['useranswer']) ? 'checked="checked"' : '' ?> />
<label for="answers[<?=$i?>]"><?=\hhu\z\Utils::t($answer['answer'])?></label>
</li>
<?php endforeach ?>
</ol>
</fieldset>
<input type="hidden" name="question" value="<?=$pos?>" />
<?php if($pos < $count) : ?>
<input type="submit" name="submit-answer" value="<?=_('solve Question')?>" />
<?php else : ?>
<input type="submit" name="submit" value="<?=_('solve')?>" />
<?php endif ?>
</form>

View file

@ -0,0 +1,16 @@
<ol>
<?php foreach($questions as $pos => &$question) : ?>
<li>
<h1><?=\hhu\z\Utils::t($question['question'])?></h1>
<ol>
<?php foreach($question['answers'] as &$answer) : ?>
<li>
<?php if($answer['useranswer']) : ?>☑<?php else : ?>☐<?php endif ?>
<?php if($answer['useranswer'] == $answer['tick']) : ?>✓<?php else : ?>×<?php endif ?>
<?=$answer['answer']?>
</li>
<?php endforeach ?>
</ol>
</li>
<?php endforeach ?>
</ol>

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 submitting.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class SubmitQuesttypeAgent extends \hhu\z\QuesttypeAgent
{
}
?>

View file

@ -0,0 +1,166 @@
<?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 SubmitQuesttypeAgent for a submit task.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class SubmitQuesttypeController extends \hhu\z\QuesttypeController
{
/**
* Required models
*
* @var array
*/
public $models = array('quests');
/**
* Save the answers of a Character for a Quest.
*
* @throws SubmissionNotValidException
* @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 already submitted answer
$characterSubmission = $this->Submit->getCharacterSubmission($quest['id'], $character['id']);
// Save answer
if(is_null($characterSubmission) && array_key_exists('answers', $_FILES))
{
$answer = $_FILES['answers'];
// Check error
if($answer['error'] !== 0) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\FileUploadException($answer['error'])
);
}
// Check mimetype
$mimetypes = $this->Submit->getAllowedMimetypes($seminary['id']);
$answerMimetype = null;
foreach($mimetypes as &$mimetype) {
if($mimetype['mimetype'] == $answer['type']) {
$answerMimetype = $mimetype;
break;
}
}
if(is_null($answerMimetype)) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\WrongFiletypeException($answer['type'])
);
}
// Check file size
if($answer['size'] > $answerMimetype['size']) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\MaxFilesizeException()
);
}
// Save file
if(!$this->Submit->setCharacterSubmission($seminary['id'], $quest['id'], $this->Auth->getUserId(), $character['id'], $answer)) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\FileUploadException(error_get_last()['message'])
);
}
}
}
/**
* 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)
{
// A moderator has to evaluate the answer
return null;
}
/**
* Action: quest.
*
* Display a big textbox to let the user enter a text that has
* to be evaluated by a moderator.
*
* @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)
{
// Answer (Submission)
$characterSubmission = $this->Submit->getCharacterSubmission($quest['id'], $character['id']);
// Has Character already solved Quest?
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
// Get allowed mimetypes
$mimetypes = $this->Submit->getAllowedMimetypes($seminary['id']);
// Pass data to view
$this->set('submission', $characterSubmission);
$this->set('solved', $solved);
$this->set('mimetypes', $mimetypes);
$this->set('exception', $exception);
}
/**
* 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 Character submission
$submission = $this->Submit->getCharacterSubmission($quest['id'], $character['id']);
// Status
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
// Pass data to view
$this->set('submission', $submission);
$this->set('solved', $solved);
}
}
?>

View file

@ -0,0 +1,106 @@
<?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 SubmitQuesttypeAgent for a submit task.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class SubmitQuesttypeModel extends \hhu\z\QuesttypeModel
{
/**
* Required models
*
* @var array
*/
public $models = array('uploads');
/**
* Save Characters submitted upload.
*
* @param int $questId ID of Quest
* @param int $characterId ID of Character
* @param array $file Submitted upload
*/
public function setCharacterSubmission($seminaryId, $questId, $userId, $characterId, $file)
{
// Save file on harddrive
$uploadId = $this->Uploads->uploadFile($userId, $file['name'], $file['tmp_name'], $file['type'], $seminaryId);
if($uploadId === false) {
return false;
}
// Create database record
$this->db->query(
'INSERT INTO questtypes_submit_characters '.
'(quest_id, character_id, upload_id) '.
'VALUES '.
'(?, ?, ?) ',
'iii',
$questId, $characterId, $uploadId
);
return true;
}
/**
* Get upload submitted by Character.
*
* @param int $questId ID of Quest
* @param int $characterId ID of Character
* @return array Text submitted by Character or NULL
*/
public function getCharacterSubmission($questId, $characterId)
{
$data = $this->db->query(
'SELECT upload_id '.
'FROM questtypes_submit_characters '.
'WHERE quest_id = ? AND character_id = ?',
'ii',
$questId, $characterId
);
if(!empty($data)) {
return $this->Uploads->getUploadById($data[0]['upload_id']);
}
return null;
}
/**
* Get allowed mimetypes for uploading a file.
*
* @param int $seminaryId ID of Seminary
* @return array Allowed mimetypes
*/
public function getAllowedMimetypes($seminaryId)
{
return $this->db->query(
'SELECT id, mimetype, size '.
'FROM questtypes_submit_mimetypes '.
'WHERE seminary_id = ?',
'i',
$seminaryId
);
}
}
?>

View file

@ -0,0 +1,27 @@
<?php if(!is_null($exception)) : ?>
<p class="error">
<?php if($exception->getNestedException() instanceof \hhu\z\exceptions\WrongFiletypeException) : ?>
<?=sprintf(_('File has wrong type “%s”'), $exception->getNestedException()->getType())?>
<?php elseif($exception->getNestedException() instanceof \hhu\z\exceptions\WrongFiletypeException) : ?>
<?=_('File exceeds size maximum')?>
<?php elseif($exception->getNestedException() instanceof \hhu\z\exceptions\FileUploadException) : ?>
<?=sprintf(_('Error during file upload: %s'), $exception->getNestedException()->getNestedMessage())?>
<?php else : ?>
<?=$exception->getNestedException()->getMessage()?>
<?php endif ?>
</p>
<?php endif ?>
<?php if(is_null($submission)) : ?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="answers" required="required" accept="<?=implode(',', array_map(function($m) { return $m['mimetype']; }, $mimetypes))?>" /><br />
<?=_('Allowed file types')?>:
<ul>
<?php foreach($mimetypes as &$mimetype) : ?>
<li><?=$mimetype['mimetype']?> (<?=_('max.')?> <?=$numberFormatter->format(round($mimetype['size']/(1024*1024),2))?>MiB)</li>
<?php endforeach ?>
</ul>
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>
<?php else : ?>
<a href="<?=$linker->link(array('uploads','index',$submission['url']))?>"><?=$submission['name']?></a> (<?=sprintf(_('submitted at %s on %sh'), $dateFormatter->format(new \DateTime($submission['created'])), $timeFormatter->format(new \DateTime($submission['created'])))?>)
<?php endif ?>

View file

@ -0,0 +1,11 @@
<form method="post">
<a href="<?=$linker->link(array('uploads','index',$submission['url']))?>"><?=$submission['name']?></a> (<?=sprintf(_('submitted at %s on %sh'), $dateFormatter->format(new \DateTime($submission['created'])), $timeFormatter->format(new \DateTime($submission['created'])))?>)
<br /><br />
<?php if($solved) : ?>
<?=_('solved')?>
<?php else : ?>
<input type="submit" name="submit" value="<?=_('solved')?>" />
<input type="submit" name="submit" value="<?=_('unsolved')?>" />
<?php endif ?>
</form>

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,171 @@
<?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
{
/**
* 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 regexs
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
// Save answers
foreach($regexs as &$regex)
{
$pos = intval($regex['number']) - 1;
$answer = (array_key_exists($pos, $answers)) ? $answers[$pos] : '';
$this->Textinput->setCharacterSubmission($regex['id'], $character['id'], $answer);
}
}
/**
* 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
* @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
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
// Match regexs with user answers
$allSolved = true;
foreach($regexs as $i => &$regex)
{
if(!array_key_exists($i, $answers))
{
$allSolved = false;
break;
}
if(!$this->isMatching($regex['regex'], $answers[$i]))
{
$allSolved = false;
break;
}
}
// Set status
return $allSolved;
}
/**
* 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']);
// Process text
$textParts = preg_split('/(\$\$)/', ' '.$task['text'].' ', -1, PREG_SPLIT_NO_EMPTY);
// Get Character answers
$regexs = null;
if($this->request->getGetParam('show-answer') == 'true')
{
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
foreach($regexs as &$regex) {
$regex['answer'] = $this->Textinput->getCharacterSubmission($regex['id'], $character['id']);
}
}
// Has Character already solved Quest?
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
// Pass data to view
$this->set('texts', $textParts);
$this->set('regexs', $regexs);
}
/**
* 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'], -1, PREG_SPLIT_NO_EMPTY);
// Get Character answers
$regexs = $this->Textinput->getTextinputRegexs($quest['id']);
foreach($regexs as &$regex) {
$regex['answer'] = $this->Textinput->getCharacterSubmission($regex['id'], $character['id']);
$regex['right'] = $this->isMatching($regex['regex'], $regex['answer']);
}
// Pass data to view
$this->set('texts', $textParts);
$this->set('regexs', $regexs);
}
private function isMatching($regex, $answer)
{
$score = preg_match($regex, $answer);
return ($score !== false && $score > 0);
}
}
?>

View file

@ -0,0 +1,117 @@
<?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
);
if(!empty($data)) {
return $data[0];
}
return null;
}
/**
* 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 id, number, regex '.
'FROM questtypes_textinput_regexs '.
'WHERE questtypes_textinput_quest_id = ? '.
'ORDER BY number ASC',
'i',
$questId
);
}
/**
* Save Characters submitted answer for one textinput field.
*
* @param int $regexId ID of regex
* @param int $characterId ID of Character
* @param string $answer Submitted answer for this field
*/
public function setCharacterSubmission($regexId, $characterId, $answer)
{
$this->db->query(
'INSERT INTO questtypes_textinput_regexs_characters '.
'(questtypes_textinput_regex_id, character_id, value) '.
'VALUES '.
'(?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'value = ?',
'iiss',
$regexId, $characterId, $answer, $answer
);
}
/**
* Get answer of one regex input field submitted by Character.
*
* @param int $regexId ID of regex
* @param int $characterId ID of Character
* @return string Submitted answer for this field or empty string
*/
public function getCharacterSubmission($regexId, $characterId)
{
$data = $this->db->query(
'SELECT value '.
'FROM questtypes_textinput_regexs_characters '.
'WHERE questtypes_textinput_regex_id = ? AND character_id = ? ',
'ii',
$regexId, $characterId
);
if(!empty($data)) {
return $data[0]['value'];
}
return '';
}
}
?>

View file

@ -0,0 +1,11 @@
<form method="post">
<?php foreach($texts as $i => &$text) : ?>
<?php if($i > 0) : ?>
<input type="text" name="answers[<?=$i-1?>]" value="<?=$regexs[$i-1]['answer']?>" />
<?php endif ?>
<?=\hhu\z\Utils::t($text)?>
<?php endforeach ?>
<br /><br />
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>

View file

@ -0,0 +1,7 @@
<?php foreach($texts as $i => &$text) : ?>
<?php if($i > 0) : ?>
<span style="background-color:grey"><?=$regexs[$i-1]['answer']?></span>
<?php if($regexs[$i-1]['right']) : ?>✓<?php else: ?>✕<?php endif ?>
<?php endif ?>
<?=\hhu\z\Utils::t($text)?>
<?php endforeach ?>