Configure og promote on a per content type basis
alex_b - October 19, 2007 - 21:59
| Project: | OG promote |
| Version: | 5.x-1.0 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Description
I would like to define a promote role on a per content type basis. What about adding on admin/og/og_promote above "Groups:" a check box select list "Group types" IF more than 1 group type is enabled.
Here is how the internals would work:
A user would be promoted if the group he joins
* is og_promote enabled
* and/or is of an og_promote enabled group content type
Thoughts?
I am not yet sure, but I might roll a patch for this.

#1
This is a patch for og_promote.module 's DRUPAL-5 branch (couldn't select version in drop down).
It adds the option to chose promote/demote groups by content type. We are about to deploy og_promote with this patch on a production site (Green Media Toolshed).
#2
Remove debug output.
#3
I don´t see any diference, i go to og promete settings and get the same form as the original one, where are the added features?
#4
That sounds strange. Were there any problems with applying the patch?
#5
no, i believe not, it wasn't me who aplyied the patch (i don't know much of linux commands), it was my partner, but i saw while he did it and after he ejecuted the patch it said something like: "file patched" and that's it, right? the file is diferent from the original now. This is my new "patched" og_promote.module file:
<?php
// $Id: og_promote.module,v 1.5 2007/02/19 01:57:53 darrenoh Exp $
/**
* Implementation of hook_help().
*/
function og_promote_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Promote users that join certain groups to a special role.');
case 'admin/settings/og_promote':
return t('Choose one role that all users who join the groups you select will be promoted to. Should a user leave that group, he will be demoted again.');
}
}
/**
* Implementation of hook_menu().
*/
function og_promote_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/og/og_promote',
'title' => t('OG promote'),
'description' => t('Choose a role to which members of selected groups will be promoted.'),
'callback' => 'drupal_get_form',
'callback arguments' => array('og_promote_admin_settings'),
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
}
return $items;
}
/**
* Implementation of hook_og().
*/
function og_promote_og($op, $nid, $uid, $args = array()) {
$type = _og_promote_get_node_type($nid);
if (($role_id = variable_get('og_promote_role', 0)) != 0 && (in_array($nid, variable_get('og_promote_groups', NULL)) || in_array($type, variable_get('og_promote_group_types', NULL)))) {
switch ($op) {
case 'user insert':
case 'user update':
if ($args['is_active']) {
$user = user_load(array('uid' => $uid));
if (!in_array($role_id, array_keys($user->roles))) {
$edit = array();
$edit['roles'] = $user->roles;
$edit['roles'][$role_id] = '';
user_save($user, $edit);
// Rebuild user's menu.
cache_clear_all('menu:'. $user->uid, TRUE);
}
}
break;
case 'user delete':
$groups = variable_get('og_promote_groups', array());
$group_types = variable_get('og_promote_group_types', array());
$user_groups = array_keys(og_get_subscriptions($uid));
$user_groups_new = array_diff($user_groups, array($nid));
$user_group_types_new = array();
foreach ($user_groups_new as $gid) {
$user_group_types_new[] = _og_promote_get_node_type($gid);
}
// check that the user isn't in any of the other groups given explicitely or any of the other groups given by content type.
if ((in_array($nid, $groups) || in_array($type, $group_types)) && !(count(array_intersect($groups, $user_groups_new)) + count(array_intersect($group_types, $user_group_types_new)))) {
$user = user_load(array('uid' => $uid));
$edit = array();
$edit['roles'] = array();
foreach ($user->roles as $key => $value) {
if ($key != $role_id) {
$edit['roles'][$key] = $value;
}
}
user_save($user, $edit);
// Rebuild user's menu.
cache_clear_all('menu:'. $user->uid, TRUE);
}
break;
}
}
}
/**
* Configuration form.
*/
function og_promote_admin_settings() {
$result = db_query('SELECT rid, name FROM {role} ORDER BY name');
$role_names = array();
while ($role = db_fetch_object($result)) {
if ($role->rid != DRUPAL_ANONYMOUS_RID && $role->rid !=DRUPAL_AUTHENTICATED_RID) {
$role_names[$role->rid] = check_plain($role->name);
}
}
if (count($role_names)) {
$form['og_promote_role'] = array('#type' => 'radios', '#title' => t('Roles'), '#options' => $role_names, '#default_value' => variable_get('og_promote_role', 0), '#description' => t('Choose a role that users will promoted to if they become member of any of the groups specified below. If the member gets unsubscribed, it will be removed from the role.'));
}
else {
$form['og_promote_role'] = array('#type' => 'item', '#title' => t('No roles'), '#description' => t('You need to define some additional roles to use this module.'));
}
$options = array();
$values = array();
$types = variable_get('og_node_types', array('og'));
foreach ($types as $type) {
$options[$type] = node_get_types('name', array('type' => $type));
}
if (count($options)) {
$form['og_promote_group_types'] = array('#type' => 'checkboxes', '#title' => t('Group content types'), '#options' => $options, '#default_value' => variable_get('og_promote_group_types', array()), '#description' => t('The group content types that are defined for this site.'));
}
else {
$form['og_promote_group_types'] = array('#type' => 'item', '#title' => t('No group content types'), '#options' => $options, '#default_value' => variable_get('og_promote_group_types', array()), '#description' => t('You need to create some group content types.'));
}
foreach ($types as $type) {
$result = db_query("SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE n.type = '%s' ORDER BY n.title", $type);
while ($group = db_fetch_object($result)) {
$options[$group->nid] = check_plain($group->title);
switch ($group->selective) {
case OG_OPEN:
$options[$group->nid] .= check_plain($row->title) .' '. t('(open group)');
break;
case OG_MODERATED:
$options[$group->nid] .= check_plain($row->title) .' '. t('(moderated group)');
break;
case OG_INVITE_ONLY:
$options[$group->nid] .= check_plain($row->title) .' '. t('(invite only group)');
break;
case OG_CLOSED:
$options[$group->nid] .= check_plain($row->title) .' '. t('(closed group)');
break;
}
}
}
if (count($options)) {
$form['og_promote_groups'] = array('#type' => 'checkboxes', '#title' => t('Groups'), '#options' => $options, '#default_value' => variable_get('og_promote_groups', array()), '#description' => t('The groups that are defined for this site.'));
}
else {
$form['og_promote_groups'] = array('#type' => 'item', '#title' => t('No groups'), '#options' => $options, '#default_value' => variable_get('og_promote_groups', array()), '#description' => t('You need to create some groups.'));
}
return system_settings_form($form);
}
/**
* Helper function, returns node type of given node id.
*/
function _og_promote_get_node_type($nid) {
return db_result(db_query('SELECT type FROM {node} WHERE nid = %d', $nid));
}
#6
Looks good.
Stupid question: do you have more than one content type configured as an og group?
#7
Yes, i have two initially, and created some more just to try this, and nothing :S
#8
Updated patch.
Fixed: Group types spilled over in group listings on settings page.
Fixed: Test for activated group types broken (at least in PHP 5.0) - set in_array() checking to strict.