-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathTimePoint.class.inc
More file actions
1439 lines (1296 loc) · 43.6 KB
/
TimePoint.class.inc
File metadata and controls
1439 lines (1296 loc) · 43.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php declare(strict_types=1);
/**
* This file contains a TimePoint class which represent's a single session
* for a single candidate in Loris.
*
* PHP Version 8
*
* @category Main
* @package Loris
* @author Alex Zijdenbos <zijdenbos@example.com>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris-Trunk/
*/
use \LORIS\StudyEntities\Candidate\CandID;
use \LORIS\Data\Filters\HasAnyPermissionOrUserSiteMatch;
/**
* The Loris TimePoint class
*
* @category Main
* @package Loris
* @author Alex Zijdenbos <zijdenbos@example.com>
* @license http://www.gnu.org/licenses/gpl-3.0.txt GPLv3
* @link https://www.github.com/aces/Loris-Trunk/
*/
class TimePoint implements \LORIS\StudyEntities\AccessibleResource,
\LORIS\StudyEntities\SiteHaver
{
/* NOTE If these are private variables then they should probably be
* declared and scoped as such. var is equivalent to public.
*/
var $_timePointInfo;
var $_stageOptions = [
null,
'Not Started',
'Screening',
'Visit',
'Approval',
'Subject',
'Recycling Bin',
];
var $_dynamicStageOptions = [
'Screening',
'Visit',
'Approval',
];
var $_statusOptions = [
null,
'Pass',
'Failure',
'Withdrawal',
'In Progress',
];
var $bvlQcTypes = [
null,
'Visual',
'Hardcopy',
];
var $bvlQcStatuses = [
null,
'Complete',
];
var $bvlQcExclusionStatuses = [
null,
'Excluded',
'Not Excluded',
];
/**
* A DTO for TimePoint related data
*/
protected ?TimePointData $data;
/**
* Construct a TimePoint object seeded with the data
* from the given DTO.
*
* @param ?TimePointData $data The Timepoint DTO
*/
public function __construct(?TimePointData $data=null)
{
$this->data = $data;
}
/**
* Returns an object representing this Timepoint, and ensures that only
* one is ever created per Session.
*
* @param SessionID $sessionID The ID of the TimePoint to retrieve from the
* database
*
* @return TimePoint object representing $sessionID
*/
static function &singleton(SessionID $sessionID)
{
// instantiate new TimePoint object
static $timePointList = [];
if (!isset($timePointList["$sessionID"])) {
try {
$timePointList["$sessionID"] = new TimePoint();
$timePointList["$sessionID"]->select($sessionID);
} catch (Exception $e) {
// Since there was a problem with this $sessionID, don't store
// it in the static list but then keep propagating the error..
unset($timePointList["$sessionID"]);
throw $e;
}
}
return $timePointList["$sessionID"];
}
/**
* Loads the data from the given sessionID into the current TimePoint class.
*
* @param SessionID $sessionID The $sessionID to load.
*
* @return void (but side-effect loads current object.)
*/
function select(SessionID $sessionID): void
{
$factory = NDB_Factory::singleton();
$db = $factory->database();
$config = $factory->config();
$useScreening = $config->getSetting('useScreening');
$screeningIndex = array_search('Screening', $this->_stageOptions);
if ($useScreening != 'true' && $screeningIndex !== false) {
unset($this->_stageOptions[$screeningIndex]);
$this->_stageOptions = array_values($this->_stageOptions);
}
// get user data from database
$query = "SELECT s.ID AS SessionID, c.CandID, p.Name AS PSC, s.CenterID,
s.VisitNo, s.Visit_label, s.CohortID, s.Submitted,
s.Current_stage, s.Screening, s.Date_screening, s.Visit,
s.Date_visit, s.Approval, s.Date_approval, s.Active,
s.registeredBy, s.UserID, u.Real_name, s.Hardcopy_request,
s.BVLQCStatus, s.BVLQCType, s.BVLQCExclusion, s.Scan_done,
pr.Name as ProjectName, p.Alias as SiteAlias,
s.ProjectID as ProjectID,
l.language_code as LanguageCode,
l.language_label as LanguageLabel
FROM session AS s
LEFT JOIN psc AS p USING (CenterID)
LEFT JOIN users AS u on (s.registeredBy=u.UserID)
LEFT JOIN Project AS pr USING(ProjectID)
LEFT JOIN candidate AS c ON (s.CandidateID=c.ID)
LEFT JOIN language l ON (s.languageID=l.language_id)
WHERE s.ID = :SID AND s.Active = 'Y'";
$row = $db->pselectRow(
$query,
['SID' => $sessionID]
);
$this->data = new TimePointData(
$sessionID,
isset($row['ProjectID'])
? ProjectID::singleton($row['ProjectID'])
: null,
isset($row['CenterID'])
? \CenterID::singleton($row['CenterID'])
: null,
);
// store user data in object property
if (is_array($row) && count($row) > 0) {
$this->_timePointInfo = $row;
if (isset($row['LanguageCode'])) {
$this->_timePointInfo['Language'] = new \Language(
$row['LanguageLabel'],
$row['LanguageCode']
);
}
unset($row['language_code'], $row['language_label']);
$cohortSettings = $config->getCohortSettings(
intval($row['CohortID'] ?? null)
);
$title = $cohortSettings['title'] ?? '';
$this->_timePointInfo['CohortTitle'] = $title;
$this->_timePointInfo['CandID']
= new CandID(strval($row['CandID']));
} else {
// return error when 0 rows to prevent creation of an empty object
throw new LorisException(
"Failed to retrieve data for timepoint $sessionID"
);
}
// New feature, older databases might not have the table created,
// so require a config option to be set
if ($config->getSetting("SupplementalSessionStatus") == 'true') {
$query = "SELECT ss.Name, ss.Value
FROM session_status ss
JOIN session s ON (s.ID=ss.SessionID)
WHERE s.ID=:sessionID AND s.Active='Y'";
$statuses = $db->pselect($query, [":sessionID" => $sessionID]);
foreach ($statuses as $row) {
if (!isset($this->_timePointInfo['status'])) {
$this->_timePointInfo['status'] = [];
}
$this->_timePointInfo['status'][$row['Name']] = $row['Value'];
}
}
$this->_timePointInfo['WindowInfo'] = $db->pSelectRow(
"SELECT (w.WindowMinDays IS NOT NULL
OR w.OptimumMinDays IS NOT NULL)
as WindowDefined,
DATEDIFF(s.Date_visit, c.DoB) as AgeDays,
DATEDIFF(s.Date_visit, c.DoB) BETWEEN
w.OptimumMinDays
AND w.OptimumMaxDays as Optimum,
(DATEDIFF(s.Date_visit, c.DoB) BETWEEN
w.WindowMinDays AND w.WindowMaxDays
) as Permitted
FROM session s
JOIN candidate c ON c.ID=s.CandidateID
JOIN Visit_Windows w ON (
LOWER(w.Visit_label)=LOWER(s.Visit_label)
)
WHERE s.ID=:sessionID",
[":sessionID" => $sessionID]
);
}
/**
* Registers a new timepoint into the session table
*
* @param \Candidate $candidate The Candidate to create this timepoint for
* @param integer $CohortID The cohortID of the new timepoint
* @param string|null $nextVisitLabel The visit label of the timepoint
* being created. If null,
* consecutive V1, V2, etc will be
* created.
* @param \Site $psc The center of the timepoint
* @param \Project $project The project of the timepoint
* @param integer|null $lang The language id
*
* @throws DatabaseException
*
* @return void
*/
static function createNew(
\Candidate $candidate,
int $CohortID,
?string $nextVisitLabel,
\Site $psc,
\Project $project,
?int $lang = null
): void {
// insert into session set CandID=$candID,
// CohortID=$CohortID, VisitNo=nextVisitNumber(),
// UserID=$State->getUsername(), etc... NO Date_* are
// specified here!
$factory = NDB_Factory::singleton();
$db = $factory->database();
// if this is a web hit (thus state exists)
if (isset($_SESSION) && is_object($_SESSION['State'])) {
// get username from State
$userID = $_SESSION['State']->getUsername();
} else {
// not a web hit, so use the unix username from the environment
$userID = getenv('USER');
}
// generate today's date for the Date registered field
$today = date("Y-m-d");
$visitNo = $candidate->getNextVisitNo();
// setup the insert data array
$VisitLabel = $nextVisitLabel;
if ($VisitLabel === null) {
$VisitLabel = "V$visitNo";
}
$language = $lang;
if ($language === null) {
$langList = Utility::getLanguageList();
$language = array_key_first($langList);
}
$insertData = [
'CandidateID' => $candidate->getID(),
'CohortID' => $CohortID,
'VisitNo' => $visitNo,
'Visit_label' => $VisitLabel,
'CenterID' => $psc->getCenterID()->__toString(),
'ProjectID' => $project->getId(),
'Current_stage' => 'Not Started',
'Submitted' => 'N',
'registeredBy' => $userID,
'UserID' => $userID,
'Date_registered' => $today,
'Date_active' => $today,
'languageID' => $language,
];
// insert the data
$db->insert('session', $insertData);
}
/**
* Validates if a given visit_label follows the study's config settings
* and if that same visit label can be created for the given candidate.
*
* @param CandID $candid The candidate identifier
* @param \ProjectID $projectID The Project identifier
* @param integer $cohortID The cohortID
* @param string $visitName The visit name
*
* @throws LorisException
*
* @return void
*/
static function isValidVisitLabel(
CandID $candid,
\ProjectID $projectID,
int $cohortID,
string $visitName
): void {
// This can happen if the user quickly clicks "Create Time Point" before the
// page has loaded and the Visit Label dropdown hasn't been selected yet.
// The page will create "V1" when this is the case without this check.
if (empty($visitName)) {
throw new \LorisException(
'A visit label is required.'
);
}
// Check if visit label exists for this cohort
if (!array_key_exists(
$visitName,
\Utility::getVisitsForCohort(
$cohortID
)
)
) {
throw new \LorisException(
'This visit label is not valid for this cohort.'
);
}
$candidate =& \Candidate::singleton($candid);
// The function below gets a list of VisitNames from the DB, not real labels
$timePointArray = $candidate->getListOfVisitLabels();
//If the visitLabel is already in use then let the user pick another
foreach ($timePointArray AS $used_name) {
if (strcasecmp($visitName, $used_name) == 0) {
// Get project name
$project = \Project::getProjectFromID($projectID);
$projectName = $project->getName();
// Get cohort title
$factory = \NDB_Factory::singleton();
$config = $factory->config();
$cohortSettings = $config->getCohortSettings($cohortID);
$cohortTitle = $cohortSettings['title'] ?? 'Unknown';
// Get participant PSCID
$pscid = $candidate->getPSCID();
$message = sprintf(
dgettext(
"create_timepoint",
"A timepoint with these conditions (%s, %s, %s, %s) "
. "already exists."
),
$pscid,
$projectName,
$cohortTitle,
$visitName
);
throw new \LorisException($message);
}
}
}
/**
* Gets a piece of data regarding this TimePoint. Usually, you should
* use one of the get* methods below, but in cases where it does not
* exist you can use this function directly.
*
* @param string|null $var The variable to retrieve about this timepoint.
*
* @return mixed The value of $var for this timepoint, or all values.
*/
function getData(?string $var = null)
{
if (is_null($var)) {
return $this->_timePointInfo;
} elseif (isset($this->_timePointInfo[$var])) {
return $this->_timePointInfo[$var];
}
return null;
}
/**
* Sets a piece of data for this timepoint and saves it to the session table.
* $newData should be in the form: array($key => $value).
*
* This should usually not be used directly but instead use one of the set*
* methods below. However, there are cases where it may be easier to directly
* use setData.
*
* @param array $newData Key-value pair(s) of the field(s) to change and its
* new value(s).
*
* @return bool
*
* @throws DatabaseException
* @throws LorisException
*/
public function setData(array $newData): bool
{
/* $newData should be in the form: array($key => $value). Count should
* equal 1 if this is the case.
*/
if (empty($newData)) {
throw new LorisException(
'TimePoint::setData() cannot be called with an empty array!'
);
}
\NDB_Factory::singleton()->database()->update(
'session',
$newData,
[
'ID' => $this->getData('SessionID'),
]
);
// this select updates the timepoint object
$this->select($this->getSessionID());
return true;
}
/**
* Returns the CandID for the candidate who had this visit.
*
* @return CandID (6-10 digit) CandID for this timepoint.
*/
function getCandID(): CandID
{
/* _timePointInfo contains information that comes from the database
* which returns values in string format. CandID is used here to
* ensure we return a validated CandID object.
*/
return $this->_timePointInfo["CandID"];
}
/**
* Returns the SessionID for this visit.
*
* @return SessionID The ID for this session.
*/
function getSessionID(): SessionID
{
return $this->data->getSessionID();
}
/**
* Return the string representing the center for this visit.
*
* @return string The center of this visit
*/
function getPSC(): string
{
return $this->_timePointInfo["PSC"];
}
/**
* Returns the CenterID for this visit.
*
* @return \CenterID
*/
function getCenterID(): \CenterID
{
return $this->data->getCenterID();
}
/**
* Return the visit number for this timepoint.
*
* @return integer the number of this visit.
*/
function getVisitNo(): int
{
/* _timePointInfo contains information that comes from the database
* which returns values in string format. Intval is used here to
* ensure we return a numerical ID instead of its string representation.
*/
return intval($this->_timePointInfo["VisitNo"]);
}
/**
* Returns the visit label of this timepoint.
*
* @return string the visit label
*/
function getVisitLabel(): string
{
return $this->_timePointInfo["Visit_label"];
}
/**
* Returns the CohortID of this timepoint.
*
* @return int|null the CohortID, or null
*/
function getCohortID(): ?int
{
/* _timePointInfo contains information that comes from the database
* which returns values in string format. Intval is used here to
* ensure we return a numerical ID instead of its string representation.
*/
if (is_numeric($this->_timePointInfo["CohortID"])) {
return intval($this->_timePointInfo["CohortID"]);
}
return null;
}
/**
* Gets this timepoint's screening status.
*
* @return ?string 'Pass', 'Failure', 'Withdrawal', or 'In Progress'
*/
function getScreeningStatus(): ?string
{
return $this->_timePointInfo["Screening"];
}
/**
* Gets the visit status of this timepoint.
*
* @return ?string 'Pass', 'Failure', 'Withdrawal', or 'In Progress'
*/
function getVisitStatus(): ?string
{
return $this->_timePointInfo["Visit"];
}
/**
* Return the approval status of this timepoint.
*
* @return string|null 'In Progress', 'Pass' or 'Failure'
*/
function getApprovalStatus(): ?string
{
return $this->_timePointInfo["Approval"];
}
/**
* Returns the date of screening for this session.
*
* @return string|null of form "YYYY-MM-DD" (or null)
*/
function getDateOfScreening(): ?string
{
return $this->_timePointInfo["Date_screening"];
}
/**
* Returns the date of visit for this session.
*
* @return string|null of form "YYYY-MM-DD" (or null)
*/
function getDateOfVisit(): ?string
{
return $this->_timePointInfo["Date_visit"];
}
/**
* Returns the date of approval of this session.
*
* @return string|null of form "YYYY-MM-DD" (or null)
*/
function getDateOfApproval(): ?string
{
return $this->_timePointInfo["Date_approval"];
}
/**
* Returns whether this timepoint was submitted to DCC
*
* @return true if submitted
*/
function isSubmitted(): bool
{
return $this->_timePointInfo["Submitted"] == 'Y';
}
/**
* Returns whether this timepoint is active or not.
*
* @return boolean true if active.
*/
function isActive(): bool
{
return $this->_timePointInfo["Active"] == 'Y';
}
/**
* Gets whether or not a scan was done for this TimePoint
*
* @return string 'Y' if a scan was done 'N' if not.
*/
function getScanDone(): string
{
return $this->_timePointInfo["Scan_done"];
}
/**
* Returns BVLQC status (Y-hardcopy QC, N-no QC, V-visual QC)
*
* @return string|null
*/
function getBVLQCStatus(): ?string
{
return $this->_timePointInfo['BVLQCStatus'];
}
/**
* Returns BVLQC exclusion type
*
* @return string|null
*/
function getBVLQCType(): ?string
{
return $this->_timePointInfo['BVLQCType'];
}
/**
* Returns BVLQC exclusion status
*
* @return string
*/
function getBVLQCExclusion(): string
{
return $this->_timePointInfo['BVLQCExclusion'];
}
/**
* Returns the list of BVLQC exclusion statuses
*
* @return array
*/
function getBVLQCExclusionStatusList(): array
{
return $this->bvlQcExclusionStatuses;
}
/**
* Returns the current stage of the selected timepoint
*
* @return string the current stage
*/
function getCurrentStage(): string
{
return $this->_timePointInfo["Current_stage"];
}
/**
* Gets the current stage status
*
* @return string|null status of current stage
*/
function getCurrentStatus(): ?string
{
$method = 'get' . $this->getCurrentStage() . 'Status';
if (method_exists($this, $method)) {
return $this->$method();
} else {
return null;
}
}
/**
* Gets the date of the current stage.
*
* @return string|null "YYYY-MM-DD"
*/
function getCurrentDate(): ?string
{
$getDateMethod = "getDateOf" . $this->getCurrentStage();
if (method_exists($this, $getDateMethod)) {
return $this->$getDateMethod();
}
return null;
}
/**
* Defines the next study stage
*
* @return string|null study stage title
*/
function getNextStage(): ?string
{
if ($this->getData('Current_stage') == 'Approval'
&& $this->getData('Approval') != 'Pass'
) {
// if the time point is sent to DCC and the outcome is not Pass
// then the next stage is Recycling Bin
return 'Recycling Bin';
}
// otherwise just get the next stage in the sequence
$index = array_search(
$this->getData('Current_stage'),
$this->_stageOptions
);
$nextStageIndex = $index + 1;
return $this->_stageOptions[$nextStageIndex];
}
/**
* Defines a stage as dynamic - that can be assigned a BVL
* battery, its status updated, and sent to DCC
*
* @param string $stage A study stage, from the field session.Current_stage
*
* @return bool
*/
function isStudyStageDynamic(string $stage): bool
{
// if not $stage is passed, evaluate the current stage
if (empty($stage)) {
throw new Exception("Study Stage Not Specified");
} else {
return in_array($stage, $this->_dynamicStageOptions);
}
}
/**
* Determine whether the current stage is static or dynamic.
*
* @return boolean true if stage is dynamic.
*/
function isCurrentStageDynamic()
{
$isStageDynamic = $this->isStudyStageDynamic($this->getCurrentStage());
return $isStageDynamic;
}
/**
* Determine whether the next stage is dynamic or static
*
* @return boolean true if stage is dynamic.
*/
function isNextStageDynamic(): bool
{
return $this->isStudyStageDynamic($this->getNextStage());
}
/**
* Derives what stage the TimePoint should be set to and updates
* the session table.
*
* @return void
*/
function setCurrentStage(): void
{
$db = \NDB_Factory::singleton()->database();
// this class does not handle Subject & Recycling Bin stage at the moment
if (in_array($this->getCurrentStage(), ['Subject', 'Recycling Bin'])) {
return;
}
// if sent to DCC then it either Approval, Subject or Recycling Bin
if ($this->isSubmitted() && $this->getApprovalStatus() != 'NULL') {
if ($this->getApprovalStatus() == 'Failure') {
$newCurrentStage = 'Recycling Bin';
// delete from conflicts_unresolved if marked Recycling Bin
$commentIDs = $db->pselect(
"SELECT CommentID FROM flag WHERE SessionID=:SID",
['SID' => $this->getSessionID()]
);
foreach ($commentIDs as $commentID) {
$deleteWhere1 = ['CommentId1' => $commentID];
$deleteWhere2 = ['CommentId2' => $commentID];
$db->delete('conflicts_unresolved', $deleteWhere1);
$db->delete('conflicts_unresolved', $deleteWhere2);
}
} else {
// in all other cases
$newCurrentStage = 'Approval';
}
} else {
// of not sent to DCC get the latest stage
if ($this->getVisitStatus() != null) {
$newCurrentStage = 'Visit';
} elseif ($this->getScreeningStatus() != null) {
$newCurrentStage = 'Screening';
} else {
$newCurrentStage = 'Not Started';
}
}
// generate today's date in MySQL format
$today = date("Y-m-d");
// update session table
$update_columns = [
"Date_stage_change" => $today,
'Current_stage' => $newCurrentStage,
];
$db->update(
'session',
$update_columns,
['ID' => $this->getSessionID()]
);
// reload TimePoint object
$this->select($this->getSessionID());
}
/**
* Starts a stage.
*
* @param string $stage The stage which is being started.
* valid options are those in session.Current_stage
* enum
*
* @return void (but as a side-effect modifies database.)
*/
function startStage(string $stage): void
{
$setArray = [
'Current_stage' => $stage,
'Date_stage_change' => date('Y-m-d'),
];
if (in_array($stage, $this->_dynamicStageOptions)
&& $this->getData($stage) == null
) {
$setArray[$stage] = 'In Progress';
$setArray['Date_status_change'] = date('Y-m-d');
}
if ($setArray['Current_stage'] == 'Approval') {
$setArray['Date_approval'] = date('Y-m-d');
}
$this->setData($setArray);
}
/**
* Sends Time Point to DCC
* also allow "unsend", i.e. sets session.Submit='N' in case it ever
* becomes necessary
*
* @param string $type see session.QCd field options, what is passed
* here is the label from the bvlQcTypes array
*
* @return void
*/
function setBVLQCType(string $type): void
{
$db = \NDB_Factory::singleton()->database();
if (!in_array($type, $this->bvlQcTypes)) {
throw new Exception("You need to specify a valid BVL QC Type");
}
$setArray = ['BVLQCType' => $type];
// update session table
$db->update(
'session',
$setArray,
['ID' => $this->getSessionID()]
);
// reload TimePoint object
$this->select($this->getSessionID());
}
/**
* Set BVL QC Status.
*
* @param string $status The status to set the BVL QC to.
* Valid options are Complete or null.
*
* @return void
*/
function setBVLQCStatus(string $status): void
{
$db =& \NDB_Factory::singleton()->database();
if (!in_array($status, $this->bvlQcStatuses)) {
throw new Exception("You need to specify a valid BVL QC Type");
}
// get the current bvlqc type
$qcType = $this->getBVLQCType();
// can't set the status to Complete unless the type is set
if ($status == 'Complete' && empty($qcType)) {
throw new Exception("Cannot complete QC until the type is defined");
}
$setArray = ['BVLQCStatus' => $status];
// update session table
$db->update(
'session',
$setArray,
['ID' => $this->getSessionID()]
);
// reload TimePoint object
$this->select($this->getSessionID());
}
/**
* Sets the BVL QC Exclusionary status for this timepoint.
*
* @param string $status The status to set this TimePoint to.
* Valid options are "Excluded" or "Not Excluded"
*
* @return true on success
*/
function setBVLQCExclusion(string $status): bool
{
$db = \NDB_Factory::singleton()->database();
// check the status
if (!in_array($status, $this->bvlQcExclusionStatuses)) {
throw new Exception(
"TimePoint::setBVLQCExclusion: "
. "You need to specify a valid BVL QC Exclusion Status"
);
}
// fields to update
$setArray = ['BVLQCExclusion' => $status];
// update session table
$db->update(
'session',
$setArray,
['ID' => $this->getSessionID()]
);
// reload TimePoint object
$this->select($this->getSessionID());
return true;
}
/**
* Determines whether the next stage for the timepoint is startable.
*
* Rules: 1. User must have permission.
* 2. If the stage to be started is a testing stage, then there can
* be no other timepoints of the same cohort that are in a
* testing stage (screening or visit).
* 3. If the stage to be started is not a testing stage, the the
* current stage status cannot be "In Progress".
*
* @return bool
*/
function isStartable(): bool
{
$currentStage = $this->getData('Current_stage');
// if we have not yet started, and at least one timepoint is in
// screening or visit, do not allow the user to start another timepoint
if ($currentStage == 'Not Started') {
return true;
} elseif ($currentStage == 'Screening'
&& in_array($this->getData('Screening'), ['Pass', 'Failure'])
) {
return true;
}
return false;
}
/**
* Returns either EDC or DOB depending on the settings of the cohort