implement CRUD for Questgroupshierarchy
This commit is contained in:
parent
8ef116f8ae
commit
7264b623c1
11 changed files with 737 additions and 4 deletions
|
|
@ -59,6 +59,32 @@
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a Questgroup hierarchy by its URL.
|
||||
*
|
||||
* throws IdNotFoundException
|
||||
* @param int $questgroupshierarchyURL URL of a Questgroup hierarchy
|
||||
* @return array Questgroup hierarchy
|
||||
*/
|
||||
public function getHierarchyByUrl($seminaryId, $questgroupshierarchyUrl)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id, seminary_id, parent_questgroupshierarchy_id, pos, title_singular, title_plural, url '.
|
||||
'FROM questgroupshierarchy '.
|
||||
'WHERE questgroupshierarchy.seminary_id = ? AND questgroupshierarchy.url = ?',
|
||||
'is',
|
||||
$seminaryId,
|
||||
$questgroupshierarchyUrl
|
||||
);
|
||||
if(empty($data)) {
|
||||
throw new \nre\exceptions\IdNotFoundException($questgroupshierarchyUrl);
|
||||
}
|
||||
|
||||
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the toplevel hierarchy entries of a Seminary.
|
||||
*
|
||||
|
|
@ -123,6 +149,170 @@
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a title for a Questgroupshierarchy already exists
|
||||
* for a Seminary.
|
||||
*
|
||||
* @param string $title
|
||||
* @param int $seminaryId
|
||||
* @param int $questgroupshierarchyId Do not check this Questgroupshierarchy (for editing)
|
||||
* @return Whether title already exists or not
|
||||
*/
|
||||
public function questgroupshierarchyTitleSingularExists($title, $seminaryId, $questgroupshierarchyId=null)
|
||||
{
|
||||
$data = $this->db->query(
|
||||
'SELECT id '.
|
||||
'FROM questgroupshierarchy '.
|
||||
'WHERE seminary_id = ? AND (title_singular = ? OR url = ?) ',
|
||||
'iss',
|
||||
$seminaryId,
|
||||
$title,
|
||||
\nre\core\Linker::createLinkParam($title)
|
||||
);
|
||||
|
||||
|
||||
return (!empty($data) && (is_null($questgroupshierarchyId) || $questgroupshierarchyId != $data[0]['id']));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a new Questgroupshierarchy for a Seminary
|
||||
*
|
||||
* @param int $userId ID of creating user
|
||||
* @param int $seminaryId ID of Seminary
|
||||
* @param int $parentQuestgroupsierarchyId ID of parent Questgroupshierarchy
|
||||
* @param string $title Title (singular)
|
||||
* @param string $course Title (plural)
|
||||
*/
|
||||
public function createQuestgroupshierarchy($userId, $seminaryId, $parentQuestgroupsierarchyId, $titleSingular, $titlePlural)
|
||||
{
|
||||
// Get last position
|
||||
$pos = $this->db->query(
|
||||
'SELECT COALESCE(MAX(pos),0) AS pos '.
|
||||
'FROM questgroupshierarchy '.
|
||||
'WHERE seminary_id = ? AND '.
|
||||
'parent_questgroupshierarchy_id '.(!is_null($parentQuestgroupsierarchyId) ? sprintf('= %d', $parentQuestgroupsierarchyId) : 'IS NULL'),
|
||||
'i',
|
||||
$seminaryId
|
||||
);
|
||||
$pos = intval($pos[0]['pos']);
|
||||
|
||||
// Create Questgroupshierarchy
|
||||
$this->db->query(
|
||||
'INSERT INTO questgroupshierarchy '.
|
||||
'(created_user_id, seminary_id, parent_questgroupshierarchy_id, pos, title_singular, title_plural, url) '.
|
||||
'VALUES '.
|
||||
'(?, ?, ?, ?, ?, ?, ?)',
|
||||
'iiiisss',
|
||||
$userId,
|
||||
$seminaryId,
|
||||
$parentQuestgroupsierarchyId,
|
||||
$pos + 1,
|
||||
$titleSingular,
|
||||
$titlePlural,
|
||||
\nre\core\Linker::createLinkParam($titleSingular)
|
||||
);
|
||||
|
||||
|
||||
return $this->db->getInsertId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit a Questgroupshierarchy.
|
||||
*
|
||||
* @param int $questgroupshierarchyId ID of Questgroupshierarchy to edit
|
||||
* @param string $title New title (singular)
|
||||
* @param string $course New title (plural)
|
||||
*/
|
||||
public function editQuestgroupshierarchy($questgroupshierarchyId, $titleSingular, $titlePlural)
|
||||
{
|
||||
$this->db->query(
|
||||
'UPDATE questgroupshierarchy '.
|
||||
'SET title_singular = ?, title_plural = ?, url = ? '.
|
||||
'WHERE id = ?',
|
||||
'sssi',
|
||||
$titleSingular,
|
||||
$titlePlural,
|
||||
\nre\core\Linker::createLinkParam($titleSingular),
|
||||
$questgroupshierarchyId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Move a Questgroupshierarchy up (decrement position) or down
|
||||
* (increment position).
|
||||
*
|
||||
* @param array $questgroupshierarchy Questgroupshierarchy to move
|
||||
* @param boolean $up True for moving up, false for down
|
||||
*/
|
||||
public function moveQuestgroupshierarchy($questgroupshierarchy, $up)
|
||||
{
|
||||
$this->db->setAutocommit(false);
|
||||
try {
|
||||
// Set temporary position
|
||||
$this->db->query(
|
||||
'UPDATE questgroupshierarchy '.
|
||||
'SET pos = 0 '.
|
||||
'WHERE id = ?',
|
||||
'i',
|
||||
$questgroupshierarchy['id']
|
||||
);
|
||||
// Switch entry
|
||||
if(is_null($questgroupshierarchy['parent_questgroupshierarchy_id'])) {
|
||||
$this->db->query(
|
||||
'UPDATE questgroupshierarchy '.
|
||||
'SET pos = ? '.
|
||||
'WHERE parent_questgroupshierarchy_id IS NULL AND pos = ?',
|
||||
'ii',
|
||||
$questgroupshierarchy['pos'],
|
||||
$questgroupshierarchy['pos'] + ($up ? -1 : 1)
|
||||
);
|
||||
}
|
||||
else {
|
||||
$this->db->query(
|
||||
'UPDATE questgroupshierarchy '.
|
||||
'SET pos = ? '.
|
||||
'WHERE parent_questgroupshierarchy_id = ? AND pos = ?',
|
||||
'iii',
|
||||
$questgroupshierarchy['pos'],
|
||||
$questgroupshierarchy['parent_questgroupshierarchy_id'],
|
||||
$questgroupshierarchy['pos'] + ($up ? -1 : 1)
|
||||
);
|
||||
}
|
||||
// Set new position
|
||||
$this->db->query(
|
||||
'UPDATE questgroupshierarchy '.
|
||||
'SET pos = ? '.
|
||||
'WHERE id = ?',
|
||||
'ii',
|
||||
$questgroupshierarchy['pos'] + ($up ? -1 : 1),
|
||||
$questgroupshierarchy['id']
|
||||
);
|
||||
|
||||
$this->db->commit();
|
||||
}
|
||||
catch(\nre\exceptions\DatamodelException $e) {
|
||||
$this->db->rollback();
|
||||
$this->db->setAutocommit(true);
|
||||
throw $e;
|
||||
}
|
||||
$this->db->setAutocommit(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Delete a Questgroupshierarchy.
|
||||
*
|
||||
* @param int $seminaryId ID of the seminary to delete
|
||||
*/
|
||||
public function deleteQuestgroupshierarchy($questgroupshierarchyId)
|
||||
{
|
||||
$this->db->query('DELETE FROM questgroupshierarchy WHERE id = ?', 'i', $questgroupshierarchyId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue