Exemplo n.º 1
0
 /**
  * Renders the specified question to HTML, reporting any errors.
  * 
  * @param Object $question The question to render as HTML.
  * @param Array $resultsList The details of how to process results.
  * @param Boolean $showErrorsOnForm If true, show the errors on the form, acts as an override to disable showing errors if they are not wanted. 
  * @param Integer $questionNum The question number being rendered.
  * @param Boolean $pagingMode Is this question being rendered in paging mode?
  * @param Integer $questionIndex The index of the question within the quiz.
  * 
  * @return String The HTML to render the question as HTML.
  */
 function render_quizzes_handleQuizRendering_singleQuestion($question, $resultsList, $showErrorsOnForm, $questionNum, $pagingMode = false, $questionIndex = 0)
 {
     // See if we want to show an answer
     $selectAnswer = false;
     if (isset($resultsList['answer_list'][$question->question_id])) {
         $selectAnswer = $resultsList['answer_list'][$question->question_id];
     }
     switch ($question->question_type) {
         case 'multi':
             $quObj = new WPCW_quiz_MultipleChoice($question);
             break;
         case 'open':
             $quObj = new WPCW_quiz_OpenEntry($question);
             break;
         case 'upload':
             $quObj = new WPCW_quiz_FileUpload($question);
             break;
         case 'truefalse':
             $quObj = new WPCW_quiz_TrueFalse($question);
             break;
         case 'random_selection':
             die(__('This question cannot be rendered. This is an error.', 'wp_courseware'));
             break;
             // Not expecting anything here... so not handling the error case.
         // Not expecting anything here... so not handling the error case.
         default:
             die(__('Unexpected question type, aborting.', 'wp_courseware'));
             break;
     }
     // Add extra CSS classes - this adds an index to the class to allow us to style certain questions.
     $quObj->cssClasses .= 'wpcw_fe_quiz_q_single_' . $questionIndex;
     // Use the objects to render the questions, showing an answer as wrong if appropriate.
     $showAsError = false;
     // Only worry about errors if actual data has been submitted.
     $errorToShow = false;
     if ($showErrorsOnForm && isset($_POST['submit'])) {
         // Something went wrong
         if (isset($resultsList['error_answer_list'][$question->question_id])) {
             $errorToShow = $resultsList['error_answer_list'][$question->question_id];
             $showAsError = 'error';
         } else {
             if (!isset($resultsList['answer_list'][$question->question_id])) {
                 // Not missing if we're paging!
                 if (!$pagingMode) {
                     $showAsError = 'missing';
                 }
             } else {
                 if (isset($resultsList['wrong_answer_list'][$question->question_id])) {
                     $showAsError = 'wrong';
                 }
             }
         }
     }
     // Use object to render
     return $quObj->renderForm_toString($this->unitQuizDetails, $questionNum, $selectAnswer, $showAsError, $errorToShow);
 }
Exemplo n.º 2
0
/**
 * Shows the quiz for the unit, based on the type being shown.
 * 
 * @param Integer $unitID The ID of the unit that's currently pending.
 * @param Object $quizDetails The details of the quiz to show.
 * @param Array $resultsList The list of checked answers, wrong answers, and error messages for the quiz form.
 * 
 * @return String The HTML that renders the quiz for answering.
 */
