/**
     * function which redefines Question::createAnswersForm
     * @param the formvalidator instance
     */
    function createAnswersForm($form)
    {
        $defaults = array();
        if (!empty($this->id)) {
            $objAnswer = new answer($this->id);
            $preArray = explode('@@', $objAnswer->selectAnswer(1));
            $defaults['formula'] = array_pop($preArray);
            $defaults['answer'] = array_shift($preArray);
            $defaults['answer'] = preg_replace("/\\[.*\\]/", "", $defaults['answer']);
            $defaults['weighting'] = $this->weighting;
        } else {
            $defaults['answer'] = get_lang('DefaultTextInBlanks');
        }
        $lowestValue = "1.00";
        $highestValue = "20.00";
        // javascript //
        echo '<script>

            function parseTextNumber(textNumber, floatValue) {
                if (textNumber.indexOf(".") > -1) {
                    textNumber = parseFloat(textNumber);
                    floatValue.exists = "true";
                } else {
                    textNumber = parseInt(textNumber);
                }
                return textNumber;
            }

            function updateRandomValue(element) {
                // "floatValue" helps to distinguish between an integer (10) and a float with all 0 decimals (10.00)
                var floatValue = { exists: "false" };
                var index = (element.name).match(/\\[[^\\]]*\\]/g);
                var lowestValue = parseTextNumber(document.getElementById("lowestValue"+index).value, floatValue);
                var highestValue = parseTextNumber(document.getElementById("highestValue"+index).value, floatValue);
                var result = Math.random() * (highestValue - lowestValue) + lowestValue;
                if (floatValue.exists == "true") {
                    result = parseFloat(result).toFixed(2);
                } else {
                    result = parseInt(result);
                }
                document.getElementById("randomValue"+index).innerHTML = "' . get_lang("ExampleValue") . ': " + result;
           }

            function FCKeditor_OnComplete(editorInstance) {
                if (window.attachEvent) {
                    editorInstance.EditorDocument.attachEvent("onkeyup", updateBlanks) ;
                } else {
                    editorInstance.EditorDocument.addEventListener("keyup", updateBlanks, true);
                }
            }

            var firstTime = true;

            function updateBlanks() {
                if (firstTime) {
                    field = document.getElementById("answer");
                    var answer = field.value;
                } else {
                    var oEditor = FCKeditorAPI.GetInstance("answer");
                    //var answer =  oEditor.GetXHTML(true);
                    var answer = oEditor.EditorDocument.body.innerHTML;
                }
                var blanks = answer.match(/\\[[^\\]]*\\]/g);
                var fields = "<div class=\\"control-group\\"><label class=\\"control-label\\">' . get_lang('VariableRanges') . '</label><div class=\\"controls\\"><table>";
                if (blanks!=null) {
                    if (typeof updateBlanks.randomValues === "undefined") {
                        updateBlanks.randomValues = [];
                    }
                    for (i=0 ; i<blanks.length ; i++){
                        if (document.getElementById("lowestValue["+i+"]") && document.getElementById("highestValue["+i+"]")) {
                            lowestValue = document.getElementById("lowestValue["+i+"]").value;
                            highestValue = document.getElementById("highestValue["+i+"]").value;
                        } else {
                            lowestValue = ' . $lowestValue . '.toFixed(2);
                            highestValue = ' . $highestValue . '.toFixed(2);
                            for (j=0; j<blanks.length; j++) {
                                updateBlanks.randomValues[j] = parseFloat(Math.random() * (highestValue - lowestValue) + lowestValue).toFixed(2);
                            }
                        }
                        fields += "<tr><td><label>"+blanks[i]+"</label></td><td><input class=\\"span1\\" style=\\"margin-left: 0em;\\" size=\\"5\\" value=\\""+lowestValue+"\\" type=\\"text\\" id=\\"lowestValue["+i+"]\\" name=\\"lowestValue["+i+"]\\" onblur=\\"updateRandomValue(this)\\"/></td><td><input class=\\"span1\\" style=\\"margin-left: 0em; width:80px;\\" size=\\"5\\" value=\\""+highestValue+"\\" type=\\"text\\" id=\\"highestValue["+i+"]\\" name=\\"highestValue["+i+"]\\" onblur=\\"updateRandomValue(this)\\"/></td><td><label class=\\"span3\\" id=\\"randomValue["+i+"]\\"/>' . get_lang('ExampleValue') . ': "+updateBlanks.randomValues[i]+"</label></td></tr>";
                    }
                }
                document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
                if (firstTime) {
                    firstTime = false;
                }
            }

            window.onload = updateBlanks;

        </script>';
        // answer
        $form->addElement('label', null, '<br /><br />' . get_lang('TypeTextBelow') . ', ' . get_lang('And') . ' ' . get_lang('UseTagForBlank'));
        $form->addElement('html_editor', 'answer', '<img src="../img/fill_field.png">', 'id="answer" cols="122" rows="6" onkeyup="javascript: updateBlanks(this);"', array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '350'));
        $form->addRule('answer', get_lang('GiveText'), 'required');
        $form->addRule('answer', get_lang('DefineBlanks'), 'regex', '/\\[.*\\]/');
        $form->addElement('label', null, get_lang('IfYouWantOnlyIntegerValuesWriteBothLimitsWithoutDecimals'));
        $form->addElement('html', '<div id="blanks_weighting"></div>');
        $notationListButton = Display::url(get_lang('NotationList'), api_get_path(WEB_PATH) . 'main/exercice/evalmathnotation.php', array('class' => 'btn ajax', '_target' => '_blank'));
        $form->addElement('html', '<div class="control-group">
                <label class="control-label"></label>
                <div class="controls">' . $notationListButton . '</div>
            </div>');
        $form->addElement('label', null, get_lang('FormulaExample'));
        $form->addElement('text', 'formula', get_lang('Formula'), array('id' => 'formula', 'class' => 'span4'));
        $form->addRule('formula', get_lang('GiveFormula'), 'required');
        $form->addElement('text', 'weighting', get_lang('Weighting'), array('id' => 'weighting', 'class' => 'span1'));
        $form->setDefaults(array('weighting' => '10'));
        $form->addElement('text', 'answerVariations', get_lang('AnswerVariations'), array('class' => 'span1'));
        $form->addRule('answerVariations', get_lang('GiveAnswerVariations'), 'required');
        $form->setDefaults(array('answerVariations' => '1'));
        global $text, $class;
        // setting the save button here and not in the question class.php
        $form->addElement('style_submit_button', 'submitQuestion', $text, 'class="' . $class . '"');
        if (!empty($this->id)) {
            $form->setDefaults($defaults);
        } else {
            if ($this->isContent == 1) {
                $form->setDefaults($defaults);
            }
        }
    }
