views:

382

answers:

2

I am creating an 'award' system for my website to encourage a community fealing, much in the same way that stack overflow uses badges.

The site is built in CakePHP, i have created an Award model, controller etc - currently the methods for calculating whether a user has an award are contained within the Award controller.

However, i can't decide on the best way to update awards - say for example, you make a certain number of comments and receive an award for this, CRON wouldn't be sufficient otherwise the user might visit their profile expecting an Award and not have one until the CRON is run.

I was thinking of using requestAction() to call the method to update awards in the Award controller however this doesn't really fit the MVC design pattern and thought there must be a better way!

A: 

If the only way to get an award is through making a comment, I'd just override Model::save to determine if it's a new comment being added. If it is, make a call to the awards model to update the award.

If there are multiple actions that can give awards, you could either tap into each model to perform this check, or modify AppModel so that every save results in a call to checkNewAwards()...

Or perhaps I'm not understanding the question?

Travis Leleu
Thanks, I have around 20 different awards, each has an associated method to check for it - do you think i should move these from the Awards Controller to the AppModel?
Tom
I think I probably would at that point in time. With upwards of 20 awards, it would be much nicer to have that code centralized to perform the checks. The tradeoff is that you will then be checking the conditions on every save, even those where you wouldn't normally do it. So it'll cost you some execution time, but I would think that would be balanced out by code maintainability, easily.
Travis Leleu
To reduce on wear and tear i could provide a list of awards to check in each model save, so in the comments model it would only check awards to do with comments etc..i think a combination of everyone's advice here should do the trick!
Tom
I think that would be the "most right" type solution -- good balance. Most of the work I do is of the rapid prototype realm, so I would probably put it in the AppModel::save() method, just because it's faster that way. Glad you found a solution that fits your needs, though.
Travis Leleu
A: 

create a function in the helper and put it in your layout so it runs every time the layout is rendered.

ondrobaco
this wouldn't be wise as the methods can be fairly database intensive...
Tom