<form name="theform" method="post" action="question.php">
<center>
<table cellpadding="5">
<tr valign="top">
    <td align="right"><b><?php  print_string("category", "quiz") ?>:</b></td>
    <td>
    <?php   quiz_category_select_menu($course->id, true, true, $question->category); ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php  print_string("questionname", "quiz") ?>:</b></td>
    <td>
        <input type="text" name="name" size="50" value="<?php  p($question->name) ?>" alt="<?php  print_string("questionname", "quiz") ?>" />
        <?php  if (isset($err["name"])) formerr($err["name"]); ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php  print_string("question", "quiz") ?>:</b>
    <br />
    <br />
    <br />
    <font size="1">
    <?php
           if ($usehtmleditor) {
               helpbutton("richtext", get_string("helprichtext"), "moodle", true, true);
           } else {
               helpbutton("text", get_string("helptext"), "moodle", true, true);
           }
    ?>
    </font>
    </td>
    <td>
        <?php  if (isset($err["questiontext"])) {
               formerr($err["questiontext"]); 
               echo "<br />";
           }

           print_textarea($usehtmleditor, 15, 60, 630, 300, "questiontext", $question->questiontext);

           if ($usehtmleditor) {
               echo '<input type="hidden" name="questiontextformat" value="'.FORMAT_HTML.'" />';
           } else {
               echo "<div align=\"right\">";
               print_string("formattexttype");
               echo ":&nbsp;";
               if (!isset($question->questiontextformat)) {
                   $question->questiontextformat = FORMAT_MOODLE;
               }
               choose_from_menu(format_text_menu(), "questiontextformat", $question->questiontextformat, "");
               helpbutton("textformat", get_string("helpformatting"));
               echo "</div>";
           }
        ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php  print_string("imagedisplay", "quiz") ?>:</b></td>
    <td>
    <?php   if (empty($images)) {
            print_string("noimagesyet");
        } else {
            choose_from_menu($images, "image", "$question->image", get_string("none"),"","");
        }
    ?>
    </td>
</tr>

<tr valign="top">
    <td align="right"><b><?php  print_string("correctanswer", "quiz") ?>:</b></td>
    <?php
        // Even thou the rest of the module can handle up to six numerical answers,
        // this form will limit the number of numerical answers to one only.
        if (is_numeric($answers[0]->min) && is_numeric($answers[0]->answer)) {
            $acceptederror = (float)($answers[0]->answer)
                           - (float)($answers[0]->min);
        } else {
            $acceptederror = "";
        }
    ?>
    <td>
        <input align="left" type="text" id="correct0" name="answer[]" size="20" value="<?php  p($answers[0]->answer) ?>" alt="<?php  print_string("correctanswer", "quiz") ?>" />&nbsp;&nbsp;
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php  print_string("acceptederror", "quiz"); ?>:</b></td>
    <td>
        <input align="left" type="text" id="acceptederror0" name="acceptederror[]" size="15" value="<?php  p($acceptederror) ?>" alt="<?php  print_string("acceptederror", "quiz"); ?>" />&plusmn;
        <!-- Values max and min will be determined when the form is submitted -->
        <input type="hidden" id="min0" name="min[]" value="" />
        <input type="hidden" id="max0" name="max[]" value="" />
        <input type="hidden" name="fraction[]" value="1" />
        <br/>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php  print_string("feedback", "quiz") ?>:</b></td>
    <td>
        <textarea name="feedback[]" rows="2" cols="50"><?php  p($answers[0]->feedback) ?></textarea>
    </td>
</tr>
<tr valign="top">
<td align="right"><b><?php  print_string("unit", "quiz") ?>:</b></td>
    <td>
        <input type="hidden" name="multiplier[]" value="1.0" />
        <input align="left" type="text" id="defaultunit" name="unit[]"
                size="5" value="<?php  p($units[0]->unit) ?>" alt="<?php  print_string("unit", "quiz") ?>" />
        <b>(<?php  print_string("optional", "quiz") ?>)</b>
    </td>
</tr>
<tr valign="top">
<td></td>
<td align="left"><b><?php  print_string("alternativeunits", "quiz") ?>:</b></td>
<td></td>
</tr>
<?php
for ($i=1; $i<count($units); $i++) {
    $unit=$units[$i];
?>
<tr valign="top">
<td></td>
    <td align="left">
        <b><?php  print_string("multiplier", "quiz") ?>:</b>
        <input type="text" id="<?php  p("multiplier$i") ?>" size="10"
                align="right" name="multiplier[]"
                value="<?php  p($unit->multiplier) ?>"
                alt="<?php  print_string("multiplier", "quiz") ?>" />
        <b>&nbsp;&nbsp;&nbsp;<?php  print_string("unit", "quiz") ?>:</b>
        <input align="left" type="text" id="<?php  p("unit$i") ?>"
                name="unit[]"
                size="5" value="<?php  p($unit->unit) ?>" />
    </td>
</tr>
<?php
} /// END for
?>
</table>

<input type="hidden" name="sesskey" value="<?php echo $USER->sesskey; ?>" />
<input type="hidden" name="id" value="<?php  p($question->id) ?>" />
<input type="hidden" name="qtype" value="<?php  p($question->qtype) ?>" />
<input type="submit" onclick="return determineMinAndMax();"  value="<?php  print_string("savechanges") ?>" />
</center>
</form>
<script language="javascript" type="text/javascript">
function determineMinAndMax() {
    // This client-side script will determine the values for min and max
    // based on the input for answer and acceptederror.
    with(document.theform) {
        if (correct0.value=='') {
            alert('<?php  print_string("missingcorrectanswer","quiz") ?>');
            return false;
        } else if (isNaN(correct0.value) && defaultunit.value) {
            alert('<?php  print_string("unitonlyworksfornumericanswers","quiz") ?>');
            return false;
        } else if (''!=defaultunit.value && !isNaN(defaultunit.value)) {
            alert('<?php  print_string("unitmustnotbenumeric","quiz") ?>');
            return false;
        } else if (acceptederror0.value=='') {
            var correct= parseFloat(correct0.value);
            if (!isNaN(correct)) {
                min0.value= correct;
                max0.value= correct;
                correct0.value += defaultunit.value;
            }
            return true;
        } else if (isNaN(acceptederror0.value) || isNaN(correct0.value)) {
            alert('<?php  print_string("answerswithacceptederrormarginmustbenumeric", "quiz") ?>');
            return false;
        } else {
            var correct= parseFloat(correct0.value);
            var error= Math.abs(acceptederror0.value);
            min0.value= correct-error;
            max0.value= correct+error;
            correct0.value += defaultunit.value;
            return true;
        }
    }
}
</script>
