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 8377ca9868
6 changed files with 242 additions and 4 deletions

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
);
}
}
}
?>