Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. class surveyController extends mainController {
  3. function __construct() {
  4. parent::__construct();
  5. //To change for every Controller
  6. $this->viewDir = 'Survey';
  7. }
  8. public function beforeRender($content=null) {
  9. return false;
  10. }
  11. //Public (Servey form)
  12. /*public function index() {
  13. $this->view->survey_code = $this->getPost('code', 0);
  14. $survey_type = [];
  15. $this->view->survey_title = null;
  16. $this->view->survey_tag = null;
  17. $this->view->survey_type_id = 0;
  18. $this->view->structure = null;
  19. $this->view->survey_patient_string = '';
  20. $this->view->survey_answered = false;
  21. $this->view->survey_date = null;
  22. $survey = $this->db
  23. ->where('sy.code', $this->view->survey_code)
  24. ->join('survey_registry syr', 'syr.survey_id=sy.id')
  25. ->getOne('survey sy', "sy.type_id survey_type_id, sy.aswered_recipient, sy.aswered_date, syr.surname patient_surname, syr.name patient_name, syr.birthdate partient_bday, syr.email patient_email");
  26. if (isset($survey['survey_type_id'])) {
  27. $survey_type = $this->db->where('id', $survey['survey_type_id'])->where('status', 1)->getOne('survey_types');
  28. $this->view->survey_answered = $survey['aswered_recipient'] == 1 ? true : false;
  29. $this->view->survey_date = $survey['aswered_date'];
  30. $this->db->where('code', $this->view->survey_code)->update('survey', ['opened_recipient'=>1, 'updated_at'=>date('Y-m-d H:i:s')]);
  31. }
  32. //$this->view->debug = $survey;
  33. if (is_array($survey_type) && !empty($survey_type)) {
  34. $this->view->survey_type_id = $survey_type['type_id'];
  35. $this->view->survey_title = $survey_type['survey_label'];
  36. $this->view->survey_tag = $survey_type['survey_tag'];
  37. $survey_lang = $survey_type['survey_lang'];
  38. //$structure = json_decode(file_get_contents(RESOURCE_DIR.'survey/'.$survey_lang.'/'.$this->view->survey_tag.'.json'), true);
  39. //$global_fields = json_decode(file_get_contents(RESOURCE_DIR.'survey/'.$survey_lang.'/global.json'), true);
  40. $this->setSurveyForm($survey_lang, $this->view->survey_tag, $survey);
  41. //$this->view->structure = array_merge($structure, $global_fields);
  42. //$this->view->survey_patient_string = strip_tags($survey['patient_surname']).' '.strip_tags($survey['patient_name']).', '.$this->helper->getDateString($survey['partient_bday'], false).', '.$survey['patient_email'];
  43. }
  44. return $this->setJsonView('index');
  45. }
  46. //Private (managers)
  47. public function surveyPrivateSend() {
  48. $data = $this->getPost('data', null);
  49. return $this->setRawJsonResponse('ok', null, ['data'=>$data]);
  50. }
  51. //Public (people)
  52. public function surveyPublicSend() {
  53. $data = $this->getPost('data', null);
  54. $code = $this->getPost('survey_code', null);
  55. $files = $_FILES;
  56. $max_file_size = 1048576*5; //5 MB
  57. //Remove file information
  58. if (isset($data['file'])) unset($data['file']);
  59. //Remove privacy information (last item)
  60. array_pop($data);
  61. //Remove file group information
  62. array_pop($data);
  63. $insert_data = [
  64. 'json_answers'=>json_encode($data),
  65. 'aswered_recipient'=>1,
  66. 'aswered_date'=>date('Y-m-d H:i:s'),
  67. 'updated_at'=>date('Y-m-d H:i:s')
  68. ];
  69. $update = $this->db->where('code', $code)->update('survey', $insert_data);
  70. if ($update) {
  71. if (isset($files['data'])) {
  72. foreach($files['data']['name']['file'] as $index => $item) {
  73. if ((int)$files['data']['error']['file'][$index] == 0) {
  74. $file_name = $this->utility->slugify($files['data']['name']['file'][$index]);
  75. $file_type = $files['data']['type']['file'][$index];
  76. $file_tmp_name = $files['data']['tmp_name']['file'][$index];
  77. $file_ext = strtolower(pathinfo($files['data']['name']['file'][$index], PATHINFO_EXTENSION));
  78. $file_size = $files['data']['size']['file'][$index];
  79. $file_uuid = $this->getGUID();
  80. if ($file_size <= $max_file_size) {
  81. //Unique index on survey_uuid and file_title to avoid duplicate on multiple submits (i.g. poor connection)
  82. $insert = $this->db->insert('survey_attachments', [
  83. 'uuid'=>$file_uuid,
  84. 'survey_uuid'=>$code,
  85. 'file_title'=>$file_name,
  86. 'file_type'=>$file_type,
  87. 'file_ext'=>$file_ext,
  88. 'file_size'=>$file_size,
  89. 'created_at'=>date('Y-m-d H:i:s')
  90. ]);
  91. if ($insert) {
  92. $moved = move_uploaded_file($file_tmp_name, ATTACH_DIR.$file_uuid);
  93. //If not moved, try to delete the record
  94. if (!$moved) {
  95. $this->db->where('uuid', $file_uuid)->delete('survey_attachments');
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. return $this->setRawJsonResponse('ok', null);
  104. }*/
  105. public function surveyList() {
  106. if(!$this->checkPermissions([ADMIN_ROLE_ID, GLOBAL_MANAGER_ID, MANAGER_ID])) {
  107. return $this->redirect('login', 'permissionDenied');
  108. }
  109. $this->view->currentPage = $this->getPost('pageNumb', 1);
  110. $this->view->orderField = $this->getPost('orderField', 'created_at');
  111. $this->view->orderDir = $this->getPost('orderDir', 'desc');
  112. $this->view->isGlobal = $this->user->is([GLOBAL_MANAGER_ID]) ? true : false;
  113. $this->view->typeList = [];
  114. $searchData = $this->getPost('searchData', []);
  115. parse_str($searchData, $this->view->strOutput);
  116. //Get all users (Manager and Global manager) in current user centers
  117. $cc_list = [];
  118. $cc_user_list = [];
  119. $query_user_list = [];
  120. $cc = $this->db
  121. ->where('ucct.user_id', $this->user->getUserId())
  122. ->where('ucct.role_id IN('.MANAGER_ID.', '.GLOBAL_MANAGER_ID.')')
  123. ->get('users_clinical_centers_to ucct', null, ['ucct.center_id']);
  124. if (is_array($cc)) {
  125. foreach($cc as $item) {
  126. //Get unique array
  127. $cc_list[$item['center_id']] = $item['center_id'];
  128. }
  129. if (!empty($cc_list)) {
  130. $cc_user_list = $this->db
  131. ->where('ucct.center_id IN('.implode(',', $cc_list).')')
  132. ->where('ucct.role_id IN('.MANAGER_ID.')')
  133. ->get('users_clinical_centers_to ucct', null, ['ucct.user_id']);
  134. if (is_array($cc_user_list) && !empty($cc_user_list)) {
  135. foreach($cc_user_list as $item) {
  136. $query_user_list[] = $item['user_id'];
  137. }
  138. }
  139. }
  140. }
  141. $this->view->debugcc = $cc_list;
  142. $survey_types = null;
  143. if (is_array($cc_list) && !empty($cc_list)) {
  144. $survey_types = $this->db
  145. ->where('status', 1)
  146. ->where('center_id IN('.implode(',', $cc_list).')')
  147. ->orderBy('survey_label', 'ASC')
  148. ->get('survey_types');
  149. }
  150. if (is_array($survey_types) && !empty($survey_types)) {
  151. foreach($survey_types as $item) {
  152. $this->view->typeList[$item['id']] = '... '.str_ireplace('QUESTIONARIO ', '', $item['survey_label']);
  153. }
  154. }
  155. if ($this->user->is([GLOBAL_MANAGER_ID])) {
  156. if (!empty($query_user_list)) {
  157. $this->db->where('sy.manager_id IN('.implode(',', $query_user_list).')');
  158. } else {
  159. $this->db->where('sy.manager_id', $this->user->getUserId());
  160. }
  161. } else {
  162. $this->db->where('sy.manager_id', $this->user->getUserId());
  163. }
  164. $survey_type = 0;
  165. if (isset($this->view->strOutput['searchData']['survey_type'])) {
  166. $this->session->deleteSession('search_survey_type');
  167. $survey_type = (int)$this->view->strOutput['searchData']['survey_type'];
  168. $this->session->refreshSession('search_survey_type', $survey_type);
  169. } else {
  170. if ($this->session->getSessionValue('search_survey_type') !== false) {
  171. $survey_type = $this->session->getSessionValue('search_survey_type');
  172. }
  173. }
  174. if ((int)$survey_type > 0) {
  175. $this->db->where('sy.type_id', $survey_type);
  176. } else {
  177. $this->session->deleteSession('search_survey_type');
  178. }
  179. $survey_patient = '';
  180. if (isset($this->view->strOutput['searchData']['survey_patient'])) {
  181. $this->session->deleteSession('search_survey_patient');
  182. $survey_patient = $this->view->strOutput['searchData']['survey_patient'];
  183. $this->session->refreshSession('search_survey_patient', $survey_patient);
  184. } else {
  185. if ($this->session->getSessionValue('search_survey_patient') !== false) {
  186. $survey_patient = $this->session->getSessionValue('search_survey_patient');
  187. }
  188. }
  189. if (trim($survey_patient) != '') {
  190. //$this->db->where("CONCAT(sry.name, ' ', sry.name)", $survey_patient, 'LIKE');
  191. $patient_words = $this->utility->splitWords($survey_patient);
  192. $patient_subquery = [];
  193. if (is_array($patient_words)) {
  194. foreach($patient_words as $word) {
  195. $patient_subquery[] = "CONCAT(sry.name, ' ', sry.surname) LIKE '%$word%'";
  196. }
  197. if (!empty($patient_subquery)) {
  198. $patient_subquery = implode(' AND ', $patient_subquery);
  199. }
  200. }
  201. if (!is_array($patient_subquery)) {
  202. $this->db->where("(".$patient_subquery.")");
  203. }
  204. } else {
  205. $this->session->deleteSession('search_survey_patient');
  206. }
  207. $this->view->survey = $this->db
  208. ->join('survey_registry sry', 'sry.survey_id=sy.id')
  209. ->join('users u', 'u.id=sy.manager_id')
  210. ->join('survey_types sts', 'sts.id=sy.type_id')
  211. ->orderBy('sy.'.$this->view->orderField, $this->view->orderDir)
  212. ->paginate('survey sy', $this->view->currentPage, ['sy.*', 'sry.*', 'sy.id survey_id', 'sy.created_at survey_created_at', 'u.name manager_name', 'u.surname manager_surname', 'sts.survey_label survey_label', "(SELECT COUNT(*) FROM survey_attachments satt WHERE satt.survey_uuid LIKE sy.code) attach_count"]);
  213. if (is_array($this->view->survey) && !empty($this->view->survey)) {
  214. foreach($this->view->survey as $index => $item) {
  215. if (($item['manager_id'] == $this->user->getUserId()) || $this->user->is([ADMIN_ROLE_ID, GLOBAL_MANAGER_ID])) {
  216. $this->view->survey[$index]['extra_buttons'] = true;
  217. } else {
  218. $this->view->survey[$index]['extra_buttons'] = false;
  219. }
  220. }
  221. }
  222. $this->view->testt = $this->db->getLastQuery();
  223. $this->setPagination($this->db, $this->db->totalCount, $this->view->currentPage, $this->view->baseUri.'survey-list/'.time().'/'.$this->view->orderField.'/'.$this->view->orderDir);
  224. $this->actionTitle = _('Survey');
  225. return $this->setJsonView('surveyList');
  226. }
  227. public function surveyEdit() {
  228. if(!$this->checkPermissions([ADMIN_ROLE_ID, GLOBAL_MANAGER_ID, MANAGER_ID])) {
  229. return $this->redirect('login', 'permissionDenied');
  230. }
  231. $id = $this->getPost('id', 0);
  232. $this->view->id = $id;
  233. $op_label = $id == 0 ? _('New') : _('Edit');
  234. $this->view->debug = $data;
  235. //Manager clinical center
  236. $this->view->manager_cc = [];
  237. $cc = $this->db
  238. ->where('ucct.user_id', $this->user->getUserId())
  239. ->where('ucct.role_id', MANAGER_ID)
  240. ->join('clinical_centers cc', 'cc.id=ucct.center_id')
  241. ->getOne('users_clinical_centers_to ucct', 'ucct.center_id, cc.description');
  242. if (isset($cc['center_id'])) {
  243. $this->view->manager_cc = $cc;
  244. }
  245. $survey_types = $this->db->where('status', 1)->where('center_id', $cc['center_id'])->orderBy('survey_label', 'asc')->get('survey_types');
  246. $this->view->survey_type_list = [];
  247. if (is_array($survey_types)) {
  248. foreach($survey_types as $item) {
  249. $this->view->survey_type_list[$item['id']] = $item['survey_label'];
  250. }
  251. }
  252. $this->view->userData = $this->db
  253. ->where('sy.id', $id)
  254. ->join('survey_registry sry', 'sry.survey_id=sy.id', 'INNER')
  255. ->getOne('survey sy', 'sy.*, sry.*, sy.id survey_id');
  256. $this->actionTitle = _('Survey').' : '.$op_label;
  257. return $this->setJsonView('surveyEdit');
  258. }
  259. //Survey saved and sent by Manager
  260. public function surveySave() {
  261. if(!$this->checkPermissions([ADMIN_ROLE_ID, GLOBAL_MANAGER_ID, MANAGER_ID])) {
  262. return $this->redirect('login', 'permissionDenied');
  263. }
  264. $data = $this->getPost('data', null);
  265. $survey_id = $data['id']['value'];
  266. $center_id = (int)$data['center_id']['value'];
  267. /*$bpMin = (int)$data['request_bp_min']['value'];
  268. $bpMax = (int)$data['request_bp_max']['value'];
  269. $heartRate = (int)$data['request_heart_rate']['value'];
  270. $oxygenSaturation = (int)$data['request_oxy_sat']['value'];*/
  271. //Clinical center documents
  272. $documents = $this->db->where('center_id', $center_id)->get('clinical_center_documents');
  273. if ($center_id < 1) {
  274. return $this->setRawJsonResponse('err', "Impossibile inviare la scheda, il centro clinico dell'utente corrente non è valido.");
  275. }
  276. if (empty($documents)) {
  277. return $this->setRawJsonResponse('err', "Impossibile inviare la scheda, il centro clinico dell'utente corrente non è provvisto di documenti validi (testo e-mail per i pazienti, informativa sulla privacy, ecc.)");
  278. }
  279. if (trim($data['name']['value']) == '') {
  280. return $this->setRawJsonResponse('err', _('Please provide the Name'), ['class'=>$data['name']['class']]);
  281. }
  282. if (trim($data['surname']['value']) == '') {
  283. return $this->setRawJsonResponse('err', _('Please provide the Surname'), ['class'=>$data['surname']['class']]);
  284. }
  285. if (trim($data['birthdate']['value']) == '') {
  286. return $this->setRawJsonResponse('err', _('Birthdate is required'), ['class'=>$data['birthdate']['class']]);
  287. }
  288. if (trim($data['sex']['value']) == '') {
  289. return $this->setRawJsonResponse('err', _('Please provide the Sex'), ['class'=>$data['sex']['class']]);
  290. }
  291. if (strtotime($data['birthdate']['value']) > time()) {
  292. return $this->setRawJsonResponse('err', _('Birthdate cannot be in the future'), ['class'=>$data['birthdate']['class']]);
  293. }
  294. if (!$this->utility->validateEmail($data['email']['value'])) {
  295. return $this->setRawJsonResponse('err', _('E-mail is required and must be a valid address'), ['class'=>$data['email']['class']]);
  296. }
  297. if (trim($data['phone']['value']) == '') {
  298. return $this->setRawJsonResponse('err', _('Phone is required'), ['class'=>$data['phone']['class']]);
  299. }
  300. if ((int)$data['survey_type']['value'] == 0) {
  301. return $this->setRawJsonResponse('err', _('Survey types is required'), ['class'=>$data['survey_type']['class']]);
  302. }
  303. $survey_email = null;
  304. $email_subject = null;
  305. $survey_privacy = null;
  306. foreach($documents as $document) {
  307. switch($document['ducument_type']) {
  308. case 'recipient_mail':
  309. $survey_email = $document['document_file'];
  310. $email_subject = $document['document_subject'];
  311. break;
  312. case 'privacy':
  313. $survey_privacy = $document['document_file'];
  314. break;
  315. }
  316. }
  317. $email_file = RESOURCE_DIR.'survey/it/documents/centers/'.$center_id.'/'.$survey_email;
  318. $privacy_file = RESOURCE_DIR.'survey/it/documents/centers/'.$center_id.'/'.$survey_privacy;
  319. if (!file_exists($email_file) || !file_exists($privacy_file)) {
  320. return $this->setRawJsonResponse('err', "Impossibile inviare la scheda, i testi associati al centro clinico corrente non sono validi.");
  321. }
  322. /*if ($bpMax > 250) {
  323. return $this->setRawJsonResponse('err', _("Maximum Blood Pressure is too high."), ['class'=>$data['request_bp_max']['class']]);
  324. }
  325. if ($bpMin > 250) {
  326. return $this->setRawJsonResponse('err', _("Minimum Blood Pressure is too high."), ['class'=>$data['request_bp_min']['class']]);
  327. }
  328. if ($bpMin > 0 && $bpMax > 0) {
  329. if ($bpMin > $bpMax) {
  330. return $this->setRawJsonResponse('err', _("Minimum Blood Pressure cannot be higher than the Maximum one."));
  331. }
  332. }
  333. if ($heartRate > 250) {
  334. return $this->setRawJsonResponse('err', _("Heart Rate cannot be higher then 250 bpm."), ['class'=>$data['request_heart_rate']['class']]);
  335. }
  336. if ($oxygenSaturation > 100) {
  337. return $this->setRawJsonResponse('err', _("Oxygen Saturation cannot be higher than 100%."), ['class'=>$data['request_oxy_sat']['class']]);
  338. }*/
  339. $survey_code = strtoupper($this->getGUID());
  340. if ($survey_id == 0) {
  341. $survey_id = $this->db->insert('survey', [
  342. 'manager_id'=>$this->user->getUserId(),
  343. 'type_id'=>$data['survey_type']['value'],
  344. 'code'=>$survey_code,
  345. //'bp_min'=>$bpMin,
  346. //'bn_max'=>$bpMax,
  347. //'hrate'=>$heartRate,
  348. //'oxsat'=>$oxygenSaturation,
  349. 'notes'=>trim(strip_tags($data['request_medremarks']['value'])),
  350. 'updated_at'=>date('Y-m-d H:i:s'),
  351. 'created_at'=>date('Y-m-d H:i:s')
  352. ]);
  353. } else {
  354. $this->db->where('id', $survey_id)->update('survey', [
  355. 'manager_id'=>$this->user->getUserId(),
  356. 'type_id'=>$data['survey_type']['value'],
  357. 'email_failure'=>0,
  358. 'opened_recipient'=>0,
  359. 'aswered_recipient'=>0,
  360. 'remote_added'=>0,
  361. //'bp_min'=>$bpMin,
  362. //'bn_max'=>$bpMax,
  363. //'hrate'=>$heartRate,
  364. //'oxsat'=>$oxygenSaturation,
  365. 'notes'=>trim(strip_tags($data['request_medremarks']['value'])),
  366. 'updated_at'=>date('Y-m-d H:i:s')
  367. ]);
  368. $code_result = $this->db->where('id', $survey_id)->getOne('survey', 'code');
  369. if (isset($code_result['code'])) {
  370. $survey_code = $code_result['code'];
  371. }
  372. }
  373. $this->db->replace('survey_registry', [
  374. 'survey_id'=>$survey_id,
  375. 'name'=>trim($data['name']['value']),
  376. 'surname'=>trim($data['surname']['value']),
  377. 'sex'=>$data['sex']['value'],
  378. 'birthdate'=>$data['birthdate']['value'],
  379. 'email'=>strtolower($data['email']['value']),
  380. 'phone'=>trim($data['phone']['value']),
  381. 'created_at'=>date('Y-m-d H:i:s')
  382. ]);
  383. $survey_domain = $this->config['settings']['sportellocura']['domain'];
  384. $survey_link = $survey_domain.'#/survey/'.$survey_code;
  385. $string_body = file_get_contents($email_file);
  386. $tmp_body = vsprintf($string_body, [trim($data['name']['value']), trim($data['surname']['value']), $survey_link]);
  387. $html_mail_body = $this->partial('Message/Email/survey-template', ['body'=>$tmp_body, 'email_title'=>$email_subject]);
  388. $this->setMessageQueue($email_subject, $html_mail_body, $data['email']['value'], $survey_id);
  389. return $this->setRawJsonResponse('ok', "Scheda inviata correttamente.", ['log'=>[]], ['button'=>'goto', 'destination'=>'survey-list/'.time().'/created_at/desc/1']);
  390. }
  391. public function getSurveyEmptyForm() {
  392. $type_id = $this->getPost('survey_type_id', 0);
  393. $html = '';
  394. if ($type_id > 0) {
  395. $type = $this->db->where('id', $type_id)->getOne('survey_types');
  396. $this->setSurveyForm($type['survey_lang'], $type['survey_tag'], []);
  397. $html = $this->partial('Survey/form');
  398. }
  399. return $this->setRawJsonResponse('ok', '', ['html'=>$html]);
  400. }
  401. //Ajax for modal preview
  402. public function getSurveyAnswers() {
  403. $id = $this->getPost('survey_id', 0);
  404. $attachs = [];
  405. $survey_data = [];
  406. $html = null;
  407. $survey = $this->db
  408. ->join('survey_registry sr', 'sr.survey_id=s.id')
  409. ->where('s.id', $id)
  410. ->getOne('survey s', 's.code, s.bp_min, s.bn_max, s.hrate, s.oxsat, s.notes, s.json_answers, sr.name, sr.surname, sr.birthdate, sr.email, sr.phone');
  411. if (isset($survey['code'])) {
  412. $survey_data = json_decode($survey['json_answers'], true);
  413. $attachs = $this->db->where('survey_uuid', $survey['code'], 'LIKE')->get('survey_attachments');
  414. }
  415. $html = $this->partial('Survey/survey-list-preview', ['survey'=>$survey, 'data'=>$survey_data, 'attachs'=>$attachs]);
  416. return $this->setRawJsonResponse('ok', '', ['html'=>$html]);
  417. }
  418. //Ajax
  419. public function sendbackSurveyMessage() {
  420. if(!$this->checkPermissions([ADMIN_ROLE_ID, GLOBAL_MANAGER_ID, MANAGER_ID])) {
  421. return $this->setRawJsonResponse('err', _('Permission denied.'));
  422. }
  423. $survey_uuid = $this->getPost('survey_uuid', null);
  424. $recipient = $this->getPost('recipient', null);
  425. if (!is_null($survey_uuid)) {
  426. $update1 = $this->db
  427. ->where('code', $survey_uuid)
  428. ->update('survey', [
  429. 'json_answers'=>NULL,
  430. 'opened_recipient'=>0,
  431. 'aswered_recipient'=>0,
  432. 'aswered_date'=>NULL,
  433. 'remote_added'=>0,
  434. 'remote_added_date'=>NULL,
  435. 'remote_report_created'=>0,
  436. 'updated_at'=>date('Y-m-d H:i:s'),
  437. 'created_at'=>date('Y-m-d H:i:s')
  438. ]);
  439. if ($update1) {
  440. $update2 = $this->db
  441. ->where('msg_body', '%'.$survey_uuid.'%', 'LIKE')
  442. ->where('msg_sent', 1)
  443. ->update('survey_queue', [
  444. 'msg_sent'=>0,
  445. 'msg_sent_date'=>NULL
  446. ]);
  447. if ($update2) {
  448. //Log
  449. $this->db->insert('survey_sentback_log', [
  450. 'survey_uuid'=>$survey_uuid,
  451. 'recipient'=>$recipient,
  452. 'created_at'=>date('Y-m-d H:i:s')
  453. ]);
  454. return $this->setRawJsonResponse('ok', 'Scheda inviata correttamente.');
  455. } else {
  456. return $this->setRawJsonResponse('err', _('Update error (Cod. 2)'));
  457. }
  458. } else {
  459. return $this->setRawJsonResponse('err', _('Update error (Cod. 1)'));
  460. }
  461. } else {
  462. return $this->setRawJsonResponse('err', _('Data not valid'), []);
  463. }
  464. }
  465. //Ajax
  466. public function deleteSurvey() {
  467. if(!$this->checkPermissions([ADMIN_ROLE_ID, GLOBAL_MANAGER_ID, MANAGER_ID])) {
  468. return $this->setRawJsonResponse('err', _('Permission denied.'));
  469. }
  470. $survey_uuid = $this->getPost('survey_uuid', null);
  471. $survey_id = $this->getPost('survey_id', null);
  472. $attach = $this->db->where('survey_uuid', $survey_uuid)->get('survey_attachments');
  473. if (is_array($attach) && !empty($attach)) {
  474. foreach($attach as $item) {
  475. @unlink(ATTACH_DIR.$item['uuid']);
  476. $this->db->where('uuid', $item['uuid'])->delete('survey_attachments');
  477. }
  478. }
  479. $registry = $this->db->where('survey_id', $survey_id)->delete('survey_registry');
  480. $sentback_log = $this->db->where('survey_uuid', $survey_uuid)->delete('survey_sentback_log');
  481. if ($this->db->where('code', $survey_uuid)->delete('survey')) {
  482. return $this->setRawJsonResponse('ok', _('Survey successfully deleted'));
  483. } else {
  484. return $this->setRawJsonResponse('err', _('Unable to delete the survey'));
  485. }
  486. }
  487. private function setSurveyForm($survey_lang, $survey_tag, $survey=[]) {
  488. $structure = json_decode(file_get_contents(RESOURCE_DIR.'survey/'.$survey_lang.'/'.$survey_tag.'.json'), true);
  489. $global_fields = json_decode(file_get_contents(RESOURCE_DIR.'survey/'.$survey_lang.'/global.json'), true);
  490. $this->view->structure = array_merge($structure, $global_fields);
  491. if (is_array($survey) && !empty($survey)) {
  492. $this->view->survey_patient_string = strip_tags($survey['patient_surname']).' '.strip_tags($survey['patient_name']).', '.$this->helper->getDateString($survey['partient_bday'], false).', '.$survey['patient_email'];
  493. } else {
  494. $this->view->survey_patient_string = '';
  495. }
  496. }
  497. }