questlab/doc/files/models/SeminariesModel.inc.txt

243 lines
5.6 KiB
Text

<?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 \nre\exceptions\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 \nre\exceptions\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) {
$xps += $questgroup['achievable_xps'];
}
}
return $xps;
}
/**
* Check if a Seminary title already exists.
*
* @param string $title Seminary title to check
* @param int $seminaryId Do not check this ID (for editing)
* @return boolean Whether Seminary title exists or not
*/
public function seminaryTitleExists($title, $seminaryId=null)
{
$data = $this->db->query(
'SELECT id '.
'FROM seminaries '.
'WHERE title = ? OR url = ?',
'ss',
$title,
\nre\core\Linker::createLinkParam($title)
);
return (!empty($data) && (is_null($seminaryId) || $seminaryId != $data[0]['id']));
}
/**
* Create a new Seminary.
*
* @param int $userId ID of creating user
* @param string $title Title of Seminary to create
* @param string $course Course of Seminary
* @param string $description Description of new Seminary
* @return int ID of the newly created Seminary
*/
public function createSeminary($userId, $title, $course, $description)
{
$this->db->query(
'INSERT INTO seminaries '.
'(created_user_id, title, url, course, description) '.
'VALUES '.
'(?, ?, ?, ?, ?)',
'issss',
$userId,
$title,
\nre\core\Linker::createLinkParam($title),
$course,
$description
);
return $this->db->getInsertId();
}
/**
* Set the moodpic for a Seminary.
*
* @param int $seminaryId ID of Seminary to set moodpic for
* @param int $mediaId ID of moodpic media
*/
public function setMoodpicForSeminary($seminaryId, $mediaId)
{
$this->db->query(
'UPDATE seminaries '.
'SET seminarymedia_id = ? '.
'WHERE id = ?',
'ii',
$mediaId,
$seminaryId
);
}
/**
* Edit a seminary.
*
* @throws \nre\exceptions\DatamodelException
* @param int $seminaryId ID of Seminary to edit
* @param string $title New title of Seminary
* @param string $course New course of Seminary
* @param string $description New description of Seminary
*/
public function editSeminary($seminaryId, $title, $course, $description)
{
$this->db->query(
'UPDATE seminaries '.
'SET title = ?, url = ?, course = ?, description = ? '.
'WHERE id = ?',
'ssssi',
$title,
\nre\core\Linker::createLinkParam($title),
$course,
$description,
$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);
}
}
?>