Hi, I've patched og.module with some custom code, and was hoping if any coders out there could give me suggestions or feedback about whether I did this correctly. My php skills is basic, so I may or may not have coded it well or correctly. Rather than patching og.module, should this be done in template.php? (and how do I do this?)
Basically, I'm using drupal as a course management system, where teachers can create groups (courses), and add lessons to their groups. But if a teacher adds another teacher as a subscriber to the course, then the other teacher will also be able to add lessons to the course. I don't want this, because other teachers should be only able to observe, rather than post. In order to solve this I wrote the below code change in og.module. However, I would like added teachers who have been designated as 'group admins' to be able to post lessons, but not added teachers who are not admins.
In my code, I use $group->uid == $user->uid to check if user is the owner of the group. However I would prefer to just check if they have the 'group admin' permission. Is there a function or easy way to do this? Here is my full code:
/**
* Confirm og unsubscription submit handler
*/
function og_confirm_unsubscribe_submit($form_id, $form_values) {
og_delete_subscription($form_values['gid'], $form_values['uid']);
drupal_set_message(t('User unsubscribed from group.'));
return "node/$gid";
}
// since a user's subscriptions are loaded into $user object, this function is only occassionally useful to get group subs for users other than the current user
// even then, it often makes sense to call user_load() instead of this function.
// load all subscriptions for a given user
function og_get_subscriptions($uid, $min_is_active = 1) {
static $subscriptions = array();
/* Original og code
if (!isset($subscriptions[$uid][$min_is_active])) {
$sql = "SELECT n.title, n.type, n.status, ou.* FROM {og_uid} ou INNER JOIN {node} n ON ou.nid = n.nid WHERE ou.uid = %d AND ou.is_active >= %d ORDER BY n.title";
$result = db_query($sql, $uid, $min_is_active);
while ($row = db_fetch_array($result)) {
$subscriptions[$uid][$min_is_active][$row['nid']] = $row;
}
if (!isset($subscriptions[$uid][$min_is_active])) {
$subscriptions[$uid][$min_is_active] = array();
}
}
*/
// Replacement code
if (!isset($subscriptions[$uid][$min_is_active]) && $group->uid == $user->uid) {
$sql = "SELECT n.title, n.type, n.status, ou.* FROM {og_uid} ou INNER JOIN {node} n ON ou.nid = n.nid WHERE ou.uid = %d AND ou.is_active >= %d AND ou.is_admin = 1 ORDER BY n.title";
$result = db_query($sql, $uid, $min_is_active);
while ($row = db_fetch_array($result)) {
$subscriptions[$uid][$min_is_active][$row['nid']] = $row;
}
if (!isset($subscriptions[$uid][$min_is_active])) {
$subscriptions[$uid][$min_is_active] = array();
}
} elseif (!isset($subscriptions[$uid][$min_is_active]) && $group->uid != $user->uid) {
$sql = "SELECT n.title, n.type, n.status, ou.* FROM {og_uid} ou INNER JOIN {node} n ON ou.nid = n.nid WHERE ou.uid = %d AND ou.is_active >= %d ORDER BY n.title";
$result = db_query($sql, $uid, $min_is_active);
while ($row = db_fetch_array($result)) {
$subscriptions[$uid][$min_is_active][$row['nid']] = $row;
}
if (!isset($subscriptions[$uid][$min_is_active])) {
$subscriptions[$uid][$min_is_active] = array();
}
}
return $subscriptions[$uid][$min_is_active];
}
Thanks for any suggestions of help!
YLC
Comments
Comment #1
scedwar commentedRegarding checking for og_admin, have a look through the og functions, but I use this snippet within code for block visibility to restrict visibility to group admins and for particular types:
This sort of code would be ideal to create read only groups for a particular type in OG. i.e in the list of og group type in admin/og/og you would select groups as "read only" in that only admins/owners should be able to post.
Comment #2
scedwar commentedI can squeeze some of my charity's web development budget for this, see here:
http://drupal.org/node/255672
Comment #3
Anonymous (not verified) commentedThanks for the code snippets scedwar. I still haven't had time to figure out a solution to this.
As an update, the above code I originally posted is not viable, because it is quite hackish, there is no need to patch og for this, and it also restricts what content is VIEWABLE to admins only, not just the audience box selection. Although the module OG Content Type Admin provides this type of functionality, it doesn't restrict audience when editing.
My main problem is my PHP knowledge is limited, so I don't know how to do this. Mainly I just want to restrict specific content types so that you must be a course admin in order to post to the group. I think I will have time to work on a solution this week, and I will post the results here. If anybody has any suggestions on how to do this it would be greatly appreciated.
My thought is to write a function into template.php using hook_form_alter such as phptemplate_course_node_form_alter, and then perhaps use the snippets above to control it. Is this how it normally would be done? If anybody can give me a rough snippet that would be great! Thanks!
Comment #4
scedwar commented"Although the module OG Content Type Admin provides this type of functionality, it doesn't restrict audience when editing."
In my experience this is not achievable with this module due to the design (it is meant for each admin to customise their group) and some serious bugs that allow you to bypass the restrictions. This module is, IMHO, deepyly flawed. There are outstanding issues I have raised in the issue queue about this.
Comment #5
scedwar commentedAlso see: http://drupal.org/node/311107
Comment #6
scedwar commentedI've provided a very simple patch for this here:
http://drupal.org/node/433884