update form for creating Quests and add various data import helpers

This commit is contained in:
coderkun 2014-04-18 18:44:38 +02:00
commit b903a1df77
6 changed files with 242 additions and 4 deletions

View file

@ -448,6 +448,7 @@
// TODO Validation
$name = $this->request->getPostParam('name');
$xps = $this->request->getPostParam('xps');
$prolog = $this->request->getPostParam('prolog');
$entrytext = $this->request->getPostParam('entrytext');
$wrongtext = $this->request->getPostParam('wrongtext');
$task = $this->request->getPostParam('task');
@ -478,6 +479,17 @@
throw new \nre\exceptions\ParamsNotValidException($questtype);
}
// Questmedia
$questmedia = null;
if(array_key_exists('questmedia', $_FILES) && !empty($_FILES['questmedia']) && $_FILES['questmedia']['error'] == 0) {
$questmedia = $_FILES['questmedia'];
}
// Process Prolog
if(!empty($prolog)) {
$prolog = preg_split('/\s*(_|=){5,}\s*/', $prolog, -1, PREG_SPLIT_NO_EMPTY);
}
// Create new Quest
if($validation === true)
{
@ -492,6 +504,28 @@
$task
);
// Create Questmedia
if(!is_null($questmedia))
{
$questsmediaId = $this->Quests->createQuestMedia(
$this->Auth->getUserId(),
$seminary['id'],
$questmedia['name'],
$name,
$questmedia['type'],
$questmedia['tmp_name']
);
if($questsmediaId > 0) {
$this->Quests->setQuestmedia($questId, $questsmediaId);
}
}
// Add Prolog-texts
if(!empty($prolog)) {
$this->Questtexts->addQuesttextsToQuest($this->Auth->getUserId(), $questId, 'Prolog', $prolog);
}
// Redirect
$this->redirect($this->linker->link(array('quests', 'index', $seminary['url'])));
}
@ -505,6 +539,46 @@
}
/**
* Action: createmedia.
* TODO only temporary for easier data import.
*
* Display a form for creating new Seminary media.
*
* @param string $seminaryUrl URL-title of Seminary
*/
public function createmedia($seminaryUrl)
{
// Get seminary
$seminary = $this->Seminaries->getSeminaryByUrl($seminaryUrl);
// Create media
$mediaId = null;
$error = null;
if($this->request->getRequestMethod() == 'POST' && !is_null($this->request->getPostParam('submit')))
{
$file = $_FILES['file'];
$error = $file['error'];
if(empty($error))
{
$mediaId = $this->Media->createQuestMedia(
$this->Auth->getUserId(),
$seminary['id'],
$file['name'],
$this->request->getPostParam('description'),
$file['type'],
$file['tmp_name']
);
}
}
// Pass data to view
$this->set('seminary', $seminary);
$this->set('mediaid', $mediaId);
}
/**

View file

@ -134,6 +134,62 @@
return $data[0];
}
/**
* Create a new Questsmedia by creating a new Seminarymedia and
* adding it to the list of Questsmedia.
* TODO Currently only temporary for easier data import.
*/
public function createQuestMedia($userId, $seminaryId, $filename, $description, $mimetype, $tmpFilename)
{
$uploadId = false;
$this->db->setAutocommit(false);
try {
// Create database record
$this->db->query(
'INSERT INTO seminarymedia '.
'(created_user_id, seminary_id, name, url, description, mimetype) '.
'VALUES '.
'(?, ? ,? ,?, ?, ?)',
'iissss',
$userId,
$seminaryId,
$filename,
\nre\core\Linker::createLinkParam($filename),
$description,
$mimetype
);
$uploadId = $this->db->getInsertId();
$this->db->query(
'INSERT INTO questsmedia '.
'(media_id, created_user_id) '.
'VALUES '.
'(?, ?)',
'ii',
$uploadId,
$userId
);
// Create filename
$filename = ROOT.DS.'seminarymedia'.DS.$uploadId;
if(!move_uploaded_file($tmpFilename, $filename))
{
$this->db->rollback();
$uploadId = false;
}
}
catch(\nre\exceptions\DatamodelException $e) {
$this->db->rollback();
$this->db->setAutocommit(true);
}
$this->db->setAutocommit(true);
return $uploadId;
}
}
?>

View file

