To Do List Project: Part 8

Today I began straightening out the calculation of total elapsed time for all slices associated with each To Do item. This work included three components: getting the date/time calculations correct and performed consistently (all on the PHP side and none on the JavaScript side), getting the calculations in the right place so they reflect what they’re supposed to reflect, and understanding the scope of PHP variables in different contexts across numerous files and sessions.

The calculation has to be performed for each completed slice when the Update To Do page is first created, and this work is done in the GetToDoSlices.php file. The code has to not update the calculation when a slice has been started but not completed. The code also has to make variables available that store the sums for elapsed hours and minutes, and for the currently effective start time if there is one, so the total elapsed hours and minutes can be displayed on the form in the appropriate spot. Finally, the code, this time in the UpdateStartTime.php and UpdateEndTime.php files, has to be able to mark the time that start and end events happen and update the running total as needed, both on the display and in the database.

You’ve seen a lot of this code already but I’ll just re-list the relevant files here so the action can be tracked. I can verify that it has been tested and works as expected.

Here is the entire GetToDoSlices.php file, where I’ve corrected a mistake.

Here is the UpdateStartTime.php file, which is invoked by clicking on the “Start” button.

Here is the UpdateEndTime.php file, which is invoked by clicking on the “End” button.

Here is the code in the UpdateToDo.php file that displays the accumulated hours and minutes for the current To Do item when the page is first created.

Here is the embedded JavaScript that updates the accumulated hours and minutes when the “End” button is clicked to complete a time slice.

Getting the date and time ops right is straightforward as is getting the summations and calculations right. That’s standard think and tweak. The value of this exercise for me is getting the “isness” of PHP, which in this case means getting a feel for what variables are available where. For this exercise I was able to declare and set things like $startTime, $endTime, $totalTime, $elapsedHours, and elapsedMinutes in one file and successfully refer to them in a different file without including a header or explicit link in the code. The link has to exist implicitly, and I don’t know exactly how yet, but at least I know the next set of concepts to look out for as I proceed.

How does the system keep track of these values? Is it done session by session with potentially multiple instantiations of each variable but for different users? How does this compare to what is done in Node.js? How the the server executive manage all of this? Inquiring minds wanna know! (yeah, sorry about that…)

Learning any language is an iterative process that you just have to wallow through until you can think in its idiom. You can work through the examples in a book or parrot those in a video course but until you try to change it or write your own you don’t really get the full depth of it, unless the instruction is exceptionally complete, and I’ve never encountered any such instruction in any area of my life. That indicates that there’s only so much complexity people can handle at one time. You have to understand one part before you even understand the questions that come up in the course of learning the next part. I know some instructors shared some advanced concepts in some swing dancing classes I was in that I vaguely remembered but didn’t quite process. Two years later it was all, “Oh now I get what he was talking about there!”

And so it goes. Day by day.

Posted in Software | Tagged , | Leave a comment

To Do List Project: Part 7

I decided to clean up the Update To Do page I’ve been working with to get rid of the debugging information and to put the navigation menu in a more reasonable location. Losing the debug information was trivial but moving the menu turned up something more interesting.

The PHP files that create entire pages included a file named Session.php that set up the session, user, and timeout mechanisms and generated the navigation menu (remember that the screens were utilitarian affair that only served to illustrate the principles of working with MySQL and some PHP, not to look like much, so part of the ongoing work on this project is to pretty things up over time). This file was included at the very top of the relevant page files, meaning that the menu ended up at the very top, also. When I moved the include statement for this file into the middle of the page (in its own, page-wide DIV below the title DIV) it generated the menu there as expected but also an error.

The error arose because the session management setup invokes the session_start() statement, and that statement has to be placed before the HEAD HTML element. If the menu is in the BODY of the HTML that’s obviously below the HEAD, hence the problem.

The solution was to split the statements that generate the menu into their own file so the Session file can stay at the top of the files where it’s required and a NavMenu.php files that can be placed where it needs to be.

Posted in Software | Tagged , | Leave a comment

To Do List Project: Part 6

Writing the ending time to each ToDoSlice item is more complex because we have to match to an existing record without benefit of having a unique ID to reference. Instead, we use a combination of the ToDoID index and the starting time for the slice, which we known from the sending form.

