From c9d2261ea4c8536c6d22caa3d0b5bcda870e7c69 Mon Sep 17 00:00:00 2001
From: patrickpollet <pp@patrickpollet.net>
Date: Thu, 1 Mar 2012 16:57:20 +0100
Subject: [PATCH 2/2] ajout suppression du groupe lors de la radiation d'une cohorte

---
 cohort/lib.php            |    2 +-
 enrol/cohort/locallib.php |   48 ++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/cohort/lib.php b/cohort/lib.php
index e33f77a..d8a0786 100644
--- a/cohort/lib.php
+++ b/cohort/lib.php
@@ -330,7 +330,7 @@ class cohort_existing_selector extends user_selector_base {
 
         if (!$this->is_validating()) {
             $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
-            if ($potentialmemberscount > 100) {
+            if ($potentialmemberscount > 250) {
                 return $this->too_many_results($search, $potentialmemberscount);
             }
         }
diff --git a/enrol/cohort/locallib.php b/enrol/cohort/locallib.php
index e533c86..18885ee 100644
--- a/enrol/cohort/locallib.php
+++ b/enrol/cohort/locallib.php
@@ -75,6 +75,11 @@ class enrol_cohort_handler {
         foreach ($enrols as $enrol) {
             // no problem if already unenrolled
             $plugin->unenrol_user($enrol, $ca->userid);
+            //PATCH PP remove user from group synched with cohort
+            if ($enrol->courseid) {
+                  enrol_cohort_remove_user_synched_group ($enrol, $ca->userid, $ca->cohortid);
+            }   
+            //END PATCH
         }
 
         return true;
@@ -151,7 +156,9 @@ function enrol_cohort_sync($courseid = NULL) {
     }
 
     // unenrol as necessary - ignore enabled flag, we want to get rid of all
-    $sql = "SELECT ue.userid, e.id AS enrolid
+    //PATCH PP we must retrieve also the cohortid 
+    //$sql = "SELECT ue.userid, e.id AS enrolid  
+     $sql = "SELECT ue.userid, e.id AS enrolid, e.customint1 as cohortid
               FROM {user_enrolments} ue
               JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' $onecourse)
          LEFT JOIN {cohort_members} cm ON (cm.cohortid  = e.customint1 AND cm.userid = ue.userid)
@@ -159,14 +166,25 @@ function enrol_cohort_sync($courseid = NULL) {
     //TODO: this may use a bit of SQL optimisation
     $rs = $DB->get_recordset_sql($sql, array('courseid'=>$courseid));
     $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->unenrol_user($instances[$ue->enrolid], $ue->userid);
+        //PATCH PP remove user from group synched with cohort
+        if ($courseid) {
+            if (!isset($cohorts[$ue->cohortid])) {
+                        $cohorts[$ue->cohortid]=$DB->get_record('cohort', array('id'=>$ue->cohortid));
+            }
+            enrol_cohort_remove_user_synched_group($instances[$ue->enrolid], $ue->userid, $cohorts[$ue->cohortid]);
+            //    ($enrol, $ca->userid, $ca->cohortid);
+        }   
+        //END PATCH
     }
     $rs->close();
     unset($instances);
+    unset($cohorts);
     
     // now assign all necessary roles
     if (enrol_is_enabled('cohort')) {
@@ -440,5 +458,33 @@ function enrol_cohort_sync_group_grouping ($enrol, $userid, $cohortorcohortid) {
 
 
 }
+
+/**
+ * remove user from group synched with a cohort 
+ * @param unknown_type $enrol
+ * @param unknown_type $userid
+ * @param unknown_type $cohortorcohortid
+ */
+function  enrol_cohort_remove_user_synched_group ($enrol, $userid, $cohortorcohortid) {
+   global $CFG, $DB;
+    
+    if ($enrol->courseid) { //either the target course or all course during a cron synchronisation
+        if (!empty($CFG->enrolcohortsyncautocreategroup) || !empty($CFG->enrolcohortsyncautocreategrouping)) {
+            require_once ($CFG->dirroot.'/group/lib.php');
+            if (is_numeric($cohortorcohortid))
+                $cohort=$DB->get_record('cohort', array('id'=>$cohortorcohortid));
+            else 
+                $cohort=$cohortorcohortid;
+            //look for the associated group 
+            $namefield=!empty($CFG->enrolcohortsyncuseidnumber)?'idnumber':'name'; // naming attribute for the associated group/grouping (name or idnumber)
+            $groupname= $cohort->$namefield;
+            if ($groupid=groups_get_group_by_name($enrol->courseid,$groupname)) {
+                groups_remove_member($groupid,$userid);
+            }    
+        }       
+    }
+}
+
+
 //END PATCH 
 
-- 
1.7.1

