📜 ⬆️ ⬇️

Intuitive revision of Vote Up / Down and User Karma (joyful emotions of a new drupalist)

image I want to share the joy of the successful work done today (at an intuitive level) to refine the algorithms for calculating karma and user rating in Drupal 6. But first, you need to tell a little about my level of preparation so that it becomes clear how universal API and OOP principles adorn life for people. I hope it will be useful for newbies, like me, and will help them overcome the first difficulties on the way of Drupal development. Who wants to go directly to the essence of the article, scroll to the title "The long-awaited evaluation and karma."



The first acquaintance with web-programming and CMS


')
Just over a year I am closely acquainted with such a concept as CMS. Made a few normal (functional, diverse and loved by users) sites on Joomla, first 1.10, then 1.5 (I haven't gotten to 1.6 yet). In this case, he always used only standard mechanisms. Plug-in things (modules, plug-ins, components) set only in the original version. I allowed my hands only to feel a little the templates (themes) and CSS. Nowhere did not climb further.

I am quite familiar with the basics of OOP, but I almost never came across the web application of these principles. The principles are very similar and understandable. Most of all difficulties, most likely because of a difference of terminology. I was frightened for a long time by the phrase “learn the Drupal API”. Or the word hook - "trap hook". But when it came to the fact that hooks are, in essence, event handlers, it immediately became easier and more understandable.

The peculiar inclusions of the executable code <?php ..... ?> Inside the html-pages are also somehow unusual, as well as the syntax of this new language for me. Well, the geographical separation of your offspring is unusual: scripts and themes in one place, as if by themselves, and the database from which the basic information of the site is formed may be in a completely different country. And from it it is necessary to pull the strings and knit pages. Most of the course is automatically done, but, nevertheless ...

Why I changed Joomla



From laziness of brains and following the principle of a duckling, I tried my best not to get involved with anything other than Joomla. But when it came to creating a collective blog with extensive voting options, I came to the conclusion that Drupal is better suited for this. Or rather, it is perfect, while Joomla doesn’t fit at all (at least, this is my conviction after studying numerous comparisons and descriptions of the connected components to these two CMS).

Acquaintance with something cardinally new, when you feel like a complete zero, a nothingness, killed and crushed by a large amount of incomprehensible information that needs to be processed and driven into the topic ... It's such a thrill. I already have adrenaline gushing in the blood, that I can not sleep at night. (But I do not brag that I do it effectively, but only emotionally).

Along with studying Drupal, in parallel, I began to learn php and get acquainted with jQuery, as well as recall the lessons of SQL. At first it was just a reading of the descriptions of different modules, their alternate connection and testing. And then I began to look into the API, wrote my own module (first by the lesson, and now I finish my original one).


Long-awaited evaluation and karma



Immediately installed Vote Up / Down . Oh, that was what I needed! You can vote for nodes, comments and tags (sections of taxonomy).

Then the task arose to accumulate estimates, merging them into a single figure of the user's rating. The choice fell on User Karma . This module made it possible to ideally embody an idea - to score points according to a certain algorithm, depending on user actions and votes through specified significance coefficients.

For example, according to my algorithm, a bunch of rating consists of such flowers:

Rating (Reputation) = Voices * 3 + VoicesComments * 1 + AdditionNo * 1 + LeftEvaluation * 0.2

You choose the sum amounts yourself, and prescribe the coefficients of their significance with pens. You can also set the number of days after which the estimates will melt (but I felt sorry for the rating, and so tortured inflation in real life).

But since I recently registered on Habré, I wanted to repeat the mechanism of karma, which is located here. As if an indicator of support from the community. How many people for you, and how many - against. Not a vote for some of your material, but a vote for your personality. Everyone can give you either one Plus or Minus. And only once.

A little googling in runet, I did not find the desired solution (for finalizing Vote Up / Down), and decided to understand the structure of the module for educational purposes. And I immediately noticed that sub-modules in the folders vud_node, vud_comment, vud_term are responsible for all types of voting. Having typed vud_user in Google, I found what I needed.

In January of this year, one person raised the question of voting for users , to which the developers of VoteUpDown responded that they would not realize this opportunity for Drupal 6, and for the 7th version they plan to vote for arbitrary fields of materials (as I understood, but not should trust my English.).

In February, one person in the comments posted a sub-module vud_user, as he says, made it from the module vud_comment. I downloaded it and earned it from me, just like karma on Habré, from which I was very happy.

But then my joy gave way to deep thought. After all, according to the logic of the intended Reputation, the voice for the user should also influence it, and very weighty. And for this you need to modify User Karma.

My inner instinct told me that I would hardly find such a refinement (small self-made things), and therefore made a strange move for me, relying only on intuition.

The User Karma module consisted of 4 sub-modules.
user_karma_receive_vote_node // - points when voting for your node
user_karma_receive_vote_comment - vote for your comment
user_karma_insert_node - adding an article
user_karma_give_vote - your evaluation of materials

I picked up the first one, and logically suited my _receive_vote_node and copied all the files associated with this sub-module. And the word node in the names replaced by user.

Going into these files, I made a “Search - Replace” node ---> user.

And installed them on the site. A new sub-module _receive_vote_node has appeared in the modules (admin) section. Turn it on. And in the settings of User Karma, a new addendum for “voting for the user” has appeared. I asked him a factor of 10. Immediately entered the profile of a random user, after memorizing his rating. Voted for him and updated, in anticipation of the growth of the reputation jump by 10.

SQL started using Drupal, that the {user} table does not exist and there are no nid fields in it. I fixed on {users} and uid respectively.

And it all worked for me! Just the way I wanted! Now it is necessary for someone to vote for the user profile, as his rating soars by +10 (or drops by -10).

So let's summarize my karma dr ... experiment.

Vote Up / Down adds to your articles, comments and taxonomy terms buttons for voting, which, through chaos tools through Ajax, effectively work, changing the estimates in the eyes of the user. Vote Up / Down also adds the scores to the database neatly on the shelves (user1 minus such and such usera2 comment in such and such article at 7-30 am, etc.). Then you can use this information, for example, to calculate anonymous minusists, inadequate plusists and normal people, as well as sell offended voting results, which many people have cruelly pumped into karma, and they want to know: “who did it to me so much !? "

User Karma aggregates all scores into a single number that clings to the user. This is about the country's GDP. Uncle Vasya dug 3 bags of potatoes, Abramovich dug 50 tons of diamonds, VAZ collected 100 thousand Kalin, Megaphone lost $ 2 million because of Skype - it would seem so many different numbers, and in the end we get a total figure that sums up all our joint efforts. Russia's GDP in 2010 amounted to X billion rubles. And all this bunch of pros and cons for nodes, comments, potatoes and diamonds, is ascribed to Russia as a result and appears in various reports as an indicator of its success.

Because it was my first attempt at modifying modules, it struck me most of all that I created a whole sub-module with serious customization options, in just a few keystrokes. Given the fact that I am new to these matters, I was overwhelmed with euphoria of some kind that I even wanted to share.

The moral of the story, I think, is this: “This is what the universalization of function names and a well-thought-out API lead to.” In Drupal everything is very strict and thoughtful. Beginners, do not be afraid, learn the logic of the API and will soon be able to work wonders on the fly in web programming.

Given the fact that I myself am a novice in Drupal and on Habré, I will be glad if I don’t have the opportunity to write here, as well as advice to correct my understanding of some things that I don’t understand yet. Thanks for attention.

Well, most of all I dream of making good experienced friends who would help me master Drupal.

Source: https://habr.com/ru/post/116740/


All Articles