Sort Moodle Groups
The snippet can be accessed without any authentication.
Authored by
Kai Kientopf
This python script sorts the hand in from the students into their groups.
sortGroups.py 1.52 KiB
import csv
import os
import shutil
import errno
import sys
pre = "./"
if (len(sys.argv)<2):
print("Execute in folder with groups. CSV as comand line parameter.")
sys.exit()
inputcsv = sys.argv[1]
ls = os.listdir(pre)
ls.sort()
order = dict()
members = dict()
with open(inputcsv, newline='') as csvfile:
list = csv.reader(csvfile, delimiter=',', quotechar='"')
firstline = next(list)
for x in ls:
order[x] = []
membersfile = pre + "/" + x + "/" + "members.csv"
with open(membersfile, 'w', newline='\n') as csvfile:
csvfile = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvfile.writerow(firstline)
for row in list:
old = []
new = []
groups = row[3].split(", ")
for group in groups:
if group in ls:
old.append(group)
membersfile = pre + "/" + group + "/" + "members.csv"
with open(membersfile, 'a', newline='\n') as csvfile:
csvfile = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvfile.writerow(row)
else:
new.append(group)
for o in old:
for n in new:
if n not in order[o]:
order[o].append(n)
for x in order:
for y in order[x]:
src = pre + "/" + x
dst = pre + "/" + y
if not os.path.exists(dst):
os.mkdir(dst)
os.symlink("../" + x, dst + "/" + x)
Please register or sign in to comment