unset($users[$loc->userid]);
        }
    }
}
// if all users have been removed or no users in course
if (count($users) == 0) {
    print_box(get_string("nolocationsupdated", "map"));
} else {
    //for the user left(don't already have a location)
    foreach ($users as $user) {
        require_once $CFG->dirroot . '/user/profile/lib.php';
        profile_load_data($user);
        $context = get_context_instance(CONTEXT_COURSE, $COURSE->id);
        // only display students on the map (nadavkav patch)
        if (!has_capability('moodle/legacy:admin', $context)) {
            $user->userid = $user->id;
            $user->state = map_get_user_state($user);
            $user->action = "insertlocation";
            $user->mapid = $map->id;
            $user->id = $id;
            if ($resultLocation = map_save_location($user) === true) {
                //was able to set users location
                print_box("User: {$user->firstname} {$user->lastname} - " . get_string("locationset", "map"));
            } else {
                //was not able to set user location
                print_box("User: {$user->firstname} {$user->lastname} - " . get_string("errorsetlocation", "map") . " - " . $resultLocation);
            }
        }
    }
}
print_single_button("view.php?id=" . $id, null, "View Map", "post");
    print_box(print_single_button("update_locations.php?id=" . $id, null, get_string("updateuserlocations", "map"), "post", "_self", true), "button");
}
//only allow members to add student locations or extra locations
if ($memberOfGroup) {
    //should this map show student locations
    if ($map->studentlocations == 1 && has_capability('mod/map:setownlocation', $context)) {
        $locForm = new mod_map_user_location_form();
        if (!isset($user_map_location)) {
            //no user location has been set for current user
            //fill form with profile location
            require_once $CFG->dirroot . '/user/profile/lib.php';
            profile_load_data($USER);
            print_box(format_text(get_string("usernolocation", "map"), FORMAT_PLAIN), 'generalbox', 'intro');
            $curLocation = new object();
            $curLocation->city = $USER->city;
            $curLocation->state = map_get_user_state($USER);
            $curLocation->country = $USER->country;
            $curLocation->id = $cm->id;
            $curLocation->action = "insertlocation";
            $curLocation->userid = $USER->id;
            $locForm->set_data($curLocation);
        } else {
            if ($user_map_location->showcode == 0) {
                //user has explicitly said they don't want to show up on the map
                $locForm = new mod_map_reset_location_form();
            }
            //user has set location
            $curLocation = clone $user_map_location;
            $curLocation->id = $cm->id;
            $curLocation->locationid = $user_map_location->id;
            $locForm->set_data($curLocation);