1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27:
28: 29: 30: 31: 32: 33: 34: 35: 36:
37: class Rep {
38:
39: 40: 41: 42: 43:
44: public $rep_id = '';
45:
46:
47: 48: 49: 50: 51:
52: public $date = '';
53:
54:
55: 56: 57: 58: 59:
60: public $patient = '';
61:
62:
63: 64: 65: 66: 67:
68: public $note = '';
69:
70:
71: 72: 73: 74: 75:
76: public $prescription = '';
77:
78:
79: 80: 81: 82: 83:
84: public $sym_select = array();
85:
86:
87: 88: 89: 90: 91:
92: private $symptoms_ar = array();
93:
94:
95: 96: 97: 98: 99:
100: public $remedies_ar = array();
101:
102:
103: 104: 105: 106: 107:
108: private $rel_ar = array();
109:
110:
111: 112: 113: 114: 115:
116: public $sym_count;
117:
118:
119: 120: 121: 122: 123:
124: public $rem_count;
125:
126:
127: 128: 129: 130: 131:
132: public $rel_count;
133:
134:
135: 136: 137: 138: 139:
140: private $symptoms_tbl;
141:
142:
143: 144: 145: 146: 147:
148: private $sym_rem_tbl;
149:
150:
151:
152: 153: 154: 155: 156: 157:
158: function __construct() {
159: global $db;
160: if (isset($_REQUEST['rep'])) {
161: $this->rep_id = $_REQUEST['rep'];
162: $query = "SELECT sym_id, degree FROM rep_sym WHERE rep_id='" . $this->rep_id . "' ORDER BY degree DESC, sym_id";
163: $db->send_query($query);
164: while(list($sym_id, $degree) = $db->db_fetch_row()) {
165: $this->sym_select[$sym_id] = $degree;
166: }
167: $db->free_result();
168: $query = "SELECT UNIX_TIMESTAMP(rep_timestamp), patient_id, rep_note, rep_prescription, sym_table FROM repertorizations WHERE rep_id='" . $this->rep_id . "'";
169: $db->send_query($query);
170: list($timestamp, $this->patient, $this->note, $this->prescription, $sym_table) = $db->db_fetch_row();
171: $db->free_result();
172: $this->date = date("Y-m-d", $timestamp);
173: $this->symptoms_tbl = $db->table_exists($sym_table) ? $sym_table : 'symptoms';
174: if ($this->symptoms_tbl === 'symptoms' || $this->symptoms_tbl === 'sym__de' || $this->symptoms_tbl === 'sym__en') {
175: $this->sym_rem_tbl = 'sym_rem';
176: } else {
177: $clean_symptoms_tbl = $this->symptoms_tbl;
178: if (strpos($this->symptoms_tbl, '_de') !== false || strpos($this->symptoms_tbl, '_en') !== false) {
179: $clean_symptoms_tbl = substr($this->symptoms_tbl, 0, -3);
180: }
181: $sym_rem_tbl = str_replace('sym__', 'sym_rem__', $clean_symptoms_tbl);
182: $this->sym_rem_tbl = $db->table_exists($sym_rem_tbl) ? $sym_rem_tbl : 'sym_rem';
183: }
184: } else {
185: $this->sym_rem_tbl = $db->get_custom_table("sym_rem");
186: $this->symptoms_tbl = $db->get_custom_table("symptoms");
187: }
188: if (!empty($_REQUEST['symsel'])) {
189: $sym_ar = explode("_", $_REQUEST["symsel"]);
190: foreach ($sym_ar as $symptom) {
191: list($sym_id, $degree) = explode('-', $symptom);
192: $this->sym_select[$sym_id] = $degree;
193: }
194: arsort($this->sym_select);
195: }
196: if (!empty($_REQUEST['date'])) {
197: $this->date = urldecode($_REQUEST['date']);
198: } elseif(empty($_REQUEST['rep'])) {
199: $this->date = date("Y-m-d");
200: }
201: if (!empty($_REQUEST['patient'])) {
202: $this->patient = urldecode($_REQUEST['patient']);
203: }
204: if (!empty($_REQUEST['note'])) {
205: $this->note = urldecode($_REQUEST['note']);
206: }
207: if (!empty($_REQUEST['prescription'])) {
208: $this->prescription = urldecode($_REQUEST['prescription']);
209: }
210: if (!empty($this->sym_select)) {
211: $this->get_result_data();
212: }
213: if (!empty($this->remedies_ar)) {
214: $this->get_symptoms_ar();
215: }
216: }
217:
218: 219: 220: 221: 222: 223: 224: 225:
226: private function get_result_data() {
227: global $db;
228: $query = "DROP TEMPORARY TABLE IF EXISTS result";
229: $db->send_query($query);
230: $query = "CREATE TEMPORARY TABLE result (
231: rem_id smallint(4) unsigned NOT NULL,
232: points mediumint(8) unsigned NOT NULL,
233: hits mediumint(8) unsigned NOT NULL,
234: PRIMARY KEY(rem_id)
235: ) ENGINE=MEMORY DEFAULT CHARSET=utf8";
236: $db->send_query($query);
237: foreach ($this->sym_select as $sym_id => $degree) {
238: $sym_id_query_ar[] = $this->sym_rem_tbl . ".sym_id = $sym_id";
239: $query = "SELECT rem_id, MAX(grade) FROM " . $this->sym_rem_tbl . " WHERE sym_id = $sym_id GROUP BY sym_id, rem_id ORDER BY NULL";
240: $result = $db->send_query($query);
241: while(list($rem_id, $grade) = $db->db_fetch_row($result)) {
242: $points = $grade * $degree;
243: $query = "INSERT INTO result SET rem_id = $rem_id, points = $points, hits = 1 ON DUPLICATE KEY UPDATE points = points + $points, hits = hits + 1";
244: $db->send_query($query);
245: }
246: $db->free_result($result);
247: }
248: $sym_id_query = implode(" OR ", $sym_id_query_ar);
249: $query = "SELECT remedies.rem_short, " . $this->sym_rem_tbl . ".grade, " . $this->sym_rem_tbl . ".sym_id FROM " . $this->sym_rem_tbl . ", remedies WHERE " . $this->sym_rem_tbl . ".rem_id = remedies.rem_id AND ($sym_id_query) ORDER BY remedies.rem_short, " . $this->sym_rem_tbl . ".grade DESC, " . $this->sym_rem_tbl . ".sym_id ASC";
250: $db->send_query($query);
251: while($rel = $db->db_fetch_row()) {
252: $this->rel_ar[] = $rel;
253: }
254: $db->free_result();
255: $query = "SELECT result.points, result.hits, remedies.rem_short, remedies.rem_name, remedies.rem_id FROM result, remedies WHERE remedies.rem_id = result.rem_id ORDER BY result.points DESC, result.hits DESC, remedies.rem_short ASC";
256: $db->send_query($query);
257: $this->rel_count = 0;
258: $this->remedies_ar = array();
259: while($rem = $db->db_fetch_row()) {
260: $this->remedies_ar[] = $rem;
261: $this->rel_count += $rem[1];
262: }
263: $db->free_result();
264: $query = "SELECT COUNT(*) FROM result";
265: $db->send_query($query);
266: list($this->rem_count) = $db->db_fetch_row();
267: $db->free_result();
268: $query = "DROP TEMPORARY TABLE result";
269: $db->send_query($query);
270: $this->sym_count = count($this->sym_select);
271: }
272:
273: 274: 275: 276: 277: 278:
279: private function get_symptoms_ar() {
280: global $db, $session;
281: $query = "DROP TEMPORARY TABLE IF EXISTS sym_sort";
282: $db->send_query($query);
283: $query = "CREATE TEMPORARY TABLE sym_sort (
284: id mediumint(8) unsigned NOT NULL,
285: name varchar(510) NOT NULL,
286: degree tinyint(1) unsigned NOT NULL,
287: kuenzli tinyint(1) unsigned NOT NULL,
288: PRIMARY KEY(id)
289: ) ENGINE=MEMORY DEFAULT CHARSET=utf8";
290: $db->send_query($query);
291: foreach ($this->sym_select as $sym_id => $degree) {
292: $lang = $session->lang;
293: $query = "SELECT " . $this->symptoms_tbl . ".symptom, main_rubrics.rubric_$lang, sym_src.kuenzli FROM (" . $this->symptoms_tbl . ", main_rubrics) LEFT JOIN sym_src ON sym_src.sym_id = " . $this->symptoms_tbl . ".sym_id WHERE main_rubrics.rubric_id = " . $this->symptoms_tbl . ".rubric_id AND " . $this->symptoms_tbl . ".sym_id = $sym_id";
294: $db->send_query($query);
295: list ($symptom, $main_rubric, $kuenzli) = $db->db_fetch_row();
296: $db->free_result();
297: $full_name = $db->escape_string($main_rubric . " >> " . $symptom);
298: $kuenzli = (empty($kuenzli)) ? 0 : $kuenzli;
299: $query = "INSERT INTO sym_sort VALUES ($sym_id, '$full_name', $degree, $kuenzli)";
300: $db->send_query($query);
301: }
302: $query = "SELECT * FROM sym_sort ORDER BY degree DESC, name ASC";
303: $db->send_query($query);
304: for ($sym_ar = array(); $row = $db->db_fetch_assoc(); $sym_ar[array_shift($row)] = $row);
305: $db->free_result();
306: $query = "DROP TEMPORARY TABLE sym_sort";
307: $db->send_query($query);
308: $this->symptoms_ar = $sym_ar;
309: }
310:
311: 312: 313: 314: 315: 316: 317: 318:
319: function rep_result_table() {
320:
321: global $session, $db;
322:
323: foreach ($this->symptoms_ar as $sym_id => $symptom) {
324: foreach ($this->remedies_ar as $rem_ar) {
325: $row_ar[$sym_id][$rem_ar[2]] = "<td class='main_cols'></td>";
326: }
327: }
328: foreach ($this->remedies_ar as $rem_ar) {
329: foreach ($this->rel_ar as $symrem_ar) {
330: if($symrem_ar[0] == $rem_ar[2]){
331: $max_grade = 0;
332: $kuenzli = 0;
333: $sym_rem_src = $this->get_sym_rem_src($symrem_ar[2], $rem_ar[4], $max_grade, $kuenzli);
334: $rowtxt = $max_grade;
335: if ($kuenzli == 1) {
336: $rowtxt .= "<strong> •</strong>";
337: }
338: $rowtxt = "<td class='main_cols center' title='$sym_rem_src'><a href=\"javascript:popup_url('details.php?sym=$symrem_ar[2]&rem=$rem_ar[4]&sym_rem_tbl=" . $this->sym_rem_tbl . "',540,380)\">$rowtxt</a></td>";
339: $row_ar[$symrem_ar[2]][$rem_ar[2]] = $rowtxt;
340: }
341: }
342: }
343: $result_table = "";
344: $result_table .= " <table>\n";
345: $result_table .= " <tr>\n";
346: $result_table .= " <th class='deg_col'><div class='deg_col' title='" . _("Rubric degree") . "'>" . _("Deg.") . "</div></th>\n";
347: $result_table .= " <th class='symptom_col'><div class='symptom_col'>" . _("Symptoms") . "</div></th>\n";
348: foreach ($this->remedies_ar as $rem_ar) {
349: if (isset($_REQUEST['tab'])) {
350: $url = "javascript:tabOpen(\"materia.php?rem=\", $rem_ar[4], \"GET\", 2)";
351: } else {
352: $url = "materia.php?rem=$rem_ar[4]";
353: }
354: $result_table .= " <th class='main_cols'><a title='$rem_ar[3]' href='$url'>$rem_ar[2]</a></th>\n";
355: }
356: $result_table .= " </tr>\n";
357: $tr_results_class = 'tr_results_1';
358: $result_table .= " <tr class='$tr_results_class'>\n";
359: $result_table .= " <td></td>\n";
360: $result_table .= " <td><strong>" . _("Total (Grades/Hits)") . "</strong></td>\n";
361: foreach ($this->remedies_ar as $rem_ar) {
362: $result_table .= " <td class='main_cols center'><strong>$rem_ar[0]/$rem_ar[1]</strong></td>\n";
363: }
364: $result_table .= " </tr>\n";
365: foreach ($this->symptoms_ar as $sym_id => $symptom) {
366: if ($tr_results_class === 'tr_results_1') {
367: $tr_results_class = 'tr_results_2';
368: }
369: else {
370: $tr_results_class = 'tr_results_1';
371: }
372: if ($symptom['kuenzli'] == 1) {
373: $symptom['name'] .= " <strong>•</strong>";
374: }
375: if (isset($_REQUEST['tab'])) {
376: $url = "javascript:tabOpen(\"symptominfo.php?sym=\", $sym_id, \"GET\", 3)";
377: } else {
378: $url = "symptominfo.php?sym=$sym_id";
379: }
380: $result_table .= " <tr class='$tr_results_class'>\n";
381: $result_table .= " <td class='center' title='" . _("Rubric degree") . ": $symptom[degree]'><strong>$symptom[degree]</strong></td>\n";
382: $result_table .= " <td><a href='$url'>$symptom[name]</a></td>\n ";
383: $row = $row_ar[$sym_id];
384: $row = implode("\n ",$row);
385: $result_table .= "$row\n </tr>\n";
386: }
387: $result_table .= " </table>\n";
388: return $result_table;
389: }
390:
391: 392: 393: 394: 395: 396: 397: 398: 399: 400:
401: private function get_sym_rem_src($sym_id, $rem_id, &$max_grade, &$kuenzli_dot) {
402: global $db;
403: $sources = "";
404: $kuenzli_dot = 0;
405: $max_grade = $this->get_max_grade($sym_id, $rem_id);
406: $query = "SELECT sr.src_id, sr.grade, sr.rel_id, sr.kuenzli, ss.status_symbol FROM " . $this->sym_rem_tbl . " sr, sym_status ss WHERE sr.sym_id = $sym_id AND sr.rem_id = $rem_id AND ss.status_id = sr.status_id ORDER BY ss.status_grade DESC, sr.grade DESC, sr.src_id ASC";
407: $result = $db->send_query($query);
408: while (list($src_id, $grade, $rel_id, $kuenzli, $status_symbol) = $db->db_fetch_row($result)) {
409: $source = "$src_id";
410: if ($grade != $max_grade) {
411: $source .= "($grade" . _("-gr.") . ")";
412: }
413: $query = "SELECT src_id, nonclassic FROM sym_rem_refs WHERE rel_id = $rel_id ORDER BY nonclassic, src_id";
414: $db->send_query($query);
415: unset($ref_array);
416: while (list ($ref_id, $nonclassic) = $db->db_fetch_row()) {
417: $ref = "$ref_id";
418: if ($nonclassic == 1) {
419: $ref .= "_(nk)";
420: }
421: $ref_array[] = $ref;
422: }
423: $db->free_result();
424: if (!empty($ref_array)) {
425: $refs = implode(",", $ref_array);
426: $source .= ":$refs";
427: }
428: if (!empty($status_symbol)) {
429: $source .= $status_symbol;
430: }
431: if ($kuenzli == 1) {
432: $kuenzli_dot = 1;
433: }
434: $sources_ar[] = $source;
435: }
436: $db->free_result($result);
437: if (!empty($sources_ar)) {
438: $sources = implode(" / ", $sources_ar);
439: }
440: return $sources;
441: }
442:
443: 444: 445: 446: 447: 448: 449: 450:
451: function get_max_grade($sym_id, $rem_id) {
452: global $db;
453: $query = "SELECT MAX(grade) FROM " . $this->sym_rem_tbl . " WHERE sym_id = $sym_id AND rem_id = $rem_id";
454: $db->send_query($query);
455: list($max_grade) = $db->db_fetch_row();
456: $db->free_result();
457: return (empty($max_grade)) ? 0 : $max_grade;
458: }
459:
460: 461: 462: 463: 464: 465:
466: function save_rep() {
467: global $db;
468: $user = $_REQUEST['user'];
469: $patient = (empty($this->patient)) ? _("n.a.") : $this->patient;
470: if (!empty($this->rep_id)) {
471: $query = "UPDATE repertorizations SET patient_id = '" . $patient . "', rep_timestamp = FROM_UNIXTIME('" . $this->date_to_timestamp($this->date) . "'), rep_note = '" . $this->note . "', rep_prescription = '" . $this->prescription . "' WHERE rep_id = '" . $this->rep_id . "'";
472: $db->send_query($query);
473: } else {
474: $query = "INSERT INTO repertorizations (patient_id, rep_timestamp, rep_note, rep_prescription, sym_table, username) VALUES ('" . $patient . "', FROM_UNIXTIME('" . $this->date_to_timestamp($this->date) . "'), '" . $this->note . "', '" . $this->prescription . "', '" . $this->symptoms_tbl . "', '$user')";
475: $db->send_query($query);
476: $this->rep_id = $db->db_insert_id();
477: }
478: foreach ($this->sym_select as $sym_id => $degree) {
479: $query = "INSERT INTO rep_sym (rep_id, sym_id, degree) VALUES ({$this->rep_id}, $sym_id, $degree)";
480: $db->send_query($query);
481: }
482: }
483:
484: 485: 486: 487: 488: 489: 490:
491: function print_PDF($task) {
492: global $session;
493: if ($task == 'save_PDF') {
494: $dest = 'D';
495: } else {
496: $dest = 'I';
497: }
498: if($session->lang == 'de') {
499: $date = $this->date_to_german($this->date);
500: } else {
501: $date = $this->date;
502: }
503:
504: $pdf = new PDF('L');
505: $pdf->SetTopMargin(20);
506: $pdf->SetFont('Arial','',12);
507: $pdf->SetTitle(_("Repertorization result"), true);
508: $pdf->SetAuthor(_("OpenHomeopath"), true);
509: $pdf->SetCreator(_("openhomeo.org"), true);
510: $pdf->AddPage();
511: $w1 = $pdf->GetStringWidth(_('Patient:') . ' ' . _('Rep.-Date:') . ' ');
512: $w3 = $pdf->GetStringWidth(_('Case taking:') . ' ');
513: $w4 = $pdf->GetStringWidth(_('Prescription:') . ' ' . _('Rep.-No.:') . ' ');
514: $pdf->SetFont('', 'B');
515: $w2 = $pdf->GetStringWidth(iconv('UTF-8', 'windows-1252', $this->patient) . $date . $date);
516: $pdf->SetFont('', '');
517: $pdf->write(7, _('Patient:') . ' ');
518: $pdf->SetFont('', 'B');
519: $pdf->write(7, iconv('UTF-8', 'windows-1252', $this->patient));
520: $pdf->SetFont('', '');
521:
522: $pdf->Cell(295 - ($w1 + $w2));
523: $pdf->Cell(0, 7 , _('Rep.-Date:') . ' ');
524: $pdf->SetFont('', 'B');
525: $pdf->Cell(0, 7, $date, 0, 1, 'R');
526: $pdf->SetFont('', '');
527: $pdf->write(7, _('Prescription:') . ' ');
528: $pdf->SetFont('', 'B');
529: $pdf->write(7, iconv('UTF-8', 'windows-1252', $this->prescription));
530: if (!empty($this->rep_id)) {
531: $pdf->SetFont('', '');
532:
533: $pdf->Cell(295 -($w1 + $w2));
534: $pdf->Cell(0, 7, _('Rep.-No.:') . ' ');
535: $pdf->SetFont('', 'B');
536: $pdf->Cell(0, 7, $this->rep_id, 0, 1, 'R');
537: } else {
538: $pdf->Ln(7);
539: }
540: $pdf->SetFont('', '');
541: $pdf->write(7, _('Case taking:') . ' ');
542: $pdf->SetFont('', 'I', 11);
543: $pdf->Cell(0, 1, "", 0, 2);
544: $note_ar = explode("%br", $this->note);
545: foreach ($note_ar as $note_line) {
546: $pdf->Cell(0, 5, iconv('UTF-8', 'windows-1252', $note_line), 0, 2);
547: }
548:
549: $pdf->Ln(10);
550: $header_ar = array();
551: $first_row_ar = array();
552: $data_ar = array();
553: $this->get_table_data($header_ar, $first_row_ar, $data_ar, $summary);
554: $pdf->create_result_table($header_ar, $first_row_ar, $data_ar, 70);
555: $pdf->SetFont('Arial', '', 10);
556: $pdf->Ln(5);
557: $pdf->write(7, iconv('UTF-8', 'windows-1252', $summary));
558: $pdf->Output("OpenHomeopath_" . _("Repertorization result") . "_" . date("Y-m-d_H-i") . ".pdf", $dest);
559: }
560:
561: 562: 563: 564: 565: 566: 567:
568: function date_to_timestamp($date) {
569: return strtotime($date);
570: }
571:
572:
573: 574: 575: 576: 577: 578: 579:
580: function date_to_german($date) {
581: list($year, $month, $day) = explode("-", $date, 3);
582: return "$day.$month.$year";
583: }
584: 585: 586: 587: 588: 589: 590: 591: 592: 593:
594: private function get_table_data(&$header_ar, &$first_row_ar, &$data_ar, &$summary) {
595: global $db;
596: $limit = 20;
597: if (!empty($this->sym_select)) {
598: $remedies_ar = array_slice($this->remedies_ar, 0, $limit);
599: foreach ($this->symptoms_ar as $sym_id => $symptom) {
600: foreach ($remedies_ar as $rem_ar) {
601: $row_ar[$sym_id][$rem_ar[2]] = "";
602: }
603: }
604: foreach ($remedies_ar as $rem_ar) {
605: foreach ($this->rel_ar as $symrem_ar) {
606: if($symrem_ar[0] == $rem_ar[2]){
607: $max_grade = 0;
608: $kuenzli = 0;
609: $sym_rem_src = $this->get_sym_rem_src($symrem_ar[2], $rem_ar[4], $max_grade, $kuenzli);
610: $rowtxt = $max_grade;
611: if ($kuenzli == 1) {
612: $rowtxt .= " •";
613: }
614: $row_ar[$symrem_ar[2]][$rem_ar[2]] = $rowtxt;
615: }
616: }
617: }
618: $header_ar[] = iconv('UTF-8', 'windows-1252', _("Deg."));
619: $header_ar[] = iconv('UTF-8', 'windows-1252', _("Symptoms"));
620: foreach ($remedies_ar as $rem_ar) {
621: $header_ar[] = iconv('UTF-8', 'windows-1252', $rem_ar[2]);
622: }
623: $first_row_ar[] = "";
624: $first_row_ar[] = iconv('UTF-8', 'windows-1252', _("Total (Grades/Hits)"));
625: foreach ($remedies_ar as $rem_ar) {
626: $first_row_ar[] = "$rem_ar[0]/$rem_ar[1]";
627: }
628: $i = 0;
629: foreach ($this->symptoms_ar as $sym_id => $symptom) {
630: if ($symptom['kuenzli'] == 1) {
631: $symptom['name'] .= " •";
632: }
633: $data_ar[$i][] = $symptom['degree'];
634: $data_ar[$i][] = iconv('UTF-8', 'windows-1252', $symptom['name']);
635: foreach ($row_ar[$sym_id] as $row) {
636: $data_ar[$i][] = $row;
637: }
638: $i++;
639: }
640: }
641: $sym_txt = sprintf(ngettext("%d selected symptom", "%d selected symptoms", $this->sym_count), $this->sym_count);
642: $rem_txt = sprintf(ngettext("%d remedy", "%d remedies", $this->rem_count), $this->rem_count);
643: $rel_txt = sprintf(ngettext("%d symptom-remedy-relation", "%d symptom-remedy-relations", $this->rel_count), $this->rel_count);
644: $summary = sprintf(_("For %s there are %s and %s.") . "\n", $sym_txt, $rem_txt, $rel_txt);
645: if ($this->rem_count > $limit) {
646: $summary .= sprintf(_("The %d most important remedies were printed. ") . "\n", $limit);
647: }
648: }
649: }
650: