### Eclipse Workspace Patch 1.0
#P moodle18
Index: mod/workshop/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/workshop/view.php,v
retrieving revision 1.59.2.1
diff -u -r1.59.2.1 view.php
--- mod/workshop/view.php	4 Apr 2007 04:03:27 -0000	1.59.2.1
+++ mod/workshop/view.php	15 May 2007 07:48:59 -0000
@@ -300,34 +300,12 @@
             workshop_grade_assessments($workshop);
         }
 
-        /// Check to see if groups are being used in this workshop
-        /*
-        setup_and_print_groups($course, groupmode($course, $cm), "view.php?id=$cm->id");
-        $currentgroup = get_current_group($course->id);
-        */
-        
-        /// copied code from assignment module, if this is not the way to do this please change it
-        /// the above code does not work
-        /// set_and_print_groups() is not fully implemented as function groups_instance_print_grouping_selector()
-        /// and function groups_instance_print_group_selector() are missing.
-       
         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-        $changegroup = optional_param('group', -1, PARAM_INT);   // choose the current group
+
+        /// find out current groups mode
         $groupmode = groupmode($course, $cm);
-        $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);   
-    
-        /// Now we need a menu for separategroups as well!
-        if ($groupmode == VISIBLEGROUPS || ($groupmode
-            && has_capability('moodle/site:accessallgroups', $context))) {
-        
-            //the following query really needs to change
-            if ($groups = groups_get_groups_names($course->id)) { //TODO:
-                print_box_start('groupmenu');
-                print_group_menu($groups, $groupmode, $currentgroup, "view.php?id=$cm->id");
-                print_box_end(); // groupmenu
-            }
-        }
-        
+        $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
+
         /// Print admin links
         echo "<table width=\"100%\"><tr><td>";
         echo "<a href=\"submissions.php?id=$cm->id&amp;action=adminlist\">".
Index: mod/data/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/data/index.php,v
retrieving revision 1.12
diff -u -r1.12 index.php
--- mod/data/index.php	3 Jan 2007 04:55:22 -0000	1.12
+++ mod/data/index.php	15 May 2007 07:48:41 -0000
@@ -75,7 +75,7 @@
         array_push($table->align, 'center');
     }
 
-    $currentgroup = get_current_group($course->id);
+    $currentgroup = get_and_set_current_group($course, groupmode($course));
     if ($currentgroup and has_capability('mod/data:manageentries', $context)) {
         $group = groups_get_group($currentgroup, false);
         $groupname = " ($group->name)";
Index: mod/data/import.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/data/import.php,v
retrieving revision 1.12.2.1
diff -u -r1.12.2.1 import.php
--- mod/data/import.php	14 Feb 2007 20:54:28 -0000	1.12.2.1
+++ mod/data/import.php	15 May 2007 07:48:40 -0000
@@ -79,11 +79,8 @@
     print_heading(format_string($data->name));
 
 /// Groups needed for Add entry tab
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = get_and_set_current_group($course, $groupmode);
-    } else {
-        $currentgroup = 0;
-    }
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = get_and_set_current_group($course, $groupmode);
 
 /// Print the tabs
     $currenttab = 'add';
Index: mod/data/templates.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/data/templates.php,v
retrieving revision 1.60.2.1
diff -u -r1.60.2.1 templates.php
--- mod/data/templates.php	5 Mar 2007 07:15:23 -0000	1.60.2.1
+++ mod/data/templates.php	15 May 2007 07:48:43 -0000
@@ -104,11 +104,8 @@
 
 
 /// Groups needed for Add entry tab
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = get_and_set_current_group($course, $groupmode);
-    } else {
-        $currentgroup = 0;
-    }
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = get_and_set_current_group($course, $groupmode);
 
 /// Print the tabs.
     $currenttab = 'templates';
Index: mod/data/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/data/lib.php,v
retrieving revision 1.113.2.5
diff -u -r1.113.2.5 lib.php
--- mod/data/lib.php	26 Apr 2007 21:38:28 -0000	1.113.2.5
+++ mod/data/lib.php	15 May 2007 07:48:43 -0000
@@ -1472,11 +1472,8 @@
     print_heading(format_string($data->name));
 
 /// Groups needed for Add entry tab
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = get_and_set_current_group($course, $groupmode);
-    } else {
-        $currentgroup = 0;
-    }
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = get_and_set_current_group($course, $groupmode);
 
     /// Print the tabs
 
@@ -1493,7 +1490,7 @@
     }
 }
 
-function data_user_can_add_entry($data, $currentgroup=false, $groupmode='') {
+function data_user_can_add_entry($data, $currentgroup, $groupmode) {
     global $USER;
 
     if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
@@ -1505,16 +1502,18 @@
         return false;
     }
 
+    if (!$groupmode or has_capability('moodle/site:accessallgroups', $context)) {
+        return true;
+    }
+
     if ($currentgroup) {
-        return (has_capability('moodle/site:accessallgroups', $context) or ismember($currentgroup));
+        return ismember($currentgroup);
     } else {
         //else it might be group 0 in visible mode
         if ($groupmode == VISIBLEGROUPS){
-            
-            $result = groups_is_member($currentgroup);
-            return $result;
-        } else {
             return true;
+        } else {
+            return false;
         }
     }
 }
Index: mod/data/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/data/view.php,v
retrieving revision 1.56.2.5
diff -u -r1.56.2.5 view.php
--- mod/data/view.php	18 Apr 2007 02:31:34 -0000	1.56.2.5
+++ mod/data/view.php	15 May 2007 07:48:45 -0000
@@ -181,6 +181,12 @@
         echo '<td id="middle-column">';
     }
 
+/// Check to see if groups are being used here
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = setup_and_print_groups($course, $groupmode,
+                                            'view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).
+                                            '&amp;order='.s($order).'&amp;');
+
     print_heading(format_string($data->name));
 
     // Do we need to show a link to the RSS feed for the records?
@@ -195,15 +201,6 @@
         print_box(format_text($data->intro), 'generalbox', 'intro');
     }
 
-/// Check to see if groups are being used here
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = setup_and_print_groups($course, $groupmode,
-                                            'view.php?d='.$data->id.'&amp;search='.s($search).'&amp;sort='.s($sort).
-                                            '&amp;order='.s($order).'&amp;');
-    } else {
-        $currentgroup = 0;
-    }
-
 /// Delete any requested records
 
     if ($delete && confirm_sesskey() && (has_capability('mod/data:manageentries', $context) or data_isowner($delete))) {
Index: mod/data/edit.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/data/edit.php,v
retrieving revision 1.21.2.3
diff -u -r1.21.2.3 edit.php
--- mod/data/edit.php	5 Mar 2007 07:15:23 -0000	1.21.2.3
+++ mod/data/edit.php	15 May 2007 07:48:40 -0000
@@ -113,15 +113,11 @@
                         '', $meta, true, update_module_button($cm->id, $course->id, get_string('modulename', 'data')),
                         navmenu($course, $cm), '', '');
 
-    print_heading(format_string($data->name));
-
 /// Check to see if groups are being used here
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = setup_and_print_groups($course, $groupmode, 'edit.php?d='.$data->id);
 
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = setup_and_print_groups($course, $groupmode, 'edit.php?d='.$data->id.'&amp;sesskey='.sesskey().'&amp;');
-    } else {
-        $currentgroup = 0;
-    }
+    print_heading(format_string($data->name));
 
     if ($currentgroup) {
         $groupselect = " AND groupid = '$currentgroup'";
Index: group/lib/modulelib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/lib/modulelib.php,v
retrieving revision 1.1.4.3
diff -u -r1.1.4.3 modulelib.php
--- group/lib/modulelib.php	1 Mar 2007 11:32:04 -0000	1.1.4.3
+++ group/lib/modulelib.php	15 May 2007 07:48:27 -0000
@@ -420,7 +420,7 @@
         $SESSION->currentgroupinggroup[$cm->course][$cm->groupingid] = $currentgroup;
     }
     if ($full) {
-        return groups_groupid_to_group($currentgroup);
+        return groups_get_group($currentgroup, false);
     } else {
         return $currentgroup;
     }
Index: group/lib/legacylib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/lib/legacylib.php,v
retrieving revision 1.6.2.4
diff -u -r1.6.2.4 legacylib.php
--- group/lib/legacylib.php	5 May 2007 15:14:57 -0000	1.6.2.4
+++ group/lib/legacylib.php	15 May 2007 07:48:27 -0000
@@ -201,25 +201,29 @@
 function get_current_group($courseid, $full = false) {
     global $SESSION;
 
+    if (isset($SESSION->currentgroup[$courseid])) {
+        if ($full) {
+            return groups_get_group($SESSION->currentgroup[$courseid], false);
+        } else {
+            return $SESSION->currentgroup[$courseid];
+        }
+    }
+
     $mygroupid = mygroupid($courseid);
     if (is_array($mygroupid)) {
         $mygroupid = array_shift($mygroupid);
-    }
-
-    if (isset($SESSION->currentgroup[$courseid])) {
-    	$currentgroup = $SESSION->currentgroup[$courseid];
-    } else {
-    	$currentgroup = $mygroupid;
-    }
-    
-    if ($currentgroup) {
-    	$SESSION->currentgroup[$courseid] = $mygroupid;
+        set_current_group($courseid, $mygroupid);
+        if ($full) {
+            return groups_get_group($mygroupid, false);
+        } else {
+            return $mygroupid;
+        }
     }
 
     if ($full) {
-        return groups_groupid_to_group($currentgroup);
+        return false;
     } else {
-        return $currentgroup;
+        return 0;
     }
 }
 
@@ -265,12 +269,11 @@
                 /*)}else {
                     $currentgroupid = $group->id;*/
             } elseif ($groupmode == SEPARATEGROUPS) { // student in separate groups switching
-                if (ismember($group->id)) { //check if is a member
+                if (ismember($groupid)) { //check if is a member
                     $currentgroupid = set_current_group($course->id, $groupid); //might need to set_current_group?
                 }
                 else {
-                    echo($group->id);
-                    notify('You do not belong to this group!', 'error');
+                    notify('You do not belong to this group! ('.$groupid.')', 'error');
                 }
             }
         }
