$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; } } //break; }
function get_users_having_attribute_value($attributevalue) { global $CFG, $DB; //build a filter $filter = '(&(' . $this->config->user_attribute . '=*)' . $this->config->objectclass . ')'; $filter = '(&' . $filter . '(' . $this->config->cohort_synching_ldap_attribute_attribute . '=' . ldap_addslashes($attributevalue) . '))'; if ($CFG->debug_ldap_groupes) { pp_print_object('looking for ', $filter); } // call Moodle ldap_get_userlist that return it as an array with Moodle user attributes names $matchings = $this->ldap_get_userlist($filter); // return the FIRST entry found if (empty($matchings)) { if ($CFG->debug_ldap_groupes) { pp_print_object('not found', ''); } return array(); } if ($CFG->debug_ldap_groupes) { pp_print_object('found ', count($matchings) . ' matching users in LDAP'); } $ret = array(); //remove all matching LDAP users unkown to Moodle foreach ($matchings as $member) { $params = array('username' => $member); if ($user = $DB->get_record('user', $params, 'id,username')) { $ret[$user->id] = $user->username; } } if ($CFG->debug_ldap_groupes) { pp_print_object('found ', count($ret) . ' matching users known to Moodle'); } return $ret; }
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); } // print ($cohortid." "); if ($CFG->debug_ldap_groupes) { pp_print_object("members of LDAP {$cohortname} known to Moodle", $ldap_members); } $cohort_members = $plugin->get_cohort_members($cohortid); // if ($CFG->debug_ldap_groupes){ // pp_print_object("current members of cohort $cohortname", $cohort_members); // } foreach ($cohort_members as $userid => $user) { if (!isset($ldap_members[$userid])) { cohort_remove_member($cohortid, $userid); print "removing " . $user->username . " from cohort " . $cohortname . 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 " . $cohortname . PHP_EOL;