improve CRUD for Character types (issue #319) and implement CRUD for Avatars (issue #37)

This commit is contained in:
coderkun 2014-07-10 12:57:25 +02:00
commit 7873ee0d5b
12 changed files with 760 additions and 245 deletions

View file

@ -86,6 +86,60 @@
return $data[0];
}
/**
* Set the picture for an Avatar.
*
* @param int $userId ID of creating user
* @param int $charactertypeId ID of Charactertype of Avatar
* @param int $xplevelId ID of XP-level of Avatar
* @param int $avatarpictureId ID of Avatar picture to set
*/
public function setAvatarForTypeAndLevel($userId, $charactertypeId, $xplevelId, $avatarpictureId)
{
$this->db->query(
'INSERT INTO avatars '.
'(created_user_id, charactertype_id, xplevel_id, avatarpicture_id) '.
'VALUES '.
'(?, ?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'avatarpicture_id = ?',
'iiiii',
$userId,
$charactertypeId,
$xplevelId,
$avatarpictureId,
$avatarpictureId
);
}
/**
* Set the portrait picture for an Avatar.
*
* @param int $userId ID of creating user
* @param int $charactertypeId ID of Charactertype of Avatar
* @param int $xplevelId ID of XP-level of Avatar
* @param int $avatarpictureId ID of Avatar portrait picture to set
*/
public function setAvatarPortraitForTypeAndLevel($userId, $charactertypeId, $xplevelId, $avatarpictureId)
{
$this->db->query(
'INSERT INTO avatars '.
'(created_user_id, charactertype_id, xplevel_id, small_avatarpicture_id) '.
'VALUES '.
'(?, ?, ?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'small_avatarpicture_id = ?',
'iiiii',
$userId,
$charactertypeId,
$xplevelId,
$avatarpictureId,
$avatarpictureId
);
}
}

View file

@ -578,25 +578,6 @@
}
/**
* Get all XP-levels for a Seminary.
*
* @param int $seminaryId ID of Seminary
* @return array List of XP-levels
*/
public function getXPLevelsForSeminary($seminaryId)
{
return $this->db->query(
'SELECT id, xps, level, name '.
'FROM xplevels '.
'WHERE seminary_id = ? '.
'ORDER BY level ASC',
'i',
$seminaryId
);
}
/**
* Get Characters with the given Character role.
*

View file

@ -43,7 +43,7 @@
public function getCharacterTypesForSeminary($seminaryId)
{
return $this->db->query(
'SELECT id, name, url '.
'SELECT id, seminary_id, name, url '.
'FROM charactertypes '.
'WHERE seminary_id = ? '.
'ORDER BY name ASC',
@ -51,6 +51,56 @@
$seminaryId
);
}
/**
* Get Character type by its URL.
*
* @param int $seminaryId ID of Seminary
* @param string $charactretypeUrl URL-title of Character type
* @return array Character type data
*/
public function getCharactertypeByUrl($seminaryId, $charactertypeUrl)
{
$data = $this->db->query(
'SELECT id, seminary_id, name, url '.
'FROM charactertypes '.
'WHERE seminary_id = ? AND url = ?',
'is',
$seminaryId,
$charactertypeUrl
);
if(empty($data)) {
throw new \nre\exceptions\IdNotFoundException($charactertypeUrl);
}
return $data[0];
}
/**
* Get Character type by its ID.
*
* @param string $charactertypeId ID of Character type
* @return array Character type data
*/
public function getCharactertypeById($charactertypeId)
{
$data = $this->db->query(
'SELECT id, seminary_id, name, url '.
'FROM charactertypes '.
'WHERE id = ?',
'i',
$charactertypeId
);
if(empty($data)) {
throw new \nre\exceptions\IdNotFoundException($charactertypeId);
}
return $data[0];
}
/**
@ -75,8 +125,8 @@
return (!empty($data) && (is_null($charactertypeId) || $charactertypeId != $data[0]['id']));
}
/**
* Create a new Charactertype for a Seminary.
*
@ -87,19 +137,47 @@
*/
public function createCharactertype($userId, $seminaryId, $name)
{
$this->db->query(
'INSERT INTO charactertypes '.
'(created_user_id, seminary_id, name, url) '.
'VALUES '.
'(?, ?, ?, ?) ',
'iiss',
$userId,
$seminaryId,
$name,
\nre\core\Linker::createLinkParam($name)
);
return $this->db->getInsertId();
$charactertypeId = null;
$this->db->setAutocommit(false);
try {
// Create Charactertype
$this->db->query(
'INSERT INTO charactertypes '.
'(created_user_id, seminary_id, name, url) '.
'VALUES '.
'(?, ?, ?, ?) ',
'iiss',
$userId,
$seminaryId,
$name,
\nre\core\Linker::createLinkParam($name)
);
$charactertypeId = $this->db->getInsertId();
// Create avatars
$this->db->query(
'INSERT INTO avatars '.
'(created_user_id, charactertype_id, xplevel_id) '.
'SELECT ?, ?, xplevels.id '.
'FROM xplevels '.
'WHERE seminary_id = ?',
'iii',
$userId,
$charactertypeId,
$seminaryId
);
$this->db->commit();
}
catch(\Exception $e) {
$this->db->rollback();
$this->db->setAutocommit(true);
throw $e;
}
$this->db->setAutocommit(true);
return $charactertypeId;
}

View file

@ -173,6 +173,60 @@
return $mediaId;
}
/**
* Create a new Avatar picture for a Charactertype by creating a new Seminarymedia and
* adding it to the list of Avatar pictures.
*
* @param int $userId ID of user that does the upload
* @param int $seminaryId ID of Seminary
* @param string $filename Filename of uploading media
* @param string $description Description for media
* @param string $mimetype Mimetype of media
* @param string $tmpFilename Name of temporary uploaded file
* @return mixed ID of media record or false if upload failed
*/
public function createAvatarpicture($userId, $seminaryId, $filename, $description, $mimetype, $tmpFilename)
{
$mediaId = false;
$this->db->setAutocommit(false);
try {
// Create Seminary media record
$mediaId = $this->createSeminaryMedia($userId, $seminaryId, $filename, $description, $mimetype);
// Add media to Achievements media
$this->db->query(
'INSERT INTO avatarpictures '.
'(seminarymedia_id, created_user_id) '.
'VALUES '.
'(?, ?) '.
'ON DUPLICATE KEY UPDATE '.
'created_user_id = ?',
'iii',
$mediaId,
$userId,
$userId
);
// Create filename
$filename = ROOT.DS.\nre\configs\AppConfig::$dirs['seminarymedia'].DS.$mediaId;
if(!move_uploaded_file($tmpFilename, $filename))
{
$this->db->rollback();
$mediaId = false;
}
}
catch(\nre\exceptions\DatamodelException $e) {
$this->db->rollback();
$this->db->setAutocommit(true);
}
$this->db->setAutocommit(true);
return $mediaId;
}
/**
* Create a new Questgroup picture (Moodpic).
@ -339,6 +393,18 @@
}
/**
* Create a new Achievement media by creating a new Seminarymedia and
* adding it to the list of media for Achievements.
*
* @param int $userId ID of user that does the upload
* @param int $seminaryId ID of Seminary
* @param string $filename Filename of uploading media
* @param string $description Description for media
* @param string $mimetype Mimetype of media
* @param string $tmpFilename Name of temporary uploaded file
* @return mixed ID of media record or false if upload failed
*/
public function createAchievementMedia($userId, $seminaryId, $filename, $description, $mimetype, $tmpFilename)
{
$mediaId = false;