questlab/models/CharactergroupsqueststationsModel.inc
2016-01-15 23:01:30 +01:00

415 lines
14 KiB
PHP

<?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 CharactergroupsqueststationsAgent to interact with
* Charactergroupsqueststations-table.
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
*/
class CharactergroupsqueststationsModel extends \hhu\z\Model
{
/**
* Status: Entered
*
* @var int;
*/
const STATUS_ENTERED = 0;
/**
* Status: Unsolved
*
* @var int;
*/
const STATUS_UNSOLVED = 2;
/**
* Status: Solved
*
* @var int;
*/
const STATUS_SOLVED = 3;
/**
* Construct a new CharactergroupsqueststationsModel.
*/
public function __construct()
{
parent::__construct();
}
/**
* Get a Station by its ID.
*
* @throws \nre\exceptions\IdNotFoundException
* @param int $stationId ID of Station to get
* @return array Station data
*/
public function getStationById($stationId)
{
$data = $this->db->query(
'SELECT id, charactergroupsquest_id, stationtype_id, title, url, stationpicture_id, prolog, task, latitude, longitude, righttext, wrongtext '.
'FROM charactergroupsqueststations '.
'WHERE id = ?',
'i',
$stationId
);
if(empty($data)) {
throw new \nre\exceptions\IdNotFoundException($stationId);
}
return $data[0];
}
/**
* Get a Station by its ID.
*
* @throws \nre\exceptions\IdNotFoundException
* @param int $stationId ID of Station to get
* @return array Station data
*/
public function getStationByUrl($groupsquestId, $stationUrl)
{
$data = $this->db->query(
'SELECT id, charactergroupsquest_id, stationtype_id, title, url, stationpicture_id, prolog, task, latitude, longitude, righttext, wrongtext '.
'FROM charactergroupsqueststations '.
'WHERE charactergroupsquest_id = ? AND url = ?',
'is',
$groupsquestId,
$stationUrl
);
if(empty($data)) {
throw new \nre\exceptions\IdNotFoundException($stationUrl);
}
return $data[0];
}
/**
* Get all Stations for a Character groups Quest.
*
* @param int $questId ID of the Character groups Quest
* @return array List of Stations
*/
public function getStationsForQuest($questId)
{
return $this->db->query(
'SELECT id, title, url, stationpicture_id, latitude, longitude '.
'FROM charactergroupsqueststations '.
'WHERE charactergroupsquest_id = ? '.
'ORDER BY pos ASC',
'i',
$questId
);
}
/**
* Get all Stations for a Character groups Quest and a Character group.
*
* @param int $questId ID of Character groups Quest
* @param int $groupId ID of Character group
* @return array List of Station
*/
public function getStationsForQuestAndGroup($questId, $groupId)
{
return $this->db->query(
'SELECT charactergroupsqueststations.id, charactergroupsqueststations_charactergroups.created, title, url, stationpicture_id, latitude, longitude '.
'FROM charactergroupsqueststations_charactergroups '.
'INNER JOIN charactergroupsqueststations ON charactergroupsqueststations.id = charactergroupsqueststations_charactergroups.charactergroupsqueststation_id '.
'WHERE '.
'charactergroupsqueststations_charactergroups.charactergroup_id = ? AND '.
'charactergroupsqueststations_charactergroups.status = ? AND '.
'charactergroupsquest_id = ? '.
'ORDER BY charactergroupsqueststations_charactergroups.created ASC',
'iii',
$groupId,
self::STATUS_ENTERED,
$questId
);
}
/**
* Mark a Station as entered for a Character group.
*
* @param int $stationId ID of Station to mark
* @param int $groupId ID of Character group
*/
public function setStationEntered($stationId, $groupId)
{
$this->setStatus($stationId, $groupId, self::STATUS_ENTERED);
}
/**
* Mark a Station as solved for a Character group.
*
* @param int $stationId ID of Station to mark
* @param int $groupId ID of Character group
*/
public function setStationSolved($stationId, $groupId)
{
$this->setStatus($stationId, $groupId, self::STATUS_SOLVED);
}
/**
* Mark a Station as unsolved for a Character group.
*
* @param int $stationId ID of Station to mark
* @param int $groupId ID of Character group
*/
public function setStationUnsolved($stationId, $groupId)
{
$this->setStatus($stationId, $groupId, self::STATUS_UNSOLVED);
}
/**
* Check if a Character group has tried to solve a Station.
*
* @param int $stationId ID of Station to check
* @param int $groupId ID of Character group to check
* @return bool Whether the group has tried the station or not
*/
public function hasCharactergroupTriedStation($stationId, $groupId)
{
$data = $this->db->query(
'SELECT created '.
'FROM charactergroupsqueststations_charactergroups '.
'WHERE charactergroupsqueststation_id = ? AND charactergroup_id = ? AND (status = ? OR status = ?)',
'iiii',
$stationId,
$groupId,
self::STATUS_UNSOLVED,
self::STATUS_SOLVED
);
if(!empty($data)) {
return $data[0]['created'];
}
return false;
}
/**
* Check if a Character group has solved a Station.
*
* @param int $questId ID of Quest to check
* @param int $groupId ID of Character to check
* @result boolean Whether Character has solved the Quest or not
*/
public function hasCharactergroupSolvedStation($stationId, $groupId)
{
$data = $this->db->query(
'SELECT created '.
'FROM charactergroupsqueststations_charactergroups '.
'WHERE charactergroupsqueststation_id = ? AND charactergroup_id = ? AND status = ?',
'iii',
$stationId,
$groupId,
self::STATUS_SOLVED
);
if(!empty($data)) {
return $data[0]['created'];
}
return false;
}
/**
* Check if a Character groups Quest Station title already exists.
*
* @param int $questId ID of Character groups Quest
* @param string $title Station title to check
* @param int $stationId Do not check this ID (for editing)
* @return boolean Whether Station title exists or not
*/
public function stationTitleExists($questId, $title, $stationId=null)
{
$data = $this->db->query(
'SELECT id '.
'FROM charactergroupsqueststations '.
'WHERE charactergroupsquest_id = ? AND (title = ? OR url = ?)',
'iss',
$questId,
$title,
\nre\core\Linker::createLinkParam($title)
);
return (!empty($data) && (is_null($stationId) || $stationId != $data[0]['id']));
}
/**
* Set the media for a Character groups Quest Station.
*
* @param int $stationId ID of Station to upload media for
* @param int $mediaId ID of Station media
*/
public function setPictureForStation($station, $mediaId)
{
$this->db->query(
'UPDATE charactergroupsqueststations '.
'SET stationpicture_id = ? '.
'WHERE id = ?',
'ii',
$mediaId,
$station
);
}
/**
* Create a new Character groups Quest Station.
*
* @param int $questId ID of Quest to create the Station for
* @param int $stationtypeId ID of Station type
* @param string $title Title
* @param string $prolog Prolog
* @param string $task Task description
* @param int $latitude GPS latitude
* @param int $longitude GPS longitude
* @param string $righttext Text for correctly solved task
* @param string $wrongtext Text for failed task
* @return int ID of newly created station
*/
public function createStation($questId, $stationtypeId, $title, $prolog, $task, $latitude, $longitude, $righttext, $wrongtext)
{
// Get position
$pos = $this->db->query(
'SELECT COALESCE(MAX(pos),0)+1 AS pos '.
'FROM charactergroupsqueststations '.
'WHERE charactergroupsquest_id = ?',
'i',
$questId
);
$pos = $pos[0]['pos'];
// Create Station
$this->db->query(
'INSERT INTO charactergroupsqueststations '.
'(charactergroupsquest_id, stationtype_id, title, url, pos, prolog, task, latitude, longitude, righttext, wrongtext) '.
'VALUES '.
'(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
'iississddss',
$questId, $stationtypeId, $title,
\nre\core\Linker::createLinkParam($title), $pos,
$prolog, $task, $latitude, $longitude, $righttext, $wrongtext
);
// Return ID of newly created Station
return $this->db->getInsertId();
}
/**
* Edit a Character groups Quest Station.
*
* @param int $stationId ID of Station to edit
* @param int $stationtypeId ID of Station type
* @param string $title Title
* @param string $prolog Prolog
* @param string $task Task description
* @param int $latitude GPS latitude
* @param int $longitude GPS longitude
* @param string $righttext Text for correctly solved task
* @param string $wrongtext Text for failed task
*/
public function editStation($stationId, $stationtypeId, $title, $prolog, $task, $latitude, $longitude, $righttext, $wrongtext)
{
$this->db->query(
'UPDATE charactergroupsqueststations '.
'SET stationtype_id = ?, title = ?, url = ?, prolog = ?, task = ?, latitude = ?, longitude = ?, righttext = ?, wrongtext = ? '.
'WHERE id = ?',
'issssddssi',
$stationtypeId, $title,
\nre\core\Linker::createLinkParam($title),
$prolog, $task, $latitude, $longitude, $righttext, $wrongtext,
$stationId
);
}
/**
* Delete a Character-group Quest station.
*
* @param int $stationId ID of Station to delete
*/
public function deleteStation($stationId)
{
$this->db->query(
'DELETE FROM charactergroupsqueststations '.
'WHERE ID = ?',
'i',
$stationId
);
}
/**
* Mark a Station for a Character group.
*
* @param int $stationId ID of Station to mark
* @param int $groupId ID of Character group
* @param int $status Status to set
*/
private function setStatus($stationId, $groupId, $status)
{
// Check if status is already set
$count = $this->db->query(
'SELECT count(*) AS c '.
'FROM charactergroupsqueststations_charactergroups '.
'WHERE charactergroupsqueststation_id = ? AND charactergroup_id = ? AND status = ?',
'iii',
$stationId,
$groupId,
$status
);
if(!empty($count) && intval($count[0]['c']) > 0) {
// Do not set status twice
return;
}
// Set status
$this->db->query(
'INSERT INTO charactergroupsqueststations_charactergroups '.
'(charactergroupsqueststation_id, charactergroup_id, status) '.
'VALUES '.
'(?, ?, ?) ',
'iii',
$stationId,
$groupId,
$status
);
}
}
?>