Index: course/modedit.php
===================================================================
RCS file: /globalcvs/ou-moodle/course/modedit.php,v
retrieving revision 1.6
diff -u -r1.6 modedit.php
--- course/modedit.php	5 Jan 2007 17:24:17 -0000	1.6
+++ course/modedit.php	23 Feb 2007 12:43:24 -0000
@@ -226,6 +226,11 @@
                 set_coursemodule_groupmode($fromform->coursemodule, $fromform->groupmode);
             }
 
+//ou-specific begins
+            if (isset($fromform->grouping)) {
+                set_coursemodule_grouping($fromform->coursemodule, $fromform->grouping);
+            }
+//ou-specific ends
 
             add_to_log($course->id, "course", "update mod",
                        "../mod/$fromform->modulename/view.php?id=$fromform->coursemodule",
@@ -260,6 +265,11 @@
                 $fromform->groupmode = $course->groupmode;  /// Default groupmode the same as course
             }
 
+//ou-specific begins
+            if (!isset($fromform->grouping)) { // to deal with pre-1.5 modules
+                $fromform->grouping = GROUP_ANY_GROUPING;  /// Default.
+            }
+//ou-specific ends
             $fromform->instance = $returnfromfunc;
 
             // course_modules and course_sections each contain a reference
Index: course/moodleform_mod.php
===================================================================
RCS file: /globalcvs/ou-moodle/course/moodleform_mod.php,v
retrieving revision 1.3
diff -u -r1.3 moodleform_mod.php
--- course/moodleform_mod.php	16 Jan 2007 17:49:19 -0000	1.3
+++ course/moodleform_mod.php	23 Feb 2007 12:43:24 -0000
@@ -62,13 +62,21 @@
      * Adds all the standard elements to a form to edit the settings for an activity module.
      *
      * @param bool $supportsgroups does this module support groups?
+//ou-specific begins
+     * @param bool $supportsgroupings Does this module support groupings? (Currently only the Wiki.)
      */
-    function standard_coursemodule_elements($supportsgroups=true){
+    function standard_coursemodule_elements($supportsgroups=true, $supportsgroupings=false){
+//ou-specific ends
         $mform =& $this->_form;
         $mform->addElement('header', '', get_string('modstandardels', 'form'));
         if ($supportsgroups){
             $mform->addElement('modgroupmode', 'groupmode', get_string('groupmode'));
         }
+//ou-specific begins
+        if ($supportsgroupings) {
+            $mform->addElement('modgrouping', 'grouping', get_string('grouping', 'group'), $this->_cm);            
+        }
+//ou-specific ends
         $mform->addElement('modvisible', 'visible', get_string('visible'));
 // ou-specific begins 
         $mform->addElement('modstealth', 'stealth', get_string('stealthedfromusers'));
Index: course/lib.php
===================================================================
RCS file: /globalcvs/ou-moodle/course/lib.php,v
retrieving revision 1.56
diff -u -r1.56 lib.php
--- course/lib.php	18 Jan 2007 17:51:27 -0000	1.56
+++ course/lib.php	23 Feb 2007 12:43:24 -0000
@@ -1960,6 +1960,15 @@
     return set_field("course_modules", "groupmode", $groupmode, "id", $id);
 }
 
+//ou-specific begins
+/**
+ * Set which, if any, grouping (collection of groups) to use for an activity.
+ */
+function set_coursemodule_grouping($id, $groupingid) {
+    return set_field('course_modules', 'groupingid', $groupingid, 'id', $id);
+}
+//ou-specific ends
+
 /**
 * $prevstateoverrides = true will set the visibility of the course module
 * to what is defined in visibleold. This enables us to remember the current
Index: mod/wiki/view.php
===================================================================
RCS file: /globalcvs/ou-moodle/mod/wiki/view.php,v
retrieving revision 1.13
diff -u -r1.13 view.php
--- mod/wiki/view.php	22 Feb 2007 13:39:03 -0000	1.13
+++ mod/wiki/view.php	23 Feb 2007 12:43:25 -0000
@@ -64,8 +64,11 @@
 
     require_course_login($course, true, $cm);
     
-    /// Add the course module 'groupmode' to the wiki object, for easy access.
+    /// Add the course module 'groupmode' and 'groupingid' to the wiki object, for easy access.
     $wiki->groupmode = $cm->groupmode;
+//ou-specific begins
+    $wiki->groupingid  = $cm->groupingid;
+//ou-specific ends
 
     /// Default format:
     $moodle_format=FORMAT_MOODLE;
Index: mod/wiki/lib.php
===================================================================
RCS file: /globalcvs/ou-moodle/mod/wiki/lib.php,v
retrieving revision 1.8
diff -u -r1.8 lib.php
--- mod/wiki/lib.php	22 Feb 2007 13:39:03 -0000	1.8
+++ mod/wiki/lib.php	23 Feb 2007 12:43:25 -0000
@@ -412,7 +412,11 @@
     $groupmode = groupmode($course, $wiki);
     // if groups mode is in use and no group supplied, use the first one found
     if ($groupmode && !$groupid) {
-        if(($mygroupids=mygroupid($course->id)) && count($mygroupids)>0) {
+//ou-specific begins
+        // Provide the course-module ($wiki) to get the groupid.
+        $groupid = groups_m_get_my_group($wiki);
+
+        /*TODO: if(($mygroupids=mygroupid($course->id)) && count($mygroupids)>0) {
             // Use first group. They ought to be able to change later
             $groupid=$mygroupids[0];
         } else {
@@ -423,7 +427,8 @@
             } 
             $unkeyed=array_values($coursegroups); // Make sure first item is index 0
             $groupid=$unkeyed[0]->id;
-        }
+        }*/
+//ou-specific ends
     }
 
     /// If the wiki entry doesn't exist, can this user create it?
