Fixed admin sync to moodle

darta 2021-11-11 11:39:55 +01:00
parent fbe87a9b27
commit 59770ce22c
1 changed files with 32 additions and 39 deletions

View File

@ -778,26 +778,24 @@ class Admin():
groups=groups+u['keycloak_groups']
groups=list(dict.fromkeys(groups))
### Create all groups. Skip / in system groups
total=len(groups)
i=0
ev=Events('Syncing groups from keycloak to moodle',total=len(groups))
for g in groups:
parts=g.split('/')
subpath=''
for i in range(1,len(parts)):
pathslist=[]
for group in groups:
pathpart=''
for part in group.split('.'):
if pathpart=='':
pathpart=part
else:
pathpart=pathpart+'.'+part
pathslist.append(pathpart)
ev.increment({'name':pathpart})
try:
log.warning(' MOODLE GROUPS: Adding group as cohort ('+str(i)+'/'+str(total)+'): '+subpath)
ev.increment({'name':subpath})
if parts[i] in ['admin','manager','teacher','student']:
subpath=parts[i]
else:
subpath=subpath+'/'+parts[i]
self.moodle.add_system_cohort(subpath)
log.info('MOODLE: Adding group '+pathpart)
self.moodle.add_system_cohort(pathpart)
except:
log.error(' MOODLE GROUPS: Group '+subpath+ ' probably already exists')
i=i+1
#print(traceback.format_exc())
log.error('MOODLE: Group '+pathpart+' probably already exists.')
### Get all existing moodle cohorts
cohorts=self.moodle.get_cohorts()
@ -816,31 +814,26 @@ class Admin():
# user_id=user['id']
self.resync_data()
### Add user to their cohorts (groups)
ev=Events('Syncing users groups from keycloak to moodle cohorts',total=len(self.internal['users']))
ev=Events('Syncing users with moodle cohorts',total=len(self.internal['users']))
cohorts=self.moodle.get_cohorts()
for u in self.internal['users']:
total=len(u['keycloak_groups'])
index=0
ev.increment({'name':u['username']})
for g in u['keycloak_groups']:
parts=g.split('/')
subpath=''
for i in range(1,len(parts)):
if parts[i] in ['admin','manager','teacher','student']:
subpath=parts[i]
else:
subpath=subpath+'/'+parts[i]
groups=u['keycloak_groups']+[u['roles'][0]]
for path in groups:
try:
cohort=[c for c in cohorts if c['name']==path][0]
except:
# print(traceback.format_exc())
log.error(' MOODLE USER GROUPS: keycloak group '+path+' does not exist as moodle cohort. This should not happen. User '+u['username']+ ' not added.')
try:
cohort=[c for c in cohorts if c['name']==subpath][0]
except:
log.error(' MOODLE USER GROUPS: keycloak group '+subpath+' does not exist as moodle cohort. This should not happen. User '+u['username']+ ' not added.')
try:
self.moodle.add_user_to_cohort(u['moodle_id'],cohort['id'])
except:
log.error(traceback.format_exc())
log.error(' MOODLE USER GROUPS: User '+u['username']+' already exists in cohort '+cohort['name'])
ev.increment({'name':'User '+u['username']+' added to moodle cohorts','data':[]})
try:
self.moodle.add_user_to_cohort(u['moodle_id'],cohort['id'])
except:
log.error(' MOODLE USER GROUPS: User '+u['username']+' already exists in cohort '+cohort['name'])
index=index+1
self.resync_data()
def sync_to_nextcloud(self):