Skip to content

Instantly share code, notes, and snippets.

@ostearn
Created January 21, 2018 12:38
Show Gist options
  • Save ostearn/36879facb57adf5e375c7f1b4c5b9520 to your computer and use it in GitHub Desktop.
Save ostearn/36879facb57adf5e375c7f1b4c5b9520 to your computer and use it in GitHub Desktop.
function dotheenrols(){
global $OUTPUT, $USER, $DB;
$all_users = $DB->get_records_sql('SELECT * FROM mdl_user WHERE deleted = 0');
foreach ($all_users as $r_user){
//set all user table variables
if($r_user->id){$user_id = $r_user->id;}
if($user_id >27){
if ($record = $DB->get_record('badge_issued', array('badgeid' => 3, 'userid' => $user_id))) {
//they have the badge
$context = context_course::instance(19);
$context2 = context_course::instance(29);
// What role to enrol as?
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
// Loop through the students.
if (!is_enrolled($context, $user_id)) {
// Not already enrolled so try enrolling them.
if (!enrol_try_internal_enrol(19, $user_id, $studentroleid, time())) {
// There's a problem.
echo"There's a problem.";
//throw new moodle_exception('unabletoenrolerrormessage', 'langsourcefile');
}
}
if (!is_enrolled($context2, $user_id)) {
// Not already enrolled so try enrolling them.
if (!enrol_try_internal_enrol(29, $user_id, $studentroleid, time())) {
// There's a problem.
echo"There's a problem.";
//throw new moodle_exception('unabletoenrolerrormessage', 'langsourcefile');
}
}
}
}
}
echo"done the enrols";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment