implement uploading a file as answer for Questtype ?Submit?

This commit is contained in:
coderkun 2014-04-04 13:32:31 +02:00
commit 8abf29b2b0
6 changed files with 207 additions and 34 deletions

View file

@ -0,0 +1,51 @@
<?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\exceptions;
/**
* Exception: File exceeds size maximum.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class MaxFilesizeException extends \nre\core\Exception
{
/**
* Error code
*
* @var int
*/
const CODE = 202;
/**
* Error message
*
* @var string
*/
const MESSAGE = 'File exceeds size maximum';
/**
* Construct a new exception.
*/
function __construct($message=self::MESSAGE, $code=self::CODE)
{
parent::__construct(
$message,
$code
);
}
}
?>

View file

@ -0,0 +1,51 @@
<?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\exceptions;
/**
* Exception: File has wrong filetype.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class WrongFiletypeException extends \nre\core\Exception
{
/**
* Error code
*
* @var int
*/
const CODE = 201;
/**
* Error message
*
* @var string
*/
const MESSAGE = 'File has wrong type';
/**
* Construct a new exception.
*/
function __construct($message=self::MESSAGE, $code=self::CODE)
{
parent::__construct(
$message,
$code
);
}
}
?>

View file

@ -32,6 +32,7 @@
/** /**
* Save the answers of a Character for a Quest. * Save the answers of a Character for a Quest.
* *
* @throws SubmissionNotValidException
* @param array $seminary Current Seminary data * @param array $seminary Current Seminary data
* @param array $questgroup Current Questgroup data * @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data * @param array $quest Current Quest data
@ -44,8 +45,34 @@
$characterSubmission = $this->Submit->getCharacterSubmission($quest['id'], $character['id']); $characterSubmission = $this->Submit->getCharacterSubmission($quest['id'], $character['id']);
// Save answer // Save answer
if(is_null($characterSubmission) && array_key_exists(0, $answers)) { if(is_null($characterSubmission) && array_key_exists('answers', $_FILES))
$this->Submit->setCharacterSubmission($quest['id'], $character['id'], $answers[0]); {
$answer = $_FILES['answers'];
// Check mimetype
$mimetypes = $this->Submit->getAllowedMimetypes($seminary['id']);
$answerMimetype = null;
foreach($mimetypes as &$mimetype) {
if($mimetype['mimetype'] == $answer['type']) {
$answerMimetype = $mimetype;
break;
}
}
// Check file
if(is_null($answerMimetype)) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\WrongFiletypeException()
);
}
if($answer['size'] > $answerMimetype['size']) {
throw new \hhu\z\exceptions\SubmissionNotValidException(
new \hhu\z\exceptions\MaxFilesizeException()
);
}
// Save file
$this->Submit->setCharacterSubmission($seminary['id'], $quest['id'], $this->Auth->getUserId(), $character['id'], $answer);
} }
} }
@ -77,26 +104,25 @@
* @param array $questgroup Current Questgroup data * @param array $questgroup Current Questgroup data
* @param array $quest Current Quest data * @param array $quest Current Quest data
* @param array $character Current Character data * @param array $character Current Character data
* @param Exception $exception Character submission exception
*/ */
public function quest($seminary, $questgroup, $quest, $character) public function quest($seminary, $questgroup, $quest, $character, $exception)
{ {
// Answer (Submission) // Answer (Submission)
$characterSubmission = $this->Submit->getCharacterSubmission($quest['id'], $character['id']); $characterSubmission = $this->Submit->getCharacterSubmission($quest['id'], $character['id']);
// Wordcount
$wordcount = 0;
if(!is_null($characterSubmission)) {
$wordcount = count(preg_split('/\s+/', $characterSubmission['text']));
}
// Has Character already solved Quest? // Has Character already solved Quest?
$solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']); $solved = $this->Quests->hasCharacterSolvedQuest($quest['id'], $character['id']);
// Get allowed mimetypes
$mimetypes = $this->Submit->getAllowedMimetypes($seminary['id']);
// Pass data to view // Pass data to view
$this->set('submission', $characterSubmission); $this->set('submission', $characterSubmission);
$this->set('wordcount', $wordcount);
$this->set('solved', $solved); $this->set('solved', $solved);
$this->set('mimetypes', $mimetypes);
$this->set('exception', $exception);
} }

