Module: User Thank/Flame
Posted by J. David Smith
|
Module: User Thank/Flame March 04, 2008 09:47PM |
Registered: 18 years ago Posts: 49 |
Alright, due to the lack of a thanking/flaming module for Phorum and my need for one, I wrote this module.
It puts a star and a fire image in the lower right hand corner of ever post, the star allows the user to thank the author, and the fire to flame him. An overall ranking is shown based on the amount of thanks and flames (the algorithm is simple, thanks - flames = rank).
You can choose whether you want images or text down in the bottom right corner in the module settings page.
On the list page each thread has a rating based on the amount of thanks/flames each message in the thread has. Sorting based on rating doesn't work.
There is also a 'Top Users' page. This may conflict with the Top Users plugin, I am not sure, I have them both running on a test site on my local machine. If you are running both modules, I would recommend changing 'TopUsers' in lang/english.php to something else.
I would greatly appreciate any language files people can provide for this module in languages other than english.
For those of you who have installed this before, please re-read the readme in this new version (v0.7) and remove the {INCLUDE "user_thank_flame::css"} from you css.tpl (the css_register hook is now used).
6b.1 includes many, many, bugfixes and index validity checking (no more Undefined index messages)
0.7 should fix all of the foreach() warnings, as well as adding new images (thanks to Ryan), new lang support (French and Swedish, from SchoolAngels and sebdah), and a loophole fix.
Please report any bugs. Thank you.
Credits:
- Images: Ryan
- French Language: SchoolAngels
- Swedish Language: sebdah
- Special Thanks to Maurice Makaay for all the help he's given me on this module
Edited 11 time(s). Last edit at 03/18/2010 12:16PM by J. David Smith.
It puts a star and a fire image in the lower right hand corner of ever post, the star allows the user to thank the author, and the fire to flame him. An overall ranking is shown based on the amount of thanks and flames (the algorithm is simple, thanks - flames = rank).
You can choose whether you want images or text down in the bottom right corner in the module settings page.
On the list page each thread has a rating based on the amount of thanks/flames each message in the thread has. Sorting based on rating doesn't work.
There is also a 'Top Users' page. This may conflict with the Top Users plugin, I am not sure, I have them both running on a test site on my local machine. If you are running both modules, I would recommend changing 'TopUsers' in lang/english.php to something else.
I would greatly appreciate any language files people can provide for this module in languages other than english.
For those of you who have installed this before, please re-read the readme in this new version (v0.7) and remove the {INCLUDE "user_thank_flame::css"} from you css.tpl (the css_register hook is now used).
6b.1 includes many, many, bugfixes and index validity checking (no more Undefined index messages)
0.7 should fix all of the foreach() warnings, as well as adding new images (thanks to Ryan), new lang support (French and Swedish, from SchoolAngels and sebdah), and a loophole fix.
Please report any bugs. Thank you.
Credits:
- Images: Ryan
- French Language: SchoolAngels
- Swedish Language: sebdah
- Special Thanks to Maurice Makaay for all the help he's given me on this module
Edited 11 time(s). Last edit at 03/18/2010 12:16PM by J. David Smith.
|
Re: Module: User Thank/Flame March 05, 2008 02:41AM |
Admin Registered: 21 years ago Posts: 8,532 |
I took a quick look at the module and here are some comments for you.
- There is no need for "emerald", "classic" and "custom" in the templates. You only need "emerald", unless you really want to use different templates for each main template. If no specific template can be found, Phorum will revert to the "emerald" template by default.
- I'd put the images inside the template directory too, to let them be part of the template data. Then you can use different icons if a different template is used (it would now be possible too, but it would feel a bit more logical to have them in the actual template dir IMO).
- The module does not use any localization, so it can not easily be translated to different languages. It's a nice one to have and people will request for it. See supporting multiple languages in the developer documentation.
- You are using a lot of hooks in your templates and are generating HTML output from the module code. That kind of defeats the purpose of the templates (pulling the markup code from the module code and putting that into templates, so code and markup are fully separated).
- Why this init code?
- You are filling some meta data, but it's actually not checked in the code as far as I can see. So the check against bashing this system doesn't really work.
- For quick checking, don't record votes as $meta["mod_user_thank_flame"]["flames"][] = $uid; If you do something like $meta["mod_user_thank_flame"]["flames"][$uid] = 1; then checking for a flame will be a hash lookup. WIth your current method, it would need an iteration over the array to see if the user_id is in it or not. No problem with small amounts, but killing for performance when there are a lot of votes being cast.
I hope these hints are useful to you. Thanks for posting your module!
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
- There is no need for "emerald", "classic" and "custom" in the templates. You only need "emerald", unless you really want to use different templates for each main template. If no specific template can be found, Phorum will revert to the "emerald" template by default.
- I'd put the images inside the template directory too, to let them be part of the template data. Then you can use different icons if a different template is used (it would now be possible too, but it would feel a bit more logical to have them in the actual template dir IMO).
- The module does not use any localization, so it can not easily be translated to different languages. It's a nice one to have and people will request for it. See supporting multiple languages in the developer documentation.
- You are using a lot of hooks in your templates and are generating HTML output from the module code. That kind of defeats the purpose of the templates (pulling the markup code from the module code and putting that into templates, so code and markup are fully separated).
- Why this init code?
$meta["mod_user_thank_flame"] = array (
"thanks" => array(0 => ""),
"flames" => array(0 => ""),
);
Why not use => array() instead?
- You are filling some meta data, but it's actually not checked in the code as far as I can see. So the check against bashing this system doesn't really work.
- For quick checking, don't record votes as $meta["mod_user_thank_flame"]["flames"][] = $uid; If you do something like $meta["mod_user_thank_flame"]["flames"][$uid] = 1; then checking for a flame will be a hash lookup. WIth your current method, it would need an iteration over the array to see if the user_id is in it or not. No problem with small amounts, but killing for performance when there are a lot of votes being cast.
I hope these hints are useful to you. Thanks for posting your module!
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
|
Re: Module: User Thank/Flame March 05, 2008 08:56AM |
Registered: 18 years ago Posts: 49 |
I am still working on language support, that is why it is not included. Thanks for the tips on the template files. Also, how would I call, say, a module function without using a hook?
Would I use code such as the following?
Would I use code such as the following?
<?php
require('./mods/user_thank_flame/user_thank_flame.php')
tf_thank( ?> {USER->user_id} , {MESSAGES->user_id} , {MESSAGES->meta} <?php );
?>
Also, is there a way to pass the results of a hook to a variable? or set a template variable in the function? (I believe the variable $PHORUM['DATA']['hits'] would be the same as {hits} wouldn't it? or am I wrong?)
|
Re: Module: User Thank/Flame March 05, 2008 09:12AM |
Admin Registered: 21 years ago Posts: 8,532 |
You shouldn't have to call functions from your templates. Whether you use {HOOK ...} or a straight function call doesn't really matter. Deep inside, both are function calls. What you should do instead, is use other hooks to setup template data in $PHORUM['DATA'], which you can later on use in a template to display the data.
So for the MESSAGES array, you could use the "read" hook to add information in let's say {MESSAGES->user->flame_count} and {MESSAGES->user->thank_count} (or whatever you like) to the messages array. Then you would do all display logic in your template file using these variables.
That is correct.
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
So for the MESSAGES array, you could use the "read" hook to add information in let's say {MESSAGES->user->flame_count} and {MESSAGES->user->thank_count} (or whatever you like) to the messages array. Then you would do all display logic in your template file using these variables.
Quote
I believe the variable $PHORUM['DATA']['hits'] would be the same as {hits} wouldn't it?
That is correct.
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
|
Re: Module: User Thank/Flame March 05, 2008 07:13PM |
Registered: 18 years ago Posts: 49 |
how can I access a variable in a template that I set to $GLOBALS['PHORUM']['DATA']['hits'] or $PHORUM['DATA']['hits'] in the module? You said before that it would be {hits} But this is not working Any help (the code runs through fine, it sets hits to 1 if the user has been thanked/flamed or is the author, but the template is not picking up the variable. I have the code below:
portion of 'bar.tpl':
Here is the function code:
The functions.php file hasn't changed at all since I uploaded the package files, aside from a couple new comments.
Edited 2 time(s). Last edit at 03/05/2008 07:21PM by freereign.
portion of 'bar.tpl':
{HOOK "tftest" USER->user_id MESSAGES->author_id MESSAGES->message_id}
{hits}
The call to hits is just to test it.
Here is the function code:
//This function is hooked to tftest
function tf_user_check ($info)
{
//Get the user id
$uid = $info[0];
//Get the author id
$aid = $info[1];
//Get the message id
$message_id = $info[2];
//Get the message meta array
$meta = tf_get_meta($message_id);
//Check the message meta array for the required 'mod_user_thank_flame' array
$checked = tf_check_meta($meta);
$meta = $checked;
//Set hits to 0
$PHORUM['DATA']['hits'] = 0;
//Check if the user has already thanked or flamed the author, or if the user if the author
if ($meta["mod_user_thank_flame"]["thanks"][$uid] == 1 || $meta["mod_user_thank_flame"]["flames"][$uid] == 1 || $uid == $aid)
{
//If so set hits to 1
$PHORUM['DATA']['hits'] = 1;
}
//Troubleshooting stuff
print $meta["mod_user_thank_flame"]['thanks'][$uid];
print $PHORUM['DATA']['hits'];
//return hits
return $PHORUM['DATA']['hits'];
}
The functions.php file hasn't changed at all since I uploaded the package files, aside from a couple new comments.
Edited 2 time(s). Last edit at 03/05/2008 07:21PM by freereign.
|
Re: Module: User Thank/Flame March 05, 2008 07:47PM |
Admin Registered: 25 years ago Posts: 4,501 |
|
Re: Module: User Thank/Flame March 06, 2008 02:20AM |
Admin Registered: 21 years ago Posts: 8,532 |
Well, even worse. Inside the templates, you are not seeing the global $PHORUM, but a stripped down copy of it. Therefore, adding
This is however not the way to do is. You should not be using template hooks to time hook triggering for your module. In this case, you are working with the messages array on the read page. Please, use the "read" hook for working with this data. That saves you so much troubles, you won't even believe it. In general, when preparing data for displaying, you should be using hooks that are in the code. Hooks in the templates are only meant to give you a chance to output extra data at the spot of the hook call. Of course, creative use of template hooks is possible but it is not the optimal way to do things.
Example read hook (some possible PHP warnings for undefined indexes were fixed and anonymous author processing is skipped):
With this code in the read hook, you can use {MESSAGES->mod_user_thank_flame->hits} in the template code.
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
global $PHORUM;to the start of the hook function solves only halve the problem. You would have to add a global $PHORUM tot the template code too, somewhere before running your hook code. E.g.
<?php global $PHORUM ?>
{HOOK "tftest" USER->user_id MESSAGES->author_id MESSAGES->message_id}
{hits}
This is however not the way to do is. You should not be using template hooks to time hook triggering for your module. In this case, you are working with the messages array on the read page. Please, use the "read" hook for working with this data. That saves you so much troubles, you won't even believe it. In general, when preparing data for displaying, you should be using hooks that are in the code. Hooks in the templates are only meant to give you a chance to output extra data at the spot of the hook call. Of course, creative use of template hooks is possible but it is not the optimal way to do things.
Example read hook (some possible PHP warnings for undefined indexes were fixed and anonymous author processing is skipped):
function tf_read ($messages)
{
global $PHORUM;
//Get the user id
$uid = $PHORUM['user']['user_id'];
foreach ($messages as $id => $message)
{
//Set hits to 0
$messages[$id]['mod_user_thank_flame']['hits'] = 0;
//Skip processing for anonymous posts
if (empty($message['user_id'])) continue;
//Get the author id
$aid = $message['user_id'];
//Get the message id
$message_id = $message['message_id'];
//Get the message meta array
$meta = empty($message['meta']) ? array() : $message['meta'];
//Check the message meta array for the required
//'mod_user_thank_flame' array
$checked = tf_check_meta($meta);
$meta = $checked;
//Set hits to 0
$messages[$id]['mod_user_thank_flame']['hits'] = $hits;
$hits = 0;
//Check if the user has already thanked or flamed the author,
//or if the user is the author
if (!empty($meta["mod_user_thank_flame"]["thanks"][$uid]) ||
!empty($meta["mod_user_thank_flame"]["flames"][$uid]) ||
$uid == $aid)
{
//If so set hits to 1
$messages[$id]['mod_user_thank_flame']['hits'] = $hits;
}
}
With this code in the read hook, you can use {MESSAGES->mod_user_thank_flame->hits} in the template code.
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
|
Re: Module: User Thank/Flame March 10, 2008 03:50PM |
Registered: 18 years ago Posts: 136 |
|
Re: Module: User Thank/Flame March 10, 2008 04:30PM |
Registered: 18 years ago Posts: 49 |
Thanks for the tips mmakaay, I will work on that (just got back from out of town). Thanks for the support amnesia, actually, it is at 0.3. Next release will have the thank/flame restrictions fixed and possibly a settings page. Versions 0.1 and 0.2 weren't released to anyone but a few friends to get the major bugs worked out (we some how missed the thank/flame restrictions though).
|
Re: Module: User Thank/Flame March 12, 2008 04:28PM |
Registered: 18 years ago Posts: 49 |
Alright, thanks for the tip mmakaay. It works perfectly! I modified it slightly to check if the user is logged in as well, but other than that it is unchanged. Right after I post this I will upload v0.4a! (sorry, no settings page yet. That will be at v0.4)
Edited 1 time(s). Last edit at 03/12/2008 05:04PM by freereign.
Edited 1 time(s). Last edit at 03/12/2008 05:04PM by freereign.
Sorry, only registered users may post in this forum.