function WPCW_quizzes_handleQuizRendering($unitID, $quizDetails, $resultsList = false)
{
    // Hopefully not needed, but just in case.
    if (!$quizDetails) {
        return false;
    }
    // Create an array so that we can use isset($answerList['key']).
    if (!$resultsList) {
        $resultsList = array('answer_list' => array(), 'wrong_answer_list' => array(), 'error_answer_list' => array());
    }
    // Render the wrapper for the quiz using the pending message section
    // Use the Quiz ID and Unit ID to validate permissions.
    $html = sprintf('<div class="wpcw_fe_quiz_box_wrap" id="wpcw_fe_quiz_complete_%d_%d">', $unitID, $quizDetails->quiz_id);
    // enctype="multipart/form-data" for file uploads.
    $html .= sprintf('<form method="post" enctype="multipart/form-data" id="quiz_complete_%d_%d">', $unitID, $quizDetails->quiz_id);
    $html .= '<div class="wpcw_fe_quiz_box wpcw_fe_quiz_box_pending">';
    // #### 1 - Quiz Title - constant for all quizzes
    $html .= sprintf('<div class="wpcw_fe_quiz_title">%s</div>', $quizDetails->quiz_title);
    // #### 2 - Pass mark - just needed for blocking quizes
    if ('quiz_block' == $quizDetails->quiz_type) {
        $totalQs = count($quizDetails->questions);
        $passQs = ceil($quizDetails->quiz_pass_mark / 100 * $totalQs);
        $html .= '<div class="wpcw_fe_quiz_pass_mark">';
        $html .= sprintf(__('You\'ll need to correctly answer at least <b>%d of the %d</b> questions below (<b>at least %d%%</b>) to progress to the next unit.', 'wp_courseware'), $passQs, $totalQs, $quizDetails->quiz_pass_mark);
        $html .= '</div>';
    }
    // Header before questions
    $html .= '<div class="wpcw_fe_quiz_q_hdr"></div>';
    // #### 3 - The actual question form.
    if ($quizDetails->questions && count($quizDetails->questions) > 0) {
        $questionNum = 1;
        foreach ($quizDetails->questions as $question) {
            // See if we want to show an answer
            $selectAnswer = false;
            if (isset($resultsList['answer_list'][$question->question_id])) {
                $selectAnswer = $resultsList['answer_list'][$question->question_id];
            }
            switch ($question->question_type) {
                case 'multi':
                    $quObj = new WPCW_quiz_MultipleChoice($question);
                    break;
                case 'open':
                    $quObj = new WPCW_quiz_OpenEntry($question);
                    break;
                case 'upload':
                    $quObj = new WPCW_quiz_FileUpload($question);
                    break;
                case 'truefalse':
                    $quObj = new WPCW_quiz_TrueFalse($question);
                    break;
                    // Not expecting anything here... so not handling the error case.
                // Not expecting anything here... so not handling the error case.
                default:
                    break;
            }
            // Use the objects to render the questions, showing an answer as wrong if appropriate.
            $showAsError = false;
            // Only worry about errors if actual data has been submitted.
            $errorToShow = false;
            if (isset($_POST['submit'])) {
                // Something went wrong
                if (isset($resultsList['error_answer_list'][$question->question_id])) {
                    $errorToShow = $resultsList['error_answer_list'][$question->question_id];
                    $showAsError = 'error';
                } else {
                    if (!isset($resultsList['answer_list'][$question->question_id])) {
                        $showAsError = 'missing';
                    } else {
                        if (isset($resultsList['wrong_answer_list'][$question->question_id])) {
                            $showAsError = 'wrong';
                        }
                    }
                }
            }
            // Use object to render
            $html .= $quObj->renderForm_toString($quizDetails, $questionNum++, $selectAnswer, $showAsError, $errorToShow);
        }
    }
    // #### 4 - The submit answers button. //<a href="#" class="fe_btn fe_btn_completion btn_completion" id="quiz_complete_%d_%d">%s</a>
    $html .= sprintf('<div class="wpcw_fe_quiz_submit">
	
						<div class="wpcw_fe_upload_progress">
					        <div class="wpcw_fe_upload_progress_bar"></div>
					        <div class="wpcw_fe_upload_progress_percent">0%%</div>
					    </div>
					    
						<div class="wpcw_fe_submit_loader wpcw_loader">
							<img src="%simg/ajax_loader.gif" />
						</div>
						
						<input type="submit" class="fe_btn fe_btn_completion btn_completion" name="submit" value="%s">						
					</div>', WPCW_plugin_getPluginPath(), __('Submit Answers', 'wp_courseware'));
    $html .= '</div>';
    // .wpcw_fe_quiz_box
    $html .= '</form>';
    $html .= '</div>';
    // .wpcw_fe_quiz_box_wrap
    return $html;
}