Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -


From Juneday education
Jump to: navigation, search

How to get a list of students per group

First, download a JSON with all groups per "group_category_id". In our example, we wanted the groups from TIG015, group category 1637 ("Projektgrupper"). The URL to the JSON is .

Next, download a JSON with all the students, using this URL:[]=enrollments&include[]=group_ids&per_page=200 .

Save the files as users.json and groups.json and edit them and remove "while(1);" from the start of the files.

Next, create two text files from the JSON files, groups.txt and studs.txt :

$ cat groups.json | jq -j '.[]|.name, " ", .id, "\n"' > groups.txt

$ cat users.json | jq -j '.[]|.login_id, " ",.name," ", (.group_ids[0]), "\n"' | rev | sort -nk1|rev > studs.txt

Next, loop over each student line in studs.txt and append the group name (looked up from groups.txt):

cat studs.txt | while read stud;do gid=$(echo "$stud"|tr ' ' '\n'|grep [0-9]); gname=`grep "$gid" groups.txt | cut -d ' ' -f1`; echo "$stud $gname";done | grep gus | grep -v null | rev | sort | rev > studs_and_group_names.txt

The result will look like this:

gusxxx Daniel XXX 5539 A
gusxxy Rebecka XXX 5539 A
gusxxz Teodor XXX 5539 A
gusxxa Thor XXX 5539 A
gusxxb Evelina XXX 5539 B
gusxxc Samuel XXX 6994 B

We protected the gus-id and last name here, but the above means that Daniel, Rebecka, Teodor and Thor are in group A, Evelina and Samuel are in group "B" etc.

We thank the creators of Canvas for making this such a simple task.

Converting to required format

If we want to use the students and groups above in our script to create users and groups on a Unix computer we need to convert the content of the file a bit. Here's how to do it.



if [ "$FILE" = "" ] || [ ! -f $FILE ]
    echo "Missing argument or non existing file (\"$FILE\")"

# Associative array for mapping student (value) to group (key)
declare -A GROUP_STUDS

# Read file, put students in map (assoc array in bash)
while read LINE
    # Find GID (last word)
    GID=$(echo $LINE | grep -oE '[^ ]+$')
    # Find gusid (first word)
    GUSID=$(echo $LINE | awk '{print $1}')
    # debug printout
    #    echo "$GID $GUSID"
    if [ "${GROUP_STUDS[$GID]}" = "" ]
done < $FILE  
# debug, print all keys
#echo "Group ids: ${!GROUP_STUDS[@]}"

# format according to script
for key in "${!GROUP_STUDS[@]}"
  echo "$key:${GROUP_STUDS[$key]}"