fixed moodle cohorts sync

root 2021-06-02 10:18:37 +02:00
parent d430ca4f9e
commit 9eb536e17c
2 changed files with 84 additions and 30 deletions

View File

@ -87,6 +87,9 @@ class Admin():
self.external={'users':[], self.external={'users':[],
'groups':[], 'groups':[],
'roles':[]} 'roles':[]}
log.warning(' SYSTEM READY TO HANDLE CONNECTIONS')
# self.test_cohorts()
self.delete_all_moodle_cohorts()
## This function should be moved to postup.py ## This function should be moved to postup.py
def default_setup(self): def default_setup(self):
@ -221,7 +224,38 @@ class Admin():
log.warning('MOODLE: OK') log.warning('MOODLE: OK')
except: except:
log.warning('MOODLE: Seems to be there already') log.warning('MOODLE: Seems to be there already')
### testing
def test_cohorts(self):
cohorts=self.moodle.get_cohorts()
testc=[c for c in cohorts if c['name'] in ['teacher','/teacher','student','/student']]
pprint(testc)
groups=[]
for u in self.internal['users']:
groups=groups+u['keycloak_groups']
groups=list(dict.fromkeys(groups))
pprint(groups)
pprint([g for g in groups if 'teacher' in g['keycloak_groups']])
exit(1)
total=len(groups)
i=0
for g in groups:
parts=g.split('/')
subpath=''
for i in range(1,len(parts)):
try:
log.warning(' MOODLE GROUPS: Adding group as cohort ('+str(i)+'/'+str(total)+'): '+subpath)
subpath=subpath+'/'+parts[i]
self.moodle.add_system_cohort(subpath)
except:
log.error('probably exists')
i=i+1
### end testing
def resync_data(self): def resync_data(self):
self.internal={'users':self._get_mix_users(), self.internal={'users':self._get_mix_users(),
'groups':self._get_mix_groups(), 'groups':self._get_mix_groups(),
@ -288,7 +322,7 @@ class Admin():
def _get_mix_users(self): def _get_mix_users(self):
kusers=self.get_keycloak_users() kusers=self.get_keycloak_users()
pprint(kusers) # pprint(kusers)
musers=self.get_moodle_users() musers=self.get_moodle_users()
nusers=self.get_nextcloud_users() nusers=self.get_nextcloud_users()
@ -515,11 +549,13 @@ class Admin():
self.resync_data() self.resync_data()
def sync_to_moodle(self): # works from the internal (keycloak) def sync_to_moodle(self): # works from the internal (keycloak)
### Process all groups from the users keycloak_groups key
groups=[] groups=[]
for u in self.internal['users']: for u in self.internal['users']:
groups=groups+u['keycloak_groups'] groups=groups+u['keycloak_groups']
groups=list(dict.fromkeys(groups)) groups=list(dict.fromkeys(groups))
### Create all groups. Skip / in system groups
total=len(groups) total=len(groups)
i=0 i=0
for g in groups: for g in groups:
@ -528,55 +564,66 @@ class Admin():
for i in range(1,len(parts)): for i in range(1,len(parts)):
try: try:
log.warning(' MOODLE GROUPS: Adding group as cohort ('+str(i)+'/'+str(total)+'): '+subpath) log.warning(' MOODLE GROUPS: Adding group as cohort ('+str(i)+'/'+str(total)+'): '+subpath)
subpath=subpath+'/'+parts[i] if parts[i] in ['admin','manager','teacher','student']:
subpath=parts[i]
else:
subpath=subpath+'/'+parts[i]
self.moodle.add_system_cohort(subpath) self.moodle.add_system_cohort(subpath)
except: except:
log.error('probably exists') log.error(' MOODLE GROUPS: Group '+subpath+ ' probably already exists')
i=i+1 i=i+1
# print('ADDING FULL PATH: '+str(g))
log.error('getting cohorts')
### Get all existing moodle cohorts
cohorts=self.moodle.get_cohorts() cohorts=self.moodle.get_cohorts()
### Create users in moodle
for u in self.internal['users']: for u in self.internal['users']:
if not u['moodle']: if not u['moodle']:
log.info('Creating moodle user: '+u['username']) log.error('Creating moodle user: '+u['username'])
user=self.moodle.create_user(u['email'],u['username'],'1Random 1String',u['first'],u['last'])[0] user=self.moodle.create_user(u['email'],u['username'],'1Random 1String',u['first'],u['last'])[0]
print(str(user)) # print(str(user))
user_id=user['id'] user_id=user['id']
# [{'id': 5, 'username': 'xkrlzwd'}] # [{'id': 5, 'username': 'xkrlzwd'}]
for g in u['keycloak_groups']: # for g in u['keycloak_groups']:
log.info('Adding moodle user: '+u['username']+' to cohort '+g) # log.info('Adding moodle user: '+u['username']+' to cohort '+g)
cohort_id=[c['id'] for c in cohorts if c['name']==g][0] # cohort_id=[c['id'] for c in cohorts if c['name']==g][0]
print(user_id) # # print(user_id)
print(cohort_id) # # print(cohort_id)
self.moodle.add_user_to_cohort(user_id,cohort_id) # self.moodle.add_user_to_cohort(user_id,cohort_id)
## Update cohorts on existing ## Update cohorts on existing
# self.resync_data()
# for u in self.internal['users']:
# if u['moodle']:
self.resync_data() self.resync_data()
for u in self.internal['users']: for u in self.internal['users']:
if u['moodle']: total=len(u['keycloak_groups'])
total=len(groups)
index=0 index=0
for g in groups: for g in u['keycloak_groups']:
print('THE FULL GROUP TO BE ADDED NOW: '+g) log.error('for user groups')
parts=g.split('/') parts=g.split('/')
subpath='' subpath=''
pprint(parts)
for i in range(1,len(parts)): for i in range(1,len(parts)):
try: if parts[i] in ['admin','manager','teacher','student']:
subpath=parts[i]
else:
subpath=subpath+'/'+parts[i] subpath=subpath+'/'+parts[i]
# log.info('Adding moodle user: '+u['username']+' to cohort '+subpath) # log.info('Adding moodle user: '+u['username']+' to cohort '+subpath)
cohort=[c['id'] for c in cohorts if c['name']==subpath][0] try:
cohort=[c for c in cohorts if c['name']==subpath][0]
log.warning(' MOODLE USER GROUPS GROUPS: Adding user '+u['username']+' to group as cohort '+cohort['name']+' ('+str(index)+'/'+str(total)+'): '+subpath) except:
# print(u['moodle_id']) pprint(subpath)
# print('Adding to cohort id '+str(cohort_id)+' with name '+g) log.error(' MOODLE USER GROUPS: keycloak group does not exist as moodle cohort')
try:
self.moodle.add_user_to_cohort(u['moodle_id'],cohort['id']) self.moodle.add_user_to_cohort(u['moodle_id'],cohort['id'])
except: except:
log.error('probably exists') log.error(' MOODLE USER GROUPS: User '+u['username']+' already exists in cohort '+cohort['name'])
index=index+1 index=index+1
self.resync_data()
### MISING ASSING USER TO ROLE COHORT ### MISING ASSING USER TO ROLE COHORT
# for g in u['keycloak_groups']: # for g in u['keycloak_groups']:
@ -587,7 +634,10 @@ class Admin():
# print('Adding to cohort id '+str(cohort_id)+' with name '+g) # print('Adding to cohort id '+str(cohort_id)+' with name '+g)
# pprint(self.moodle.add_user_to_cohort(u['moodle_id'],cohort_id)) # pprint(self.moodle.add_user_to_cohort(u['moodle_id'],cohort_id))
def delete_all_moodle_cohorts(self):
cohorts=self.moodle.get_cohorts()
ids=[c['id'] for c in cohorts]
self.moodle.delete_cohorts(ids)
def sync_to_nextcloud(self): def sync_to_nextcloud(self):
groups=[] groups=[]
@ -610,9 +660,9 @@ class Admin():
i=i+1 i=i+1
for u in self.internal['users']: for u in self.internal['users']:
print('User '+u['username']) # print('User '+u['username'])
if not u['nextcloud']: if not u['nextcloud']:
print(' Is not in nextcloud') # print(' Is not in nextcloud')
log.warning(' NEXTCLOUD USERS: Creating nextcloud user: '+u['username']+' in groups '+str(u['keycloak_groups'])) log.warning(' NEXTCLOUD USERS: Creating nextcloud user: '+u['username']+' in groups '+str(u['keycloak_groups']))
# group=u['keycloak_groups'][0] if len(u['keycloak_groups']) else False # group=u['keycloak_groups'][0] if len(u['keycloak_groups']) else False
try: try:

View File

@ -144,7 +144,11 @@ class Moodle():
def get_cohort_members(self, cohort_id): def get_cohort_members(self, cohort_id):
members = self.call('core_cohort_get_cohort_members', cohortids=[cohort_id])[0]['userids'] members = self.call('core_cohort_get_cohort_members', cohortids=[cohort_id])[0]['userids']
return members return members
def delete_cohorts(self, cohortids):
deleted = self.call('core_cohort_delete_cohorts', cohortids=cohortids)
return deleted
def get_user_cohorts(self, user_id): def get_user_cohorts(self, user_id):
user_cohorts=[] user_cohorts=[]
cohorts=self.get_cohorts() cohorts=self.get_cohorts()