1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
Copyright (C) 2018 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
|
|
|
* Access Data of a review |
18
|
|
|
*/ |
19
|
|
|
Class Review{ |
20
|
|
|
|
21
|
|
|
private $linkpdo; |
22
|
|
|
public $id_visit; |
23
|
|
|
public $username; |
24
|
|
|
public $reviewDate; |
25
|
|
|
public $reviewDateObject; |
26
|
|
|
public $validated; |
27
|
|
|
public $isLocal; |
28
|
|
|
public $isAdjudication; |
29
|
|
|
public $deleted; |
30
|
|
|
public $id_review; |
31
|
|
|
public $associatedFiles; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function __construct($id_review, PDO $linkpdo){ |
35
|
|
|
$this->linkpdo=$linkpdo; |
36
|
|
|
$this->id_review=$id_review; |
37
|
|
|
$dataFetcher = $this->linkpdo->prepare('SELECT * FROM reviews WHERE id_review=:idReview'); |
38
|
|
|
$dataFetcher->execute(array( |
39
|
|
|
"idReview" => $id_review, |
40
|
|
|
)); |
41
|
|
|
$reviewData = $dataFetcher->fetch(PDO::FETCH_ASSOC); |
42
|
|
|
|
43
|
|
|
$this->id_visit=$reviewData['id_visit']; |
44
|
|
|
$this->username=$reviewData['username']; |
45
|
|
|
$this->reviewDate=$reviewData['review_date']; |
46
|
|
|
$this->reviewDateObject=new DateTimeImmutable($reviewData['review_date']); |
47
|
|
|
$this->validated=intval($reviewData['validated']); |
48
|
|
|
$this->isLocal=intval($reviewData['is_local']); |
49
|
|
|
$this->deleted=$reviewData['deleted']; |
50
|
|
|
$this->isAdjudication=$reviewData['is_adjudication']; |
51
|
|
|
//Store associated file as a php array |
52
|
|
|
$this->associatedFiles = json_decode($reviewData['sent_files'], true); |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return specific data as an associative array (database answer) |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function getSpecificData(){ |
62
|
|
|
|
63
|
|
|
$visitObject=$this->getParentVisitObject(); |
64
|
|
|
$visitCharacteristics=$visitObject->getVisitCharacteristics(); |
65
|
|
|
$dataFetcher = $this->linkpdo->prepare('SELECT * FROM '.$visitCharacteristics->tableReviewSpecificName.' WHERE id_review=:idReview'); |
66
|
|
|
$dataFetcher->execute(array( |
67
|
|
|
"idReview" => $this->id_review, |
68
|
|
|
)); |
69
|
|
|
$reviewData = $dataFetcher->fetch(PDO::FETCH_ASSOC); |
70
|
|
|
|
71
|
|
|
return $reviewData; |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return user who has made this review |
77
|
|
|
* @return User |
78
|
|
|
*/ |
79
|
|
|
public function getUserObject() : User { |
80
|
|
|
return new User($this->username, $this->linkpdo); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return parent visit Object |
85
|
|
|
* @return Visit |
86
|
|
|
*/ |
87
|
|
|
public function getParentVisitObject() : Visit { |
88
|
|
|
$visitsObject=new Visit($this->id_visit, $this->linkpdo); |
89
|
|
|
return $visitsObject; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set current review to deleted |
95
|
|
|
*/ |
96
|
|
|
public function deleteReview(){ |
97
|
|
|
|
98
|
|
|
$visitObject=$this->getParentVisitObject(); |
99
|
|
|
if($this->isLocal && ($visitObject->stateQualityControl == Visit::QC_ACCEPTED || $visitObject->stateQualityControl ==Visit::QC_REFUSED)) { |
100
|
|
|
throw new Exception("Can't delete Local form with QC done"); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$update = $this->linkpdo->prepare('UPDATE reviews SET deleted=1 WHERE id_review = :id_review'); |
104
|
|
|
$update->execute(array('id_review' => $this->id_review)); |
105
|
|
|
|
106
|
|
|
//If local form have beed destroyed, reset QC and mark investigator form Not Done |
107
|
|
|
if($this->isLocal){ |
108
|
|
|
$visitObject->resetQC(); |
109
|
|
|
$visitObject->changeVisitStateInvestigatorForm(Visit::LOCAL_FORM_NOT_DONE); |
110
|
|
|
}else{ |
111
|
|
|
$formProcessor=$visitObject->getFromProcessor($this->isLocal, $this->username); |
|
|
|
|
112
|
|
|
$formProcessor->setVisitValidation(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function updateReviewDate(){ |
119
|
|
|
$update = $this->linkpdo->prepare('UPDATE reviews SET review_date = :reviewdate WHERE id_review = :idReview'); |
120
|
|
|
$update->execute( array( 'reviewdate'=> date("Y-m-d H:i:s"), 'idReview' => $this->id_review ) ); |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function changeReviewValidationStatus(bool $validate){ |
125
|
|
|
|
126
|
|
|
$update = $this->linkpdo->prepare('UPDATE reviews SET validated = :validated WHERE id_review = :idReview'); |
127
|
|
|
$update->execute( array( 'validated'=> intval($validate), 'idReview' => $this->id_review ) ); |
128
|
|
|
|
129
|
|
|
if($validate){ |
130
|
|
|
$this->updateReviewDate(); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Update the file array column |
136
|
|
|
* File array should be an associative array following |
137
|
|
|
* key => filename |
138
|
|
|
* The fulll associated array should be sent |
139
|
|
|
*/ |
140
|
|
|
public function updateAssociatedFiles($fileArray){ |
141
|
|
|
|
142
|
|
|
$updateRequest = $this->linkpdo->prepare('UPDATE reviews |
143
|
|
|
SET sent_files = :sent_files |
144
|
|
|
WHERE id_review = :id_review'); |
145
|
|
|
|
146
|
|
|
$answer = $updateRequest->execute(array( 'id_review' => $this->id_review , |
147
|
|
|
'sent_files' => json_encode($fileArray) )); |
148
|
|
|
|
149
|
|
|
return $answer; |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function storeAssociatedFile($temporaryFile, $finalFilename){ |
154
|
|
|
$path = $this->getAssociatedFileRootPath(); |
155
|
|
|
if ( !is_dir($path) ) { |
156
|
|
|
mkdir($path, 0755, true); |
157
|
|
|
} |
158
|
|
|
//Copy file to finale destination with final name |
159
|
|
|
$result = move_uploaded_file($temporaryFile, $path.'/'.$finalFilename); |
160
|
|
|
|
161
|
|
|
if($result){ |
162
|
|
|
return $finalFilename; |
163
|
|
|
}else{ |
164
|
|
|
throw New Exception('Error writing associated File'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Return path where are stored the associated files |
172
|
|
|
*/ |
173
|
|
|
private function getAssociatedFileRootPath() : String { |
174
|
|
|
$path = $_SERVER['DOCUMENT_ROOT'] . '/data/upload/attached_review_file/'.$this->getParentVisitObject()->study.'/'.$this->id_review; |
175
|
|
|
return $path; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Return file destination of an associated file |
181
|
|
|
*/ |
182
|
|
|
public function getAssociatedFilePath(string $fileKey) : String { |
183
|
|
|
$fileArray = $this->associatedFiles; |
184
|
|
|
error_log($this->getAssociatedFileRootPath().'/'.$fileArray[$fileKey]); |
185
|
|
|
return $this->getAssociatedFileRootPath().'/'.$fileArray[$fileKey]; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function deleteAssociatedFile($fileKey) { |
189
|
|
|
|
190
|
|
|
if(! $this->validated){ |
191
|
|
|
unlink( $this->getAssociatedFilePath($fileKey) ); |
192
|
|
|
unset($this->associatedFiles[$fileKey]); |
193
|
|
|
$this->updateAssociatedFiles($this->associatedFiles); |
194
|
|
|
|
195
|
|
|
}else{ |
196
|
|
|
throw new Exception('Unavailable Key or validated Review, can\'t remove file'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* In case of failure of writing specific form. |
203
|
|
|
* Only used in form processor |
204
|
|
|
*/ |
205
|
|
|
public function hardDeleteReview() { |
206
|
|
|
|
207
|
|
|
$dbStatus = $this->linkpdo->prepare('DELETE FROM reviews WHERE id_review=:idReview'); |
208
|
|
|
$dbStatus->execute(array ('idReview'=> $this->id_review)); |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Unlock the current form |
214
|
|
|
* @throws Exception |
215
|
|
|
*/ |
216
|
|
|
public function unlockForm(){ |
217
|
|
|
|
218
|
|
|
$visitObject=$this->getParentVisitObject(); |
219
|
|
|
if($this->isLocal && ($visitObject->stateQualityControl == Visit::QC_ACCEPTED || $visitObject->stateQualityControl ==Visit::QC_REFUSED)) { |
220
|
|
|
throw new Exception("Can't Unlock Local form with QC done"); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
//Update review table |
224
|
|
|
$this->changeReviewValidationStatus(false); |
225
|
|
|
|
226
|
|
|
if($this->isLocal) { |
227
|
|
|
$visitObject->changeVisitStateInvestigatorForm(Visit::LOCAL_FORM_DRAFT); |
228
|
|
|
}else{ |
229
|
|
|
$formProcessor=$visitObject->getFromProcessor($this->isLocal, $this->username); |
|
|
|
|
230
|
|
|
$formProcessor->setVisitValidation(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/* |
237
|
|
|
* Create new entry in review table |
238
|
|
|
*/ |
239
|
|
|
public static function createReview(int $id_visit, string $username, bool $local, bool $adjudication, PDO $linkpdo) : Review { |
240
|
|
|
|
241
|
|
|
$newReview = $linkpdo->prepare('INSERT INTO reviews(id_visit, username, review_date, validated , is_local, is_adjudication, sent_files) VALUES (:idvisit, :username, :reviewdate, :validated, :local, :adjudication, :emptyFileArray)'); |
242
|
|
|
$newReview->execute(array( |
243
|
|
|
'idvisit' =>$id_visit, |
244
|
|
|
'username'=>$username, |
245
|
|
|
'reviewdate'=>date("Y-m-d H:i:s"), |
246
|
|
|
'validated'=> 0, |
247
|
|
|
'local'=>intval($local), |
248
|
|
|
'emptyFileArray'=>json_encode( array(), JSON_FORCE_OBJECT ), |
249
|
|
|
'adjudication'=>intval($adjudication) |
250
|
|
|
)); |
251
|
|
|
$idReview=$linkpdo->lastInsertId(); |
252
|
|
|
|
253
|
|
|
$reviewObject = new Review($idReview, $linkpdo); |
254
|
|
|
return $reviewObject; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
} |