@@ -280,8 +283,8 @@
         if (has_capability('moodle/site:accessallgroups', $context)) { // Sets current default group
             $currentgroupid = set_current_group($course->id, 0);
 
-        } elseif ($groupmode == VISIBLEGROUPS) {  // All groups are visible
-            $currentgroupid = 0;
+        } else if ($groupmode == VISIBLEGROUPS) {  // All groups are visible
+            $currentgroupid = set_current_group($course->id, 0);
         }
     }
 
@@ -317,24 +320,45 @@
 
     $context = get_context_instance(CONTEXT_COURSE, $course->id);
 
-    if ($groupmode == VISIBLEGROUPS
-        or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
-        groups_instance_print_grouping_selector();
-    }//added code here to allow non-editting teacher to swap in-between his own groups
-    //added code for students in separategrous to swtich groups
-    else if ($groupmode == SEPARATEGROUPS and has_capability('moodle/course:view', $context)) {
-        groups_instance_print_group_selector();
+    if ($groupmode == SEPARATEGROUPS and !$currentgroup and !has_capability('moodle/site:accessallgroups', $context)) {
+        //we are in separate groups and the current group is group 0, as last set.
+        //this can mean that either, this guy has no group
+        //or, this guy just came from a visible all forum, and he left when he set his current group to 0 (show all)
+
+        if ($usergroups = user_group($course->id, $USER->id)){
+            //for the second situation, we need to perform the trick and get him a group.
+            $first = reset($usergroups);
+            $currentgroup = get_and_set_current_group($course, $groupmode, $first->id);
+
+        } else {
+            //else he has no group in this course
+            print_heading(get_string('notingroup'));
+            print_footer($course);
+            exit;
+        }
     }
 
-    return $currentgroup;
-}
+    if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
 
+        if ($groups = get_groups($course->id)) {
+
+            echo '<div class="groupselector">';
+            print_group_menu($groups, $groupmode, $currentgroup, $urlroot, 1);
+            echo '</div>';
+        }
+
+    } else if ($groupmode == SEPARATEGROUPS and has_capability('moodle/course:view', $context)) {
+        //get all the groups this guy is in in this course
+        if ($usergroups = user_group($course->id, $USER->id)){
+            echo '<div class="groupselector">';
+            //print them in the menu
+            print_group_menu($usergroups, $groupmode, $currentgroup, $urlroot, 0);
+            echo '</div>';
+        }
+    }
+
+    return $currentgroup;
 
-function groups_instance_print_grouping_selector() {
-    //TODO: ??
-}
-function groups_instance_print_group_selector() {
-    //TODO: ??
 }
 
 
Index: mod/resource/mod_form.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/resource/mod_form.php,v
retrieving revision 1.3.2.2
diff -u -r1.3.2.2 mod_form.php
--- mod/resource/mod_form.php	24 Apr 2007 21:09:29 -0000	1.3.2.2
+++ mod/resource/mod_form.php	15 May 2007 07:48:58 -0000
@@ -43,7 +43,7 @@
         $mform->addElement('header', 'typedesc', get_string('resourcetype'.$type,'resource'));
         $this->_resinstance->setup_elements($mform);
 
-        $this->standard_coursemodule_elements();
+        $this->standard_coursemodule_elements(false);
 
         $this->add_action_buttons();
     }
Index: mod/chat/report.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/chat/report.php,v
retrieving revision 1.31
diff -u -r1.31 report.php
--- mod/chat/report.php	9 Jan 2007 04:50:08 -0000	1.31
+++ mod/chat/report.php	15 May 2007 07:48:38 -0000
@@ -46,13 +46,10 @@
                       '', '', true, '', navmenu($course, $cm));
 
     /// Check to see if groups are being used here
-        if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-            $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
-        } else {
-            $currentgroup = false;
-        }
+        $groupmode = groupmode($course, $cm);
+        $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
 
-        if (!empty($currentgroup)) {
+        if ($currentgroup) {
             $groupselect = " AND groupid = '$currentgroup'";
         } else {
             $groupselect = "";
Index: mod/chat/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/chat/view.php,v
retrieving revision 1.55.2.1
diff -u -r1.55.2.1 view.php
--- mod/chat/view.php	2 Mar 2007 02:32:42 -0000	1.55.2.1
+++ mod/chat/view.php	15 May 2007 07:48:38 -0000
@@ -75,23 +75,11 @@
     }
 
     echo '<td id="middle-column">';
-    
-    if ($chat->studentlogs or has_capability('mod/chat:readlog',$context)) {
-        echo '<div class="reportlink">';
-        echo "<a href=\"report.php?id=$cm->id\">".
-              get_string('viewreport', 'chat').'</a>';
-        echo '</div>';
-    }
-
-    print_heading(format_string($chat->name));
 
 /// Check to see if groups are being used here
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
-    } else {
-        $currentgroup = 0;
-    }
-
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
+    
     if ($currentgroup) {
         $groupselect = " AND groupid = '$currentgroup'";
         $groupparam = "&amp;groupid=$currentgroup";
@@ -100,10 +88,20 @@
         $groupparam = "";
     }
 
+    if ($chat->studentlogs or has_capability('mod/chat:readlog',$context)) {
+        echo '<div class="reportlink">';
+        echo "<a href=\"report.php?id=$cm->id\">".
+              get_string('viewreport', 'chat').'</a>';
+        echo '</div>';
+    }
+
+
+    print_heading(format_string($chat->name));
+
 /// Print the main part of the page
 
     if (has_capability('mod/chat:chat',$context)) {
-        print_simple_box_start('center');
+        print_box_start('generalbox', 'enterlink');
         // users with screenreader set, will only see 1 link, to the manual refresh page
         // for better accessibility
         if (!empty($USER->screenreader)) {
@@ -113,30 +111,32 @@
         }
         
         link_to_popup_window ($chattarget,
-                              "chat$course->id$chat->id$groupparam", "$strenterchat", 500, 700, get_string('modulename', 'chat'));
-        print_simple_box_end();
+                              "chat$course->id$chat->id$groupparam", "<p>$strenterchat</p>", 500, 700, get_string('modulename', 'chat'));
         
         // if user is using screen reader, then there is no need to display this link again
         if ($CFG->chat_method == 'header_js' && empty($USER->screenreader)) {
             // show frame/js-less alternative
-            print_simple_box_start('center');
             link_to_popup_window ("/mod/chat/gui_basic/index.php?id=$chat->id$groupparam",
-                                  "chat$course->id$chat->id$groupparam", '('.get_string('noframesjs', 'message').')', 500, 700, get_string('modulename', 'chat'));
-            print_simple_box_end();
+                                  "chat$course->id$chat->id$groupparam", '<p>('.get_string('noframesjs', 'message').')</p>', 500, 700, get_string('modulename', 'chat'));
         }
-    } else {
-/*    XXX TODO
+
+        print_box_end();
+
+    } else if (isguestuser()) {
         $wwwroot = $CFG->wwwroot.'/login/index.php';
         if (!empty($CFG->loginhttps)) {
             $wwwroot = str_replace('http:','https:', $wwwroot);
         }
 
         notice_yesno(get_string('noguests', 'chat').'<br /><br />'.get_string('liketologin'),
-                     $wwwroot, $_SERVER['HTTP_REFERER']);
+                     $wwwroot, $CFG->wwwroot.'/course/view.php?id='.$course->id);
                      
         print_footer($course);
         exit;
-*/
+
+    } else {
+        // show some error message
+        require_capability('mod/chat:chat', $context);
     }
 
 
Index: mod/journal/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/journal/view.php,v
retrieving revision 1.48
diff -u -r1.48 view.php
--- mod/journal/view.php	11 Jan 2007 08:06:16 -0000	1.48
+++ mod/journal/view.php	15 May 2007 07:48:56 -0000
@@ -32,21 +32,16 @@
                  "<a href=\"index.php?id=$course->id\">$strjournals</a> -> ".format_string($journal->name), '', '', true,
                   update_module_button($cm->id, $course->id, $strjournal), navmenu($course, $cm));
 
+/// Check to see if groups are being used here
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
+
     if (isteacher($course->id)) {
-        $currentgroup = get_current_group($course->id);
-        if ($currentgroup and isteacheredit($course->id)) {
-            $group = groups_get_group($currentgroup); //TODO:
-            $groupname = " ($group->name)";
-        } else {
-            $groupname = "";
-        }
         $entrycount = journal_count_entries($journal, $currentgroup);
 
         echo '<div class="reportlink"><a href="report.php?id='.$cm->id.'">'.
-              get_string('viewallentries','journal', $entrycount)."</a>$groupname</div>";
+              get_string('viewallentries','journal', $entrycount).'</a></div>';
 
-    } else if (!$cm->visible) {
-        notice(get_string('activityiscurrentlyhidden'));
     }
 
     $journal->intro = trim($journal->intro);
Index: mod/journal/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/journal/lib.php,v
retrieving revision 1.79
diff -u -r1.79 lib.php
--- mod/journal/lib.php	3 Jan 2007 04:55:23 -0000	1.79
+++ mod/journal/lib.php	15 May 2007 07:48:56 -0000
@@ -407,7 +407,7 @@
     if ($entry) {
         echo "&nbsp;&nbsp;<font size=\"1\">".get_string("lastedited").": ".userdate($entry->modified)."</font>";
     }
-    echo "</tr>";
+    echo "</td></tr>";
 
     echo "\n<tr><td width=\"100%\">";
     if ($entry) {
Index: mod/journal/report.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/journal/report.php,v
retrieving revision 1.34
diff -u -r1.34 report.php
--- mod/journal/report.php	21 Sep 2006 09:54:00 -0000	1.34
+++ mod/journal/report.php	15 May 2007 07:48:56 -0000
@@ -44,11 +44,8 @@
 
 
 /// Check to see if groups are being used in this journal
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
-    } else {
-        $currentgroup = false;
-    }
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
 
 /// Process incoming data if there is any
     if ($data = data_submitted()) {
Index: theme/standard/styles_layout.css
===================================================================
RCS file: /cvsroot/moodle/moodle/theme/standard/styles_layout.css,v
retrieving revision 1.365.2.63
diff -u -r1.365.2.63 styles_layout.css
--- theme/standard/styles_layout.css	11 May 2007 11:35:14 -0000	1.365.2.63
+++ theme/standard/styles_layout.css	15 May 2007 07:49:01 -0000
@@ -120,6 +120,16 @@
   text-align: center;
 }
 
+div.groupselector {
+  text-align: right;
+  margin-top: 3px;
+  margin-bottom: 3px;
+}
+
+.left div.groupselector {
+  text-align: left;
+}
+
 #addmembersform table {
   margin-left: auto;
   margin-right: auto;
@@ -2632,6 +2642,13 @@
    vertical-align: middle;   
 }
 
+#mod-chat-view #enterlink {
+   text-align: center;
+   margin-left: auto;
+   margin-right: auto;
+   width: 50%;
+}
+
 .chat-event .picture,
 .chat-message .picture {
   width:40px;
Index: blocks/online_users/block_online_users.php
===================================================================
RCS file: /cvsroot/moodle/moodle/blocks/online_users/block_online_users.php,v
retrieving revision 1.46.2.3
diff -u -r1.46.2.3 block_online_users.php
--- blocks/online_users/block_online_users.php	30 Apr 2007 02:55:49 -0000	1.46.2.3
+++ blocks/online_users/block_online_users.php	15 May 2007 07:48:17 -0000
@@ -43,7 +43,7 @@
                              && !has_capability('moodle/site:accessallgroups', $context));
 
         //Get the user current group
-        $currentgroup = $isseparategroups ? get_current_group($COURSE->id) : NULL;
+        $currentgroup = $isseparategroups ? get_and_set_current_group($COURSE, groupmode($COURSE)) : NULL;
 
         $groupmembers = "";
         $groupselect = "";
