Moodle List Getter

This function will find a table of enrolled users in Moodle, and weed out non-critical information (everything except name and email) and all non-student participants.

by mlms13

JavaScript

function pruneTable() {
    var r = document.getElementById('region-main'),
        t = r.getElementsByTagName('table')[0],
        tb = t.getElementsByTagName('tbody')[0],
        i,
        j;

    // remove the table header
    t.removeChild(t.getElementsByTagName('thead')[0]);

    //loop through all table rows
    for (i = tb.children.length - 1; i > -1; i--) {
        // if the row contains a student
        if (tb.children[i].children[2].firstChild.firstChild.textContent.toLowerCase() === 'student') {
            // delete columns 2+
            for (j = tb.children[i].children.length - 1; j > 0; j--) {
                tb.children[i].removeChild(tb.children[i].children[j]);
            }
        } else {
            // otherwise, if the row is a teacher, delete everything
            window.console && console.log('Removing non-student participant: ' +
                tb.children[i].children[0].children[1].textContent
            );
            tb.removeChild(tb.children[i]);
        }
    }
}