Cake Star Rating

AJAX star rating using CakePHP

Well, it finally came time to include a rating system into one of my applications. Since I like to believe in the whole software-reuse thing, I started by culling the many voting and rating scripts available. Once again I was frustrated by over-complication. I can understand the desire to make a piece of software as configurable as possible to allow maximum customization, but I guess I've learned that personally, I prefer to have the basic functionality there, and if I need/want to customize it further, I can write the code to do so.

The beauty of cake is the ability to write components/plugins/helpers etc. that can be reused without extraneous files (how many db.config.inc files have you downloaded?)

That led the search to RosSoft's helper. Unfortunately, it requires some CJSwhatsit that took me more than three seconds to find information on, so I quickly abandoned it. Besides, I wasn't going to go through some 3-chapter tutorial to learn some other thing I'd probably only use for this.

Finally, I found an excellent simple star rating system using css from komodomedia and subsequently discovered an ajax implementation at masugadesign. Unfortunately, it looks like the php code was based upon slim's php/mysql tutorial. Don't get me wrong, it's a great tutorial to get someone started on having a database backend with the rating system, but aside from the need for cleaner code, there was a huge problem I saw. Maybe it's just me, but I was not comfortable with keeping track of users who rated by using a serialized array of ip addresses. For example, if you have a youtube video with 1200 ratings, something's bound to break. Plus I wasn't too sure about using varchar for numeric data.

So, I decided to roll my own component and helper. Besides being more fun, it gave me a chance to try out writing a helper (which is pretty simple, but still...).

I decided to use two separate tables, one for user information (which could include an ip address) and the 'article' table which holds the rating information. I added a link (intersection) table to map the id's of both for the many-to-many relationship.

I was tempted to stuff everything into a single component class, but I thought I'd try and stay a little more faithful to the whole MVC thing and sliced everything out between the helper and component (and of course the models).

More information, usage and code can be found in part 2.

BACK TO TOP