Browse files

refactored document handling in DB, temporarily disabling IVP

  • Loading branch information...
1 parent bd38506 commit 45a96a23245ff008949220382ef790b8314ca9e3 @tedch tedch committed Jun 20, 2011
View
26 application/controllers/crf.php
@@ -7,6 +7,17 @@ function __construct() {
$this->load->library('tank_auth');
}
+ private function _readonly() {
+ $may_write = $this->db
+ ->select('*')
+ ->from('users_groups')
+ ->join('groups', 'users_groups.group = groups.id', 'inner')
+ ->where('user', $this->tank_auth->get_user_id())
+ ->where('groups.name', 'wuser')
+ ->get();
+ return $may_write->num_rows() === 0;
+ }
+
function edit($token = -1, $date = -1) {
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
@@ -16,6 +27,9 @@ function edit($token = -1, $date = -1) {
if ($date === date('Y-m-d')) {
$this->load->model('CrfModel');
$crf = $this->CrfModel->load($token, $date);
+ if ($this->_readonly()) {
+ $crf['READONLY'] = 'READONLY';
+ }
$this->load->view('crfform', $crf);
} else if ($date < date('Y-m-d')) {
redirect("/pdf/$token" . '_crf_' . "$date.pdf");
@@ -59,12 +73,15 @@ function edit($token = -1, $date = -1) {
if ($this->form_validation->run() == FALSE) {
$this->load->model('CrfModel');
$crf = $this->CrfModel->init_from_post();
+ if ($this->_readonly()) {
+ $crf['READONLY'] = 'READONLY';
+ }
$this->load->view('crfform', $crf);
} else {
$this->load->model('CrfModel');
$patient = $this->input->post('patient');
$date = $this->input->post('date');
- $old_crf = $this->CrfModel->load($patient, $date);
+ $old_crf = $this->CrfModel->load($patient, $date, FALSE);
if (!$old_crf) {
$this->CrfModel->save();
@@ -75,8 +92,11 @@ function edit($token = -1, $date = -1) {
}
} else {
$this->load->model('CrfModel');
- $new_crf = $this->CrfModel->init($token, date('Y-m-d'));
- $this->load->view('crfform', $new_crf);
+ $crf = $this->CrfModel->init($token, date('Y-m-d'));
+ if ($this->_readonly()) {
+ $crf['READONLY'] = 'READONLY';
+ }
+ $this->load->view('crfform', $crf);
}
}
}
View
3 application/helpers/document_helper.php
@@ -8,6 +8,7 @@ function list_documents_by_date($patient, $doctype)
'wtp' => array('table' => 'wtps'));
if ($doctype === 'wtp') return '---'; // <<< XXX: TEMPORARY
+ if ($doctype === 'ivp') return '---'; // <<< XXX: TEMPORARY
$CI =& get_instance();
@@ -18,6 +19,7 @@ function list_documents_by_date($patient, $doctype)
$active = $CI->db
->select('*')
->from($docs[$doctype]['table'])
+ ->join('documents', $docs[$doctype]['table'] . '.document = documents.id', 'inner')
->where('patient', $patient)
->where('date', $today)
->where('pdf_url IS NULL')
@@ -36,6 +38,7 @@ function list_documents_by_date($patient, $doctype)
$historic = $CI->db
->select('*')
->from($docs[$doctype]['table'])
+ ->join('documents', $docs[$doctype]['table'] . '.document = documents.id', 'inner')
->where('patient', $patient)
->where('date <', $today)
->where('pdf_url IS NOT NULL')
View
187 application/models/crfmodel.php
@@ -8,13 +8,11 @@ function __construct() {
private function load_editors(&$crf) {
$crf['editors'] = array();
- if (isset($crf['id'])) {
+ if (isset($crf['document'])) {
$editors = $this->db
- ->select('username')
- ->select("to_char(stamp, 'YYYY-MM-DD HH24:MI:SS') as stamp", FALSE)
- ->from('crf_editors')
- ->join('users', 'crf_editors.user = users.id', 'inner')
- ->where('crf_editors.crf', $crf['id'])
+ ->select('*')
+ ->from('document_edits')
+ ->where('document', $crf['document'])
->order_by('stamp', 'desc')
->get();
foreach ($editors->result_array() as $ed) {
@@ -23,17 +21,36 @@ private function load_editors(&$crf) {
}
}
- function load($token, $date) {
+ function load($token, $date, $log = TRUE) {
$crf = $this->db
- ->select('*')
+ ->select('crfs.*')
+ ->select('documents.patient as patient')
+ ->select('documents.date as date')
->from('crfs')
+ ->join('documents', 'crfs.document = documents.id', 'inner')
->where('patient', $token)
->where('date', $date)
->limit(1)
->get();
if ($crf->num_rows() > 0) {
$crf = $crf->row_array();
$this->load_editors($crf);
+ if ($log) {
+ $event_type = $this->db
+ ->select('id')
+ ->from('event_types')
+ ->where('name', 'LOAD')
+ ->limit(1)
+ ->get();
+ if ($event_type->num_rows() > 0) {
+ $event_type = $event_type->row_array();
+ $event_log = array(
+ 'user' => $this->tank_auth->get_user_id(),
+ 'event_type' => $event_type['id'],
+ 'document' => $crf['document']);
+ $this->db->insert('event_log', $event_log);
+ }
+ }
return $crf;
}
return FALSE;
@@ -90,67 +107,98 @@ private function not_empty($value) {
}
function save() {
- $newcrf = array_filter(array(
- 'patient' => $this->input->post('patient'),
- 'date' => $this->input->post('date'),
- 'length' => $this->input->post('length'),
- 'weight' => $this->input->post('weight'),
- 'waist' => $this->input->post('waist'),
- 'hip' => $this->input->post('hip'),
- 'bhb' => $this->input->post('bhb'),
- 'fpglukos' => $this->input->post('fpglukos'),
- 'bhba1c' => $this->input->post('bhba1c'),
- 'pnatrium' => $this->input->post('pnatrium'),
- 'pkalium' => $this->input->post('pkalium'),
- 'pkreatinin' => $this->input->post('pkreatinin'),
- 'pkolesterol' => $this->input->post('pkolesterol'),
- 'pldlkolesterol' => $this->input->post('pldlkolesterol'),
- 'phdlkolesterol' => $this->input->post('phdlkolesterol'),
- 'fptriglycerider' => $this->input->post('phdlkolesterol'),
- 'ptsh' => $this->input->post('ptsh'),
- 'pft4' => $this->input->post('pft4'),
- 'pcrp' => $this->input->post('pcrp'),
- 'ualbumin' => $this->input->post('ualbumin'),
- 'bts' => $this->input->post('bts'),
- 'btd' => $this->input->post('btd'),
- 'pulse' => $this->input->post('pulse'),
- 'bts24day' => $this->input->post('bts24day'),
- 'btd24day' => $this->input->post('btd24day'),
- 'bts24night' => $this->input->post('bts24night'),
- 'btd24night' => $this->input->post('btd24night'),
- 'bts24' => $this->input->post('bts24'),
- 'btd24' => $this->input->post('btd24'),
- 'serum' => $this->input->post('serum'),
- 'plasma' => $this->input->post('plasma')), array($this, 'not_empty'));
-
- $this->db->insert('crfs', $newcrf);
-
- $crf_id = $this->db
- ->distinct()
+ $document_type = $this->db
->select('id')
- ->from('crfs')
- ->where('patient', $newcrf['patient'])
- ->where('date', $newcrf['date'])
+ ->from('document_types')
+ ->where('name', 'CRF')
->limit(1)
->get();
+ if ($document_type->num_rows() > 0) {
+ $document_type = $document_type->row_array();
+ $document_type = $document_type['id'];
- $crfid = 0;
- if ($crf_id->num_rows() > 0) {
- $row = $crf_id->row_array();
- $crfid = $row['id'];
- } else {
- die('FATAL: Error in application/models/crfmodel.php, newly created CRF not found.');
- }
+ $patient = $this->input->post('patient');
+ $date = $this->input->post('date');
+
+ $newdoc = array_filter(array(
+ 'patient' => $patient,
+ 'date' => $date,
+ 'document_type' => $document_type), array($this, 'not_empty'));
+
+ $this->db->insert('documents', $newdoc);
+
+ $document = $this->db
+ ->select('id')
+ ->from('documents')
+ ->where('patient', $patient)
+ ->where('date', $date)
+ ->where('document_type', $document_type)
+ ->get();
+
+ if ($document->num_rows() > 0) {
+ $document = $document->row_array();
+ $document = $document['id'];
+ } else {
+ die('FATAL: Error in application/models/crfmodel.php, newsly created DOCUMENT not found.');
+ }
+
+ $newcrf = array_filter(array(
+ 'document' => $document,
+ 'length' => $this->input->post('length'),
+ 'weight' => $this->input->post('weight'),
+ 'waist' => $this->input->post('waist'),
+ 'hip' => $this->input->post('hip'),
+ 'bhb' => $this->input->post('bhb'),
+ 'fpglukos' => $this->input->post('fpglukos'),
+ 'bhba1c' => $this->input->post('bhba1c'),
+ 'pnatrium' => $this->input->post('pnatrium'),
+ 'pkalium' => $this->input->post('pkalium'),
+ 'pkreatinin' => $this->input->post('pkreatinin'),
+ 'pkolesterol' => $this->input->post('pkolesterol'),
+ 'pldlkolesterol' => $this->input->post('pldlkolesterol'),
+ 'phdlkolesterol' => $this->input->post('phdlkolesterol'),
+ 'fptriglycerider' => $this->input->post('phdlkolesterol'),
+ 'ptsh' => $this->input->post('ptsh'),
+ 'pft4' => $this->input->post('pft4'),
+ 'pcrp' => $this->input->post('pcrp'),
+ 'ualbumin' => $this->input->post('ualbumin'),
+ 'bts' => $this->input->post('bts'),
+ 'btd' => $this->input->post('btd'),
+ 'pulse' => $this->input->post('pulse'),
+ 'bts24day' => $this->input->post('bts24day'),
+ 'btd24day' => $this->input->post('btd24day'),
+ 'bts24night' => $this->input->post('bts24night'),
+ 'btd24night' => $this->input->post('btd24night'),
+ 'bts24' => $this->input->post('bts24'),
+ 'btd24' => $this->input->post('btd24'),
+ 'serum' => $this->input->post('serum'),
+ 'plasma' => $this->input->post('plasma')), array($this, 'not_empty'));
- $edtr = array(
- 'user' => $this->tank_auth->get_user_id(),
- 'crf' => $crfid);
+ $this->db->insert('crfs', $newcrf);
- $this->db->insert('crf_editors', $edtr);
+ $event_type = $this->db
+ ->select('id')
+ ->from('event_types')
+ ->where('name', 'CREATE')
+ ->get();
+
+ if ($event_type->num_rows() > 0) {
+ $event_type = $event_type->row_array();
+ $event_type = $event_type['id'];
+
+ $event = array(
+ 'user' => $this->tank_auth->get_user_id(),
+ 'event_type' => $event_type,
+ 'document' => $document);
+
+ $this->db->insert('event_log', $event);
+ }
+ }
}
function update($oldcrf) {
$crfid = $oldcrf['id'];
+ $document = $oldcrf['document'];
$changed = FALSE;
@@ -185,10 +233,23 @@ function update($oldcrf) {
}
if ($changed) {
- $edtr = array(
- 'user' => $this->tank_auth->get_user_id(),
- 'crf' => $crfid);
- $this->db->insert('crf_editors', $edtr);
+ $event_type = $this->db
+ ->select('id')
+ ->from('event_types')
+ ->where('name', 'CHANGE')
+ ->get();
+
+ if ($event_type->num_rows() > 0) {
+ $event_type = $event_type->row_array();
+ $event_type = $event_type['id'];
+
+ $event = array(
+ 'user' => $this->tank_auth->get_user_id(),
+ 'event_type' => $event_type,
+ 'document' => $document);
+
+ $this->db->insert('event_log', $event);
+ }
}
}
}
View
7 application/models/mainmodel.php
@@ -14,12 +14,9 @@ function get_all_patients($offset, $ipp, $all = FALSE) {
->join('users_groups', 'groups.id = users_groups.group', 'inner')
->where('users_groups.user', $userid)
->get();
- $vcs = array();
+ $vcs = array('Dalby', 'Bara');
foreach ($groups->result_array() as $group) {
- if ($group['group'] == 'user') {
- $vcs[] = 'Dalby';
- $vcs[] = 'Bara';
- } else if ($group['group'] == 'cpf') {
+ if ($group['group'] == 'cpf') {
$vcs[] = 'CPF';
}
}
View
8 application/views/crfform.php
@@ -353,7 +353,11 @@
</table>
<?php
-echo '<p>' . form_reset('reset', 'Ångra') . form_submit('submit', 'Spara') . '</p>';
+if (isset($READONLY) && $READONLY == 'READONLY') {
+ echo '<p></p>';
+} else {
+ echo '<p>' . form_reset('reset', 'Ångra') . form_submit('submit', 'Spara') . '</p>';
+}
echo form_close();
?>
@@ -363,7 +367,7 @@
<ul>
<?php
foreach ($editors as $ed) {
- echo '<li>' . $ed['stamp'] . ' (<b>' . $ed['username'] . '</b>)</li>' . "\n";
+ echo '<li>' . $ed['stamp'] . ' (<b>' . $ed['username'] . '</b>) ' . $ed['event_type'] . '</li>' . "\n";
}}
?>
</ul></div>
View
2 application/views/footer.php
@@ -2,7 +2,7 @@
$CI =& get_instance();
?>
<address>
- <p>DALICO-admin v0.95<br />
+ <p>DALICO-admin v0.96<br />
&copy;&nbsp;2011&nbsp;<a href="mailto:Christian.LD.Andersson@gmail.com">Christian Andersson</a><br />
</p><ul>
<li>SERVER_ADDR: <?=$CI->input->server('SERVER_ADDR')?></li>

0 comments on commit 45a96a2

Please sign in to comment.