]> git.mjollnir.org Git - moodle.git/commitdiff
Improving multichoice display MDL-14763
authorpichetp <pichetp>
Mon, 18 Aug 2008 18:31:31 +0000 (18:31 +0000)
committerpichetp <pichetp>
Mon, 18 Aug 2008 18:31:31 +0000 (18:31 +0000)
question/type/multianswer/edit_multianswer_form.php
question/type/multianswer/questiontype.php
question/type/multichoice/questiontype.php

index 6239f1f8e64515d94e9a0edfcc730d20a5265fcd..77fadcef8af5a96c03a4ad271ad89973143bf30f 100644 (file)
@@ -66,8 +66,8 @@ class question_edit_multianswer_form extends question_edit_form {
             $mform->addElement('static', 'sub_'.$sub."_".'defaultgrade', get_string('defaultgrade', 'quiz'));
             $mform->setDefault('sub_'.$sub."_".'defaultgrade',$this->questiondisplay->options->questions[$sub]->defaultgrade);
 
-                if ($this->questiondisplay->options->questions[$sub]->qtype =='multichoice'  ) {
-                    $mform->addElement('checkbox', 'sub_'.$sub."_".'layout', get_string('layout', 'quiz')) ;//, $gradeoptions);
+                if ($this->questiondisplay->options->questions[$sub]->qtype =='multichoice'   ) {
+                    $mform->addElement('static', 'sub_'.$sub."_".'layout', get_string('layout', 'quiz'),array('cols'=>60, 'rows'=>1)) ;//, $gradeoptions);
                 }
             foreach ($this->questiondisplay->options->questions[$sub]->answer as $key =>$ans) {
 
@@ -151,7 +151,22 @@ class question_edit_multianswer_form extends question_edit_form {
                     $answercount = 0;
                     $maxgrade = false;
                     $maxfraction = -1;
-
+                    if ($subquestion->qtype == 'multichoice' ) {
+                        $default_values[$prefix.'layout']  = $subquestion->layout ;
+                        switch ($subquestion->layout) {
+                            case '0':
+                                $default_values[$prefix.'layout']= get_string('selectelement', 'qtype_multianswer');
+                                break;
+                            case '1':
+                                $default_values[$prefix.'layout']= get_string('verticallayout', 'qtype_multianswer');
+                                break;                         
+                            case '2':
+                                $default_values[$prefix.'layout']= get_string('horizontallayout', 'qtype_multianswer');
+                                break;
+                            default:
+                                $default_values[$prefix.'layout']= get_string('unknownlayout', 'qtype_multianswer');
+                        } 
+                    }
                     foreach ($subquestion->answer as $key=>$answer) {
                         if ( $subquestion->qtype == 'numerical' && $key == 0 ) {
                             $default_values[$prefix.'tolerance['.$key.']']  = $subquestion->tolerance[0] ;
index b0635196376c772c84867773c47ce9b34e0c1c6e..75c15ac74672bf749a2c5c55ec78187a03dc338d 100644 (file)
@@ -420,30 +420,127 @@ class embedded_cloze_qtype extends default_questiontype {
                     echo $feedbackimg;
                     break;
                 case 'multichoice':
-                    $outputoptions = '<option></option>'; // Default empty option
-                    foreach ($answers as $mcanswer) {
+             //    echo "<p>layout <pre>".$wrapped->options->layout;print_r($wrapped->options);echo "</pre><p>";
+                 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) . '</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 (!empty($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){
+              //          echo "vertical";
+              //   echo "<p>answers <pre>";print_r($answers);echo "</pre><p>";
+                        $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;
+                          //  echo "aid ".$a->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);
                     }
-                    // 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 (!empty($feedback) && !empty($USER->screenreader)) {
-                        echo "<img src=\"$CFG->pixpath/i/feedback.gif\" alt=\"$feedback\" />";
+                }
+    
+                // Print the answer text
+                $a->text = '<span class="anun">' . $ordernumber . '<span class="anumsep">.</span></span> ' .
+                        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";
                     }
-                    echo $feedbackimg;
+                    
                     break;
                 default:
                     $a = new stdClass;
@@ -808,7 +905,7 @@ define("NUMERICAL_ABS_ERROR_MARGIN", 6);
 
 // Remaining ANSWER regexes
 define("ANSWER_TYPE_DEF_REGEX",
-       '(NUMERICAL|NM)|(MULTICHOICE|MC)|(SHORTANSWER|SA|MW)');
+       '(NUMERICAL|NM)|(MULTICHOICE|MC)|(MULTICHOICE_V|MCV)|(MULTICHOICE_H|MCH)|(SHORTANSWER|SA|MW)');
 define("ANSWER_START_REGEX",
        '\{([0-9]*):(' . ANSWER_TYPE_DEF_REGEX . '):');
 
@@ -823,8 +920,10 @@ define("ANSWER_REGEX",
 define("ANSWER_REGEX_NORM", 1);
 define("ANSWER_REGEX_ANSWER_TYPE_NUMERICAL", 3);
 define("ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE", 4);
-define("ANSWER_REGEX_ANSWER_TYPE_SHORTANSWER", 5);
-define("ANSWER_REGEX_ALTERNATIVES", 6);
+define("ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_REGULAR", 5);
+define("ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_HORIZONTAL", 6);
+define("ANSWER_REGEX_ANSWER_TYPE_SHORTANSWER", 7);
+define("ANSWER_REGEX_ALTERNATIVES", 8);
 
 function qtype_multianswer_extract_question($text) {
     $question = new stdClass;
@@ -853,6 +952,23 @@ function qtype_multianswer_extract_question($text) {
             $wrapped->correctfeedback = '';
             $wrapped->partiallycorrectfeedback = '';
             $wrapped->incorrectfeedback = '';
+            $wrapped->layout = 0;
+        } else if(!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_REGULAR])) {
+            $wrapped->qtype = 'multichoice';
+            $wrapped->single = 1;
+            $wrapped->answernumbering = 0;
+            $wrapped->correctfeedback = '';
+            $wrapped->partiallycorrectfeedback = '';
+            $wrapped->incorrectfeedback = '';
+            $wrapped->layout = 1;
+        } else if(!empty($answerregs[ANSWER_REGEX_ANSWER_TYPE_MULTICHOICE_HORIZONTAL])) {
+            $wrapped->qtype = 'multichoice';
+            $wrapped->single = 1;
+            $wrapped->answernumbering = 0;
+            $wrapped->correctfeedback = '';
+            $wrapped->partiallycorrectfeedback = '';
+            $wrapped->incorrectfeedback = '';
+            $wrapped->layout = 2;
         } else {
             print_error('unknownquestiontype', 'question', '', $answerregs[2]);
             return false;
index 4d8f51719811d3307ce84ca53a7623d1d5e42cbf..701ba4b481176785cf2bd87561cb7d57cb57f659 100644 (file)
@@ -105,6 +105,9 @@ class question_multichoice_qtype extends default_questiontype {
         }
         $options->answers = implode(",",$answers);
         $options->single = $question->single;
+        if(isset($question->layout)){
+             $options->layout = $question->layout;
+        }
         $options->answernumbering = $question->answernumbering;
         $options->shuffleanswers = $question->shuffleanswers;
         $options->correctfeedback = trim($question->correctfeedback);