@@ -51,7 +51,7 @@
         //Add this to the SQL to show only group users
         if ($currentgroup !== NULL) {
             $groupmembers = ', '.groups_members_from_sql(); //TODO: ", {$CFG->prefix}groups_members gm ";
-            $groupselect .= groups_members_where_sql($currentgroup, 'u.id'); //" AND u.id = gm.userid AND gm.groupid = '$currentgroup'";
+            $groupselect = ' AND '.groups_members_where_sql($currentgroup, 'u.id'); //" AND u.id = gm.userid AND gm.groupid = '$currentgroup'";
         }
 
         if ($COURSE->id == SITEID) {  // Site-level
Index: course/grades.php
===================================================================
RCS file: /cvsroot/moodle/moodle/course/grades.php,v
retrieving revision 1.34.2.1
diff -u -r1.34.2.1 grades.php
--- course/grades.php	28 Feb 2007 05:36:13 -0000	1.34.2.1
+++ course/grades.php	15 May 2007 07:48:17 -0000
@@ -22,11 +22,8 @@
     $stractivityreport = get_string("activityreport");
 
 /// Check to see if groups are being used in this course
-    if ($groupmode = groupmode($course)) {   // Groups are being used
-        $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
-    } else {
-        $currentgroup = false;
-    }
+    $groupmode = groupmode($course);
+    $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
 
 /// Get a list of all students
     if ($currentgroup) {
@@ -308,7 +305,6 @@
         $options["download"] = "xls";
         print_single_button("grades.php", $options, get_string("downloadexcel"));
         echo "<td>";
-        $options = array();
         $options["download"] = "txt";
         print_single_button("grades.php", $options, get_string("downloadtext"));
         echo "</table>";
Index: course/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/course/lib.php,v
retrieving revision 1.484.2.10
diff -u -r1.484.2.10 lib.php
--- course/lib.php	13 Apr 2007 06:33:02 -0000	1.484.2.10
+++ course/lib.php	15 May 2007 07:48:20 -0000
@@ -1418,7 +1418,8 @@
                 }
 
                 if ($isediting) {
-                    if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource') {
+                    // TODO: we must define this as mod property!
+                    if ($groupbuttons and $mod->modname != 'label' and $mod->modname != 'resource' and $mod->modname != 'glossary') {
                         if (! $mod->groupmodelink = $groupbuttonslink) {
                             $mod->groupmode = $course->groupmode;
                         }
Index: course/moodleform_mod.php
===================================================================
RCS file: /cvsroot/moodle/moodle/course/moodleform_mod.php,v
retrieving revision 1.5.2.1
diff -u -r1.5.2.1 moodleform_mod.php
--- course/moodleform_mod.php	13 Mar 2007 14:46:17 -0000	1.5.2.1
+++ course/moodleform_mod.php	15 May 2007 07:48:20 -0000
@@ -65,6 +65,7 @@
         $mform =& $this->_form;
         $mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
         if ($supportsgroups){
+            // TODO: we must define this as mod property!
             $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
         }
         $mform->addElement('modvisible', 'visible', get_string('visible'));
Index: mod/forum/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/view.php,v
retrieving revision 1.94.2.3
diff -u -r1.94.2.3 view.php
--- mod/forum/view.php	15 Mar 2007 06:23:54 -0000	1.94.2.3
+++ mod/forum/view.php	15 May 2007 07:48:55 -0000
@@ -79,15 +79,9 @@
         notice(get_string('noviewdiscussionspermission', 'forum'));
     }
     
+/// find out current groups mode
     $groupmode = groupmode($course, $cm);
-    $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
-    
-    if ($groupmode == SEPARATEGROUPS && ($currentgroup === false) &&
-            !has_capability('moodle/site:accessallgroups', $context)) {
-        notice(get_string('notingroup', 'forum'));
-    }
-
-
+    $currentgroup = setup_and_print_groups($course, $groupmode, 'view.php?id=' . $cm->id);
 
 /// Okay, we can show the discussions. Log the forum view.
     if ($cm->id) {
@@ -120,40 +114,6 @@
 
     print_box_start('forumcontrol');
 
-    /// 2 ways to do this, 1. we can changed the setup_and_print_groups functions
-    /// in moodlelib, taking in 1 more parameter, and tell the function when to
-    /// allow student menus, 2, we can just use this code to explicitly print this
-    /// menu for students in forums.
-
-    /// Now we need a menu for separategroups as well!
-    if ($groupmode == VISIBLEGROUPS || ($groupmode
-            && has_capability('moodle/site:accessallgroups', $context))) {
-        
-        //the following query really needs to change
-        if ($groups = groups_get_groups_names($course->id)) { //TODO:
-            print_box_start('groupmenu');
-            print_group_menu($groups, $groupmode, $currentgroup, "$CFG->wwwroot/mod/forum/view.php?id=$cm->id");
-            print_box_end(); // groupmenu
-        }
-    }
-
-    /// Only print menus the student is in any course
-    else if ($groupmode == SEPARATEGROUPS){
-        $validgroups = array();
-        // Get all the groups this guy is in in this course
-
-        if ($p = user_group($course->id,$USER->id)){
-            /// Extract the name and id for the group
-            foreach ($p as $index => $object){
-                $validgroups[$object->id] = $object->name;
-            }
-            /// Print them in the menu
-            print_box_start('groupmenu');
-            print_group_menu($validgroups, $groupmode, $currentgroup, "view.php?id=$cm->id",0);
-            print_box_end(); // groupmenu
-        }
-    }
-
     print_box_start('subscription');
 
     if (!empty($USER->id) && !has_capability('moodle/legacy:guest', $context, NULL, false)) {
Index: mod/forum/search.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/search.php,v
retrieving revision 1.72.2.4
diff -u -r1.72.2.4 search.php
--- mod/forum/search.php	20 Mar 2007 05:12:17 -0000	1.72.2.4
+++ mod/forum/search.php	15 May 2007 07:48:55 -0000
@@ -404,7 +404,7 @@
 function forum_menu_list($course)  {
 
     $menu = array();
-    $currentgroup = get_current_group($course->id);
+    $currentgroup = get_and_set_current_group($course, groupmode($course));
 
     if ($forums = get_all_instances_in_course("forum", $course)) {
         if ($course->format == 'weeks') {
Index: mod/forum/subscribers.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/subscribers.php,v
retrieving revision 1.34
diff -u -r1.34 subscribers.php
--- mod/forum/subscribers.php	14 Aug 2006 05:55:43 -0000	1.34
+++ mod/forum/subscribers.php	15 May 2007 07:48:55 -0000
@@ -51,11 +51,8 @@
     }
 
 /// Check to see if groups are being used in this forum
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = setup_and_print_groups($course, $groupmode, "subscribers.php?id=$forum->id");
-    } else {
-        $currentgroup = false;
-    }
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = setup_and_print_groups($course, $groupmode, "subscribers.php?id=$forum->id");
 
     if (empty($USER->subscriptionsediting)) {         /// Display an overview of subscribers
 
Index: mod/forum/post.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/post.php,v
retrieving revision 1.134.2.8
diff -u -r1.134.2.8 post.php
--- mod/forum/post.php	27 Mar 2007 05:13:09 -0000	1.134.2.8
+++ mod/forum/post.php	15 May 2007 07:48:53 -0000
@@ -100,7 +100,11 @@
             }
         }
 
-        $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
+        if (isset($_SERVER["HTTP_REFERER"])) {
+            $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
+        } else {
+            $SESSION->fromurl = '';
+        }
 
 
         // Load up the $post variable.
@@ -114,11 +118,14 @@
         $post->userid     = $USER->id;
         $post->message    = '';
 
-        $post->groupid = get_current_group($course->id);
-        if ($post->groupid == 0) {
-            $post->groupid = -1;
+        if ($groupmode = groupmode($course, $cm)) {
+            $post->groupid = get_and_set_current_group($course, $groupmode);
+            if ($post->groupid == 0) {
+                $post->groupid = -1; //TODO: why -1??
+            }
+        } else {
+            $post->groupid = -1; //TODO: why -1??
         }
-
         forum_set_return();
 
     } else if (!empty($reply)) {      // User is writing a new reply
Index: mod/forum/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/lib.php,v
retrieving revision 1.536.2.30
diff -u -r1.536.2.30 lib.php
--- mod/forum/lib.php	7 May 2007 04:59:01 -0000	1.536.2.30
+++ mod/forum/lib.php	15 May 2007 07:48:52 -0000
@@ -950,7 +950,7 @@
             ' JOIN '.$CFG->prefix.'forum_discussions d ON p.discussion = d.id '.
             ' LEFT JOIN '.$CFG->prefix.'forum_read r ON r.postid = p.id AND r.userid = '.$USER->id.' WHERE (';
         foreach ($trackingforums as $track) {
-            $sql .= '(d.forum = '.$track->id.' AND (d.groupid = -1 OR d.groupid = 0 OR d.groupid = '.get_current_group($track->course,false).')) OR ';
+            $sql .= '(d.forum = '.$track->id.' AND (d.groupid = -1 OR d.groupid = 0 OR d.groupid = '.get_current_group($track->course).')) OR ';
         }
         $sql = substr($sql,0,-3); // take off the last OR
         $sql .= ') AND p.modified >= '.$cutoffdate.' AND r.id is NULL GROUP BY d.forum,d.course';
@@ -1730,7 +1730,7 @@
  * Get all discussions in a forum
  */
 function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
-                               $user=0, $fullpost=true, $visiblegroups=-1, $limit=0, $userlastmodified=false) {
+                               $user=0, $fullpost=true, $currentgroup=-1, $limit=0, $userlastmodified=false) {
     global $CFG, $USER;
 
     $timelimit = '';
@@ -1764,10 +1764,14 @@
         $limitnum = $limit;
     }
 
-    if ($visiblegroups == -1) {
+    if ($currentgroup == -1) {
+        $currentgroup = get_current_group($cm->course);
+    }
+
+    if ($currentgroup) {
+        $groupselect = " AND (d.groupid = '$currentgroup' OR d.groupid = -1) ";
+    } else {
         $groupselect = "";
-    } else  {
-        $groupselect = " AND (d.groupid = '$visiblegroups' OR d.groupid = '-1') ";
     }
 
     if (empty($forumsort)) {
@@ -3211,7 +3215,7 @@
 /**
  * TODO document
  */
-function forum_user_can_post_discussion($forum, $currentgroup=false, $groupmode=false, $cm=NULL, $context=NULL) {
+function forum_user_can_post_discussion($forum, $currentgroup=-1, $groupmode=-1, $cm=NULL, $context=NULL) {
 // $forum is an object
     global $USER, $SESSION;
 
@@ -3224,6 +3228,17 @@
         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     }
 
+    if ($currentgroup == -1) {
+        $currentgroup = get_current_group($cm->course);
+    }
+
+    if ($groupmode == -1) {
+        if (!$course = get_record('course', 'id', $cm->course)) {
+            error('Can not find course');
+        }
+        $groupmode = groupmode($course, $cm);
+    }
+
     if ($forum->type == 'news') {
         $capname = 'mod/forum:addnews';
     } else {
@@ -3235,17 +3250,23 @@
     }
 
     if ($forum->type == 'eachuser') {
-        return (!forum_user_has_posted_discussion($forum->id, $USER->id));
-    } else if ($currentgroup) {
-        return (has_capability('moodle/site:accessallgroups', $context)
-                or ismember($currentgroup));
+        if (forum_user_has_posted_discussion($forum->id, $USER->id)) {
+            return false;
+        }
+    }
+
+    if (!$groupmode or has_capability('moodle/site:accessallgroups', $context)) {
+        return true;
+    }
+
+    if ($currentgroup) {
+        return ismember($currentgroup);
     } else {
         //else it might be group 0 in visible mode
         if ($groupmode == VISIBLEGROUPS){
-            return (ismember($currentgroup));
-        }
-        else {
             return true;
+        } else {
+            return false;
         }
     }
 }
@@ -3435,21 +3456,8 @@
 // Decide if current user is allowed to see ALL the current discussions or not
 
 // First check the group stuff
-
-    if ($groupmode == -1) {    // We need to reconstruct groupmode because none was given
-        $groupmode = groupmode($course, $cm);   // Works even if $cm is not valid
-    }
-
-    if ($currentgroup == -1) {    // We need to reconstruct currentgroup because none was given
-        $currentgroup = get_current_group($course->id);
-    }
-
-    if (!$currentgroup and 
-       ($groupmode != SEPARATEGROUPS or has_capability('moodle/site:accessallgroups', $context)) ) {
-        $visiblegroups = -1;
-    } else {
-        $visiblegroups = $currentgroup;
-    }
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = get_and_set_current_group($course, $groupmode);
 
 // If the user can post discussions, then this is a good place to put the
 // button for it. We do not show the button if we are showing site news
@@ -3480,7 +3488,7 @@
 
     $getuserlastmodified = ($displayformat == 'header');
 
-    if (! $discussions = forum_get_discussions($forum->id, $sort, 0, $fullpost, $visiblegroups,0,$getuserlastmodified) ) {
+    if (! $discussions = forum_get_discussions($forum->id, $sort, 0, $fullpost, $currentgroup,0,$getuserlastmodified) ) {
         echo '<div class="forumnodiscuss">';
         if ($forum->type == 'news') {
             echo '('.get_string('nonews', 'forum').')';
Index: mod/forum/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/index.php,v
retrieving revision 1.90.2.2
diff -u -r1.90.2.2 index.php
--- mod/forum/index.php	5 Mar 2007 02:14:17 -0000	1.90.2.2
+++ mod/forum/index.php	15 May 2007 07:48:46 -0000
@@ -18,7 +18,7 @@
     }
 
     require_course_login($course);
-    $currentgroup = get_current_group($course->id);
+    $currentgroup = get_and_set_current_group($course, groupmode($course));
     $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
 
 
Index: mod/forum/discuss.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/discuss.php,v
retrieving revision 1.95.2.7
diff -u -r1.95.2.7 discuss.php
--- mod/forum/discuss.php	13 Mar 2007 04:47:30 -0000	1.95.2.7
+++ mod/forum/discuss.php	15 May 2007 07:48:45 -0000
@@ -203,11 +203,7 @@
 
     echo '<table width="100%" class="discussioncontrols"><tr><td>';
 
-    if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', $modcontext))) {
-        if ($groups = groups_get_groups_names($course->id)) { //TODO:
-            print_group_menu($groups, $groupmode, $discussion->groupid, "view.php?id=$cm->id&amp;group=");
-        }
-    }
+    // groups selector not needed here
 
     echo "</td><td>";
     forum_print_mode_form($discussion->id, $displaymode);
Index: user/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/user/index.php,v
retrieving revision 1.162.2.11
diff -u -r1.162.2.11 index.php
--- user/index.php	9 May 2007 10:02:07 -0000	1.162.2.11
+++ user/index.php	15 May 2007 07:49:01 -0000
@@ -192,13 +192,9 @@
         echo '</td>';
     }
 
-    if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', $context))) {
-        if ($groups_names = groups_get_groups_names($course->id)) { //TODO:
-            echo '<td class="left">';
-            print_group_menu($groups_names, $groupmode, $currentgroup, $baseurl);
-            echo '</td>';
-        }
-    }
+    echo '<td class="left">';
+    setup_and_print_groups($course, $groupmode, $baseurl);
+    echo '</td>';
 
     // get minimum lastaccess for this course and display a dropbox to filter by lastaccess going back this far.
     // this might not work anymore because you always going to get yourself as the most recent entry? added $USER!=$user ch
Index: mod/hotpot/report.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/hotpot/report.php,v
retrieving revision 1.26.2.2
diff -u -r1.26.2.2 report.php
--- mod/hotpot/report.php	17 Mar 2007 13:28:34 -0000	1.26.2.2
+++ mod/hotpot/report.php	15 May 2007 07:48:56 -0000
@@ -598,10 +598,8 @@
     $users = array();
 
     /// Check to see if groups are being used in this module
-    $currentgroup = false;
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id&mode=simple");
-    }
+    $groupmode = groupmode($course, $cm); //TODO: there is no $cm defined!
+    $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id&mode=simple");
 
     $sort = "u.lastname ASC";
 
Index: mod/chat/gui_header_js/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/chat/gui_header_js/index.php,v
retrieving revision 1.24.2.1
diff -u -r1.24.2.1 index.php
--- mod/chat/gui_header_js/index.php	28 Feb 2007 05:36:19 -0000	1.24.2.1
+++ mod/chat/gui_header_js/index.php	15 May 2007 07:48:38 -0000
@@ -32,7 +32,7 @@
 /// Check to see if groups are being used here
      if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
         if ($groupid = get_and_set_current_group($course, $groupmode, $groupid)) {
-            if (! groups_group_exists($groupid)) {
+            if (!$group = groups_get_group($groupid, false)) {
                 error("That group (id $groupid) doesn't exist!");
             }
             $groupname = ': '.$group->name;
Index: mod/assignment/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/assignment/index.php,v
retrieving revision 1.28
diff -u -r1.28 index.php
--- mod/assignment/index.php	3 Jan 2007 04:55:21 -0000	1.28
+++ mod/assignment/index.php	15 May 2007 07:48:34 -0000
@@ -43,7 +43,7 @@
         $table->align = array ("left", "left", "left", "right");
     }
 
-    $currentgroup = get_current_group($course->id);
+    $currentgroup = get_and_set_current_group($course, groupmode($course));
     if ($currentgroup and has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $id))) {
         $group = groups_get_group($currentgroup, false);
         $groupname = " ($group->name)";
Index: mod/assignment/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/assignment/lib.php,v
retrieving revision 1.219.2.9
diff -u -r1.219.2.9 lib.php
--- mod/assignment/lib.php	16 Apr 2007 14:43:37 -0000	1.219.2.9
+++ mod/assignment/lib.php	15 May 2007 07:48:38 -0000
@@ -94,7 +94,7 @@
                              "", "", true, '', navmenu($this->course, $this->cm));
                 notice(get_string("activityiscurrentlyhidden"), "$CFG->wwwroot/course/view.php?id={$this->course->id}");
             }
-            $this->currentgroup = get_current_group($this->course->id);
+            $this->currentgroup = get_and_set_current_group($this->course, groupmode($this->course, $this->cm));
         }
 
     /// Set up things for a HTML editor if it's needed
@@ -154,6 +154,9 @@
                      true, update_module_button($this->cm->id, $this->course->id, $this->strassignment), 
                      navmenu($this->course, $this->cm));
 
