diff --git a/Task_Data.txt b/Task_Data.txt index e69de29..6697904 100644 --- a/Task_Data.txt +++ b/Task_Data.txt @@ -0,0 +1,37 @@ +[ + { + "TaskId": 1, + "TaskName": "Test", + "TaskDescription": "Test" + }, + { + "TaskId": "2", + "TaskName": "Test2", + "TaskDescription": "Test2" + }, + { + "TaskId": 3, + "TaskName": "Lesenya", + "TaskDescription": "Lesenya Man" + }, + { + "TaskId": 4, + "TaskName": "Mookho", + "TaskDescription": "My Wife" + }, + { + "TaskId": 5, + "TaskName": "Likeleli", + "TaskDescription": "My girl" + }, + { + "TaskId": 6, + "TaskName": "Demy", + "TaskDescription": "My life" + }, + { + "TaskId": 7, + "TaskName": "Keneuoe", + "TaskDescription": "My sister" + } +] \ No newline at end of file diff --git a/index.php b/index.php index ea44966..07754c2 100644 --- a/index.php +++ b/index.php @@ -29,10 +29,10 @@
- +
- +
@@ -60,18 +60,6 @@
- -

Task Name

-

Task Description

-
- -

Task Name

-

Task Description

-
- -

Task Name

-

Task Description

-
@@ -83,27 +71,53 @@ \ No newline at end of file diff --git a/index1.php b/index1.php new file mode 100644 index 0000000..ea44966 --- /dev/null +++ b/index1.php @@ -0,0 +1,109 @@ + + */ +?> + + + + Basic Task Manager + + + + + + + + +
+ +
+ + + + + \ No newline at end of file diff --git a/list_tasks.php b/list_tasks.php new file mode 100644 index 0000000..4f43640 --- /dev/null +++ b/list_tasks.php @@ -0,0 +1,31 @@ + + * Task_Data.txt is expected to be a json encoded string, e.g: [{"TaskId":1,"TaskName":"Test","TaskDescription":"Test"},{"TaskId":"2","TaskName":"Test2","TaskDescription":"Test2"}] + */ +$taskData = file_get_contents('Task_Data.txt'); +$html = ' +

No Tasks Available

+

Click here to create one

+
'; +if (strlen($taskData) < 1) { + die($html); +} +$taskArray = json_decode($taskData); +if (sizeof($taskArray) > 0) { + $html = ''; + foreach ($taskArray as $task) { + $html .= ' +

'.$task->TaskName.'

+

'.$task->TaskDescription.'

+
'; + } +} +die($html); +?> \ No newline at end of file diff --git a/task.class.php b/task.class.php index e0fd225..8c8824b 100644 --- a/task.class.php +++ b/task.class.php @@ -3,10 +3,21 @@ * This class handles the modification of a task object */ class Task { - public $TaskId; - public $TaskName; - public $TaskDescription; + //private fields + private $TaskId; + private $TaskName; + private $TaskDescription; + protected $TaskDataSource; + //Constructor to provide default values public function __construct($Id = null) { + $this->TaskDataSource = file_get_contents('Task_Data.txt'); + if (strlen($this->TaskDataSource) > 0) + $this->TaskDataSource = json_decode($this->TaskDataSource); // 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 ($Id) { // This is an existing task $this->LoadFromId($Id); @@ -17,22 +28,95 @@ public function __construct($Id = null) { } protected function Create() { // This function needs to generate a new unique ID for the task - // Assignment: Generate unique id for the new task - $this->TaskName = ''; - $this->TaskDescription = ''; + $this->setTaskId(); + //$this->TaskName = 'New Task'; + //$this->TaskDescription = 'New Description'; + $this->setTaskName(); + $this->setTaskDescription(); + } + //functions to get task properties(Data Encapsulation) + public function getTaskName(){ + return $this->TaskName; + } + public function getTaskDescription(){ + return $this->TaskDescription; + } + public function getTaskId(){ + return $this->TaskId; + } + //functions to set tast properties(Data encapsulation) + public function setTaskName($name = "New Task"){ + $this->TaskName = $name; + } + public function setTaskId(){ + $this->TaskId = $this->getUniqueId(); + } + public function setTaskDescription($description = "New Description"){ + $this->TaskDescription = $description; + } + + protected function getUniqueId() { + // Assignment: Code to get new unique ID + $arr_data = array(); + $file_data = file_get_contents("Task_Data.txt"); + $arr_data = json_decode($file_data,true); + $arr_id = array(); + //Initialise counter variable + $i = 0; + //Put all ids in an array and assign new Id to the number next to the highest value in the array + foreach($arr_data as $value){ + $arr_id[$i] = $value["TaskId"]; + $i++; + } + sort($arr_id); + return ($arr_id[count($arr_id)-1]+1); } protected function LoadFromId($Id = null) { if ($Id) { // Assignment: Code to load details here... - } else - return null; + //Create an empty array + $arr_data = array(); + //load all file contents + $file_data = file_get_contents("Task_Data.txt"); + //Decode data and put it into associative array + $arr_data = json_decode($file_data,true); + foreach($arr_data as $value){ + if($Id==$value["TaskId"]){ + $this->TaskId = $value["TaskId"]; + $this->setTaskName($value["TaskName"]); + $this->setTaskDescription($value["TaskDescription"]); + } + } + } else{ + return null; + } } public function Save() { //Assignment: Code to save task here + $my_data = array("TaskId"=>$this->getTaskId(),"TaskName"=>$this->getTaskName(),"TaskDescription"=>$this->getTaskDescription()); + //append data to the end of file data + $this->TaskDataSource[] = $my_data; + file_put_contents("Task_Data.txt",json_encode($this->TaskDataSource,JSON_PRETTY_PRINT)); } public function Delete() { //Assignment: Code to delete task here + //Create an empty array + $arr_data = array(); + //Read all file contents + $file_data = file_get_contents("Task_Data.txt"); + //Decode data and put it into associative array + $arr_data = json_decode($file_data,true); + //Declare and initialize a counter variable + $i = 0; + foreach($arr_data as $key=>$value){ + if(($this->TaskName==$value["TaskName"])&&($this->TaskDescription==$value["TaskDescription"])){ + unset($arr_data[$i]); + } + $i++; + } + $arr_data = array_values($arr_data);//re-index the array after deletion + file_put_contents("Task_Data.txt",json_encode($arr_data,JSON_PRETTY_PRINT)); } } ?> \ No newline at end of file diff --git a/update_task.php b/update_task.php index 4c69960..88ef454 100644 --- a/update_task.php +++ b/update_task.php @@ -1,7 +1,33 @@ setTaskName($_POST['TaskName']); + $tas->setTaskDescription($_POST['TaskDescription']); + $tas->Save(); + echo $tas->getTaskName(); +} + } +} + +if(isset($_POST['mydelete'])){ + if($_POST['mydelete']=="deletetask"){ +if(isset($_POST['TaskName'])||isset($_POST['TaskDescription'])){ + $tas->setTaskName($_POST['TaskName']); + $tas->setTaskDescription($_POST['TaskDescription']); + $tas->Delete(); + echo $tas->getTaskName(); +} + } +} + + ?> \ No newline at end of file