fixed moodle cohorts sync
parent
d430ca4f9e
commit
9eb536e17c
|
@ -87,6 +87,9 @@ class Admin():
|
|||
self.external={'users':[],
|
||||
'groups':[],
|
||||
'roles':[]}
|
||||
log.warning(' SYSTEM READY TO HANDLE CONNECTIONS')
|
||||
# self.test_cohorts()
|
||||
self.delete_all_moodle_cohorts()
|
||||
|
||||
## This function should be moved to postup.py
|
||||
def default_setup(self):
|
||||
|
@ -221,7 +224,38 @@ class Admin():
|
|||
log.warning('MOODLE: OK')
|
||||
except:
|
||||
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):
|
||||
self.internal={'users':self._get_mix_users(),
|
||||
'groups':self._get_mix_groups(),
|
||||
|
@ -288,7 +322,7 @@ class Admin():
|
|||
|
||||
def _get_mix_users(self):
|
||||
kusers=self.get_keycloak_users()
|
||||
pprint(kusers)
|
||||
# pprint(kusers)
|
||||
musers=self.get_moodle_users()
|
||||
nusers=self.get_nextcloud_users()
|
||||
|
||||
|
@ -515,11 +549,13 @@ class Admin():
|
|||
self.resync_data()
|
||||
|
||||
def sync_to_moodle(self): # works from the internal (keycloak)
|
||||
### Process all groups from the users keycloak_groups key
|
||||
groups=[]
|
||||
for u in self.internal['users']:
|
||||
groups=groups+u['keycloak_groups']
|
||||
groups=list(dict.fromkeys(groups))
|
||||
|
||||
### Create all groups. Skip / in system groups
|
||||
total=len(groups)
|
||||
i=0
|
||||
for g in groups:
|
||||
|
@ -528,55 +564,66 @@ class Admin():
|
|||
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]
|
||||
if parts[i] in ['admin','manager','teacher','student']:
|
||||
subpath=parts[i]
|
||||
else:
|
||||
subpath=subpath+'/'+parts[i]
|
||||
self.moodle.add_system_cohort(subpath)
|
||||
except:
|
||||
log.error('probably exists')
|
||||
log.error(' MOODLE GROUPS: Group '+subpath+ ' probably already exists')
|
||||
i=i+1
|
||||
# print('ADDING FULL PATH: '+str(g))
|
||||
|
||||
log.error('getting cohorts')
|
||||
### Get all existing moodle cohorts
|
||||
cohorts=self.moodle.get_cohorts()
|
||||
|
||||
### Create users in moodle
|
||||
for u in self.internal['users']:
|
||||
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]
|
||||
print(str(user))
|
||||
# print(str(user))
|
||||
user_id=user['id']
|
||||
# [{'id': 5, 'username': 'xkrlzwd'}]
|
||||
|
||||
for g in u['keycloak_groups']:
|
||||
log.info('Adding moodle user: '+u['username']+' to cohort '+g)
|
||||
cohort_id=[c['id'] for c in cohorts if c['name']==g][0]
|
||||
# for g in u['keycloak_groups']:
|
||||
# log.info('Adding moodle user: '+u['username']+' to cohort '+g)
|
||||
# cohort_id=[c['id'] for c in cohorts if c['name']==g][0]
|
||||
|
||||
print(user_id)
|
||||
print(cohort_id)
|
||||
self.moodle.add_user_to_cohort(user_id,cohort_id)
|
||||
# # print(user_id)
|
||||
# # print(cohort_id)
|
||||
# self.moodle.add_user_to_cohort(user_id,cohort_id)
|
||||
|
||||
## Update cohorts on existing
|
||||
# self.resync_data()
|
||||
# for u in self.internal['users']:
|
||||
# if u['moodle']:
|
||||
self.resync_data()
|
||||
for u in self.internal['users']:
|
||||
if u['moodle']:
|
||||
total=len(groups)
|
||||
total=len(u['keycloak_groups'])
|
||||
index=0
|
||||
for g in groups:
|
||||
print('THE FULL GROUP TO BE ADDED NOW: '+g)
|
||||
for g in u['keycloak_groups']:
|
||||
log.error('for user groups')
|
||||
parts=g.split('/')
|
||||
subpath=''
|
||||
pprint(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]
|
||||
# log.info('Adding moodle user: '+u['username']+' to cohort '+subpath)
|
||||
cohort=[c['id'] 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)
|
||||
# print(u['moodle_id'])
|
||||
# print('Adding to cohort id '+str(cohort_id)+' with name '+g)
|
||||
# log.info('Adding moodle user: '+u['username']+' to cohort '+subpath)
|
||||
try:
|
||||
cohort=[c for c in cohorts if c['name']==subpath][0]
|
||||
except:
|
||||
pprint(subpath)
|
||||
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'])
|
||||
except:
|
||||
log.error('probably exists')
|
||||
log.error(' MOODLE USER GROUPS: User '+u['username']+' already exists in cohort '+cohort['name'])
|
||||
index=index+1
|
||||
|
||||
self.resync_data()
|
||||
### MISING ASSING USER TO ROLE COHORT
|
||||
|
||||
# for g in u['keycloak_groups']:
|
||||
|
@ -587,7 +634,10 @@ class Admin():
|
|||
# print('Adding to cohort id '+str(cohort_id)+' with name '+g)
|
||||
# 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):
|
||||
groups=[]
|
||||
|
@ -610,9 +660,9 @@ class Admin():
|
|||
i=i+1
|
||||
|
||||
for u in self.internal['users']:
|
||||
print('User '+u['username'])
|
||||
# print('User '+u['username'])
|
||||
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']))
|
||||
# group=u['keycloak_groups'][0] if len(u['keycloak_groups']) else False
|
||||
try:
|
||||
|
|
|
@ -144,7 +144,11 @@ class Moodle():
|
|||
def get_cohort_members(self, cohort_id):
|
||||
members = self.call('core_cohort_get_cohort_members', cohortids=[cohort_id])[0]['userids']
|
||||
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):
|
||||
user_cohorts=[]
|
||||
cohorts=self.get_cohorts()
|
||||
|
|
Loading…
Reference in New Issue