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



Meta:Canvas

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 https://canvas.gu.se/api/v1/group_categories/1637/groups?per_page=100 .

Next, download a JSON with all the students, using this URL:

https://canvas.gu.se/api/v1/courses/27048/users?include[]=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.

#!/bin/bash

FILE=$1

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

# 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
do
    # 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]}" = "" ]
    then
	GROUP_STUDS[$GID]="$GUSID"
    else
	GROUP_STUDS[$GID]="${GROUP_STUDS[$GID]},$GUSID"
    fi
done < $FILE  
	      
# debug, print all keys
#echo "Group ids: ${!GROUP_STUDS[@]}"

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