]> git.mjollnir.org Git - moodle.git/commitdiff
migrated 'missingtype' questoin creation forms to use formslib
authorjamiesensei <jamiesensei>
Thu, 18 Jan 2007 10:22:56 +0000 (10:22 +0000)
committerjamiesensei <jamiesensei>
Thu, 18 Jan 2007 10:22:56 +0000 (10:22 +0000)
question/type/missingtype/edit_missingtype_form.php [new file with mode: 0644]
question/type/missingtype/editquestion.html [deleted file]
question/type/missingtype/editquestion.php [deleted file]
question/type/missingtype/questiontype.php

diff --git a/question/type/missingtype/edit_missingtype_form.php b/question/type/missingtype/edit_missingtype_form.php
new file mode 100644 (file)
index 0000000..15a3cda
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+/**
+ * Defines the editing form for the missingtype question type.
+ *
+ * @copyright &copy; 2007 Jamie Pratt
+ * @author Jamie Pratt me@jamiep.org
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package questions
+ */
+
+/**
+ * missingtype editing form definition.
+ */
+class question_edit_missingtype_form extends question_edit_form {
+    /**
+     * Add question-type specific form fields.
+     *
+     * @param object $mform the form being built.
+     */
+    function definition_inner(&$mform) {
+        $creategrades = get_grade_options();
+        $gradeoptions = $creategrades->gradeoptionsfull;
+        $repeated = array();
+        $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_multichoice', '{no}'));
+        $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
+        $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
+        $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
+
+        if (isset($this->question->options)){
+            $countanswers = count($this->question->options->answers);
+        } else {
+            $countanswers = 0;
+        }
+        $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
+                            QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
+        $repeatedoptions = array();
+        $repeatedoptions['answer']['type'] = PARAM_NOTAGS;//text with no multilang support
+        $repeatedoptions['fraction']['default'] = 0;
+        $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice'));
+    }
+
+    function set_data($question) {
+        if (isset($question->options)){
+            $answers = $question->options->answers;
+            if (count($answers)) {
+                $key = 0;
+                foreach ($answers as $answer){
+                    $default_values['answer['.$key.']'] = $answer->answer;
+                    $default_values['fraction['.$key.']'] = $answer->fraction;
+                    $default_values['feedback['.$key.']'] = $answer->feedback;
+                    $key++;
+                }
+            }
+            $question = (object)((array)$question + $default_values);
+        }
+        parent::set_data($question);
+    }
+
+    function qtype() {
+        return 'missingtype';
+    }
+
+    function validation($data){
+        $errors = array();
+        $answers = $data['answer'];
+        $answercount = 0;
+
+        $totalfraction = 0;
+        $maxfraction = -1;
+
+        foreach ($answers as $key => $answer){
+            //check no of choices
+            $trimmedanswer = trim($answer);
+            if (!empty($trimmedanswer)){
+                $answercount++;
+            }
+            //check grades
+            if ($answer != '') {
+                if ($data['fraction'][$key] > 0) {
+                    $totalfraction += $data['fraction'][$key];
+                }
+                if ($data['fraction'][$key] > $maxfraction) {
+                    $maxfraction = $data['fraction'][$key];
+                }
+            }
+        }
+
+        if ($answercount==0){
+            $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 1);
+        }
+
+
+        $totalfraction = round($totalfraction, 2);
+        if ($totalfraction != 1) {
+            $totalfraction = $totalfraction * 100;
+            $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
+        }
+
+        return $errors;
+    }
+}
+?>
\ No newline at end of file
diff --git a/question/type/missingtype/editquestion.html b/question/type/missingtype/editquestion.html
deleted file mode 100644 (file)
index 23cfade..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-$QTYPES[$question->qtype]->print_question_form_start($question, array(), $course, $usehtmleditor);
-
-for ($i=1; $i<=count($answers); $i++) {
-    if (!isset($answers[$i-1]->fraction)) {
-        $answers[$i-1]->answer = '';
-        $answers[$i-1]->fraction = 0;
-        $answers[$i-1]->feedback = '';
-    }
-?>
-
-  <tr valign="top">
-    <td align="right"><b><?php  echo get_string("choice", "quiz")." $i";  ?>:</b></td>
-    <td align="left">
-      <input type="text" name="answer[]" size="50" value="<?php  p($answers[$i-1]->answer) ?>" alt="<?php  echo get_string("choice", "quiz")." $i";  ?>"/>&nbsp;&nbsp;
-      <?php
-          print_string("grade");
-          echo ":&nbsp;";
-          choose_from_menu($gradeoptionsfull, "fraction[]", $answers[$i-1]->fraction, "");
-      ?>
-      <br />
-    </td>
-  </tr>
-
-  <tr valign="top">
-    <td align="right"><b><?php  print_string("feedback", "quiz") ?>:</b></td>
-    <td align="left">
-      <textarea name="feedback[]" rows="2" cols="50"><?php  p($answers[$i-1]->feedback) ?></textarea>
-    </td>
-  </tr>
-
-  <tr valign="top">
-    <td colspan="2">&nbsp;</td>
-  </tr>
-
-<?php
-} /// End of loop, printing answers
-
-$QTYPES['missingtype']->print_replacement_options($question, $course, $contextquiz);
-$QTYPES['missingtype']->print_question_form_end($question);
-?>
diff --git a/question/type/missingtype/editquestion.php b/question/type/missingtype/editquestion.php
deleted file mode 100644 (file)
index 5f69b9c..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php // $Id$
-
-    if (!empty($options->answers)) {
-        $answersraw = get_records_list('question_answers', 'id', $options->answers);
-
-    }
-
-    $answers = array();
-    if (!empty($answersraw)) {
-        foreach ($answersraw as $answer) {
-            $answers[] = $answer;   // insert answers into slots
-        }
-    }
-
-    $yesnooptions = array();
-    $yesnooptions[0] = get_string('no');
-    $yesnooptions[1] = get_string('yes');
-
-    print_heading(get_string('warningmissingtype', 'quiz'));
-    require("$CFG->dirroot/question/type/missingtype/editquestion.html");
-
-?>
index 94a417d364888450b2d586937f75f8cb570c5e05..d91a136ce92f897ee107009ee42a109b55ace152 100644 (file)
@@ -18,11 +18,11 @@ class question_missingtype_qtype extends default_questiontype {
     function name() {
         return 'missingtype';
     }
-    
+
     function menu_name() {
         return false;
     }
-    
+
     function is_usable_by_random() {
         return false;
     }
@@ -48,7 +48,7 @@ class question_missingtype_qtype extends default_questiontype {
             foreach ($answers as $answer) {
                 $a = new stdClass;
                 $a->text = format_text("$answer->answer", FORMAT_MOODLE, $formatoptions, $cmoptions->course);
-    
+
                 $anss[] = clone($a);
             }
         }
@@ -58,6 +58,14 @@ class question_missingtype_qtype extends default_questiontype {
     function grade_responses(&$question, &$state, $cmoptions) {
         return true;
     }
+
+    function display_question_editing_page(&$mform, $question, $wizardnow){
+
+        print_heading(get_string('warningmissingtype', 'quiz'));
+
+        $mform->display();
+
+    }
 }
 //// END OF CLASS ////