+        $groupmode = groupmode($this->course, $this->cm);
+        $currentgroup = setup_and_print_groups($this->course, $groupmode, 'view.php?id=' . $this->cm->id);
+        
         echo '<div class="reportlink">'.$this->submittedlink().'</div>';
     }
 
@@ -794,16 +797,11 @@
         $cm         = $this->cm;
         $context    = get_context_instance(CONTEXT_MODULE, $cm->id);
 
-    /// Get all teachers and students
+        /// Get all ppl that can submit assignments
 
-        $currentgroup = get_current_group($course->id);
+        $currentgroup = get_and_set_current_group($course, groupmode($course, $cm));
 
-        if ($currentgroup) {
-            $users = get_group_users($currentgroup);
-        } else {
-            $users = get_users_by_capability($context, 'mod/assignment:submit', 'u.id, u.id', '',
-                                 '', '', '', '', false);
-        }
+        $users = get_users_by_capability($context, 'mod/assignment:submit', 'u.id, u.id', '', '', '', $currentgroup, '', false);
 
         $select = 'SELECT u.id, u.firstname, u.lastname, u.picture,
                           s.id AS submissionid, s.grade, s.submissioncomment, 
@@ -996,44 +994,14 @@
         
         print_header_simple(format_string($this->assignment->name,true), "", '<a href="index.php?id='.$course->id.'">'.$this->strassignments.'</a> -> <a href="view.php?a='.$this->assignment->id.'">'.format_string($this->assignment->name,true).'</a> -> '. $this->strsubmissions, '', '', true, update_module_button($cm->id, $course->id, $this->strassignment), navmenu($course, $cm));
 
-    ///Position swapped
-    /*
-        if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-            $currentgroup = setup_and_print_groups($course, $groupmode, 'submissions.php?id='.$this->cm->id);
-        } else {
-            $currentgroup = false;
-        }
-    */
-        
-        /// copied code from assignment module, if this is not the way to do this please change it
-        /// the above code does not work
-        /// set_and_print_groups() is not fully implemented as function groups_instance_print_grouping_selector()
-        /// and function groups_instance_print_group_selector() are missing.
-       
         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-        $changegroup = optional_param('group', -1, PARAM_INT);   // choose the current group
+
+        /// find out current groups mode
         $groupmode = groupmode($course, $cm);
-        $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);   
-    
-        /// Now we need a menu for separategroups as well!
-        if ($groupmode == VISIBLEGROUPS || ($groupmode
-            && has_capability('moodle/site:accessallgroups', $context))) {
-        
-            //the following query really needs to change
-            if ($groups = groups_get_groups_names($course->id)) { //TODO:
-                print_box_start('groupmenu');
-                print_group_menu($groups, $groupmode, $currentgroup, 'submissions.php?id='.$this->cm->id);
-                print_box_end(); // groupmenu
-            }
-        }
-   
-    /// Get all teachers and students
-        if ($currentgroup) {
-            $users = get_group_users($currentgroup);
-        } else {
-            $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-            $users = get_users_by_capability($context, 'mod/assignment:submit'); // everyone with this capability set to non-prohibit
-        }
+        $currentgroup = setup_and_print_groups($course, $groupmode, 'submissions.php?id=' . $this->cm->id);
+
+        /// Get all ppl that are allowed to submit assignments
+        $users = get_users_by_capability($context, 'mod/assignment:submit', '', '', '', '', $currentgroup, '', false);
 
         $tablecolumns = array('picture', 'fullname', 'grade', 'submissioncomment', 'timemodified', 'timemarked', 'status');
         $tableheaders = array('', get_string('fullname'), get_string('grade'), get_string('comment', 'assignment'), get_string('lastmodified').' ('.$course->student.')', get_string('lastmodified').' ('.$course->teacher.')', get_string('status'));
