-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_cohorts.py
executable file
·89 lines (67 loc) · 2.46 KB
/
add_cohorts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/env python
"""
Takes a file preprocessed by preprocess_teachers_and_courses.py
Uses the Moodle API to create all the cohorts in the file.
Parameter course_category_id is the id number of the yearly category.
It can be found here:
https://moodle.gymnasedebeaulieu.ch/course/management.php
This script replaces an older script that generated a file that could be imported with
Administration du site -> Utilisateur -> Cohortes -> Déposer les cohortes.
That technique prevented us from synchronizing cohorts more than once, becuse that
admin page choked as soon as a cohort in the file already existed in Moodle.
"""
import argparse
import os
import sys
import dotenv
import pandas as pd
import structlog
from lib.io import read_csv
from lib.moodle_api import URL, MoodleClient
from preprocess_teachers_and_courses import COURSE_COHORT
log = structlog.get_logger()
def add_cohorts(moodle: MoodleClient, course_category_id: str, src: pd.DataFrame):
wanted = set(src[COURSE_COHORT].dropna())
log.info("wanted cohorts", count=len(wanted))
result = moodle(
"core_cohort_search_cohorts",
query="",
context={"contextlevel": "coursecat", "instanceid": course_category_id},
includes="self",
limitfrom=0,
limitnum=10000,
)
existing = {c.name for c in result.cohorts}
log.info(
"fetched cohorts from moodle",
course_category_id=course_category_id,
count=len(existing),
)
missing = wanted - existing
if not missing:
log.info("Nothing to do")
sys.exit(0)
log.info("Missing cohorts", missing=missing)
user_input = input(f"Do you want to create {len(missing)} cohorts (yes/no): ")
if user_input.lower() != "yes":
print("Aborting")
sys.exit(0)
data = [
dict(categorytype=dict(type="id", value=course_category_id), name=c, idnumber=c)
for c in missing
]
moodle("core_cohort_create_cohorts", cohorts=data)
log.info("Done")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("course_category_id")
parser.add_argument("preprocessed")
args = parser.parse_args()
dotenv.load_dotenv()
token = os.getenv("TOKEN")
if not token:
sys.exit("Missing environment variable 'TOKEN'")
log.info("connecting", url=URL)
moodle = MoodleClient(URL, token)
preprocessed = read_csv(args.preprocessed)
add_cohorts(moodle, args.course_category_id, preprocessed)