You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mainController.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. class mainController extends Controller {
  3. public $userGroupId = 0;
  4. public $helper;
  5. public $notification;
  6. function __construct() {
  7. parent::__construct();
  8. $this->helper = new Helper($this->utility);
  9. $this->notification = new Notification($this, 0); //2 for debug
  10. $this->userGroupId = $this->user->getUserField('userGroupId');
  11. $groupInfoKey = 'group:info:'.$this->userGroupId;
  12. $groupInfoCache = $this->memoryCache->read($groupInfoKey);
  13. if (is_null($groupInfoCache)) {
  14. $this->groupInfo = $this->db->where('id', $this->userGroupId)->getOne('users_groups');
  15. $this->memoryCache->write($groupInfoKey, $this->groupInfo, '+10 years');
  16. } else {
  17. $this->groupInfo = $groupInfoCache;
  18. }
  19. $this->view->groupName = $this->groupInfo['group_name'];
  20. $this->view->groupId = $this->groupInfo['id'];
  21. $this->view->currentUserId = $this->user->getUserId();
  22. define('STATUS_TECH_NAME', _('Technician'));
  23. define('AGE_MONTH_LIMIT', 18);
  24. if($_SERVER['REMOTE_ADDR'] != '195.181.176.98') {
  25. //exit();
  26. }
  27. }
  28. public function beforeRender($content=null) {
  29. return $content;
  30. //TODO: check to groupId (?)
  31. //if ((int)$this->userGroupId < 1) {
  32. //}
  33. if (!$this->user->disclaimerAccepted()) {
  34. $this->viewDir = 'Main';
  35. return $this->setView('indexNoPrivacy');
  36. } else {
  37. return false;
  38. }
  39. }
  40. public function convertOldUserLang($checkLang='') {
  41. if (strlen($checkLang) == 2) return $checkLang;
  42. $map = ['ENGLISH'=>'en', 'ITALIANO'=>'it', 'PORTUGUES'=>'pt', 'FRANCAIS'=>'fr'];
  43. return isset($map[$checkLang]) ? $map[$checkLang] : $this->config['settings']['default-lang'];
  44. }
  45. public function getClinicalCenterCountries($groupByContinent=false) {
  46. $this->db->where('cc.group_id', $this->userGroupId)
  47. ->join('countries c', 'c.country_iso2_code=cc.country_code', 'INNER')
  48. ->join('continents cn', 'cn.code=cc.continent_code', 'INNER')
  49. ->groupBy('c.country_iso2_code');
  50. if ($groupByContinent) {
  51. $this->db
  52. ->orderBy('cn.name', 'asc')
  53. ->orderBy('c.country_name', 'asc');
  54. } else {
  55. $this->db->orderBy('c.country_name', 'asc');
  56. }
  57. $results = $this->db->get('clinical_centers cc', null, ['c.country_iso2_code country_code', 'c.country_name country_name', 'cn.code continent_code', 'cn.name continent_name', '(SELECT COUNT(*) AS total FROM clinical_centers WHERE clinical_centers.country_code=c.country_iso2_code AND clinical_centers.group_id='.$this->userGroupId.') cc_count', '(SELECT COUNT(*) AS total FROM clinical_centers WHERE clinical_centers.continent_code=cn.code AND clinical_centers.group_id='.$this->userGroupId.') cn_count']);
  58. if ($groupByContinent) {
  59. $grouped = [];
  60. if (is_array($results)) {
  61. foreach($results as $result) {
  62. $grouped[$result['continent_code']]['name'] = $result['continent_name'];
  63. $grouped[$result['continent_code']]['count'] = $result['cn_count'];
  64. $grouped[$result['continent_code']]['list'][] = $result;
  65. }
  66. }
  67. return $grouped;
  68. }
  69. return $results;
  70. }
  71. public function getUserClinicalCenters($userId, $roleId=0) {
  72. $reindexedResults = [];
  73. $key = 'clinical:center:'.$userId.':'.$roleId;
  74. $value = $this->memoryCache->read($key);
  75. $value = null; //Non usare la cache
  76. if (is_null($value)) {
  77. $results = $this->db
  78. ->where('ucct.user_id', $userId)
  79. ->where('ucct.role_id', $roleId)
  80. //->where('cc.group_id', $this->userGroupId)
  81. ->join('clinical_centers cc', 'cc.id=ucct.center_id', 'INNER')
  82. ->orderBy('cc.description', 'asc')
  83. ->get('users_clinical_centers_to ucct', null, ['cc.id', 'cc.description', 'cc.address', 'cc.anonymize', 'cc.has_remote_visit', 'cc.notes']);
  84. //Assign the Clinical Center id to the array index
  85. if (is_array($results)) {
  86. foreach($results as $result) {
  87. $reindexedResults[$result['id']] = $result;
  88. }
  89. }
  90. $this->memoryCache->write($key, $reindexedResults, '+10 years');
  91. } else {
  92. $reindexedResults = $value;
  93. }
  94. return $reindexedResults;
  95. }
  96. public function getUserMedicalSpecialties($userId=0) {
  97. $reindexedResults = [];
  98. $key = 'medical:specialties:'.$userId;
  99. $value = $this->memoryCache->read($key);
  100. if (is_null($value)) {
  101. $results = $this->db
  102. ->where('umst.user_id', $userId)
  103. ->join('users_medical_specialties ms', 'ms.id=umst.specialty_id', 'INNER')
  104. ->orderBy('ms.description', 'asc')
  105. ->get('users_medical_specialties_to umst', null, ['ms.id', 'ms.description']);
  106. //Assign the Medical Specialty id to the array index
  107. if (is_array($results)) {
  108. foreach($results as $result) {
  109. $reindexedResults[$result['id']] = $result;
  110. }
  111. }
  112. $this->memoryCache->write($key, $reindexedResults, '+10 years');
  113. } else {
  114. $reindexedResults = $value;
  115. }
  116. return $reindexedResults;
  117. }
  118. public function getActiveMedicalSpecialtiesIdByGroupId($groupId=0) {
  119. $specialties = [];
  120. $results = $this->db
  121. ->where('u.group_id', $groupId)
  122. ->where('u.status', 1)
  123. ->join('users u', 'u.id=umst.user_id', 'INNER')
  124. ->groupBy('umst.specialty_id')
  125. ->get('users_medical_specialties_to umst', null, ['umst.specialty_id']);
  126. if (is_array($results)) {
  127. foreach($results as $result) {
  128. $specialties[] = $result['specialty_id'];
  129. }
  130. }
  131. return $specialties;
  132. }
  133. public function getGUID(){
  134. if (function_exists('com_create_guid')){
  135. return com_create_guid();
  136. }
  137. else {
  138. mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
  139. $charid = strtoupper(md5(uniqid(rand(), true)));
  140. $hyphen = chr(45);// "-"
  141. $uuid =
  142. substr($charid, 0, 8).$hyphen.
  143. substr($charid, 8, 4).$hyphen.
  144. substr($charid,12, 4).$hyphen.
  145. substr($charid,16, 4).$hyphen.
  146. substr($charid,20,12);
  147. return $uuid;
  148. }
  149. }
  150. public function setMessageQueue($subject, $body, $recipient, $survey_id=0, $delay=null) {
  151. $this->db->insert('survey_queue', [
  152. 'survey_id'=>$survey_id,
  153. 'msg_subject'=>$subject,
  154. 'msg_body'=>$body,
  155. 'msg_recipient'=>$recipient,
  156. 'msg_delay_at'=>$delay
  157. ]);
  158. }
  159. public function canViewRemoteVisit() {
  160. $cc = [];
  161. if ($this->user->is(APPLICANT_ROLE_ID)) {
  162. $cc = $this->getUserClinicalCenters($this->user->getUserId(), APPLICANT_ROLE_ID);
  163. if (is_array($cc) && !empty($cc)) {
  164. foreach($cc as $item) {
  165. if ($item['has_remote_visit'] == 1) {
  166. return true;
  167. }
  168. }
  169. }
  170. }
  171. return false;
  172. }
  173. public function getSetting($key=null) {
  174. $value = $this->db->where('title', $key, 'LIKE')->getOne('settings');
  175. return isset($value[$key]) ? $value[$key] : false;
  176. }
  177. //TODO
  178. public function deleteUserCacheKeys($userId=0) {
  179. }
  180. }