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