forked from Stratusolve/Stratusolve-Exercise-Old
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.class.php
More file actions
126 lines (108 loc) · 3.92 KB
/
task.class.php
File metadata and controls
126 lines (108 loc) · 3.92 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
<?php
/**
* This class handles the modification of a task object
*/
class Task {
public $TaskId;
public $TaskName;
public $TaskDescription;
protected $TaskDataSource;
public function __construct($Id = null) {
$this->TaskDataSource = file_get_contents('Task_Data.txt');
if (strlen($this->TaskDataSource) > 0)
$this->TaskDataSource = json_decode($this->TaskDataSource, true); // Should decode to an array of Task objects
else
$this->TaskDataSource = array(); // If it does not, then the data source is assumed to be empty and we create an empty array
if (!$this->TaskDataSource)
$this->TaskDataSource = array(); // If it does not, then the data source is assumed to be empty and we create an empty array
if (!$this->LoadFromId($Id))
$this->Create();
}
protected function Create() {
// This function needs to generate a new unique ID for the task
// Assignment: Generate unique id for the new task
$this->setTaskId($this->getUniqueId());
$this->setTaskName('New Task');
$this->setDescription('New Description');
}
protected function getUniqueId() {
// Assignment: Code to get new unique ID
if (!empty($this->TaskDataSource)) {
$highest = 0;
foreach($this->TaskDataSource as $dataSource) {
$highest = max($highest, $dataSource['TaskId']);
}
return ++$highest;
}
return 1; // Placeholder return for now
}
protected function LoadFromId($Id = null) {
$Id = (int) $Id;
if ($Id && !empty($this->TaskDataSource)) {
foreach($this->TaskDataSource as $dataSource) {
if ($Id == $dataSource['TaskId']) {
$this->setTaskId($dataSource['TaskId']);
$this->setTaskName($dataSource['TaskName']);
$this->setDescription($dataSource['TaskDescription']);
return true;
}
}
}
return null;
}
public function Save() {
//Assignment: Code to save task here
$key = $this->findArrayKey(true);
$this->TaskDataSource[$key] = [
'TaskId'=>$this->getTaskId(),
'TaskName' =>$this->getTaskName(),
'TaskDescription' => $this->getDescription(),
];
file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource));
}
public function setTaskId($taskId) {
$this->TaskId = $taskId;
}
public function setTaskName($taskName) {
$this->TaskName = $taskName;
}
public function setDescription($description) {
$this->TaskDescription = $description;
}
public function getTaskId() {
return $this->TaskId;
}
public function getTaskName() {
return $this->TaskName;
}
public function getDescription() {
return $this->TaskDescription;
}
public function findArrayKey($generateIfNotExists = false) {
$taskKey = -1;
if (!is_null($this->TaskId) && $this->TaskId > -1) {
foreach ($this->TaskDataSource as $key=>$dataSource) {
if ($this->TaskId == $dataSource['TaskId']) {
$taskKey = $key;
break;
}
}
}
if (-1 === $taskKey && $generateIfNotExists) {
$currentMaxKey = max(array_keys($this->TaskDataSource));
$taskKey = $currentMaxKey == 0 ?: $currentMaxKey + 1;
}
return $taskKey;
}
public function Delete() {
//Assignment: Code to delete task here
$deleted = false;
$key = $this->findArrayKey();
if ($key > -1) {
unset($this->TaskDataSource[$key]);
file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource));
$deleted = true;
}
return $deleted;
}
}