Index: addinstance.php
===================================================================
RCS file: /cvsroot/moodle/moodle/enrol/cohort/addinstance.php,v
retrieving revision 1.5
diff -u -a -w -b -B -r1.5 addinstance.php
--- addinstance.php	1 Aug 2010 19:59:25 -0000	1.5
+++ addinstance.php	2 Nov 2010 10:28:43 -0000
@@ -59,7 +59,11 @@
     redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
 
 } else if ($data = $mform->get_data()) {
-    $enrol->add_instance($course, array('customint1'=>$data->cohortid, 'roleid'=>$data->roleid));
+    $enrol->add_instance($course, array('customint1'=>$data->cohortid, 'roleid'=>$data->roleid,
+    //PATCH PP
+                                   'customint2'=>$data->create_group,'customint3'=>$data->create_grouping,
+                                   'customtext1'=>$data->use_cohort_field));
+    //END PATCH PP
     enrol_cohort_sync($course->id);
     redirect(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
 }
Index: addinstance_form.php
===================================================================
RCS file: /cvsroot/moodle/moodle/enrol/cohort/addinstance_form.php,v
retrieving revision 1.6
diff -u -a -w -b -B -r1.6 addinstance_form.php
--- addinstance_form.php	19 Sep 2010 10:49:24 -0000	1.6
+++ addinstance_form.php	2 Nov 2010 10:28:43 -0000
@@ -66,12 +66,25 @@
         $mform->addRule('roleid', get_string('required'), 'required', null, 'client');
         $mform->setDefault('roleid', $enrol->get_config('roleid'));
 
+        // PATCH PP automatically create group/grouping local to this course
+        $mform->addElement('selectyesno','create_group',get_string('create_group','enrol_cohort'));
+        $mform->addElement('selectyesno','create_grouping',get_string('create_grouping','enrol_cohort'));
+        $mform->addElement('hidden','use_cohort_field',null);
+        // END PATCH PP
+
+
         $mform->addElement('hidden', 'id', null);
         $mform->setType('id', PARAM_INT);
 
         $this->add_action_buttons(true, get_string('addinstance', 'enrol'));
 
-        $this->set_data(array('id'=>$course->id));
+        $this->set_data(array('id'=>$course->id,
+        // PATCH PP automatically create group/grouping local to this course (add default values from admin settings)
+                               'create_group'=>$enrol->get_config('create_groups'),
+                               'create_grouping'=>$enrol->get_config('create_groupings'),
+                               'use_cohort_field'=>$enrol->get_config('use_cohort_field'),
+        // END PATCH PP
+        ));
     }
 
     //TODO: validate duplicate role-cohort does not exist
Index: locallib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/enrol/cohort/locallib.php,v
retrieving revision 1.3
diff -u -a -w -b -B -r1.3 locallib.php
--- locallib.php	31 Jul 2010 20:30:30 -0000	1.3
+++ locallib.php	2 Nov 2010 10:28:43 -0000
@@ -112,7 +112,10 @@
             $params['courseid'] = $courseid;
             $onecourse = "AND e.courseid = :courseid";
         }
-        $sql = "SELECT cm.userid, e.id AS enrolid
+        //PATCH PP automatically create group/grouping local to this course
+        // added extrafields retrieved from table mdl_enrol for later processing
+        //END PATCH PP
+        $sql = "SELECT cm.userid, e.id AS enrolid,e.courseid as courseid,e.customint1 as cohortid,e.customint2 as creategroup,e.customint3 as creategrouping,e.customtext1 as namefield
                   FROM {cohort_members} cm
                   JOIN {enrol} e ON (e.customint1 = cm.cohortid AND e.status = :statusenabled AND e.enrol = 'cohort' $onecourse)
              LEFT JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = cm.userid)
@@ -121,14 +124,51 @@
         $params['courseid'] = $courseid;
         $rs = $DB->get_recordset_sql($sql, $params);
         $instances = array(); //cache
+        $cohorts =array(); //cache
         foreach($rs as $ue) {
             if (!isset($instances[$ue->enrolid])) {
                 $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
             }
             $cohort->enrol_user($instances[$ue->enrolid], $ue->userid);
+            //PATCH PP  automatically create group/grouping local to this course
+            if ($ue->courseid) { //either the target course or all course during a cron synchronisation
+                // not anymore included by default in Moodle 2.0
+                require_once ($CFG->dirroot.'/group/lib.php');
+                if (!empty($ue->creategroup) || !empty($ue->creategrouping)) {
+                    if (!isset($cohorts[$ue->cohortid])) {
+                        $cohorts[$ue->cohortid]=$DB->get_record('cohort', array('id'=>$ue->cohortid));
+                    }
+
+                    // we must create the group evn if create_grouping is true and not create_group ...
+                    $namefield=is_null($ue->namefield)?'name':$ue->namefield;  // naming attribute for the associated group/grouping (name or idnumber)
+                    $groupname= $cohorts[$ue->cohortid]->$namefield;
+                    if (! $groupid=groups_get_group_by_name($ue->courseid,$groupname)) {
+                        $group=new StdClass();
+                        $group->name=$groupname;
+                        $group->description=get_string('group_auto_cohort','enrol_cohort',$groupname);
+                        $group->courseid=$ue->courseid;
+                        $groupid=groups_create_group($group);
+                    }
+                    if (! groups_is_member($groupid, $ue->userid)) {
+                        groups_add_member($groupid,$ue->userid);
+                    }
+                    if (!empty($ue->creategrouping)) {
+                        if (! $groupingid=groups_get_grouping_by_name($ue->courseid,$groupname)) {
+                            $grouping=new StdClass();
+                            $grouping->name=$groupname;
+                            $grouping->description=get_string('grouping_auto_cohort','enrol_cohort',$groupname);
+                            $grouping->courseid=$ue->courseid;
+                            $groupingid=groups_create_grouping($grouping);
+                            groups_assign_grouping($groupingid, $groupid);
+                        }
+                    }
+                }
+            }
+            //END PATCH PP
         }
         $rs->close();
         unset($instances);