@@ -1420,8 +1388,8 @@
      */
     function get_graders($user) {
         //potential graders
-        $potgraders = get_users_by_capability($this->context, 'mod/assignment:grade', $fields='', $sort='', $limitfrom='', 
-                                               $limitnum='', $groups='', $exceptions='', $doanything=false, $view=false);
+        $potgraders = get_users_by_capability($this->context, 'mod/assignment:grade', '', '', '', '', '', '', false, false);
+
         $graders = array();
         if (groupmode($this->course, $this->cm) == SEPARATEGROUPS) {   // Separate groups are being used
             if ($groups = user_group($this->course->id, $user->id)) {  // Try to find all groups
@@ -2247,7 +2215,7 @@
     global $CFG;
 
     if ($groupid) {     /// How many in a particular group?
-        return count_records_sql("SELECT COUNT(DISTINCT g.userid, g.groupid)
+        return count_records_sql("SELECT COUNT(DISTINCT gm.userid, gm.groupid)
                                      FROM {$CFG->prefix}assignment_submissions a,
                                           ".groups_members_from_sql()."
                                     WHERE a.assignment = $assignment->id 
@@ -2258,7 +2226,7 @@
         $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 
         // this is all the users with this capability set, in this context or higher
-        if ($users = get_users_by_capability($context, 'mod/assignment:submit')) {
+        if ($users = get_users_by_capability($context, 'mod/assignment:submit', '', '', '', '', 0, '', false)) {
             foreach ($users as $user) {
                 $array[] = $user->id;
             }
@@ -2477,8 +2445,8 @@
             
             // count how many people can submit
             $submissions = 0; // init
-            if ($students = get_users_by_capability($context, 'mod/assignment:submit')) {
-                foreach ($students as $student) {
+            if ($students = get_users_by_capability($context, 'mod/assignment:submit', '', '', '', '', 0, '', false)) {
+                 foreach ($students as $student) {
                     if (get_records_sql("SELECT id,id FROM {$CFG->prefix}assignment_submissions
                                          WHERE assignment = $assignment->id AND
                                                userid = $student->id AND
Index: mod/data/db/access.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/data/db/access.php,v
retrieving revision 1.11.4.2
diff -u -r1.11.4.2 access.php
--- mod/data/db/access.php	5 Mar 2007 11:38:21 -0000	1.11.4.2
+++ mod/data/db/access.php	15 May 2007 07:48:45 -0000
@@ -140,7 +140,6 @@
         'captype' => 'write',
         'contextlevel' => CONTEXT_MODULE,
         'legacy' => array(
-            'teacher' => CAP_ALLOW,
             'editingteacher' => CAP_ALLOW,
             'admin' => CAP_ALLOW
         )
Index: mod/choice/report.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/report.php,v
retrieving revision 1.52
diff -u -r1.52 report.php
--- mod/choice/report.php	9 Jan 2007 03:14:34 -0000	1.52
+++ mod/choice/report.php	15 May 2007 07:48:40 -0000
@@ -38,11 +38,18 @@
         redirect("report.php?id=$cm->id");                      
     }
         
-    if ($download <> "xls" and $download <> "txt" and $download <> "ods") {
+    if (!$download) {
         print_header_simple(format_string($choice->name).": $strresponses", "",
                  "<a href=\"index.php?id=$course->id\">$strchoices</a> ->
                   <a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "", '', true,
                   update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
+
+        /// Check to see if groups are being used in this choice
+        $groupmode = groupmode($course, $cm);
+        setup_and_print_groups($course, $groupmode, 'report.php?id='.$id);
+    } else {
+        $groupmode = groupmode($course, $cm);
+        get_and_set_current_group($course, $groupmode);
     }
 
     $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC');
Index: mod/choice/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/lib.php,v
retrieving revision 1.53.2.1
diff -u -r1.53.2.1 lib.php
--- mod/choice/lib.php	24 Apr 2007 20:08:16 -0000	1.53.2.1
+++ mod/choice/lib.php	15 May 2007 07:48:39 -0000
@@ -278,12 +278,17 @@
 }
 
 
-function choice_show_reportlink($choice, $courseid, $cmid) {
-    $context = get_context_instance(CONTEXT_MODULE, $cmid);
-    if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id)) {
+function choice_show_reportlink($choice, $courseid, $cmid, $groupmode) {
+    //TODO: rewrite with SQL
+    $currentgroup = get_current_group($courseid);
+    if ($allanswers = get_records("choice_answers", "choiceid", $choice->id)) {
         $responsecount = 0;
         foreach ($allanswers as $aa) {
-            if (has_capability('mod/choice:readresponses', $context)) {
+            if ($groupmode and $currentgroup) {
+                if (ismember($currentgroup, $aa->userid)) {
+                    $responsecount++;
+                }
+            } else {
                 $responsecount++;
             }
         }
@@ -299,34 +304,34 @@
 
     global $CFG, $COLUMN_HEIGHT, $USER;
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+
     print_heading(get_string("responses", "choice"));
+
     if (empty($forcepublish)) { //alow the publish setting to be overridden
         $forcepublish = $choice->publish;
     }
 
-        /// Check to see if groups are being used in this choice
-    if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
-        $currentgroup = setup_and_print_groups($course, $groupmode, $_SERVER['PHP_SELF']."?id=$cm->id");
-    } else {
-        $currentgroup = false;
-    }
+    $groupmode = groupmode($course, $cm);
+    $currentgroup = get_current_group($course->id);
 
-    if ($currentgroup) {
-        $users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber');
-    } else {
-        $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC');
-    }
+    $users = get_users_by_capability($context, 'mod/choice:choose', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber', 'u.firstname ASC', '', '', $currentgroup, '', false);
 
     if (!$users) {
         print_heading(get_string("nousersyet"));
     }
 
+    $answers = array () ;
     if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
         foreach ($allresponses as $aa) {
-            $answers[$aa->userid] = $aa;
+            //TODO: rewrite with SQL
+            if ($groupmode and $currentgroup) {
+                if (ismember($currentgroup, $aa->userid)) {
+                    $answers[$aa->userid] = $aa;
+                }
+            } else {
+                $answers[$aa->userid] = $aa;
+            }
         }
-    } else {
-        $answers = array () ;
     }
 
     $timenow = time();
@@ -396,10 +401,7 @@
                 // MDL-7861
                 echo "<table class=\"choiceresponse\"><tr><td></td></tr>";
                 foreach ($userlist as $user) {
-                    // this needs to be fixed
-                    // hide admin/editting teacher (users with editting privilages)
-                    // show users without? I could be wrong.
-                    if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) { // make sure admins and hidden teachers are not shown in not answered yet column.
+                    if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id, false)) {
                         $columncount[$optionid] += 1;
                         echo "<tr>";
                         if (has_capability('mod/choice:readresponses', $context) && $optionid!=0) {
@@ -489,7 +491,7 @@
                 }
                 $column[$optionid] = 0;
                 foreach ($userlist as $user) {
-                    if (!($optionid==0 && has_capability('mod/choice:readresponses', $context, $user->id))) { //make sure admins and hidden teachers are not shown in not answered yet column.
+                    if ($optionid!=0 or has_capability('mod/choice:choose', $context, $user->id, false)) {
                          $column[$optionid]++;
                     }
                 }
Index: mod/choice/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/choice/view.php,v
retrieving revision 1.93.2.1
diff -u -r1.93.2.1 view.php
--- mod/choice/view.php	28 Feb 2007 05:36:29 -0000	1.93.2.1
+++ mod/choice/view.php	15 May 2007 07:48:40 -0000
@@ -59,11 +59,13 @@
                   update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
 
     add_to_log($course->id, "choice", "view", "view.php?id=$cm->id", $choice->id, $cm->id);
-                                                      
+
+    /// Check to see if groups are being used in this choice
+    $groupmode = groupmode($course, $cm);
+    setup_and_print_groups($course, $groupmode, 'view.php?id='.$id);
+                                   
     if (has_capability('mod/choice:readresponses', $context)) {
-        choice_show_reportlink($choice, $course->id, $cm->id);
-    } else if (!$cm->visible) {
-        notice(get_string("activityiscurrentlyhidden"));
+        choice_show_reportlink($choice, $course->id, $cm->id, $groupmode);
     }
 
     if ($choice->text) {
Index: grade/exceptions.php
===================================================================
RCS file: /cvsroot/moodle/moodle/grade/exceptions.php,v
retrieving revision 1.11.6.1
diff -u -r1.11.6.1 exceptions.php
--- grade/exceptions.php	28 Feb 2007 05:36:17 -0000	1.11.6.1
+++ grade/exceptions.php	15 May 2007 07:48:20 -0000
@@ -14,11 +14,12 @@
 
     require_capability('moodle/course:managegrades', get_context_instance(CONTEXT_MODULE, $id));
 
-    $group = get_current_group($course->id);
-    
     print_header("$course->shortname: ".get_string('grades'), $course->fullname, grade_nav($course, $action));
 
-    grade_preferences_menu($action, $course, $group);
+    $groupmode = groupmode($course);
+    $group = setup_and_print_groups($course, $groupmode, 'exceptions.php?id=' . $course->id);
+
+    grade_preferences_menu($action, $course);
 
     grade_set_uncategorized();
 
Index: grade/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/grade/index.php,v
retrieving revision 1.10.2.1
diff -u -r1.10.2.1 index.php
--- grade/index.php	4 Apr 2007 04:03:27 -0000	1.10.2.1
+++ grade/index.php	15 May 2007 07:48:20 -0000
@@ -5,7 +5,6 @@
     $id       = required_param('id');              // course id
     $download = optional_param('download');
     $user     = optional_param('user', -1);
-    $group    = optional_param('group', -1);
     $action   = optional_param('action', 'grades');
     $cview    = optional_param('cview', -1);
 
@@ -14,15 +13,7 @@
     }
 
     require_login($course->id);
-    
-    /* 
-    if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $course->id))) {
-        $group = get_and_set_current_group($course, $course->groupmode, $group);
-    } else {
-        $group = get_current_group($course->id);
-    }
-    */
-   
+
     // if the user set new prefs make sure they happen now
     if ($action == 'set_grade_preferences' && $prefs = data_submitted()) {
         if (!confirm_sesskey()) {
@@ -50,29 +41,12 @@
     }
 
     print_header($course->shortname.': '.get_string('grades'), $course->fullname, grade_nav($course, $action));
-    
-    grade_preferences_menu($action, $course, $group);
 
-    /// copied code from assignment module, if this is not the way to do this please change it
-    /// the above code does not work
-    /// set_and_print_groups() is not fully implemented as function groups_instance_print_grouping_selector()
-    /// and function groups_instance_print_group_selector() are missing. 
-    $context = get_context_instance(CONTEXT_COURSE, $course->id);
-    $changegroup = optional_param('group', -1, PARAM_INT);   // choose the current group
+    /// find out current groups mode
     $groupmode = groupmode($course);
-    $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);   
-    
-    /// Now we need a menu for separategroups as well!
-    if ($groupmode == VISIBLEGROUPS || ($groupmode
-        && has_capability('moodle/site:accessallgroups', $context))) {
-        
-        //the following query really needs to change
-        if ($groups = groups_get_groups_names($course->id)) { //TODO:
-            print_box_start('groupmenu');
-            print_group_menu($groups, $groupmode, $currentgroup, 'index.php?id='.$course->id);
-            print_box_end(); // groupmenu
-        }
-    }
+    $currentgroup = setup_and_print_groups($course, $groupmode, 'index.php?id=' . $course->id);
+
+    grade_preferences_menu($action, $course);
 
     grade_set_uncategorized();
 
Index: grade/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/grade/lib.php,v
retrieving revision 1.65.2.2
diff -u -r1.65.2.2 lib.php
--- grade/lib.php	12 Mar 2007 07:00:01 -0000	1.65.2.2
+++ grade/lib.php	15 May 2007 07:48:26 -0000
@@ -311,12 +311,9 @@
         }
     
 
-        if (isset($_REQUEST['group'])) {
-            $group = clean_param($_REQUEST['group'], PARAM_INT);
-        }
         // if the user has selected a group to view by get the group members
-        if (isset($group) && $group != 0) {
-            $groupmembers = get_group_users($group);
+        if ($currentgroup = get_current_group($course->id)) {
+            $groupmembers = get_group_users($currentgroup);
         }
 
         // this next block catches any students who do not have a grade for any item in a particular category
@@ -1064,7 +1061,7 @@
 }
 
 
-function grade_preferences_menu($action, $course, $group=0) {
+function grade_preferences_menu($action, $course) {
 
     if (!has_capability('moodle/course:managegrades', get_context_instance(CONTEXT_COURSE, $course->id))) {
         return;
@@ -1206,17 +1203,7 @@
     $stractivityreport = get_string("activityreport");
 
 /// Check to see if groups are being used in this course
-    if ($groupmode = groupmode($course)) {   // Groups are being used
-        if (isset($_GET['group'])) {
-            $changegroup = $_GET['group'];  /// 0 or higher
-        } else {
-            $changegroup = -1;              /// This means no group change was specified
-        }
-
-        $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
-    } else {
-        $currentgroup = false;
-    }
+    $currentgroup = get_current_group($course->id);
 
     if ($currentgroup) {
         $students = get_group_students($currentgroup, "u.lastname ASC");
@@ -1741,7 +1728,6 @@
     global $course;
     global $USER;
     global $preferences;
-    global $group;
 
     $context = get_context_instance(CONTEXT_COURSE, $course->id);
     
@@ -1783,11 +1769,11 @@
                 $student_heading_link = get_string('student','grades');
                 //only set sorting links if more than one student displayed.
                 if ($view_by_student == -1) {
-                    $student_heading_link .='<br /><a href="?id='.$course->id.'&amp;group='.$group.'&amp;action=vcats&amp;cview='.$cview.'&amp;sort=lastname">'.get_string('sortbylastname','grades').'</a>';
-                    $student_heading_link .= '<br /><a href="?id='.$course->id.'&amp;group='.$group.'&amp;action=vcats&amp;cview='.$cview.'&amp;sort=firstname">'.get_string('sortbyfirstname','grades').'</a>';
+                    $student_heading_link .='<br /><a href="?id='.$course->id.'&amp;action=vcats&amp;cview='.$cview.'&amp;sort=lastname">'.get_string('sortbylastname','grades').'</a>';
+                    $student_heading_link .= '<br /><a href="?id='.$course->id.'&amp;action=vcats&amp;cview='.$cview.'&amp;sort=firstname">'.get_string('sortbyfirstname','grades').'</a>';
                 }
                 else {
-                    $student_heading_link .= '<br /><a href="?id='.$course->id.'&amp;group='.$group.'&amp;action=vcats&amp;cview='.$cview.'">'.get_string('showallstudents','grades').'</a>';
+                    $student_heading_link .= '<br /><a href="?id='.$course->id.'&amp;action=vcats&amp;cview='.$cview.'">'.get_string('showallstudents','grades').'</a>';
                 }
             }
             echo '<table align="center" class="grades">';
@@ -1848,7 +1834,7 @@
                         $student_link = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$student.'&amp;course='.$course->id.'">';
                     }
                     else {
-                        $student_link = '<a href="?id='.$course->id.'&amp;group='.$group.'&amp;action=vcats&amp;user='.$student.'&amp;cview='.$cview.'">';
+                        $student_link = '<a href="?id='.$course->id.'&amp;action=vcats&amp;user='.$student.'&amp;cview='.$cview.'">';
                     }
                     $student_link .= $grades_by_student[$student]['student_data']['lastname'].', '.$grades_by_student[$student]['student_data']['firstname'].'</a>';
                     $row .= '<th class="fullname" scope="row">'.$student_link.'</th>';
@@ -1933,14 +1919,14 @@
                 
                 if ($first == 0) {
                     if (has_capability('moodle/course:viewcoursegrades', $context) && $view_by_student == -1) {
-                        $total_sort_link = '<a href="?id='.$course->id.'&amp;group='.$group.'&amp;action=vcats&amp;cview='.$cview.'&amp;sort=highgrade_category"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('highgradedescending','grades').'" /></a>';
-                        $total_sort_link .= '<a href="?id='.$course->id.'&amp;group='.$group.'&amp;action=vcats&amp;cview='.$cview.'&amp;sort=highgrade_category_asc"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('highgradeascending','grades').'" /></a>';
+                        $total_sort_link = '<a href="?id='.$course->id.'&amp;action=vcats&amp;cview='.$cview.'&amp;sort=highgrade_category"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('highgradedescending','grades').'" /></a>';
+                        $total_sort_link .= '<a href="?id='.$course->id.'&amp;action=vcats&amp;cview='.$cview.'&amp;sort=highgrade_category_asc"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('highgradeascending','grades').'" /></a>';
                     }
                     else {
                         $total_sort_link = '';
                     }
                     
-                    $stats_link = '<a href="javascript:void(0)" onclick="window.open(\'?id='.$course->id.'&amp;action=stats&amp;group='.$group.'&amp;category='.$cview.'\',\''.get_string('statslink','grades').'\',\'height=200,width=300,scrollbars=no\')">'.get_string('statslink','grades').'</a>';
+                    $stats_link = '<a href="javascript:void(0)" onclick="window.open(\'?id='.$course->id.'&amp;action=stats&amp;category='.$cview.'\',\''.get_string('statslink','grades').'\',\'height=200,width=300,scrollbars=no\')">'.get_string('statslink','grades').'</a>';
                     if ($all_categories[$cview]['stats']['drop'] != 0) {
                         $header .= '<th class="'.$class.'" colspan="'.$grade_columns.'" scope="col">'.get_string('total','grades').'&nbsp; (Lowest '. $all_categories[$cview]['stats']['drop']. ' Dropped)'.$total_sort_link.' '.$stats_link.'</th>';
                     }
@@ -2039,7 +2025,6 @@
     global $CFG;
     global $course;
     global $USER;
-    global $group; // yu: fix for 5814
     global $preferences;
     
     if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
@@ -2078,8 +2063,8 @@
         if (has_capability('moodle/course:viewcoursegrades', $context)) {
             $student_heading_link = get_string('student','grades');
             if ($view_by_student == -1) {
-                $student_heading_link .='<a href="?id='.$course->id.'&amp;action=grades&amp;sort=lastname&amp;group='.$group.'"><br /><font size="-2">'.get_string('sortbylastname','grades').'</font></a>';
-                $student_heading_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=firstname&amp;group='.$group.'"><br /><font size="-2">'.get_string('sortbyfirstname','grades').'</font></a>';
+                $student_heading_link .='<a href="?id='.$course->id.'&amp;action=grades&amp;sort=lastname"><br /><font size="-2">'.get_string('sortbylastname','grades').'</font></a>';
+                $student_heading_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=firstname"><br /><font size="-2">'.get_string('sortbyfirstname','grades').'</font></a>';
             }
             else {
                 $student_heading_link .= '<br /><a href="?id='.$course->id.'&amp;&amp;action=grades"><font size="-2">'.get_string('showallstudents','grades').'</font></a>';
@@ -2184,14 +2169,14 @@
                 }
                 
                 if (has_capability('moodle/course:viewcoursegrades', $context) && $view_by_student == -1) {
-                    $grade_sort_link = '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=highgrade&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('highgradedescending','grades').'" /></a>';
-                    $grade_sort_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=highgrade_asc&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('highgradeascending','grades').'" /></a>';
-                    $points_sort_link = '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=points&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('pointsdescending','grades').'" /></a>';
-                    $points_sort_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=points_asc&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('pointsascending','grades').'" /></a>';
-                    $weighted_sort_link = '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=weighted&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('weighteddescending','grades').'" /></a>';
-                    $weighted_sort_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=weighted_asc&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('weightedascending','grades').'" /></a>';
-                    $percent_sort_link = '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=percent&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('percentdescending','grades').'" /></a>';
-                    $percent_sort_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=percent_asc&amp;group='.$group.'"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('percentascending','grades').'" /></a>';
+                    $grade_sort_link = '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=highgrade"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('highgradedescending','grades').'" /></a>';
+                    $grade_sort_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=highgrade_asc"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('highgradeascending','grades').'" /></a>';
+                    $points_sort_link = '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=points"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('pointsdescending','grades').'" /></a>';
+                    $points_sort_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=points_asc"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('pointsascending','grades').'" /></a>';
+                    $weighted_sort_link = '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=weighted"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('weighteddescending','grades').'" /></a>';
+                    $weighted_sort_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=weighted_asc"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('weightedascending','grades').'" /></a>';
+                    $percent_sort_link = '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=percent"><img src="'.$CFG->wwwroot.'/pix/t/down.gif" alt="'.get_string('percentdescending','grades').'" /></a>';
+                    $percent_sort_link .= '<a href="?id='.$course->id.'&amp;action=grades&amp;sort=percent_asc"><img src="'.$CFG->wwwroot.'/pix/t/up.gif" alt="'.get_string('percentascending','grades').'" /></a>';
                 }
                 $stats_link = '<a href="javascript:void(0)" onclick="window.open(\'?id='.$course->id.'&amp;action=stats&amp;category=all\',\''.get_string('statslink','grades').'\',\'height=200,width=300,scrollbars=no\')"><font size="-2">'.get_string('statslink','grades').'</font></a>';
                 $header .= '<th colspan="'.$total_columns.'" scope="col">'.get_string('total','grades').'&nbsp;'.$stats_link.'</th>';
@@ -3010,7 +2995,6 @@
                $url .= '&amp;cview='.$cview;
             }
         }
-        setup_and_print_groups($course, $course->groupmode, $url);
         echo '</td>';
 
         echo '</tr></table>';
Index: lib/weblib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/weblib.php,v
retrieving revision 1.812.2.55
diff -u -r1.812.2.55 weblib.php
--- lib/weblib.php	11 May 2007 13:54:28 -0000	1.812.2.55
+++ lib/weblib.php	15 May 2007 07:48:34 -0000
@@ -4295,21 +4295,28 @@
 function print_group_menu($groups, $groupmode, $currentgroup, $urlroot, $showall=1, $return=false) {
 
     $output = '';
+    $groupsmenu = array();
 
 /// Add an "All groups" to the start of the menu
     if ($showall){
         $groupsmenu[0] = get_string('allparticipants');
     }
-    foreach ($groups as $key => $groupname) {
-        $groupsmenu[$key] = $groupname;
+    foreach ($groups as $key => $group) {
+        $groupsmenu[$key] = format_string($group->name);
     }
 
     if ($groupmode == VISIBLEGROUPS) {
-        $grouplabel = get_string('groupsvisible').':';
+        $grouplabel = get_string('groupsvisible');
     } else {
-        $grouplabel = get_string('groupsseparate').':';
+        $grouplabel = get_string('groupsseparate');
+    }
+
+    if (count($groupsmenu) == 1) {
+        $groupname = reset($groupsmenu);
+        $output .= $grouplabel.': '.$groupname;
+    } else {
+        $output .= popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', $currentgroup, '', '', '', true, 'self', $grouplabel);
     }
-    $output .= popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', $currentgroup, '', '', '', true, 'self', $grouplabel);
 
     if ($return) {
         return $output;
Index: mod/glossary/mod_form.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/glossary/mod_form.php,v
retrieving revision 1.13
diff -u -r1.13 mod_form.php
--- mod/glossary/mod_form.php	16 Jan 2007 15:36:34 -0000	1.13
+++ mod/glossary/mod_form.php	15 May 2007 07:48:56 -0000
@@ -143,7 +143,7 @@
         $mform->disabledIf('assesstimefinish', 'ratingtime');
 
 //-------------------------------------------------------------------------------
-        $this->standard_coursemodule_elements();
+        $this->standard_coursemodule_elements(false);
 
 //-------------------------------------------------------------------------------
         // buttons
Index: mod/quiz/report/analysis/report.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/quiz/report/analysis/report.php,v
retrieving revision 1.30.2.4
diff -u -r1.30.2.4 report.php
--- mod/quiz/report/analysis/report.php	24 Apr 2007 16:59:24 -0000	1.30.2.4
+++ mod/quiz/report/analysis/report.php	15 May 2007 07:48:57 -0000
@@ -26,12 +26,10 @@
             if (!$download) {
                 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id&amp;mode=analysis");
             } else {
-                $changegroup = optional_param('group', -1, PARAM_INT);
-
-                $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
+                $currentgroup = get_and_set_current_group($course, $groupmode);
             }
         } else {
-            $currentgroup = false;
+            $currentgroup = get_and_set_current_group($course, $groupmode);
         }
 
         // set Table and Analysis stats options
@@ -76,13 +74,18 @@
             $usermax = get_records_sql_menu($sql);
         }
 
-        $sql = 'SELECT  qa.* FROM '.$CFG->prefix.'user u '.
-            'JOIN '.$CFG->prefix.'quiz_attempts qa ON u.id = qa.userid ';
-        if (!empty($currentgroup)) {
-            $sql .= groups_members_join_sql($currentgroup);
+        $groupmembers = '';
+        $groupwhere = '';
+
+        //Add this to the SQL to show only group users
+        if ($currentgroup) {
+            $groupmembers = ', '.groups_members_from_sql();
+            $groupwhere = ' AND '.groups_members_where_sql($currentgroup, 'u.id');
         }
-        $sql .= ' WHERE qa.quiz = '.$quiz->id.  // ULPGC ecastro
-            ' AND ( qa.sumgrades >= '.$scorelimit.' ) ';
+
+        $sql = 'SELECT  qa.* FROM '.$CFG->prefix.'quiz_attempts qa, '.$CFG->prefix.'user u '.$groupmembers.
+                 'WHERE u.id = qa.userid AND qa.quiz = '.$quiz->id.' AND ( qa.sumgrades >= '.$scorelimit.' ) '.$groupwhere;
+
         // ^^^^^^ es posible seleccionar aqu� TODOS los quizzes, como quiere Jussi,
         // pero habr�a que llevar la cuenta ed cada quiz para restaura las preguntas (quizquestions, states)
 
Index: group/index.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/index.php,v
retrieving revision 1.13.2.11
diff -u -r1.13.2.11 index.php
--- group/index.php	5 Apr 2007 10:46:20 -0000	1.13.2.11
+++ group/index.php	15 May 2007 07:48:27 -0000
@@ -27,6 +27,11 @@
 $userid     = optional_param('user', false, PARAM_INT);
 $action = groups_param_action();
 
+if (empty($CFG->enablegroupings)) {
+    // NO GROUPINGS YET!
+    $groupingid = GROUP_NOT_IN_GROUPING;
+}
+
 if ($groupid) {
     $groupingsforgroup = groups_get_groupings_for_group($groupid);
     if ($groupingsforgroup) {
@@ -201,6 +206,11 @@
 */
     echo '<table cellpadding="6" class="generaltable generalbox groupmanagementtable boxaligncenter" summary="">'."\n";
     echo '<tr>'."\n";
+
+if (empty($CFG->enablegroupings)) {
+// NO GROUPIGS YET!
+    $sel_groupingid = -1;
+} else {
     echo '<td class="generalboxcontent">'."\n";
     echo '<p><label for="groupings">' .  get_string('groupings', 'group') . '<span id="dummygrouping">&nbsp;</span></label></p>'."\n";
     echo '<select name="grouping" id="groupings" size="15" class="select"';
@@ -265,7 +275,9 @@
     
     echo '<p><input type="submit" ' . $printerfriendly_disabled . ' name="act_printerfriendly" id="printerfriendly" value="'
             . get_string('printerfriendly', 'group') . '" /></p>'."\n";
-    echo "</td>\n<td>\n";
+    echo "</td>\n";
+}
+    echo "<td>\n";
     echo '<p><label for="groups"><span id="groupslabel">'.get_string('groupsinselectedgrouping', 'group').' </span><span id="thegrouping">'.get_string('grouping', 'group').'</span></label></p>'."\n";
     echo '<select name="group" id="groups" size="15" class="select" onchange="membersCombo.refreshMembers(this.options[this.selectedIndex].value);"'."\n";
     echo ' onclick="window.status=this.options[this.selectedIndex].title;" onmouseout="window.status=\'\';">'."\n";
@@ -307,7 +319,14 @@
         echo '<p><input type="submit" '.$disabled.' name="act_removegroup" '
                 . 'id="removegroup" value="' . get_string('removegroupfromselectedgrouping', 'group') . '" /></p>'."\n";
     }
-    
+
+if (empty($CFG->enablegroupings)) {
+// NO GROUPIGS YET!
+    echo '<p><input type="submit" name="act_showcreateorphangroupform" id="showcreateorphangroupform" value="'
+            . get_string('creategroup', 'group') . '" /></p>'."\n";
+    echo '<p><input type="submit" name="act_printerfriendly" id="printerfriendly" value="'
+            . get_string('printerfriendly', 'group') . '" /></p>'."\n";
+} else {    
     echo '<p><input type="submit" ' . $showcreategroupform_disabled . ' name="act_showcreategroupform" id="showcreategroupform" value="'
             . get_string('creategroupinselectedgrouping', 'group') . '" /></p>'."\n";
     
@@ -318,7 +337,8 @@
         echo '<p><input type="submit" '.$disabled.' name="act_addgroupstogroupingform" '
                 . 'id="showaddgroupstogroupingform" value="' . get_string('addgroupstogrouping', 'group') . '" /></p>'."\n";
     }
-    
+}
+
     echo '</td>'."\n";
     echo '<td>'."\n";
     echo '<p><label for="members"><span id="memberslabel">'.get_string('membersofselectedgroup', 'group').' </span><span id="thegroup">'.get_string('group', 'group').'</span></label></p>'."\n";
Index: group/grouping.php
===================================================================
RCS file: /cvsroot/moodle/moodle/group/grouping.php,v
retrieving revision 1.4.2.4
diff -u -r1.4.2.4 grouping.php
--- group/grouping.php	3 Apr 2007 06:38:40 -0000	1.4.2.4
+++ group/grouping.php	15 May 2007 07:48:26 -0000
@@ -18,6 +18,11 @@
 
 $delete = optional_param('delete', false, PARAM_BOOL);
 
+if (empty($CFG->enablegroupings)) {
+    // NO GROUPIGS YET!
+    error('No groupings yet');
+}
+
 // Get the course information so we can print the header and
 // check the course id is valid
 $course = groups_get_course_info($courseid);
Index: blocks/news_items/block_news_items.php
===================================================================
RCS file: /cvsroot/moodle/moodle/blocks/news_items/block_news_items.php,v
retrieving revision 1.19.2.2
diff -u -r1.19.2.2 block_news_items.php
--- blocks/news_items/block_news_items.php	15 Apr 2007 07:24:36 -0000	1.19.2.2
+++ blocks/news_items/block_news_items.php	15 May 2007 07:48:17 -0000
@@ -29,46 +29,29 @@
             $text = '';
 
             if (!$forum = forum_get_course_forum($COURSE->id, 'news')) {
-                return $this->content;
+                return '';
             }
 
+            if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $COURSE->id)) {
+                return '';
+            }
 
-        /// First work out whether we can post to this group and if so, include a link
+            $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 
-            if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_COURSE, $COURSE->id))) {     /// Teachers can always post
-                $visiblegroups = -1; 
+        /// First work out whether we can post to this group and if so, include a link
+            $groupmode    = groupmode($COURSE, $cm);
+            $currentgroup = get_and_set_current_group($COURSE, $groupmode);
+            
 
+            if (forum_user_can_post_discussion($forum, $currentgroup, $groupmode, $cm, $context)) {
                 $text .= '<div class="newlink"><a href="'.$CFG->wwwroot.'/mod/forum/post.php?forum='.$forum->id.'">'.
                           get_string('addanewtopic', 'forum').'</a>...</div>';
-
-            } else {                              /// Check the group situation
-                $currentgroup = get_current_group($COURSE->id);
-
-                if (forum_user_can_post_discussion($forum, $currentgroup)) {
-                    $text .= '<div align="center" class="newlink"><a href="'.$CFG->wwwroot.'/mod/forum/post.php?forum='.$forum->id.'">'.
-                              get_string('addanewtopic', 'forum').'</a>...</div>';
-                }
-
-                if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $COURSE->id)) {
-                    $this->content->text = $text;
-                    return $this->content;
-                }
-    
-                $groupmode = groupmode($COURSE, $cm);
-    
-                /// Decides if current user is allowed to see ALL the current discussions or not
-    
-                if (!$currentgroup and ($groupmode != SEPARATEGROUPS) ) {
-                    $visiblegroups = -1;
-                } else {
-                    $visiblegroups = $currentgroup;
-                }
             }
 
         /// Get all the recent discussions we're allowed to see
 
             if (! $discussions = forum_get_discussions($forum->id, 'p.modified DESC', 0, false, 
-                                                       $visiblegroups, $COURSE->newsitems) ) {
+                                                       $currentgroup, $COURSE->newsitems) ) {
                 $text .= '('.get_string('nonews', 'forum').')';
                 $this->content->text = $text;
                 return $this->content;
Index: mod/exercise/view.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/exercise/view.php,v
retrieving revision 1.31
diff -u -r1.31 view.php
--- mod/exercise/view.php	6 Jan 2007 19:23:05 -0000	1.31
+++ mod/exercise/view.php	15 May 2007 07:48:45 -0000
@@ -337,14 +337,7 @@
         /// Check to see if groups are being used in this exercise
         /// and if so, set $currentgroup to reflect the current group
         $groupmode = groupmode($course, $cm);   // Groups are being used?
-        $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
-
-        /// Allow the teacher to change groups (for this session)
-        if ($groupmode) {
-            if ($groups = groups_get_groups_names($course->id)) { //TODO:
-                print_group_menu($groups, $groupmode, $currentgroup, "view.php?id=$cm->id");
-            }
-        }
+        $currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
 
         print_heading_with_help(get_string("managingassignment", "exercise"), "managing", "exercise");
 
Index: mod/quiz/report/overview/report.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/quiz/report/overview/report.php,v
retrieving revision 1.75.2.5
diff -u -r1.75.2.5 report.php
--- mod/quiz/report/overview/report.php	12 Apr 2007 20:11:32 -0000	1.75.2.5
+++ mod/quiz/report/overview/report.php	15 May 2007 07:48:58 -0000
@@ -66,42 +66,17 @@
                 notify(get_string('numattempts', 'quiz', $a));
             }
         }
-        
-        /* 
-        // Check to see if groups are being used in this quiz
-        if ($groupmode = groupmode($course, $cm)) {   // Groups are being used
+
+        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+        /// find out current groups mode
+        if ($groupmode = groupmode($course, $cm)) { // Groups are being used
             if (!$download) {
                 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id&amp;mode=overview");
             } else {
-                $changegroup = optional_param('group', -1, PARAM_INT);
-
-                $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
+                $currentgroup = get_and_set_current_group($course, $groupmode);
             }
         } else {
-            $currentgroup = false;
-        }
-        */
-        
-        /// copied code from assignment module, if this is not the way to do this please change it
-        /// the above code does not work
-        /// set_and_print_groups() is not fully implemented as function groups_instance_print_grouping_selector()
-        /// and function groups_instance_print_group_selector() are missing.
-       
-        $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-        $changegroup = optional_param('group', -1, PARAM_INT);   // choose the current group
-        $groupmode = groupmode($course, $cm);
-        $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);   
-    
-        /// Now we need a menu for separategroups as well!
-        if ($groupmode == VISIBLEGROUPS || ($groupmode
-            && has_capability('moodle/site:accessallgroups', $context))) {
-        
-            //the following query really needs to change
-            if ($groups = groups_get_groups_names($course->id)) { //TODO:
-                print_box_start('groupmenu');
-                print_group_menu($groups, $groupmode, $currentgroup, "report.php?id=$cm->id&amp;mode=overview");
-                print_box_end(); // groupmenu
-            }
+            $currentgroup = get_and_set_current_group($course, $groupmode);
         }
 
         // Set table options
Index: blocks/blog_tags/block_blog_tags.php
===================================================================
RCS file: /cvsroot/moodle/moodle/blocks/blog_tags/block_blog_tags.php,v
retrieving revision 1.18.4.1
diff -u -r1.18.4.1 block_blog_tags.php
--- blocks/blog_tags/block_blog_tags.php	1 Mar 2007 11:17:33 -0000	1.18.4.1
+++ blocks/blog_tags/block_blog_tags.php	15 May 2007 07:48:17 -0000
@@ -132,7 +132,7 @@
 
                     case BLOG_GROUP_LEVEL:
                         $filtertype = 'group';
-                        $filterselect = get_current_group($this->instance->pageid);
+                        $filterselect = get_and_set_current_group($COURSE, groupmode($COURSE));
                     break;
 
                     case BLOG_COURSE_LEVEL:
