Enhance module to “force” user to select a group upon registration

esadot - September 21, 2006 - 23:18
Project:Organic Groups Mandatory Group
Version:4.7.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

With reference to: http://drupal.org/node/84239

Rational: "force" user to select a group when register.

Suggested implementation:
1. add next line to og_user function (group.module file) within case 'register' just before return $form;:

<?php
$form
['og_register']['og_register']['#validate'] = array('og_mandatory_group_registration_validate' =>
array());
?>

2. Add next function to og_mandatory_group.module
<?php
function og_mandatory_group_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");
  }
}
?>

3. GIU enhancement is needed to accommodate the new mode. Below list is a suggestion:
a. default group (the current)
b. at least one group should be selected
c. mixed of the above (i.e. at least one group, but the admin could set up a default group)

#1

pwolanin - September 22, 2006 - 01:52

Ok, to clarify, if you want to implement these features via og_mandatory_group, then all the code additions will only be to this module (none to og itself). So you need to add the validation funtion to the form, maybe like follows (I'm not sure if this will work):

<?php
function og_mandatory_group_user($op, &$edit, &$account, $category = NULL) {
  if (!
module_exist('og')) {
    return;
  }
  switch (
$op) {
    case
'register':
         return
$form['og_register']['og_register']['#validate'] = array(
           
'og_mandatory_group_registration_validate' => array());
      break;
    case
'insert'//existing code follows
?>

#2

pwolanin - September 23, 2006 - 00:31

from e-mail:

Emek Sadot

OK, makes sense.
The thing is that $form is empty at og_mandatory_group_user (works ok at og_user). any ideas?

From my look at the hook_user API, $form will be empty. The $form that is returned is recursively merged with the existing $form array.

did you try this and it doesn't work?

#3

esadot - September 23, 2006 - 03:35

Sorry to say, it doesn’t.

The $form carries entire data in og_user function, however it's empty at og_mandatory_group_user. I guess I’d expected the $form to have the same "behavior". Am I missing something?

This is the new, not-working, code I merged (in bold):

function og_mandatory_group_user($op, &$edit, &$account, $category = NULL) {

if(!module_exist('og')){
return;
}

switch ($op) {

case 'register':
$form['og_register']['og_register']['#validate'] = array('og_mandatory_group_registration_validate' => array());
break;

case 'insert':


function og_mandatory_group_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");
}

#4

esadot - September 23, 2006 - 03:40

Clarification for the above comment: og_mandatory_group_registration_validate function is not even reached.

#5

pwolanin - September 23, 2006 - 03:49

do you return $form (as in#1 above)?

#6

esadot - September 23, 2006 - 16:42

Yes I did. It didn't work.

#7

pwolanin - September 23, 2006 - 21:45
Status:active» needs work

I didn't pay close attention to the above- you have the wrong definition for the validate function. Look back at the code I have in the forum topic and compare.

Anyhow, an initial patch is attached. It adds the validation function and forces at least one group to be choosen. Of course, this doesn't take into account that there may be zero groups in the registration form. Also, there is some funky code in og.module, which is why i had to put the "unset" in there. Will file an issue against OG.

AttachmentSize
require_num_groups_1.diff 1.37 KB

#8

pwolanin - September 23, 2006 - 22:39

oops- don't use that pathc- it overrides all the other validation for the registration for.

Just found one easier way- implement the "validate" op of hook_user.

#9

pwolanin - September 23, 2006 - 22:58

try this instead

AttachmentSize
require_num_groups_2.diff 3.67 KB

#10

esadot - September 23, 2006 - 23:05

I omitted the $form_values, $form from the validation function definition as in og module it pop ups some alarms. Anyway, it’s nice to see that we are making a progress.

I checked the patch and it seems to work.
I added check for zero groups, and zero groups at the registration form. It seems to work as well (in bold).

case 'register':

$group = db_fetch_object(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'))));
if ($group != 0) {

$form['#validate'] = array('og_mandatory_group_registration_validate' => array());
$form['og_register']['minimum'] = array ('#value' => t('You must join at least one group.'),);
return $form;
}
break;

#11

esadot - September 23, 2006 - 23:06

oops, just noticed patch #2. I tested #1.

#12

esadot - September 23, 2006 - 23:29

I’ve tested patch #2. Seems to work. I added non-groups and empty groups at registration form checks.

<?php
case 'register':
     
$group = db_fetch_object(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'))));
      if (
$group != 0) {
       
$form['og_register']['minimum'] = array ('#value' => t('You must join at least one group.'),);
        return
$form;
      }
      break;
?>

#13

pwolanin - September 23, 2006 - 23:46

I don't really think you need the query, the unset() deals with the bug in og.

Issue filed here against OG: http://drupal.org/node/85724

#14

esadot - September 24, 2006 - 00:17

I believe you need the query to avoid the wording instruction “You must join at least one group." on the screen. If you won’t query, you’ll get the wording though no group is populated at the registration form.

#15

pwolanin - September 24, 2006 - 01:03

Oh yes, I see now. My approach for that would be to count the number of checkboxes in the form. However, to make that work, I think you'd need to implement hook_form_alter, rather than using hook_user('register').

#16

esadot - September 24, 2006 - 03:44

Sure, but as we have a working code in place, I rather keep it.

#17

pwolanin - September 25, 2006 - 03:04
Status:needs work» needs review

attached is a patch I'm fairly happy with vs. the 4.7 version.

note that there are some non-substantive formatting changes here as well.

added hook_form_alter, plus put a new check box in hook_settings

please test this out.

AttachmentSize
require_num_groups_3.diff.txt 6.07 KB

#18

pwolanin - September 25, 2006 - 03:29
Status:needs review» needs work

hmmm, after thinking more, I'm not really satisfied with this patch- if the mandatory group is in the form, its checkbox needs to be disabled.

The only way to do this (I think) is to use hook_form_alter to take it out of the options for the checkboxes and make it a separate checkbox element with the 'disabled' attribute. The advantage of this, of course, is that the counting of the other groups gets much simplified. However, some extra work may have to be done in hook_user.

#19

esadot - September 25, 2006 - 16:22

1. Well … if the mandatory group is in the form, I’d recommend its checkbox to be checked without the user ability to clear it.

2. The “Require new users to join at least one group in addition to any mandatory group” checkbox / feature doesn’t work when mandatory group (at setting) is NOT selected.
I believe two changes are needed:
a. following code in og_mandatory_group_form_alter ($form_id, &$form) function might be nested one unnecessary level, and $form['og_mandatory_in_form'] should not be cleared.

<?php
$form
['og_mandatory_in_form'] = array('#type' => 'value', '#value' => FALSE,);
           if (
$group_count > 0 && variable_get('og_mandatory_additional_group', FALSE)) {
            
$form['og_register']['minimum'] = array ('#value' => t('You must join at least one group.'),);
           }
?>

I believe it should be:

<?php
/**
* Implementation of hook_form_alter
*
*/
function og_mandatory_group_form_alter ($form_id, &$form) {
  if (
$form_id == 'user_register') {
    unset(
$form['og_register']['og_register']['#options'][0]); //temporary fix
   
   
$group_count = count($form['og_register']['og_register']['#options']);
    if (
$mandatory_group = variable_get('og_mandatory_group', 0)) {
       if (
in_array($mandatory_group, array_keys($form['og_register']['og_register']['#options']))) {
        
$form['og_register']['og_register']['#options'][$mandatory_group] .= ' ('. t('This group is mandatory'). ')';
        
$form['og_register']['og_register']['#default_value'] = array($mandatory_group);
        
$form['og_mandatory_in_form'] = array('#type' => 'value', '#value' => $mandatory_group,);
         if (
$group_count > 1 && variable_get('og_mandatory_additional_group', FALSE)) {
          
$form['og_register']['minimum'] = array ('#value' => t('You must join at least one additional (non-mandatory) group.'),);
         }
       }
    }
   
// REMOVED $form['og_mandatory_in_form'] = array('#type' => 'value', '#value' => FALSE,);
    // EXTRACT FROM ABOVE IF
   
if ($group_count > 0 && variable_get('og_mandatory_additional_group', FALSE)) {
     
$form['og_register']['minimum'] = array ('#value' => t('You must join at least one group.'),);
    }
  }
}
?>

b. Next code (in validate case of og_mandatory_group_user function) is not been reached, which allow the user to register even though a group was not selected (and mandatory group in NOT selected at setting).

<?php
if (count($edit['og_register']) > 0 && count(array_filter($edit['og_register'])) < 1) {
           
form_set_error('og_register', "You must join at least one group");
          }
?>

3. I think that we should offer two modes, and consider if a third one is necessary:
a. only mandatory group (already supported).
b. only join-at-least-one (need above bug fix).
c. mixed (personally, I’m not sure if it will have a demand).

4. In setting: if mandatory group is selected, then it’s not possible to disable the mandatory group, and only enable the “Require new users to join at least one group in addition to any mandatory group” without going through the “reset to default”

#20

pwolanin - September 26, 2006 - 01:16
Status:needs work» needs review

mcuh improved behavior in attached patch. Settings now include none as an option for the mandatory group. Also, the mandatory group appears as a selected, disabled checkbox in the registration form.

AttachmentSize
require_num_groups_20.diff.txt 7.35 KB

#21

esadot - September 26, 2006 - 14:33

The “none” is a very good option, however I suggest separating the mandatory group and join-at-least-one to two distinct configuration blocks. As it stands, the “none” looks as it pertains to the “Require new users to join …” checkbox as well.
May I suggest the following code at og_mandatory_group_settings():

<?php
 
if (count($options)) {
   
$options = array_merge(array(0 => theme('placeholder', t('none'))), $options);
   
$form['og_mandatory_group_setting_block'] = array(
     
'#type' => 'fieldset', '#title' => t('Mandatory Group settings'),
     
'#collapsible' => FALSE, '#collapsed' => False
   
);
   
$form['og_mandatory_group_setting_block']['og_mandatory_group'] = array(
     
'#type' => 'radios',
     
'#options' => $options,
     
'#default_value' => variable_get('og_mandatory_group', 0),
    );
  }
 
$form['join_at_least_one_group_setting_block'] = array(
   
'#type' => 'fieldset', '#title' => t('Join at least on Group settings'),
   
'#collapsible' => FALSE, '#collapsed' => False
 
);
 
$form['join_at_least_one_group_setting_block']['og_mandatory_additional_group'] = array(
     
'#type' => 'checkbox',
     
'#title' => t('Require new users to join at least one group in addition to any mandatory group'),
     
'#default_value' => variable_get('og_mandatory_additional_group', FALSE),
    );
  return
$form;
?>

Second, it seems that the mandatory group is not working. The “if” condition at og_mandatory_group_form_alter () does not yield true

<?php
if (in_array($mandatory_group, array_keys($form['og_register']['og_register']['#options']))) {
?>

#22

pwolanin - September 26, 2006 - 15:51

yes, I agree about there needing to be some visual separation of the different settings.

#23

pwolanin - September 27, 2006 - 01:20

ok, I think I was messing up the group options by using array_merge. Try out the attached patch.

AttachmentSize
require_num_groups_22.diff.txt 7.77 KB

#24

pwolanin - September 27, 2006 - 01:21

see also this additioanl bug report for OG: http://drupal.org/node/86011

#25

esadot - September 27, 2006 - 04:04

O yeah. Looks much better, though it’s kind of late now, and I haven’t had the chance to thoroughly test it. But, at first glace, it looks very good.

One feel&look issue: the checked checkbox of the mandatory group at registration form is too mild, hardly can be seen. I tested in all four themes that comes with Drupal, and a few others, all of which the checkbox can hardly been seen as checked. It would be nice if there is an option to “bold” the "v" sign.

#26

pwolanin - September 27, 2006 - 14:07

I think the grayed-out check is the standard way the form elements look when they are disabled.

#27

esadot - September 27, 2006 - 23:04

Looks good to me.
Please let me know upon upgrading the module, as I like to use this feature.

#28

pwolanin - September 28, 2006 - 01:49

Well, since you've applied the patch, you can use it now! i'll probably do a little more testing before I actually commit the changes. If you have any suggestings for updating the README file to explain the new features, that would be very helpful.

#29

esadot - September 28, 2006 - 02:55

Ok, 1 2 3, testing, testing …

The organic group mandatory module is happy to announce a new feature; supports an option which obliged the end user to join at least one group during registration.

Usage: say you’ve established moderated groups, and populate them into the registration form: if you want to force a new user to join at least one group, then this is your lucky day.

#30

pwolanin - October 5, 2006 - 02:05

here's patch with an updated README file

AttachmentSize
require_num_groups_30.diff.txt 9.05 KB

#31

pwolanin - October 6, 2006 - 02:59
Status:needs review» reviewed & tested by the community

any last feedback?

#32

esadot - October 6, 2006 - 14:55

Yes - thanks you very much :)
I’m content.

Emek
http://www.sadot.tv

#33

pwolanin - October 6, 2006 - 18:11
Status:reviewed & tested by the community» closed

committed the last patch.

 
 

Drupal is a registered trademark of Dries Buytaert.