Intro

I've seen a number of posts about the frustrations of using OG and forums, I've "suffered" too. I got drupal just over a month ago with the aim of building an e-learning portal type site for my students at college. I leapt at OG, it's ideal for splitting the students into groups of courses. Naturally, I wanted to use forums and I had the following needs:

  • Each course has their own list of private forums.
  • Tutors should have a totally private list of forums
  • There should be some forums that are open to everyone.

Here's how I did it. I don't know how "legal" it is Drupal wise, but it works good enough until webchick or somebody else gets og_forum working how we want ;)

This method does not touch any of the core files, instead my approach was thru theming as it is part of the presentation... isn't it?..... You will need a template.php and a forum-list.tpl.php in your theme folder.

General logic

In the template.php I create an array ($ugrps) that contains the nid's of all the forums I want visible. Then in forum-list.tpl.php the line if (in_array($forum->tid, $forumgrps)) { checks if the current forum is the one I want to see.

This method can easily be changed to forums visible by role or quantity of post, points or whatever. I haven't tried to have for example a public forum inside a private container as I don't need it.

The Set up

Groups

I have the created the following og groups:

  • HNC Graphics
  • HNC Photography
  • NC Design
  • Staff
  • The Commons

The Commons is the group for everyone, and I use og_mandatory to ensure everybody is in it. As the admin (user 1) of the site the only groups I shall be in are the Commons and Staff, so I logged in as other members of staff to create the other 3 course groups.

The Forums

For each course, I created a Forum Container (name of the group) and 2 forums (Assignments and Class Notes). For the Staff Group I did the same except the forums were named Student Matters and Misc. Finally, for The Commons group I created a container and two forums Announcements and Break Time.

Numbers....

Now we need to collect some numbers, specifically the node numbers for the groups and forums. The quickest way is to go into phpmyadmin and select the sites database. Select the og table and press the Browse button. The two important columns are nid and description. Write down the group names and then from the descriptions write the nid numbers next to the coresponding group. I had:

  • HNC Graphics - 3
  • HNC Photography - 5
  • NC Design - 4
  • Staff - 33
  • The Commons - 2

Next, the forums. This could be difficult, especially if you have multiple forums with the same name like I have eg. Assignments. So what I suggest is that you log on to the site as the different group admins, go to the forum page and run the mouse over the titles of each container and forum. As you do so, look in the browsers status bar to get the number. For example, when the mouse hovered over Announcements, I saw http://localhost/forums/29. As I said, do this for each and every container and forum, and you should end up with a list like this:

  • HNC Graphics - 15,18,21
  • HNC Photography - 17,20,23
  • NC Design - 16,19,22
  • Staff - 30,31,32
  • The Commons - 27,28,29

template.php

Using your favorite editor, open template.php in your theme folder, if there isn't one there create a new file, then copy and paste the following into it:

function _user_grps() {
  global $user;
  $grps = array();         // Holds the current user's group nids
  $ugrps = array();       // Holds the forum nids
  $sql = 'SELECT nid FROM {og_uid} WHERE uid = %d';
  $res = db_query($sql, $user->uid);
  while ($mem = db_fetch_object($res)) {
    $grps[] = $mem->nid;
    }

  $ugrps = array(27,28,29); // public forums
  if (in_array(3,$grps)) {
    $ugrps[] = 15;
    $ugrps[] = 18;
    $ugrps[] = 21;
  }
  if (in_array(4,$grps)) {
    $ugrps[] = 16;
    $ugrps[] = 19;
    $ugrps[] = 22;
  }
  if (in_array(5,$grps)) {
    $ugrps[] = 17;
    $ugrps[] = 20;
    $ugrps[] = 23;
  }
  if (in_array(33,$grps)) {
    $ugrps[] = 30;
    $ugrps[] = 31;
    $ugrps[] = 32;
  }
  return $ugrps;
}

Now you customise this to your use.

  1. $ugrps = array(27,28,29); // public forums
    Those 3 numbers, are the numbers of the forums and containers that are open to everyone. Change them to your public forums.
  2. if (in_array(3,$grps)) {
    $ugrps[] = 15;
    $ugrps[] = 18;
    $ugrps[] = 21;
    }

    The number 3 is the group nid and the 15,18 and 21 are the forum nid's. Substitute your numbers, adding or deleting $ugrps[] = xx; as needed.

Done that? Okay, save it now!!

forum-list.tpl.php

