/**
 * Function called when the user is marking a unit as complete. 
 */
function WPCW_AJAX_units_handleUserProgress()
{
    // Security check
    if (!wp_verify_nonce(WPCW_arrays_getValue($_POST, 'progress_nonce'), 'wpcw-progress-nonce')) {
        die(__('Security check failed!', 'wp_courseware'));
    }
    $unitID = WPCW_arrays_getValue($_POST, 'id');
    // Validate the course ID
    if (!preg_match('/unit_complete_(\\d+)/', $unitID, $matches)) {
        echo WPCW_UnitFrontend::message_error_getCompletionBox_error();
        die;
    }
    $unitID = $matches[1];
    // Get the post object for this quiz item.
    $post = get_post($unitID);
    if (!$post) {
        echo WPCW_UnitFrontend::message_createMessage_error(__('Error - could not save your progress.', 'wp_courseware') . ' ' . __('Could not find training unit.', 'wp_courseware'));
        die;
    }
    // Initalise the unit details
    $fe = new WPCW_UnitFrontend($post);
    // #### Get associated data for this unit. No course/module data, then it's not a unit
    if (!$fe->check_unit_doesUnitHaveParentData()) {
        echo WPCW_UnitFrontend::message_createMessage_error(__('Error - could not save your progress.', 'wp_courseware') . ' ' . __('Could not find course details for unit.', 'wp_courseware'));
        die;
    }
    // #### User not allowed access to content
    if (!$fe->check_user_canUserAccessCourse()) {
        echo $fe->message_user_cannotAccessCourse();
        die;
    }
    WPCW_units_saveUserProgress_Complete($fe->fetch_getUserID(), $fe->fetch_getUnitID(), 'complete');
    // Unit complete, check if course/module is complete too.
    do_action('wpcw_user_completed_unit', $fe->fetch_getUserID(), $fe->fetch_getUnitID(), $fe->fetch_getUnitParentData());
    // Only complete if allowed to continue.
    echo $fe->render_completionBox_complete();
    die;
}