Advanced Ratings & Calculation Syntax | ||||||||||||||||||||||||||||||||||||
Use this page to configure a "rating" sytem to evaluate tracker items or wiki pages. Introduced in Tiki5, the advanced rating feature allows for more control over the aggregation of scores. You will also see in this documentation page how to use the calculations syntax, which also applies at the Mathematical Calculation Tracker Field. Related Topics
Note Tiki Calculations started out as an advanced rating system, and has since evolved into a powerful general purpose calculations system.
The feature must first be enabled through this same administration panel. Along with the feature, a few options are available. Among them, the score recalculation period must be defined. These are the available options:
The value ranges for each object type can also be specified through the administration panels.
The common sort_mode parameter to lists can be used to activate sorting using advanced ratings. To do so, the sort mode must be set to adv_rating_X_asc or adv_rating_X_desc where X is the ID of the rating configuration. The default sort can also be set to advanced ratings in the administration panel where applicable. |
Calculation configuration | |
|
Sorting items according to advanced rating | |
|
Set-up | |
By default, each calculated value is kept for 1 hour (3600 seconds). This limit does not apply when recalculating on vote, but is used for every other technique to avoid recalculating the same scores over and over again. The calculation is defined as a small piece of code, similar to functional languages, which is very close to mathematical representations. Creating custom formulas is expected to require some mathematical skills. However, this documentation should provide examples for most frequent cases. The editor in the administration panel performs extensive validation and will make it impossible to save the formula unless it can be evaluated. Checks are performed for:
Copy to clipboard
Copy to clipboard
mul is a mathematical function. (mul 3600 24 30) is equivalent to 3600*24*30. The functions can be combined in various ways. For example, we could calculate a score that considers the votes from the past month, but gives extra emphasis on the recent ones. Copy to clipboard
All available options are documented in the following section. |
Syntax | |
General Reference | |
|
Sample and use case | |
|
add (Sum) | |
Performs a simple sum accepting multiple input.
Copy to clipboard
Copy to clipboard
|
sub (Substract) | |
Performs a simple substraction accepting multiple input Copy to clipboard
|
div (Divide) | |
Performs a simple division accepting multiple input values. Copy to clipboard
|
mul (Multiply) | |
Performs a simple multiplication accepting multiple input values. Copy to clipboard
|
and / or | |
andEnsures all elements evaluate to true. Copy to clipboard
orEnsures that at least one element evaluates to true. Elements are evauated sequentially until a false element is found. Others are left unevaluated. Copy to clipboard
|
avg | |
Calculates the average of multiple values. All entries in the list will be flattened if arrays are present. Copy to clipboard
Copy to clipboard
Copy to clipboard
|
clean | |
Clean accents and special characters from a string of text, replacing the accented characters with the non-accented equivalent where possible. Added in Tiki 18.2 Copy to clipboard
|
coalesce | |
Returns the first non-empty value from the list. Copy to clipboard
|
comment | |
Any comment block is stripped from the formula at parse-time Copy to clipboard
|
concat | |
Concatenates a string of text. (new in Tiki12) Note: The quoted string syntax was included in Tiki13. Copy to clipboard
|
contains | |
Searches for a value within a vector of values (new in Tiki15.0, backported to Tiki14.2 and Tiki12.5). Copy to clipboard
Copy to clipboard
Copy to clipboard
|
count | |
Returns the total number of entries within an array passed as argument. Copy to clipboard
Copy to clipboard
Only one key need to be created it will populate properly |
Currency | |
New in Tiki21: https://sourceforge.net/p/tikiwiki/code/71175 Allows to convert a calculation into currency field. Expects 3 arguments. First is the calculation of the amount. Second is the source currency - i.e. which currency is the amount in? Third is the currency field. Copy to clipboard
Copy to clipboard
Copy to clipboard
|
currency-convert | |
New in Tiki22: https://sourceforge.net/p/tikiwiki/code/76059 This adds 2 things:
|
Date and Time | |
Note about Date and Time and timezone
Tiki convert back the value to display based on the Tiki preferences or the user preferences settings related to timezone. Calculation are done on the value (unix timestamp from the database) in UTC and date formulas have to include this factor to allow exact calculation.
Copy to clipboard
Copy to clipboard
Date
Takes two optional arguments, format and timestamp and uses the PHP date function. See here for reference. Format defaults to "U" which is the Unix timestamp, and the timestamp defaults to "now". Copy to clipboard
Date-diff
Calculates the difference between two dates as unix timestamps. Optionally supply a third parameter to make it calculate the difference between two dates calculating only working days. See str-to-time for more info about calculation of working days. |
equals | |
Compares multiple values. Copy to clipboard
|
for-each | |
For a list of value pairs, such as the output of split-list, evaluates a formula for each set of values, returns the list of results. Within the formula, variables coming from the list will be used first. Fallback will be on the other variables available in the execution context. As of Tiki18, list items can be the output of ItemsList tracker field where individual formula fields are the linked fields from the other tracker. See example below. Copy to clipboard
|
hash | |
Generates a hash based on multiple values. Used primarily to generate aggregate hashes in the PluginActivityStream. Note that because it is a hash, the exact value coming out does not matter. Only that given the same parameter, it will produce the same value. Copy to clipboard
|
if | |
Conditionally evaluates a branch. Copy to clipboard
Copy to clipboard
|
IsEmpty | |
New in Tiki14: https://sourceforge.net/p/tikiwiki/code/53588 Copy to clipboard
Copy to clipboard
|
less / more | |
less-thanNew in Tiki14.1 and Tiki12.5: Checks whether the first value is less than the second. Copy to clipboard
more-thanNew in Tiki14.1 and Tiki12.5: Checks whether the first value is more than the second. Copy to clipboard
|
lower | |
Converts string to lower case. Copy to clipboard
|
map | |
Generates a map (or dictionary). Copy to clipboard
|
Not | |
New in Tiki14: https://sourceforge.net/p/tikiwiki/code/53590 |
number-format | |
Format a number with grouped thousands. See PHP's number_format function for exact arguments. New in Tiki18. Copy to clipboard
|
pad | |
New in Tiki15 committed in r57094 Takes two to four arguments, input string, output length, padding string (defaults to a space) and padding type (defaults to right)
Copy to clipboard
|
round | |
Rounds to a specific number of digits (new in Tiki12) Copy to clipboard
|
str | |
Generates a static string when needed and the processor attempts to process the string as a variable. Any arguments will be concatenated using spaces. Note: The quoted string syntax was included in Tiki13. Copy to clipboard
Copy to clipboard
|
str-to-time | |
Parse about any English textual datetime description into a Unix timestamp. See PHP's strtotime function for more details on valid formats and options. New in Tiki18. Copy to clipboard
Copy to clipboard
Copy to clipboard
New in Tiki 26: in order to make use of business/working days calculation, you should create a Holidays calendar, add the event dates that are holidays (might be one-time, single-day, multi-day events or recurring events like every weekend Sat/Sun, for example). Then, you can choose this holidays calendar in Tiki admin calendar section to be the default holidays calendar. Then, any calculation involving 'business' or 'working' days will actually exclude any days marked as holidays. |
str-replace | |
Replace substring in a string. See PHP's str_replace function for exact arguments. New in Tiki18. Copy to clipboard
|
preg-replace | |
Perform a regular expression search and replace on a field value. The preg-replace math function is available in Tiki 21+ Copy to clipboard
Copy to clipboard
|
split-list | |
Produces a multi-dimensional array out of a text string. Each line is expected to be an independent value, each line will be split by a separator into the specified keys. Copy to clipboard
|
subtotal | |
Special function to aggregate data in a table. See Report formatting for more details.
|
upper | |
Converts string to upper case. Copy to clipboard
|
Advanced Rating-specific Reference | |
rating-average and rating-sum | |
The rating functions calculate the score from the rating history table. Each rating performed on the site is kept in the database and can be used to calculate custom ratings on. The various options allow to adapt the score calculation to reflect the importance on the site, whether it is to support quality improvement on documentation or to rank incoming data on a feed aggregator.
|
article-info | |
Pulls information from an article to include in the calculation. The first argument must always resolve to 'article'. If any other value, the calculation will be skipped for the evaluated object, making the formula type-specific. Available properties:
Copy to clipboard
|
attribute | |
Pulls information from the generic object attributes. Copy to clipboard
|
tracker-field | |
Pulls information from the tracker item. The field value is converted to numeric value automatically. Zero is provided if the value is not found or unapplicable. Copy to clipboard
Item Link and Dynamic Item list values If you grab directly the information from an Item Link and Dynamic Item List using the permaname the result will be the value of the field in this tracker and not the value of the field from the remote tracker.
You have to use the following to pull the "real" value of an item coming from an item link and an item dynamic list field type
Copy to clipboard
|
category-present | |
Gives 1 in score of every listed category present on the object. Copy to clipboard
|
Appendix | |
When unified search is used, recalculation can be configured to be done during re-indexing, removing the need for this script. Copy to clipboard
|
Sample and use case | |
|
Calculate date difference to see if tracker item is new or not | |
In this use case we compare a field that contains creation date to the actual date and if the difference is lower than 7 days (604800) the field will have the value "new" else "notnew". Copy to clipboard
|
Using Concat to create a reference made of month and string value from other fields | |
In this use case we concatenate the month as word from a date field and a value from a text field (can be a dropdown, etc) and we use to create a reference for this item. Then the result, the value, can be used for many things like populating an itemList field, filtering, searching, etc. Copy to clipboard
For example it will set the value of field with : "Credit | February" |
Add different values from fields from a Tracker in another Tracker | |
In this use case we have 2 trackers. One that records time spent (permaname_timeSpent) with individuals for different interaction with an individual (tracker actions) that contain an itemLink to the other tracker. And the other tracker (tracker users) that will collect all the actions and the time spent for this user and calculate (display) the total time spent per type of actions. We first use an itemList (permaname_listActionsAndTime) to filter all the actions made for this specific user as you will usually do : ItemList => TrackerB, TrackerB_usernameField, TrackerA_usernameField, field to display : Actions and Time, use the format that fit the fields order to display and easy to read list of actions|time. You should have a list displaying in each tracker user items something like that: Copy to clipboard
We want to see the the time spent with the user so we will use a condition (if) based on the string corresponding to the action and a OR statement to collect the time spent if the permanameActions equals a specific string.
Copy to clipboard
|
Assign a label to a range of values | |
In this case we combine less-than and more-than in a cascade of conditions to determine what value (a label) should be saved in the field based on a numerical value from another existing field. For example we have appartments and a field with their surface and we want to group them by size (less than 80m2, between 80m2 and 120m2, etc...) If used in a search (customSearch, pluginList filter, etc) the label must be unique; Tiki search engine (tested with MySQL search) will consider characters like "_", "-" , "+" (may be more) as separators and not character of the string. For example if you search for "20" both "10-20" and "20-30" will be outputted in the results. Copy to clipboard
Copy to clipboard
|
Simple Wiki Ratings | |
|
Related | |
alias
Advanced+Rating | AdvancedRating | Advanced Ratings | AdvancedRatings
|