restructure application classes
This commit is contained in:
commit
a6d9bf653a
3471 changed files with 597952 additions and 0 deletions
575
models/AchievementsModel.inc
Normal file
575
models/AchievementsModel.inc
Normal file
|
|
@ -0,0 +1,575 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Achievements-tables.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class AchievementsModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new AchievementsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get an Achievement by its URL.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param string $achievementUrl URL-title of Achievement
|
||||
* @return array Achievement data
|
||||
*/
|
||||
public function getAchievementByUrl($seminaryId, $achievementUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT achievements.id, achievementconditions.condition, title, url, description, progress, unachieved_achievementsmedia_id, achieved_achievementsmedia_id '.
|
||||
'FROM achievements '.
|
||||
'LEFT JOIN achievementconditions ON achievementconditions.id = achievements.achievementcondition_id '.
|
||||
'WHERE seminary_id = ? AND url = ?',
|
||||
'is',
|
||||
$seminaryId, $achievementUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($achievementUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all not yet achieved Achievements for a Seminary that can
|
||||
* only be achieved once (only by one Character).
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @return array Achievements data
|
||||
*/
|
||||
public function getUnachievedOnlyOnceAchievementsForSeminary($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT achievements.id, achievements.pos, achievementconditions.condition, title, url, description, progress, hidden, only_once, all_conditions, deadline, unachieved_achievementsmedia_id, achieved_achievementsmedia_id '.
|
||||
'FROM achievements '.
|
||||
'LEFT JOIN achievementconditions ON achievementconditions.id = achievements.achievementcondition_id '.
|
||||
'WHERE achievements.seminary_id = ? AND only_once = 1 AND NOT EXISTS ('.
|
||||
'SELECT character_id '.
|
||||
'FROM achievements_characters '.
|
||||
'WHERE achievements_characters.achievement_id = achievements.id'.
|
||||
')',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Achievements that have a deadline.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @return array Achievements data
|
||||
*/
|
||||
public function getDeadlineAchievements($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT achievements.id, achievements.pos, achievementconditions.condition, title, url, description, progress, hidden, only_once, all_conditions, deadline, unachieved_achievementsmedia_id, achieved_achievementsmedia_id '.
|
||||
'FROM achievements '.
|
||||
'LEFT JOIN achievementconditions ON achievementconditions.id = achievements.achievementcondition_id '.
|
||||
'WHERE achievements.seminary_id = ? AND deadline IS NOT NULL '.
|
||||
'ORDER BY deadline ASC',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get seldom Achievements.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $count Number of Achievements to retrieve
|
||||
* @return array List of seldom Achievements
|
||||
*/
|
||||
public function getSeldomAchievements($seminaryId, $count, $alsoWithDeadline=true)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT achievements.id, achievements.pos, achievements.title, achievements.url, achievements.description, achievements.progress, achievements.hidden, achievements.unachieved_achievementsmedia_id, achievements.achieved_achievementsmedia_id, count(DISTINCT achievements_characters.character_id) AS c '.
|
||||
'FROM achievements_characters '.
|
||||
'INNER JOIN characters_characterroles ON characters_characterroles.character_id = achievements_characters.character_id '.
|
||||
'INNER JOIN characterroles ON characterroles.id = characters_characterroles.characterrole_id AND characterroles.name = ? '.
|
||||
'LEFT JOIN achievements ON achievements.id = achievements_characters.achievement_id '.
|
||||
'WHERE achievements.seminary_id = ? AND achievements.only_once = 0 '.
|
||||
(!$alsoWithDeadline ? 'AND achievements.deadline IS NULL ' : null).
|
||||
'GROUP BY achievements_characters.achievement_id '.
|
||||
'ORDER BY count(DISTINCT achievements_characters.character_id) ASC '.
|
||||
'LIMIT ?',
|
||||
'sii',
|
||||
'user',
|
||||
$seminaryId,
|
||||
$count
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all achieved Achievements for a Character.
|
||||
*
|
||||
* @param int $characterId ID of Character
|
||||
* @return array Achievements data
|
||||
*/
|
||||
public function getAchievedAchievementsForCharacter($characterId, $alsoWithDeadline=true)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT achievements.id, achievements.pos, achievements_characters.created, achievements.title, achievements.url, achievements.description, achievements.progress, unachieved_achievementsmedia_id, achieved_achievementsmedia_id '.
|
||||
'FROM achievements '.
|
||||
'INNER JOIN achievements_characters ON achievements_characters.achievement_id = achievements.id '.
|
||||
'WHERE achievements_characters.character_id = ? '.
|
||||
(!$alsoWithDeadline ? 'AND achievements.deadline IS NULL ' : null).
|
||||
'ORDER BY achievements_characters.created DESC',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all not yet achieved Achievements for a Character.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $characterId ID of Character
|
||||
* @return array Achievements data
|
||||
*/
|
||||
public function getUnachhievedAchievementsForCharacter($seminaryId, $characterId, $includeOnlyOnce=false, $alsoWithDeadline=true)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT achievements.id, achievements.pos, achievementconditions.condition, title, url, description, progress, hidden, only_once, all_conditions, deadline, unachieved_achievementsmedia_id, achieved_achievementsmedia_id '.
|
||||
'FROM achievements '.
|
||||
'LEFT JOIN achievementconditions ON achievementconditions.id = achievements.achievementcondition_id '.
|
||||
'WHERE achievements.seminary_id = ? AND only_once <= ? AND NOT EXISTS ('.
|
||||
'SELECT character_id '.
|
||||
'FROM achievements_characters '.
|
||||
'WHERE '.
|
||||
'achievements_characters.achievement_id = achievements.id AND '.
|
||||
'achievements_characters.character_id = ?'.
|
||||
') '.
|
||||
(!$alsoWithDeadline ? 'AND achievements.deadline IS NULL ' : null).
|
||||
'ORDER BY achievements.pos ASC',
|
||||
'iii',
|
||||
$seminaryId,
|
||||
$includeOnlyOnce,
|
||||
$characterId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the rank for the number of achieved Achievements.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $xps Amount of achieved Achievements
|
||||
* @return int Rank of Achievements count
|
||||
*/
|
||||
public function getCountRank($seminaryId, $count)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT count(*) AS c '.
|
||||
'FROM ('.
|
||||
'SELECT count(DISTINCT achievement_id) '.
|
||||
'FROM achievements_characters '.
|
||||
'LEFT JOIN achievements ON achievements.id = achievements_characters.achievement_id '.
|
||||
'WHERE achievements.seminary_id = ? '.
|
||||
'GROUP BY character_id '.
|
||||
'HAVING count(DISTINCT achievement_id) > ?'.
|
||||
') AS ranking',
|
||||
'ii',
|
||||
$seminaryId,
|
||||
$count
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['c'] + 1;
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all date conditions for an Achievement.
|
||||
*
|
||||
* @param int $achievementId ID of Achievement
|
||||
* @return array Date conditions
|
||||
*/
|
||||
public function getAchievementConditionsDate($achievementId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT `select` '.
|
||||
'FROM achievementconditions_date '.
|
||||
'WHERE achievement_id = ?',
|
||||
'i',
|
||||
$achievementId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check a date condition.
|
||||
*
|
||||
* @param string $select SELECT-string with date-functions
|
||||
* @return boolean Result
|
||||
*/
|
||||
public function checkAchievementConditionDate($select)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT ('.$select.') AS got '
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return ($data[0]['got'] == 1);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Character conditions for an Achievement.
|
||||
*
|
||||
* @param int $achievementId ID of Achievement
|
||||
* @return array Character conditions
|
||||
*/
|
||||
public function getAchievementConditionsCharacter($achievementId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT field, value '.
|
||||
'FROM achievementconditions_character '.
|
||||
'WHERE achievement_id = ?',
|
||||
'i',
|
||||
$achievementId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check a Character condition.
|
||||
*
|
||||
* @param string $field Field to check
|
||||
* @param int $value The value the field has to match
|
||||
* @param int $characterId ID of Character
|
||||
* @return boolean Result
|
||||
*/
|
||||
public function checkAchievementConditionCharacter($field, $value, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
"SELECT ($field >= $value) AS got ".
|
||||
'FROM v_characters '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return ($data[0]['got'] == 1);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the progress for a Character condition.
|
||||
*
|
||||
* @param string $field Field to check
|
||||
* @param int $value The value the field has to match
|
||||
* @param int $characterId ID of Character
|
||||
* @return float Percentage progress
|
||||
*/
|
||||
public function getAchievementConditionCharacterProgress($field, $value, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
"SELECT $field AS field ".
|
||||
'FROM v_characters '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['field'] / $value;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Quest conditions for an Achievement.
|
||||
*
|
||||
* @param int $achievementId ID of Achievement
|
||||
* @return array Quest conditions
|
||||
*/
|
||||
public function getAchievementConditionsQuest($achievementId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT field, `count`, value, quest_id, status, groupby '.
|
||||
'FROM achievementconditions_quest '.
|
||||
'WHERE achievement_id = ?',
|
||||
'i',
|
||||
$achievementId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check a Quest condition.
|
||||
*
|
||||
* @param string $field Field to check
|
||||
* @param boolean $count Conut field-value
|
||||
* @param int $value The value the field has to match
|
||||
* @param int $status Status of Quest or NULL
|
||||
* @param string $groupby Field to group or NULL
|
||||
* @param int $questId ID of related Quest or NULL
|
||||
* @param int $characterId ID of Character
|
||||
* @return boolean Result
|
||||
*/
|
||||
public function checkAchievementConditionQuest($field, $count, $value, $status, $groupby, $questId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT ('.(
|
||||
$count
|
||||
? "count($field) >= $value"
|
||||
: "$field = $value"
|
||||
). ') AS got '.
|
||||
'FROM quests_characters '.
|
||||
'WHERE '.
|
||||
'character_id = ?'.
|
||||
(!is_null($questId) ? " AND quest_id = $questId" : '').
|
||||
(!is_null($status) ? " AND status = $status" : '').
|
||||
(!is_null($groupby) ? " GROUP BY $groupby" : ''),
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
foreach($data as &$datum) {
|
||||
if($datum['got'] == 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the progress for a Quest condition.
|
||||
*
|
||||
* @param string $field Field to check
|
||||
* @param boolean $count Conut field-value
|
||||
* @param int $value The value the field has to match
|
||||
* @param int $status Status of Quest or NULL
|
||||
* @param string $groupby Field to group or NULL
|
||||
* @param int $questId ID of related Quest or NULL
|
||||
* @param int $characterId ID of Character
|
||||
* @return float Percentage progress
|
||||
*/
|
||||
public function getAchievementConditionQuestProgress($field, $count, $value, $status, $groupby, $questId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT '.(
|
||||
$count
|
||||
? "count($field)"
|
||||
: "$field"
|
||||
). ' AS field '.
|
||||
'FROM quests_characters '.
|
||||
'WHERE '.
|
||||
'character_id = ?'.
|
||||
(!is_null($questId) ? " AND quest_id = $questId" : '').
|
||||
(!is_null($status) ? " AND status = $status" : '').
|
||||
(!is_null($groupby) ? " GROUP BY $groupby" : ''),
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data))
|
||||
{
|
||||
$maxField = 0;
|
||||
foreach($data as &$datum) {
|
||||
$maxField = max($maxField, intval($datum['field']));
|
||||
}
|
||||
|
||||
return $maxField / $value;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Metaachievement conditions for an Achievement.
|
||||
*
|
||||
* @param int $achievementId ID of Achievement
|
||||
* @return array Metaachievement conditions
|
||||
*/
|
||||
public function getAchievementConditionsAchievement($achievementId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT field, `count`, value, meta_achievement_id, groupby '.
|
||||
'FROM achievementconditions_achievement '.
|
||||
'WHERE achievement_id = ?',
|
||||
'i',
|
||||
$achievementId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check a Metaachievement condition.
|
||||
*
|
||||
* @param string $field Field to check
|
||||
* @param boolean $count Conut field-value
|
||||
* @param int $value The value the field has to match
|
||||
* @param string $groupby Field to group or NULL
|
||||
* @param int $metaAchievementId ID of related Achievement or NULL
|
||||
* @param int $characterId ID of Character
|
||||
* @return boolean Result
|
||||
*/
|
||||
public function checkAchievementConditionAchievement($field, $count, $value, $groupby, $metaAchievementId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT ('.(
|
||||
$count
|
||||
? "count($field) >= $value"
|
||||
: "$field = $value"
|
||||
). ') AS got '.
|
||||
'FROM achievements_characters '.
|
||||
'WHERE '.
|
||||
'character_id = ?'.
|
||||
(!is_null($metaAchievementId) ? " AND achievement_id = $metaAchievementId" : '').
|
||||
(!is_null($groupby) ? " GROUP BY $groupby" : ''),
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
foreach($data as &$datum) {
|
||||
if($datum['got'] == 1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the progress for a Metaachievement condition.
|
||||
*
|
||||
* @param string $field Field to check
|
||||
* @param boolean $count Conut field-value
|
||||
* @param int $value The value the field has to match
|
||||
* @param string $groupby Field to group or NULL
|
||||
* @param int $metaAchievementId ID of related Achievement or NULL
|
||||
* @param int $characterId ID of Character
|
||||
* @return float Percentage progress
|
||||
*/
|
||||
public function getAchievementConditionAchievementProgress($field, $count, $value, $groupby, $metaAchievementId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT '.(
|
||||
$count
|
||||
? "count($field)"
|
||||
: "$field"
|
||||
). ' AS field '.
|
||||
'FROM achievements_characters '.
|
||||
'WHERE '.
|
||||
'character_id = ?'.
|
||||
(!is_null($metaAchievementId) ? " AND achievement_id = $metaAchievementId" : '').
|
||||
(!is_null($groupby) ? " GROUP BY $groupby" : ''),
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data))
|
||||
{
|
||||
$maxField = 0;
|
||||
foreach($data as &$datum) {
|
||||
$maxField = max($maxField, intval($datum['field']));
|
||||
}
|
||||
|
||||
return $maxField / $value;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set an Achievement as achieved for a Character.
|
||||
*
|
||||
* @param int $achievementId ID of Achievement
|
||||
* @param int $characterId ID of Character
|
||||
*/
|
||||
public function setAchievementAchieved($achievementId, $characterId)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO achievements_characters '.
|
||||
'(achievement_id, character_id) '.
|
||||
'VALUES '.
|
||||
'(?, ?)',
|
||||
'ii',
|
||||
$achievementId, $characterId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a Character has achieved an Achievement.
|
||||
*
|
||||
* @param int $achievementId ID of Achievement
|
||||
* @param int $characterId ID of Character
|
||||
* @return boolean Whether Character has achieved the Achievement or not
|
||||
*/
|
||||
public function hasCharacterAchievedAchievement($achievementId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT achievement_id, character_id, created '.
|
||||
'FROM achievements_characters '.
|
||||
'WHERE achievement_id = ? AND character_id = ?',
|
||||
'ii',
|
||||
$achievementId, $characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
92
models/AvatarsModel.inc
Normal file
92
models/AvatarsModel.inc
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Avatars-tables.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class AvatarsModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new AvatarsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get an Avatar by its ID
|
||||
*
|
||||
* @param int $avatarId ID of Avatar
|
||||
* @return array Avatar data
|
||||
*/
|
||||
public function getAvatarById($avatarId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, charactertype_id, xplevel_id, avatarpicture_id, small_avatarpicture_id '.
|
||||
'FROM avatars '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$avatarId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an Avatar by its Character type and XP-level.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param string $charactertypeUrl URL-title of Character type
|
||||
* @param int $xplevel XP-level
|
||||
* @return array Avatar data
|
||||
*/
|
||||
public function getAvatarByTypeAndLevel($seminaryId, $charactertypeUrl, $xplevel)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT avatars.id, charactertype_id, xplevel_id, avatarpicture_id, small_avatarpicture_id '.
|
||||
'FROM avatars '.
|
||||
'INNER JOIN charactertypes ON charactertypes.id = avatars.charactertype_id '.
|
||||
'INNER JOIN xplevels ON xplevels.id = avatars.xplevel_id AND xplevels.seminary_id = charactertypes.seminary_id '.
|
||||
'WHERE charactertypes.seminary_id = ? AND charactertypes.url = ? AND xplevels.level = ?',
|
||||
'isi',
|
||||
$seminaryId,
|
||||
$charactertypeUrl,
|
||||
$xplevel
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($charactertypeUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
490
models/CharactergroupsModel.inc
Normal file
490
models/CharactergroupsModel.inc
Normal file
|
|
@ -0,0 +1,490 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model of the CharactergroupsAgent to interact with
|
||||
* Charactergroups-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class CharactergroupsModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new CharactergroupsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get Character groups-groups of a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of the corresponding Seminary
|
||||
* @return array Character groups-groups data
|
||||
*/
|
||||
public function getGroupsroupsForSeminary($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, name, url, preferred '.
|
||||
'FROM charactergroupsgroups '.
|
||||
'WHERE seminary_id = ?',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Character groups-group by its URL.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $seminaryId ID of the corresponding Seminary
|
||||
* @param string $groupsgroupUrl URL-name of the Character groups-group
|
||||
* @return array Character groups-group data
|
||||
*/
|
||||
public function getGroupsgroupByUrl($seminaryId, $groupsgroupUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, name, url, preferred '.
|
||||
'FROM charactergroupsgroups '.
|
||||
'WHERE seminary_id = ? AND url = ?',
|
||||
'is',
|
||||
$seminaryId, $groupsgroupUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($groupsgroupUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Character groups-group by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param string $groupsgroupId ID of the Character groups-group
|
||||
* @return array Character groups-group data
|
||||
*/
|
||||
public function getGroupsgroupById($groupsgroupId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, name, url, preferred '.
|
||||
'FROM charactergroupsgroups '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$groupsgroupId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($groupsgroupId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a Character groups-group name already exists.
|
||||
*
|
||||
* @param string $name Name to check
|
||||
* @param int $groupsgroupId Do not check this ID (for editing)
|
||||
* @return boolean Whether name exists or not
|
||||
*/
|
||||
public function characterGroupsgroupNameExists($name, $groupsgroupId=null)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id '.
|
||||
'FROM charactergroupsgroups '.
|
||||
'WHERE name = ? OR url = ?',
|
||||
'ss',
|
||||
$name,
|
||||
\nre\core\Linker::createLinkParam($name)
|
||||
);
|
||||
|
||||
|
||||
return (!empty($data) && (is_null($groupsgroupId) || $groupsgroupId != $data[0]['id']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Character groups-group.
|
||||
*
|
||||
* @param int $userId ID of user
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param string $name Name of new groups-group
|
||||
* @param boolean $preferred Whether groups-group is preferred or not
|
||||
* @return int ID of newly created groups-group
|
||||
*/
|
||||
public function createGroupsgroup($userId, $seminaryId, $name, $preferred)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO charactergroupsgroups '.
|
||||
'(created_user_id, seminary_id, name, url, preferred) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?, ?, ?)',
|
||||
'iissd',
|
||||
$userId,
|
||||
$seminaryId,
|
||||
$name,
|
||||
\nre\core\Linker::createLinkParam($name),
|
||||
$preferred
|
||||
);
|
||||
|
||||
|
||||
return $this->db->getInsertId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit a Character groups-group.
|
||||
*
|
||||
* @param int $groupsgroupId ID of groups-group to edit
|
||||
* @param string $name New name of groups-group
|
||||
* @param boolean $preferred Whether groups-group is preferred or not
|
||||
*/
|
||||
public function editGroupsgroup($groupsgroupId, $name, $preferred)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE charactergroupsgroups '.
|
||||
'SET name = ?, url = ?, preferred = ? '.
|
||||
'WHERE id = ?',
|
||||
'ssdi',
|
||||
$name,
|
||||
\nre\core\Linker::createLinkParam($name),
|
||||
$preferred,
|
||||
$groupsgroupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a Character groups-group.
|
||||
*
|
||||
* @param int $groupsgroupId ID of groups-group to delete
|
||||
*/
|
||||
public function deleteGroupsgroup($groupsgroupId)
|
||||
{
|
||||
$this->db->query('DELETE FROM charactergroupsgroups WHERE id = ?', 'i', $groupsgroupId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Character groups for a Character groups-group.
|
||||
*
|
||||
* @param int $groupsgroupId ID of the Character groups-group
|
||||
* @return array Character groups
|
||||
*/
|
||||
public function getGroupsForGroupsgroup($groupsgroupId, $sortorder='name')
|
||||
{
|
||||
// Set sort order
|
||||
switch($sortorder)
|
||||
{
|
||||
case 'xps':
|
||||
$sortorder = 'xps DESC';
|
||||
break;
|
||||
case 'name':
|
||||
default:
|
||||
$sortorder = 'name ASC';
|
||||
break;
|
||||
}
|
||||
|
||||
// Get and return Character groups
|
||||
return $this->db->query(
|
||||
'SELECT id, name, url, xps, motto, charactergroupsmedia_id '.
|
||||
'FROM v_charactergroups '.
|
||||
'WHERE charactergroupsgroup_id = ? '.
|
||||
"ORDER BY $sortorder",
|
||||
'i',
|
||||
$groupsgroupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Character groups for a Character.
|
||||
*
|
||||
* @param int $characterId ID of the Character
|
||||
* @return array Character groups
|
||||
*/
|
||||
public function getGroupsForCharacter($characterId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT charactergroups.id, charactergroups.charactergroupsgroup_id, charactergroups.name, charactergroups.url, charactergroups.charactergroupsmedia_id, charactergroups.xps, charactergroupsgroups.id AS charactergroupsgroup_id, charactergroupsgroups.name AS charactergroupsgroup_name, charactergroupsgroups.url AS charactergroupsgroup_url '.
|
||||
'FROM characters_charactergroups '.
|
||||
'LEFT JOIN v_charactergroups AS charactergroups ON charactergroups.id = characters_charactergroups.charactergroup_id '.
|
||||
'LEFT JOIN charactergroupsgroups ON charactergroupsgroups.id = charactergroups.charactergroupsgroup_id '.
|
||||
'WHERE characters_charactergroups.character_id = ?',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Character group by its URL.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $groupsgroupId ID of the Character groups-group
|
||||
* @param string $groupUrl URL-name of the Character group
|
||||
* @return array Character group data
|
||||
*/
|
||||
public function getGroupByUrl($groupsgroupId, $groupUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, name, url, xps, motto, charactergroupsmedia_id '.
|
||||
'FROM v_charactergroups '.
|
||||
'WHERE charactergroupsgroup_id = ? AND url = ?',
|
||||
'is',
|
||||
$groupsgroupId, $groupUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($groupUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Character group by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $groupsgroupId ID of the Character group
|
||||
* @return array Character group data
|
||||
*/
|
||||
public function getGroupById($groupId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, name, url, xps, motto, charactergroupsmedia_id '.
|
||||
'FROM v_charactergroups '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$groupId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($groupId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the Character groups for a Quest.
|
||||
*
|
||||
* @param int $questId ID of the Character groups Quest
|
||||
* @return array Character groups
|
||||
*/
|
||||
public function getGroupsForQuest($questId)
|
||||
{
|
||||
$groups = $this->db->query(
|
||||
'SELECT charactergroups.id, charactergroups.name, charactergroups.url, charactergroupsquests_groups.created, charactergroupsquests_groups.xps_factor, charactergroupsquests.xps '.
|
||||
'FROM charactergroupsquests_groups '.
|
||||
'LEFT JOIN charactergroups ON charactergroups.id = charactergroupsquests_groups.charactergroup_id '.
|
||||
'LEFT JOIN charactergroupsquests ON charactergroupsquests.id = charactergroupsquests_groups.charactergroupsquest_id '.
|
||||
'WHERE charactergroupsquests_groups.charactergroupsquest_id = ? '.
|
||||
'ORDER BY xps_factor DESC',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
foreach($groups as &$group) {
|
||||
$group['xps'] = round($group['xps'] * $group['xps_factor'], 1);
|
||||
}
|
||||
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a Character group name already exists.
|
||||
*
|
||||
* @param string $name Name to check
|
||||
* @param int $groupsgroupId Do not check this ID (for editing)
|
||||
* @return boolean Whether name exists or not
|
||||
*/
|
||||
public function characterGroupNameExists($name, $groupId=null)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id '.
|
||||
'FROM charactergroups '.
|
||||
'WHERE name = ? OR url = ?',
|
||||
'ss',
|
||||
$name,
|
||||
\nre\core\Linker::createLinkParam($name)
|
||||
);
|
||||
|
||||
|
||||
return (!empty($data) && (is_null($groupId) || $groupId != $data[0]['id']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Character group.
|
||||
*
|
||||
* @param int $userId ID of user
|
||||
* @param int $groupsgroupId ID of Character groups-group
|
||||
* @param string $name Name of new group
|
||||
* @param string $motto Motto of new group
|
||||
* @return int ID of newly created group
|
||||
*/
|
||||
public function createGroup($userId, $groupsgroupId, $name, $motto)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO charactergroups '.
|
||||
'(created_user_id, charactergroupsgroup_id, name, url, motto) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?, ?, ?)',
|
||||
'iisss',
|
||||
$userId,
|
||||
$groupsgroupId,
|
||||
$name,
|
||||
\nre\core\Linker::createLinkParam($name),
|
||||
$motto
|
||||
);
|
||||
|
||||
|
||||
return $this->db->getInsertId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the media for a Character group.
|
||||
*
|
||||
* @param int $groupId ID of Character group to set media for
|
||||
* @param int $mediaId ID of Character groups media
|
||||
*/
|
||||
public function setMediaForGroup($groupId, $mediaId)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE charactergroups '.
|
||||
'SET charactergroupsmedia_id = ? '.
|
||||
'WHERE id = ?',
|
||||
'ii',
|
||||
$mediaId,
|
||||
$groupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit a Character group.
|
||||
*
|
||||
* @param int $groupId ID of Character group to edit
|
||||
* @param string $name New name of group
|
||||
* @param string $motto New motto of group
|
||||
*/
|
||||
public function editGroup($groupId, $name, $motto)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE charactergroups '.
|
||||
'SET name = ?, url = ?, motto = ? '.
|
||||
'WHERE id = ?',
|
||||
'sssi',
|
||||
$name,
|
||||
\nre\core\Linker::createLinkParam($name),
|
||||
$motto,
|
||||
$groupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a Character group.
|
||||
*
|
||||
* @param int $groupId ID of Character group to delete
|
||||
*/
|
||||
public function deleteGroup($groupId)
|
||||
{
|
||||
$this->db->query('DELETE FROM charactergroups WHERE id = ?', 'i', $groupId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the rank of a XP-value of a Character.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $xps XP-value to get rank for
|
||||
* @return int Rank of XP-value
|
||||
*/
|
||||
public function getXPRank($groupsgroupId, $xps)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT count(id) AS c '.
|
||||
'FROM v_charactergroups '.
|
||||
'WHERE charactergroupsgroup_id = ? AND xps > ?',
|
||||
'id',
|
||||
$groupsgroupId, $xps
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['c'] + 1;
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a Character to a Character group.
|
||||
*
|
||||
* @param int $groupId ID of Character group
|
||||
* @param int $characterId ID of Character to add
|
||||
*/
|
||||
public function addCharacterToCharactergroup($groupId, $characterId)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO characters_charactergroups '.
|
||||
'(character_id, charactergroup_id) '.
|
||||
'VALUES '.
|
||||
'(?, ?)',
|
||||
'ii',
|
||||
$characterId,
|
||||
$groupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove a Character from a Character group.
|
||||
*
|
||||
* @param int $groupId ID of Character group
|
||||
* @param int $characterId ID of Character to remove
|
||||
*/
|
||||
public function removeCharacterFromCharactergroup($groupId, $characterId)
|
||||
{
|
||||
$this->db->query(
|
||||
'DELETE FROM characters_charactergroups '.
|
||||
'WHERE charactergroup_id = ? AND character_id = ?',
|
||||
'ii',
|
||||
$groupId,
|
||||
$characterId
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
389
models/CharactergroupsquestsModel.inc
Normal file
389
models/CharactergroupsquestsModel.inc
Normal file
|
|
@ -0,0 +1,389 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model of the CharactergroupsquestsAgent to interact with
|
||||
* Charactergroupsquests-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class CharactergroupsquestsModel extends \hhu\z\Model
|
||||
{
|
||||
/**
|
||||
* Required models
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $models = array('uploads');
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new CharactergroupsquestsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get Character groups Quests of a Character groups-groups.
|
||||
*
|
||||
* @param int $groupsgroupId ID of the Character groups-group
|
||||
* @return array Character groups Quest data
|
||||
*/
|
||||
public function getQuestsForCharactergroupsgroup($groupsgroupId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, questgroups_id, title, url, xps '.
|
||||
'FROM charactergroupsquests '.
|
||||
'WHERE charactergroupsgroup_id = ?',
|
||||
'i',
|
||||
$groupsgroupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Character groups Quest by its URL.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $groupsgroupId ID of the Character groups-group
|
||||
* @param string $questUrl URL-title of the Character groups Quest
|
||||
* @return array Character groups Quest data
|
||||
*/
|
||||
public function getQuestByUrl($groupsgroupId, $questUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, questgroups_id, title, url, description, xps, rules, won_text, lost_text, questsmedia_id '.
|
||||
'FROM charactergroupsquests '.
|
||||
'WHERE charactergroupsgroup_id = ? AND url = ?',
|
||||
'is',
|
||||
$groupsgroupId,
|
||||
$questUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Character groups Quest by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $questId ID of the Character groups Quest
|
||||
* @return array Character groups Quest data
|
||||
*/
|
||||
public function getQuestById($questId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, questgroups_id, title, url, description, xps, rules, won_text, lost_text, questsmedia_id '.
|
||||
'FROM charactergroupsquests '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Character groups Quests for a Character group.
|
||||
*
|
||||
* @param int $groupId ID of the Character group
|
||||
* @return array Character groups Quests
|
||||
*/
|
||||
public function getQuestsForGroup($groupId)
|
||||
{
|
||||
$quests = $this->db->query(
|
||||
'SELECT charactergroupsquests.id, charactergroupsquests_groups.created, charactergroupsquests.title, charactergroupsquests.url, charactergroupsquests.xps, charactergroupsquests_groups.xps_factor '.
|
||||
'FROM charactergroupsquests_groups '.
|
||||
'LEFT JOIN charactergroupsquests ON charactergroupsquests.id = charactergroupsquests_groups.charactergroupsquest_id '.
|
||||
'WHERE charactergroupsquests_groups.charactergroup_id = ?',
|
||||
'i',
|
||||
$groupId
|
||||
);
|
||||
foreach($quests as &$quest) {
|
||||
$quest['group_xps'] = round($quest['xps'] * $quest['xps_factor'], 1);
|
||||
}
|
||||
|
||||
|
||||
return $quests;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get XPs of a Character group for a Character groups Quest.
|
||||
*
|
||||
* @param int $questId ID of Character groups Quest
|
||||
* @param int $groupId ID of Character group to get XPs of
|
||||
* @return array XP-record
|
||||
*/
|
||||
public function getXPsOfGroupForQuest($questId, $groupId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT charactergroupsquests_groups.created, charactergroupsquests_groups.xps_factor, charactergroupsquests.xps '.
|
||||
'FROM charactergroupsquests_groups '.
|
||||
'LEFT JOIN charactergroupsquests ON charactergroupsquests.id = charactergroupsquests_groups.charactergroupsquest_id '.
|
||||
'WHERE charactergroupsquests_groups.charactergroupsquest_id = ? AND charactergroupsquests_groups.charactergroup_id = ?',
|
||||
'ii',
|
||||
$questId,
|
||||
$groupId
|
||||
);
|
||||
if(empty($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = $data[0];
|
||||
$data['xps'] = round($data['xps'] * $data['xps_factor'], 1);
|
||||
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set XPs of a Character group for a Character groups Quest.
|
||||
*
|
||||
* @param int $questId ID of Character groups Quest
|
||||
* @param int $groupId ID of Character group to set XPs of
|
||||
* @param float $xpsFactor XPs-factor
|
||||
*/
|
||||
public function setXPsOfGroupForQuest($questId, $groupId, $xpsFactor)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO charactergroupsquests_groups '.
|
||||
'(charactergroupsquest_id, charactergroup_id, xps_factor) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) '.
|
||||
'ON DUPLICATE KEY UPDATE '.
|
||||
'xps_factor = ?',
|
||||
'iidd',
|
||||
$questId,
|
||||
$groupId,
|
||||
$xpsFactor,
|
||||
$xpsFactor
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove a Character group from a Character groups Quest.
|
||||
*
|
||||
* @param int $questId ID of Character groups Quest
|
||||
* @param int $groupId ID of Character group to remove
|
||||
*/
|
||||
public function deleteGroupForQuest($questId, $groupId)
|
||||
{
|
||||
$this->db->query(
|
||||
'DELETE FROM charactergroupsquests_groups '.
|
||||
'WHERE charactergroupsquest_id = ? AND charactergroup_id = ?',
|
||||
'ii',
|
||||
$questId, $groupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a Character groups Quest title already exists.
|
||||
*
|
||||
* @param string $name Character groups Quest title to check
|
||||
* @param int $questId Do not check this ID (for editing)
|
||||
* @return boolean Whether Character groups Quest title exists or not
|
||||
*/
|
||||
public function characterGroupsQuestTitleExists($title, $questId=null)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id '.
|
||||
'FROM charactergroupsquests '.
|
||||
'WHERE title = ? OR url = ?',
|
||||
'ss',
|
||||
$title,
|
||||
\nre\core\Linker::createLinkParam($title)
|
||||
);
|
||||
|
||||
return (!empty($data) && (is_null($questId) || $questId != $data[0]['id']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the media for a Character groups Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to upload media for
|
||||
* @param int $mediaId ID of Quests media
|
||||
*/
|
||||
public function setMediaForQuest($questId, $mediaId)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE charactergroupsquests '.
|
||||
'SET questsmedia_id = ? '.
|
||||
'WHERE id = ?',
|
||||
'ii',
|
||||
$mediaId,
|
||||
$questId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Upload a media for a Character groups Quest.
|
||||
*
|
||||
* @param int $userId ID of user that does the upload
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $questId ID of Quest to upload media for
|
||||
* @param array $file File-array of file to upload
|
||||
* @param string $filename Filename for media
|
||||
* @return boolean Whether upload succeeded or not
|
||||
*/
|
||||
public function uploadMediaForQuest($userId, $seminaryId, $questId, $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 charactergroupsquests_seminaryuploads '.
|
||||
'(seminaryupload_id, charactergroupsquest_id, created_user_id) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) ',
|
||||
'iii',
|
||||
$uploadId, $questId, $userId
|
||||
);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get uploaded Medai for a Character groups Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to get media for
|
||||
* @return array Seminary uploads
|
||||
*/
|
||||
public function getMediaForQuest($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT seminaryupload_id, created, created_user_id '.
|
||||
'FROM charactergroupsquests_seminaryuploads '.
|
||||
'WHERE charactergroupsquest_id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Character groups Quest.
|
||||
*
|
||||
* @param int $userId ID of user
|
||||
* @param int $groupsgroupId ID of Character groups-group
|
||||
* @param int $questgroupId ID of Quest group
|
||||
* @param string $title Title of new Quest
|
||||
* @param string $description Description of new Quset
|
||||
* @param int $xps Amount of XPs for new Quest
|
||||
* @param string $rules Rules of new Quest
|
||||
* @param string $wonText Won-text of new Quset
|
||||
* @param string $lostText Lost-text of new Quest
|
||||
* @return int ID of newly created Quest
|
||||
*/
|
||||
public function createQuest($userId, $groupsgroupId, $questgroupId, $title, $description, $xps, $rules, $wonText, $lostText)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO charactergroupsquests '.
|
||||
'(created_user_id, charactergroupsgroup_id, questgroups_id, title, url, description, xps, rules, won_text, lost_text) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
'iiisssdsss',
|
||||
$userId,
|
||||
$groupsgroupId,
|
||||
$questgroupId,
|
||||
$title,
|
||||
\nre\core\Linker::createLinkParam($title),
|
||||
$description,
|
||||
$xps,
|
||||
$rules,
|
||||
$wonText,
|
||||
$lostText
|
||||
);
|
||||
|
||||
|
||||
return $this->db->getInsertId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit a Character groups Quest.
|
||||
*
|
||||
* @param int $questId ID of Character groups Quest to edit
|
||||
* @param int $groupsgroupId ID of Character groups-group
|
||||
* @param int $questgroupId ID of Quest group
|
||||
* @param string $title Title of new Quest
|
||||
* @param string $description Description of new Quset
|
||||
* @param int $xps Amount of XPs for new Quest
|
||||
* @param string $rules Rules of new Quest
|
||||
* @param string $wonText Won-text of new Quset
|
||||
* @param string $lostText Lost-text of new Quest
|
||||
*/
|
||||
public function editQuest($questId, $groupsgroupId, $questgroupId, $title, $description, $xps, $rules, $wonText, $lostText)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE charactergroupsquests '.
|
||||
'SET charactergroupsgroup_id = ?, questgroups_id = ?, title = ?, url = ?, description = ?, xps = ?, rules = ?, won_text = ?, lost_text= ? '.
|
||||
'WHERE id = ?',
|
||||
'iisssdsssi',
|
||||
$groupsgroupId,
|
||||
$questgroupId,
|
||||
$title,
|
||||
\nre\core\Linker::createLinkParam($title),
|
||||
$description,
|
||||
$xps,
|
||||
$rules,
|
||||
$wonText,
|
||||
$lostText,
|
||||
$questId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a Character groups Quest.
|
||||
*
|
||||
* @param int $questId ID of Character groups Quest to delete
|
||||
*/
|
||||
public function deleteQuest($questId)
|
||||
{
|
||||
$this->db->query('DELETE FROM charactergroupsquests WHERE id = ?', 'i', $questId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
100
models/CharacterrolesModel.inc
Normal file
100
models/CharacterrolesModel.inc
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with characterroles-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class CharacterrolesModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new CharacterrolesModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all characterroles for a Character referenced by its ID.
|
||||
*
|
||||
* @param int $userId ID of an user
|
||||
* @return array Characterroles for a Character
|
||||
*/
|
||||
public function getCharacterrolesForCharacterById($characterId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT characterroles.id, characterroles.created, characterroles.name '.
|
||||
'FROM characters_characterroles '.
|
||||
'LEFT JOIN characterroles ON characterroles.id = characters_characterroles.characterrole_id '.
|
||||
'WHERE characters_characterroles.character_id = ?',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a role to a Character.
|
||||
*
|
||||
* @param int $characterId ID of Character to add role to
|
||||
* @param string $characterrole Role to add
|
||||
*/
|
||||
public function addCharacterroleToCharacter($characterId, $characterrole)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT IGNORE INTO characters_characterroles '.
|
||||
'(character_id, characterrole_id) '.
|
||||
'SELECT ?, id '.
|
||||
'FROM characterroles '.
|
||||
'WHERE name = ?',
|
||||
'is',
|
||||
$characterId,
|
||||
$characterrole
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove a role from a Character.
|
||||
*
|
||||
* @param int $characterId ID of Character to remove role from
|
||||
* @param string $characterrole Role to remove
|
||||
*/
|
||||
public function removeCharacterroleFromCharacter($characterId, $characterrole)
|
||||
{
|
||||
$this->db->query(
|
||||
'DELETE FROM characters_characterroles '.
|
||||
'WHERE character_id = ? AND characterrole_id = ('.
|
||||
'SELECT id '.
|
||||
'FROM characterroles '.
|
||||
'WHERE name = ?'.
|
||||
')',
|
||||
'is',
|
||||
$characterId,
|
||||
$characterrole
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
544
models/CharactersModel.inc
Normal file
544
models/CharactersModel.inc
Normal file
|
|
@ -0,0 +1,544 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Characters-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class CharactersModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new CharactersModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all characters for an user.
|
||||
*
|
||||
* @param int $userId ID of the user
|
||||
* @return array Characters
|
||||
*/
|
||||
public function getCharactersForUser($userId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url, seminaries.id AS seminary_id, seminaries.url AS seminary_url, seminaries.title AS seminary_title, seminaries.url AS seminary_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'LEFT JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'LEFT JOIN seminaries ON seminaries.id = charactertypes.seminary_id '.
|
||||
'WHERE user_id = ?',
|
||||
'i',
|
||||
$userId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Characters for a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of the Seminary
|
||||
* @return array Characters
|
||||
*/
|
||||
public function getCharactersForSeminary($seminaryId, $onlyWithRole=false)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url, seminaries.id AS seminary_url, seminaries.title AS seminary_title, seminaries.url AS seminary_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'LEFT JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'LEFT JOIN seminaries ON seminaries.id = charactertypes.seminary_id '.
|
||||
'WHERE seminaries.id = ?'.
|
||||
($onlyWithRole ? ' AND EXISTS (SELECT character_id FROM characters_characterroles WHERE character_id = characters.id)' : null),
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Characters for a Character group.
|
||||
*
|
||||
* @param int $groupId ID of the Character group
|
||||
* @return array Characters
|
||||
*/
|
||||
public function getCharactersForGroup($groupId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'LEFT JOIN characters_charactergroups ON characters_charactergroups.character_id = characters.id '.
|
||||
'LEFT JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'WHERE characters_charactergroups.charactergroup_id = ? '.
|
||||
'ORDER BY characters.xps DESC',
|
||||
'i',
|
||||
$groupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the character of a user for a Seminary.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $userId ID of the user
|
||||
* @param int $seminaryId ID of the Seminary
|
||||
* @return array Character data
|
||||
*/
|
||||
public function getCharacterForUserAndSeminary($userId, $seminaryId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'LEFT JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'WHERE characters.user_id = ? AND charactertypes.seminary_id = ?',
|
||||
'ii',
|
||||
$userId, $seminaryId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($userId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Character by its Url.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $seminaryId ID of the Seminary
|
||||
* @param string $characterUrl URL-name of the Character
|
||||
* @return array Character data
|
||||
*/
|
||||
public function getCharacterByUrl($seminaryId, $characterUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'LEFT JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'WHERE charactertypes.seminary_id = ? AND characters.url = ?',
|
||||
'is',
|
||||
$seminaryId, $characterUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($characterUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Character by its Id.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param string $characterId ID of the Character
|
||||
* @return array Character data
|
||||
*/
|
||||
public function getCharacterById($characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'LEFT JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'WHERE characters.id = ?',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($characterUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Characters with the most amount of Achievements.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $conut Amount of Characters to retrieve
|
||||
* @return array List of Characters
|
||||
*/
|
||||
public function getCharactersWithMostAchievements($seminaryId, $count, $alsoWithDeadline=true)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url, count(DISTINCT achievement_id) AS c '.
|
||||
'FROM achievements_characters '.
|
||||
'INNER JOIN achievements ON achievements.id = achievements_characters.achievement_id '.
|
||||
'INNER JOIN v_characters AS characters ON characters.id = achievements_characters.character_id '.
|
||||
'INNER JOIN characters_characterroles ON characters_characterroles.character_id = characters.id '.
|
||||
'INNER JOIN characterroles ON characterroles.id = characters_characterroles.characterrole_id AND characterroles.name = ? '.
|
||||
'INNER JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'WHERE achievements.seminary_id = ? AND deadline IS NULL '.
|
||||
(!$alsoWithDeadline ? 'AND achievements.deadline IS NULL ' : null).
|
||||
'GROUP BY achievements_characters.character_id '.
|
||||
'ORDER BY count(DISTINCT achievements_characters.achievement_id) DESC '.
|
||||
'LIMIT ?',
|
||||
'sii',
|
||||
'user',
|
||||
$seminaryId,
|
||||
$count
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculate only XPs for a Character achieved through Quests.
|
||||
*
|
||||
* @param int $characterId ID of Character
|
||||
* @return int Quest-XPs for Character
|
||||
*/
|
||||
public function getQuestXPsOfCharacter($characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT quest_xps '.
|
||||
'FROM v_charactersxps '.
|
||||
'WHERE character_id = ?',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['quest_xps'];
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the XP-level of a Character.
|
||||
*
|
||||
* @param string $characterId ID of the Character
|
||||
* @return array XP-level of Character
|
||||
*/
|
||||
public function getXPLevelOfCharacters($characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT xplevels.xps, xplevels.level, xplevels.name '.
|
||||
'FROM v_charactersxplevels '.
|
||||
'INNER JOIN xplevels ON xplevels.id = v_charactersxplevels.xplevel_id '.
|
||||
'WHERE v_charactersxplevels.character_id = ?',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the rank of a XP-value of a Character.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $xps XP-value to get rank for
|
||||
* @return int Rank of XP-value
|
||||
*/
|
||||
public function getXPRank($seminaryId, $xps)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT count(characters.id) AS c '.
|
||||
'FROM charactertypes '.
|
||||
'INNER JOIN v_characters AS characters ON characters.charactertype_id = charactertypes.id '.
|
||||
'INNER JOIN characters_characterroles ON characters_characterroles.character_id = characters.id '.
|
||||
'INNER JOIN characterroles ON characterroles.id = characters_characterroles.characterrole_id AND characterroles.name = ? '.
|
||||
'WHERE seminary_id = ? AND characters.xps > ?',
|
||||
'sid',
|
||||
'user',
|
||||
$seminaryId, $xps
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['c'] + 1;
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the superior $count Characters in the ranking.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $xps XP-value of Character
|
||||
* @param int $count Count of Characters to determine
|
||||
* @return array List of superior Characters
|
||||
*/
|
||||
public function getSuperiorCharacters($seminaryId, $xps, $count)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'INNER JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'INNER JOIN characters_characterroles ON characters_characterroles.character_id = characters.id '.
|
||||
'INNER JOIN characterroles ON characterroles.id = characters_characterroles.characterrole_id AND characterroles.name = ? '.
|
||||
'WHERE charactertypes.seminary_id = ? AND characters.xps > ? '.
|
||||
'ORDER BY characters.xps ASC, RAND() '.
|
||||
'LIMIT ?',
|
||||
'sidd',
|
||||
'user',
|
||||
$seminaryId, $xps, $count
|
||||
);
|
||||
$data = array_reverse($data);
|
||||
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the inferior $count Characters in the ranking.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $xps XP-value of Character
|
||||
* @param int $count Count of Characters to determine
|
||||
* @return array List of inferior Characters
|
||||
*/
|
||||
public function getInferiorCharacters($seminaryId, $characterId, $xps, $count)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'INNER JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'INNER JOIN characters_characterroles ON characters_characterroles.character_id = characters.id '.
|
||||
'INNER JOIN characterroles ON characterroles.id = characters_characterroles.characterrole_id AND characterroles.name = ? '.
|
||||
'WHERE charactertypes.seminary_id = ? AND characters.xps <= ? AND characters.id <> ? '.
|
||||
'ORDER BY characters.xps DESC, RAND() '.
|
||||
'LIMIT ?',
|
||||
'sidid',
|
||||
'user',
|
||||
$seminaryId, $xps, $characterId, $count
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Characters that solved a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to get Characters for
|
||||
* @return array Characters data
|
||||
*/
|
||||
public function getCharactersSolvedQuest($questId)
|
||||
{
|
||||
return $data = $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url, quests_characters.created AS submission_created '.
|
||||
'FROM quests_characters '.
|
||||
'INNER JOIN v_characters AS characters ON characters.id = quests_characters.character_id '.
|
||||
'INNER JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'WHERE quests_characters.quest_id = ? AND quests_characters.status = ? AND NOT EXISTS ('.
|
||||
'SELECT id '.
|
||||
'FROM quests_characters AS qc '.
|
||||
'WHERE qc.quest_id = quests_characters.quest_id AND qc.character_id = quests_characters.character_id AND qc.created > quests_characters.created'.
|
||||
') '.
|
||||
'ORDER BY quests_characters.created ASC',
|
||||
'ii',
|
||||
$questId, QuestsModel::QUEST_STATUS_SOLVED
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Characters that did not solv a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to get Characters for
|
||||
* @return array Characters data
|
||||
*/
|
||||
public function getCharactersUnsolvedQuest($questId)
|
||||
{
|
||||
return $data = $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url, quests_characters.created AS submission_created '.
|
||||
'FROM quests_characters '.
|
||||
'INNER JOIN v_characters AS characters ON characters.id = quests_characters.character_id '.
|
||||
'INNER JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'WHERE quests_characters.quest_id = ? AND quests_characters.status = ? AND NOT EXISTS ('.
|
||||
'SELECT id '.
|
||||
'FROM quests_characters AS qc '.
|
||||
'WHERE qc.quest_id = quests_characters.quest_id AND qc.character_id = quests_characters.character_id AND qc.created > quests_characters.created'.
|
||||
') '.
|
||||
'ORDER BY quests_characters.created ASC',
|
||||
'ii',
|
||||
$questId, QuestsModel::QUEST_STATUS_UNSOLVED
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Characters that sent a submission for a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to get Characters for
|
||||
* @return array Characters data
|
||||
*/
|
||||
public function getCharactersSubmittedQuest($questId)
|
||||
{
|
||||
return $data = $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url, quests_characters.created AS submission_created '.
|
||||
'FROM quests_characters '.
|
||||
'INNER JOIN v_characters AS characters ON characters.id = quests_characters.character_id '.
|
||||
'INNER JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'WHERE quests_characters.quest_id = ? AND quests_characters.status = ? AND NOT EXISTS ('.
|
||||
'SELECT id '.
|
||||
'FROM quests_characters AS qc '.
|
||||
'WHERE qc.quest_id = quests_characters.quest_id AND qc.character_id = quests_characters.character_id AND qc.created > quests_characters.created'.
|
||||
') '.
|
||||
'ORDER BY quests_characters.created ASC',
|
||||
'ii',
|
||||
$questId, QuestsModel::QUEST_STATUS_SUBMITTED
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param string $characterrole Character role
|
||||
* @return array List of users
|
||||
*/
|
||||
public function getCharactersWithCharacterRole($seminaryId, $characterrole)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT characters.id, characters.created, characters.charactertype_id, characters.name, characters.url, characters.user_id, characters.xps, characters.xplevel, characters.avatar_id, charactertypes.name AS charactertype_name, charactertypes.url AS charactertype_url, seminaries.id AS seminary_url, seminaries.title AS seminary_title, seminaries.url AS seminary_url '.
|
||||
'FROM v_characters AS characters '.
|
||||
'LEFT JOIN charactertypes ON charactertypes.id = characters.charactertype_id '.
|
||||
'LEFT JOIN seminaries ON seminaries.id = charactertypes.seminary_id '.
|
||||
'LEFT JOIN characters_characterroles ON characters_characterroles.character_id = characters.id '.
|
||||
'LEFT JOIN characterroles ON characterroles.id = characters_characterroles.characterrole_id '.
|
||||
'WHERE seminaries.id = ? AND characterroles.name = ?',
|
||||
'is',
|
||||
$seminaryId, $characterrole
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a Character name already exists.
|
||||
*
|
||||
* @param string $name Character name to check
|
||||
* @param int $characterId Do not check this ID (for editing)
|
||||
* @return boolean Whether Character name exists or not
|
||||
*/
|
||||
public function characterNameExists($name, $characterId=null)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id '.
|
||||
'FROM characters '.
|
||||
'WHERE name = ? OR url = ?',
|
||||
'ss',
|
||||
$name,
|
||||
\nre\core\Linker::createLinkParam($name)
|
||||
);
|
||||
|
||||
|
||||
return (!empty($data) && (is_null($characterId) || $characterId != $data[0]['id']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Character.
|
||||
*
|
||||
* @param int $userId User-ID that creates the new character
|
||||
* @param int $charactertypeId ID of type of new Character
|
||||
* @param string $characterName Name for the new Character
|
||||
* @return int ID of Character
|
||||
*/
|
||||
public function createCharacter($userId, $charactertypeId, $characterName)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO characters '.
|
||||
'(user_id, charactertype_id, name, url) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?, ?)',
|
||||
'iiss',
|
||||
$userId,
|
||||
$charactertypeId,
|
||||
$characterName,
|
||||
\nre\core\Linker::createLinkParam($characterName)
|
||||
);
|
||||
|
||||
|
||||
return $this->db->getInsertId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit a new Character.
|
||||
*
|
||||
* @param int $characterId ID of the Character to edit
|
||||
* @param int $charactertypeId ID of new type of Character
|
||||
* @param string $characterName New name for Character
|
||||
*/
|
||||
public function editCharacter($characterId, $charactertypeId, $characterName)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE characters '.
|
||||
'SET charactertype_id = ?, name = ?, url = ? '.
|
||||
'WHERE id = ?',
|
||||
'issi',
|
||||
$charactertypeId,
|
||||
$characterName,
|
||||
\nre\core\Linker::createLinkParam($characterName),
|
||||
$characterId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a Character.
|
||||
*
|
||||
* @param int $characterId ID of the Character to delete
|
||||
*/
|
||||
public function deleteCharacter($characterId)
|
||||
{
|
||||
$this->db->query('DELETE FROM characters WHERE id = ?', 'i', $characterId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
57
models/CharactertypesModel.inc
Normal file
57
models/CharactertypesModel.inc
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Charactertypes-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class CharactertypesModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new CharactertypesModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all Character types of a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary to get types of
|
||||
* @return array Character types
|
||||
*/
|
||||
public function getCharacterTypesForSeminary($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, name, url '.
|
||||
'FROM charactertypes '.
|
||||
'WHERE seminary_id = ? '.
|
||||
'ORDER BY name ASC',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
83
models/DatabaseModel.inc
Normal file
83
models/DatabaseModel.inc
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* NRE
|
||||
*
|
||||
* @author coderkun <olli@coderkun.de>
|
||||
* @copyright 2013 coderkun (http://www.coderkun.de)
|
||||
* @license http://www.gnu.org/licenses/gpl.html
|
||||
* @link http://www.coderkun.de/projects/nre
|
||||
*/
|
||||
|
||||
namespace nre\models;
|
||||
|
||||
|
||||
/**
|
||||
* Default implementation of a database model.
|
||||
*
|
||||
* @author coderkun <olli@coderkun.de>
|
||||
*/
|
||||
class DatabaseModel extends \nre\core\Model
|
||||
{
|
||||
/**
|
||||
* Database connection
|
||||
*
|
||||
* @static
|
||||
* @var DatabaseDriver
|
||||
*/
|
||||
protected $db = NULL;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new datamase model.
|
||||
*
|
||||
* @throws DatamodelException
|
||||
* @throws DriverNotFoundException
|
||||
* @throws DriverNotValidException
|
||||
* @param string $type Database type
|
||||
* @param array $config Connection settings
|
||||
*/
|
||||
function __construct($type, $config)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Load database driver
|
||||
$this->loadDriver($type);
|
||||
|
||||
// Establish database connection
|
||||
$this->connect($type, $config);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Load the database driver.
|
||||
*
|
||||
* @throws DriverNotFoundException
|
||||
* @throws DriverNotValidException
|
||||
* @param string $driverName Name of the database driver
|
||||
*/
|
||||
private function loadDriver($driverName)
|
||||
{
|
||||
\nre\core\Driver::load($driverName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Establish a connection to the database.
|
||||
*
|
||||
* @throws DatamodelException
|
||||
* @param string $driverName Name of the database driver
|
||||
* @param array $config Connection settings
|
||||
*/
|
||||
private function connect($driverName, $config)
|
||||
{
|
||||
$this->db = \nre\core\Driver::factory($driverName, $config);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
293
models/MediaModel.inc
Normal file
293
models/MediaModel.inc
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with the Media-tables.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class MediaModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new MediaModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get a medium by its URL.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param string $mediaURL URL-name of the Medium
|
||||
* @return array Medium data
|
||||
*/
|
||||
public function getMediaByUrl($mediaUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, name, url, description, mimetype '.
|
||||
'FROM media '.
|
||||
'WHERE url = ?',
|
||||
's',
|
||||
$mediaUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($mediaUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a medium by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $mediaId ID of the Medium
|
||||
* @return array Medium data
|
||||
*/
|
||||
public function getMediaById($mediaId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, name, url, description, mimetype '.
|
||||
'FROM media '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$mediaId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($mediaId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Seminary medium by its URL.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $seminaryId ID of the seminary
|
||||
* @param string $seminaryMediaUrl URL-name of the Seminary medium
|
||||
* @return array Seminary medium data
|
||||
*/
|
||||
public function getSeminaryMediaByUrl($seminaryId, $seminaryMediaUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, name, url, description, mimetype '.
|
||||
'FROM seminarymedia '.
|
||||
'WHERE url = ?',
|
||||
's',
|
||||
$seminaryMediaUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($seminaryMediaUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Seminary medium by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $seminaryMediaId ID of the Seminary medium
|
||||
* @return array Seminary medium data
|
||||
*/
|
||||
public function getSeminaryMediaById($mediaId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, name, url, description, mimetype '.
|
||||
'FROM seminarymedia '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$mediaId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($mediaId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Quests media by creating a new Seminarymedia and
|
||||
* adding it to the list of Quests media.
|
||||
*
|
||||
* @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 createQuestMedia($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 Quests media
|
||||
$this->db->query(
|
||||
'INSERT INTO questsmedia '.
|
||||
'(media_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 Character groups media by creating a new Seminarymedia and
|
||||
* adding it to the list of media for Character groups.
|
||||
*
|
||||
* @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 createCharactergroupMedia($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 Character groups media
|
||||
$this->db->query(
|
||||
'INSERT INTO charactergroupsmedia '.
|
||||
'(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 Seminary media.
|
||||
*
|
||||
* @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
|
||||
* @return mixed ID of media record or false if upload failed
|
||||
*/
|
||||
private function createSeminaryMedia($userId, $seminaryId, $filename, $description, $mimetype)
|
||||
{
|
||||
// Check for existing database record
|
||||
$data = $this->db->query(
|
||||
'SELECT id '.
|
||||
'FROM seminarymedia '.
|
||||
'WHERE url = ?',
|
||||
's',
|
||||
\nre\core\Linker::createLinkParam($filename)
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['id'];
|
||||
}
|
||||
|
||||
// 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
|
||||
);
|
||||
|
||||
|
||||
return $this->db->getInsertId();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
791
models/QuestgroupsModel.inc
Normal file
791
models/QuestgroupsModel.inc
Normal file
|
|
@ -0,0 +1,791 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Questgroups-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class QuestgroupsModel extends \hhu\z\Model
|
||||
{
|
||||
/**
|
||||
* Questgroup-status: Entered
|
||||
*
|
||||
* @var int;
|
||||
*/
|
||||
const QUESTGROUP_STATUS_ENTERED = 0;
|
||||
|
||||
/**
|
||||
* Required models
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $models = array('questgroupshierarchy', 'quests', 'questtexts');
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new QuestgroupsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all Questgroups for a Questgroup hierarchy.
|
||||
*
|
||||
* @param int $hierarchyId ID of the Questgroup hierarchy to get Questgroups for
|
||||
* @param int $parentQuestgroupId ID of the parent Questgroup hierarchy
|
||||
* @return array Questgroups for the given hierarchy
|
||||
*/
|
||||
public function getQuestgroupsForHierarchy($hierarchyId, $parentQuestgroupId=null)
|
||||
{
|
||||
// Get Questgroups
|
||||
$questgroups = array();
|
||||
if(is_null($parentQuestgroupId))
|
||||
{
|
||||
$questgroups = $this->db->query(
|
||||
'SELECT questgroups.id, questgroups_questgroupshierarchy.questgroupshierarchy_id, questgroups_questgroupshierarchy.pos, questgroups.title, questgroups.url, questgroups.questgroupspicture_id '.
|
||||
'FROM questgroups_questgroupshierarchy '.
|
||||
'INNER JOIN questgroups ON questgroups.id = questgroups_questgroupshierarchy.questgroup_id '.
|
||||
'WHERE questgroups_questgroupshierarchy.questgroupshierarchy_id = ? AND questgroups_questgroupshierarchy.parent_questgroup_id IS NULL '.
|
||||
'ORDER BY questgroups_questgroupshierarchy.pos ASC',
|
||||
'i',
|
||||
$hierarchyId
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$questgroups = $this->db->query(
|
||||
'SELECT questgroups.id, questgroups_questgroupshierarchy.questgroupshierarchy_id, questgroups_questgroupshierarchy.pos, questgroups.title, questgroups.url, questgroups.questgroupspicture_id '.
|
||||
'FROM questgroups_questgroupshierarchy '.
|
||||
'INNER JOIN questgroups ON questgroups.id = questgroups_questgroupshierarchy.questgroup_id '.
|
||||
'WHERE questgroups_questgroupshierarchy.questgroupshierarchy_id = ? AND questgroups_questgroupshierarchy.parent_questgroup_id = ? '.
|
||||
'ORDER BY questgroups_questgroupshierarchy.pos ASC',
|
||||
'ii',
|
||||
$hierarchyId, $parentQuestgroupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// Return Questgroups
|
||||
return $questgroups;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Questgroups for a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @return array List of Questgroups
|
||||
*/
|
||||
public function getQuestgroupsForSeminary($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, title, url '.
|
||||
'FROM questgroups '.
|
||||
'WHERE seminary_id = ? '.
|
||||
'ORDER BY title ASC',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Questgroup by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $questgroupId ID of a Questgroup
|
||||
* @return array Questgroup data
|
||||
*/
|
||||
public function getQuestgroupById($questgroupId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, title, url, questgroupspicture_id '.
|
||||
'FROM questgroups '.
|
||||
'WHERE questgroups.id = ?',
|
||||
'i',
|
||||
$questgroupId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questgroupId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Questgroup by its URL.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $seminaryId ID of the corresponding seminary
|
||||
* @param string $questgroupURL URL-title of a Questgroup
|
||||
* @return array Questgroup data
|
||||
*/
|
||||
public function getQuestgroupByUrl($seminaryId, $questgroupUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, title, url, questgroupspicture_id '.
|
||||
'FROM questgroups '.
|
||||
'WHERE seminary_id = ? AND url = ?',
|
||||
'is',
|
||||
$seminaryId, $questgroupUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questgroupUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get texts of a Questgroup.
|
||||
*
|
||||
* @param int $questgroupId ID of a Questgroup
|
||||
* @return array Texts of this Questgroup
|
||||
*/
|
||||
public function getQuestgroupTexts($questgroupId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, pos, text '.
|
||||
'FROM questgrouptexts '.
|
||||
'WHERE questgroup_id = ? '.
|
||||
'ORDER BY pos ASC',
|
||||
'i',
|
||||
$questgroupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the first text of a Questgroup.
|
||||
*
|
||||
* @param int $questgroupId ID of a Questgroup
|
||||
* @return string First text of this Questgroup or NULL
|
||||
*/
|
||||
public function getFirstQuestgroupText($questgroupId)
|
||||
{
|
||||
// Text of Questgroup itself
|
||||
$questgroupTexts = $this->getQuestgroupTexts($questgroupId);
|
||||
if(!empty($questgroupTexts)) {
|
||||
return $questgroupTexts[0]['text'];
|
||||
}
|
||||
|
||||
// Text of first Quest
|
||||
$quest = $this->Quests->getFirstQuestOfQuestgroup($questgroupId);
|
||||
if(!is_null($quest))
|
||||
{
|
||||
$questText = $this->Questtexts->getFirstQuestText($quest['id']);
|
||||
if(!is_null($questText)) {
|
||||
return $questText;
|
||||
}
|
||||
}
|
||||
|
||||
// Text of ChildQuestgroups
|
||||
$questgroupHierarchy = $this->Questgroupshierarchy->getHierarchyForQuestgroup($questgroupId);
|
||||
$childQuestgroupshierarchy = $this->Questgroupshierarchy->getChildQuestgroupshierarchy($questgroupHierarchy['id']);
|
||||
foreach($childQuestgroupshierarchy as &$hierarchy)
|
||||
{
|
||||
// Get Questgroups
|
||||
$questgroups = $this->getQuestgroupsForHierarchy($hierarchy['id'], $questgroupId);
|
||||
foreach($questgroups as &$group)
|
||||
{
|
||||
$childQuestgroupText = $this->getFirstQuestgroupText($group['id']);
|
||||
if(!is_null($childQuestgroupText)) {
|
||||
return $childQuestgroupText;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// No text found
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the next Questgroup.
|
||||
*
|
||||
* Determine the next Questgroup. If there is no next Questgroup
|
||||
* on the same level as the given Quest then the followed-up
|
||||
* Questgroup from a higher hierarchy level is returned.
|
||||
*
|
||||
* @param int $questgroupId ID of Questgroup to get next Questgroup of
|
||||
* @return array Questgroup data
|
||||
*/
|
||||
public function getNextQuestgroup($questgroupId)
|
||||
{
|
||||
$currentQuestgroup = $this->getQuestgroupById($questgroupId);
|
||||
$currentQuestgroup['hierarchy'] = $this->Questgroupshierarchy->getHierarchyForQuestgroup($currentQuestgroup['id']);
|
||||
if(empty($currentQuestgroup['hierarchy'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$nextQuestgroup = $this->_getNextQuestgroup($currentQuestgroup['hierarchy']['parent_questgroup_id'], $currentQuestgroup['hierarchy']['questgroup_pos']);
|
||||
if(is_null($nextQuestgroup) && !is_null($currentQuestgroup['hierarchy']['parent_questgroup_id'])) {
|
||||
$nextQuestgroup = $this->getNextQuestgroup($currentQuestgroup['hierarchy']['parent_questgroup_id']);
|
||||
}
|
||||
|
||||
|
||||
return $nextQuestgroup;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the previous Questgroup.
|
||||
*
|
||||
* Determine the previous Questgroup. If there is no previous
|
||||
* Questgroup on the same level as the given Quest then the
|
||||
* followed-up Questgroup from a higher hierarchy level is
|
||||
* returned.
|
||||
*
|
||||
* @param int $questgroupId ID of Questgroup to get previous Questgroup of
|
||||
* @return array Questgroup data
|
||||
*/
|
||||
public function getPreviousQuestgroup($questgroupId)
|
||||
{
|
||||
$currentQuestgroup = $this->getQuestgroupById($questgroupId);
|
||||
$currentQuestgroup['hierarchy'] = $this->Questgroupshierarchy->getHierarchyForQuestgroup($currentQuestgroup['id']);
|
||||
if(empty($currentQuestgroup['hierarchy'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$previousQuestgroup = $this->_getPreviousQuestgroup($currentQuestgroup['hierarchy']['parent_questgroup_id'], $currentQuestgroup['hierarchy']['questgroup_pos']);
|
||||
if(is_null($previousQuestgroup) && !is_null($currentQuestgroup['hierarchy']['parent_questgroup_id'])) {
|
||||
$previousQuestgroup = $this->getPreviousQuestgroup($currentQuestgroup['hierarchy']['parent_questgroup_id']);
|
||||
}
|
||||
|
||||
|
||||
return $previousQuestgroup;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mark a Questgroup as entered for a Character.
|
||||
*
|
||||
* @param int $questId ID of Quest to mark as entered
|
||||
* @param int $characterId ID of Character that entered the Quest
|
||||
*/
|
||||
public function setQuestgroupEntered($questgroupId, $characterId)
|
||||
{
|
||||
$this->setQuestgroupStatus($questgroupId, $characterId, self::QUESTGROUP_STATUS_ENTERED, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the given Character has entered a Questgroup.
|
||||
*
|
||||
* @param int $questgroupId ID of Questgroup to check
|
||||
* @param int $characterId ID of Character to check
|
||||
* @result boolean Whether Character has entered the Questgroup or not
|
||||
*/
|
||||
public function hasCharacterEnteredQuestgroup($questgroupId, $characterId)
|
||||
{
|
||||
$count = $this->db->query(
|
||||
'SELECT count(id) AS c '.
|
||||
'FROM questgroups_characters '.
|
||||
'WHERE questgroup_id = ? AND character_id = ? AND status IN (?)',
|
||||
'iii',
|
||||
$questgroupId,
|
||||
$characterId,
|
||||
self::QUESTGROUP_STATUS_ENTERED
|
||||
);
|
||||
|
||||
|
||||
return (!empty($count) && intval($count[0]['c']) > 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the given Character has solved the Quests form
|
||||
* this Questgroup.
|
||||
*
|
||||
* @param int $questgroupId ID of Questgroup to check
|
||||
* @param int $characterId ID of Character to check
|
||||
* @result boolean Whether Character has solved the Questgroup or not
|
||||
*/
|
||||
public function hasCharacterSolvedQuestgroup($questgroupId, $characterId)
|
||||
{
|
||||
// Get data of Questgroup
|
||||
$questgroup = $this->getQuestgroupById($questgroupId);
|
||||
|
||||
// Chack all Quests
|
||||
$currentQuest = $this->Quests->getFirstQuestOfQuestgroup($questgroup['id']);
|
||||
if(!is_null($currentQuest))
|
||||
{
|
||||
if(!$this->Quests->hasCharacterSolvedQuest($currentQuest['id'], $characterId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get next Quests
|
||||
$nextQuests = !is_null($currentQuest) ? $this->Quests->getNextQuests($currentQuest['id']) : null;
|
||||
while(!is_null($currentQuest) && !empty($nextQuests))
|
||||
{
|
||||
// Get choosed Quest
|
||||
$currentQuest = null;
|
||||
foreach($nextQuests as &$nextQuest) {
|
||||
if($this->Quests->hasCharacterEnteredQuest($nextQuest['id'], $characterId)) {
|
||||
$currentQuest = $nextQuest;
|
||||
}
|
||||
}
|
||||
|
||||
// Check Quest
|
||||
if(is_null($currentQuest)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check status
|
||||
if(!$this->Quests->hasCharacterSolvedQuest($currentQuest['id'], $characterId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$nextQuests = !is_null($currentQuest) ? $this->Quests->getNextQuests($currentQuest['id']) : null;
|
||||
}
|
||||
}
|
||||
|
||||
// Check all child Questgroups
|
||||
$questgroup['hierarchy'] = $this->Questgroupshierarchy->getHierarchyForQuestgroup($questgroup['id']);
|
||||
if(!empty($questgroup['hierarchy']))
|
||||
{
|
||||
$childQuestgroupshierarchy = $this->Questgroupshierarchy->getChildQuestgroupshierarchy($questgroup['hierarchy']['id']);
|
||||
foreach($childQuestgroupshierarchy as &$hierarchy)
|
||||
{
|
||||
// Get Questgroups
|
||||
$questgroups = $this->getQuestgroupsForHierarchy($hierarchy['id'], $questgroup['id']);
|
||||
foreach($questgroups as &$group) {
|
||||
if(!$this->hasCharacterSolvedQuestgroup($group['id'], $characterId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all related Questgroups of a Questtext.
|
||||
*
|
||||
* @param int $questtextId ID of the Questtext
|
||||
* @return array Related Questgroups for the Questtext
|
||||
*/
|
||||
public function getRelatedQuestsgroupsOfQuesttext($questtextId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT questgroups.id, questgroups_questtexts.questtext_id, questgroups.title, questgroups.url, questgroups_questtexts.entry_text '.
|
||||
'FROM questgroups_questtexts '.
|
||||
'INNER JOIN questgroups ON questgroups.id = questgroups_questtexts.questgroup_id '.
|
||||
'WHERE questgroups_questtexts.questtext_id = ?',
|
||||
'i',
|
||||
$questtextId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all related Questgroups of a Quest.
|
||||
*
|
||||
* @param int $questId ID of the Quest
|
||||
* @return array Related Quests for the Quest
|
||||
*/
|
||||
public function getRelatedQuestsgroupsOfQuest($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT questgroups_questtexts.questgroup_id AS id '.
|
||||
'FROM quests '.
|
||||
'INNER JOIN questtexts ON questtexts.quest_id = quests.id '.
|
||||
'INNER JOIN questgroups_questtexts ON questgroups_questtexts.questtext_id = questtexts.id '.
|
||||
'WHERE quests.id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all related Questgroups of a Questgroup.
|
||||
*
|
||||
* @param int $questgroupId ID of the Questgroup
|
||||
* @return array Related Questgroups for the Questgroup
|
||||
*/
|
||||
public function getRelatedQuestsgroupsOfQuestgroup($questgroupId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT DISTINCT questgroups_questtexts.questgroup_id AS id '.
|
||||
'FROM questgroups '.
|
||||
'INNER JOIN quests ON quests.questgroup_id = questgroups.id '.
|
||||
'INNER JOIN questtexts ON questtexts.quest_id = quests.id '.
|
||||
'INNER JOIN questgroups_questtexts ON questgroups_questtexts.questtext_id = questtexts.id '.
|
||||
'WHERE questgroups.id = ?',
|
||||
'i',
|
||||
$questgroupId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculate cumulated data for a Questgroup, its
|
||||
* sub-Questgroups and all its Quests.
|
||||
*
|
||||
* @param int $questgroupId ID of Questgroup
|
||||
* @param int $characterId ID of Character
|
||||
* @param array $calculatedQuests IDs of already calculated Quests
|
||||
* @return array Cumulated data for Questgroup
|
||||
*/
|
||||
public function getCumulatedDataForQuestgroup($questgroupId, $characterId=null, &$calculatedQuests=array())
|
||||
{
|
||||
// Cumulated data
|
||||
$data = array(
|
||||
'xps' => 0,
|
||||
'character_xps' => 0
|
||||
);
|
||||
|
||||
// Current Questgroup
|
||||
$questgroup = $this->getQuestgroupById($questgroupId);
|
||||
|
||||
// Quests of current Questgroup
|
||||
$quest = $this->Quests->getFirstQuestOfQuestgroup($questgroup['id']);
|
||||
if(!is_null($quest))
|
||||
{
|
||||
$questData = $this->getCumulatedDataForQuest($quest, $characterId, $calculatedQuests);
|
||||
$data['xps'] += $questData['xps'];
|
||||
$data['character_xps'] += $questData['character_xps'];
|
||||
}
|
||||
|
||||
// XPs of child Questgroups
|
||||
$questgroupHierarchy = $this->Questgroupshierarchy->getHierarchyForQuestgroup($questgroup['id']);
|
||||
if(!empty($questgroupHierarchy))
|
||||
{
|
||||
$childQuestgroupshierarchy = $this->Questgroupshierarchy->getChildQuestgroupshierarchy($questgroupHierarchy['id']);
|
||||
foreach($childQuestgroupshierarchy as &$hierarchy)
|
||||
{
|
||||
$questgroups = $this->getQuestgroupsForHierarchy($hierarchy['id'], $questgroup['id']);
|
||||
foreach($questgroups as &$questgroup)
|
||||
{
|
||||
$childData = $this->getCumulatedDataForQuestgroup($questgroup['id'], $characterId, $calculatedQuests);
|
||||
$data['xps'] += $childData['xps'];
|
||||
$data['character_xps'] += $childData['character_xps'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Return cumulated data
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculate cumulated data of the given Quest, its following
|
||||
* Quests and its related Questgroups.
|
||||
*
|
||||
* @param array $quest Quest data
|
||||
* @param int $characterId ID of Character
|
||||
* @param array $calculatedQuests IDs of already calculated Quests
|
||||
* @return array Cumulated data for Quest
|
||||
*/
|
||||
public function getCumulatedDataForQuest($quest, $characterId=null, &$calculatedQuests=array())
|
||||
{
|
||||
// Cumulated data
|
||||
$data = array(
|
||||
'xps' => $quest['xps'],
|
||||
'character_xps' => (!is_null($characterId) && $this->Quests->hasCharacterSolvedQuest($quest['id'], $characterId)) ? $quest['xps'] : 0
|
||||
);
|
||||
|
||||
// Related Questgroups
|
||||
$relatedQuestgroups = $this->getRelatedQuestsgroupsOfQuest($quest['id']);
|
||||
foreach($relatedQuestgroups as &$relatedQuestgroup)
|
||||
{
|
||||
$relatedData = $this->getCumulatedDataForQuestgroup($relatedQuestgroup['id'], $characterId, $calculatedQuests);
|
||||
$data['xps'] += $relatedData['xps'];
|
||||
$data['character_xps'] += $relatedData['character_xps'];
|
||||
}
|
||||
|
||||
// Next Quests
|
||||
$nextQuests = $this->Quests->getNextQuests($quest['id']);
|
||||
$allNextData = array(
|
||||
'xps' => array(0),
|
||||
'character_xps' => array(0),
|
||||
);
|
||||
foreach($nextQuests as &$nextQuest)
|
||||
{
|
||||
if(!array_key_exists($nextQuest['id'], $calculatedQuests))
|
||||
{
|
||||
$nextData = $this->getCumulatedDataForQuest($nextQuest, $characterId, $calculatedQuests);
|
||||
$calculatedQuests[$nextQuest['id']] = array(
|
||||
'xps' => $nextData['xps'],
|
||||
'character_xps' => $nextData['character_xps']
|
||||
);
|
||||
}
|
||||
|
||||
$allNextData['xps'][] = $calculatedQuests[$nextQuest['id']]['xps'];
|
||||
$allNextData['character_xps'][] = $calculatedQuests[$nextQuest['id']]['character_xps'];
|
||||
}
|
||||
$data['xps'] += max($allNextData['xps']);
|
||||
$data['character_xps'] += max($allNextData['character_xps']);
|
||||
|
||||
|
||||
// Return cumulated data
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Summarize XPs of all Quests for a Questgroup and its
|
||||
* sub-Questgroups solved by a Character.
|
||||
*
|
||||
* @param int $questgroupId ID of Questgroup
|
||||
* @param int $characterId ID of Character
|
||||
* @return int Sum of XPs
|
||||
*/
|
||||
public function getAchievedXPsForQuestgroup($questgroupId, $characterId)
|
||||
{
|
||||
// Sum of XPs
|
||||
$xps = 0;
|
||||
|
||||
// Current Questgroup
|
||||
$questgroup = $this->getQuestgroupById($questgroupId);
|
||||
|
||||
// Quests of current Questgroup
|
||||
$quest = $this->Quests->getFirstQuestOfQuestgroup($questgroup['id']);
|
||||
if(!is_null($quest)) {
|
||||
$xps += $this->getAchievedXPsForQuest($quest, $characterId);
|
||||
}
|
||||
|
||||
// XPs of child Questgroups
|
||||
$questgroupHierarchy = $this->Questgroupshierarchy->getHierarchyForQuestgroup($questgroup['id']);
|
||||
if(empty($questgroupHierarchy)) {
|
||||
return $xps;
|
||||
}
|
||||
$childQuestgroupshierarchy = $this->Questgroupshierarchy->getChildQuestgroupshierarchy($questgroupHierarchy['id']);
|
||||
foreach($childQuestgroupshierarchy as &$hierarchy)
|
||||
{
|
||||
$questgroups = $this->getQuestgroupsForHierarchy($hierarchy['id'], $questgroup['id']);
|
||||
foreach($questgroups as &$questgroup) {
|
||||
$xps += $this->getAchievedXPsForQuestgroup($questgroup['id'], $characterId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Return summarized XPs
|
||||
return $xps;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Summarize XPs of the given Quest, its following Quests and
|
||||
* its related Questgroups solved by a Character.
|
||||
*
|
||||
* @param int $quest Quest to summarize XPs for
|
||||
* @param int $characterId ID of Character
|
||||
* @return int Sum of XPs
|
||||
*/
|
||||
public function getAchievedXPsForQuest($quest, $characterId)
|
||||
{
|
||||
$xps = 0;
|
||||
|
||||
// XPs for the given Quest
|
||||
if($this->Quests->hasCharacterSolvedQuest($quest['id'], $characterId))
|
||||
{
|
||||
$xps += $quest['xps'];
|
||||
|
||||
// Next Quests
|
||||
$nextQuests = $this->Quests->getNextQuests($quest['id']);
|
||||
foreach($nextQuests as &$nextQuest)
|
||||
{
|
||||
if($this->Quests->hasCharacterEnteredQuest($nextQuest['id'], $characterId))
|
||||
{
|
||||
$xps += $this->getAchievedXPsForQuest($nextQuest, $characterId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Related Questgroups
|
||||
$relatedQuestgroups = $this->getRelatedQuestsgroupsOfQuest($quest['id']);
|
||||
foreach($relatedQuestgroups as &$relatedQuestgroup) {
|
||||
$xps += $this->getAchievedXPsForQuestgroup($relatedQuestgroup['id'], $characterId);
|
||||
}
|
||||
|
||||
|
||||
// Return summarized XPs
|
||||
return $xps;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Questgroup.
|
||||
*
|
||||
* @param int $userId User-ID that creates the new character
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param string $title Title for new Questgroup
|
||||
* @return int ID of new Questgroup
|
||||
*/
|
||||
public function createQuestgroup($userId, $seminaryId, $title)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO questgroups '.
|
||||
'(created_user_id, seminary_id, title, url) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?, ?)',
|
||||
'iiss',
|
||||
$userId,
|
||||
$seminaryId,
|
||||
$title,
|
||||
\nre\core\Linker::createLinkParam($title)
|
||||
);
|
||||
|
||||
|
||||
return $this->db->getInsertId();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the next (direct) Questgroup.
|
||||
*
|
||||
* @param int $parentQuestgroupId ID of parent Questgroup to get next Questgroup of
|
||||
* @param int $questgroupPos Position of Questgroup to get next Questgroup of
|
||||
* @return array Data of next Questgroup or NULL
|
||||
*/
|
||||
private function _getNextQuestgroup($parentQuestgroupId, $questgroupPos)
|
||||
{
|
||||
if(!is_null($parentQuestgroupId))
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT * '.
|
||||
'FROM questgroups_questgroupshierarchy '.
|
||||
'INNER JOIN questgroups ON questgroups.id = questgroups_questgroupshierarchy.questgroup_id '.
|
||||
'WHERE parent_questgroup_id = ? AND pos = ? + 1',
|
||||
'ii',
|
||||
$parentQuestgroupId, $questgroupPos
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT * '.
|
||||
'FROM questgroups_questgroupshierarchy '.
|
||||
'INNER JOIN questgroups ON questgroups.id = questgroups_questgroupshierarchy.questgroup_id '.
|
||||
'WHERE parent_questgroup_id IS NULL AND pos = ? + 1',
|
||||
'i',
|
||||
$questgroupPos
|
||||
);
|
||||
}
|
||||
if(empty($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the previous (direct) Questgroup.
|
||||
*
|
||||
* @param int $parentQuestgroupId ID of parent Questgroup to get previous Questgroup of
|
||||
* @param int $questgroupPos Position of Questgroup to get previous Questgroup of
|
||||
* @return array Data of previous Questgroup or NULL
|
||||
*/
|
||||
private function _getPreviousQuestgroup($parentQuestgroupId, $questgroupPos)
|
||||
{
|
||||
if(!is_null($parentQuestgroupId))
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT * '.
|
||||
'FROM questgroups_questgroupshierarchy '.
|
||||
'INNER JOIN questgroups ON questgroups.id = questgroups_questgroupshierarchy.questgroup_id '.
|
||||
'WHERE parent_questgroup_id = ? AND pos = ? - 1',
|
||||
'ii',
|
||||
$parentQuestgroupId, $questgroupPos
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT * '.
|
||||
'FROM questgroups_questgroupshierarchy '.
|
||||
'INNER JOIN questgroups ON questgroups.id = questgroups_questgroupshierarchy.questgroup_id '.
|
||||
'WHERE parent_questgroup_id IS NULL AND pos = ? - 1',
|
||||
'i',
|
||||
$questgroupPos
|
||||
);
|
||||
}
|
||||
if(empty($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mark a Questgroup for a Character.
|
||||
*
|
||||
* @param int $questgroupId ID of Questgroup to mark
|
||||
* @param int $characterId ID of Character to mark the Questgroup for
|
||||
* @param int $status Questgroup status to mark
|
||||
* @param boolean $repeated Insert although status is already set for this Questgroup and Character
|
||||
*/
|
||||
private function setQuestgroupStatus($questgroupId, $characterId, $status, $repeated=true)
|
||||
{
|
||||
// Check if status is already set
|
||||
if(!$repeated)
|
||||
{
|
||||
$count = $this->db->query(
|
||||
'SELECT count(*) AS c '.
|
||||
'FROM questgroups_characters '.
|
||||
'WHERE questgroup_id = ? AND character_id = ? AND status = ?',
|
||||
'iii',
|
||||
$questgroupId,
|
||||
$characterId,
|
||||
$status
|
||||
);
|
||||
if(!empty($count) && intval($count[0]['c']) > 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Set status
|
||||
$this->db->query(
|
||||
'INSERT INTO questgroups_characters '.
|
||||
'(questgroup_id, character_id, status) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) ',
|
||||
'iii',
|
||||
$questgroupId,
|
||||
$characterId,
|
||||
$status
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
128
models/QuestgroupshierarchyModel.inc
Normal file
128
models/QuestgroupshierarchyModel.inc
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Questgroupshierarchy-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class QuestgroupshierarchyModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new QuestgroupshierarchyModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get a Questgroup hierarchy by its ID.
|
||||
*
|
||||
* throws IdNotFoundException
|
||||
* @param int $questgroupshierarchyId ID of a Questgroup hierarchy
|
||||
* @return array Questgroup hierarchy
|
||||
*/
|
||||
public function getHierarchyById($questgroupshierarchyId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, seminary_id, parent_questgroupshierarchy_id, pos, title_singular, title_plural, url '.
|
||||
'FROM questgroupshierarchy '.
|
||||
'WHERE questgroupshierarchy.id = ?',
|
||||
'i',
|
||||
$questgroupshierarchyId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questgroupshierarchyId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the toplevel hierarchy entries of a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of the seminary to get hierarchy for
|
||||
* @return array Toplevel hierarchy
|
||||
*/
|
||||
public function getHierarchyOfSeminary($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, seminary_id, parent_questgroupshierarchy_id, pos, title_singular, title_plural, url '.
|
||||
'FROM questgroupshierarchy '.
|
||||
'WHERE '.
|
||||
'questgroupshierarchy.seminary_id = ? AND '.
|
||||
'questgroupshierarchy.parent_questgroupshierarchy_id IS NULL '.
|
||||
'ORDER BY questgroupshierarchy.pos ASC',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the Questgroup-Hierarchy for a Questgroup.
|
||||
*
|
||||
* @param int $questgroupId ID of Questgroup
|
||||
* @return array Hierarchy for Questgroup
|
||||
*/
|
||||
public function getHierarchyForQuestgroup($questgroupId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT questgroups_questgroupshierarchy.parent_questgroup_id, questgroups_questgroupshierarchy.pos AS questgroup_pos, questgroupshierarchy.id, questgroupshierarchy.seminary_id, questgroupshierarchy.parent_questgroupshierarchy_id, questgroupshierarchy.pos, questgroupshierarchy.title_singular, questgroupshierarchy.title_plural, questgroupshierarchy.url '.
|
||||
'FROM questgroups_questgroupshierarchy '.
|
||||
'INNER JOIN questgroupshierarchy ON questgroupshierarchy.id = questgroups_questgroupshierarchy.questgroupshierarchy_id '.
|
||||
'WHERE questgroups_questgroupshierarchy.questgroup_id = ?',
|
||||
'i',
|
||||
$questgroupId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the child hierarchy entries of a Questgroup hierarchy.
|
||||
*
|
||||
* @param int $questgroupshierarchyId ID of a Questgroup hierarchy
|
||||
* @return array Child Questgroup hierarchy entries
|
||||
*/
|
||||
public function getChildQuestgroupshierarchy($questgroupshierarchyId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, seminary_id, parent_questgroupshierarchy_id, pos, title_singular, title_plural, url '.
|
||||
'FROM questgroupshierarchy '.
|
||||
'WHERE questgroupshierarchy.parent_questgroupshierarchy_id = ? '.
|
||||
'ORDER BY questgroupshierarchy.pos ASC',
|
||||
'i',
|
||||
$questgroupshierarchyId
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
488
models/QuestsModel.inc
Normal file
488
models/QuestsModel.inc
Normal file
|
|
@ -0,0 +1,488 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Quests-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class QuestsModel extends \hhu\z\Model
|
||||
{
|
||||
/**
|
||||
* Quest-status: Entered
|
||||
*
|
||||
* @var int;
|
||||
*/
|
||||
const QUEST_STATUS_ENTERED = 0;
|
||||
/**
|
||||
* Quest-status: submitted
|
||||
*
|
||||
* @var int;
|
||||
*/
|
||||
const QUEST_STATUS_SUBMITTED = 1;
|
||||
/**
|
||||
* Quest-status: Unsolved
|
||||
*
|
||||
* @var int;
|
||||
*/
|
||||
const QUEST_STATUS_UNSOLVED = 2;
|
||||
/**
|
||||
* Quest-status: Solved
|
||||
*
|
||||
* @var int;
|
||||
*/
|
||||
const QUEST_STATUS_SOLVED = 3;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new QuestsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get a Quest and its data by its URL.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $seminaryId ID of the corresponding Seminary
|
||||
* @param int $questgroupId ID of the corresponding Questgroup
|
||||
* @param string $questURL URL-title of a Quest
|
||||
* @return array Quest data
|
||||
*/
|
||||
public function getQuestByUrl($seminaryId, $questgroupId, $questUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT quests.id, quests.questgroup_id, quests.questtype_id, quests.title, quests.url, quests.xps, quests.entry_text, quests.task, quests.wrong_text, quests.questsmedia_id '.
|
||||
'FROM quests '.
|
||||
'LEFT JOIN questgroups ON questgroups.id = quests.questgroup_id '.
|
||||
'WHERE questgroups.seminary_id = ? AND questgroups.id = ? AND quests.url = ?',
|
||||
'iis',
|
||||
$seminaryId, $questgroupId, $questUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Quest and its data by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param string $questId ID of a Quest
|
||||
* @return array Quest data
|
||||
*/
|
||||
public function getQuestById($questId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT quests.id, quests.questgroup_id, quests.questtype_id, quests.title, quests.url, quests.xps, quests.entry_text, quests.task, quests.wrong_text, quests.questsmedia_id '.
|
||||
'FROM quests '.
|
||||
'LEFT JOIN questgroups ON questgroups.id = quests.questgroup_id '.
|
||||
'WHERE quests.id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the first Quest of a Qusetgroup.
|
||||
*
|
||||
* @param int $questId ID of Questgroup
|
||||
* @return array Data of first Quest
|
||||
*/
|
||||
public function getFirstQuestOfQuestgroup($questgroupId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, questtype_id, title, url, xps, task '.
|
||||
'FROM quests '.
|
||||
'LEFT JOIN quests_previousquests ON quests_previousquests.quest_id = quests.id '.
|
||||
'WHERE questgroup_id = ? AND quests_previousquests.previous_quest_id IS NULL',
|
||||
'i',
|
||||
$questgroupId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Quests that follow-up a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to get next Quests of
|
||||
* @return array Quests data
|
||||
*/
|
||||
public function getNextQuests($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT quests.id, quests.questtype_id, quests.title, quests.url, quests.xps, quests.entry_text, quests.task, questgroups.title AS questgroup_title, questgroups.url AS questgroup_url '.
|
||||
'FROM quests_previousquests '.
|
||||
'INNER JOIN quests ON quests.id = quests_previousquests.quest_id '.
|
||||
'INNER JOIN questgroups ON questgroups.id = quests.questgroup_id '.
|
||||
'WHERE quests_previousquests.previous_quest_id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Quests that the given Quests follows-up to.
|
||||
*
|
||||
* @param int $questId ID of Quest to get previous Quests of
|
||||
* @return array Quests data
|
||||
*/
|
||||
public function getPreviousQuests($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT quests.id, quests.title, quests.url, quests.entry_text, questgroups.title AS questgroup_title, questgroups.url AS questgroup_url '.
|
||||
'FROM quests_previousquests '.
|
||||
'INNER JOIN quests ON quests.id = quests_previousquests.previous_quest_id '.
|
||||
'INNER JOIN questgroups ON questgroups.id = quests.questgroup_id '.
|
||||
'WHERE quests_previousquests.quest_id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mark a Quest as entered for a Character.
|
||||
*
|
||||
* @param int $questId ID of Quest to mark as entered
|
||||
* @param int $characterId ID of Character that entered the Quest
|
||||
*/
|
||||
public function setQuestEntered($questId, $characterId)
|
||||
{
|
||||
$this->setQuestStatus($questId, $characterId, self::QUEST_STATUS_ENTERED, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mark a Quest as submitted for a Character.
|
||||
*
|
||||
* @param int $questId ID of Quest to mark as unsolved
|
||||
* @param int $characterId ID of Character that unsolved the Quest
|
||||
*/
|
||||
public function setQuestSubmitted($questId, $characterId)
|
||||
{
|
||||
$this->setQuestStatus($questId, $characterId, self::QUEST_STATUS_SUBMITTED);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mark a Quest as unsolved for a Character.
|
||||
*
|
||||
* @param int $questId ID of Quest to mark as unsolved
|
||||
* @param int $characterId ID of Character that unsolved the Quest
|
||||
*/
|
||||
public function setQuestUnsolved($questId, $characterId)
|
||||
{
|
||||
$this->setQuestStatus($questId, $characterId, self::QUEST_STATUS_UNSOLVED);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Mark a Quest as solved for a Character.
|
||||
*
|
||||
* @param int $questId ID of Quest to mark as solved
|
||||
* @param int $characterId ID of Character that solved the Quest
|
||||
*/
|
||||
public function setQuestSolved($questId, $characterId)
|
||||
{
|
||||
$this->setQuestStatus($questId, $characterId, self::QUEST_STATUS_SOLVED, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the given Character has entered a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to check
|
||||
* @param int $characterId ID of Character to check
|
||||
* @result boolean Whether Character has entered the Quest or not
|
||||
*/
|
||||
public function hasCharacterEnteredQuest($questId, $characterId)
|
||||
{
|
||||
$count = $this->db->query(
|
||||
'SELECT count(id) AS c '.
|
||||
'FROM quests_characters '.
|
||||
'WHERE quest_id = ? AND character_id = ? AND status IN (?,?,?)',
|
||||
'iiiii',
|
||||
$questId,
|
||||
$characterId,
|
||||
self::QUEST_STATUS_ENTERED, self::QUEST_STATUS_SOLVED, self::QUEST_STATUS_UNSOLVED
|
||||
);
|
||||
|
||||
|
||||
return (!empty($count) && intval($count[0]['c']) > 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the given Character has tried to solve a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to check
|
||||
* @param int $characterId ID of Character to check
|
||||
* @result boolean Whether Character has tried to solved the Quest or not
|
||||
*/
|
||||
public function hasCharacterTriedQuest($questId, $characterId)
|
||||
{
|
||||
$count = $this->db->query(
|
||||
'SELECT count(id) AS c '.
|
||||
'FROM quests_characters '.
|
||||
'WHERE quest_id = ? AND character_id = ? AND status IN (?,?)',
|
||||
'iiii',
|
||||
$questId,
|
||||
$characterId,
|
||||
self::QUEST_STATUS_SOLVED, self::QUEST_STATUS_UNSOLVED
|
||||
);
|
||||
|
||||
|
||||
return (!empty($count) && intval($count[0]['c']) > 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the given Character has solved the given Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest to check
|
||||
* @param int $characterId ID of Character to check
|
||||
* @result boolean Whether Character has solved the Quest or not
|
||||
*/
|
||||
public function hasCharacterSolvedQuest($questId, $characterId)
|
||||
{
|
||||
$count = $this->db->query(
|
||||
'SELECT count(id) AS c '.
|
||||
'FROM quests_characters '.
|
||||
'WHERE quest_id = ? AND character_id = ? AND status = ?',
|
||||
'iii',
|
||||
$questId,
|
||||
$characterId,
|
||||
self::QUEST_STATUS_SOLVED
|
||||
);
|
||||
|
||||
|
||||
return (!empty($count) && intval($count[0]['c']) > 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the last Quests for a Character.
|
||||
*
|
||||
* @param int $characterId ID of Character
|
||||
* @retrun array Quest data
|
||||
*/
|
||||
public function getLastQuestForCharacter($characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT quests.id, quests.questgroup_id, quests.questtype_id, quests.title, quests.url, quests.xps, quests.task, quests.wrong_text, quests.questsmedia_id '.
|
||||
'FROM quests_characters '.
|
||||
'LEFT JOIN quests ON quests.id = quests_characters.quest_id '.
|
||||
'WHERE quests_characters.character_id = ? AND quests_characters.status IN (?, ?, ?) '.
|
||||
'ORDER BY quests_characters.created desc '.
|
||||
'LIMIT 1',
|
||||
'iiii',
|
||||
$characterId,
|
||||
self::QUEST_STATUS_ENTERED, self::QUEST_STATUS_SUBMITTED, self::QUEST_STATUS_SOLVED
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Quests for a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @return array Quests for this Seminary
|
||||
*/
|
||||
public function getQuestsForSeminary($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT DISTINCT quests.id, quests.questgroup_id, quests.questtype_id, quests.title, quests.url, quests.xps, quests.task, quests.wrong_text, quests.questsmedia_id '.
|
||||
'FROM questgroups '.
|
||||
'INNER JOIN quests ON quests.questgroup_id = questgroups.id '.
|
||||
'WHERE questgroups.seminary_id = ?',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Quests that are linked to a Questtopic.
|
||||
*
|
||||
* @param int $questtopicId ID of Questtopic
|
||||
* @return array Quests for this Questtopic
|
||||
*/
|
||||
public function getQuestsForQuesttopic($questtopicId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT DISTINCT quests.id, quests.questgroup_id, quests.questtype_id, quests.title, quests.url, quests.xps, quests.task, quests.wrong_text, quests.questsmedia_id '.
|
||||
'FROM quests_questsubtopics '.
|
||||
'INNER JOIN questsubtopics ON questsubtopics.id = quests_questsubtopics.questsubtopic_id '.
|
||||
'INNER JOIN quests ON quests.id = quests_questsubtopics.quest_id '.
|
||||
'WHERE questsubtopics.questtopic_id = ?',
|
||||
'i',
|
||||
$questtopicId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Mark a Quest for a Character.
|
||||
*
|
||||
* @param int $questId ID of Quest to mark
|
||||
* @param int $characterId ID of Character to mark the Quest for
|
||||
* @param int $status Quest status to mark
|
||||
* @param boolean $repeated Insert although status is already set for this Quest and Character
|
||||
*/
|
||||
private function setQuestStatus($questId, $characterId, $status, $repeated=true)
|
||||
{
|
||||
// Check if status is already set
|
||||
if(!$repeated)
|
||||
{
|
||||
$count = $this->db->query(
|
||||
'SELECT count(*) AS c '.
|
||||
'FROM quests_characters '.
|
||||
'WHERE quest_id = ? AND character_id = ? AND status = ?',
|
||||
'iii',
|
||||
$questId,
|
||||
$characterId,
|
||||
$status
|
||||
);
|
||||
if(!empty($count) && intval($count[0]['c']) > 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Set status
|
||||
$this->db->query(
|
||||
'INSERT INTO quests_characters '.
|
||||
'(quest_id, character_id, status) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) ',
|
||||
'iii',
|
||||
$questId,
|
||||
$characterId,
|
||||
$status
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the last status of a Quest for a Character.
|
||||
*
|
||||
* @param int $questId ID of Quest
|
||||
* @param int $characterId ID of Character to get status for
|
||||
* @return int Last status
|
||||
*/
|
||||
public function getLastQuestStatus($questId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, created, status '.
|
||||
'FROM quests_characters '.
|
||||
'WHERE quest_id = ? AND character_id = ? '.
|
||||
'ORDER BY created DESC '.
|
||||
'LIMIT 1',
|
||||
'ii',
|
||||
$questId, $characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Quest.
|
||||
*
|
||||
* @param int $userId User-ID that creates the new character
|
||||
* @param string $name Name for new Quest
|
||||
* @param int $questgroupId ID of Questgroup
|
||||
* @param int $questtypeId ID of Questtype
|
||||
* @param int $xps XPs for new Quest
|
||||
* @param string $entrytext Entrytext for new Quest
|
||||
* @param string $wrongtext Wrongtext for new Quest
|
||||
* @param string $task Task for new Quest
|
||||
* @return int ID of new Quest
|
||||
*/
|
||||
public function createQuest($userId, $name, $questgroupId, $questtypeId, $xps, $entrytext, $wrongtext, $task)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO quests '.
|
||||
'(created_user_id, questgroup_id, questtype_id, title, url, xps, entry_text, wrong_text, task) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
'iiississs',
|
||||
$userId, $questgroupId, $questtypeId,
|
||||
$name, \nre\core\Linker::createLinkParam($name),
|
||||
$xps, $entrytext, $wrongtext, $task
|
||||
);
|
||||
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
238
models/QuesttextsModel.inc
Normal file
238
models/QuesttextsModel.inc
Normal file
|
|
@ -0,0 +1,238 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Questtexts-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class QuesttextsModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new QuesttextsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the first text of a Quest.
|
||||
*
|
||||
* @param int $questId ID of a Quest
|
||||
* @return string First text of this Quest or NULL
|
||||
*/
|
||||
public function getFirstQuestText($questId)
|
||||
{
|
||||
$prolog = $this->getQuesttextsOfQuest($questId, 'Prolog');
|
||||
if(!empty($prolog)) {
|
||||
return $prolog[0]['text'];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Questtexts for a Quest.
|
||||
*
|
||||
* @param int $questId ID of the Quest
|
||||
* @param string $questtexttypeUrl URL of the Questtexttype
|
||||
* @return array All Questtexts for a Quest
|
||||
*/
|
||||
public function getQuesttextsOfQuest($questId, $questtexttypeUrl=null)
|
||||
{
|
||||
if(is_null($questtexttypeUrl))
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT questtexts.id, questtexts.text, questtexts.pos, questtexts.out_text, questtexts.abort_text, questtexts.questsmedia_id, questtexttypes.id AS type_id, questtexttypes.type, questtexttypes.url AS type_url '.
|
||||
'FROM questtexts '.
|
||||
'LEFT JOIN questtexttypes ON questtexttypes.id = questtexts.questtexttype_id '.
|
||||
'WHERE questtexts.quest_id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT questtexts.id, questtexts.text, questtexts.pos, questtexts.out_text, questtexts.abort_text, questtexts.questsmedia_id, questtexttypes.id AS type_id, questtexttypes.type, questtexttypes.url AS type_url '.
|
||||
'FROM questtexts '.
|
||||
'LEFT JOIN questtexttypes ON questtexttypes.id = questtexts.questtexttype_id '.
|
||||
'WHERE questtexts.quest_id = ? and questtexttypes.url = ?',
|
||||
'is',
|
||||
$questId,
|
||||
$questtexttypeUrl
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get count of Questtexts for a Quest.
|
||||
*
|
||||
* @param int $questId ID of the Quest
|
||||
* @param string $questtexttypeUrl URL of the Questtexttype
|
||||
* @return int Amount of Questtexts for a Quest
|
||||
*/
|
||||
public function getQuesttextCountOfQuest($questId, $questtexttypeUrl=null)
|
||||
{
|
||||
if(is_null($questtexttypeUrl))
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT count(questtexts.id) AS c '.
|
||||
'FROM questtexts '.
|
||||
'LEFT JOIN questtexttypes ON questtexttypes.id = questtexts.questtexttype_id '.
|
||||
'WHERE questtexts.quest_id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT count(questtexts.id) AS c '.
|
||||
'FROM questtexts '.
|
||||
'LEFT JOIN questtexttypes ON questtexttypes.id = questtexts.questtexttype_id '.
|
||||
'WHERE questtexts.quest_id = ? and questtexttypes.url = ?',
|
||||
'is',
|
||||
$questId,
|
||||
$questtexttypeUrl
|
||||
);
|
||||
}
|
||||
if(!empty($data)) {
|
||||
return $data[0]['c'];
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get corresponding Questtext for a Sidequest.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $sidequestId ID of the Sidequest to get the Questtext for
|
||||
* @param array Questtext data
|
||||
*/
|
||||
public function getRelatedQuesttextForQuestgroup($questgroupId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT questtexts.id, questtexts.text, questtexts.pos, questtexts.quest_id, questtexttypes.id AS type_id, questtexttypes.type, questtexttypes.url AS type_url '.
|
||||
'FROM questgroups_questtexts '.
|
||||
'LEFT JOIN questtexts ON questtexts.id = questgroups_questtexts.questtext_id '.
|
||||
'LEFT JOIN questtexttypes ON questtexttypes.id = questtexts.questtexttype_id '.
|
||||
'WHERE questgroups_questtexts.questgroup_id = ?',
|
||||
'i',
|
||||
$questgroupId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all registered Questtexttypes.
|
||||
*
|
||||
* @return array Registered Questtexttypes
|
||||
*/
|
||||
public function getQuesttexttypes()
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, type, url '.
|
||||
'FROM questtexttypes'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
154
models/QuesttopicsModel.inc
Normal file
154
models/QuesttopicsModel.inc
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Questtopics-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class QuesttopicsModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new QuesttopicsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get a Questtopic by its URL.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param string $questtopicUrl URL-Title of Questtopic
|
||||
* @return array Questtopic data
|
||||
*/
|
||||
public function getQuesttopicByUrl($seminaryId, $questtopicUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, title, url '.
|
||||
'FROM questtopics '.
|
||||
'WHERE seminary_id = ? AND url = ?',
|
||||
'is',
|
||||
$seminaryId, $questtopicUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questtopicUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Questtopics for a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @return array List of Questtopics
|
||||
*/
|
||||
public function getQuesttopicsForSeminary($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, title, url '.
|
||||
'FROM questtopics '.
|
||||
'WHERE seminary_id = ?',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get count of Quests that are linked to a Questtopic.
|
||||
*
|
||||
* @param int $questtopicId ID of Questtopic
|
||||
* @return int Count of Quests
|
||||
*/
|
||||
public function getQuestCountForQuesttopic($questtopicId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT count(DISTINCT quests_questsubtopics.quest_id) AS c ' .
|
||||
'FROM questsubtopics '.
|
||||
'LEFT JOIN quests_questsubtopics ON quests_questsubtopics.questsubtopic_id = questsubtopics.id '.
|
||||
'WHERE questsubtopics.questtopic_id = ?',
|
||||
'i',
|
||||
$questtopicId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['c'];
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get count of Quests that are linked to a Questtopic and are
|
||||
* unlocked by a Character.
|
||||
*
|
||||
* @param int $questtopicId ID of Questtopic
|
||||
* @param int $characterId ID of Character
|
||||
* @return int Count of Quests
|
||||
*/
|
||||
public function getCharacterQuestCountForQuesttopic($questtopicId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT count(DISTINCT quests_characters.quest_id) AS c '.
|
||||
'FROM questsubtopics '.
|
||||
'LEFT JOIN quests_questsubtopics ON quests_questsubtopics.questsubtopic_id = questsubtopics.id '.
|
||||
'INNER JOIN quests_characters ON quests_characters.quest_id = quests_questsubtopics.quest_id AND quests_characters.character_id = ? AND quests_characters.status = 3 '.
|
||||
'WHERE questsubtopics.questtopic_id = ?',
|
||||
'ii',
|
||||
$characterId,
|
||||
$questtopicId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0]['c'];
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all Questsubtopics for a Quest.
|
||||
*
|
||||
* @param int $questId ID of Quest
|
||||
* @return array List of Questsubtopics
|
||||
*/
|
||||
public function getQuestsubtopicsForQuest($questId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT DISTINCT id, questtopic_id, title, url '.
|
||||
'FROM quests_questsubtopics '.
|
||||
'INNER JOIN questsubtopics ON questsubtopics.id = quests_questsubtopics.questsubtopic_id '.
|
||||
'WHERE quests_questsubtopics.quest_id = ?',
|
||||
'i',
|
||||
$questId
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
77
models/QuesttypesModel.inc
Normal file
77
models/QuesttypesModel.inc
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with Questtypes-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class QuesttypesModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new QuesttypesModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all registered Questtypes.
|
||||
*
|
||||
* @return array List of registered Questtypes
|
||||
*/
|
||||
public function getQuesttypes()
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, title, url, classname '.
|
||||
'FROM questtypes '.
|
||||
'ORDER BY title ASC'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Questtype by its ID
|
||||
*
|
||||
* @param int $questtypeId ID of Questtype
|
||||
* @return array Questtype data
|
||||
*/
|
||||
public function getQuesttypeById($questtypeId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT title, classname '.
|
||||
'FROM questtypes '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$questtypeId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questtypeId);
|
||||
}
|
||||
|
||||
|
||||
return $data = $data[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
195
models/SeminariesModel.inc
Normal file
195
models/SeminariesModel.inc
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model of the SeminariesAgent to list registered seminaries.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class SeminariesModel extends \hhu\z\Model
|
||||
{
|
||||
/**
|
||||
* Required models
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $models = array('questgroupshierarchy', 'questgroups');
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new SeminariesModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get registered seminaries.
|
||||
*
|
||||
* @return array Seminaries
|
||||
*/
|
||||
public function getSeminaries()
|
||||
{
|
||||
// Get seminaries
|
||||
return $this->db->query(
|
||||
'SELECT id, created, created_user_id, title, url, course, description, seminarymedia_id, charactergroups_seminarymedia_id, achievements_seminarymedia_id, library_seminarymedia_id '.
|
||||
'FROM seminaries '.
|
||||
'ORDER BY created DESC'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a seminary and its data by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param string $seminaryId ID of a seminary
|
||||
* @return array Seminary
|
||||
*/
|
||||
public function getSeminaryById($seminaryId)
|
||||
{
|
||||
$seminary = $this->db->query(
|
||||
'SELECT id, created, created_user_id, title, url, course, description, seminarymedia_id, charactergroups_seminarymedia_id, achievements_seminarymedia_id, library_seminarymedia_id '.
|
||||
'FROM seminaries '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
if(empty($seminary)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($seminaryId);
|
||||
}
|
||||
|
||||
|
||||
return $seminary[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a seminary and its data by its URL-title.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param string $seminaryUrl URL-Title of a seminary
|
||||
* @return array Seminary
|
||||
*/
|
||||
public function getSeminaryByUrl($seminaryUrl)
|
||||
{
|
||||
$seminary = $this->db->query(
|
||||
'SELECT id, created, created_user_id, title, url, course, description, seminarymedia_id, charactergroups_seminarymedia_id, achievements_seminarymedia_id, library_seminarymedia_id '.
|
||||
'FROM seminaries '.
|
||||
'WHERE url = ?',
|
||||
's',
|
||||
$seminaryUrl
|
||||
);
|
||||
if(empty($seminary)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($seminaryUrl);
|
||||
}
|
||||
|
||||
|
||||
return $seminary[0];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Calculate sum of XPs for a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @return int Total sum of XPs
|
||||
*/
|
||||
public function getTotalXPs($seminaryId)
|
||||
{
|
||||
$xps = 0;
|
||||
|
||||
// Questgroups
|
||||
$questgroupshierarchy = $this->Questgroupshierarchy->getHierarchyOfSeminary($seminaryId);
|
||||
foreach($questgroupshierarchy as &$hierarchy)
|
||||
{
|
||||
// Get Questgroups
|
||||
$questgroups = $this->Questgroups->getQuestgroupsForHierarchy($hierarchy['id']);
|
||||
foreach($questgroups as &$questgroup)
|
||||
{
|
||||
$data = $this->Questgroups->getCumulatedDataForQuestgroup($questgroup['id']);
|
||||
$xps += $data['xps'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $xps;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new seminary.
|
||||
*
|
||||
* @param string $title Title of seminary to create
|
||||
* @param int $userId ID of creating user
|
||||
* @return int ID of the newly created seminary
|
||||
*/
|
||||
public function createSeminary($title, $userId)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO seminaries '.
|
||||
'(created_user_id, title, url) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?)',
|
||||
'iss',
|
||||
$userId,
|
||||
$title,
|
||||
\nre\core\Linker::createLinkParam($title)
|
||||
);
|
||||
|
||||
|
||||
return $this->db->getInsertId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit a seminary.
|
||||
*
|
||||
* @throws DatamodelException
|
||||
* @param int $seminaryId ID of the seminary to delete
|
||||
* @param string $title New title of seminary
|
||||
*/
|
||||
public function editSeminary($seminaryId, $title)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE seminaries '.
|
||||
'SET title = ?, url = ? '.
|
||||
'WHERE id = ?',
|
||||
'ssi',
|
||||
$title,
|
||||
\nre\core\Linker::createLinkParam($title),
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of the seminary to delete
|
||||
*/
|
||||
public function deleteSeminary($seminaryId)
|
||||
{
|
||||
$this->db->query('DELETE FROM seminaries WHERE id = ?', 'i', $seminaryId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
128
models/SeminarycharacterfieldsModel.inc
Normal file
128
models/SeminarycharacterfieldsModel.inc
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with the Seminarycharacterfields-tables.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class SeminarycharacterfieldsModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new SeminarycharacterfieldsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all Character fields of a Seminary.
|
||||
*
|
||||
* @param int $seminaryId ID of Seminary to get fields of
|
||||
* @param array Seminary Character fields
|
||||
*/
|
||||
public function getFieldsForSeminary($seminaryId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT seminarycharacterfields.id, seminarycharacterfields.title, seminarycharacterfields.url, seminarycharacterfields.regex, seminarycharacterfields.required, seminarycharacterfieldtypes.id AS type_id, seminarycharacterfieldtypes.title AS type_title, seminarycharacterfieldtypes.url AS type_url '.
|
||||
'FROM seminarycharacterfields '.
|
||||
'LEFT JOIN seminarycharacterfieldtypes ON seminarycharacterfieldtypes.id = seminarycharacterfields.seminarycharacterfieldtype_id '.
|
||||
'WHERE seminarycharacterfields.seminary_id = ? '.
|
||||
'ORDER BY pos ASC',
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the value of a Seminary field for a Character.
|
||||
*
|
||||
* @param int $characterId ID of Character
|
||||
* @param int $seminarycharacterfieldId ID of seminarycharacterfield to set value of
|
||||
* @param string $value Value to set
|
||||
*/
|
||||
public function setSeminaryFieldOfCharacter($seminarycharacterfieldId, $characterId, $value)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT INTO characters_seminarycharacterfields '.
|
||||
'(character_id, seminarycharacterfield_id, value) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?) '.
|
||||
'ON DUPLICATE KEY UPDATE '.
|
||||
'value = ?',
|
||||
'iiss',
|
||||
$characterId,
|
||||
$seminarycharacterfieldId,
|
||||
$value,
|
||||
$value
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Seminary Character fields of a Character.
|
||||
*
|
||||
* @param int $characterId ID of the Character
|
||||
* @return array Seminary Character fields
|
||||
*/
|
||||
public function getSeminaryFieldOfCharacter($fieldId, $characterId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT created, value '.
|
||||
'FROM characters_seminarycharacterfields '.
|
||||
'WHERE seminarycharacterfield_id = ? AND character_id = ?',
|
||||
'ii',
|
||||
$fieldId,
|
||||
$characterId
|
||||
);
|
||||
if(!empty($data)) {
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get Seminary Character fields of a Character.
|
||||
*
|
||||
* @param int $characterId ID of the Character
|
||||
* @return array Seminary Character fields
|
||||
*/
|
||||
public function getFieldsForCharacter($characterId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT seminarycharacterfields.id, seminarycharacterfields.title, seminarycharacterfields.url, seminarycharacterfields.regex, seminarycharacterfields.required, seminarycharacterfieldtypes.id AS type_id, seminarycharacterfieldtypes.title AS type_title, seminarycharacterfieldtypes.url AS type_url, characters_seminarycharacterfields.value '.
|
||||
'FROM characters_seminarycharacterfields '.
|
||||
'LEFT JOIN seminarycharacterfields ON seminarycharacterfields.id = characters_seminarycharacterfields.seminarycharacterfield_id '.
|
||||
'LEFT JOIN seminarycharacterfieldtypes ON seminarycharacterfieldtypes.id = seminarycharacterfields.seminarycharacterfieldtype_id '.
|
||||
'WHERE characters_seminarycharacterfields.character_id = ?',
|
||||
'i',
|
||||
$characterId
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
141
models/UploadsModel.inc
Normal file
141
models/UploadsModel.inc
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to handle files to upload.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class UploadsModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new UploadsModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Upload a file and create a database record.
|
||||
*
|
||||
* @param int $userId ID of user that uploads the file
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param string $name Name of file to upload
|
||||
* @param string $filename Filename of file to upload
|
||||
* @param string $tmpFilename Name of temporary uploaded file
|
||||
* @param string $mimetype Mimetype of file to upload
|
||||
* @return mixed ID of database record or false
|
||||
*/
|
||||
public function uploadSeminaryFile($userId, $seminaryId, $name, $filename, $tmpFilename, $mimetype)
|
||||
{
|
||||
$uploadId = false;
|
||||
$this->db->setAutocommit(false);
|
||||
|
||||
try {
|
||||
// Create database record
|
||||
$this->db->query(
|
||||
'INSERT INTO seminaryuploads '.
|
||||
'(created_user_id, seminary_id, name, url, mimetype) '.
|
||||
'VALUES '.
|
||||
'(?, ? ,? ,?, ?)',
|
||||
'iisss',
|
||||
$userId,
|
||||
$seminaryId,
|
||||
$name,
|
||||
\nre\core\Linker::createLinkParam($filename),
|
||||
$mimetype
|
||||
);
|
||||
$uploadId = $this->db->getInsertId();
|
||||
|
||||
// Create filename
|
||||
$filename = ROOT.DS.\nre\configs\AppConfig::$dirs['seminaryuploads'].DS.$filename;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an upload by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $uploadId ID of the uploaded file
|
||||
* @return array Upload data
|
||||
*/
|
||||
public function getSeminaryuploadById($seminaryuploadId)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, created, created_user_id, seminary_id, name, url, mimetype, public '.
|
||||
'FROM seminaryuploads '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$seminaryuploadId
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($seminaryuploadId);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an upload by its URL.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $uploadId ID of the uploaded file
|
||||
* @return array Upload data
|
||||
*/
|
||||
public function getSeminaryuploadByUrl($seminaryId, $seminaryuploadUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, created, created_user_id, seminary_id, name, url, mimetype, public '.
|
||||
'FROM seminaryuploads '.
|
||||
'WHERE seminary_id = ? AND url = ?',
|
||||
'is',
|
||||
$seminaryId,
|
||||
$seminaryuploadUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($seminaryuploadUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
120
models/UserrolesModel.inc
Normal file
120
models/UserrolesModel.inc
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model to interact with userroles-table.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class UserrolesModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new UserrolesModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get all userroles for an user referenced by its ID.
|
||||
*
|
||||
* @param int $userId ID of an user
|
||||
* @return array Userroles for an user
|
||||
*/
|
||||
public function getUserrolesForUserById($userId)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT userroles.id, userroles.created, userroles.name '.
|
||||
'FROM users_userroles '.
|
||||
'LEFT JOIN userroles ON userroles.id = users_userroles.userrole_id '.
|
||||
'WHERE users_userroles.user_id = ?',
|
||||
'i',
|
||||
$userId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all userroles for an user referenced by its URL-username.
|
||||
*
|
||||
* @param string $userUrl URL-Username of an user
|
||||
* @return array Userroles for an user
|
||||
*/
|
||||
public function getUserrolesForUserByUrl($userUrl)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT userroles.id, userroles.created, userroles.name '.
|
||||
'FROM users '.
|
||||
'LEFT JOIN users_userroles ON users_userroles.user_id = users.id '.
|
||||
'LEFT JOIN userroles ON userroles.id = users_userroles.userrole_id '.
|
||||
'WHERE users.url = ?',
|
||||
's',
|
||||
$userUrl
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add a role to a user.
|
||||
*
|
||||
* @param int $userId ID of user to add role to
|
||||
* @param string $userrole Role to add
|
||||
*/
|
||||
public function addUserroleToUser($userId, $userrole)
|
||||
{
|
||||
$this->db->query(
|
||||
'INSERT IGNORE INTO users_userroles '.
|
||||
'(user_id, userrole_id) '.
|
||||
'SELECT ?, id '.
|
||||
'FROM userroles '.
|
||||
'WHERE name = ?',
|
||||
'is',
|
||||
$userId,
|
||||
$userrole
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove a role from a user.
|
||||
*
|
||||
* @param int $userId ID of user to remove role from
|
||||
* @param string $userrole Role to remove
|
||||
*/
|
||||
public function removeUserroleFromUser($userId, $userrole)
|
||||
{
|
||||
$this->db->query(
|
||||
'DELETE FROM users_userroles '.
|
||||
'WHERE user_id = ? AND userrole_id = ('.
|
||||
'SELECT id '.
|
||||
'FROM userroles '.
|
||||
'WHERE name = ?'.
|
||||
')',
|
||||
'is',
|
||||
$userId,
|
||||
$userrole
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
344
models/UsersModel.inc
Normal file
344
models/UsersModel.inc
Normal file
|
|
@ -0,0 +1,344 @@
|
|||
<?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\models;
|
||||
|
||||
|
||||
/**
|
||||
* Model of the UsersAgent to list users and get their data.
|
||||
*
|
||||
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
|
||||
*/
|
||||
class UsersModel extends \hhu\z\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Construct a new UsersModel.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get registered users.
|
||||
*
|
||||
* @return array Users
|
||||
*/
|
||||
public function getUsers()
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT id, created, username, url, surname, prename, email '.
|
||||
'FROM users '.
|
||||
'ORDER BY username ASC'
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get users with the given user role.
|
||||
*
|
||||
* @param string $userrole User role
|
||||
* @return array List of users
|
||||
*/
|
||||
public function getUsersWithRole($userrole)
|
||||
{
|
||||
return $this->db->query(
|
||||
'SELECT users.id, users.created, users.username, users.url, users.surname, users.prename, users.email '.
|
||||
'FROM users '.
|
||||
'LEFT JOIN users_userroles ON users_userroles.user_id = users.id '.
|
||||
'LEFT JOIN userroles ON userroles.id = users_userroles.userrole_id '.
|
||||
'WHERE userroles.name = ? '.
|
||||
'ORDER BY username ASC',
|
||||
's',
|
||||
$userrole
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a user and its data by its ID.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param int $userId ID of an user
|
||||
* @return array Userdata
|
||||
*/
|
||||
public function getUserById($userId)
|
||||
{
|
||||
// Get user
|
||||
$user = $this->db->query(
|
||||
'SELECT id, created, username, url, surname, prename, email '.
|
||||
'FROM users '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$userId
|
||||
);
|
||||
if(empty($user)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($userId);
|
||||
}
|
||||
|
||||
|
||||
return $user[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a user and its data by its URL-username.
|
||||
*
|
||||
* @throws IdNotFoundException
|
||||
* @param string $userUrl URL-Username of an user
|
||||
* @return array Userdata
|
||||
*/
|
||||
public function getUserByUrl($userUrl)
|
||||
{
|
||||
// Get user
|
||||
$user = $this->db->query(
|
||||
'SELECT id, created, username, url, surname, prename, email '.
|
||||
'FROM users '.
|
||||
'WHERE url = ?',
|
||||
's',
|
||||
$userUrl
|
||||
);
|
||||
if(empty($user)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($userUrl);
|
||||
}
|
||||
|
||||
|
||||
return $user[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Log a user in if its credentials are valid.
|
||||
*
|
||||
* @throws DatamodelException
|
||||
* @param string $username The name of the user to log in
|
||||
* @param string $password Plaintext password of the user to log in
|
||||
*/
|
||||
public function login($username, $password)
|
||||
{
|
||||
$data = $this->db->query('SELECT id, password FROM users WHERE username = ?', 's', $username);
|
||||
if(!empty($data))
|
||||
{
|
||||
$data = $data[0];
|
||||
if($this->verify($password, $data['password'])) {
|
||||
return $data['id'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if an username already exists.
|
||||
*
|
||||
* @param string $username Username to check
|
||||
* @param int $userId Do not check this ID (for editing)
|
||||
* @return boolean Whether username exists or not
|
||||
*/
|
||||
public function usernameExists($username, $userId=null)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id '.
|
||||
'FROM users '.
|
||||
'WHERE username = ? OR url = ?',
|
||||
'ss',
|
||||
$username,
|
||||
\nre\core\Linker::createLinkParam($username)
|
||||
);
|
||||
|
||||
|
||||
return (!empty($data) && (is_null($userId) || $userId != $data[0]['id']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if an e‑mail address already exists.
|
||||
*
|
||||
* @param string $email E‑mail address to check
|
||||
* @param int $userId Do not check this ID (for editing)
|
||||
* @return boolean Whether e‑mail address exists or not
|
||||
*/
|
||||
public function emailExists($email, $userId=null)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id '.
|
||||
'FROM users '.
|
||||
'WHERE email = ?',
|
||||
's',
|
||||
$email
|
||||
);
|
||||
|
||||
|
||||
return (!empty($data) && (is_null($userId) || $userId != $data[0]['id']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new user.
|
||||
*
|
||||
* @param string $username Username of the user to create
|
||||
* @param string $email E‑Mail-Address of the user to create
|
||||
* @param string $password Password of the user to create
|
||||
* @return int ID of the newly created user
|
||||
*/
|
||||
public function createUser($username, $prename, $surname, $email, $password)
|
||||
{
|
||||
$userId = null;
|
||||
$this->db->setAutocommit(false);
|
||||
try {
|
||||
// Create user
|
||||
$this->db->query(
|
||||
'INSERT INTO users '.
|
||||
'(username, url, surname, prename, email, password) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?, ?, ?, ?)',
|
||||
'ssssss',
|
||||
$username,
|
||||
\nre\core\Linker::createLinkParam($username),
|
||||
$surname,
|
||||
$prename,
|
||||
$email,
|
||||
$this->hash($password)
|
||||
);
|
||||
$userId = $this->db->getInsertId();
|
||||
|
||||
// Add role “user”
|
||||
$this->db->query(
|
||||
'INSERT INTO users_userroles '.
|
||||
'(user_id, userrole_id) '.
|
||||
'SELECT ?, userroles.id '.
|
||||
'FROM userroles '.
|
||||
'WHERE userroles.name = ?',
|
||||
'is',
|
||||
$userId,
|
||||
'user'
|
||||
);
|
||||
}
|
||||
catch(Exception $e) {
|
||||
$this->db->rollback();
|
||||
$this->db->setAutocommit(true);
|
||||
throw $e;
|
||||
}
|
||||
$this->db->setAutocommit(true);
|
||||
|
||||
|
||||
return $userId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit a user.
|
||||
*
|
||||
* @throws DatamodelException
|
||||
* @param int $userId ID of the user to delete
|
||||
* @param string $username New name of user
|
||||
* @param string $email Changed e‑mail-address of user
|
||||
* @param string $password Changed plaintext password of user
|
||||
*/
|
||||
public function editUser($userId, $username, $prename, $surname, $email, $password)
|
||||
{
|
||||
$this->db->setAutocommit(false);
|
||||
try {
|
||||
// Update user data
|
||||
$this->db->query(
|
||||
'UPDATE users '.
|
||||
'SET username = ?, url = ?, prename = ?, surname = ?, email = ? '.
|
||||
'WHERE id = ?',
|
||||
'sssssi',
|
||||
$username,
|
||||
\nre\core\Linker::createLinkParam($username),
|
||||
$prename,
|
||||
$surname,
|
||||
$email,
|
||||
$userId
|
||||
);
|
||||
|
||||
// Set new password
|
||||
if(!empty($password))
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE users '.
|
||||
'SET password = ? '.
|
||||
'WHERE id = ?',
|
||||
'si',
|
||||
$this->hash($password),
|
||||
$userId
|
||||
);
|
||||
}
|
||||
}
|
||||
catch(Exception $e) {
|
||||
$this->db->rollback();
|
||||
$this->db->setAutocommit(true);
|
||||
throw $e;
|
||||
}
|
||||
$this->db->setAutocommit(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a user.
|
||||
*
|
||||
* @param int $userId ID of the user to delete
|
||||
*/
|
||||
public function deleteUser($userId)
|
||||
{
|
||||
$this->db->query('DELETE FROM users WHERE id = ?', 'i', $userId);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Hash a password.
|
||||
*
|
||||
* @param string $password Plaintext password
|
||||
* @return string Hashed password
|
||||
*/
|
||||
public function hash($password)
|
||||
{
|
||||
if(!function_exists('password_hash')) {
|
||||
\hhu\z\lib\Password::load();
|
||||
}
|
||||
|
||||
|
||||
return password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Verify a password.
|
||||
*
|
||||
* @param string $password Plaintext password to verify
|
||||
* @param string $hash Hashed password to match with
|
||||
* @return boolean Verified
|
||||
*/
|
||||
private function verify($password, $hash)
|
||||
{
|
||||
if(!function_exists('password_verify')) {
|
||||
\hhu\z\lib\Password::load();
|
||||
}
|
||||
|
||||
|
||||
return password_verify($password, $hash);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue