Blog of Attomsoft Inc.
Drupal
Hacking Drupal usage and coding
How to build a stackoverflow or Yahoo! Answers like system using Drupal (Part2)
Apr 26th
We’ll create the two content types which will be used by our Q&A system in today’s part-2.
The Questions content-type fields:
- Question title(Module form [title])
- Question description(Module form [body])
- Disable comment option if you don’t want users comment on the question itself.
- Points: field_question_points, type[integer], default value[0], set as required, minimum value also as 0
- Answers: field_question_answers, type[nodereference count], set as required, the “nodereference field that can be counted” option should be set to the field name that we’ll create it in “Answers” content-type in step 2. (So you could just process #5 field)
- Best answer: field_question_best_answer, type[nodereference]. Save the content type here and we will refine it later.
The Answers content-type fields:
- Set “Automatic title generation” option to “Automatically generate the title and hide the title field”, you can set the automated title pattern using tokens available there.
- Disable the comment option if you don’t want user comment on answers themselves. Or leave it R/W just like Stackoverflow.
- Target Question field: type[nodereference][nodereference from url], select our previously created ‘Questions’ type for referring.
- Selected Answer field: type[integer][Single on/off checkbox], set allowed values as ’0|No’ and ’1|Yes’ (that’s separated lines)
Here, we’ll go back to the “Questions” type and re-configure it:
- The “nodereference field that can be counted” of the “Answers” field should be set to our just created “Target Question” field.
- The “Content types that can be referenced” of the “Best answer” field should be set to our just created “Answers” type.
If you want to allow free tagging feature, here’s how:
- Create new vocabulary named “Tags” or something you prefer.
- Select the content types we just created which will allow using terms below this vocabulary and its free tagging feature.
- Enable the “Tags” option for this vocabulary to allow users free tagging
That’s all basic content types we needed for our Q&A system, we’ll build the needed views and panels in our next part tutorial.
See ya.
How to build a stackoverflow or Yahoo! Answers like system using Drupal (Part1)
Apr 23rd
Do you believe that? Stackoverflow or Yahoo! Answers interactive sites will replace old forums. Because users on most interactive sites treat finding solutions or help people as their daily job, traditional forum style systems are good at entertaining users, but not solution oriented.
From this post start, I’ll tell you how to build a stackoverflow or Yahoo! Answers like system using Drupal only, on this topic completion, we will have a Question&Answer system with following features:
- Users can post and answer question with granted access; (ACL)
- Every question can be assigned “Points” by its author, and the points will be granted to the author of the best question which picked up by the author of question; (Awarding)
- On the questions list page, users can see how many answers have been posted to single question, and whether the best answer has been picked up;
- On the question detail page, users can see the question itself and information including tags, points, authors, posted date, and all answers following the question;
- On the question detail page, if the best answer has been picked up, the question will be marked as “Closed” and the best answer will be on the top of the answers list and marked as “Best Answer”;
- If the author of a question had picked up the best answer, other users will not be able to post answers anymore, and the author himself will not be able to pick another answer as the best;
The following modules will be used by this little project:
- Drupal (itself, of cause)
- CCK (with build-in plugins and additional nodereference and the nodereference_url widget)
- Nodereference Count module
- Automatic nodetitles
- Views
- Panels
- Userpoints
- Our custom Q&A module
We’ve set our gold and list tools we’ll need later, we’ll start the building this Q&A system from next part of this topic.
See ya.
Nodereference field from custom url in Panels pane
Apr 20th
Nodereference field is a powerful CCK field type for relationship handling for Drupal, the nodereference_url module is a plugin supplying it with referenced nid from url path. I use them a lot in daily Drupal based solutions.
Nodereference Url works under path like node/add/NODETYPE/NODE_ID situation by default, it’s sufficient for normal problem such as “Post reply”, “Add comment”, etc. You just need to build the link with proper NODE_ID.
One of our projects need to build a system like “StackOverflow” with simple Question&Answers workflow. I created “Question” type and “Answer” type which has a filed referring to the “Question” type, then I used Panels to integrate single “Question” node with its related “Answer”s node.
Everything works as will, until I tried to add a “Answer Add Form” below the created panel page, the form displayed but the field referring “Question” node gave out “Referenced content not found” error.
I could find out why, until I read the document of Nodereference URL module carefully.
The reason why above solution not worked is that, the Nodereference URL module supports /add/NODETYPE/NODE_ID by default, and that’s the only pattern it works on.
At last, the solution laid just in the README.txt file with the module itself. Silly me!
It said:
By default Node Reference URL Widget will only work with node form paths that
match the standard Drupal install: "node/add/%type", where %type is a node type
like "blog" or "story". If you want to use Node Reference URL Widget on
non-standard URLs, you may do so by informing Node Reference URL Widget of these
special paths.
To do so, add additional paths to your settings.php with the following code:
$conf['nodereference_url_paths'] = array(
'node/add/%type/%nid',
'node/%/add/%type/%nid',
);
So, remained work just as easy as put above lines of code to my settings.php. And then, magic happens.
Drupal Views slideshow not work?
Apr 16th
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
Apr 16th
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.
Multiple Axis Voting System using Drupal FiveStar and Computed Field
Apr 11th
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.
- 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.
- Setup the targeted node type, enable fivestar rating on it but disable comment to prevent users comment on it directly.
- Add a node reference field to the voting node type, make the targeted node type selected.
- 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.
- Then add another fivestar field named something like “overall” or “summary”, and set its target to #3 field.
- 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.
- 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
Apr 4th
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:
- Install <Userpoints> and <Userpoints Node and Comments> modules and enable them;
- Configure “Userpoints Settings” as desired, such as branding, auto approve, node type points, comments points, etc.;
- 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:
- Install UP and Ruels modules, enable above modules all.(Note, Rules needs Token module);
- Enable Rules integration sub-module packed with UP core;
- Settings Rules on specific node events and actions appropriately(This heavily depends on skills on Rules module, learn it from Rules documentation);
- 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
Apr 2nd
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
Mar 28th
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.
- Create a new comment module using VotingAPI, make it support multiple axis voting
- Using FiveStars module, add new voting node type, refer multiple FS-fields to the node type, customize the display using templates or Views
- 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.
Url redirect made easy: Drupal Path Redirect Module
Mar 24th
The first module came to my face was Gotwo module, which looks like could feed my simple need: redirect customized Drupal path to other url.
I tried the Gotwo module and it worked, although not impressively.
Two days later, I had to change some of the destination urls I’d created using Gotwo, and I found there is no way to do this, definitely no way to edit or modify the paths added before, only two one options: delete the old one, create the new one. Big loser. ah
Then I did some search, and found Path Redirect, looks great. I tried it and it worked, no drawback.
You can create redirect path, edit them, remove them, and better, you could set the server response code , that’s amazing.
Great module.
BTW: looks like D7 will support url redirect in the core, that’s a good news.