550 lines
19 KiB
PHP
550 lines
19 KiB
PHP
<?php
|
|
|
|
class Screeningtemplate extends MY_Controller
|
|
{
|
|
var $db_oneklinik;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_oneklinik = $this->load->database("onedev", true);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
echo "SCREENING TEMPLATE API";
|
|
}
|
|
|
|
public function search()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$search = isset($prm['search']) ? trim($prm['search']) : '';
|
|
$like = '%' . $search . '%';
|
|
$row_per_page = isset($prm['row_per_page']) && intval($prm['row_per_page']) > 0 ? intval($prm['row_per_page']) : 10;
|
|
$page = isset($prm['page']) && intval($prm['page']) > 0 ? intval($prm['page']) : 1;
|
|
$offset = ($page - 1) * $row_per_page;
|
|
|
|
$allowed_order_by = array(
|
|
'id' => 't.M_ScreeningTemplateID',
|
|
'code' => 't.M_ScreeningTemplateCode',
|
|
'name' => 't.M_ScreeningTemplateName',
|
|
'description' => 't.M_ScreeningTemplateDescription'
|
|
);
|
|
$order_by = 't.M_ScreeningTemplateID';
|
|
if (isset($prm['order_by']) && isset($allowed_order_by[$prm['order_by']])) {
|
|
$order_by = $allowed_order_by[$prm['order_by']];
|
|
}
|
|
$order = isset($prm['order']) && strtolower($prm['order']) === 'desc' ? 'DESC' : 'ASC';
|
|
|
|
$sql_count = "SELECT COUNT(*) AS total
|
|
FROM one_klinik.m_screening_template t
|
|
WHERE t.M_ScreeningTemplateIsActive = 'Y'
|
|
AND (
|
|
t.M_ScreeningTemplateCode LIKE ?
|
|
OR t.M_ScreeningTemplateName LIKE ?
|
|
OR IFNULL(t.M_ScreeningTemplateDescription, '') LIKE ?
|
|
)";
|
|
$query_count = $this->db_oneklinik->query($sql_count, array($like, $like, $like));
|
|
if (!$query_count) {
|
|
$this->sys_error_db("m_screening_template count", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$total_filter = intval($query_count->row()->total);
|
|
$total_page = ceil($total_filter / $row_per_page);
|
|
|
|
$sql = "SELECT
|
|
t.M_ScreeningTemplateID AS id,
|
|
t.M_ScreeningTemplateCode AS code,
|
|
t.M_ScreeningTemplateName AS name,
|
|
t.M_ScreeningTemplateDescription AS description,
|
|
t.M_ScreeningTemplateIsActive AS is_active,
|
|
t.M_ScreeningTemplateCreated AS created,
|
|
t.M_ScreeningTemplateLastUpdated AS last_updated,
|
|
COUNT(f.M_ScreeningFormID) AS form_count
|
|
FROM one_klinik.m_screening_template t
|
|
LEFT JOIN one_klinik.m_screening_form f
|
|
ON f.M_ScreeningFormM_ScreeningTemplateID = t.M_ScreeningTemplateID
|
|
AND f.M_ScreeningFormIsActive = 'Y'
|
|
WHERE t.M_ScreeningTemplateIsActive = 'Y'
|
|
AND (
|
|
t.M_ScreeningTemplateCode LIKE ?
|
|
OR t.M_ScreeningTemplateName LIKE ?
|
|
OR IFNULL(t.M_ScreeningTemplateDescription, '') LIKE ?
|
|
)
|
|
GROUP BY
|
|
t.M_ScreeningTemplateID
|
|
ORDER BY {$order_by} {$order}
|
|
LIMIT ? OFFSET ?";
|
|
$query = $this->db_oneklinik->query($sql, array($like, $like, $like, $row_per_page, $offset));
|
|
if (!$query) {
|
|
$this->sys_error_db("m_screening_template select", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok(array(
|
|
"total" => $total_page,
|
|
"total_filter" => $total_filter,
|
|
"records" => $query->result_array()
|
|
));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getdetail()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$id = isset($prm['id']) ? intval($prm['id']) : 0;
|
|
if (!$id) {
|
|
$this->sys_error("id is mandatory");
|
|
exit;
|
|
}
|
|
|
|
$query = $this->db_oneklinik->query(
|
|
"SELECT
|
|
M_ScreeningTemplateID AS id,
|
|
M_ScreeningTemplateCode AS code,
|
|
M_ScreeningTemplateName AS name,
|
|
M_ScreeningTemplateDescription AS description,
|
|
M_ScreeningTemplateIsActive AS is_active,
|
|
M_ScreeningTemplateCreated AS created,
|
|
M_ScreeningTemplateLastUpdated AS last_updated
|
|
FROM one_klinik.m_screening_template
|
|
WHERE M_ScreeningTemplateID = ?
|
|
AND M_ScreeningTemplateIsActive = 'Y'",
|
|
array($id)
|
|
);
|
|
if (!$query) {
|
|
$this->sys_error_db("m_screening_template select detail", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$row = $query->row_array();
|
|
if (!$row) {
|
|
$this->sys_ok(array("total" => 0, "records" => null));
|
|
exit;
|
|
}
|
|
|
|
$row['forms'] = $this->get_form_rows($id);
|
|
$this->sys_ok(array("total" => 1, "records" => $row));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
public function getforms()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$template_id = isset($prm['template_id']) ? intval($prm['template_id']) : (isset($prm['id']) ? intval($prm['id']) : 0);
|
|
if (!$template_id) {
|
|
$this->sys_error("template_id is mandatory");
|
|
exit;
|
|
}
|
|
|
|
$rows = $this->get_form_rows($template_id);
|
|
$this->sys_ok(array("total" => count($rows), "records" => $rows));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
public function add()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$code = isset($prm['code']) ? trim($prm['code']) : '';
|
|
$name = isset($prm['name']) ? trim($prm['name']) : '';
|
|
$description = isset($prm['description']) ? trim($prm['description']) : null;
|
|
$userid = $this->sys_user["M_UserID"];
|
|
|
|
if ($code === '' || $name === '') {
|
|
$this->sys_error("code and name are mandatory");
|
|
exit;
|
|
}
|
|
|
|
$duplicate = $this->db_oneklinik->query(
|
|
"SELECT COUNT(*) AS total
|
|
FROM one_klinik.m_screening_template
|
|
WHERE M_ScreeningTemplateCode = ?
|
|
OR (M_ScreeningTemplateIsActive = 'Y' AND M_ScreeningTemplateName = ?)",
|
|
array($code, $name)
|
|
);
|
|
if (!$duplicate) {
|
|
$this->sys_error_db("m_screening_template duplicate check", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
if (intval($duplicate->row()->total) > 0) {
|
|
$this->sys_ok(array(
|
|
"total" => -1,
|
|
"errors" => array(array("field" => "code", "msg" => "Kode atau nama sudah ada")),
|
|
"records" => 0
|
|
));
|
|
exit;
|
|
}
|
|
|
|
$sql = "INSERT INTO one_klinik.m_screening_template (
|
|
M_ScreeningTemplateCode,
|
|
M_ScreeningTemplateName,
|
|
M_ScreeningTemplateDescription,
|
|
M_ScreeningTemplateUserID,
|
|
M_ScreeningTemplateCreated,
|
|
M_ScreeningTemplateLastUpdated
|
|
) VALUES (?, ?, ?, ?, NOW(), NOW())";
|
|
$query = $this->db_oneklinik->query($sql, array($code, $name, $description, $userid));
|
|
if (!$query) {
|
|
$this->sys_error_db("m_screening_template insert", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok(array(
|
|
"total" => 1,
|
|
"records" => array("xid" => $this->db_oneklinik->insert_id())
|
|
));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
public function update()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$id = isset($prm['id']) ? intval($prm['id']) : 0;
|
|
$code = isset($prm['code']) ? trim($prm['code']) : '';
|
|
$name = isset($prm['name']) ? trim($prm['name']) : '';
|
|
$description = isset($prm['description']) ? trim($prm['description']) : null;
|
|
$userid = $this->sys_user["M_UserID"];
|
|
|
|
if (!$id || $code === '' || $name === '') {
|
|
$this->sys_error("id, code and name are mandatory");
|
|
exit;
|
|
}
|
|
|
|
$duplicate = $this->db_oneklinik->query(
|
|
"SELECT COUNT(*) AS total
|
|
FROM one_klinik.m_screening_template
|
|
WHERE M_ScreeningTemplateID <> ?
|
|
AND (M_ScreeningTemplateCode = ?
|
|
OR (M_ScreeningTemplateIsActive = 'Y' AND M_ScreeningTemplateName = ?))",
|
|
array($id, $code, $name)
|
|
);
|
|
if (!$duplicate) {
|
|
$this->sys_error_db("m_screening_template duplicate check", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
if (intval($duplicate->row()->total) > 0) {
|
|
$this->sys_ok(array(
|
|
"total" => -1,
|
|
"errors" => array(array("field" => "code", "msg" => "Kode atau nama sudah ada")),
|
|
"records" => 0
|
|
));
|
|
exit;
|
|
}
|
|
|
|
$sql = "UPDATE one_klinik.m_screening_template SET
|
|
M_ScreeningTemplateCode = ?,
|
|
M_ScreeningTemplateName = ?,
|
|
M_ScreeningTemplateDescription = ?,
|
|
M_ScreeningTemplateUserID = ?,
|
|
M_ScreeningTemplateLastUpdated = NOW()
|
|
WHERE M_ScreeningTemplateID = ?
|
|
AND M_ScreeningTemplateIsActive = 'Y'";
|
|
$query = $this->db_oneklinik->query($sql, array($code, $name, $description, $userid, $id));
|
|
if (!$query) {
|
|
$this->sys_error_db("m_screening_template update", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok(array("total" => 1, "records" => array("xid" => $id)));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
public function delete()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$id = isset($prm['id']) ? intval($prm['id']) : 0;
|
|
if (!$id) {
|
|
$this->sys_error("id is mandatory");
|
|
exit;
|
|
}
|
|
$userid = $this->sys_user["M_UserID"];
|
|
|
|
$this->db_oneklinik->trans_begin();
|
|
|
|
$query = $this->db_oneklinik->query(
|
|
"UPDATE one_klinik.m_screening_form SET
|
|
M_ScreeningFormIsActive = 'N',
|
|
M_ScreeningFormUserID = ?,
|
|
M_ScreeningFormLastUpdated = NOW()
|
|
WHERE M_ScreeningFormM_ScreeningTemplateID = ?",
|
|
array($userid, $id)
|
|
);
|
|
if (!$query) {
|
|
$this->db_oneklinik->trans_rollback();
|
|
$this->sys_error_db("m_screening_form delete by template", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$query = $this->db_oneklinik->query(
|
|
"UPDATE one_klinik.m_screening_template SET
|
|
M_ScreeningTemplateIsActive = 'N',
|
|
M_ScreeningTemplateUserID = ?,
|
|
M_ScreeningTemplateLastUpdated = NOW()
|
|
WHERE M_ScreeningTemplateID = ?",
|
|
array($userid, $id)
|
|
);
|
|
if (!$query) {
|
|
$this->db_oneklinik->trans_rollback();
|
|
$this->sys_error_db("m_screening_template delete", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$this->db_oneklinik->trans_complete();
|
|
$this->sys_ok(array("total" => 1, "records" => array("xid" => $id)));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
public function addform()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$template_id = isset($prm['template_id']) ? intval($prm['template_id']) : 0;
|
|
$question = isset($prm['question']) ? trim($prm['question']) : '';
|
|
$answer_type = isset($prm['answer_type']) ? trim($prm['answer_type']) : 'single';
|
|
$options = isset($prm['options']) ? $this->normalize_options($prm['options'], $answer_type) : null;
|
|
$sort_order = isset($prm['sort_order']) ? intval($prm['sort_order']) : 0;
|
|
$is_required = isset($prm['is_required']) && $prm['is_required'] === 'N' ? 'N' : 'Y';
|
|
$userid = $this->sys_user["M_UserID"];
|
|
|
|
if (!$template_id || $question === '') {
|
|
$this->sys_error("template_id and question are mandatory");
|
|
exit;
|
|
}
|
|
if (!$this->is_valid_answer_type($answer_type)) {
|
|
$this->sys_error("answer_type must be single, multi, or text");
|
|
exit;
|
|
}
|
|
|
|
$sql = "INSERT INTO one_klinik.m_screening_form (
|
|
M_ScreeningFormM_ScreeningTemplateID,
|
|
M_ScreeningFormQuestion,
|
|
M_ScreeningFormAnswerType,
|
|
M_ScreeningFormOptions,
|
|
M_ScreeningFormSortOrder,
|
|
M_ScreeningFormIsRequired,
|
|
M_ScreeningFormUserID,
|
|
M_ScreeningFormCreated,
|
|
M_ScreeningFormLastUpdated
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW())";
|
|
$query = $this->db_oneklinik->query($sql, array(
|
|
$template_id,
|
|
$question,
|
|
$answer_type,
|
|
$options,
|
|
$sort_order,
|
|
$is_required,
|
|
$userid
|
|
));
|
|
if (!$query) {
|
|
$this->sys_error_db("m_screening_form insert", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok(array(
|
|
"total" => 1,
|
|
"records" => array("xid" => $this->db_oneklinik->insert_id())
|
|
));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
public function updateform()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$id = isset($prm['id']) ? intval($prm['id']) : 0;
|
|
$template_id = isset($prm['template_id']) ? intval($prm['template_id']) : 0;
|
|
$question = isset($prm['question']) ? trim($prm['question']) : '';
|
|
$answer_type = isset($prm['answer_type']) ? trim($prm['answer_type']) : 'single';
|
|
$options = isset($prm['options']) ? $this->normalize_options($prm['options'], $answer_type) : null;
|
|
$sort_order = isset($prm['sort_order']) ? intval($prm['sort_order']) : 0;
|
|
$is_required = isset($prm['is_required']) && $prm['is_required'] === 'N' ? 'N' : 'Y';
|
|
$userid = $this->sys_user["M_UserID"];
|
|
|
|
if (!$id || !$template_id || $question === '') {
|
|
$this->sys_error("id, template_id and question are mandatory");
|
|
exit;
|
|
}
|
|
if (!$this->is_valid_answer_type($answer_type)) {
|
|
$this->sys_error("answer_type must be single, multi, or text");
|
|
exit;
|
|
}
|
|
|
|
$sql = "UPDATE one_klinik.m_screening_form SET
|
|
M_ScreeningFormM_ScreeningTemplateID = ?,
|
|
M_ScreeningFormQuestion = ?,
|
|
M_ScreeningFormAnswerType = ?,
|
|
M_ScreeningFormOptions = ?,
|
|
M_ScreeningFormSortOrder = ?,
|
|
M_ScreeningFormIsRequired = ?,
|
|
M_ScreeningFormUserID = ?,
|
|
M_ScreeningFormLastUpdated = NOW()
|
|
WHERE M_ScreeningFormID = ?
|
|
AND M_ScreeningFormIsActive = 'Y'";
|
|
$query = $this->db_oneklinik->query($sql, array(
|
|
$template_id,
|
|
$question,
|
|
$answer_type,
|
|
$options,
|
|
$sort_order,
|
|
$is_required,
|
|
$userid,
|
|
$id
|
|
));
|
|
if (!$query) {
|
|
$this->sys_error_db("m_screening_form update", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok(array("total" => 1, "records" => array("xid" => $id)));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
public function deleteform()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$id = isset($prm['id']) ? intval($prm['id']) : 0;
|
|
if (!$id) {
|
|
$this->sys_error("id is mandatory");
|
|
exit;
|
|
}
|
|
$userid = $this->sys_user["M_UserID"];
|
|
|
|
$sql = "UPDATE one_klinik.m_screening_form SET
|
|
M_ScreeningFormIsActive = 'N',
|
|
M_ScreeningFormUserID = ?,
|
|
M_ScreeningFormLastUpdated = NOW()
|
|
WHERE M_ScreeningFormID = ?";
|
|
$query = $this->db_oneklinik->query($sql, array($userid, $id));
|
|
if (!$query) {
|
|
$this->sys_error_db("m_screening_form delete", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok(array("total" => 1, "records" => array("xid" => $id)));
|
|
} catch (Exception $exc) {
|
|
$this->sys_error($exc->getMessage());
|
|
}
|
|
}
|
|
|
|
private function get_form_rows($template_id)
|
|
{
|
|
$query = $this->db_oneklinik->query(
|
|
"SELECT
|
|
M_ScreeningFormID AS id,
|
|
M_ScreeningFormM_ScreeningTemplateID AS template_id,
|
|
M_ScreeningFormQuestion AS question,
|
|
M_ScreeningFormAnswerType AS answer_type,
|
|
M_ScreeningFormOptions AS options,
|
|
M_ScreeningFormSortOrder AS sort_order,
|
|
M_ScreeningFormIsRequired AS is_required,
|
|
M_ScreeningFormIsActive AS is_active,
|
|
M_ScreeningFormCreated AS created,
|
|
M_ScreeningFormLastUpdated AS last_updated
|
|
FROM one_klinik.m_screening_form
|
|
WHERE M_ScreeningFormM_ScreeningTemplateID = ?
|
|
AND M_ScreeningFormIsActive = 'Y'
|
|
ORDER BY M_ScreeningFormSortOrder ASC, M_ScreeningFormID ASC",
|
|
array($template_id)
|
|
);
|
|
if (!$query) {
|
|
$this->sys_error_db("m_screening_form select", $this->db_oneklinik);
|
|
exit;
|
|
}
|
|
|
|
$rows = $query->result_array();
|
|
foreach ($rows as $k => $row) {
|
|
$rows[$k]['options_json'] = $row['options'] ? json_decode($row['options'], true) : null;
|
|
}
|
|
return $rows;
|
|
}
|
|
|
|
private function is_valid_answer_type($answer_type)
|
|
{
|
|
return in_array($answer_type, array('single', 'multi', 'text'));
|
|
}
|
|
|
|
private function normalize_options($options, $answer_type)
|
|
{
|
|
if ($answer_type === 'text') {
|
|
return null;
|
|
}
|
|
if ($options === null) {
|
|
return null;
|
|
}
|
|
if (is_array($options)) {
|
|
return json_encode($options);
|
|
}
|
|
$options = trim($options);
|
|
return $options === '' ? null : $options;
|
|
}
|
|
}
|