/** * Handles saving what the admin wants to do for the user next. */ function WPCW_showPage_UserProgess_quizAnswers_whatsNext_savePreferences($quizDetails, $results, $page, $userID, $unitID) { // Admin wants to save the next action to this progress. if (isset($_POST['failed_quiz_next_action']) && $_POST['failed_quiz_next_action']) { global $wpdb, $wpcwdb; $wpdb->show_errors(); $userNextAction = WPCW_arrays_getValue($_POST, 'wpcw_user_continue_action'); $userRetakeMsg = filter_var(WPCW_arrays_getValue($_POST, 'wpcw_user_progress_failed_reason'), FILTER_SANITIZE_STRING); // Check action is valid. Abort if not if (!in_array($userNextAction, array('retake_quiz', 'progress_anyway'))) { return $results; } // ****** REDUNDANT SWITCH ******* // Sort out the SQL statement for what to update // switch ($userNextAction) // { // // User needs to retake the course. // case 'retake_quiz': // break; // // User is allowed to progress // case 'progress_anyway': // $userRetakeMsg = false; // // Mark the unit as completed. // WPCW_units_saveUserProgress_Complete($userID, $unitID); // // Unit complete, check if course/module is complete too. // do_action('wpcw_user_completed_unit', $userID, $unitID, WPCW_units_getAssociatedParentData($unitID)); // break; // } // Update the progress item $SQL = $wpdb->prepare("\n\t\t \tUPDATE {$wpcwdb->user_progress_quiz}\n\t\t \t SET quiz_next_step_type = '%s', \n\t\t \t quiz_next_step_msg = %s \n\t\t \tWHERE user_id = %d \n\t\t \t AND unit_id = %d \n\t\t \t AND quiz_id = %d\n\t\t \tORDER BY quiz_attempt_id DESC\n\t\t \tLIMIT 1\n\t \t\t", $userNextAction, $userRetakeMsg, $userID, $unitID, $quizDetails->quiz_id); $wpdb->query($SQL); // Need to update the results object for use later. $results->quiz_next_step_type = $userNextAction; $results->quiz_next_step_msg = $userRetakeMsg; switch ($userNextAction) { // User needs to retake the course. case 'retake_quiz': $results->extraEmailDetail = __('Since you didn\'t pass the quiz, the instructor has asked that you re-take this quiz.', 'wp_courseware'); if ($userRetakeMsg) { $results->extraEmailDetail .= "\n\n" . $userRetakeMsg; } break; // User is allowed to progress // User is allowed to progress case 'progress_anyway': $results->extraEmailDetail = __('Although you didn\'t pass the quiz, the instructor is allowing you to continue.', 'wp_courseware'); // Mark the unit as completed. WPCW_units_saveUserProgress_Complete($userID, $unitID); // Unit complete, check if course/module is complete too. do_action('wpcw_user_completed_unit', $userID, $unitID, WPCW_units_getAssociatedParentData($unitID)); break; } // Tell code to send out emails $results->sendOutEmails = true; } $results->quiz_has_just_been_graded = true; return $results; }
/** * 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; }
/** * 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_UnitFrontend::message_error_getCompletionBox_error(); die; } // Use the extracted data for further validation $unitID = $matches[1]; $quizID = $matches[2]; // 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 quiz results.', '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 quiz results.', '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; } // #### Check that the quiz is valid and belongs to this unit if (!$fe->check_quizzes_isQuizValidForUnit($quizID)) { echo WPCW_UnitFrontend::message_createMessage_error(__('Error - could not save your quiz results.', 'wp_courseware') . ' ' . __('Quiz data does not match quiz for this unit.', 'wp_courseware')); die; } $canContinue = false; // #### Do we have all the answers that we need so that we can grade the quiz? // #### Answer Check Variation A - Paging if ($fe->check_paging_areWePagingQuestions()) { // If this is false, then we keep checking for more answers. $readyForMarking = $fe->check_quizzes_canWeContinue_checkAnswersFromPaging($_POST); } else { // If this is false, then the form is represented asking for fixes. $readyForMarking = $fe->check_quizzes_canWeContinue_checkAnswersFromOnePageQuiz($_POST); } // Now checks are done, $this->unitQuizProgress contains the latest questions so that we can mark them. if ($readyForMarking) { $canContinue = $fe->check_quizzes_gradeQuestionsForQuiz(); } // #### Validate the answers that we have, which determines if we can carry on to the next // unit, or if the user needs to do something else. if ($canContinue) { 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()); } // Show the appropriate messages/forms for the user to look at. This is common for all execution // paths. echo $fe->render_detailsForUnit(false, true); die; }