Hi,

I’d like to force my new users to select a group during the registration process. Groups are advertised at the registration form, and I need the registration “submit” button to be inactive until the user select (at least) one group.

og_mandatory_group module is not an adequate solution here, since I don’t have a need for one default group.

Any advice would be welcome

Thanks in advance

Comments

bonobo’s picture

I don't know off the top of my head if you can *force* a user to choose a group, but you can restrict the ability to create content outside of groups.

Cheers,

Bill

-------
http://www.funnymonkey.com
Tools for Teachers

esadot’s picture

Appreciated Bill, but my site interest lies in having users choosing the *right* group. Having a user register without choosing a group misses the all purpose.

Again, thank for the tip.

bonobo’s picture

In thinking about this a little more, you might want to think about extending the functionality of the existing og_mandatory_group module, as this comes the closest to matching the functionality you want --

I don't think this is something that can be accomplished (or even kludged) as you describe without writing some additional code.

Cheers,

Bill

-------
http://www.funnymonkey.com
Tools for Teachers

esadot’s picture

Again, thank you Bill.

Yes, that was my thought initially, to work on the og_mandatory_group module as it seems as the best fit (particularly, as the name accommodate what I want to achieve 

My coding skills are rusty, so I thought to seek advice first.

vit137’s picture

... but while it seems a simple thing to restrict the ability to create content outside of groups, I can't find any description of how to do this. Can you point me in the right direction?

NEVERMIND.... I think I figured it out.

Gary Feldman’s picture

When you say you need the submit button to be inactive until a group is chosen, doesn't that imply a JavaScript solution on the client, instead of a PHP/Drupal solution on the server? You might still have to do something in the Drupal theme to associate an event handler with each of the group selection boxes, as well as loading the .js (if you go down that path).

Gary Feldman

esadot’s picture

Thanks Gary, the “inactive” was only a figure of speech (and I apologize for stealing Cpu cycles for that). I meant that the registration will fail if the user had not selected a group. That could be done (and probably should be done) in the same manner it fails today when the user omits the username or email address.

Thank you.

Gary Feldman’s picture

Well, I can't give you a complete answer for implementing it that way, but I'd start by looking at the _validate hooks.

Gary Feldman

esadot’s picture

Thank you Gary, I definitely will take a look.

pwolanin’s picture

Well, if og_mandatory_group isn't doing everything, sounds like (as suggested above) you need to do an extra validation on the form. Use hook_form_alter to add an extra validation function onto the user registration form and then set a form_error if no groups are checked. Probalby niceer if also you mention somewhere that a group selection is required....

---
Work: BioRAFT

esadot’s picture

Thanks.

I guess I can handle the hook_form_alter function, and the corresponded error message.

On the other hand, I have no idea where to start with the actual validation - any idea / directives as of where to look, and what to look for, in order to confirm that at least one group is checked at the registration form?

Thanks in advance

pwolanin’s picture

Looks like here's where the relevant part of the registration form is defined:

function og_user($op, $edit, &$account, $category = NULL) {
  global $user;

  switch ($op) {
    case 'register':
      $options = array();
      $values = array();
      $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE n.type IN ('. str_pad('', count(variable_get('og_node_types', array('og'))) * 5 - 1, "'%s',") .') AND o.register = 1 ORDER BY n.title'), variable_get('og_node_types', array('og')));
      while ($group = db_fetch_object($result)) {
        $options[$group->nid] = t('Subscribe to %name.', array('%name' => theme('placeholder', $group->title)));
        $values[$group->nid] = in_array($group->nid, (array) $edit['og_register']) ? $group->nid : 0;
        if ($group->selective) {
          $options[$group->nid] .= ' '. theme('placeholder', t('(approval needed)'));
        }
      }
      if (count($options)) {
        $form['og_register'] = array('#type' => 'fieldset', '#title' => t('Groups'));
        $form['og_register']['og_register'] = array('#type' => 'checkboxes', '#options' => $options, '#default_value' => $values);
        return $form;
        }
      break;

So i'd think your validation function would need to look something like (this is totally untested):

function mymodule_registration_validate($form_id, $form_values, $form) {
  if (isset($form['og_register'])) {
    if (count($form_values['og_register']) < 1) {
      form_set_error('og_register',"You must join at least one group");
    }
  }
}

---
Work: BioRAFT

esadot’s picture

Thanks.
How do I invoke the mymodule_registration_validate function?

pwolanin’s picture

You must implement either hook_form_alter or hook_user, and then add the validation function to the form. See the Froms API reference:

http://api.drupal.org/api/4.7/file/developer/topics/forms_api_reference....

here are a couple examples (line number after module name):

modules/node.module:2215:
$form['#validate']['node_search_validate'] = array();

modules/system.module:78:

$type['date'] = array(
  '#input' => TRUE, 
  '#process' => array('expand_date' => array()), 
  '#validate' => array('date_validate' => array()));

So you want to do something like this:

$form['og_register']['og_register']['#validate'] = array('mymodule_registration_validate' => array());

---
Work: BioRAFT

esadot’s picture

Thanks a million. It seems to work, at least at a first glance.
I worked at the og.module, although I think that og_mandatory_group module would be the best candidate.

First let see if it looks ok to the experts.

1. I added the validation function to the registration form at the end of the “registration” case in the og_user function.

$form['og_register']['og_register']['#validate'] = array('og_registration_validate' => array());

2. I added the actual validation function at the button of og.module.

function og_registration_validate($form_id) {
  if (count($form_id['#value']) == 1 && ($form_id['#value'][0] == 1)) {
    form_set_error('og_register',"You must join at least one group");
  }
}
pwolanin’s picture

Ok, that's good to get it working, but not the best idea for the long run. If you make your own (very small) module to do the same work, it'll make you life much easier when OG is updated.

As an alternative, I'm the current maintainer for og_mandatory_group (even though I'm not actually using it at the moment). If you want to put this in as a feature request (plus patch or at least some code) we can work on it together.

---
Work: BioRAFT

esadot’s picture

og_mandatory_group, definitely. It seems like we have a plan in place, great.

May I suggest we work of-line for a while, and post back to the benefit of the community upon having a more solid version.

pwolanin’s picture

Please post a feature request - that's the easiest way for me to track it, though we can also discuss directly:

http://drupal.org/node/add/project_issue/og_mandatory_group/feature

---
Work: BioRAFT

esadot’s picture

meg105’s picture

I can't see the function “force user to select a group upon registration" in version 6.x.
Can anyone point me that how to use it in version 6? or the function has been discarded.

thanks
meg