* @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 */ class CharactergroupsquestsModel extends \hhu\z\Model { /** * 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 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 = ?', 'i', $questId ); foreach($groups as &$group) { $group['xps'] = round($group['xps'] * $group['xps_factor'], 1); } return $groups; } /** * 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; } /** * 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'])); } /** * 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); } } ?>