We start with the UpdateEndTime.php file. We get the information for all four columns of the ToDoSlices and then try to update the end time and note fields, which is where the trouble lies. Writing the ending time should be straightforward, since there should be no previous value for it in the record after it is created. Updating the Note field, however, will present a problem if its text has not changed. For some odd reason, MySQL’s update command won’t actually perform the update if a text value doesn’t change.

You can verify this by manually issuing the same statement as this code generates, which will cause the command-line interface to report 1 row matched but 0 rows changed. This operation won’t generate an error, it just won’t do anything at all, including update any other value in the record. For that reason the code jumps through a lot of hoops to update the end time and note fields separately, see if either operation generated an error, and report back the appropriate information. It’s probably possible that the end time will be properly updated while the note update fails (this would have to be something having to do with the MySQL host at a system level) so I should add some code to check for that condition. There are other options as well, one of which could be to only update the end time if a check of the note field reveals that its text has not changed.

Posted in Software | Tagged , , , , | Leave a comment

To Do List Project: Part 5

The next step is to make the back end actually write some data to a database using PHP and MySQL. This is the UpdateStartTime.php file. The startSliceAjax function that communicates with this PHP is shown below, just slightly modified from yesterday’s version.

The PHP simply inserts the sent information into the database. Each ToDoSlice item has an index indicating the unique ID of the To Do item with which it is associated but not a unique ID of its own. When writing the start time of a new slice this isn’t a problem because the slice item is being created anew and doesn’t have to match anything.

We’re receiving the echoed text from the PHP and displaying it based on what data it contains.

Posted in Software | Tagged , , , , | Leave a comment

To Do List Project: Part 4

The five states and four transitions in the AJAX process are:

  1. request not initialized
  2. server connection established
  3. request received
  4. processing request
  5. request finished and response is ready

If we don’t care about trying to trap process errors, assume we’ll always get to state 4, and don’t implement a timeout we can change the code from last time so it only updates when the process completes:

This way we only do something when the process completes. Note that setting up the End button only happens if and when a successful AJAX operation is completed. If the two lines were left at the bottom of the function there could be timing issues, so one has to be mindful of the asynchronous nature of the operations at all times.

Posted in Software | Tagged , , | Leave a comment

To Do List Project: Part 3

Today I replaced part of the update code with a basic AJAX call to derive the same information I was getting from JavaScript. The communication itself is straightforward. Here’s the updated version of the JavaScript, which is currently embedded in the UpdateToDo.php file.

Here’s the (mindlessly simple) UpdateStartTime.php file:

If the last part of the last line is uncommented then it returns the ToDoID that was sent with the AJAX request. This test was a precursor to adding a MySQL call to create a new time slice record in the database, associated with the current To Do item for the current user.

Two lines are commented out in the embedded JavaScript above. If they are uncommented then when the user clicks on the Start button the button is first replaced with the text “error encountered” and then with the date-time string returned by the AJAX. I’m still mentally getting a feel for this. The callback function is only “called” once, right? Well, no. It must be getting called on every change of state through the whole data exchange process, and as I recall from reading there are a number of such state changes. If I modify the new function as shown below, so it includes the count of the number of times the callback function is invoked, it ends up displaying a value of four, meaning that four state changes occur.

Now the trick is to understand what they all are and the best way to differentiate between a delayed reaction and a failed transfer so the process looks smooth and graceful.

Eventually I’ll have to address time zone issues if all timestamps are generated server side.

Posted in Software | Tagged , , | Leave a comment

To Do List Project: Part 2

Today I spent most of my time reading and watching videos. I’m trying to get a feel for matters of style in PHP (wrt endless rows of echo statements vs. raw JavaScript, among other things), revisit AJAX operations, and so on. The videos came from version 2.0 of the The Complete Web Developer Course by Rob Percival and the book is PHP for the Web: Visual QuickStart Guide (5th Edition), which I’m reading on Safaribooks.com.

The PHP code from the MySQL course looked one way and WordPress’s PHP tends to look different. I just have to do enough PHP on my own that I get a more natural feel for it.

Posted in Software | Tagged , , , | Leave a comment

To Do List Project: Part 1

Today I worked out a lot of the JavaScript that might be needed to effect the screen changes I want to have happen as various buttons are pushed. A To Do item against which no time has been expended would look like this:

If the user clicks on the Start button the button is replaced by the current date and time and the End button is made visible in the right-hand cell. If the user also adds a note to the time slice information then the table would look like this:

Finally, if the user clicks on the End button then it should be replaced by the current time, the text input field should be removed and replaced with the raw text that had been entered there, and a new set of rows should be added like this:

If the user checks the Complete checkbox then the last time slice row should be completed if it had been started (as if the user had clicked on the End button) or erased entirely if it had not been started (as if the user had not clicked on the Start button). I haven’t implemented that yet.

All of this actually works in CloudNine but what’s missing is the mechanics of making the requisite database entries for the time slice information as things proceed. What really should happen is the event should be forwarded to the server, where some PHP code would attempt to write the time slice information to the MySQL database. If the operation is successful the local HTML should be updated and if it is not successful an error message should be displayed. Successful operations should also update the values of hours and minutes in the Elapsed Time fields, and that also has not yet been implemented.

The background operations are going to require some AJAX and server-side code, which I’ll look at tomorrow. In the meantime here’s the (ugly) code that performs the raw JavaScript operations. This will have to be changed when I get the whole chain of events put together. The main thing for now is that I’m getting a feel for interactively navigating and modifying the DOM in a way that my previous work hasn’t given me.

Posted in Software | Tagged , , | Leave a comment

A Change of Direction for the Time Being

I’ve been focusing on new development and JavaScript for quite a while but need to spend more time grinding through books and online courses. I’ve been extending a PHP/MySQL/Bootstrap project for a while and need to get it finished so I can use it.

The problem with projects like this is that, since I can’t practicably maintain different versions of back end software, I can’t post active examples that demo a given day’s code updates. I guess we’ll have to live with code listings and, poooooossibly, working examples of changes made to the front end. That said, changes to back end software can easily break older front end examples, so I may choose to stick with listings and static screen captures.

In the meantime I’ll be updating the project I started as part of the The Complete MySQL Developer Course by Rob Percival on Udemy.com. It’s a simple To Do List application and since the emphasis is on MySQL the UI is pretty basic. I wanted to spruce up the UI to make it responsive using Bootstrap and add features from the book The 7 Habits of Highly Effective People. That part wasn’t difficult but I’m currently working through something a bit more complex (to me, at this time…).

I am adding the ability to track multiple time slices for different activities. This requires the addition of a new database table and some new UI functionality and interactivity. Here are the two important screenshots:

I’m not worrying about the Main screen for now (the upper one), I’m working through additions to the lower screen for individual activities. You can see an ugly-looking menu in the upper right corner, some random debugging information in the upper whitespace, and the section of interest shown with different colored regions. I colored the regions so I can see how the responsive sections realign as the screen or window changes size.

The header and footer regions are straightforward. The left section in light blue contains some basic information in the upper part and some 7 Habits information as a radio button selector in the lower part. The idea behind the latter item is to ensure you’re generating activities that address all areas of your life. Right right section in light red includes a few items from top to bottom. At the top is another radio button selector that characterizes the activity in terms of urgency and importance (most people tend to neglect class II activities and allow too much time to bleed away in class IV activities). The next item is the subject of my current efforts, the list of time slices so far devoted to the activity in question. Below that is a summation of total elapsed time and below that is a means to define the time to be allotted to the activity. Finally there’s a checkbox to indicate that the item has been completed.

Most of the time slice display is generated using a separate PHP file that includes a separate MySQL query. The trick it to be able to keep updating that part of the display while the screen is still visible. This involves adding entries to the database and modifying the information in the table cells, or adding new table rows entirely.

There are loads of additional concerns but for right now these are the basics.

Last but not least I’ve been doing the development in Cloud9, but I’ll eventually install it somewhere on my site as it gets closer to being done.

Posted in Software | Tagged , , , | Leave a comment

A Simple Discrete-Event Simulation: Part 88

Direct link for mobile devices.

Today I updated the code to define DisplayGroups for all non-Path components, which means the internal data for each component will become visible when the relevant component is clicked. Moreover, each visible DisplayGroup object can be dragged to a new location. These operations work for both mouse and touch operations. Try it out and see what you think.

I’m still highly annoyed that the intersection test doesn’t always generate the proper result, which causes the pointer not to be displayed, but the effect is otherwise pretty smooth.

I cleaned up some code that caused the frame colors for the DisplayGroups to not be consistent (mostly by commenting out some old stuff) and shortened up the definition function for DisplayGroups to use default colors that can be customized separately.

Here’s the code for handling events, made consistent for mouse and touch ops.

Posted in Software | Tagged , , , , , | Leave a comment