function wrsqz_multipleAnswersTable($answer, $variables, $names) {
	$openmath = '<math xmlns="http://www.w3.org/1998/Math/MathML">';
	$closemath = '</math>';
	
	// x=v1;y=v2
    if(!isset($names)) $names = $answer;
    //clean $names array
    $names = explode(' ',$names);
    $cleanNames=array();
    foreach ($names as $name){
        if ($name[0]!='(') { //skip parameters
            if($name[0]=='#'){
                $name = mb_substr($name,1);
            }
            $cleanNames[]=$name;
        }
    }
    //make values array
	$vars = explode(' ',$answer);
    $values=array();
    foreach ($vars as $var){
        if ($var[0]!='(') { // skip parameters
            if($var[0]!='#') $var = '#'.$var;
            $value = wrsqz_assemble(wrsqz_textToVariables($var), $variables);
		//remove <math...> and </math>
		    $value = mb_substr($value,mb_strlen($openmath),-mb_strlen($closemath));
            $values[] = $value;
        }
    }

    //Write mathml
    $r = $openmath .'<mtable columnalign="left" rowspacing="0">';
	foreach ($cleanNames as $index => $name) {
		if ($var[0]!='(') { // skip parameters
			$r .= '<mtr><mtd>';
			$r .= '<mi>' . $name . '</mi>';
			$r .= '<mo>=</mo>';
			$r .= $values[$index];
			$r .= '</mtd></mtr>';
		}
	}
	$r .= '</mtable>'. $closemath;
	return $r;
}
function wrsqz_response_summary($questionType, $dbType, $question, $state, $length=80){
    if($questionType == 'match'){
        //assemble Questiontexts and answers.
        $subquestions = &$state->options->subquestions;
        $responses    = &$state->responses;
        $table = new stdClass();
        $table->data = array();
        $table->width = '100%';
        foreach ($subquestions as $key => $sub) {
            foreach ($responses as $ind => $code) {
               if (isset($sub->options->answers[$code])) {
                   //assemble $text in encoded MathML.
                   $text = format_text(wrsqz_assemble($subquestions[$ind]->questiontext,$state->vars),$question->questiontextformat);
                   //assemble $answer in text mode.
                   $answer = wrsqz_assemble(wrsqz_variablesToText($sub->options->answers[$code]->answer),$state->vars);
                   $table->data[] =  array($text ,$answer);
               }
           }
       }
       $summary = print_table($table, true);

    }else if($questionType == 'shortanswer'){
        if(isset($state->responses[''])){
            mb_parse_str($question->options->wiris->eqoption, $eqoptionArray);
            if(isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true'){
                $summary = format_text(wrsqz_mathmlEncode(stripslashes($state->responses[''])),FORMAT_HTML);
            }else{
                $summary = stripslashes($state->responses['']);
            }
        }else{
            $summary='';
        }
    }else if($questionType == 'truefalse'){
        if(isset($state->responses['']) && isset($question->options->answers[$state->responses['']])){
            $summary = $question->options->answers[$state->responses['']]->answer;
        }else{
            $summary = '';
        }
    }else if($questionType == 'multianswer'){
        global $QTYPES;
        
        $table = new stdClass();
        $table->data = array();
        $table->data[0] = array();
        $table->width = '100%';
        $numquestions = count($question->options->questions);
        $table->size=array_fill(0,$numquestions,100/$numquestions . '%');
        foreach ($question->options->questions as $key=>$wrapped) {
            $wrappedstate = clone($state);
            $wrappedstate->responses = array(''=>$state->responses[$key]);
            $table->data[0][] = $QTYPES[$wrapped->qtype]->response_summary($wrapped,$wrappedstate);
        }
        $summary = print_table($table, true);
    }
    return $summary;
}
    function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options)
    {
        global $QTYPES, $CFG, $USER;
        $readonly = empty($options->readonly) ? '' : 'readonly="readonly"';
        $disabled = empty($options->readonly) ? '' : 'disabled="disabled"';
        $formatoptions = new stdClass();
        $formatoptions->noclean = true;
        $formatoptions->para = false;
        $nameprefix = $question->name_prefix;
        // adding an icon with alt to warn user this is a fill in the gap question
        // MDL-7497
        if (!empty($USER->screenreader)) {
            echo "<img src=\"{$CFG->wwwroot}/question/type/{$question->qtype}/icon.gif\" " . "class=\"icon\" alt=\"" . get_string('clozeaid', 'qtype_multichoice') . "\" />  ";
        }
        echo '<div class="ablock clearfix">';
        // For this question type, we better print the image on top:
        if ($image = get_question_image($question)) {
            echo '<img class="qimage" src="' . $image . '" alt="" /><br />';
        }
        $qtextremaining = format_text($question->questiontext, $question->questiontextformat, $formatoptions, $cmoptions->course);
        $strfeedback = get_string('feedback', 'quiz');
        // The regex will recognize text snippets of type {#X}
        // where the X can be any text not containg } or white-space characters.
        while (ereg('\\{#([^[:space:]}]*)}', $qtextremaining, $regs)) {
            $qtextsplits = explode($regs[0], $qtextremaining, 2);
            echo $qtextsplits[0];
            echo "<label>";
            // MDL-7497
            $qtextremaining = $qtextsplits[1];
            $positionkey = $regs[1];
            if (isset($question->options->questions[$positionkey]) && $question->options->questions[$positionkey] != '') {
                $wrapped =& $question->options->questions[$positionkey];
                $answers =& $wrapped->options->answers;
                // $correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $state);
                $inputname = $nameprefix . $positionkey;
                if (isset($state->responses[$positionkey])) {
                    $response = $state->responses[$positionkey];
                } else {
                    $response = null;
                }
                // Determine feedback popup if any
                $popup = '';
                $style = '';
                $feedbackimg = '';
                $feedback = '';
                $correctanswer = '';
                $strfeedbackwrapped = $strfeedback;
                $testedstate = clone $state;
                if (($correctanswers = $QTYPES[$wrapped->qtype]->get_correct_responses($wrapped, $testedstate)) !== false) {
                    if ($options->readonly && $options->correct_responses) {
                        $delimiter = '';
                        if ($correctanswers) {
                            foreach ($correctanswers as $ca) {
                                switch ($wrapped->qtype) {
                                    case 'numerical':
                                    case 'shortanswer':
                                    case 'shortanswerwiris':
                                        $correctanswer .= $delimiter . $ca;
                                        break;
                                    case 'multichoice':
                                    case 'multichoicewiris':
                                        if (isset($answers[$ca])) {
                                            $correctanswer .= $delimiter . $answers[$ca]->answer;
                                        }
                                        break;
                                }
                                $delimiter = ', ';
                            }
                        }
                    }
                    if ($correctanswer != '') {
                        $feedback = '<div class="correctness">';
                        $feedback .= get_string('correctansweris', 'quiz', s($correctanswer, true));
                        $feedback .= '</div>';
                    }
                }
                if ($options->feedback) {
                    $chosenanswer = null;
                    switch ($wrapped->qtype) {
                        case 'shortanswerwiris':
                            $index = 0;
                            $maxgrade = 0.0;
                            if (isset($state->grades) && isset($state->grades[$positionkey])) {
                                foreach ($answers as $answer) {
                                    if (isset($state->grades[$positionkey][$index]) && $state->grades[$positionkey][$index] > $maxgrade) {
                                        $chosenanswer = clone $answer;
                                        $maxgrade = $state->grades[$positionkey][$index];
                                    }
                                    $index++;
                                }
                            }
                            //Try string comparation if wiris grade is zero for this response.
                            if (!empty($chosenanswer)) {
                                break;
                            }
                        case 'numerical':
                        case 'shortanswer':
                            $testedstate = clone $state;
                            $testedstate->responses[''] = $response;
                            foreach ($answers as $answer) {
                                if ($QTYPES[$wrapped->qtype]->test_response($wrapped, $testedstate, $answer)) {
                                    $chosenanswer = clone $answer;
                                    break;
                                }
                            }
                            break;
                        case 'multichoicewiris':
                        case 'multichoice':
                            if (isset($answers[$response])) {
                                $chosenanswer = clone $answers[$response];
                            }
                            break;
                        default:
                            break;
                    }
                    // Set up a default chosenanswer so that all non-empty wrong
                    // answers are highlighted red
                    if (empty($chosenanswer) && $response != '') {
                        $chosenanswer = new stdClass();
                        $chosenanswer->fraction = 0.0;
                    }
                    if (isset($chosenanswer->feedback) && $chosenanswer->feedback !== '') {
                        $feedback = s(str_replace(array("\\", "'"), array("\\\\", "\\'"), $feedback . $chosenanswer->feedback));
                        if ($options->readonly && $options->correct_responses) {
                            $strfeedbackwrapped = get_string('correctanswerandfeedback', 'qtype_multianswer');
                        } else {
                            $strfeedbackwrapped = get_string('feedback', 'quiz');
                        }
                        $popup = " onmouseover=\"return overlib('{$feedback}', STICKY, MOUSEOFF, CAPTION, '{$strfeedbackwrapped}', FGCOLOR, '#FFFFFF');\" " . " onmouseout=\"return nd();\" ";
                    }
                    /// Determine style
                    if ($options->feedback && $response != '') {
                        $style = 'class = "' . question_get_feedback_class($chosenanswer->fraction) . '"';
                        $feedbackimg = question_get_feedback_image($chosenanswer->fraction);
                    } else {
                        $style = '';
                        $feedbackimg = '';
                    }
                }
                if ($feedback !== '' && $popup === '') {
                    $strfeedbackwrapped = get_string('correctanswer', 'qtype_multianswer');
                    $feedback = s(str_replace(array("\\", "'"), array("\\\\", "\\'"), $feedback));
                    $popup = " onmouseover=\"return overlib('{$feedback}', STICKY, MOUSEOFF, CAPTION, '{$strfeedbackwrapped}', FGCOLOR, '#FFFFFF');\" " . " onmouseout=\"return nd();\" ";
                }
                // Print the input control
                $editor = false;
                switch ($wrapped->qtype) {
                    case 'shortanswerwiris':
                        mb_parse_str($wrapped->options->wiris->eqoption, $eqoptionArray);
                        $editor = isset($eqoptionArray['editor']) && $eqoptionArray['editor'] == 'true';
                    case 'shortanswer':
                    case 'numerical':
                        if (!$editor) {
                            $size = 1;
                            foreach ($answers as $answer) {
                                if (strlen(trim($answer->answer)) > $size) {
                                    $assembledAnswer = wrsqz_assemble(wrsqz_variablesToText($answer->answer), $state->vars);
                                    $size = strlen(trim($assembledAnswer));
                                }
                            }
                            if (strlen(trim($response)) > $size) {
                                $size = strlen(trim($response)) + 1;
                            }
                            $size = $size + rand(0, $size * 0.15);
                            $size > 60 ? $size = 60 : ($size = $size);
                            $styleinfo = "size=\"{$size}\"";
                            /**
                             * Uncomment the following lines if you want to limit for small sizes.
                             * Results may vary with browsers see MDL-3274
                             */
                            /*
                            if ($size < 2) {
                                $styleinfo = 'style="width: 1.1em;"';
                            }
                            if ($size == 2) {
                                $styleinfo = 'style="width: 1.9em;"';
                            }
                            if ($size == 3) {
                                $styleinfo = 'style="width: 2.3em;"';
                            }
                            if ($size == 4) {
                                $styleinfo = 'style="width: 2.8em;"';
                            }
                            */
                            echo "<input {$style} {$readonly} {$popup} name=\"{$inputname}\"";
                            echo "  type=\"text\" value=\"" . s($response, true) . "\" " . $styleinfo . " /> ";
                        } else {
                            echo wrsqz_embeddedShortanswerInput($wrapped, $response, $inputname, $style, $readonly, $popup);
                        }
                        if ($feedback !== '' && !empty($USER->screenreader)) {
                            echo "<img src=\"{$CFG->pixpath}/i/feedback.gif\" alt=\"{$feedback}\" />";
                        }
                        echo $feedbackimg;
                        break;
                    case 'multichoicewiris':
                    case 'multichoice':
                        if ($wrapped->options->layout == 0) {
                            $outputoptions = '<option></option>';
                            // Default empty option
                            foreach ($answers as $mcanswer) {
                                $selected = '';
                                if ($response == $mcanswer->id) {
                                    $selected = ' selected="selected"';
                                }
                                $outputoptions .= "<option value=\"{$mcanswer->id}\"{$selected}>" . s($mcanswer->answer, true) . '</option>';
                            }
                            // In the next line, $readonly is invalid HTML, but it works in
                            // all browsers. $disabled would be valid, but then the JS for
                            // displaying the feedback does not work. Of course, we should
                            // not be relying on JS (for accessibility reasons), but that is
                            // a bigger problem.
                            //
                            // The span is used for safari, which does not allow styling of
                            // selects.
                            echo "<span {$style}><select {$popup} {$readonly} {$style} name=\"{$inputname}\">";
                            echo $outputoptions;
                            echo '</select></span>';
                            if ($feedback !== '' && !empty($USER->screenreader)) {
                                echo "<img src=\"{$CFG->pixpath}/i/feedback.gif\" alt=\"{$feedback}\" />";
                            }
                            echo $feedbackimg;
                        } else {
                            if ($wrapped->options->layout == 1 || $wrapped->options->layout == 2) {
                                $ordernumber = 0;
                                $anss = array();
                                foreach ($answers as $mcanswer) {
                                    $ordernumber++;
                                    $checked = '';
                                    $chosen = false;
                                    $type = 'type="radio"';
                                    $name = "name=\"{$inputname}\"";
                                    if ($response == $mcanswer->id) {
                                        $checked = 'checked="checked"';
                                        $chosen = true;
                                    }
                                    $a = new stdClass();
                                    $a->id = $question->name_prefix . $mcanswer->id;
                                    $a->class = '';
                                    $a->feedbackimg = '';
                                    // Print the control
                                    $a->control = "<input {$readonly} id=\"{$a->id}\" {$name} {$checked} {$type} value=\"{$mcanswer->id}\" />";
                                    if ($options->correct_responses && $mcanswer->fraction > 0) {
                                        $a->class = question_get_feedback_class(1);
                                    }
                                    if ($options->feedback && $chosen || $options->correct_responses) {
                                        if ($type == ' type="checkbox" ') {
                                            $a->feedbackimg = question_get_feedback_image($mcanswer->fraction > 0 ? 1 : 0, $chosen && $options->feedback);
                                        } else {
                                            $a->feedbackimg = question_get_feedback_image($mcanswer->fraction, $chosen && $options->feedback);
                                        }
                                    }
                                    // Print the answer text: no automatic numbering
                                    $a->text = format_text($mcanswer->answer, FORMAT_MOODLE, $formatoptions, $cmoptions->course);
                                    // Print feedback if feedback is on
                                    if (($options->feedback || $options->correct_responses) && $checked) {
                                        //|| $options->readonly
                                        $a->feedback = format_text($mcanswer->feedback, true, $formatoptions, $cmoptions->course);
                                    } else {
                                        $a->feedback = '';
                                    }
                                    $anss[] = clone $a;
                                }
                                ?>
            <?php 
                                if ($wrapped->options->layout == 1) {
                                    ?>
                  <table class="answer">
                    <?php 
                                    $row = 1;
                                    foreach ($anss as $answer) {
                                        ?>
                      <tr class="<?php 
                                        echo 'r' . ($row = $row ? 0 : 1);
                                        ?>
">
                        <td class="c0 control">
                          <?php 
                                        echo $answer->control;
                                        ?>
                        </td>
                        <td class="c1 text <?php 
                                        echo $answer->class;
                                        ?>
">
                          <label for="<?php 
                                        echo $answer->id;
                                        ?>
">
                            <?php 
                                        echo $answer->text;
                                        ?>
                            <?php 
                                        echo $answer->feedbackimg;
                                        ?>
                          </label>
                        </td>
                        <td class="c0 feedback">
                          <?php 
                                        echo $answer->feedback;
                                        ?>
                        </td>
                      </tr>
                    <?php 
                                    }
                                    ?>
                  </table>
                  <?php 
                                } else {
                                    if ($wrapped->options->layout == 2) {
                                        ?>

                  <table class="answer">
                      <tr class="<?php 
                                        echo 'r' . ($row = $row ? 0 : 1);
                                        ?>
">
                    <?php 
                                        $row = 1;
                                        foreach ($anss as $answer) {
                                            ?>
                        <td class="c0 control">
                          <?php 
                                            echo $answer->control;
                                            ?>
                        </td>
                        <td class="c1 text <?php 
                                            echo $answer->class;
                                            ?>
">
                          <label for="<?php 
                                            echo $answer->id;
                                            ?>
">
                            <?php 
                                            echo $answer->text;
                                            ?>
                            <?php 
                                            echo $answer->feedbackimg;
                                            ?>
                          </label>
                        </td>
                        <td class="c0 feedback">
                          <?php 
                                            echo $answer->feedback;
                                            ?>
                        </td>
                    <?php 
                                        }
                                        ?>
                      </tr>
                  </table>
                  <?php 
                                    }
                                }
                            } else {
                                echo "no valid layout";
                            }
                        }
                        break;
                    default:
                        $a = new stdClass();
                        $a->type = $wrapped->qtype;
                        $a->sub = $positionkey;
                        print_error('unknownquestiontypeofsubquestion', 'qtype_multianswer', '', $a);
                        break;
                }
                echo "</label>";
                // MDL-7497
            } else {
                if (!isset($question->options->questions[$positionkey])) {
                    echo $regs[0] . "</label>";
                } else {
                    echo '</label><div class="error" >' . get_string('questionnotfound', 'qtype_multianswer', $positionkey) . '</div>';
                }
            }
        }
        // Print the final piece of question text:
        echo $qtextremaining;
        if (!empty($question->options->wiris->options['wirisCASForComputations'])) {
            $wirisCASContent = '';
            if (isset($state->responses['wirisCASHidden'])) {
                $wirisCASContent = htmlentities(stripslashes_safe($state->responses['wirisCASHidden']), ENT_QUOTES, 'UTF-8');
            }
            echo '<br />';
            echo wrsqz_wirisCASAppletHTML($nameprefix, $wirisCASContent, false, 630, 300);
            require_js(array('yui_yahoo', 'yui_dom-event'));
            require_js($CFG->wwwroot . '/wiris-quizzes/js/wiris-quizzes.js');
        }
        $this->print_question_submit_buttons($question, $state, $cmoptions, $options);
        echo '</div>';
    }
示例#4
0
function wrsqz_testGetVars(){
	
	$questionId=999999999;
	$program = '<session lang=\"en\" version=\"2.0\"><library closed=\"false\"><mtext style=\"color:#ffc800\" xml:lang=\"es\">variables</mtext><group><command><input><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>a</mi><mo>=</mo><mn>1</mn></math></input></command><command><input><math xmlns=\"http://www.w3.org/1998/Math/MathML\"><mi>p</mi><mo>=</mo><mi>plot</mi><mo>(</mo><mi>sin</mi><mo>(</mo><mi>random</mi><mo>(</mo><mn>0</mn><mo>.</mo><mn>5</mn><mo>,</mo><mn>2</mn><mo>.</mo><mn>0</mn><mo>)</mo><mo>*</mo><mi>x</mi><mo>)</mo><mo>)</mo></math></input></command></group></library></session>';
	
	$data = new StdClass();
	$data->question = $questionId;
	$data->program = $program;
	
	$programId=insert_record('question_wshanprom',$data);
	
	$ok=true;
	
	if($programId===false){
		$ok=false;
		$message = 'Error accessing database.';
	}
	
	$wiris = new StdClass();
	$wiris->question=$questionId;
	$wiris->idcache=0;
	$wiris->md5=md5($program);
	$wiris->eqoption='';
	
	$data=array(
		'seed' => rand(),
		'text' => ' #p  ##Ta',
		'wiris' => $wiris
	);
	
	$result=wrsqz_getInfo('shortanswer',$questionId,$data);
	
	if($result['vars']['##Ta']!='1'){
		$ok = false;
		$message = 'Error with numeric variable';
	}else if(substr($result['vars']['##Wp'],0,7)!='<image>'){
		$ok=false;
		$message = 'Error with image variable';
	}
	
	if($ok){
		$message = wrsqz_assemble('Variables retrieved sucessfully. Look at the plot #p', $result['vars']);
	}
	
	delete_records('question_wshanprom', 'question', $questionId);
	printRow('Variables',$ok,$message);
	
}