예제 #1
0
 /**
  * In single-page quiz mode, check the answers that have been provided and update the status in the 
  * database.
  * 
  * @param Array $potentialAnswers The potential answers that need checking. 
  * 
  * @return Boolean Returns true when we have all of our answers, and we're allowed to carry on for grading.
  */
 function check_quizzes_canWeContinue_checkAnswersFromOnePageQuiz($potentialAnswers)
 {
     $resultsList = array('answer_list' => array(), 'wrong_answer_list' => array(), 'error_answer_list' => array());
     // ### 1 - Extract a list of actual answers from the potential answers.
     $resultsList = $this->check_quizzes_canWeContinue_extractAnswerData($potentialAnswers);
     // ### 2 - Save the progress so far. This updates the internal progress variable too. No grading goes on
     // but it stores the answers to be graded in the next bit of code.
     $this->unchecked_QuizAnswersToGrade = $resultsList;
     // ### 3 - Has the timer expired? If so, then do no further checking and return true too allow the code
     // to continue
     if ('expired' == WPCW_arrays_getValue($potentialAnswers, 'timerexpired')) {
         return true;
     }
     $answerCountSoFar = count($resultsList['answer_list']);
     // ### 4 - See if we expect uploads, because that may explain the difference in answers v.s. expected answer counts if we're
     // paging and now on the final review page. Uploads are a special case, and we add any missing ones back from what's been saved.
     if ($this->unitQuizDetails->want_uploads) {
         foreach ($this->unitQuizDetails->questions as $questionID => $questionDetails) {
             // Got an upload question where the answer is not in the final count
             if ('upload' == $questionDetails->question_type && !isset($resultsList['answer_list'][$questionID]) && isset($this->unitQuizProgress->quiz_data[$questionID])) {
                 // Update internal variable of data we have, as we'll need this for saving
                 $this->unchecked_QuizAnswersToGrade['answer_list'][$questionID] = $this->unitQuizProgress->quiz_data[$questionID]['their_answer_raw'];
                 $answerCountSoFar++;
             }
         }
     }
     // ### 5 - Check that we have enough answers given how many questions there are.
     // If there are not enough answers, then re-render the form with the answered questions
     // marked, and highlight the fields that have errors.
     if ($this->unitQuizDetails->questions && $answerCountSoFar < count($this->unitQuizDetails->questions)) {
         // Error - not all questions are answered
         echo WPCW_UnitFrontend::message_createMessage_error(__('Please provide an answer for all of the questions on this page.', 'wp_courseware'));
         // This will trigger the form to show again with missing answers, using the ansers the user provided
         // through what has been stored in $this->unchecked_QuizAnswersToGrade
         // User may not continue - as quiz is not complete.
         return false;
     }
     return true;
 }
/**
 * Function called when user starting a quiz and needs to kick off the timer.
 */
function WPCW_AJAX_units_handleQuizTimerBegin()
{
    // Security check
    if (!wp_verify_nonce(WPCW_arrays_getValue($_POST, 'progress_nonce'), 'wpcw-progress-nonce')) {
        die(__('Security check failed!', 'wp_courseware'));
    }
    // Get unit and quiz ID
    $unitID = intval(WPCW_arrays_getValue($_POST, 'unitid'));
    $quizID = intval(WPCW_arrays_getValue($_POST, 'quizid'));
    // Get the post object for this quiz item.
    $post = get_post($unitID);
    if (!$post) {
        echo WPCW_UnitFrontend::message_createMessage_error(__('Error - could not start the timer for the quiz.', '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 start the timer for the quiz.', '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;
    }
    // #### See if we're in a position to retake this quiz?
    if (!$fe->check_quizzes_canUserRequestRetake()) {
        echo WPCW_UnitFrontend::message_createMessage_error(__('Error - could not start the timer for the quiz.', 'wp_courseware') . ' ' . __('You are not permitted to retake this quiz.', 'wp_courseware'));
        die;
    }
    // Trigger the upgrade to progress so that we can start the quiz, and trigger the timer.
    $fe->update_quizzes_beginQuiz();
    // Only complete if allowed to continue.
    echo $fe->render_detailsForUnit(false, true);
    die;
}