VoteCounter
Questions answered by this recipe
How can I get a count & summary of votes/ratings placed in a list on a page?
Description
Count & summarize votes. Votes indicated by an asterisk followed by a rating in parentheses followed by optional comment
Notes
Given a list of votes (asterisk or hash followed by some rating text within parentheses followed by optional freeform comment), 5 page variables are populated by this recipe:
Sample Data:
* (+) This is a good recipe * (++) Not just good, great! * (+) I'm satisfied * (+) It'll do the job * (-) Didn't work for me. * (+2) I really liked it
The rating text within parentheses can be arbitrary text, but +, ++, and - are recommended. Specifically any vote *not* beginning with a + or a - will be ignored for the VoteSum and VoteWeightedSum options.
PV Name | Description | Output |
{$VoteCount} | Total count of votes | 6 |
{$VoteDetail} | A summary of the rating text contained within parentheses. (VoteDetails also acceptable) | +(3), ++(1), -(1), +2(1) |
{$Votes} | Combination of vote count and vote details. | 6: +(3), ++(1), -(1), +2(1) |
{$VoteSum} | Sum of plus's minus minuses (++ or +2 or -- is 1). | 4 |
{$VoteWeightedSum} | Sum of + minus - (++ is 2, +3 is 3, etc.). | 6 |
There is also a (:votecounter ARGS:)
markup as an alternative to the PVs. It allows more flexibility, including accessing votes from a page section, returning multiple values with a single read of the page, etc. Arguments (ARGS) follow:
page=group.page#text | Specifies an alternate page to be read. Sections are allowed if Toolbox is installed) |
detailfmt="{RATETEXT}({COUNT}) " | Specify an alternate format for the details. 2 "macros" substituted: {RATETEXT} and {COUNT} . Default is {RATETEXT}({COUNT}) . |
detailsep=", " | Specify an alternate separater between detail "chunks" |
votes|votedetail|votecount|votesum|voteweightedsum | 0 or more of these can be specified as stand-alone arguments (case-insensitive) |
"Some other text: " | Text not matching patterns above will be output verbatim to allow a somewhat formatted output |
maxweight=n | Specify the maximum weight a single vote can carry for {$VoteWeightedSum} . Default is 4. This prevents a single very positive or very negative vote from overly influencing the weighted sum. |
Examples of the (:votecounter ...:)
markup:
(:votecounter votedetails:) |
(:votecounter "Here is a count: " votecount "and a weighted sum: " VoteWeightedSum:) |
VoteCounter( |
(:votecounter page=Cookbook.VoteCounter-Sandbox "Sum from another page: " votesum:) |
VoteCounter( |
(:votecounter page=Cookbook.VoteCounter-Sandbox#subset detailsep="..." votedetails:) (Note: the [[Toolbox]] recipe is not activated on this pmwiki.org page and so sections do not work here.) |
VoteCounter( |
Sandbox
Make changes to Cookbook.VoteCounter-Sandbox?action=edit to see the results here:
Current contents of the Cookbook.VoteCounter-Sandbox page:
Click here to go back to the VoteCounter recipe to see the results of the sandbox
- (+) vote
- (+) some other content Sam
- (2,5) testing indentation and non integer
- (-) I didn't like it Anonymous?
- (sam) an example of how arbitrary text can be used as a "rating"
- (sam) +sam
- (+2) What a great idea! Susie?
- (+2) Best ever! Mathilda?
- This line is ignored because it has no rating in parentheses
- (++) Another way of saying i really like it Mr Optimist?
- (+) It'll do even though I didn't call
- (++++) It'll do even though I didn't call
- (+) Any other thoughts?
- (+) Strange
- ([-]) The "unticked" flipbox
- ([-]) The "unticked" flipbox
- ([+]) The "ticked" flipbox
- ([q]) The "question" flipbox
Note that lines that don't follow the asterisk-openparen-rating-closeparen pattern are simply ignored.
Votecount PVs based on the sandbox page above
{$VoteCount} | 17 |
{$VoteDetail} | +(5), 2,5(1), -(1), sam(2), +2(2), ++(1), ++++(1), [-](2), [+](1), [q](1) |
{$Votes} | 17: +(5), 2,5(1), -(1), sam(2), +2(2), ++(1), ++++(1), [-](2), [+](1), [q](1) |
{$VoteSum} | 8 |
{$VoteWeightedSum} | 13 |
Installation
Place votecounter.phpΔ in your cookbook directory in include the usual
include_once("$FarmD/cookbook/votecounter.php");
in your config.php. For PHP 5.5 or newer please get votecounter72.phpΔ instead.
You can also set $vcDetailFmt and $vcDetailSep if you want to change the presentation of the detail section. In $vcDetailFmt occurrences of $k
will be replaced with the rating text and occurrences of $v
will be replaced with the count of the given rating text.
$vcMaxWeight can be set to fix a default maximum weight.
Release Notes
- 2010-01-31: Fixed a bug where the semantics of "weighted vote" and "vote" were reversed. Added maxweight capabilities. (NOTE: I forgot to update the $RecipeInfo line when I originally uploaded this. I fixed that a week later but did not make a new release. If your SiteAnalyzer reports an error but you're sure you already updated, this is why... The new version (uploaded 2010-02-06) fixes this. Sorry.)
- 2009-03-10: Fixed a bug related to absence of toolbox recipe, made it so setting detailfmt=x wouldn't change the global variable for all other invocations of votecounter markup.
- 2009-03-09b: Added alternate
(:votecounter ...:)
to allow for sections, refactored code for flexibility. - 2009-03-09: Fixed a bug introduced in last release. Made arbitrary text for ratings case-insensitive.
- 2009-03-08: Added capability for votes to occur at any level of either ordered lists or unordered lists (i.e., can be preceded by ** or *** or #### or whatever). Added
{$VoteSum}
and{$VoteWeightedSum}
. Moved the recipe into the votecounter.php script instead of being in config.php directly. If you are upgrading you will need to delete the previous installation from config.php. - 2009-03-07: Added $vcDetailFmt and $vcDetailSep for flexibility on the detail presentation.
- 2009-03-06: Initial release
See Also
- SimpleRating a simple rating system
- Rating A Quick and easy way for visitors to rate an item or issue.
- Rating2 Even Simpler Page Rating Counter
- StarRater 5 star rating system
- FoxVoting examples of using Fox for voting, polling and making choices
- RyeVoting Voting with several option and graph bars
Contributors
Comments
User notes +2: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.