Posted by satter9 on January 5, 2013 at 4:52pm
Hello,
Does anyone know of a way on which you can assign actions after completing a Quiz? I am trying to allow access to a certain block once a user completes a quiz. I would like to do it by either assigning a new role to the user or automatically re-directing the user to a new page. Is there a way to do this with Rules or some other way. I am new to the Quiz module so maybe there is a way to do this that I am not aware of.
Thanks,
Nils
Comments
Easiest way may be hook_form_alter
Sometimes I find adding modules like rules can be a little 'big' for something that can be done quickly using hooks. In this case you can just use hook_form_alter to add your own submit handler on the quiz node.
function mymodule_form_alter(&$form, &$form_state, $form_id) {switch ($form_id) {
case 'quiz_node_form':
$form['#submit'][] = 'mymodule_quiz_node_submit';
break;
}
}
And then in your submit you can programmatically add a role to the user if it is not already there.
function mymodule_quiz_node_submit() {global $user;
if (!in_array('quiz completed', $user->roles)) {
$rid = array_search('quiz completed', $roles);
if ($rid != FALSE) {
$new_role = $user->roles;
$new_role[$rid] = 'Employer';
user_save($user, array('roles' => $new_role));
}
}
}
Some code for adding role was borrowed from here: http://drupal.org/node/28379
Where would the code go?
Thank you for the input! I am not a coder so the above does not make a lot of sense to me. Would this code be placed in the template.php file or possibly the node.tpl.php for that theme?
Code location
When developing a Drupal site we normally create a module for the site itself which contains any customizations (hooks) for the site. To do this, make a directory in sites/all/modules for your site - I will refer to it as 'mysite'. In that directory create a file called mysite.info with the following:
name = Mysitedescription = Site specific customizations for Mysite.
core = 7.x
Then create a file called mysite.module in the same directory and place the code from my first comment in it. Now you should be able to go to http://mysite/admin/modules and enable your 'mysite' module.
If you start getting deeper into development I would enable the 'devel' module and learn how to use dpm(). The mysite module is a good start to undertanding and unleashing the true power of Drupal.
Good-luck!
Thank You!
Thank you so much! Your comment is really clear and makes a lot of sense. Creating a custom module has always seemed a little scary but I can't wait to give this a try.
Much appreciated!