$cohort_names = $plugin->get_attribute_distinct_values();
if ($CFG->debug_ldap_groupes) {
    pp_print_object("cohort idnumbers", $cohort_names);
}
foreach ($cohort_names as $n => $cohortname) {
    print "processing cohort " . $cohortname . PHP_EOL;
    $params = array('idnumber' => $cohortname);
    // not that we search for cohort IDNUMBER and not name for a match
    // thus it we do not autocreate cohorts, admin MUST create cohorts beforehand
    // and set their IDNUMBER to the exact value of the corresponding attribute in LDAP
    if (!($cohort = $DB->get_record('cohort', $params, '*'))) {
        if (empty($plugin->config->cohort_synching_ldap_attribute_autocreate_cohorts)) {
            print "ignoring {$cohortname} that does not exist in Moodle (autocreation is off)" . PHP_EOL;
            continue;
        }
        $ldap_members = $plugin->get_users_having_attribute_value($cohortname);
        // do not create yet the cohort if no known Moodle users are concerned
        if (count($ldap_members) == 0) {
            print "not creating empty cohort " . $cohortname . PHP_EOL;
            continue;
        }
        $cohort = new StdClass();
        $cohort->name = $cohort->idnumber = $cohortname;
        $cohort->contextid = context_system::instance()->id;
        $cohort->description = get_string('cohort_synchronized_with_attribute', 'local_ldap', $plugin->config->cohort_synching_ldap_attribute_attribute);
        $cohortid = cohort_add_cohort($cohort);
        print "creating cohort " . $cohortname . PHP_EOL;
    } else {
        $cohortid = $cohort->id;
        $ldap_members = $plugin->get_users_having_attribute_value($cohortname);
    }