Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
82 views
ago in Q2A Core by
edited ago by

I know this has been asked several times in one form or another, but after a LOT of searching, I have not been able to find an answer. There were a few answers that pointed to the php code itself, or to the points page in Admin. But, none explained how they are calculated and processed by the script. In other scripts that track points (or other user log activities), they are generally handled with two DB tables and a Cron job scheduler.

1. Daily running table (TABLE 1) that tracts individual activities similar to the following :

USERID REASON POINTS
user1         aupvote 1
user2 cpost 5
user1 aselected 2
user1 cdownvote -1
user3 bonus 50

This table could have thousands of records. 

2. Second Table is the main points summary table (call it qa_userpoints)

At the end of each day a Cron is run on table 1 that tabulates the data and inserts that into table 2. Table one is then emptied and a new day starts. All stats shown to users is taken from table 2. I am aware the following under this scenario:

A. Points that users see would be as of the prior day.

B. When the point values are updated, the previous amounts earned would not change. They only change moving forward.  


I am not sure how the current system works. I see point values in several tables, a Recalculate button on the points page, and a plugin called Point Recalculator. This is not to mention 3rd party plugins all using their own tables. 

What I am looking to understand is the current flow on how points are calculated. I am not even sure how it is possible without using Cron jobs unless the script is looking at (and calculating) all the various tables and data in real-time.

Another question: 

1. User1's current point total is 196 (in qa_userpoints)

2. User1 asks a question that gives him 5 points

It looks like this increases the qposts number by 1. So how does this translate to an additional 5 points in the user's total points when viewing it on the site?

or

1. User1's current point total is 196 (in qa_userpoints)

2. User1 pins a post with a 3rd party plugin that costs him 50 points

How does this translate to a reduction of 50 points to the user's total points? (I understand that different plugins handle this differently, but they still need to add or subtract from the points somehow).

I know from reading other questions regarding points both here on Q2A and on Github, that this is a touchy subject for the contributors. I am just trying to understand the thought process here since I am seeing some weirdness with the point totals.

Thanks in advance!

Q2A version: 1.8.8

1 Answer

+5 votes
ago by
selected ago by
 
Best answer

From a bird's eye view, the business logic is extremely simple:

  1. Admins configure how many points some actions or triggers give (e.g. voting questions, getting answers selected, posting, etc)
  2. Users perform actions that execute these steps:
    1. Increase counters for the given user for the event types mentioned above
    2. Recalculate points for the given user as it is just multiplying several values by the amounts set in step 1

I see point values in several tables

I can't speak for third party plugins, but the points for each user are stored in ^userpoints table, points field. The rest of the fields in that table are the counters I mentioned above.

So how does this translate to an additional 5 points in the user's total points when viewing it on the site?

You've configured posting a question to give 5 points. If X is the amount of questions posted by the user, then posting a new question would mean moving from the first equation, to the second (note Y is the result of calculating all the rest of the point modifiers):

(5 * X) + Y = 196

(5 * (X + 1)) + Y = 201

How does this translate to a reduction of 50 points to the user's total points?

It is just a matter of hooking in the recalculation process, which would translate to something similar as adding more terms to the equation. Imagine Z amount of pins, pinning something would mean moving from the first equation to the second one in this way:

(5 * X) + Y + (-50 * Z) = 196

(5 * X) + Y + (-50 * (Z + 1)) = 146

Q2A doesn't make it as simple to hook into that equation, and it makes a bit less efficient than it could. So I suggested several years ago to improve this. Here is the pull request: https://github.com/q2a/question2answer/pull/486

I am seeing some weirdness with the point totals

If you can provide step by step instructions to replicate the weirdness, I'd be happy to take a look at it. At the end of the day, as Edsger Dijkstra said, "If debugging is the process of removing software bugs, then programming must be the process of putting them in".

ago by
Thank you for the thoughtful response!
ago by
@pupi1985. After I update a point value, what is the order of recalculation? I am guessing that I press the Save and Recalculate button (Admin > Points), then press the Recalculate button (Admin > Plugins > Point Recalculator). I feel pretty confident on this...but I am not sure what the Recalculate user points button in the Admin > Stats does. It is under the Database clean-up operations section.

And if I run this operation, should I again press the Recalculate button in the Point Recalculator plugin?

I know that have pretty much beat this dead horse. :)
ago by
+1
Think of this equation:
(5 * X) + Y = 196

The 196 is the theoretical value and there should be a 196 in the field `points` for the given user.

Updating values in the Admin > Points page would mean changing the equation to, for example:

(10 * X) + Y = 250

The 250 is a bit arbitrary here. The important thing is that the theoretical 250 will be calculated whenever a point refresh happens. Which means the current `points` field for the user is still 196. In order to synchronize the theoretical value with the one in the database, it has to be refreshed.

In order to refresh it, you need to execute one of the actions that trigger point refreshes (e.g. voting, posting, etc). However, in order to avoid having to look for all users and upvote and downvote their points, Q2A provides a way to trigger a point refresh for all users. That's the recalculation process.

Running this operation will run the standard equation, ignoring plugins:

(10 * X) + Y = 250

So you should run the Point Recalculator afterwards as well to complement the original equation with the extended one:

(10 * X) + Y + (-50 * Z) = 200

So the new 200 theoretical value is set to the points table by the plugin.
ago by
I had to read your response a few times, but I got it now! Thank you. The last remaining question is :

What does the "Recalculate user points" button in the Admin > Stats do? It is under the Database clean-up operations section. Is it the same as the Save and Recalculate button (Admin > Points)? Or something non-related to refreshing the points?
...