From fda21199323daa5ae1004c5c017abf2411fa548a Mon Sep 17 00:00:00 2001
From: patrickpollet <pp@patrickpollet.net>
Date: Thu, 3 May 2012 11:00:51 +0200
Subject: [PATCH 5/5] =?UTF-8?q?synchro=20des=20cohortes=20avec=20les=20groupes=20:=20il=20FAUT=20remettre=20les=20membres=20du=20groupes=20qui=20auraient=20=C3=A9t=C3=A9=20radi=C3=A9s=20manuellement=20par=20un=20enseignant?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 enrol/cohort/locallib.php |   45 ++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/enrol/cohort/locallib.php b/enrol/cohort/locallib.php
index 18885ee..2c79daf 100644
--- a/enrol/cohort/locallib.php
+++ b/enrol/cohort/locallib.php
@@ -173,13 +173,12 @@ function enrol_cohort_sync($courseid = NULL) {
         }
         $cohort->unenrol_user($instances[$ue->enrolid], $ue->userid);
         //PATCH PP remove user from group synched with cohort
-        if ($courseid) {
+        //if ($courseid) {   why testing for courseid ? it is null when cron runs this function 
             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();
@@ -221,6 +220,46 @@ function enrol_cohort_sync($courseid = NULL) {
         role_unassign($ra->roleid, $ra->userid, $ra->contextid, 'enrol_cohort', $ra->itemid);
     }
     $rs->close();
+    
+ /* Final touch. If an enroled user, synched from a cohort has been removed from the associated group
+  * by some teacher, we must put him back in the associated group ....
+ */
+    if (enrol_is_enabled('cohort') &&  !empty($CFG->enrolcohortsyncautocreategroup)) {
+        $params = array();
+        $onecourse = "";
+        if ($courseid) {
+            $params['courseid'] = $courseid;
+            $onecourse = "AND e.courseid = :courseid";
+        }
+        //PATCH PP we must retrieve also the cohortid 
+        //$sql = "SELECT cm.userid, e.id AS enrolid
+        $sql = "SELECT cm.userid, e.id AS enrolid, e.customint1 as cohortid
+                  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)
+                 WHERE ue.id IS NOT NULL";    
+        $params['statusenabled'] = ENROL_INSTANCE_ENABLED;
+        $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));
+            }
+         
+            if (!isset($cohorts[$ue->cohortid])) {
+                        $cohorts[$ue->cohortid]=$DB->get_record('cohort', array('id'=>$ue->cohortid));
+            }
+            // put him back in group if needed 
+            enrol_cohort_sync_group_grouping ($instances[$ue->enrolid], $ue->userid, $cohorts[$ue->cohortid]);
+        }
+        $rs->close();
+        unset($instances);
+        unset($cohorts);
+    }
+    
+    
 
 }
 
-- 
1.7.1

