Drupal

Hacking Drupal usage and coding

Drupal Views slideshow not work?

Views Slideshow Module is great for building slideshow based on Views, although you need some work on its them.

I installed and configured followed documents from Drupal.org, but what I got was just a normal view without any slide effect.

After several hours searching and reading, finally I found the reason: Devel module.

It’s ridiculous that Devel module involves lots of trouble to me, most are conflicts with other modules. So for development site, just disable it, but for productive site, never enable it, it’s really boring.

Devel module exists for good reason: development, but just for that.

Rewarding your user on varies actions using Drupal Rules

Drupal Rules module is the perfect replacement for the core triggers and actions, it’s very powerful and extensible.

Today, I’ll try to show you how to build a user rewarding mechanism using Rules and Userpoints modules.

Before we step forward, you may check my old post about this here.

After reading the linked post above, I’ll make this tutorial straightforward.

#1: Reward the new registered user.

Triggered on: User account has been created.

Conditions:

1. User has role(s): set which roles you want to reward on register;

2.Compared two users: make sure the user is not created by site administers; (You may ignore this for test)

Actions: Grant userpoints to the registered user. (Not the acting user)

That’s our first reward rule based on Rules and Userpoints modules, easy, right?

#2: Reward user on every login

Triggered on: User has logged in.

Conditions:

1. User has role(s): set which roles you want to reward on register;

2.PHP code: return intval((time() – $account->access)/60) > 90; (That makes our reward only works if the user login interval more than 1.5 hours, you could set it what you want)

Actions: Grant userpoints to the logged in user.

That’s it.

If you want to add more limits on this rules, you need more conditions.

I’ve tested above rules on a new Drupal based community site, they work fluently. You can create your own reward rules based on this easily.

http://blog.attomsoft.com/?p=105

Multiple Axis Voting System using Drupal FiveStar and Computed Field

Multiple axis voting system could be done using Fivestar and Node Comment, detail goes here

I’ve tried several ways to find out which is the best, see this post.

I was trying to setup a 5-axis voting system target to a node type, and display the voting result in the targeted node view, not only single axis, but also total average result based on single axis. Here is my  solution for this problem for the productive site.

  1. Fivestar is the core, yes, it’s really doing good job. So install it first, and make sure Views module installed too. You also need node reference cck field plug-in.
  2. Setup the targeted node type, enable fivestar rating on it  but disable comment to prevent users comment on it directly.
  3. Add a node reference field to the voting node type, make the targeted node type selected.
  4. Setup the voting(aka.review) node type, add all needed field and fivestar fields for multiple axis voting. Remember set the “target” option to #3 field.
  5. Then add another fivestar field  named something like “overall” or “summary”, and set its target to #3 field.
  6. Here is the tricky part: add a computed cck field to the node type named “overall process” or something likely, we’ll use it to work out the “overall” voting result on the fly.
  7. In the “computed code” text area of #6 added field, enter codes below:
    $axis_1 = $node->field_axis_1[0]['rating'];
    $axis_2 = $node->field_axis_2[0]['rating'];
    $axis_3 = $node->field_axis_3[0]['rating'];
    //Simple average result based on 3 axises
    $axis_overall = ($axis_1 + $axis_2 + $axis_3)/3;
    //That’s it
    $node->field_axis_overall[0]['rating'] = $axis_overall;

OK, save the node type above. Create one node targeting an existed node, voting on the base axises and save it. Here you should see the “overall” voting result has been set automatically.

In this way, the “overall” field can be used by Views and other modules accessing CCK , and the field could be sued on sort criteria, it’s really important for curious voting site.

For now, that’s the best solution I’ve found out for multi-axis voting, easy and clean.

Build awarding system using Drupal

The most handy awarding system base is using Drupal Userpoints module.

<Userpoints> is a module that provides a user points api which can be used by other modules add or subtract points on user who triggered specific actions. There are lots of contributed mods based on UP that makes configuring actions and triggers easy to use, such as well know “Userpoints Node and Comments(NC)”.

Most simple approach:

Goal: Add points on node publish and subtract points on node deletion.

Solution: Userpoints core and Userpoints NC

Howto:

  1. Install <Userpoints> and <Userpoints Node and Comments> modules and enable them;
  2. Configure “Userpoints Settings” as desired, such as branding, auto approve, node type points, comments points, etc.;
  3. That’s all, now the specified points actions will be performed on actions configured in UP section

Advanced approach:

Goal: Integrated with complex work-flow controls, such as only award users when node changed to specified states.

Solution: Userpoints module, Drupal core Action and Triggers modules and Rules module.

Howto:

  1. Install UP and Ruels modules, enable above modules all.(Note, Rules needs Token module);
  2. Enable Rules integration sub-module packed with UP core;
  3. Settings Rules on specific node events and actions appropriately(This heavily depends on skills on Rules module, learn it from Rules documentation);
  4. By using UP and Rules modules, any complex user points rules can be set, such as “Register Award”, “Rating Award”, “View Award”, etc. The sky is your limit.

What’s the difference?

The simple way is handy but not good at complex situations, good for simple award mechanism like comment, forum, blog, etc. But the latter way is more extensible and good at very complex situations. If you want to build very large community with awarding system, that’s solution.

Drupal tricks: Display forum top level description on forums list page

Do you ever want to display the top level description of the ‘forum’ vocabulary on the forums list page? Maybe not.

I was trying to do this last few hours by Googling and search Drupal forums, but got nothing valuable for this problem. I think most people don’t need this solution cause they have a easy way or they never thought there is need to do this.

Anyway, I need it so I hacking it out.

Here we go:

For Durpal6 Forum module, the default <template_preprocess_forums> doesn’t give the top level description data out, and most of contributed modules don’t either. They just display descriptions of low level forums or sections, but never the top level.

Some guys on Drupal forum gave out very complicated solutions counting tens lines of code, really bad idea. I bet that’s not the Drupal way.

The most important thing you need to know about the top level forum ‘vocabulary’ is that it does not have a ‘tid’, but has a ‘vid’.

So the solution goes to find out the ‘vid’ of the top level forum vocabulary. Check here to see the API detail. It’s the default forum list preprocess function, you can use these two lines to get the ‘vid’:

 $vid = variable_get('forum_nav_vocabulary', '');
  $vocabulary = taxonomy_vocabulary_load($vid);

Attention:taxonomy_vocabulary_load is new to D6, not D5.

Put the code above in yourtheme_preprocess_forums function, and then you can set the top level forum description with this: $variables['forum_description'] = $vocabulary->description;

Of cause, you can condition this variable only display on the root page, not sections or forums page.

Ways to add multiple axis voting to Drupal node

VotingAPI is great module, but just for Drupal module developers.

I spent 6 hours on finding a way to add multiple axis voting functions to a Drupal node, here are the solutions I’ve found.

  1. Create a new comment module using VotingAPI, make it support multiple axis voting
  2. Using FiveStars module, add new voting node type, refer multiple FS-fields to the node type, customize the display using templates or Views
  3. Just using CCK module, add new voting node type with multiple integer fields as voting axis, using VotingAPI to save them to votes table or just save them to node table and display them in node

I found the #2 way is the easiest to do such thing, but lack some flexibility:

  • FiveStars based solution needs a separate node type which needs to be attached to the node type you wall users vote on, you have to implement the voting even comment form to display these two different node types together
  • Multiple based on FiveStars field are based on voteing “tag”, they are totally separated data fields,   no total average available, so you can’t find the “summary average”
  • You also need to design views to display node, comments, votes or voting form together, that’s not easy

But, you just need some time to do all things above to get what you want, so it’s my last choice.

What’s yours? Show me.