IServ2LMS/convert.py
2023-01-22 19:32:03 +01:00

107 lines
3.1 KiB
Python
Executable file

#!/usr/bin/env python3
# Konvertiere IServ-Gruppenlisten in Moodle-Importdateien
# 2022 by Daniel Spittank, daniel.spittank@gymsedan.de
# Lizenz: GPLv3
# Importe
from pprint import pprint
import csv
# Schuljahr
schuljahr='22'
# Ergebnisspeicher
members = []
maxgroups = 0
# Kollegium einlesen
with open('Export_Kollegium.csv', 'r', encoding='utf-8-sig') as f:
# CSV einlesen
cr = csv.DictReader(f, delimiter=';', lineterminator='\n')
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
gruppen = list(filter(lambda g: g.startswith('Kurs') or g.startswith('Klasse'), gt))
# Gruppen ins Format von Moodle bringen
for i, g in enumerate(gruppen):
p['course' + str(i+1)] = schuljahr + '_' + g
p['role' + str(i+1)] = 'editingteacher'
maxgroups = max(maxgroups, len(gruppen))
# Person zur Liste hinzfügen
members.append(p)
# SuS einlesen
with open('Export_SuS.csv', 'r', encoding='utf-8-sig') as f:
# CSV einlesen
cr = csv.DictReader(f, delimiter=';', lineterminator='\n')
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
gruppen = list(filter(lambda g: g.startswith('Kurs') or g.startswith('Klasse'), gt))
# Gruppen ins Format von Moodle bringen
for i, g in enumerate(gruppen):
p['course' + str(i+1)] = schuljahr + '_' + g
p['role' + str(i+1)] = 'student'
maxgroups = max(maxgroups, len(gruppen))
# Person zur Liste hinzfügen
members.append(p)
# Moodle-Importdatei schreiben
with open('import.csv', 'w', encoding='utf-8-sig') as f:
# CSV DictWriter anlegen
fieldnames = [
'username',
'firstname',
'lastname',
'email',
'profile_field_Klasse',
'profile_field_Lehrer_in',
'idnumber',
'password']
for i in range(1,maxgroups+1):
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)