@@ -466,9 +471,13 @@
     case 'group':
         /// If there is a groupmode, get the user's group id.
         $groupmode = groupmode($course, $wiki);
-        if($groupmode) {
-            if(!$groupid) {
-                if(($mygroupids=mygroupid($course->id)) && count($mygroupids)>0) {
+        if ($groupmode) {
+            if (! $groupid) {
+//ou-specific begins
+                // Provide the course-module ($wiki) to get the groupid.
+                $groupid = groups_m_get_my_group($wiki); 
+
+                /*TODO: if(($mygroupids=mygroupid($course->id)) && count($mygroupids)>0) {
                     // Use first group. They ought to be able to change later
                     $groupid=$mygroupids[0];
                 } else {
@@ -479,7 +488,8 @@
                     } 
                     $unkeyed=array_values($coursegroups); // Make sure first item is index 0
                     $groupid=$unkeyed[0]->id;
-                }
+                }*/
+//ou-specific ends
             }
         
             //echo "groupid is in wiki_get_entry ".$groupid."<br />";
@@ -499,8 +509,9 @@
     case 'teacher':
         /// If there is a groupmode, get the user's group id.
         if (groupmode($course, $wiki)) {
-            $mygroupids = mygroupid($course->id);//same here, default to the first one
-            $groupid = $groupid ? $groupid : $mygroupids[0]/*mygroupid($course->id)*/;
+//ou-specific begins
+            $groupid = groups_m_get_my_group($wiki);
+//ou-specific ends
         }
 
         /// If a specific group was requested, return it, if allowed.
@@ -541,7 +552,9 @@
     $wikis = false;
 
     $groupmode = groupmode($course, $wiki);
-    $mygroupid = mygroupid($course->id);
+//ou-specific begins
+    $mygroupid = groups_m_get_my_group($wiki);
+//ou-specific ends
     $isteacher = wiki_is_teacher($wiki, $user->id);
     $isteacheredit = wiki_is_teacheredit($wiki, $user->id);
 
@@ -634,7 +647,9 @@
                 $viewall = true;
             }
             else if ($groupmode == SEPARATEGROUPS) {
-                $viewall = mygroupid($course->id);
+//ou-specific begins
+                $viewall = groups_m_get_my_group($wiki);
+//ou-specific ends
             }
             else {
                 $viewall = false;
@@ -671,6 +686,7 @@
         $wiki_entries = wiki_get_entries($wiki, 'group');
 
         if ($groupmode and ($isteacheredit or ($isteacher and !$mygroupid))) {
+            //TODO: 'Teachers' access wikis in any grouping I guess.
             if ($groups = get_groups($course->id)) {
                 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
                 foreach ($groups as $group) {
@@ -688,25 +704,25 @@
                 }
             }
         }
-        //if a studnet with multiple groups in SPG
+        //if a student with multiple groups in SPG
         else if ($groupmode == SEPARATEGROUPS){
-            if ($groups = get_groups($course->id, $user->id)){
+//ou-specific begins
+            if ($groupids = groups_m_get_groups_for_user($wiki, $user->id)) {
 
                 $defpagename = empty($wiki->pagename) ? get_string('wikidefaultpagename', 'wiki') : $wiki->pagename;
-                foreach ($groups as $group) {
+                foreach ($groupids as $groupid) {
                     /// If this group already has an entry, use its pagename.
-                    if (isset($wiki_entries[$group->id])) {
-                        $pagename = $wiki_entries[$group->id]->pagename;
+                    if (isset($wiki_entries[$groupid])) {
+                        $pagename = $wiki_entries[$groupid]->pagename;
                     }
                     else {
                         $pagename = $defpagename;
                     }
-                    $key = 'view.php?id='.$id.($group->id?"&groupid=".$group->id:"").'&page='.$pagename;
-                    $wikis[$key] = $group->name.':'.$pagename;
+                    $key = 'view.php?id='.$id. ($groupid ? '&amp;groupid='.$groupid : '') .'&amp;page='.$pagename;
+                    $wikis[$key] = groups_get_group_name($groupid).':'.$pagename;
+//ou-specific ends
                 }
-
             }
-
         }
         /// A user can see other group wikis if there are visible groups.
         else if ($groupmode == VISIBLEGROUPS) {
@@ -717,7 +733,7 @@
             $wiki_entries = get_records_sql($sql);
             $wiki_entries=is_array($wiki_entries)?$wiki_entries:array();
             foreach ($wiki_entries as $wiki_entry) {
-                $key = 'view.php?id='.$id.($wiki_entry->groupid?"&groupid=".$wiki_entry->groupid:"").'&page='.$wiki_entry->pagename;
+                $key = 'view.php?id='.$id. ($wiki_entry->groupid ? '&amp;groupid='.$wiki_entry->groupid : '').'&amp;page='.$wiki_entry->pagename;
                 $wikis[$key] = $wiki_entry->gname.':'.$wiki_entry->pagename;
                 if ($currentid == $wiki_entry->id) {
                     $wikis['selected'] = $key;
@@ -833,19 +849,12 @@
 
     case 'group':
         /// Get the groupmode. It's been added to the wiki object.
-        $groupmode = groupmode($course, $wiki);
+//ou-specific begins
+        $groupid = groups_m_get_my_group($wiki);
 
-        ///give the first groupid by default and try
-        $mygroups = mygroupid($course->id);
+        ///give the first groupid ...(NOT REQUIRED)
+//ou-specific ends
 
-        /// If there is a groupmode, get the group id.
-        if ($groupmode) {
-            $groupid = $groupid ? $groupid : $mygroups[0]/*mygroupid($course->id)*/;
-        }
-        /// If mode is 'nogroups', then groupid is zero.
-        else {
-            $groupid = 0;
-        }
         $wiki_entry->wikiid = $wiki->id;
         $wiki_entry->groupid = $groupid;
         $wiki_entry->pagename = wiki_page_name($wiki);
@@ -859,8 +868,9 @@
 
         /// If there is a groupmode, get the user's group id.
         if ($groupmode and $groupid == 0) {
-            $mygroupid = mygroupid($course->id);
-            $groupid = $mygroupid[0]/*mygroupid($course->id)*/;
+//ou-specific begins
+            $groupid = groups_m_get_my_group($wiki);
+//ou-specific ends
         }
 
         $wiki_entry->wikiid = $wiki->id;
@@ -880,7 +890,9 @@
 
     /// Get the groupmode. It's been added to the wiki object.
     $groupmode = groupmode($course, $wiki);
-    $mygroupid = mygroupid($course->id);
+//ou-specific begins
+    $mygroupid = groups_m_get_my_group($wiki);
+//ou-specific ends
 
     switch ($wiki->wtype) {
 
@@ -1028,7 +1040,9 @@
 
     /// Get the groupmode. It's been added to the wiki object.
     $groupmode = groupmode($course, $wiki);
-    $usersgroup = mygroupid($course->id);
+//ou-specific begins
+    $usersgroup = groups_m_get_my_group($wiki);
+//ou-specific ends
     $isteacher = wiki_is_teacher($wiki, $USER->id);
 
     /// A user can access a group wiki, if:
@@ -1038,7 +1052,9 @@
     ///     - they are an editing teacher or administrator,
     ///     - they are a non-editing teacher not assigned to a specific group.
     if (($groupmode == NOGROUPS) or ($groupmode == VISIBLEGROUPS) or
-        (($groupmode == SEPARATEGROUPS) and @in_array($groupid, $usersgroup)/*($usersgroup == $groupid)*/) or
+//ou-specific begins
+        (($groupmode == SEPARATEGROUPS) and ($usersgroup == $groupid)/*@in_array($groupid, $usersgroup)*/) or
+//ou-specific ends
         (wiki_is_teacheredit($wiki, $USER->id)) or
         (wiki_is_teacher($wiki, $USER->id) and !$usersgroup)) {
         $can_access = true;
Index: lib/db/upgrade.php
===================================================================
RCS file: /globalcvs/ou-moodle/lib/db/upgrade.php,v
retrieving revision 1.4
diff -u -r1.4 upgrade.php
--- lib/db/upgrade.php	16 Jan 2007 17:31:01 -0000	1.4
+++ lib/db/upgrade.php	23 Feb 2007 12:43:25 -0000
@@ -497,6 +497,22 @@
     }
 // ou-specific ends    
 
+
+//ou-specific begins
+    if ($result && $oldversion < 2007021400) {
+
+    /// Add field 'groupingid' to table course_modules.
+        $table = new XMLDBTable('course_modules');
+        $field = new XMLDBField('groupingid');
+        //setAttributes($type, $precision, $unsigned XMLDB_UNSIGNED, $notnull, $sequence, $enum, $enumvalues, $default, $previous)
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '10', $unsigned=false, XMLDB_NOTNULL, null, null, null, 0, 'groupmode');
+
+    /// Launch add field.
+        $result = $result && add_field($table, $field);
+        ///$result = $result && drop_field($table, $field); //($result && $oldversion < 2007020201)
+    }
+//ou-specific ends
+
     return $result;
 
 }
Index: lib/db/install.xml
===================================================================
RCS file: /globalcvs/ou-moodle/lib/db/install.xml,v
retrieving revision 1.10
diff -u -r1.10 install.xml
--- lib/db/install.xml	7 Jan 2007 18:47:28 -0000	1.10
+++ lib/db/install.xml	23 Feb 2007 12:43:25 -0000
@@ -141,7 +141,10 @@
         <FIELD NAME="indent" TYPE="int" LENGTH="5" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="score" NEXT="visible"/>
         <FIELD NAME="visible" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="false" DEFAULT="1" SEQUENCE="false" ENUM="false" PREVIOUS="indent" NEXT="visibleold"/>
         <FIELD NAME="visibleold" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="false" DEFAULT="1" SEQUENCE="false" ENUM="false" PREVIOUS="visible" NEXT="groupmode"/>
-        <FIELD NAME="groupmode" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="visibleold"/>
+<!--ou-specific begins-->
+        <FIELD NAME="groupmode" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="visibleold" next="groupingid"/>
+        <FIELD NAME="groupingid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="groupmode"/>
+<!--ou-specific ends-->
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id" COMMENT="Primary key for course_modules"/>
Index: group/db/dbmodulelib.php
===================================================================
RCS file: /globalcvs/ou-moodle/group/db/dbmodulelib.php,v
retrieving revision 1.1
diff -u -r1.1 dbmodulelib.php
--- group/db/dbmodulelib.php	5 Dec 2006 13:55:10 -0000	1.1
+++ group/db/dbmodulelib.php	23 Feb 2007 12:43:24 -0000
@@ -18,7 +18,7 @@
  * @param int  $cmid The id of the module instance.
  * @return int The grouping id (or false if it is null or an error occurred)
  */
-function groups_db_m_get_groupingid($cmid) {
+function groups___db_m_get_groupingid($cmid) {
 	// @@@ Check nulls are turned into false
 	$query = get_record('course_modules', 'groupingid', $userid);
 	return $query;
Index: group/db/dbgroupinglib.php
===================================================================
RCS file: /globalcvs/ou-moodle/group/db/dbgroupinglib.php,v
retrieving revision 1.4
diff -u -r1.4 dbgroupinglib.php
--- group/db/dbgroupinglib.php	25 Jan 2007 12:41:58 -0000	1.4
+++ group/db/dbgroupinglib.php	23 Feb 2007 12:43:24 -0000
@@ -124,20 +124,19 @@
  * @return int The id of the grouping or false if there is no such id recorded 
  * or if an error occurred. 
  */
-function groups_db_get_grouping_for_coursemodule($coursemoduleid) {
-	if (!$coursemoduleid) {
-		$groupingid = false;
-	} else {
-		$record = get_record('course_modules', 'id', $coursemoduleid, 'id, ' .
-				             'groupingid');
-		if (!$record) {
-			$groupingid = false;
-		} else {
-			$groupingid = $record->groupingid;
-		}
-	}
-	
-	return $groupingid;
+function groups_db_get_grouping_for_coursemodule($cm) {
+	if (is_object($cm) and isset($cm->course) and isset($cm->groupingid)) {
+        //Do NOT test cm->module!
+        return $cm->groupingid;
+    } elseif (is_numeric($cm)) {
+        // Treat param as the course module ID.
+        $coursemoduleid = $cm;
+        $record = get_record('course_modules', 'id', $coursemoduleid, 'id, groupingid');
+        if ($record and isset($record->groupingid)) {
+            return $record->groupingid;
+        }
+    }
+    return false;
 }
 
 
Index: group/lib/utillib.php
===================================================================
RCS file: /globalcvs/ou-moodle/group/lib/utillib.php,v
retrieving revision 1.7
diff -u -r1.7 utillib.php
--- group/lib/utillib.php	22 Feb 2007 16:16:34 -0000	1.7
+++ group/lib/utillib.php	23 Feb 2007 12:43:24 -0000
@@ -40,10 +40,12 @@
     if (GROUP_NOT_IN_GROUPING == $groupingid) {
         $groupids = groups_get_groups_not_in_any_grouping($courseid);
         return count($groupids);
-     } else {
+    } elseif (GROUP_ANY_GROUPING == $groupingid) {
+        return count_records('groups_courses_groups', 'courseid', $courseid);
+    } else {
         return count_records('groups_groupings_groups', 'groupingid ', $groupingid);
-     }
- }
+    }
+}
 
 
 /**
@@ -123,6 +125,27 @@
 
 
 /**
+ * Get an sorted array of user-id/display-name objects.
+ */
+function groups_userids_to_user_names($userids, $courseid) {
+    if (! $userids) {
+        return array();
+    }
+    $member_names = array();
+    foreach ($userids as $id) {
+        $user = new object;
+        $user->id = $id;
+        $user->name = groups_get_user_displayname($id, $courseid);
+        $member_names[] = $user;
+    }
+    if (! usort($member_names, 'groups_compare_name')) {
+        debug('Error usort [groups_compare_name].');
+    }
+    return $member_names;
+}
+
+
+/**
  * Takes an array of groups (i.e of objects) and converts it to the 
  * corresponding array of group IDs. 
  * @param $groups array The array of group-like objects, only the $group->id member is required.
@@ -139,18 +162,24 @@
 //ou-specific begins
     if (ou_auto_groups_visible($courseid)) { //TODO:
         foreach ($groups as $group) {
-            array_push($groupids, $group->id);
+            if (isset($group->id)) {
+                array_push($groupids, $group->id);
+            } else {
+                //Warn if there's no "groupid" member.
+                array_push($groupids, $group->groupid);
+            }
         }
     } else {
         foreach ($groups as $group) {
-            if (! ou_is_auto_groupid($group->id)) {
-                array_push($groupids, $group->id);
+            if (isset($group->id)) {
+                if (! ou_is_auto_groupid($group->id)) {
+                    array_push($groupids, $group->id);
+                }
+            } elseif (! ou_is_auto_groupid($group->groupid)) {
+                array_push($groupids, $group->groupid);
             }
         }
     }
-    /*foreach ($groups as $group) {
-        array_push($groupids, $group->id);
-    }*/
 //ou-specific ends
 	return $groupids;
 }
Index: group/lib/basicgrouplib.php
===================================================================
RCS file: /globalcvs/ou-moodle/group/lib/basicgrouplib.php,v
retrieving revision 1.7
diff -u -r1.7 basicgrouplib.php
--- group/lib/basicgrouplib.php	7 Feb 2007 15:17:18 -0000	1.7
+++ group/lib/basicgrouplib.php	23 Feb 2007 12:43:24 -0000
@@ -118,7 +118,7 @@
  * Get the group settings object for a group - this contains the following 
  * properties:
  * name, description, lang, theme, picture, hidepicture
- * @param int $groupid The id of the gruop
+ * @param int $groupid The group ID.
  * @return object The group settings object 
  */
 function groups_get_group_settings($groupid, $courseid=false, $alldata=false) {
Index: group/lib/modulelib.php
===================================================================
RCS file: /globalcvs/ou-moodle/group/lib/modulelib.php,v
retrieving revision 1.1
diff -u -r1.1 modulelib.php
--- group/lib/modulelib.php	5 Dec 2006 13:55:08 -0000	1.1
+++ group/lib/modulelib.php	23 Feb 2007 12:43:24 -0000
@@ -1,9 +1,5 @@
 <?php
-
-// @@@ TO DO Still tons to do here! Most of this isn't done or just copied
-// and pasted so far and needs to be sorted out. 
-
-/*******************************************************************************
+/**
  * modulelib.php
  * 
  * This file contains functions to be used by modules to support groups. More
@@ -12,10 +8,15 @@
  * 
  * For queries, suggestions for improvements etc. please post on the Groups 
  * forum on the moodle.org site.
- ******************************************************************************/
+ *
+ * @copyright &copy; 2006 The Open University
+ * @author J.White AT open.ac.uk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package groups
+ */ 
 
-/*******************************************************************************
- * Permission types
+/*
+ * (OLD) Permission types
  * 
  * There are six types of permission that a user can hold for a particular
  * group - 'student view', 'student contribute', 'teacher view', 
@@ -50,7 +51,7 @@
  * @name GROUPS_STUDENT_CONTRIBUTE 'student contribute' permission
  * @name GROUPS_TEACHER_VIEW 'teacher view' permission
  * @name GROUPS_TEACHER_CONTRIBUTE 'teacher contribute' permission
- ******************************************************************************/
+ */
 define('GROUPS_STUDENT', 1);
 define('GROUPS_TEACHER', 2);
 define('GROUPS_VIEW', 4);
@@ -147,31 +148,44 @@
 }
 	 
 /**
- * Gets an array of the groupids of all the groups that the specified user has
- * particular permission for in this particular instance of the module
+ * Gets an array of the group IDs of all groups for the user in this course module.
  * @uses $USER     
- * @param int $cmid The id of the module instance
- * @param int $permissiontype The permission type - see note on permission types 
- * above
- * @param int $userid The id of the user, defaults to the current user 
- * @return array An array of the group ids 
- */	
-function groups_m_get_groups_for_user($cmid, $permissiontype, $userid) {
-	$groupingid = groups_db_get_groupingid($cmid);
-	$groupids = groups_get_groups_in_grouping($groupingid);
-	if (!$groupids) {
-		$groupidsforuser = false;
-	} else {
-		$groupidsforuser = array();
-		foreach($groupids as $groupid) {
-			if (groups_m_has_permission($cmid, $groupid, $permissiontype, $userid) ) {
-				array_push($groupidsforuser, $groupid);
-			}
-		}
-	}
-	
-	return $groupidsforuser;
-}  
+ * @param object $cm The course-module object.
+ * @param int $userid The ID of the user.
+ * @return array An array of group IDs, or false. 
+ */
+function groups_m_get_groups_for_user($cm, $userid) {
+//echo 'User'; print_object($cm);
+    $groupingid = groups_get_grouping_for_coursemodule($cm);
+    if (!$groupingid) {
+        return false;
+    }
+    if (!isset($cm->course) || !groupmode($cm->course, $cm)) {
+        return false;
+    }
+    elseif (GROUP_ANY_GROUPING == $groupingid) {
+        return groups_get_groups_for_user($userid, $cm->course);
+    }
+    return groups_get_groups_for_user_in_grouping($userid, $groupingid);
+} 
+
+
+/**
+ * Get the ID of the first group for the global $USER in the course module.
+ * Replaces legacylib function 'mygroupid'.
+ * @uses $USER
+ * @param $cm A course module object.
+ * @return int A single group ID for this user.
+ */ 
+function groups_m_get_my_group($cm) {
+    global $USER;
+    $groupids = groups_m_get_groups_for_user($cm, $USER->id);
+    if (!$groupids || count($groupids) == 0) {
+        return 0;
+    }
+    return array_shift($groupids);
+}
+
 
 /**
  * Indicates if a specified user has a particular type of permission for a 
@@ -187,9 +201,17 @@
  * @return boolean True if the user has the specified permission type, false 
  * otherwise or if an error occurred. 
  */
- function groups_m_has_permission($cmid, $groupid, $permissiontype, $userid) {
-	$groupingid = groups_get_grouping_for_coursemodule($coursemoduleid);
-	$isstudent = isstudent($courseid, $userid);
+ function groups_m_has_permission($cm, $groupid, $permissiontype, $userid = null) {
+    if (!$userid) {
+        global $USER;
+        $userid = $USER->id;
+    }
+	$groupingid = groups_get_grouping_for_coursemodule($cm);
+	if (!$groupingid || !is_object($cm) || !isset($cm->course)) {
+        return false;
+    }
+    $courseid = $cm->course;
+    $isstudent = isstudent($courseid, $userid);
 	$isteacher = isteacher($courseid, $userid);
 	$groupmember = groups_is_member($groupid, $userid);
 	$memberofsomegroup = groups_is_member_of_some_group_in_grouping($userid, $groupingid);
@@ -264,7 +286,7 @@
                                               $permissiontype) {
 	// Get all the users as $userid
 	$validuserids = array();	
-	foreach($userids as $userid) {
+	foreach($validuserids as $userid) {
 		$haspermission = groups_m_has_permission($cmid, $groupid, 
 										$permissiontype, $userid);
 		if ($haspermission) {
@@ -313,88 +335,7 @@
 	} else {
 		// Check if each user is enrolled on the course @@@ TO DO 
 	}
-}
-
-/**
- * Indicates if a user is a member of a particular group. In general you should 
- * use groups_m_has_permission, however this function is provided for 
- * circumstances where this function isn't sufficient for some reason. 
- * @param int $groupid The id of the group
- * @param int $userid The id of the user 
- * @return boolean True if the user is a member of the group, false otherwise 
- */
-function groups_m_is_member($groupid, $userid) {
-	return groups_is_member($groupid, $userid);
-}
-
-/**
- * Determines if a user has a particular permission type for a particular group. Permissions
- * are set at grouping level and are set via the grouping settings. This is the function
- * to check, if somebody can't access something that you think they ought to be able to!
- * This is for the current user. What about for general user?
- * @param int $courseid The id of the course
- * @param int $groupid The id of the group
- * @param string $permissiontype The permission type: 'view', 'studentcontribute', 
- * 'teacherview', 'teachercontribute', 'viewmembers', 'contribute', 'all'
- */
-function groups_m_has_permission($courseid, $groupid, $permissiontype) {
-	$userid = $USER->id;
-	$groupingid = groups_get_grouping_for_coursemodule($coursemoduleid);
-	$isstudent = isstudent($courseid, $userid);
-	$isteacher = isteacher($courseid, $userid);
-	$groupmember = groups_is_member($groupid, $userid);
-	$memberofsomegroup = groups_is_member_of_some_group_in_grouping($userid, $groupingid);
-	
-	$groupingsettings = groups_get_grouping_settings($groupingid);
-	$viewowngroup = $groupingsettings->viewowngroup;
-	$viewallgroupsmembers = $groupingsettings->viewallgroupmembers;
-	$viewallgroupsactivities = $groupingsettings->viewallgroupsactivities;
-	$teachersgroupsmark = $groupingsettings->teachersgroupsmark;
-	$teachersgroupsview = $groupingsettings->teachersgroupsview;
-	$teachersgroupmark = $groupingsettings->teachersgroupmark;
-	$teachersgroupview = $groupingsettings->teachersgroupview;
-	$teachersoverride = $groupingsettings->teachersoverride;
-		
-	$permission = false;
-	
-	switch ($permissiontype) {
-		case 'view':
-			if (($isstudent and $groupmember) or 
-			    ($isteacher and $groupmember) or 
-			    ($isstudent and $viewallgroupsactivities) or 
-			    ($isteacher and !$teachersgroupview) or 
-			    ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-				$permission = true;
-			} 
-			break;
-			
-		case 'studentcontribute':
-			if (($isstudent and $groupmember) or 
-			    ($isteacher and $groupmember) or 
-			    ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-				$permission = true;
-			} 
-			break;
-		case 'teachermark':
-			if (($isteacher and $groupmember) or 
-				($isteacher and !$teachersgroupmark) or
-			    ($isteacher and !$memberofsomegroup and $teachersoverride)) {
-				$permission = true;
-			}  
-			break;
-		
-		case 'viewmembers':	
-			if (($isstudent and $groupmember and $viewowngroup) or 
-			    ($isstudent and $viewallgroupsmembers) or 
-				($isteacher and $groupmember) or 
-			    ($isteacher and !$teachersgroupview) or 
-			    ($isteacher and !$memberofsomegroup and $teachersoverride) or 
-			    $isteacheredit) {
-				$permission = true;
-			}  
-			break;
-	}
-	return $permission;	
+    return $memberids;
 }
 
 ?>
Index: group/lib/groupinglib.php
===================================================================
RCS file: /globalcvs/ou-moodle/group/lib/groupinglib.php,v
retrieving revision 1.6
diff -u -r1.6 groupinglib.php
--- group/lib/groupinglib.php	22 Feb 2007 16:16:34 -0000	1.6
+++ group/lib/groupinglib.php	23 Feb 2007 12:43:24 -0000
@@ -136,7 +136,7 @@
     $records = get_records_sql($sql);
 
 //print_object($records);
-    return groups_groups_to_groupids($records); //TODO:check.
+    return groups_groups_to_groupids($records);
 }
 
 /**
Index: group/lib/legacylib.php
===================================================================
RCS file: /globalcvs/ou-moodle/group/lib/legacylib.php,v
retrieving revision 1.5
diff -u -r1.5 legacylib.php
--- group/lib/legacylib.php	7 Feb 2007 15:03:10 -0000	1.5
+++ group/lib/legacylib.php	23 Feb 2007 12:43:24 -0000
@@ -156,7 +156,7 @@
  */
 function groupmode($course, $cm=null) {
 
-    if ($cm and !$course->groupmodeforce) {
+    if (is_object($cm) && isset($cm->groupmode) && !isset($course->groupmodeforce)) {
         return $cm->groupmode;
     }
     return $course->groupmode;
Index: group/lib.php
===================================================================
RCS file: /globalcvs/ou-moodle/group/lib.php,v
retrieving revision 1.1
diff -u -r1.1 lib.php
--- group/lib.php	5 Jan 2007 17:27:59 -0000	1.1
+++ group/lib.php	23 Feb 2007 12:43:24 -0000
@@ -14,7 +14,7 @@
 
 require_once($CFG->dirroot.'/group/lib/utillib.php');
 
-require_once($CFG->dirroot.'/group/lib/automaticgroupinglib.php');
+require_once($CFG->dirroot.'/group/lib/modulelib.php');
 
 require_once($CFG->dirroot.'/group/lib/legacylib.php');
 
Index: lib/formslib.php
===================================================================
RCS file: /globalcvs/ou-moodle/lib/formslib.php,v
retrieving revision 1.8
diff -u -r1.8 formslib.php
--- lib/formslib.php	7 Jan 2007 18:47:28 -0000	1.8
+++ lib/formslib.php	23 Feb 2007 12:43:25 -0000
@@ -1478,6 +1478,9 @@
 MoodleQuickForm::registerElementType('hidden', "$CFG->libdir/form/hidden.php", 'MoodleQuickForm_hidden');
 MoodleQuickForm::registerElementType('modvisible', "$CFG->libdir/form/modvisible.php", 'MoodleQuickForm_modvisible');
 MoodleQuickForm::registerElementType('modgroupmode', "$CFG->libdir/form/modgroupmode.php", 'MoodleQuickForm_modgroupmode');
+//ou-specific begins
+MoodleQuickForm::registerElementType('modgrouping', "$CFG->libdir/form/modgrouping.php", 'MoodleQuickForm_modgrouping');
+//ou-specific ends
 MoodleQuickForm::registerElementType('selectyesno', "$CFG->libdir/form/selectyesno.php", 'MoodleQuickForm_selectyesno');
 MoodleQuickForm::registerElementType('modgrade', "$CFG->libdir/form/modgrade.php", 'MoodleQuickForm_modgrade');
 MoodleQuickForm::registerElementType('cancel', "$CFG->libdir/form/cancel.php", 'MoodleQuickForm_cancel');
Index: version.php
===================================================================
RCS file: /globalcvs/ou-moodle/version.php,v
retrieving revision 1.17
diff -u -r1.17 version.php
--- version.php	16 Jan 2007 17:31:01 -0000	1.17
+++ version.php	23 Feb 2007 12:43:24 -0000
@@ -6,7 +6,7 @@
 // This is compared against the values stored in the database to determine
 // whether upgrades should be performed (see lib/db/*.php)
 
-   $version = 2007010405;  // YYYYMMDD = date
+   $version = 2007021400;  // YYYYMMDD = date
                            //       XY = increments within a single day
 
    $release = '1.8 dev';    // Human-friendly version name
Index: lib/form/modgrouping.php
===================================================================
RCS file: lib/form/modgrouping.php
diff -N lib/form/modgrouping.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ lib/form/modgrouping.php	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,130 @@
+<?php
+//ou-specific begins
+global $CFG;
+require_once "$CFG->libdir/form/select.php";
+
+/**
+ * HTML class for a drop down element to select a grouping for an activity mod update form
+ *
+ * @author       Nick Freear
+ * @access       public
+ */
+class MoodleQuickForm_modgrouping extends MoodleQuickForm_select{
+
+/**
+    * Options for element :
+    *
+    * $url must be relative to home page  eg /mod/survey/stuff.php
+    * courseid => int course id if null then uses $COURSE global
+    * width => int Height to assign to popup window
+    * title => string Text to be displayed as popup page title
+    * options => string List of additional options for popup window
+    */
+    //var $_options = array('courseid'=>null);
+    var $_courseid = null;
+    var $_groupingid = null;
+    var $_cm = null;
+
+    /**
+     * Class constructor
+     *
+     * @param     string    Select name attribute
+     * @param     mixed     Label(s) for the select
+     * @param     mixed     Either a typical HTML attribute string or an associative array
+     * @param     mixed     $options ignored
+     * @access    public
+     * @return    void
+     */
+    function MoodleQuickForm_modgrouping($elementName=null, $elementLabel=null, $attributes=null, $options=null)
+    {
+        HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes, null);
+        $this->_type = 'modgrouping';
+        
+        // set the options, do not bother setting bogus ones
+        /*if (is_array($options)) {
+            foreach ($options as $name => $value) {
+                if (isset($this->_options[$name])) {
+                    if (is_array($value) && is_array($this->_options[$name])) {
+                        $this->_options[$name] = @array_merge($this->_options[$name], $value);
+                    } else {
+                        $this->_options[$name] = $value;
+                    }
+                }
+            }
+        }*/
+        if (is_object($attributes) and isset($attributes->module)) {
+            //and isset($attributes->course) and isset($attributes->grouping)) {
+            // Good, we have a course module object.
+            $this->_cm = $attributes;
+            $this->_courseid = $attributes->course;
+            $this->_groupingid = $attributes->groupingid;
+        }
+//print_object($this); print_object($attributes);
+
+    } //end constructor
+
+    /**
+     * Called by HTML_QuickForm whenever form event is made on this element
+     *
+     * @param     string    $event  Name of event
+     * @param     mixed     $arg    event arguments
+     * @param     object    $caller calling object
+     * @since     1.0
+     * @access    public
+     * @return    mixed
+     */
+    function onQuickFormEvent($event, $arg, &$caller)
+    {
+        global $COURSE;
+
+        switch ($event) {
+            case 'createElement':
+//$e = new Exception;
+//echo "<pre>\n". $e->getTraceAsString() ."</pre>\n";
+//print_object($this); print_object($arg); print_object($caller);
+        /*Events: addElement, createElement, updateValue, updateValue, updateValue, updateValue, updateValue */
+                /*if ($this->_options['courseid'] !== null) {
+                    $courseid = $this->_options['courseid'];
+                } else {
+                    $courseid = $COURSE->id;
+                }*/
+
+                $sel_grouping = GROUP_ANY_GROUPING; //TODO:check.
+                $courseid = 0;
+                if (isset($this->_cm)) {
+                    $sel_grouping = $this->_groupingid;
+                    $courseid = $this->_courseid;
+                } elseif (is_object($arg['2']) and isset($arg['2']->module)) {
+                    /// Good, we have a course module object.
+                    $cm = $arg['2'];
+                    $sel_grouping = $cm->groupingid;
+                    $courseid = $cm->course;
+                } else {
+                    $courseid = $COURSE->id;
+                }
+                //$sel_grouping= groups_get_grouping_for_coursemodule($this->_cm['id']);
+                $groupingids = groups_get_groupings($courseid);
+                if (groups_count_groups_in_grouping(GROUP_NOT_IN_GROUPING, $courseid) > 0) {
+                    $groupingids[] = GROUP_NOT_IN_GROUPING;
+                }
+                $groupingids[] = GROUP_ANY_GROUPING;
+
+                $choices = array();
+                if ($groupingids) {    
+                    // Put the groupings into a hash and sort them
+                    foreach($groupingids as $id) {
+                        $choices[$id] = groups_get_grouping_displayname($id, $courseid);
+                    }
+                }
+                $this->setHelpButton(array('grouping', get_string('grouping', 'group')));
+                $this->load($choices);
+                $this->setSelected(array($sel_grouping)); //GROUP_ANY_GROUPING
+                break;
+        }
+        return parent::onQuickFormEvent($event, $arg, $caller);
+    }
+
+}
+
+//ou-specific begins
+?>
Index: mod/wiki/mod_form.php
===================================================================
RCS file: mod/wiki/mod_form.php
diff -N mod/wiki/mod_form.php
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ mod/wiki/mod_form.php	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,99 @@
+<?php
+require_once ('moodleform_mod.php');
+
+class mod_wiki_mod_form extends moodleform_mod {
+
+	function definition() {
+
+		global $CFG, $COURSE, $WIKI_TYPES;
+		$mform    =& $this->_form;
+
+        if (!empty($this->_instance)) {
+            $queryobject = new stdClass();
+            $queryobject->id = $this->_instance;
+            $wikihasentries = wiki_has_entries($queryobject);
+        } else {
+            $wikihasentries=false;
+        }
+//-------------------------------------------------------------------------------
+        $mform->addElement('header', 'general', get_string('general', 'form'));
+        $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
+		$mform->setType('name', PARAM_NOTAGS);
+		$mform->addRule('name', null, 'required', null, 'client');
+
+		$mform->addElement('htmleditor', 'summary', get_string('summary'));
+		$mform->setType('summary', PARAM_RAW);
+        $mform->setHelpButton('summary', array('writing', 'questions', 'richtext'), false, 'editorhelpbutton');
+		$mform->addRule('summary', get_string('required'), 'required', null, 'client');
+
+        if (!$wikihasentries){
+            asort($WIKI_TYPES);
+            $mform->addElement('select', 'wtype', get_string('wikitype', 'wiki'), $WIKI_TYPES);
+    		$mform->setHelpButton('wtype', array('wikitype', get_string('wikitype', 'wiki'), 'wiki'));
+    		$mform->setDefault('wtype', 'group');
+        } else {
+            $mform->addElement('static', 'wtype', get_string('wikitype', 'wiki'));
+        }
+
+        $mform->addElement('selectyesno', 'ewikiprinttitle', get_string('ewikiprinttitle', 'wiki'));
+        $mform->setDefault('ewikiprinttitle', 1);
+		$mform->setAdvanced('ewikiprinttitle');
+
+        $htmlmodes = array(0 => get_string('nohtml', 'wiki'),
+                         1 => get_string('safehtml', 'wiki'),
+                         2 => get_string('htmlonly', 'wiki'));
+        $mform->addElement('select', 'htmlmode', get_string('htmlmode', 'wiki'), $htmlmodes);
+        $mform->setDefault('htmlmode', 2);
+		$mform->setAdvanced('htmlmode');
+
+        $mform->addElement('selectyesno', 'ewikiacceptbinary', get_string('ewikiacceptbinary', 'wiki'));
+        $mform->setDefault('ewikiacceptbinary', 0);
+		$mform->setHelpButton('ewikiacceptbinary', array('ewikiacceptbinary', get_string('ewikiacceptbinary', 'wiki'), 'wiki'));
+		$mform->setAdvanced('ewikiacceptbinary');
+
+		$mform->addElement('advcheckbox', 'disablecamelcase', get_string('wikilinkoptions', 'wiki'), get_string('disablecamel', 'wiki'));
+		$mform->setDefault('disablecamelcase', 0);
+		$mform->setHelpButton('disablecamelcase', array('wikilinkoptions', get_string('wikilinkoptions', 'wiki'), 'wiki'));
+		$mform->setAdvanced('disablecamelcase');
+
+		$studentadminoptionsgrp = array();
+		$studentadminoptionsgrp[] =& $mform->createElement('advcheckbox', 'setpageflags', '', get_string('allowsetpage', 'wiki'));
+		$mform->setDefault('setpageflags', 0);
+		$studentadminoptionsgrp[] =& $mform->createElement('advcheckbox', 'strippages', '', get_string('allowstrippages', 'wiki'));
+		$mform->setDefault('strippages', 0);
+		$studentadminoptionsgrp[] =& $mform->createElement('advcheckbox', 'removepages', '', get_string('allowremovepages', 'wiki'));
+		$mform->setDefault('removepages', 0);
+		$studentadminoptionsgrp[] =& $mform->createElement('advcheckbox', 'revertchanges', '', get_string('allowrevertchanges', 'wiki'));
+		$mform->setDefault('revertchanges', 0);
+		$mform->addGroup($studentadminoptionsgrp, 'studentadminoptions', get_string('studentadminoptions', 'wiki'), null, false);
+        $mform->setAdvanced('studentadminoptions');
+
+//-------------------------------------------------------------------------------
+        $mform->addElement('header', 'optional', get_string('optional', 'form'));
+        $mform->addElement('text', 'pagename', get_string('wikiname', 'wiki'));
+        if ($wikihasentries) {
+            $mform->hardFreeze('pagename');
+        }
+        $mform->setHelpButton('pagename', array('wikiname', get_string('wikiname', 'wiki'), 'wiki'));
+        $mform->setType('pagename', PARAM_NOTAGS);
+		$mform->setAdvanced('pagename');
+
+        if ($wikihasentries){
+            $mform->addElement('choosecoursefile', 'initialcontent', get_string('initialcontent', 'wiki'));
+        } else {
+            $mform->addElement('static', 'initialcontent', get_string('initialcontent', 'wiki'));
+        }
+        $mform->setHelpButton('initialcontent', array('initialcontent', get_string('initialcontent', 'wiki'), 'wiki'));
+		$mform->setAdvanced('initialcontent');
+
+//-------------------------------------------------------------------------------
+		$this->standard_coursemodule_elements($supportsgroups=true, $supportsgroupings=true);
+//-------------------------------------------------------------------------------
+        // buttons
+        $this->add_action_buttons();
+
+	}
+
+
+}
+?>
