/**
 * Code that checks if we're resetting the user progress.
 */
function WPCW_users_processUserResetAbility()
{
    // Check bulk by default, otherwise check the single user change.
    $resetTypeCommand = WPCW_arrays_getValue($_GET, 'wpcw_user_progress_reset_point_bulk');
    if (!$resetTypeCommand) {
        $resetTypeCommand = WPCW_arrays_getValue($_GET, 'wpcw_user_progress_reset_point_single');
    }
    // Detect the reset command.
    if ($resetTypeCommand) {
        // Check for a specific module/unit/course to reset. If none found, then refresh.
        if (!preg_match('/^(course|module|unit)_([0-9]+)$/', $resetTypeCommand, $matches)) {
            // No parameter found, reset.
            wp_redirect(add_query_arg('wpcw_reset', false, 'users.php'));
            die;
        }
        $userList = array();
        // Check array of users first (as not triggered for a single update)
        if (isset($_GET['users'])) {
            // Check if we've chosen any users to reset. If not, reset.
            $userList = array_map('intval', (array) $_GET['users']);
        } else {
            if (isset($_GET['wpcw_users_single']) && isset($_GET['wpcw_user_progress_reset_point_single']) && $_GET['wpcw_user_progress_reset_point_single']) {
                // Add a single user ID.
                $userList[] = intval($_GET['wpcw_users_single']);
            }
        }
        // No users at all.
        if (empty($userList)) {
            wp_redirect(add_query_arg('wpcw_reset', false, 'users.php'));
            die;
        }
        // See what we tried to reset to.
        $unitList = false;
        $courseMap = new WPCW_CourseMap();
        switch ($matches[1]) {
            case 'unit':
                $courseMap->loadDetails_byUnitID($matches[2]);
                $unitList = $courseMap->getUnitIDList_afterUnit($matches[2]);
                break;
            case 'module':
                $courseMap->loadDetails_byModuleID($matches[2]);
                $unitList = $courseMap->getUnitIDList_afterModule($matches[2]);
                break;
            case 'course':
                $courseMap->loadDetails_byCourseID($matches[2]);
                $unitList = $courseMap->getUnitIDList_forCourse();
                break;
            default:
                // No parameter found, reset.
                wp_redirect(add_query_arg('wpcw_reset', false, 'users.php'));
                die;
                break;
        }
        // Now do the reset of the progress.
        WPCW_users_resetProgress($userList, $unitList, $courseMap->getCourseDetails(), $courseMap->getUnitCount());
        // Redirect to remove the GET flags from the URL.
        wp_redirect(add_query_arg('wpcw_reset', 'true', 'users.php'));
        die;
    }
}