ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilECSCmsCourseMemberCommandQueueHandler.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
25 {
26  protected ilLogger $log;
27 
29  private int $mid = 0;
30 
31  private ?ilECSnodeMappingSettings $mapping = null;
32 
36  public function __construct(ilECSSetting $server)
37  {
38  global $DIC;
39  $this->log = $DIC->logger()->wsrv();
40  $this->server = $server;
41  }
42 
46  public function getServer(): \ilECSSetting
47  {
48  return $this->server;
49  }
50 
54  public function getMid(): int
55  {
56  return $this->mid;
57  }
58 
62  public function checkAllocationActivation(ilECSSetting $server, $a_content_id): bool
63  {
64  try {
65  $crsm_reader = new ilECSCourseMemberConnector($server);
66  $details = $crsm_reader->getCourseMember($a_content_id, true);
67  $this->mid = $details->getMySender();
68 
69  // Check if import is enabled
70  $part = ilECSParticipantSetting::getInstance($this->getServer()->getServerId(), $this->getMid());
71  if (!$part->isImportEnabled()) {
72  $this->log->warning('Import disabled for mid ' . $this->getMid());
73  return false;
74  }
75  // Check course allocation setting
77  $this->getServer()->getServerId(),
78  $this->getMid()
79  );
80  return $this->mapping->isCourseAllocationEnabled();
81  } catch (ilECSConnectorException $e) {
82  $this->log->error('Reading course member details failed with message ' . $e->getMessage());
83  return false;
84  }
85  }
86 
87 
91  public function handleCreate(ilECSSetting $server, $a_content_id): bool
92  {
93  if (!$this->checkAllocationActivation($server, $a_content_id)) {
94  return true;
95  }
96  try {
97  //$course = $this->readCourse($server, $a_content_id);
98  $course_member = $this->readCourseMember($server, $a_content_id);
99  $this->doUpdate($a_content_id, $course_member);
100  return true;
101  } catch (ilECSConnectorException $e) {
102  $this->log->error('Course member creation failed with mesage ' . $e->getMessage());
103  return false;
104  }
105  return true;
106  }
107 
111  public function handleDelete(ilECSSetting $server, $a_content_id): bool
112  {
113  // nothing todo
114  return true;
115  }
116 
120  public function handleUpdate(ilECSSetting $server, $a_content_id): bool
121  {
122  if (!$this->checkAllocationActivation($server, $a_content_id)) {
123  return true;
124  }
125 
126  try {
127  $course_member = $this->readCourseMember($server, $a_content_id);
128  $this->doUpdate($a_content_id, $course_member);
129  return true;
130  } catch (ilECSConnectorException $e) {
131  $this->log->error('Course member update failed with mesage ' . $e->getMessage());
132  return false;
133  }
134  return true;
135  }
136 
137 
141  protected function doUpdate($a_content_id, $course_member): bool
142  {
143  $this->log->debug('Starting ecs member update');
144 
145  if (is_array($course_member) && !isset($course_member["lectureID"])) {
146  $this->log->warning('Missing course id in course_member');
147  return false;
148  }
149 
150  $course_id = (int) $course_member->lectureID;
151  if (!$course_id) {
152  $this->log->warning('Missing course id');
153  return false;
154  }
155  $this->log->debug('sid: ' . $this->getServer()->getServerId() . ' course_id: ' . $course_id . ' mid: ' . $this->mid);
156  //$crs_obj_id = ilECSImportManager::getInstance()->_lookupObjId($this->getServer()->getServerId(), $course_id, $this->mid);
157  $crs_obj_id = ilECSImportManager::getInstance()->lookupObjIdByContentId($this->getServer()->getServerId(), $this->mid, $course_id);
158 
159  if (!$crs_obj_id) {
160  $this->log->info('No main course created. Group scenario >= 3 ?');
161  }
162 
163  $course = $this->readCourse($course_member);
164  if (is_null($course)) {
165  $this->log->info('No course found, skip processing' . print_r($course_member, true));
166  return true;
167  }
168  // Lookup already imported users and update their status
169  $assignments = $this->readAssignments($course, $course_member);
170 
171  $this->log->debug('Parallel group assignments');
172  $this->log->dump($assignments, ilLogLevel::DEBUG);
173  $this->log->debug('------------------ End assignemtns');
174 
175  // iterate through all parallel groups
176  foreach ($assignments as $cms_id => $assigned) {
177  $sub_id = ($cms_id === $course_id) ? null : (string) $cms_id;
178 
179  $this->log->debug('sub id is ' . is_null($sub_id) ? "<null>" : $sub_id . ' for ' . $cms_id);
180 
181  $obj_id = ilECSImportManager::getInstance()->lookupObjIdByContentId(
182  $this->getServer()->getServerId(),
183  $this->getMid(),
184  $course_id,
185  $sub_id
186  );
187 
188  $this->refreshAssignmentStatus($course_member, $obj_id, $sub_id, $assigned);
189  }
190  return true;
191  }
192 
196  protected function readAssignments($course, $course_member): array
197  {
198  //TODO check if this switch is still needed
199  if (!property_exists($course, 'groupScenario')) {
200  $course->groupScenario = ilECSMappingUtils::PARALLEL_ONE_COURSE;
201  }
202 
203  switch ((int) $course->groupScenario) {
205  $this->log->debug('Parallel group scenario one course.');
206  break;
207 
209  $this->log->debug('Parallel group scenario groups in courses.');
210  break;
211 
213  $this->log->debug('Parallel group scenario only courses.');
214  break;
215 
216  default:
217  $this->log->debug('Parallel group scenario undefined.');
218  break;
219  }
220 
221  $course_id = $course_member->lectureID;
222  $assigned = array();
223  foreach ((array) $course_member->members as $member) {
224  $assigned[$course_id][$member->personID] = array(
225  'id' => $member->personID,
226  'role' => $member->role
227  );
228  if ((int) $course->groupScenario === ilECSMappingUtils::PARALLEL_ONE_COURSE) {
229  $this->log->debug('Group scenarion "one course". Ignoring group assignments');
230  continue;
231  }
232 
233  if (property_exists($member, 'groups')) {
234  foreach ((array) $member->groups as $pgroup) {
235  // the sequence number in the course ressource
236  $sequence_number = (int) $pgroup->num;
237  // find parallel group with by sequence number
238  if (property_exists($course, 'groups')) {
239  $tmp_pgroup = $course->groups[$sequence_number] ?? null;
240 
241  if (is_object($tmp_pgroup) && $tmp_pgroup->id !== '') {
242  $this->log->debug('Found parallel group with id: ' . $tmp_pgroup->id . ': for sequence number: ' . $sequence_number);
243 
244  // @todo check hierarchy of roles
245  $assigned[$tmp_pgroup->id][$member->personID] = array(
246  'id' => $member->personID,
247  'role' => $pgroup->role
248  );
249  } else {
250  $this->log->warning('Cannot find parallel group with sequence id: ' . $sequence_number);
251  }
252  } else {
253  $this->log->error('Invalid course/group-configuration found, ignoring}');
254  }
255 
256  }
257  }
258  }
259  $this->log->debug('ECS member assignments ' . print_r($assigned, true));
260  return $assigned;
261  }
262 
263 
264 
268  protected function refreshAssignmentStatus(object $course_member, int $obj_id, ?string $sub_id, $assigned): bool
269  {
270  $this->log->debug('Currrent sub_id = ' . $sub_id . ', obj_id = ' . $obj_id);
271 
272  $type = ilObject::_lookupType($obj_id);
273  if ($type === 'crs') {
275  } elseif ($type === 'grp') {
277  } else {
278  $this->log->warning('Invalid object type given for obj_id: ' . $obj_id);
279  return false;
280  }
281 
282  $course_id = (int) $course_member->lectureID;
284  $course_id,
285  is_null($sub_id) ? $sub_id : (int) $sub_id,
286  $obj_id
287  );
288  $this->log->debug('Current coursemember assignments:');
289  $this->log->dump($usr_ids, ilLogLevel::DEBUG);
290 
291  // Delete remote deleted
292  foreach ($usr_ids as $usr_id) {
293  if (!isset($assigned[$usr_id])) {
294  $ass = ilECSCourseMemberAssignment::lookupAssignment($course_id, is_null($sub_id) ? $sub_id : (int) $sub_id, $obj_id, $usr_id);
295  if ($ass instanceof ilECSCourseMemberAssignment) {
297  $this->mapping->getAuthMode(),
298  (string) $usr_id
299  );
300 
301  $this->log->debug('Local user assignment: ' . $usr_id . ' <-> ' . $login);
302 
303  if ($il_usr_id = ilObjUser::_lookupId($login)) {
304  // this removes also admin, tutor roles
305  $part->delete($il_usr_id);
306  $this->log->info('Deassigning user ' . $usr_id . ' ' . 'from course ' . ilObject::_lookupTitle($obj_id));
307  } else {
308  $this->log->notice('Deassigning unknown ILIAS user ' . $usr_id . ' ' . 'from course ' . ilObject::_lookupTitle($obj_id));
309  }
310 
311  $ass->delete();
312  }
313  }
314  }
315 
316  $this->log->debug('Handled assignmnent...');
317 
318  // Assign new participants
319  foreach ((array) $assigned as $person_id => $person) {
320  $role = $this->lookupRole((string) $person['role'], $type);
321  $role_info = ilECSMappingUtils::getRoleMappingInfo($role);
322 
323  $this->log->debug('Using role info...');
325  $this->mapping->getAuthMode(),
326  (string) $person_id
327  );
328  $this->log->info('Handling user ' . $person_id);
329 
330  if (in_array($person_id, $usr_ids, true)) {
331  if ($il_usr_id = ilObjUser::_lookupId($login)) {
332  $this->log->info('Update user assignment for: ' . $person_id);
333  $part->updateRoleAssignments($il_usr_id, array($part->getAutoGeneratedRoleId($role)));
334  } elseif ($role_info['create']) {
335  $this->createMember($person_id);
336  $this->log->info('Added new user ' . $person_id);
338  $this->mapping->getAuthMode(),
339  (string) $person_id
340  );
341  if ($role && $il_usr_id = ilObjUser::_lookupId($login)) {
342  $part->add($il_usr_id, $role);
343  $part->sendNotification(ilCourseMembershipMailNotification::TYPE_ADMISSION_MEMBER, $il_usr_id);
344  }
345  }
346  } else {
347  if ($il_usr_id = ilObjUser::_lookupId($login)) {
348  // user exists => assign to course/group
349  if ($role) {
350  // Assign user
351  $this->log->info('Assigning new user ' . $person_id . ' ' . 'to ' . ilObject::_lookupTitle($obj_id) . ' using role: ' . $role);
352  $part->add($il_usr_id, $role);
353  $part->sendNotification(ilCourseMembershipMailNotification::TYPE_ADMISSION_MEMBER, $il_usr_id);
354  }
355  } else {
356  // no local user exists
357  if ($role_info['create']) {
358  $this->createMember($person_id);
359  $this->log->info('Added new user ' . $person_id);
361  $this->mapping->getAuthMode(),
362  (string) $person_id
363  );
364  }
365  // Assign to role
366  if ($role && $il_usr_id = ilObjUser::_lookupId($login)) {
367  $this->log->info('Assigning new role to existing user ' . $person_id . ' ' . 'to ' . ilObject::_lookupTitle($obj_id) . ' using role: ' . $role);
368  $part->add($il_usr_id, $role);
369  $part->sendNotification(ilCourseMembershipMailNotification::TYPE_ADMISSION_MEMBER, $il_usr_id);
370  }
371  }
372 
373  $assignment = new ilECSCourseMemberAssignment();
374  $assignment->setServer($this->getServer()->getServerId());
375  $assignment->setMid($this->mid);
376  $assignment->setCmsId($course_id);
377  $assignment->setCmsSubId(is_null($sub_id) ? $sub_id : (int) $sub_id);
378  $assignment->setObjId($obj_id);
379  $assignment->setUid((string) $person_id);
380  $assignment->save();
381  }
382  }
383  return true;
384  }
385 
389  protected function lookupRole(string $role_value, $a_obj_type): int
390  {
391  $role_mappings = $this->mapping->getRoleMappings();
392 
393  /* Zero is an allowed value */
394  if (!$role_value) {
395  $this->log->debug('No role assignment attribute: role');
396  }
397  foreach ($role_mappings as $name => $map) {
398  $this->log->debug('Role "name" is ' . $name);
399 
400  // map is a string of ids seperated by ","
401  $exploded_map = (array) explode(',', $map);
402  if (in_array($role_value, $exploded_map, true)) {
403  switch ($name) {
407  if ($a_obj_type === 'crs') {
408  $this->log->debug('Role: ' . $role_value . ' maps: ' . $map);
409  return $name;
410  }
411  break;
412 
415  if ($a_obj_type === 'grp') {
416  $this->log->debug('Role: ' . $role_value . ' maps: ' . $map);
417  return $name;
418  }
419  break;
420  }
421  }
422  }
423  $this->log->info('No role assignment mapping for role ' . $role_value);
424  return 0;
425  }
426 
430  private function createMember($a_person_id): void
431  {
432  if (!$this->mapping instanceof ilECSNodeMappingSettings) {
433  $this->log->warning('Node mapping settings not initialized.');
434  }
435  $auth_mode = $this->mapping->getAuthMode();
436 
437  if (
438  $this->mapping->getAuthMode() ===
440  ) {
441  $this->log->info('Not handling direct user creation for auth mode: ' . $auth_mode);
442  return;
443  }
444  if (strpos($auth_mode, 'ldap') !== 0) {
445  $this->log->info('Not handling direct user creation for auth mode: ' . $auth_mode);
446  return;
447  }
448 
449  try {
451  $server->doConnectionCheck();
452 
453  $query = new ilLDAPQuery($server);
454  $query->bind(ilLDAPQuery::LDAP_BIND_DEFAULT);
455 
456  $users = $query->fetchUser((string) $a_person_id);
457  if ($users) {
459 
460  $xml = new ilLDAPAttributeToUser($server);
461  $xml->setNewUserAuthMode($server->getAuthenticationMappingKey());
462  $xml->setUserData($users);
463  $xml->refresh();
464  }
465  } catch (ilLDAPQueryException $exc) {
466  $this->log->error($exc->getMessage());
467  }
468  }
469 
470 
474  private function readCourseMember(ilECSSetting $server, $a_content_id)
475  {
476  return (new ilECSCourseMemberConnector($server))->getCourseMember($a_content_id);
477  }
478 
482  private function readCourse($course_member)
483  {
484  $ecs_id = ilECSImportManager::getInstance()->lookupEContentIdByContentId(
485  $this->getServer()->getServerId(),
486  $this->getMid(),
487  $course_member->lectureID
488  );
489  if (0 === $ecs_id) {
490  return null;
491  }
492  return (new ilECSCourseConnector($this->getServer()))->getCourse($ecs_id);
493  }
494 }
handleCreate(ilECSSetting $server, $a_content_id)
Handle create.
static _getFirstActiveServer()
Get first active server.
static getInstance(int $a_server_id, int $mid)
Get instance by server id and mid.
static getRoleMappingInfo($a_role_type_info=0)
Get role mapping info.
static getInstanceByServerId(int $a_server_id)
Get instance by server id.
static getInstanceByServerMid(int $a_server_id, int $a_mid)
Get instance.
if(!file_exists('../ilias.ini.php'))
static _lookupId($a_user_str)
lookupRole(string $role_value, $a_obj_type)
Lookup local role by assignment.
static _checkExternalAuthAccount(string $a_auth, string $a_account, bool $tryFallback=true)
check whether external account and authentication method matches with a user
server()
description: > This example shows how a Progress Bar can be rendered and updated by the server...
Definition: server.php:27
checkAllocationActivation(ilECSSetting $server, $a_content_id)
Check if course allocation is activated for one recipient of the.
static _getAuthModeName($a_auth_key)
Interface for all command queue handler classes.
handleDelete(ilECSSetting $server, $a_content_id)
Handle delete.
static lookupAssignment(int $a_cms_id, ?int $a_cms_sub_id, int $a_obj_id, string $a_usr_id)
Lookup assignment of user.
static getInstance()
Get the singleton instance of this ilECSImportManager.
static _lookupTitle(int $obj_id)
static _getInstanceByObjId(int $a_obj_id)
readAssignments($course, $course_member)
Read assignments for all parallel groups.
global $DIC
Definition: shib_login.php:25
static lookupUserIds(int $a_cms_id, ?int $a_cms_sub_id, int $a_obj_id)
Lookup user ids.
Connector for course member ressource.
Storage of ecs course assignments.
static _getInstanceByObjId(int $a_obj_id)
Get singleton instance.
readCourseMember(ilECSSetting $server, $a_content_id)
Read course from ecs.
handleUpdate(ilECSSetting $server, $a_content_id)
Handle update.
Update/create ILIAS user account by given LDAP attributes according to user attribute mapping setting...
refreshAssignmentStatus(object $course_member, int $obj_id, ?string $sub_id, $assigned)
Refresh status of course member assignments.
static _lookupType(int $id, bool $reference=false)