View file

@ -19,32 +19,48 @@
*/ */
class SubmitQuesttypeModel extends \hhu\z\QuesttypeModel class SubmitQuesttypeModel extends \hhu\z\QuesttypeModel
{ {
/**
* Required models
*
* @var array
*/
public $models = array('uploads');
/** /**
* Save Characters submitted text. * Save Characters submitted upload.
* *
* @param int $questId ID of Quest * @param int $questId ID of Quest
* @param int $characterId ID of Character * @param int $characterId ID of Character
* @param string $text Submitted text * @param array $file Submitted upload
*/ */
public function setCharacterSubmission($questId, $characterId, $text) 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( $this->db->query(
'INSERT INTO questtypes_submit_characters '. 'INSERT INTO questtypes_submit_characters '.
'(quest_id, character_id, text) '. '(quest_id, character_id, upload_id) '.
'VALUES '. 'VALUES '.
'(?, ?, ?) ', '(?, ?, ?) ',
'iis', 'iii',
$questId, $characterId, $text $questId, $characterId, $uploadId
); );
return true;
} }
/** /**
* Get text submitted by Character. * Get upload submitted by Character.
* *
* @param int $questId ID of Quest * @param int $questId ID of Quest
* @param int $characterId ID of Character * @param int $characterId ID of Character
@ -53,20 +69,38 @@
public function getCharacterSubmission($questId, $characterId) public function getCharacterSubmission($questId, $characterId)
{ {
$data = $this->db->query( $data = $this->db->query(
'SELECT created, text '. 'SELECT upload_id '.
'FROM questtypes_submit_characters '. 'FROM questtypes_submit_characters '.
'WHERE quest_id = ? AND character_id = ?', 'WHERE quest_id = ? AND character_id = ?',
'ii', 'ii',
$questId, $characterId $questId, $characterId
); );
if(!empty($data)) { if(!empty($data)) {
return $data[0]; return $this->Uploads->getUploadById($data[0]['upload_id']);
} }
return null; 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

@ -1,14 +1,25 @@
<form method="post"> <?php if(!is_null($exception)) : ?>
<textarea name="answers[]" <?=(!is_null($submission)) ? 'disabled="disabled"' : ''?>><?=!is_null($submission) ? $submission['text'] : ''?></textarea><br /> <p class="error">
<?php if($exception->getNestedException() instanceof \hhu\z\exceptions\WrongFiletypeException) : ?>
<?php if(!is_null($submission)) : ?> <?=_('File has wrong type')?>
<?php if($wordcount > 1) : ?> <?php elseif($exception->getNestedException() instanceof \hhu\z\exceptions\WrongFiletypeException) : ?>
<?=$wordcount?> <?=_('Words')?><br /> <?=_('File exceeds size maximum')?>
<?php else : ?> <?php else : ?>
<?=$wordcount?> <?=_('Word')?><br /> <?=$exception->getNestedException()->getMessage()?>
<?php endif ?> <?php endif ?>
(<?=sprintf(_('submitted at %s on %sh'), $dateFormatter->format(new \DateTime($submission['created'])), $timeFormatter->format(new \DateTime($submission['created'])))?>) </p>
<?php else : ?> <?php endif ?>
<?php if(is_null($submission)) : ?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="answers" /><br />
Erlaubte Dateiformate:
<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')?>" /> <input type="submit" name="submit" value="<?=_('solve')?>" />
<?php endif ?>
</form> </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

@ -1,5 +1,5 @@
<form method="post"> <form method="post">
<?=\hhu\z\Utils::t($submission['text'])?> <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 /> <br /><br />
<?php if($solved) : ?> <?php if($solved) : ?>