$cohort = new StdClass();
     $cohort->name = $cohort->idnumber = $groupname;
     $cohort->contextid = context_system::instance()->id;
     //$cohort->component='sync_ldap';
     $cohort->description = get_string('cohort_synchronized_with_group', 'local_ldap', $groupname);
     //print_r($cohort);
     $cohortid = cohort_add_cohort($cohort);
     print "creating cohort " . $group . PHP_EOL;
 } else {
     $cohortid = $cohort->id;
     $ldap_members = $plugin->ldap_get_group_members($groupname);
 }
 if ($CFG->debug_ldap_groupes) {
     pp_print_object("members of LDAP group {$groupname} known to Moodle", $ldap_members);
 }
 $cohort_members = $plugin->get_cohort_members($cohortid);
 if ($CFG->debug_ldap_groupes) {
     pp_print_object("current members of cohort {$groupname}", $cohort_members);
 }
 foreach ($cohort_members as $userid => $user) {
     if (!isset($ldap_members[$userid])) {
         cohort_remove_member($cohortid, $userid);
         print "removing " . $user->username . " from cohort " . $groupname . PHP_EOL;
     }
 }
 foreach ($ldap_members as $userid => $username) {
     if (!$plugin->cohort_is_member($cohortid, $userid)) {
         cohort_add_member($cohortid, $userid);
         print "adding " . $username . " to cohort " . $groupname . PHP_EOL;
     }
 }