From 25b37cd1b3b544683127aabfed4c285383cfd21c Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 15:25:28 +0200 Subject: [PATCH 01/17] Improved the starting point --- .idea/copyright/profiles_settings.xml | 3 + .idea/workspace.xml | 158 ++++++++++++++++---------- index.php | 27 +++-- list_tasks.php | 20 ++++ 4 files changed, 135 insertions(+), 73 deletions(-) create mode 100644 .idea/copyright/profiles_settings.xml create mode 100644 list_tasks.php diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 77e0469..90968ba 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,9 +1,7 @@ - - - + @@ -14,7 +12,6 @@ - @@ -23,11 +20,11 @@ - + - + @@ -37,12 +34,10 @@ - + - - @@ -51,17 +46,17 @@ - + - - + + - + @@ -82,16 +77,16 @@ - + + true - - - + DEFINITION_ORDER - @@ -116,10 +111,10 @@ + + - - @@ -128,22 +123,14 @@ - - - - - - + + - + @@ -168,43 +155,64 @@ + project + + + + + - + + 1463122043916 + + - - + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + @@ -215,15 +223,49 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + @@ -231,7 +273,7 @@ - + @@ -239,12 +281,10 @@ - + - - diff --git a/index.php b/index.php index ea44966..f04b68d 100644 --- a/index.php +++ b/index.php @@ -60,18 +60,6 @@
@@ -83,27 +71,38 @@ \ No newline at end of file diff --git a/list_tasks.php b/list_tasks.php new file mode 100644 index 0000000..d2ccbfa --- /dev/null +++ b/list_tasks.php @@ -0,0 +1,20 @@ + + */ +$taskData = file_get_contents('Task_Data.txt'); +if (strlen($taskData) < 1) { + $html = ' +

No Tasks Available

+

Click here to create one

+
'; + die($html); +} + +?> \ No newline at end of file From 7b425dd90015b26681e2299b8303fa73d52b57c3 Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 15:40:28 +0200 Subject: [PATCH 02/17] Improved the task class starting point --- task.class.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/task.class.php b/task.class.php index e0fd225..d6d1b81 100644 --- a/task.class.php +++ b/task.class.php @@ -6,6 +6,7 @@ class Task { public $TaskId; public $TaskName; public $TaskDescription; + protected $TaskDataSource; public function __construct($Id = null) { if ($Id) { // This is an existing task @@ -14,12 +15,17 @@ public function __construct($Id = null) { // This is a new task $this->Create(); } + $this->TaskDataSource = file_get_contents('Task_Data.txt'); + if (strlen($this->TaskDataSource) > 0) + $this->TaskDataSource = json_decode($this->TaskDataSource); + else + $this->TaskDataSource = 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->TaskName = 'New Task'; + $this->TaskDescription = 'New Description'; } protected function LoadFromId($Id = null) { if ($Id) { From d1807527c14a80d93d1d7b118c1583b4c7f7c500 Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 15:42:30 +0200 Subject: [PATCH 03/17] Improved the task class starting point --- task.class.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/task.class.php b/task.class.php index d6d1b81..dc3f1b7 100644 --- a/task.class.php +++ b/task.class.php @@ -8,6 +8,12 @@ class Task { 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); // 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 ($Id) { // This is an existing task $this->LoadFromId($Id); @@ -15,11 +21,6 @@ public function __construct($Id = null) { // This is a new task $this->Create(); } - $this->TaskDataSource = file_get_contents('Task_Data.txt'); - if (strlen($this->TaskDataSource) > 0) - $this->TaskDataSource = json_decode($this->TaskDataSource); - else - $this->TaskDataSource = null; } protected function Create() { // This function needs to generate a new unique ID for the task From c077e2b501718204e148eaba5034a706ca6bdd2b Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 16:13:30 +0200 Subject: [PATCH 04/17] Improved task listing --- list_tasks.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/list_tasks.php b/list_tasks.php index d2ccbfa..646cabd 100644 --- a/list_tasks.php +++ b/list_tasks.php @@ -9,12 +9,22 @@ * @author johangriesel */ $taskData = file_get_contents('Task_Data.txt'); -if (strlen($taskData) < 1) { - $html = ' +$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 $index=>$task) { + $html .= ' +

'.$task->TaskName.'

+

'.$task->TaskDescription.'

+
'; + } +} +die($html); ?> \ No newline at end of file From 6ae29c659d4541a560a29f1813143ea94f8d0801 Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 17:17:04 +0200 Subject: [PATCH 05/17] Starting data improved --- Task_Data.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/Task_Data.txt b/Task_Data.txt index e69de29..eb99a7e 100644 --- a/Task_Data.txt +++ b/Task_Data.txt @@ -0,0 +1 @@ +[{"TaskId":1,"TaskName":"Test","TaskDescription":"Test"},{"TaskId":"2","TaskName":"Test2","TaskDescription":"Test2"}] \ No newline at end of file From e35661eae4dd22f574577102d8c2108b349a2943 Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 17:18:10 +0200 Subject: [PATCH 06/17] Fixed task listing --- list_tasks.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/list_tasks.php b/list_tasks.php index 646cabd..6123c52 100644 --- a/list_tasks.php +++ b/list_tasks.php @@ -7,6 +7,7 @@ * @package ${NAMESPACE} * @subpackage ${NAME} * @author johangriesel + * 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 = ' @@ -19,7 +20,7 @@ $taskArray = json_decode($taskData); if (sizeof($taskArray) > 0) { $html = ''; - foreach ($taskArray as $index=>$task) { + foreach ($taskArray as $task) { $html .= '

'.$task->TaskName.'

'.$task->TaskDescription.'

From 1b57454d1531ce4ca6e377e2d0e0f50cb1331c3f Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 17:19:20 +0200 Subject: [PATCH 07/17] Fixed zero tasks --- list_tasks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/list_tasks.php b/list_tasks.php index 6123c52..4f43640 100644 --- a/list_tasks.php +++ b/list_tasks.php @@ -10,7 +10,7 @@ * 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 = '
+$html = '

No Tasks Available

Click here to create one

'; From 26a76a364b2479b5f11740dc439105487c22a138 Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 17:21:59 +0200 Subject: [PATCH 08/17] Improved the task class --- .idea/Stratusolve-Exercise.iml | 10 ++++ .idea/misc.xml | 14 ----- .idea/workspace.xml | 99 +++++++++++++++++++++++----------- task.class.php | 15 +++--- 4 files changed, 86 insertions(+), 52 deletions(-) delete mode 100644 .idea/misc.xml diff --git a/.idea/Stratusolve-Exercise.iml b/.idea/Stratusolve-Exercise.iml index c956989..b313593 100644 --- a/.idea/Stratusolve-Exercise.iml +++ b/.idea/Stratusolve-Exercise.iml @@ -4,5 +4,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 19f74da..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 90968ba..232a325 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,11 @@ - + + + + + @@ -25,17 +29,17 @@ - + - + - + @@ -47,17 +51,17 @@ - + - + - - + + @@ -71,9 +75,9 @@ @@ -85,20 +89,13 @@ - - - - - - - - + - @@ -175,17 +172,18 @@ - - - + + - + @@ -205,6 +203,9 @@ + + \ No newline at end of file diff --git a/task.class.php b/task.class.php index dc3f1b7..2594f68 100644 --- a/task.class.php +++ b/task.class.php @@ -14,19 +14,22 @@ public function __construct($Id = null) { else $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); - } else { - // This is a new task + 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->TaskId = $this->getUniqueId(); $this->TaskName = 'New Task'; $this->TaskDescription = 'New Description'; + var_dump($this); + } + protected function getUniqueId() { + // Assignment: Code to get new unique ID + return -1; // Placeholder return for now } protected function LoadFromId($Id = null) { if ($Id) { From 2c7770a5c72e678b53d080a45e9c84fd07f33ef2 Mon Sep 17 00:00:00 2001 From: JohanGriesel Date: Thu, 15 Dec 2016 17:22:31 +0200 Subject: [PATCH 09/17] Improved the task class --- .idea/workspace.xml | 14 ++++++-------- task.class.php | 1 - 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 232a325..8bd72f9 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,6 @@ - - @@ -60,8 +58,8 @@ - - + + @@ -172,12 +170,12 @@ - @@ -318,8 +316,8 @@ - - + + diff --git a/task.class.php b/task.class.php index 2594f68..5a39240 100644 --- a/task.class.php +++ b/task.class.php @@ -25,7 +25,6 @@ protected function Create() { $this->TaskId = $this->getUniqueId(); $this->TaskName = 'New Task'; $this->TaskDescription = 'New Description'; - var_dump($this); } protected function getUniqueId() { // Assignment: Code to get new unique ID From 778915b5043549d9e2e980e4b16a3aa278b83be7 Mon Sep 17 00:00:00 2001 From: "local\\Tjaart.Bester" Date: Thu, 22 Feb 2018 20:09:21 +0200 Subject: [PATCH 10/17] Set task information on madal popup for edit or delete. --- index.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.php b/index.php index f04b68d..c802d59 100644 --- a/index.php +++ b/index.php @@ -84,6 +84,8 @@ $('#deleteTask').show(); currentTaskId = triggerElement.attr("id"); console.log('Task ID: '+triggerElement.attr("id")); + $('#InputTaskName').val(document.getElementById(currentTaskId).children[0].firstChild.data); + $('#InputTaskDescription').val(document.getElementById(currentTaskId).children[1].firstChild.data); } }); $('#saveTask').click(function() { From 15c41100fc4eb48b5eae3c4d4fcf1791c488c8f9 Mon Sep 17 00:00:00 2001 From: "local\\Tjaart.Bester" Date: Thu, 22 Feb 2018 20:11:03 +0200 Subject: [PATCH 11/17] Post update with task information to update_task.php --- index.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index c802d59..8528176 100644 --- a/index.php +++ b/index.php @@ -89,8 +89,13 @@ } }); $('#saveTask').click(function() { - //Assignment: Implement this functionality - alert('Save... Id:'+currentTaskId); + var taskToUpdate = {"TaskId": currentTaskId, "TaskName": document.getElementById('InputTaskName').value, "TaskDescription": document.getElementById('InputTaskDescription').value}; + $.post("update_task.php", + {"action":"update", "taskToUpdate":taskToUpdate }, + function ( data ) { + alert('Update... Id:'+data); + currentTaskId = data; + }); $('#myModal').modal('hide'); updateTaskList(); }); From 8c629223999abf1ec32720426a62960b5cddb9ab Mon Sep 17 00:00:00 2001 From: "local\\Tjaart.Bester" Date: Thu, 22 Feb 2018 20:12:24 +0200 Subject: [PATCH 12/17] Process task based on action ('update' or 'delete') --- update_task.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/update_task.php b/update_task.php index 4c69960..e8a4d37 100644 --- a/update_task.php +++ b/update_task.php @@ -2,6 +2,18 @@ /** * This script is to be used to receive a POST with the object information and then either updates, creates or deletes the task object */ -require('Task.class.php'); -// Assignment: Implement this script -?> \ No newline at end of file +require('task.class.php'); + +$action = $_POST['action']; +$task = new Task(); +$taskToUpdate = $_POST['taskToUpdate']; +$task->TaskId = $taskToUpdate['TaskId']; +$task->TaskName = $taskToUpdate['TaskName']; +$task->TaskDescription = $taskToUpdate['TaskDescription']; +if ($action == 'update') { + $task->Save(); +} else if ($action == 'delete') { + $task->Delete(); +} + +?> From 8e71c02ac2c6c944c3045ffd037548130e0b710e Mon Sep 17 00:00:00 2001 From: "local\\Tjaart.Bester" Date: Thu, 22 Feb 2018 20:13:44 +0200 Subject: [PATCH 13/17] Post delete action to update_task.php --- index.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index 8528176..ffa9582 100644 --- a/index.php +++ b/index.php @@ -100,8 +100,13 @@ function ( data ) { updateTaskList(); }); $('#deleteTask').click(function() { - //Assignment: Implement this functionality - alert('Delete... Id:'+currentTaskId); + var taskToUpdate = {"TaskId": currentTaskId, "TaskName": document.getElementById('InputTaskName').value, "TaskDescription": document.getElementById('InputTaskDescription').value}; + $.post("update_task.php", + {"action":"delete", "taskToUpdate":taskToUpdate }, + function ( data ) { + alert('Delete... Id:'+data); + currentTaskId = data; + }); $('#myModal').modal('hide'); updateTaskList(); }); From 13d5ffd877b85ffbb75df3ce7e02b6cf7cb0b5bc Mon Sep 17 00:00:00 2001 From: "local\\Tjaart.Bester" Date: Thu, 22 Feb 2018 20:18:00 +0200 Subject: [PATCH 14/17] Add new task to TaskDataSource array or update task within TaskDataSource in case of update. --- task.class.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/task.class.php b/task.class.php index 5a39240..0c4ce22 100644 --- a/task.class.php +++ b/task.class.php @@ -38,7 +38,19 @@ protected function LoadFromId($Id = null) { } public function Save() { - //Assignment: Code to save task here + if($this->TaskId == -1) { + $this->TaskId = $this->getUniqueId(); + array_push($this->TaskDataSource, $this); + } else { + foreach ($this->TaskDataSource as $task) { + if($this->TaskId == $task->TaskId) { + $task->TaskName = $this->TaskName; + $task->TaskDescription = $this->TaskDescription; + } + } + } + file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource)); + echo $this->TaskId; } public function Delete() { //Assignment: Code to delete task here From 118be6d16f5d0556048bed36197a7cc0356b89a1 Mon Sep 17 00:00:00 2001 From: "local\\Tjaart.Bester" Date: Thu, 22 Feb 2018 20:19:02 +0200 Subject: [PATCH 15/17] Delete task from TaskDataSource based on TaskId. --- task.class.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/task.class.php b/task.class.php index 0c4ce22..f7368fa 100644 --- a/task.class.php +++ b/task.class.php @@ -53,7 +53,15 @@ public function Save() { echo $this->TaskId; } public function Delete() { - //Assignment: Code to delete task here + $this->TaskDataSource = $this->loadFromFile($this->TaskDataSourcePath); + //echo '$this->TaskId=' . $this->TaskId . ' | $this->TaskName=' . $this->TaskName . ' | $this->TaskDescription=' . $this->TaskDescription . '\n
/n'; + for ($pos = 0; $pos < count($this->TaskDataSource); $pos++) { + if ($this->TaskDataSource[$pos]->TaskId == $this->TaskId) { + unset($this->TaskDataSource[$pos]); + } + } + file_put_contents($this->TaskDataSourcePath, json_encode($this->TaskDataSource)); + echo $this->TaskId; } } ?> \ No newline at end of file From d1d04979a129b59a665ed2521a417245c6c9ba85 Mon Sep 17 00:00:00 2001 From: "local\\Tjaart.Bester" Date: Thu, 22 Feb 2018 20:21:36 +0200 Subject: [PATCH 16/17] Generate TaskId by determining which is bigger (array length on last Id++). --- task.class.php | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/task.class.php b/task.class.php index f7368fa..a7507f3 100644 --- a/task.class.php +++ b/task.class.php @@ -28,15 +28,39 @@ protected function Create() { } protected function getUniqueId() { // Assignment: Code to get new unique ID - return -1; // Placeholder return for now + $newID = count($this->TaskDataSource); + if($newID == 0) { + ++$newID; + return $newID; + } else { + sort($this->TaskDataSource); + if ($newID < (int)end($this->TaskDataSource)->TaskId) { + $newID = (int)end($this->TaskDataSource)->TaskId; + ++$newID; + } + return $newID; + } + } + public function loadFromFile($Path) { + $TempTaskDataSource = file_get_contents($Path); + if (strlen($TempTaskDataSource) > 0) + $TempTaskDataSource = json_decode($TempTaskDataSource); // Should decode to an array of Task objects + else + $TempTaskDataSource = array(); // If it does not, then the data source is assumed to be empty and we create an empty array + return $TempTaskDataSource; + } protected function LoadFromId($Id = null) { if ($Id) { - // Assignment: Code to load details here... + $this->TaskDataSource = $this->loadFromFile($this->TaskDataSourcePath); + foreach ($this->TaskDataSource as $task) { + if($Id == $task->TaskId) { + return json_encode($task); + } + } } else return null; } - public function Save() { if($this->TaskId == -1) { $this->TaskId = $this->getUniqueId(); From 837fdd01a048e5eedb5f48a788968f1fd62d1ee1 Mon Sep 17 00:00:00 2001 From: "local\\Tjaart.Bester" Date: Thu, 22 Feb 2018 20:23:19 +0200 Subject: [PATCH 17/17] Created function for loading task information from file. --- task.class.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/task.class.php b/task.class.php index a7507f3..352bda3 100644 --- a/task.class.php +++ b/task.class.php @@ -7,13 +7,9 @@ class Task { public $TaskName; public $TaskDescription; protected $TaskDataSource; + protected $TaskDataSourcePath = 'Task_Data.txt'; 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 - + $this->TaskDataSource = $this->loadFromFile($this->TaskDataSourcePath); 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))