+        unset($cohorts);
     }
 
     // unenrol as necessary - ignore enabled flag, we want to get rid of all
@@ -145,6 +186,9 @@
             $instances[$ue->enrolid] = $DB->get_record('enrol', array('id'=>$ue->enrolid));
         }
         $cohort->unenrol_user($instances[$ue->enrolid], $ue->userid);
+        //PATCH PP  sync autocreate group/grouping
+        // fortunately nothing to do unenrol_user do remove group membership
+        //END PATCH PP
     }
     $rs->close();
     unset($instances);
Index: settings.php
===================================================================
RCS file: /cvsroot/moodle/moodle/enrol/cohort/settings.php,v
retrieving revision 1.4
diff -u -a -w -b -B -r1.4 settings.php
--- settings.php	31 Jul 2010 20:30:30 -0000	1.4
+++ settings.php	2 Nov 2010 10:28:43 -0000
@@ -37,8 +37,25 @@
         $options = get_default_enrol_roles(get_context_instance(CONTEXT_SYSTEM));
         $student = get_archetype_roles('student');
         $student = reset($student);
-        $settings->add(new admin_setting_configselect('enrol_cohort/roleid',
-            get_string('defaultrole', 'role'), '', $student->id, $options));
-    }
+        $settings->add(new admin_setting_configselect('enrol_cohort/roleid', get_string('defaultrole', 'role'), '', $student->id, $options));
+
+        //PATCH PP
+        /** acceder a ces valeurs utilser
+         *  $enrol = enrol_get_plugin('cohort');
+         *  if ($enrol->get_config ('xxxxxx')) )
+         *
+         */
+
+        $settings->add(new admin_setting_configcheckbox('enrol_cohort/create_groups', get_string('create_group', 'enrol_cohort'), get_string('config_create_group', 'enrol_cohort'), 0));
+        $settings->add(new admin_setting_configcheckbox('enrol_cohort/create_groupings', get_string('create_grouping', 'enrol_cohort'), get_string('config_create_grouping', 'enrol_cohort'), 0));
+
+        $options = array (
+            'idnumber' => get_string('idnumber', 'cohort'),
+            'name' => get_string('name', 'cohort')
+        );
+
+        $settings->add(new admin_setting_configselect('enrol_cohort/use_cohort_field', get_string('use_cohort_field', 'enrol_cohort'), get_string('config_use_cohort_field', 'enrol_cohort'), 'name', $options));
+        //END PATCH PP
 }
 
+}
\ No newline at end of file
Index: version.php
===================================================================
RCS file: /cvsroot/moodle/moodle/enrol/cohort/version.php,v
retrieving revision 1.4
diff -u -a -w -b -B -r1.4 version.php
--- version.php	31 Jul 2010 20:30:30 -0000	1.4
+++ version.php	2 Nov 2010 10:28:43 -0000
@@ -26,5 +26,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version = 2010073100;
+//$plugin->version = 2010073100;
+//PATCH PP
+$plugin->version = 2010073101;
 $plugin->cron    = 60;
\ No newline at end of file
Index: lang/en/enrol_cohort.php
===================================================================
RCS file: /cvsroot/moodle/moodle/enrol/cohort/lang/en/enrol_cohort.php,v
retrieving revision 1.3
diff -u -a -w -b -B -r1.3 enrol_cohort.php
--- lang/en/enrol_cohort.php	31 Jul 2010 20:12:21 -0000	1.3
+++ lang/en/enrol_cohort.php	2 Nov 2010 10:28:43 -0000
@@ -27,3 +27,18 @@
 $string['cohort:config'] = 'Configure cohort instances';
 $string['pluginname'] = 'Cohort sync';
 $string['pluginname_desc'] = 'Cohort enrolment plugin synchronises cohort members with course participants.';
+
+
+
+
+$string['create_group']='Create local group';
+$string['config_create_group']='Automatically create a group with the same name/idnumber of the cohort';
+
+$string['create_grouping']='Create local grouping';
+$string['config_create_grouping']='Automatically create a group with the same name/idnumber of the cohort';
+
+$string['use_cohort_field']='Use this field for naming';
+$string['config_use_cohort_field']='Name the group/grouping as';
+
+$string['group_auto_cohort']='group autocreated from cohort {$a}';
+$string['grouping_auto_cohort']='grouping autocreated from cohort {$a}';
\ No newline at end of file
