// generate full and selected course lists
$availablecourses = array();
foreach ($SESSION->bulk_users as $user) {
    $usercourses = get_my_courses($user);
    foreach ($usercourses as $key => $junk) {
        if (!in_array($key, $availablecourses)) {
            $availablecourses[] = $key;
        }
    }
}
$unaccessible = array_diff($SESSION->bulk_courses, $availablecourses);
if (!empty($unaccessible)) {
    $SESSION->bulk_courses = array();
}
$coursenames = gen_course_list($searchtext, array_diff($availablecourses, $SESSION->bulk_courses));
$selcoursenames = gen_course_list('', array_intersect($availablecourses, $SESSION->bulk_courses));
// print the page
if ($accept) {
    if (empty($SESSION->bulk_courses)) {
        redirect($return);
        die;
    }
    admin_externalpage_print_header();
    notice_yesno(get_string('confirmation', $pluginname, NULL, $langdir), 'iterator.php', $return, array('confirm' => '1'), NULL, 'post', 'get');
    admin_externalpage_print_footer();
    die;
}
admin_externalpage_print_header();
if (isset($SESSION->purge_progress)) {
    notice_yesno(get_string('continue', $pluginname, NULL, $langdir), 'iterator.php', 'index.php', NULL, array('reset' => '1'), 'get', 'post');
} else {
            $coursecnt++;
        }
        // if no courses pass the filter in that category, delete the current string
        if ($coursecnt == 0) {
            unset($courselist[$catcnt]);
        } else {
            $courselist[$catcnt] .= '</optgroup>';
            $catcnt++;
        }
    }
    // return the html code with categorized courses
    return implode(' ', $courselist);
}
// generate full and selected course lists
$coursenames = gen_course_list($searchtext, $SESSION->bulk_courses, true);
$selcoursenames = gen_course_list('', $SESSION->bulk_courses);
// generate the list of groups names from the selected courses.
// groups with the same name appear only once
$groupnames = array();
foreach ($SESSION->bulk_courses as $course) {
    $cgroups = groups_get_all_groups($course);
    foreach ($cgroups as $cgroup) {
        if (!in_array($cgroup->name, $groupnames)) {
            $groupnames[] = $cgroup->name;
        }
    }
}
sort($groupnames);
// generate html code for the group select control
foreach ($groupnames as $key => $name) {
    $groupnames[$key] = '<option value="' . s($name, true) . '" >' . s($name, true) . '</option>';