SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    reputation consultant ThaVincy's Avatar
    Join Date
    Mar 2003
    Location
    Flanders
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Where do I "print" in this code?

    Hi all,

    We are building a modified Wordpress website for a client who wants a Quiz on his website. We have found a great plugin that works just fine, it asks 15 multiple choice questions and when the user submits the test, it returns a custom score by calculating the results.

    We would like however, that underneath the variable and custom score, a list of correct answers is printed for the visitor to look at. I don't know where to put this code though.

    This is the question/answers file which specifies the questions, answers and score information:

    Code PHP:
    <?php
     
    /*
        Here are the questions, please take a look at all the other options at
        the bottom of this file. You can set virtually anything there. Please
        make sure your numbering starts at 1, and note that wb_exam will
        currently ask the questions 1 to <number of questions>. If a question
        number is missing people will only see a Submit button...
        * "scores" is _always_ an array: answer => score
        * Check your comma's carefully
        * OPEN type questions can have multiple answers with different scoring ;-)
    */
     
    $questions = array(
     
        1 => array(
                "question" => "Als je een cadeau krijgt dat je al hebt dan ...",
                "type" => "RADIO",
                "options" => array(
                    1 => " weiger je het cadeau.",
                    2 => " geef je een compliment voor de goede keuze ervan en vraag je of je het nog kan omruilen.",
                    3 => " zeg je daar niets over en neem je het cadeau aan.<br/>"
                ),
                "scores" => array (
                    1 => 0,
                    2 => 1,
                    3 => 0,
                )
            ),
     
        2 => array(
                "question" => "Bij het beklimmen van een trap gaat ...",
                "type" => "RADIO",
                "options" => array(
                    1 => " de man eerst de trap op, voor de vrouw.",
                    2 => " de vrouw eerst de trap op, voor de man.<br/>"
                ),
                "scores" => array (
                    1 => 1,
                    2 => 0,
                )
            ) /* Please not the ABSENCE of a comma after the last question */
    );
     
    /*
        Rankings have three fields:
        "lower" is the lowest score for which you'll get this text
        "upper" is the highest score for which you'll get this answer
        "text" is the text you'll get. "text" must contain exactly _one_ instance
        %s, which will replaced by your score.
    */
     
     
    $rankings = array (
        1 => array(
            "lower" => 0,
            "upper" => 5,
            "text" => "<strong>Dat ziet er niet zo best uit...</strong><p>U heeft nog niet al te veel begrepen van de hedendaagse omgangsvormen, of bent gewoon de wijze lessen van uw ouders vergeten, u scoorde slechts %s punten op 15.</p>"
        ),
        2 => array(
            "lower" => 6,
            "upper" => 10,
            "text" => "<strong>U kent uw basisetiquette</strong><p>Mooi zo, uw kennis van de hedendaagse omgansvormen is vrij degelijk en u scoorde %s punten op 15.</p>"
        ),
        3 => array(
            "lower" => 11,
            "upper" => 15,
            "text" => "<strong>U bent een echte specialist!</strong>Proficiat, u bent een echte gentleman/lady en scoorde liefst %s punten op 15, wat aantoont dat u een meer dan behoorlijke kennis van de hedendaagse omgangsvormen heeft.</p>"
        )
    );
     
    /*
        The text on the submit button
    */
     
    $submit_button_string = " bereken score ";
     
    /*
        The String at the top of the questions
    */
     
    $intro_string = "<p>Hoe goed ken jij de etiquetteregels? Hoe gedraag jij je tegenover anderen bij een eerste ontmoeting? Weet jij nog wat je moeder je geleerd heeft over tafelmanieren?</p><p>Ontdek het hier met onze unieke etiquettetest. Beantwoord de onderstaande vragen en je krijgt onmiddellijk je score voorgeschoteld.</p>";
     
    /*
        The strings for asking a questions. This string _must_ contain two %s
        The first will replaced by the question number, the second one will be
        replaced by the actual question.
    */
     
    $question_string = "<p><strong>vraag %s:</strong> %s</p>";
     
    ?>

    And this code puts all together and returns the results:

    Code PHP:
    <?php
    /*
        The main function wb_quiz(). This is the one you'll use as a template tag
        in your pagetemplate. You can leave the_loop in your template to display
        the content of your page with _each_and_every_ question of your exam, or
        you can skip it entirely.
    */
     
    function wb_quiz(){
        /*
            Some functions used by the <?php wb_quiz() ?> templatetag.
        */
     
        function wb_quiz_score_question($nummer) {
            global $questions;
            $score = 0;
            if ($questions[$nummer]["type"] == "CHECK") {
                foreach ($questions[$nummer]["options"] as $key => $value) {
                    if (isset($_POST["Q".$nummer."ANSW".$key])) {
                        $score = $score + $questions[$nummer]["scores"][$key];
                    }
                }
            }
            else {
                if (isset($questions[$nummer]["scores"][$_POST["Q".$nummer."ANSW"]])) {
                    $score = $score + $questions[$nummer]["scores"][$_POST["Q".$nummer."ANSW"]];
                }
            }
            return $score;
        }
     
        function wb_quiz_evaluate() {
            global $questions, $rankings;
            $score = 0;
            foreach ($questions as $questno => $curquest) {
                $score = $score + wb_quiz_score_question($questno);
            }
            foreach ($rankings as $ranking) {
                if (($score >= $ranking["lower"]) and ($score <= $ranking["upper"])) {
                    printf ($ranking["text"], $score);
                }
            }
        }
     
        function wb_quiz_ask_questions() {
            global $questions, $question_string, $submit_button_string, $intro_string;
            echo $intro_string;
            ?> <br /><code><form method="post" action="<?php get_permalink(); ?>">
                <input type="hidden" name="evaluate" value="yep" /><?php
            foreach ($questions as $questno => $curquest) {
                printf($question_string, $questno, $curquest['question']);
                if ($curquest["type"] == "OPEN") { ?>
                    <input type="text" name="Q<?php echo $questno; ?>ANSW"  size="40" maxlength="60" /> <br /> <?php
                }
                elseif ($curquest["type"] == "RADIO") {
                    foreach ($curquest["options"] as $key => $antwoord) {  ?>
                    <p class="indent"><input type="radio" name="Q<?php echo $questno; ?>ANSW" value="<?php echo $key ?>" />&nbsp;<?php echo $antwoord ?> </p><?php }
                }
                elseif ($curquest["type"] == "CHECK") {
                    foreach ($curquest["options"] as $key => $antwoord) { ?>
                        <input type="checkbox"
                            name="Q<?php echo $questno; ?>ANSW<?php echo $key ?>"
                            value="<?php echo $key ?>" /><?php echo $antwoord ?> <br /><?php
                    }
                }
            }
            ?><br /><p align="center"><input type="submit" name=".submit" value="<?php echo $submit_button_string ?>" /></p>
            </form></code><?php
        }
     
     
    /*
        First let's get and read our questions + settings
    */
        global $questions, $rankings, $question_string, $submit_button_string, $intro_string;
        $question_file = get_post_custom_values("question_file");
        if (!isset($question_file)) {
            $question_file[0] = 'wb_quiz_questions.php';
        }
        require($question_file[0]);
     
    /*
        Let's check if we're evaluating or asking questions///
    */
     
        if (!isset($_POST['evaluate'])) {
            wb_quiz_ask_questions();
        }
        else {
            wb_quiz_evaluate();
        }
     
    }
    ?>

    I presume we have to specify the correct answers text in the first file as some kind of "$correctanswers_string", and add some sort of "print" command in the second file, but I have no idea where without causing parse errors. This text is always the same, no matter what the score of the user is, and should be displayed under the result of the test.

    Can anyone point me in the right direction? How and where do I do this?

    Thanks in advance,

    Stef
    ThaVincy | redesigning your image
    Twizted Imagebuilding personal and corporate reputation solutions

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    wb_quiz_evaluate() is the function that runs after the form is submitted. Add your code there. You do not have to define a string in one file and print it in another if you just want to output static text.

    PHP Code:
    echo "This is some text, printed directly to the webpage."

  3. #3
    reputation consultant ThaVincy's Avatar
    Join Date
    Mar 2003
    Location
    Flanders
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dan! Works like a charm!
    ThaVincy | redesigning your image
    Twizted Imagebuilding personal and corporate reputation solutions


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •