]> git.mjollnir.org Git - moodle.git/commitdiff
Added question type that is used if a question of unknown type is encountered.
authorgustav_delius <gustav_delius>
Fri, 24 Mar 2006 21:21:59 +0000 (21:21 +0000)
committergustav_delius <gustav_delius>
Fri, 24 Mar 2006 21:21:59 +0000 (21:21 +0000)
Also removed some of the unnecessary calls to name()

lib/questionlib.php
mod/quiz/editlib.php
question/question.php
question/type/description/question.html
question/type/missingtype/display.html [new file with mode: 0644]
question/type/missingtype/editquestion.html [new file with mode: 0644]
question/type/missingtype/editquestion.php [new file with mode: 0644]
question/type/missingtype/icon.gif [new file with mode: 0644]
question/type/missingtype/questiontype.php [new file with mode: 0644]
question/type/question.html
question/type/questiontype.php

index d24dc8017a275891aefc6376073e7c7989492dbf..6029898a12cefd9e10fd9d5fcc9b2c5a27361612 100644 (file)
@@ -521,6 +521,10 @@ function get_question_options(&$questions) {
         $keys = array_keys($questions);
         // update each question object
         foreach ($keys as $i) {
+            if (!array_key_exists($questions[$i]->qtype, $QTYPES)) {
+                $questions[$i]->qtype = 'missingtype';
+            }
+            
             // set name prefix
             $questions[$i]->name_prefix = question_make_name_prefix($i);
 
@@ -529,6 +533,9 @@ function get_question_options(&$questions) {
         }
         return true;
     } else { // deal with single question
+        if (!array_key_exists($questions->qtype, $QTYPES)) {
+            $questions->qtype = 'missingtype';
+        }
         $questions->name_prefix = question_make_name_prefix($questions->id);
         return $QTYPES[$questions->qtype]->get_question_options($questions);
     }
@@ -1155,12 +1162,12 @@ function print_question_icon($question, $editlink=true, $return = false) {
 
     $namestr = get_string($question->qtype, 'quiz');
     $html = '<img border="0" height="16" width="16" src="'.$CFG->wwwroot.'/question/type/'.
-            $QTYPES[$question->qtype]->name().'/icon.gif" alt="'.
+            $question->qtype.'/icon.gif" alt="'.
             $namestr.'" title="'.$namestr.'" />';
 
     if ($editlink) {
         $html =  "<a href=\"$CFG->wwwroot/question/question.php?id=$question->id\" title=\""
-                .$QTYPES[$question->qtype]->name()."\">".
+                .$question->qtype."\">".
                 $html."</a>\n";
     }
     if ($return) {
index ff6d5c49ae8ea246ec8e3f1efc02910814fc3972..74bc1836362dc068d1138aec2e55c5ca3fe79682 100644 (file)
@@ -181,9 +181,15 @@ function quiz_print_question_list($quiz, $allowdelete=true, $showbreaks=true, $r
     echo "<tr><th colspan=\"3\" nowrap=\"nowrap\" class=\"header\">$strorder</th><th class=\"header\">#</th><th align=\"left\" width=\"100%\" nowrap=\"nowrap\" class=\"header\">$strquestionname</th><th nowrap=\"nowrap\" class=\"header\">$strtype</th><th nowrap=\"nowrap\" class=\"header\">$strgrade</th><th align=\"center\" width=\"60\" nowrap=\"nowrap\" class=\"header\">$straction</th></tr>\n";
     foreach ($order as $i => $qnum) {
 
-        if ($qnum and empty($questions[$qnum])) {
+        if (!$qnum or empty($questions[$qnum])) {
             continue;
         }
+
+        // If the questiontype is missing change the question type
+        if (!array_key_exists($questions[$qnum]->qtype, $QTYPES)) {
+            $questions[$qnum]->qtype = 'missingtype';
+        }
+
         // Show the re-ordering field if the tool is turned on.
         // But don't show it in front of pagebreaks if they are hidden.
         if ($reordertool) {
index 49ff99cd4d130c56dc5e6b5c46a9058347ac500c..c4be99a42843208bee87140a061608d3fd664953 100644 (file)
@@ -52,6 +52,9 @@
         }
 
         $qtype = $question->qtype;
+        if (!isset($QTYPES[$qtype])) {
+            $qtype = 'missingtype';
+        }
 
     } else if ($category) { // only for creating new questions
         if (! $category = get_record("question_categories", "id", $category)) {
 
     echo '<br />';
     print_simple_box_start('center');
-    require_once('type/'.$QTYPES[$qtype]->name().'/editquestion.php');
+    require_once('type/'.$qtype.'/editquestion.php');
     print_simple_box_end();
 
     if ($usehtmleditor) {
index e805ffa68df21f1a8ee047b15ac0bf668c50b88d..b57f49404d9d36307b8d7d27d057ba4b00586608 100644 (file)
@@ -1,4 +1,4 @@
-<table id="q<?php echo $question->id; ?>" class="question <?php echo $this->name(); ?>"><tr><td>
+<table id="q<?php echo $question->id; ?>" class="question <?php echo $question->qtype; ?>"><tr><td>
   <div class="info">
     <span class="edit"><?php echo $editlink; ?></span>
   </div>
diff --git a/question/type/missingtype/display.html b/question/type/missingtype/display.html
new file mode 100644 (file)
index 0000000..a833706
--- /dev/null
@@ -0,0 +1,24 @@
+<?php notify('This question is of a type that has not been installed on your Moodle yet.<br />Please alert your Moodle administrator.'); ?>
+<div class="qtext">
+  <?php echo $questiontext; ?>
+</div>
+
+<?php if ($image) { ?>
+  <img class="qimage" src="<?php echo $q->imageurl; ?>" alt="" />
+<?php } ?>
+
+<div class="ablock clearfix">
+  <div class="prompt">
+    <?php print_string('answers', 'quiz'); ?>
+  </div>
+
+  <table class="answer">
+    <?php $row = 1; foreach ($anss as $answer) { ?>
+      <tr class="<?php echo 'r'.$row = $row ? 0 : 1; ?>">
+        <td class="c1 text">
+          <?php echo $answer->text; ?>
+        </td>
+      </tr>
+    <?php } ?>
+  </table>
+</div>
\ No newline at end of file
diff --git a/question/type/missingtype/editquestion.html b/question/type/missingtype/editquestion.html
new file mode 100644 (file)
index 0000000..85dfb37
--- /dev/null
@@ -0,0 +1,127 @@
+<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 align="left">
+      <?php   question_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 align="left">
+      <input type="text" name="name" size="40" 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 align="left">
+      <?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 align="left">
+      <?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("defaultgrade", "quiz") ?>:</b></td>
+    <td align="left">
+        <input type="text" name="defaultgrade" size="3" value="<?php  p($question->defaultgrade) ?>" alt="<?php  print_string("defaultgrade", "quiz") ?>" />
+
+        <?php  if (isset($err["defaultgrade"])) formerr($err["defaultgrade"]); ?>
+    </td>
+</tr>
+<tr valign="top">
+    <td align="right"><b><?php  print_string("penaltyfactor", "quiz") ?>:</b></td>
+    <td align="left">
+        <input type="text" name="penalty" size="3" value="<?php  p($question->penalty) ?>" alt="<?php  print_string("penaltyfactor", "quiz") ?>" />
+        <?php helpbutton('penalty', get_string('penalty', 'quiz'), 'quiz'); ?>
+        <?php  if (isset($err["penalty"])) formerr($err["penalty"]); ?>
+    </td>
+</tr>
+
+<?php
+    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);
+?>
+</table>
+
+</center>
+</form>
+
diff --git a/question/type/missingtype/editquestion.php b/question/type/missingtype/editquestion.php
new file mode 100644 (file)
index 0000000..2871089
--- /dev/null
@@ -0,0 +1,22 @@
+<?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_with_help(get_string("editingmissingtype", "quiz"), "multichoice", "quiz");
+    require("$CFG->dirroot/question/type/missingtype/editquestion.html");
+
+?>
diff --git a/question/type/missingtype/icon.gif b/question/type/missingtype/icon.gif
new file mode 100644 (file)
index 0000000..d264c81
Binary files /dev/null and b/question/type/missingtype/icon.gif differ
diff --git a/question/type/missingtype/questiontype.php b/question/type/missingtype/questiontype.php
new file mode 100644 (file)
index 0000000..f759bbd
--- /dev/null
@@ -0,0 +1,57 @@
+<?php  // $Id$
+
+///////////////////
+/// missingtype ///
+///////////////////
+
+/// QUESTION TYPE CLASS //////////////////
+
+/**
+ * Missing question type class
+ *
+ * When a question is encountered with a type that is not installed then its
+ * type is changed to 'missingtype'. This questiontype just makes sure that the
+ * necessary information is printed about the question.
+ */
+class question_missingtype_qtype extends default_questiontype {
+
+    function name() {
+        return 'missingtype';
+    }
+
+    function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
+        global $CFG;
+
+        $answers = &$question->options->answers;
+
+        $formatoptions = new stdClass;
+        $formatoptions->noclean = true;
+        $formatoptions->para = false;
+
+        // Print formulation
+        $questiontext = format_text($question->questiontext,
+                         $question->questiontextformat,
+                         $formatoptions, $cmoptions->course);
+        $image = get_question_image($question, $cmoptions->course);
+
+        // Print each answer in a separate row
+        foreach ($answers as $answer) {
+            $a->text = format_text("$answer->answer", FORMAT_MOODLE, $formatoptions, $cmoptions->course);
+
+            $anss[] = clone($a);
+        }
+        include("$CFG->dirroot/question/type/missingtype/display.html");
+    }
+
+    function grade_responses(&$question, &$state, $cmoptions) {
+        return true;
+    }
+}
+//// END OF CLASS ////
+
+//////////////////////////////////////////////////////////////////////////
+//// INITIATION - Without this line the question type is not in use... ///
+//////////////////////////////////////////////////////////////////////////
+$QTYPES['missingtype']= new question_missingtype_qtype();
+
+?>
index 83b73a280ece34f3747cd6e87e78183cf848e728..8e438da4553f81c818f1d99ebb3c95e6e2676f87 100644 (file)
@@ -3,7 +3,7 @@
  * print_question() method.
  */
 ?>
-<table id="q<?php echo $question->id; ?>" class="question <?php echo $this->name(); ?>"><tr><td>
+<table id="q<?php echo $question->id; ?>" class="question <?php echo $question->qtype; ?>"><tr><td>
   <div class="info">
     <span class="no"><?php echo $number; ?></span>
     <span class="edit"><?php echo $editlink; ?></span>
index e70d93ebcd45e3769b108e4b04f38ec3a7c2bb11..cf9c6f6efd39f6a6312af71ac7b661c6f2a610b7 100644 (file)
@@ -126,10 +126,7 @@ class default_questiontype {
     *                          it is not a standard question object.
     */
     function save_question_options($question) {
-        /// This default implementation must be overridden:
-
-        $result->error = "Unsupported question type ($question->qtype)!";
-        return $result;
+        return null;
     }
 
     /**
@@ -330,9 +327,11 @@ class default_questiontype {
     function get_correct_responses(&$question, &$state) {
         /* The default implementation returns the response for the first answer
         that gives full marks. */
-        foreach ($question->options->answers as $answer) {
-            if (((int) $answer->fraction) === 1) {
-                return array('' => $answer->answer);
+        if ($question->options->answers) {
+            foreach ($question->options->answers as $answer) {
+                if (((int) $answer->fraction) === 1) {
+                    return array('' => $answer->answer);
+                }
             }
         }
         return null;