From a526cc03fe93a017684347c71b7d8593be99e7a1 Mon Sep 17 00:00:00 2001 From: Lebogang Date: Fri, 2 Feb 2018 12:39:20 +0200 Subject: [PATCH 1/8] Updated the task to execute the required functionality --- .idea/.name | 1 - .idea/Stratusolve-Exercise.iml | 10 - .idea/copyright/profiles_settings.xml | 3 - .idea/workspace.xml | 289 +++++++++++--------------- Task_Data.txt | 2 +- index.php | 61 +++++- task.class.php | 75 ++++++- update_task.php | 18 ++ 8 files changed, 270 insertions(+), 189 deletions(-) delete mode 100644 .idea/.name delete mode 100644 .idea/copyright/profiles_settings.xml diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index db3a436..0000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -Stratusolve-Exercise \ No newline at end of file diff --git a/.idea/Stratusolve-Exercise.iml b/.idea/Stratusolve-Exercise.iml index b313593..c956989 100644 --- a/.idea/Stratusolve-Exercise.iml +++ b/.idea/Stratusolve-Exercise.iml @@ -4,15 +4,5 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8bd72f9..07d5a14 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,12 +1,18 @@ - + + + + + + + + + + - - - - - - - - - - + - + - + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - - + + + + + @@ -74,6 +98,7 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + project + + + true + + + + DIRECTORY + + false + + + + + + + + + + + @@ -186,37 +242,39 @@ - - + + - + - - - - + + + + - - + + + - + - - + + - - - + - - + + - + - - + + + + + + + + + + + + + + + + + + + + + - + - + - + - - + + + + + + + + + + - + @@ -275,15 +360,39 @@ - + - - + + + + + + + + + + - + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Task_Data.txt b/Task_Data.txt index 2696a06..830bd08 100644 --- a/Task_Data.txt +++ b/Task_Data.txt @@ -1 +1 @@ -[{"TaskId":1,"TaskName":"Task 2","TaskDescription":"Saving and updating"},{"TaskId":2,"TaskName":"Task 3 ","TaskDescription":"Testing deleting of task 4"},{"TaskId":3,"TaskName":"Negative value","TaskDescription":"Testing whether we will have a negative value or not"},{"TaskId":4,"TaskName":"Not sure what task number this","TaskDescription":"But it should work just fine I supposed"}] \ No newline at end of file +{"2":{"TaskId":3,"TaskName":"Negative value","TaskDescription":"Testing whether we will have a negative value or not"},"3":{"TaskId":4,"TaskName":"Another Task test","TaskDescription":"Testing whether task will be saved or not"},"4":{"TaskId":5,"TaskName":"Task 3","TaskDescription":"This should do the trick"},"5":{"TaskId":6,"TaskName":"Another task test","TaskDescription":"Testing Updating and deleting of tasks. (An update test)"}} \ No newline at end of file diff --git a/index.php b/index.php index e673cc0..7fd59e7 100644 --- a/index.php +++ b/index.php @@ -78,11 +78,19 @@ if (triggerElement.attr("id") == 'newTask') { modal.find('.modal-title').text('New Task'); $('#deleteTask').hide(); + $("#InputTaskName").val(''); + $("#InputTaskDescription").val(''); currentTaskId = -1; } else { modal.find('.modal-title').text('Task details'); $('#deleteTask').show(); currentTaskId = triggerElement.attr("id"); + var taskNameElement = triggerElement.find('h4'); + var taskDescriptionElement = triggerElement.find('p'); + + $("#InputTaskName").val($(taskNameElement).text()); + $("#InputTaskDescription").val($(taskDescriptionElement).text()); + console.log('Task ID: '+triggerElement.attr("id")); } }); diff --git a/task.class.php b/task.class.php index 7c2c5cb..394ae17 100644 --- a/task.class.php +++ b/task.class.php @@ -24,9 +24,9 @@ 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->TaskId = $this->getUniqueId(); - $this->TaskName = 'New Task'; - $this->TaskDescription = 'New Description'; + $this->setTaskId($this->getUniqueId()); + $this->setTaskName('New Task'); + $this->setDescription('New Description'); } protected function getUniqueId() { @@ -46,9 +46,9 @@ protected function LoadFromId($Id = null) { if ($Id && !empty($this->TaskDataSource)) { foreach($this->TaskDataSource as $dataSource) { if ($Id == $dataSource['TaskId']) { - $this->TaskId = $dataSource['TaskId']; - $this->TaskName = $dataSource['TaskName']; - $this->TaskDescription = $dataSource['TaskDescription']; + $this->setTaskId($dataSource['TaskId']); + $this->setTaskName($dataSource['TaskName']); + $this->setDescription($dataSource['TaskDescription']); return true; } @@ -60,53 +60,67 @@ protected function LoadFromId($Id = null) { public function Save() { //Assignment: Code to save task here - $feedback = ['message' => 'No post data found', 'success' => false]; - if (isset($_POST)) { - - $this->TaskId = filter_input(INPUT_POST, 'task_id', FILTER_SANITIZE_NUMBER_INT); - $this->TaskName = filter_input(INPUT_POST, 'task_name', FILTER_SANITIZE_STRING); - $this->TaskDescription = filter_input(INPUT_POST, 'task_description', FILTER_SANITIZE_STRING); - $isUpdated = false; - - if ($this->TaskId > 0 && !empty($this->TaskDataSource)) { - foreach ($this->TaskDataSource as $key=>$dataSource) { - if ($this->TaskId == $dataSource['TaskId']) { - $this->TaskDataSource[$key] = ['TaskId'=>$this->TaskId, 'TaskName' =>$this->TaskName, 'TaskDescription' => $this->TaskDescription]; - $isUpdated = true; - $feedback = ['message' => 'Task successfully updated', 'success' => true]; - break; - } - } - } + $key = $this->findArrayKey(); + if ($key === -1) { + $currentMaxKey = max(array_keys($this->TaskDataSource)); + $key = $currentMaxKey == 0 ?: $currentMaxKey + 1; + } + $this->TaskDataSource[$key] = [ + 'TaskId'=>$this->getTaskId(), + 'TaskName' =>$this->getTaskName(), + 'TaskDescription' => $this->getDescription(), + ]; - if (!$isUpdated) { - $this->TaskId = $this->getUniqueId(); - $this->TaskDataSource[] = ['TaskId'=>$this->TaskId, 'TaskName' =>$this->TaskName, 'TaskDescription' => $this->TaskDescription]; - $feedback = ['message' => 'Task successfully added', 'success' => true]; - } + file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource)); + } - file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource)); - } - echo json_encode($feedback); - exit(); + public function setTaskId($taskId) { + $this->TaskId = $taskId; } - public function Delete() { - //Assignment: Code to delete task here - $feedback = ['message' => 'Task not found', 'success' => false, 'task Id'=>$this->TaskId]; - if (!is_null($this->TaskId) && $this->TaskId > 0) { + 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() { + $taskKey = -1; + if (!is_null($this->TaskId) && $this->TaskId > -1) { foreach ($this->TaskDataSource as $key=>$dataSource) { if ($this->TaskId == $dataSource['TaskId']) { - unset($this->TaskDataSource[$key]); - file_put_contents('Task_Data.txt', json_encode($this->TaskDataSource)); - $feedback = ['message' => 'Task successfully deleted', 'success' => true]; + $taskKey = $key; + break; } } } + return (int)$taskKey; + } - echo json_encode($feedback); - exit; - + 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; } } ?> \ No newline at end of file diff --git a/update_task.php b/update_task.php index cb33f13..4f47f5c 100644 --- a/update_task.php +++ b/update_task.php @@ -5,21 +5,36 @@ require('Task.class.php'); // Assignment: Implement this script -if (isset($_POST) ) { +$message = 'You either tried accessing this directly or submitted an invalid action. Please trying saving the form again'; +$success = false; + +if (isset($_POST)) { + $action = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_STRING); $taskId = filter_input(INPUT_POST, 'task_id', FILTER_SANITIZE_NUMBER_INT); - $taskClass = new Task($taskId); if ($action == 'save') { + $taskName = filter_input(INPUT_POST, 'task_name', FILTER_SANITIZE_STRING); + $taskDescription = filter_input(INPUT_POST, 'task_description', FILTER_SANITIZE_STRING); + + $taskClass->setTaskName($taskName); + $taskClass->setDescription($taskDescription); $taskClass->Save(); + $success = true; + $message = 'Task has been successfully created/updated'; } if ($action == 'delete') { - $taskClass->Delete(); + $deleted = $taskClass->Delete(); + $message = "Something went wrong, couldn't delete task"; + if ($deleted) { + $success = true; + $message = "Task has been successfully deleted"; + } } } -echo json_encode(['message' => 'You either tried accessing this directly or submitted an invalid action. Please trying saving the form again', 'success'=>false]); +echo json_encode(['message' => $message, 'success' => $success]); exit; ?> \ No newline at end of file From 971ec3a2f03f5d9987af7f47903157c2d7df4360 Mon Sep 17 00:00:00 2001 From: Lebogang Date: Thu, 21 Feb 2019 15:47:39 +0200 Subject: [PATCH 5/8] Lebogang's final Stratusolve task --- .idea/workspace.xml | 67 +++++++++++++++++++++++++-------------------- index.php | 1 - task.class.php | 15 +++++----- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 18594ae..b49876d 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,11 +2,8 @@ - - - @@ -249,12 +249,12 @@