Tree::visitGroupToTreeObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
nc 1
nop 2
dl 0
loc 12
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
/**
3
 Copyright (C) 2018-2020 KANOUN Salim
4
 This program is free software; you can redistribute it and/or modify
5
 it under the terms of the Affero GNU General Public v.3 License as published by
6
 the Free Software Foundation;
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
 Affero GNU General Public Public for more details.
11
 You should have received a copy of the Affero GNU General Public Public along
12
 with this program; if not, write to the Free Software Foundation, Inc.,
13
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14
 */
15
16
/**
17
 * Build JSON for JSTree with patient's / visit's data
18
 *
19
 */
20
21
class Tree
22
{
23
  private $role;
24
  private $username;
25
  private $studyObject;
26
  private $linkpdo;
27
28
  public function __construct(string $role, string $username, string $study, PDO $linkpdo)
29
  {
30
	$this->linkpdo=$linkpdo;
31
	$this->role=$role;
32
	$this->username=$username;
33
	$this->studyObject=new Study($study, $linkpdo);
34
  }
35
36
  /**
37
   * Determine class value of Investigator and Controller visit item
38
   * to make specific color decoration depending on status of visit
39
   */
40
  private function determineClassOfVisit(Visit $visitObject): String
41
  {
42
43
	if ($this->role == User::INVESTIGATOR) {
44
45
	  //Add upload status / user form in class 
46
	  if ($visitObject->statusDone == Visit::DONE && $visitObject->uploadStatus == Visit::NOT_DONE && $visitObject->stateInvestigatorForm != Visit::DONE) {
47
		$class="NotBoth";
48
	  }else if ($visitObject->statusDone == Visit::DONE && $visitObject->stateInvestigatorForm != Visit::DONE) {
49
		$class="NotForm";
50
	  }else if ($visitObject->statusDone == Visit::DONE && $visitObject->uploadStatus == Visit::NOT_DONE) {
51
		$class="NotUpload";
52
	  }else {
53
		$class="OK";
54
	  }
55
	}else if ($this->role == User::CONTROLLER) {
56
	  if ($visitObject->stateQualityControl == Visit::QC_ACCEPTED || $visitObject->stateQualityControl == Visit::QC_REFUSED) {
57
		$class="OK";
58
	  }else if ($visitObject->stateQualityControl == Visit::QC_NOT_DONE || $visitObject->stateQualityControl == Visit::QC_WAIT_DEFINITVE_CONCLUSION) {
59
		$class="NotBoth";
60
	  }
61
	}else if ($this->role == User::REVIEWER) {
62
	  //Add status of review process (need to remove space from status string)
63
	  $class=str_replace(" ", "", $visitObject->reviewStatus); ;
64
	}
65
66
	return $class;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $class does not seem to be defined for all execution paths leading up to this point.
Loading history...
67
  }
68
69
  /**
70
   * Create visit entry in Tree from a visit Object
71
   */
72
  private function visitObjectToTreeObject(Visit $visitObject)
73
  {
74
75
	$jsonVisitLevel['id']=$visitObject->id_visit;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$jsonVisitLevel was never initialized. Although not strictly required by PHP, it is generally a good practice to add $jsonVisitLevel = array(); before regardless.
Loading history...
76
	$jsonVisitLevel['parent']=$visitObject->patientCode.'_'.$visitObject->visitGroupObject->groupModality;
77
	$jsonVisitLevel['icon']='/assets/images/report-icon.png';
78
	$jsonVisitLevel['text']=$visitObject->visitType;
79
	$jsonVisitLevel['level']='visit';
80
	$jsonVisitLevel['state']['opened']=false;
81
82
	if ($this->role == User::INVESTIGATOR || $this->role == User::CONTROLLER || $this->role == User::REVIEWER) {
83
	  //NB SI BESOIN ON PEUT AJOUTER UN CUSTOM ATRRIBUT A LA PLACE DE class
84
	  $attr['class']=$this->determineClassOfVisit($visitObject);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$attr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attr = array(); before regardless.
Loading history...
85
	  $jsonVisitLevel['li_attr']=$attr;
86
	}
87
88
	return $jsonVisitLevel;
89
  }
90
91
  /**
92
   * Create a patient entry in Tree
93
   */
94
  private function patientObjectToTreeObject(String $patientCode)
95
  {
96
97
	$jsonPatientLevel['id']=$patientCode;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$jsonPatientLevel was never initialized. Although not strictly required by PHP, it is generally a good practice to add $jsonPatientLevel = array(); before regardless.
Loading history...
98
	$jsonPatientLevel['parent']='#';
99
	$jsonPatientLevel['icon']='/assets/images/person-icon.png';
100
	$jsonPatientLevel['text']=$patientCode;
101
	$jsonPatientLevel['level']='patient';
102
	$jsonPatientLevel['state']['opened']=false;
103
104
	return $jsonPatientLevel;
105
  }
106
107
  /**
108
   * Create a Visit group entry in tree
109
   */
110
  private function visitGroupToTreeObject($patientCode, $groupModality)
111
  {
112
113
	$jsonGroupLevel['id']=$patientCode.'_'.$groupModality;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$jsonGroupLevel was never initialized. Although not strictly required by PHP, it is generally a good practice to add $jsonGroupLevel = array(); before regardless.
Loading history...
114
	$jsonGroupLevel['parent']=$patientCode;
115
	$jsonGroupLevel['icon']='/assets/images/person-icon.png';
116
	$jsonGroupLevel['text']=$groupModality;
117
	$jsonGroupLevel['level']='visit_group';
118
	$jsonGroupLevel['state']['opened']=true;
119
120
121
	return $jsonGroupLevel;
122
  }
123
124
  /**
125
   * sort Visits in key by modality
126
   */
127
  private function processVisitsArray($visitsArray)
128
  {
129
130
	$sortedModalities=[];
131
132
	foreach ($visitsArray as $visitObject) {
133
	  $sortedModalities[$visitObject->visitGroupObject->groupModality][]=$visitObject;
134
	}
135
136
	return $sortedModalities;
137
  }
138
139
  /**
140
   * Select visit from patients for some roles 
141
   */
142
  private function makeTreeFromPatients($patientsArray)
143
  {
144
145
	$resultTree=[];
146
	foreach ($patientsArray as $patientObject) {
147
	  //If investigator display all created visits
148
	  if ($this->role == User::INVESTIGATOR) $visitsArray=$patientObject->getAllCreatedPatientsVisits();
149
	  //if Reviewer display all QC accepted visits
150
	  if ($this->role == User::REVIEWER) $visitsArray=$patientObject->getAllQcDonePatientsVisits();
151
	  $stortedVisits=$this->processVisitsArray($visitsArray);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $visitsArray does not seem to be defined for all execution paths leading up to this point.
Loading history...
152
	  $resultTree[$patientObject->patientCode]['patientObject']=$patientObject;
153
	  $resultTree[$patientObject->patientCode]['modalities']=$stortedVisits;
154
	}
155
156
	return $resultTree;
157
  }
158
159
  /**
160
   * Sort an array of Visits by patient code
161
   */
162
  private function makeTreeFromVisits($visitsArray)
163
  {
164
165
	$resultTree=[];
166
167
	foreach ($visitsArray as $visitObject) {
168
	  $resultTree[$visitObject->patientCode]['patientObject']=$visitObject->getPatient();
169
	  $resultTree[$visitObject->patientCode]['modalities'][$visitObject->visitGroupObject->groupModality][]=$visitObject;
170
	}
171
172
	return $resultTree;
173
  }
174
175
  /**
176
   * Generate the final JSON tree by adding patient, modality and visit items
177
   */
178
  private function treeStructuretoJsonTree($treeStructure)
179
  {
180
	$jsonTree=[];
181
	foreach ($treeStructure as $patientCode => $patientData) {
182
	  $jsonTree[]=$this->patientObjectToTreeObject($patientCode);
183
	  foreach ($patientData['modalities'] as $modality => $visitObjects) {
184
		$jsonTree[]=$this->visitGroupToTreeObject($patientCode, $modality);
185
		foreach ($visitObjects as $visitObject) {
186
		  $jsonTree[]=$this->visitObjectToTreeObject($visitObject);
187
		}
188
	  }
189
	}
190
191
	return $jsonTree;
192
  }
193
194
  /**
195
   * Return JSON for JSTree according to role  (patient + Visit)
196
   * @return array
197
   */
198
  public function buildTree()
199
  {
200
201
	$possibleVisitGroups=$this->studyObject->getAllPossibleVisitGroups();
202
203
	if ($this->role == User::INVESTIGATOR) {
204
	  //retrieve from DB the patient's list of the requested study and included in user's center or affiliated centers
205
206
	  $patientObjectArray=$this->studyObject->getPatientsLinkedToUserCenters($this->username);
207
208
	  $treeStructure=$this->makeTreeFromPatients($patientObjectArray);
209
	}else if ($this->role == User::CONTROLLER) {
210
211
	  $controllerVisitsArray=[];
212
213
	  foreach ($possibleVisitGroups as $visitGroup) {
214
		$studyVisitManager=$visitGroup->getStudyVisitManager();
215
		$visitsArray=$studyVisitManager->getVisitForControllerAction();
216
		array_push($controllerVisitsArray, ...$visitsArray);
217
	  }
218
219
	  $treeStructure=$this->makeTreeFromVisits($controllerVisitsArray);
220
	}else if ($this->role == User::MONITOR) {
221
222
	  $monitorVisitsArray=[];
223
224
	  foreach ($possibleVisitGroups as $visitGroup) {
225
		$studyVisitManager=$visitGroup->getStudyVisitManager();
226
		$visitsArray=$studyVisitManager->getCreatedVisits();
227
		array_push($monitorVisitsArray, ...$visitsArray);
228
	  }
229
230
	  $treeStructure=$this->makeTreeFromVisits($monitorVisitsArray);
231
	}else if ($this->role == User::REVIEWER) {
232
	  //SK attention une review pending fait reafficher toutes les reviews du patient
233
	  //que soit la modalite
234
	  //peut etre jouer avec le job des users pour filtrer le group de visite
235
236
	  $patientsList=[];
237
	  //For each visit group list patient having a least on visit awaiting review
238
	  foreach ($possibleVisitGroups as $visitGroup) {
239
		$studyVisitManager=$visitGroup->getStudyVisitManager();
240
		$visits=$studyVisitManager->getAwaitingReviewVisit($this->username);
241
		if (!empty($visits)) {
242
		  foreach ($visits as $visitObject) {
243
			$patientsList[$visitObject->patientCode]=$visitObject->patientCode;
244
		  }
245
		}
246
	  }
247
	  //extract unique patient array
248
	  $patientCodeArray=array_keys($patientsList);
249
	  $patientObjectArray=[];
250
	  foreach ($patientCodeArray as $patientCode) {
251
		$patientObjectArray[]=new Patient($patientCode, $this->linkpdo);
252
	  }
253
254
	  $treeStructure=$this->makeTreeFromPatients($patientObjectArray);
255
256
	}
257
258
	$jsonTree=$this->treeStructuretoJsonTree($treeStructure);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $treeStructure does not seem to be defined for all execution paths leading up to this point.
Loading history...
259
260
	return  $jsonTree;
261
  }
262
}
263