Ejemplo n.º 1
0
/**
 * Handle showing the box that allows a user to mark a unit as completed.
 */
function WPCW_units_processUnitContent($content)
{
    // #### Ensure we're only showing a course unit, a single item
    if (!is_single() || 'course_unit' != get_post_type()) {
        return $content;
    }
    global $post;
    // #### Get associated data for this unit. No course/module data, not a unit
    $parentData = WPCW_units_getAssociatedParentData($post->ID);
    if (!$parentData) {
        return $content;
    }
    // #### Ensure we're logged in
    $user_id = get_current_user_id();
    if ($user_id == 0) {
        // Return no access error without content
        return WPCW_units_createErrorMessage($parentData->course_message_unit_not_logged_in);
    }
    $userProgress = new UserProgress($parentData->course_id, $user_id);
    // #### User not allowed access to content, so certainly can't say they've done this unit.
    if (!$userProgress->canUserAccessCourse()) {
        // Return no access error without content
        return WPCW_units_createErrorMessage($parentData->course_message_unit_no_access);
    }
    // #### Is user allowed to access this unit yet?
    if (!$userProgress->canUserAccessUnit($post->ID)) {
        // Return no access error without content
        return WPCW_units_createErrorMessage($parentData->course_message_unit_not_yet);
    }
    // #### Determine if this unit has been completed or not.
    $progressDetails = WPCW_units_getUserUnitDetails($user_id, $post->ID);
    $completed = $progressDetails && $progressDetails->unit_completed_status == 'complete';
    // #### Show completion box
    if ($completed) {
        $completionBox = WPCW_units_getCompletionBox_complete($parentData, $post->ID, $user_id);
    } else {
        $completionBox = WPCW_units_getCompletionBox_pending($parentData, $post->ID);
    }
    // #### Show the navigation box (disable the next item if not completed yet).
    $navigationBox = WPCW_units_getNavigationBox($parentData, $post->ID, $userProgress, !$completed);
    return $content . $completionBox . $navigationBox;
}
Ejemplo n.º 2
0
/**
 * Function called when a user is submitting quiz answers via
 * the frontend form. 
 */
function WPCW_AJAX_units_handleQuizResponse()
{
    // Security check
    if (!wp_verify_nonce(WPCW_arrays_getValue($_POST, 'progress_nonce'), 'wpcw-progress-nonce')) {
        die(__('Security check failed!', 'wp_courseware'));
    }
    // Quiz ID and Unit ID are combined in the single CSS ID for validation.
    // So validate both are correct and that user is allowed to access quiz.
    $quizAndUnitID = WPCW_arrays_getValue($_POST, 'id');
    // e.g. quiz_complete_69_1 or quiz_complete_17_2 (first ID is unit, 2nd ID is quiz)
    if (!preg_match('/quiz_complete_(\\d+)_(\\d+)/', $quizAndUnitID, $matches)) {
        echo WPCW_units_getCompletionBox_error();
        die;
    }
    // Use the extracted data for further validation
    $unitID = $matches[1];
    $quizID = $matches[2];
    $user_id = get_current_user_id();
    // #### Get associated data for this unit. No course/module data, not a unit
    $parentData = WPCW_units_getAssociatedParentData($unitID);
    if (!$parentData) {
        // No error, as not a valid unit.
        die;
    }
    // #### User not allowed access to content, so certainly can't say they've done this unit.
    if (!WPCW_courses_canUserAccessCourse($parentData->course_id, $user_id)) {
        // No error, as not a valid unit.
        die;
    }
    // #### Check that the quiz is valid and belongs to this unit
    $quizDetails = WPCW_quizzes_getQuizDetails($quizID, true);
    if (!($quizDetails && $quizDetails->parent_unit_id == $unitID)) {
        die;
    }
    // Validate the quiz answers... which means we might have to
    // send back the form to be re-filled.
    $canContinue = WPCW_quizzes_handleQuizRendering_canUserContinueAfterQuiz($quizDetails, $_POST, $user_id);
    // Check that user is allowed to progress.
    if ($canContinue) {
        WPCW_units_saveUserProgress_Complete($user_id, $unitID, 'complete');
        // Unit complete, check if course/module is complete too.
        do_action('wpcw_user_completed_unit', $user_id, $unitID, $parentData);
        // Only complete if allowed to continue.
        echo WPCW_units_getCompletionBox_complete($parentData, $unitID, $user_id);
    }
    die;
}