Skip to content

Instantly share code, notes, and snippets.

@Canx
Created August 22, 2018 14:41
Show Gist options
  • Save Canx/b13c0027e46aa195044999bfa28179b5 to your computer and use it in GitHub Desktop.
Save Canx/b13c0027e46aa195044999bfa28179b5 to your computer and use it in GitHub Desktop.
Script to reset courses and unenrol students.
#!/bin/bash
#
# 'reset_courses' is a script to bulk reset all courses and unenrol their students.
# by Ruben Cancho (@canx)
#
# DEPENDENCIES: moosh installed.
# NOTE: for deleting old students is recommended to install "Inactive User Cleanup" plugin
#
############ CONFIG #############
userdb='root' # add your moodle db user
passdb='' # add your moodle db password
moodle='/var/www/moodle'
MOOSH='/usr/local/bin/moosh'
#################################
courseids=`mysql -u $userdb -p$passdb -e "use moodle;select id from mdl_course" | sed '1d'`
cd $moodle
for id in $courseids; do
if [ $id != "1" ]
then
echo "reseting course:" $id
$MOOSH -n course-reset $id
studentids=`mysql -u $userdb -p$passwd -e "use moodle; SELECT usr.id FROM mdl_course c INNER JOIN mdl_context cx ON c.id = cx.instanceid AND cx.contextlevel = '50' INNER JOIN mdl_role_assignments ra ON cx.id = ra.contextid INNER JOIN mdl_role r ON ra.roleid = r.id INNER JOIN mdl_user usr ON ra.userid = usr.id WHERE r.shortname = 'student' AND c.id = $id ORDER BY c.fullname, usr.firstname" | sed '1d'`
if [ ! -z "$studentids" ]
then
echo "unenroling students..."
$MOOSH -n course-unenrol $id $studentids
else
echo "no students found."
fi
fi
done
# force moodle cron execution
php $moodle"/admin/cli/cron.php"
@Sieteliz1
Copy link

Hello, this script works good. But there is one problem. It does not remove cohort from course. Maybe it could be fixed somehow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment