Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lebogang's final changes #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/Stratusolve-Exercise.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

284 changes: 176 additions & 108 deletions .idea/workspace.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Task_Data.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"TaskId":1,"TaskName":"Test","TaskDescription":"Test"},{"TaskId":"2","TaskName":"Test2","TaskDescription":"Test2"}]
{"5":{"TaskId":6,"TaskName":"Task 1","TaskDescription":"Testing if it will save accordingly"},"6":{"TaskId":7,"TaskName":"Task 2","TaskDescription":"Testing if update and delete work correctly"},"7":{"TaskId":8,"TaskName":"Task 4","TaskDescription":"Testing resetting of field values"},"8":{"TaskId":9,"TaskName":"Task 3","TaskDescription":"Testing adding again to see if the changes will work just fine"},"9":{"TaskId":10,"TaskName":"Task 5","TaskDescription":"Testing without the closing php tag"},"10":{"TaskId":11,"TaskName":"Task 6","TaskDescription":"Surely now everything works accordingly? "}}
59 changes: 53 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,78 @@
if (triggerElement.attr("id") == 'newTask') {
modal.find('.modal-title').text('New Task');
$('#deleteTask').hide();
setFieldValues();
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');

setFieldValues($(taskNameElement).text(), $(taskDescriptionElement).text());

console.log('Task ID: '+triggerElement.attr("id"));
}
});

var setFieldValues = function(taskName, taskDescription) {
if (taskName === undefined) {
taskName = "";
}
if (taskDescription === undefined) {
taskDescription = "";
}
$("#InputTaskName").val(taskName);
$("#InputTaskDescription").val(taskDescription);
};

var handleSubmission = function(path, data) {
$.post(path, data, function (feedback) {
var message = feedback.message;
var messageClass = 'alert alert-danger';
if (feedback.success) {
messageClass = 'alert alert-success';
updateTaskList();
}

var messageBox = $('<div id="feedback-message" class="' + messageClass + '">' + message + '</div>');

$(".modal-body form").prepend(messageBox);
setTimeout(function() {
$("#feedback-message").remove();
setFieldValues();
$('#myModal').modal('hide');
}, 3000);

}, 'json');
};

$('#saveTask').click(function() {
//Assignment: Implement this functionality
alert('Save... Id:'+currentTaskId);
$('#myModal').modal('hide');
updateTaskList();
//alert('Save... Id:'+currentTaskId);

var taskName = $("#InputTaskName").val();
var taskDescription = $("#InputTaskDescription").val();

var data = {task_id:currentTaskId, task_name:taskName, task_description:taskDescription, action:'save'};
handleSubmission('update_task.php', data)
});

$('#deleteTask').click(function() {
//Assignment: Implement this functionality
alert('Delete... Id:'+currentTaskId);
$('#myModal').modal('hide');
updateTaskList();
//alert('Delete... Id:'+currentTaskId);

var data = {task_id:currentTaskId, action:'delete'};
handleSubmission('update_task.php', data);
});

function updateTaskList() {
$.post("list_tasks.php", function( data ) {
$( "#TaskList" ).html( data );
});
}

updateTaskList();
</script>
</html>
99 changes: 89 additions & 10 deletions task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ class Task {
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); // Should decode to an array of Task objects
$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

Expand All @@ -19,29 +20,107 @@ public function __construct($Id = null) {
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';
$this->setTaskId($this->getUniqueId());
$this->setTaskName('New Task');
$this->setDescription('New Description');
}

protected function getUniqueId() {
// Assignment: Code to get new unique ID
return -1; // Placeholder return for now
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) {
if ($Id) {
// Assignment: Code to load details here...
} else
return 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;
}
}
?>
42 changes: 41 additions & 1 deletion update_task.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
<?php
header('Content-type:application/json;charset=utf-8');
/**
* 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
?>

$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);

switch ($action) {
case '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';
break;

case 'delete' :
$deleted = $taskClass->Delete();
$message = "Something went wrong, couldn't delete task";
if ($deleted) {
$success = true;
$message = "Task has been successfully deleted";
}
break;

default :
$message = 'Unknown action selected';

}
}

echo json_encode(['message' => $message, 'success' => $success]);
exit;