2023-01-22 19:32:03 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# Konvertiere IServ-Gruppenlisten in Moodle-Importdateien
|
2024-09-07 14:35:42 +02:00
|
|
|
# 2022-2024 by Daniel Spittank, daniel.spittank@gymsedan.de
|
2023-01-22 19:32:03 +01:00
|
|
|
# Lizenz: GPLv3
|
|
|
|
|
|
|
|
# Importe
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
import csv
|
|
|
|
|
|
|
|
# Schuljahr
|
2024-09-07 14:35:42 +02:00
|
|
|
schuljahr='24'
|
|
|
|
categoryid='2'
|
|
|
|
|
|
|
|
# Gruppenfilter
|
|
|
|
filter_gruppen_entfernen = ['11','12','13','14','15','16','17','18','19','20','21','22','23']
|
|
|
|
|
|
|
|
gruppen_vorgeben = True
|
|
|
|
filter_gruppen_ausschliesslich = [
|
|
|
|
'Klasse_08C',
|
|
|
|
'Klasse_08C_PK',
|
|
|
|
'Kurs_10_IFR2',
|
|
|
|
'Kurs_09_IFR2',
|
|
|
|
'Kurs_EF_IF_G1',
|
|
|
|
'Kurs_EF_IF_G2',
|
|
|
|
'Kurs_Q2_IF_G1',
|
|
|
|
'Kurs_Q2_IF_G2',
|
|
|
|
'Klasse_09A_E5',
|
|
|
|
'Klasse_09B_E5',
|
|
|
|
'Klasse_09C_E5',
|
|
|
|
'Kurs_07_ER1',
|
|
|
|
'Kurs_09_ER1',
|
|
|
|
'Kurs_10_ER1',
|
|
|
|
'Kurs_Q1_ER_G1',
|
|
|
|
'Klasse_06A_IF'
|
|
|
|
]
|
2023-01-22 19:32:03 +01:00
|
|
|
|
|
|
|
# Ergebnisspeicher
|
|
|
|
members = []
|
2023-01-22 20:20:31 +01:00
|
|
|
maxkurse = 0
|
|
|
|
allekurse = []
|
2023-01-22 19:32:03 +01:00
|
|
|
|
|
|
|
# Kollegium einlesen
|
|
|
|
with open('Export_Kollegium.csv', 'r', encoding='utf-8-sig') as f:
|
|
|
|
# CSV einlesen
|
2024-09-07 14:35:42 +02:00
|
|
|
cr = csv.DictReader(f, delimiter=',', lineterminator='\n')
|
2023-01-22 19:32:03 +01:00
|
|
|
|
|
|
|
for row in cr:
|
|
|
|
# Person erstellen
|
|
|
|
p = {
|
|
|
|
'firstname' : row['Vorname'],
|
|
|
|
'lastname' : row['Nachname'],
|
|
|
|
'username' : row['Account'],
|
|
|
|
'email' : row['E-Mail-Adresse'],
|
|
|
|
'profile_field_Klasse' : 'Kollegium',
|
|
|
|
'profile_field_Lehrer_in' : '1',
|
|
|
|
'idnumber' : row['Klasse/Information'], #row['Import-ID']
|
|
|
|
'password': 'oauth2'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Gruppen der Person laden
|
|
|
|
gt = row['Alle Gruppen'].split(',')
|
|
|
|
|
|
|
|
# Gruppen filtern
|
2024-09-07 14:35:42 +02:00
|
|
|
if gruppen_vorgeben:
|
|
|
|
gruppen = list(filter(lambda g: g in filter_gruppen_ausschliesslich, gt))
|
|
|
|
else:
|
|
|
|
gruppen = list(filter(lambda g: g.startswith('Kurs_') or g.startswith('Klasse_') and not any(fg in g for fg in filter_gruppen_entfernen), gt))
|
2023-01-22 19:32:03 +01:00
|
|
|
|
2023-01-22 20:20:31 +01:00
|
|
|
# Gruppen ins Format von Moodle bringen und Gruppen sammeln
|
2023-01-22 19:32:03 +01:00
|
|
|
for i, g in enumerate(gruppen):
|
|
|
|
p['course' + str(i+1)] = schuljahr + '_' + g
|
|
|
|
p['role' + str(i+1)] = 'editingteacher'
|
|
|
|
|
2023-01-22 20:20:31 +01:00
|
|
|
# Kurse sammeln
|
|
|
|
if g not in allekurse:
|
|
|
|
allekurse.append(g)
|
|
|
|
|
|
|
|
# Anzahl der maximalen Gruppen pro Person merken
|
|
|
|
maxkurse = max(maxkurse, len(gruppen))
|
|
|
|
|
2023-01-22 19:32:03 +01:00
|
|
|
# Person zur Liste hinzfügen
|
|
|
|
members.append(p)
|
|
|
|
|
|
|
|
# SuS einlesen
|
|
|
|
with open('Export_SuS.csv', 'r', encoding='utf-8-sig') as f:
|
|
|
|
# CSV einlesen
|
2024-09-07 14:35:42 +02:00
|
|
|
cr = csv.DictReader(f, delimiter=',', lineterminator='\n')
|
2023-01-22 19:32:03 +01:00
|
|
|
|
|
|
|
for row in cr:
|
|
|
|
# Person erstellen
|
|
|
|
p = {
|
|
|
|
'firstname' : row['Vorname'],
|
|
|
|
'lastname' : row['Nachname'],
|
|
|
|
'username' : row['Account'],
|
|
|
|
'email' : row['E-Mail-Adresse'],
|
|
|
|
'profile_field_Klasse' : row['Klasse/Information'],
|
|
|
|
'profile_field_Lehrer_in' : '0',
|
|
|
|
'idnumber' : row['Import-ID'],
|
|
|
|
'password': 'oauth2'
|
|
|
|
}
|
|
|
|
|
|
|
|
# Gruppen der Person laden
|
|
|
|
gt = row['Alle Gruppen'].split(',')
|
|
|
|
|
|
|
|
# Gruppen filtern
|
2024-09-07 14:35:42 +02:00
|
|
|
if gruppen_vorgeben:
|
|
|
|
gruppen = list(filter(lambda g: g in filter_gruppen_ausschliesslich, gt))
|
|
|
|
else:
|
|
|
|
gruppen = list(filter(lambda g: g.startswith('Kurs_') or g.startswith('Klasse_') and not any(fg in g for fg in filter_gruppen_entfernen), gt))
|
2023-01-22 19:32:03 +01:00
|
|
|
|
2023-01-22 20:20:31 +01:00
|
|
|
# Gruppen ins Format von Moodle bringen und Gruppen sammeln
|
2023-01-22 19:32:03 +01:00
|
|
|
for i, g in enumerate(gruppen):
|
|
|
|
p['course' + str(i+1)] = schuljahr + '_' + g
|
|
|
|
p['role' + str(i+1)] = 'student'
|
|
|
|
|
2023-01-22 20:20:31 +01:00
|
|
|
# Kurse sammeln
|
|
|
|
if g not in allekurse:
|
|
|
|
allekurse.append(g)
|
|
|
|
|
|
|
|
maxkurse = max(maxkurse, len(gruppen))
|
|
|
|
|
2023-01-22 19:32:03 +01:00
|
|
|
# Person zur Liste hinzfügen
|
|
|
|
members.append(p)
|
|
|
|
|
2023-01-22 20:20:31 +01:00
|
|
|
# Moodle-Importdatei für User schreiben
|
|
|
|
with open('Import_Users.csv', 'w', encoding='utf-8-sig') as f:
|
2023-01-22 19:32:03 +01:00
|
|
|
# CSV DictWriter anlegen
|
|
|
|
fieldnames = [
|
|
|
|
'username',
|
|
|
|
'firstname',
|
|
|
|
'lastname',
|
|
|
|
'email',
|
|
|
|
'profile_field_Klasse',
|
|
|
|
'profile_field_Lehrer_in',
|
|
|
|
'idnumber',
|
|
|
|
'password']
|
|
|
|
|
2023-01-22 20:20:31 +01:00
|
|
|
for i in range(1,maxkurse+1):
|
2023-01-22 19:32:03 +01:00
|
|
|
fieldnames.append("course" + str(i))
|
|
|
|
fieldnames.append("role" + str(i))
|
|
|
|
|
|
|
|
cw = csv.DictWriter(f, delimiter=';', fieldnames=fieldnames)
|
|
|
|
|
|
|
|
# Datei schreiben
|
|
|
|
cw.writeheader()
|
|
|
|
for p in members:
|
|
|
|
cw.writerow(p)
|
2023-01-22 20:20:31 +01:00
|
|
|
|
|
|
|
# Moodle-Importdatei für Kurse schreiben
|
|
|
|
with open('Import_Courses.csv', 'w', encoding='utf-8-sig') as f:
|
|
|
|
# CSV DictWriter anlegen
|
|
|
|
fieldnames = [
|
|
|
|
'shortname',
|
|
|
|
'fullname',
|
|
|
|
'category',
|
|
|
|
'visible']
|
|
|
|
|
|
|
|
cw = csv.DictWriter(f, delimiter=';', fieldnames=fieldnames)
|
|
|
|
|
|
|
|
# Datei schreiben
|
|
|
|
cw.writeheader()
|
|
|
|
for c in allekurse:
|
|
|
|
cw.writerow({
|
|
|
|
'shortname' : schuljahr + '_' + c,
|
|
|
|
'fullname' : schuljahr + '_' + c,
|
|
|
|
'category' : categoryid,
|
|
|
|
'visible' : '1'
|
|
|
|
})
|