Ejemplo n.º 2
0
    /**
     * function which redefines Question::createAnswersForm
     * @param the formvalidator instance
     */
    function createAnswersForm($form)
    {
        $defaults = array();
        if (!empty($this->id)) {
            $objAnswer = new answer($this->id);
            // the question is encoded like this
            // [A] B [C] D [E] F::10,10,10@1
            // number 1 before the "@" means that is a switchable fill in blank question
            // [A] B [C] D [E] F::10,10,10@ or  [A] B [C] D [E] F::10,10,10
            // means that is a normal fill blank question
            $pre_array = explode('::', $objAnswer->selectAnswer(1));
            //make sure we only take the last bit to find special marks
            $sz = count($pre_array);
            $is_set_switchable = explode('@', $pre_array[$sz - 1]);
            if ($is_set_switchable[1]) {
                $defaults['multiple_answer'] = 1;
            } else {
                $defaults['multiple_answer'] = 0;
            }
            //Take the complete string except after the last '::'
            $defaults['answer'] = '';
            for ($i = 0; $i < $sz - 1; $i++) {
                $defaults['answer'] .= $pre_array[$i];
            }
            $a_weightings = explode(',', $is_set_switchable[0]);
        } else {
            $defaults['answer'] = get_lang('DefaultTextInBlanks');
        }
        $setValues = null;
        if (isset($a_weightings) && count($a_weightings) > 0) {
            foreach ($a_weightings as $i => $weighting) {
                $setValues .= 'document.getElementById("weighting[' . $i . ']").value = "' . $weighting . '";';
            }
        }
        // javascript
        echo '<script>

			function FCKeditor_OnComplete(editorInstance) {
				if (window.attachEvent) {
					editorInstance.EditorDocument.attachEvent("onkeyup", updateBlanks) ;
				} else {
					editorInstance.EditorDocument.addEventListener("keyup", updateBlanks, true);
				}
			}

            var firstTime = true;

            function updateBlanks() {
                if (firstTime) {
                    field = document.getElementById("answer");
                    var answer = field.value;

                } else {
                    var oEditor = FCKeditorAPI.GetInstance("answer");
                    //var answer =  oEditor.GetXHTML(true);
                    var answer = oEditor.EditorDocument.body.innerHTML;
                }

                var blanks = answer.match(/\\[[^\\]]*\\]/g);
                var fields = "<div class=\\"control-group\\"><label class=\\"control-label\\">' . get_lang('Weighting') . '</label><div class=\\"controls\\"><table>";
                if (blanks!=null) {
                    for (i=0 ; i<blanks.length ; i++){
                        if (document.getElementById("weighting["+i+"]"))
                            value = document.getElementById("weighting["+i+"]").value;
                        else
                            value = "10";
                        fields += "<tr><td><label>"+blanks[i]+"</label></td><td><input style=\\"margin-left: 0em;\\" size=\\"5\\" value=\\""+value+"\\" type=\\"text\\" id=\\"weighting["+i+"]\\" name=\\"weighting["+i+"]\\" /></td></tr>";
                    }
                }
                document.getElementById("blanks_weighting").innerHTML = fields + "</table></div></div>";
                if (firstTime) {
                    firstTime = false;
                    ' . $setValues . '
			    }
            }

            window.onload = updateBlanks;
		</script>';
        // answer
        $form->addElement('label', null, '<br /><br />' . get_lang('TypeTextBelow') . ', ' . get_lang('And') . ' ' . get_lang('UseTagForBlank'));
        $form->addElement('html_editor', 'answer', '<img src="../img/fill_field.png">', 'id="answer" cols="122" rows="6" onkeyup="javascript: updateBlanks(this);"', array('ToolbarSet' => 'TestQuestionDescription', 'Width' => '100%', 'Height' => '350'));
        $form->addRule('answer', get_lang('GiveText'), 'required');
        $form->addRule('answer', get_lang('DefineBlanks'), 'regex', '/\\[.*\\]/');
        //added multiple answers
        $form->addElement('checkbox', 'multiple_answer', '', get_lang('FillInBlankSwitchable'));
        $form->addElement('html', '<div id="blanks_weighting"></div>');
        global $text, $class;
        // setting the save button here and not in the question class.php
        $form->addElement('style_submit_button', 'submitQuestion', $text, 'class="' . $class . '"');
        if (!empty($this->id)) {
            $form->setDefaults($defaults);
        } else {
            if ($this->isContent == 1) {
                $form->setDefaults($defaults);
            }
        }
    }