|
1
|
|
|
<?php |
|
2
|
|
|
class VolunteerShift |
|
3
|
|
|
{ |
|
4
|
|
|
protected $dbData; |
|
5
|
|
|
protected $myStart = null; |
|
6
|
|
|
protected $myEnd = null; |
|
7
|
|
|
|
|
8
|
|
View Code Duplication |
public function __construct($shiftID, $dbData = null) |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
if($dbData === null) |
|
11
|
|
|
{ |
|
12
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
|
13
|
|
|
$filter = new \Data\Filter('_id eq '.$shiftID); |
|
14
|
|
|
$depts = $dataTable->read($filter); |
|
15
|
|
|
if(empty($depts)) |
|
16
|
|
|
{ |
|
17
|
|
|
throw new Exception('Unable to locate shift with ID '.$shiftID); |
|
18
|
|
|
} |
|
19
|
|
|
$dbData = $depts[0]; |
|
20
|
|
|
} |
|
21
|
|
|
$this->dbData = $dbData; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function __get($propName) |
|
25
|
|
|
{ |
|
26
|
|
|
switch($propName) |
|
27
|
|
|
{ |
|
28
|
|
|
case 'startTime': |
|
29
|
|
|
if($this->myStart === null) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->myStart = new \DateTime($this->dbData['startTime']); |
|
32
|
|
|
} |
|
33
|
|
|
return $this->myStart; |
|
34
|
|
|
case 'endTime': |
|
35
|
|
|
if($this->myEnd === null) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->myEnd = new \DateTime($this->dbData['endTime']); |
|
38
|
|
|
} |
|
39
|
|
|
return $this->myEnd; |
|
40
|
|
|
default: |
|
41
|
|
|
return $this->dbData[$propName]; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function isSame($shift) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->dbData['_id']->{'$id'} === $shift->dbData['_id']->{'$id'}; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function overlaps($shift) |
|
51
|
|
|
{ |
|
52
|
|
|
if($this->isSame($shift)) |
|
53
|
|
|
{ |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
if($this->startTime >= $shift->startTime && $this->startTime < $shift->endTime) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
if($this->endTime <= $shift->endTime && $this->endTime > $shift->startTime) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function isFilled() |
|
68
|
|
|
{ |
|
69
|
|
|
return isset($this->dbData['status']) && ($this->dbData['status'] === 'pending' || $this->dbData['status'] === 'filled'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function findOverlaps($uid, $shortCircuit = false) |
|
73
|
|
|
{ |
|
74
|
|
|
static $userShifts = null; |
|
75
|
|
|
static $count = 0; |
|
76
|
|
|
if($userShifts === null) |
|
77
|
|
|
{ |
|
78
|
|
|
$dataTable = DataSetFactory::getDataTableByNames('fvs', 'shifts'); |
|
79
|
|
|
$filter = new \Data\Filter("participant eq '$uid'"); |
|
80
|
|
|
$userShifts = $dataTable->read($filter); |
|
81
|
|
|
$count = count($userShifts); |
|
82
|
|
|
for($i = 0; $i < $count; $i++) |
|
83
|
|
|
{ |
|
84
|
|
|
$userShifts[$i] = new VolunteerShift(false, $userShifts[$i]); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
$res = array(); |
|
88
|
|
|
for($i = 0; $i < $count; $i++) |
|
89
|
|
|
{ |
|
90
|
|
|
if($this->overlaps($userShifts[$i])) |
|
91
|
|
|
{ |
|
92
|
|
|
if($shortCircuit === true) |
|
93
|
|
|
{ |
|
94
|
|
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
array_push($res, $userShifts[$i]); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
if($shortCircuit === true) |
|
100
|
|
|
{ |
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
return $res; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.