The Homepage of Daniel Hollands: Web Development Graduate!
17 Oct 2009
I don’t know where I thought I would be by this point, but I think I’ve made a lot of progress in just three and a half days of coding.
(Warning, this is a VERY long post.)
I started by building the view chapter ability. This accepts the ID number of the chapter that the user wants to view, and upon pulling it from the database, displays it on the page. Due to the relationship between the Chapter and the Path models, CakePHP ‘automagically’ pulls the related paths in the same query, allowing for them to also be displayed. I also built a simple method which checks a path’s ID for a following chapter, and either forwards to the chapter in question, or shows an error saying that the chapter doesn’t exist yet. So by this point, a user is able to manually enter a chapter’s ID number, have that chapter display, then click on the path of interest, and have it either forward to the correct chapter, or just display an error.
Next up was the story view page. Again, this was fairly simple to achieve thanks to the existing relations, allowing for display of information such as which tags have been assigned to the story in question. Couple this with a method which automatically finds the first chapter of a given story, and the user is now also able to manually enter a story’s slug, find out about the story, then start reading the first chapter.
Now for the first real challenge, creating a list of stories based upon a selected tag. This could have been easy enough as there is a relation between stories and tags, meaning that upon retrieving data about a specific tag, all the stories associated with that tag are also pulled. The problem is that I wanted to use pagination, and although Cake makes pagination easy for direct queries, it’s not so easy when trying to do it on HABTM relations such as the one between the Tag and Story models. After some research, however, I was able to find a solution to the problem. I don’t in any way understand how this works, all I know is that it does. Add to this a list of popular tags in the header of the site, and the user is now able to select a tag of interest from the menu, view all of the stories within that tag (while also being able to sort them by title and date), and choose the one of most interest.
By this point I was feeling quite good. I was starting to get back into the swing of not only coding in PHP, but also the object oriented nature of Cake. I was even understanding the different roles of the Models and Controllers, and (I feel) using them to the best effect. It was Friday morning, so I set myself a challenge – so code in the ability to add a chapter to the database before going to bed.
This was not as easy as predicted, however, as before you can even look at building the form to accept the chapter data, there are several checks which need to be preformed. The first is to check if the path being added to is valid. This fails if the path doesn’t exist, or if someone else has already added the follow on chapter.
The second revolves around the chapter reservation system the site uses. To prevent two people from trying to add to the same path simultaneously, the path first needs to be reserved. This gives the user 30 minutes in which to write their chapter, while showing a message to anyone else trying to access the path that it has been reserved by someone else.
The problem with this is that it can’t exist until the user authorisation system has been built. Cake provides a component called Auth which does a lot of the legwork for you, but unfortunately this is something I’ve always had trouble understanding. After a lot of trial and error, however, I got it working (well, as working as needed at this stage), and created two user accounts for testing purposes.
A bit more trial and error and I ended up with a system which checked for reservations (returning 0 if no reservation existed, 1 if the reservation belonged to the logged in user, or 2 if the reservation belonged to someone else), but as of yet was unable to create reservations. I had also spent time implementing some accessibility into the forms the site uses by following a tutorial by Andrew Sellick, and another by The CSS Guy (although the latter doesn’t work correctly at this point due to CSS conflicts).
I think it was about midnight by the time I got to this stage, and while I could have kept going, I thought it would be better to get some sleep and continue in the morning. Challenge failed, but I didn’t mind as I had still accomplished a lot.
In the morning I moved directly onto creating the form which allows users to add chapters. Once again I run into a minor roadblock as I needed to save data to two different models (Chapter and Path) from a single form. Cake provides a saveAll method by which to do this, but without the help of a tutorial by teknoid, I would have never figured it out. Once this was working, I then built in the ability to reserve a chapter, and declared success!
So, as it stands at the moment, a user is able to go to the site’s homepage, choose a tag from the list in the header and see all the stories aligned with that tag. Upon choosing a story,the user is able to start reading the first chapter and navigate via the paths available at the end of the first chapter, and all continuing chapters. Should you reach a path without a continuing chapter, the user is first asked to log into the site, then to reserve the chapter, before being given the ability to successfully add the new chapter itself.
Not bad for three and a half days work.
I’ve still got a long way to go (i.e. I still need to allow reservations to be extended, chapters to be edited, users to register, as well as a ton of other functionality), but I’m feeling quite good about what I’ve already achieved.
One Response for "Three and a half days coding"
[...] after all the great work that I did on the site during the first three days and a half days of coding, it’s now four days later and and I’ve had nothing but trouble. It has been so much [...]