Questtype ?Textinput?: add support for field sizes (Issue #252) and general improvements

This commit is contained in:
coderkun 2014-05-19 11:36:36 +02:00
commit 8d903135a5
3476 changed files with 599099 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 submitting.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class SubmitQuesttypeAgent extends \hhu\z\agents\QuesttypeAgent
{
}
?>

View file

@ -0,0 +1,227 @@
<?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\controllers\QuesttypeController
{
/**
* Required models
*
* @var array
*/
public $models = array('quests', 'uploads', 'users');
/**
* 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)
{
// Save answer
if(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;
$answer['mimetype'] = \hhu\z\Utils::getMimetype($answer['tmp_name'], $answer['type']);
foreach($mimetypes as &$mimetype) {
if($mimetype['mimetype'] == $answer['mimetype']) {
$answerMimetype = $mimetype;
break;
}
}
if(is_null($answerMimetype)) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\WrongFiletypeException($answer['mimetype'])
);
}
// Check file size
if($answer['size'] > $answerMimetype['size']) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\MaxFilesizeException()
);
}
// Create filename
$filename = sprintf(
'%s,%s,%s.%s',
$character['url'],
mb_substr($quest['url'], 0, 32),
date('Ymd-His'),
mb_substr(mb_substr($answer['name'], strrpos($answer['name'], '.')+1), 0, 4)
);
// Save file
if(!$this->Submit->setCharacterSubmission($seminary['id'], $quest['id'], $this->Auth->getUserId(), $character['id'], $answer, $filename)) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\FileUploadException(error_get_last()['message'])
);
}
}
}
/**
* 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)
{
$this->Submit->addCommentForCharacterAnswer($this->Auth->getUserId(), $data['submission_id'], $data['comment']);
}
/**
* 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)
{
// Answers (Submissions)
$characterSubmissions = $this->Submit->getCharacterSubmissions($quest['id'], $character['id']);
foreach($characterSubmissions as &$submission)
{
$submission['upload'] = $this->Uploads->getSeminaryuploadById($submission['upload_id']);
$submission['comments'] = $this->Submit->getCharacterSubmissionComments($submission['id']);
foreach($submission['comments'] as &$comment)
{
try {
$comment['user'] = $this->Users->getUserById($comment['created_user_id']);
$comment['user']['character'] = $this->Characters->getCharacterForUserAndSeminary($comment['user']['id'], $seminary['id']);
}
catch(\nre\exceptions\IdNotFoundException $e) {
}
}
}
// Show answer of Character
if($this->request->getGetParam('show-answer') == 'true') {
$this->redirect($this->linker->link(array('uploads','seminary',$seminary['url'], $characterSubmissions[count($characterSubmissions)-1]['upload']['url'])));
}
// Has Character already solved Quest?
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
// Last Quest status for Character
$lastStatus = $this->Quests->getLastQuestStatus($quest['id'], $character['id']);
// Get allowed mimetypes
$mimetypes = $this->Submit->getAllowedMimetypes($seminary['id']);
// Pass data to view
$this->set('seminary', $seminary);
$this->set('submissions', $characterSubmissions);
$this->set('solved', $solved);
$this->set('lastStatus', $lastStatus);
$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 submissions
$submissions = $this->Submit->getCharacterSubmissions($quest['id'], $character['id']);
foreach($submissions as &$submission)
{
$submission['upload'] = $this->Uploads->getSeminaryuploadById($submission['upload_id']);
$submission['comments'] = $this->Submit->getCharacterSubmissionComments($submission['id']);
foreach($submission['comments'] as &$comment)
{
try {
$comment['user'] = $this->Users->getUserById($comment['created_user_id']);
$comment['user']['character'] = $this->Characters->getCharacterForUserAndSeminary($comment['user']['id'], $seminary['id']);
}
catch(\nre\exceptions\IdNotFoundException $e) {
}
}
}
// Status
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
// Pass data to view
$this->set('seminary', $seminary);
$this->set('submissions', $submissions);
$this->set('solved', $solved);
}
}
?>

View file

@ -0,0 +1,142 @@
<?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\models\QuesttypeModel
{
/**
* Required models
*
* @var array
*/
public $models = array('uploads');
/**
* Save Characters submitted upload.
*
* @param int $seminaryId ID of Seminary
* @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, $filename)
{
// Save file on harddrive
$uploadId = $this->Uploads->uploadSeminaryFile($userId, $seminaryId, $file['name'], $filename, $file['tmp_name'], $file['type']);
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;
}
/**
* Add a comment for the answer of a Character.
*
* @param int $userId ID of user that comments
* @param int $submissionId ID of Character answer to comment
* @param string $comment Comment text
*/
public function addCommentForCharacterAnswer($userId, $submissionId, $comment)
{
$this->db->query(
'INSERT INTO questtypes_submit_characters_comments '.
'(created_user_id, questtypes_submit_character_id, comment) '.
'VALUES '.
'(?, ?, ?)',
'iis',
$userId,
$submissionId,
$comment
);
}
/**
* Get all uploads 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 getCharacterSubmissions($questId, $characterId)
{
return $this->db->query(
'SELECT id, created, upload_id '.
'FROM questtypes_submit_characters '.
'WHERE quest_id = ? AND character_id = ? '.
'ORDER BY created ASC',
'ii',
$questId, $characterId
);
}
/**
* 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
);
}
/**
* Get all comments for a Character submission.
*
* @param int $characterSubmissionId ID of Character submission
* @return array Comments for this submission
*/
public function getCharacterSubmissionComments($characterSubmissionId)
{
return $this->db->query(
'SELECT id, created, created_user_id, comment '.
'FROM questtypes_submit_characters_comments '.
'WHERE questtypes_submit_character_id = ?',
'i',
$characterSubmissionId
);
}
}
?>

View file

@ -0,0 +1,53 @@
<?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(!$solved && (is_null($lastStatus) || count($submissions) == 0 || $lastStatus['created'] > $submissions[count($submissions)-1]['created'])) : ?>
<form method="post" enctype="multipart/form-data" class="submit">
<input type="file" name="answers" required="required" accept="<?=implode(',', array_map(function($m) { return $m['mimetype']; }, $mimetypes))?>" />
<p><?=_('Allowed file types')?>:</p>
<ul>
<?php foreach($mimetypes as &$mimetype) : ?>
<li><?=sprintf(_('%s-files'), strtoupper(explode('/',$mimetype['mimetype'])[1]))?> <?php if($mimetype['size'] > 0) : ?>(<?=_('max.')?> <?=round($mimetype['size']/(1024*1024),2)?>MiB)<?php endif ?></li>
<?php endforeach ?>
</ul>
<input type="submit" name="submit" value="<?=_('solve')?>" />
</form>
<?php endif ?>
<?php if(count($submissions) > 0) : ?>
<h2><?=_('Your submissions')?></h2>
<ol class="admnql">
<?php foreach($submissions as $index => &$submission) : ?>
<li>
<a href="<?=$linker->link(array('uploads','seminary',$seminary['url'], $submission['upload']['url']))?>"><?=$submission['upload']['name']?></a><span><?=sprintf(_('submitted at %s on %sh'), $dateFormatter->format(new \DateTime($submission['created'])), $timeFormatter->format(new \DateTime($submission['created'])))?></span>
<?php if($lastStatus['status'] == 1 && ($index > 0 || count($submissions) == 1)) : ?>
<p><?=_('This submission is waiting for approval')?></p>
<?php endif ?>
<?php if(count($submission['comments']) > 0) : ?>
<ol>
<?php foreach($submission['comments'] as &$comment) : ?>
<li>
<?php if(array_key_exists('user', $comment) && array_key_exists('character', $comment['user'])) : ?>
<p class="fwb"><?=sprintf(_('Approved on %s at %s'), $dateFormatter->format(new \DateTime($comment['created'])), $timeFormatter->format(new \DateTime($comment['created'])))?></p>
<?php endif ?>
<?php if(!empty($comment['comment'])) : ?>
<p><?=\hhu\z\Utils::t($comment['comment'])?></p>
<?php endif ?>
</li>
<?php endforeach ?>
</ol>
<?php endif ?>
</li>
<?php endforeach ?>
</ol>
<?php endif ?>

View file

@ -0,0 +1,33 @@
<?php if(count($submissions) > 0) : ?>
<ol class="admnql">
<?php foreach($submissions as &$submission) : ?>
<li>
<p><a href="<?=$linker->link(array('uploads','seminary',$seminary['url'], $submission['upload']['url']))?>"><?=$submission['upload']['name']?></a></p>
<p><small><?=sprintf(_('submitted at %s on %sh'), $dateFormatter->format(new \DateTime($submission['created'])), $timeFormatter->format(new \DateTime($submission['created'])))?></small></p>
<?php if(count($submission['comments']) > 0) : ?>
<ol>
<?php foreach($submission['comments'] as &$comment) : ?>
<li>
<?php if(array_key_exists('user', $comment) && array_key_exists('character', $comment['user'])) : ?>
<p class="fwb"><?=sprintf(_('Approved by %s on %s at %s'), $comment['user']['character']['name'], $dateFormatter->format(new \DateTime($comment['created'])), $timeFormatter->format(new \DateTime($comment['created'])))?></p>
<?php endif ?>
<p><?=\hhu\z\Utils::t($comment['comment'])?></p>
</li>
<?php endforeach ?>
</ol>
<?php endif ?>
</li>
<?php endforeach ?>
</ol>
<?php endif ?>
<form method="post" class="logreg">
<?php $submission = array_pop($submissions); ?>
<?php if(!$solved) : ?>
<?=_('Comment')?><br />
<textarea name="characterdata[comment]"></textarea><br />
<input type="hidden" name="characterdata[submission_id]" value="<?=$submission['id']?>" />
<input type="submit" name="submit" value="<?=_('solved')?>" />
<input type="submit" name="submit" value="<?=_('unsolved')?>" />
<?php endif ?>
</form>