If you have been theming your forums you probably have this already in your theme folder, if not create it (if you don't know how visit http://drupal.org/node/65171 for instructions - this is already turning into war & peace ;) ). You will need to add some lines...

At the start change:

global $user;

if ($forums) {

  $header = array(t('Forum'), t('Topics'), t('Posts'), t('Last post'));

  foreach ($forums as $forum) {

to

global $user;

if ($forums) {

  $header = array(t('Forum'), t('Topics'), t('Posts'), t('Last post'));
  $forumgrps = _user_grps();  // visible forums

  foreach ($forums as $forum) {

Find

  if ($forum->container) {
    $description  = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
    $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";

    if ($forum->description) {
      $description .= ' <div class="description">'. filter_xss_admin($forum->description) ."</div>\n";
    }
    $description .= "</div>\n";

    $rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => '4'));
  }

and change it to:

  if ($forum->container) {
    if (in_array($forum->tid, $forumgrps)) {    // <<===== Added line
      $description  = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
      $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";

      if ($forum->description) {
        $description .= ' <div class="description">'. filter_xss_admin($forum->description) ."</div>\n";
      }
      $description .= "</div>\n";

      $rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => '4'));
    }      // <<===== Added line
  }

Finally, find

    else {
      $new_topics = _forum_topics_unread($forum->tid, $user->uid);
      $forum->old_topics = $forum->num_topics - $new_topics;
      if (!$user->uid) {
        $new_topics = 0;
      }

      $description  = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
      $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";

      if ($forum->description) {
        $description .= ' <div class="description">'. filter_xss_admin($forum->description) ."</div>\n";
      }
      $description .= "</div>\n";

      $rows[] = array(
        array('data' => $description, 'class' => 'forum'),
        array('data' => $forum->num_topics . ($new_topics ? '<br />'. l(format_plural($new_topics, '1 new', '%count new'), "forum/$forum->tid", NULL, NULL, 'new') : ''), 'class' => 'topics'),
        array('data' => $forum->num_posts, 'class' => 'posts'),
        array('data' => _forum_format($forum->last_post), 'class' => 'last-reply'));
    }

And change it to

    else {
      if (in_array($forum->tid, $forumgrps)) {       // <<===== Added line
        $new_topics = _forum_topics_unread($forum->tid, $user->uid);
        $forum->old_topics = $forum->num_topics - $new_topics;
        if (!$user->uid) {
          $new_topics = 0;
        }

        $description  = '<div style="margin-left: '. ($forum->depth * 30) ."px;\">\n";
        $description .= ' <div class="name">'. l($forum->name, "forum/$forum->tid") ."</div>\n";

        if ($forum->description) {
          $description .= ' <div class="description">'. filter_xss_admin($forum->description) ."</div>\n";
        }
        $description .= "</div>\n";

        $rows[] = array(
          array('data' => $description, 'class' => 'forum'),
          array('data' => $forum->num_topics . ($new_topics ? '<br />'. l(format_plural($new_topics, '1 new', '%count new'), "forum/$forum->tid", NULL, NULL, 'new') : ''), 'class' => 'topics'),
          array('data' => $forum->num_posts, 'class' => 'posts'),
          array('data' => _forum_format($forum->last_post), 'class' => 'last-reply'));
      }                        // <<===== Added line
    }

Done that? Then save it and you're all done and finished. All that remains is to go to the site and test it out as different group members etc.

Comments

michelle’s picture

I'm not to the point of needing this yet, so I only skimmed it, but it looks like something I'll be wanting on my upcoming site. Could you add this to the handbook, please, so it doesn't get lost in the depths of the forums?

Thanks,

Michelle

lenzjo’s picture

I thought about the handbook, BUT....

  1. I'm not sure how robust it is, for example I have not tested it yet with anon visitors.
  2. Being new to Drupal (5-6 weeks) I'm not sure how valid this method is.
  3. As far as I'm concerned this is only a temp fix until og_forums has multi forums per group and is compatible with "normal" forums.

It works fine for my purposes, where every visitor is in at least one og group, otherwise - who knows? Once I finish the site, I may be a bit more rigorous with it as I need public/private forums on other sites I am to build. I might even have a go at making a small module to automate the process, esp. if sufficient interest is shown.

I posted it because I thought others might either find it useful, or correct me in the error of my ways ;) But, no offence, you are the only one to even comment on it, so it doesn't look as if many care one way or the other about this... and I have enough to do right now, besides putting this e-learning site together, I'm developing various e-learning related modules.

michelle’s picture

I'm interested because the site I am in the planning stages of will need both public and private forums. But, as I haven't even installed OG, yet, I'm in no position to help.

I'll bookmark this thread so I don't lose it, though. :)

Thanks,

Michelle

lenzjo’s picture

Hey.. no problem, I'm just glad somebody might find it useful :) I'm already thinking of updating it. Why? Because the course groups, by neccesity, have to have an admin - the tutor(s) included with the students. Although I have a general tutors forum, I thought it might be nice if, for each course the group of tutors involved could have their own private forum within the courses container like:

HNC Graphics    <-- Container
--Tutors        <-- Accessible by tutors only
--Assignments   <-- Accesible by students and tutors
--Class Notes   <--    "      "     "      "    "

As tutors and students are roles, I can filter the forum display by both og group and user role. Just have to dream up some code to get it working now....

BTW, when you do get around to using this, if you have any problems or questions feel free to contact me okay.