@ -436,6 +436,25 @@
return $this->db->getInsertId();
}
/**
* Set the media for a Quest.
*
* @param int $questId ID of Quest to set media for
* @param int $questmediaId ID of Questsmedia to set
*/
public function setQuestmedia($questId, $questsmediaId)
{
$this->db->query(
'UPDATE quests '.
'SET questsmedia_id = ? '.
'WHERE id = ?',
'ii',
$questsmediaId,
$questId
);
}
}
?>

View file

@ -150,6 +150,71 @@
);
}
/**
* Get a Questtexttype by its URL.
*
* @param string $questtexttypeUrl URL-type of Questtexttype
* @return array Questtexttype data
*/
public function getQuesttexttypeByUrl($questtexttypeUrl)
{
$data = $this->db->query(
'SELECT id, type, url '.
'FROM questtexttypes '.
'WHERE url = ?',
's',
$questtexttypeUrl
);
if(!empty($data)) {
return $data[0];
}
return null;
}
/**
* Add a list of Questtexts to a Quest.
*
* @param int $userId ID of user
* @param int $questId ID of Quest to add texts to
* @param string $questtexttypeUrl URL-type of Questtexttype of texts
* @param array $texts List of texts to add.
*/
public function addQuesttextsToQuest($userId, $questId, $questtexttypeUrl, $texts)
{
$questtexttype = $this->getQuesttexttypeByUrl($questtexttypeUrl);
if(is_null($questtexttype)) {
return;
}
foreach($texts as &$text)
{
$pos = $this->db->query(
'SELECT COALESCE(MAX(pos),0)+1 AS pos '.
'FROM questtexts '.
'WHERE quest_id = ?',
'i',
$questId
);
$pos = $pos[0]['pos'];
$this->db->query(
'INSERT INTO questtexts '.
'(created_user_id, quest_id, questtexttype_id, pos, text) '.
'VALUES '.
'(?, ?, ?, ?, ?)',
'iiiis',
$userId, $questId, $questtexttype['id'], $pos,
$text
);
}
}
}
?>

View file

@ -7,7 +7,7 @@
<h2><a href="<?=$linker->link(array('index',$seminary['url']),1)?>"><?=_('Quests')?></a></h2>
<h3><?=_('Create')?></h3>
<form method="post">
<form method="post" enctype="multipart/form-data">
<fieldset>
<label for="name"><?=_('Name')?>:</label>
<input type="text" name="name" value="" placeholder="<?=_('Name')?>" /><br />
@ -25,12 +25,17 @@
</select><br />
<label for="name">XPs:</label>
<input type="number" name="xps" value="" placeholder="<?=_('XPs')?>" /><br />
<input type="file" name="questmedia" /><br />
</fieldset>
<fieldset>
<label for="prolog">Prolog:</label><br />
<textarea name="prolog" placeholder="Prolog" style="width:100%; height:10em;"></textarea><br />
<label for="entrytext"><?=('Entry text')?>:</label><br />
<textarea name="entrytext" placeholder="<?=_('Entry text')?>"></textarea><br />
<textarea name="entrytext" placeholder="<?=_('Entry text')?>" style="width:100%; height:10em;"></textarea><br />
<label for="wrongtext"><?=('Wrong text')?>:</label><br />
<textarea name="wrongtext" placeholder="<?=_('Wrong text')?>"></textarea><br />
<textarea name="wrongtext" placeholder="<?=_('Wrong text')?>" style="width:100%; height:10em;"></textarea><br />
<label for="task"><?=_('Task')?>:</label><br />
<textarea name="task" placeholder="<?=('Task')?>"></textarea><br />
<textarea name="task" placeholder="<?=('Task')?>" style="width:100%; height:10em;"></textarea><br />
</fieldset>
<input type="submit" name="create" value="<?=_('Create')?>" />
</form>

View file

@ -0,0 +1,19 @@
<?php if(!is_null($seminary['seminarymedia_id'])) : ?>
<div class="moodpic">
<img src="<?=$linker->link(array('media','seminaryheader',$seminary['url']))?>" />
</div>
<?php endif ?>
<h1><a href="<?=$linker->link(array('seminaries',$seminary['url']))?>"><?=$seminary['title']?></a></h1>
<h2>Create Questsmedia</h2>
<?php if(!is_null($mediaid)) :?>
<p>New mediaId: <?=var_dump($mediaid)?></p>
<?php endif ?>
<?php if(!empty($error)) :?>
<p>Error: <?=$error?></p>
<?php endif ?>
<form method="post" enctype="multipart/form-data">
<input type="text" name="description" placeholder="Description" /><br />
<input type="file" name="file" required="required" /><br />
<input type="submit" name="submit" value="create" />
</form>