]> git.mjollnir.org Git - moodle.git/commitdiff
Refactor how question types register themselves with the question bank. This change...
authortjhunt <tjhunt>
Tue, 15 Aug 2006 21:25:38 +0000 (21:25 +0000)
committertjhunt <tjhunt>
Tue, 15 Aug 2006 21:25:38 +0000 (21:25 +0000)
15 files changed:
lib/questionlib.php
question/type/calculated/questiontype.php
question/type/description/questiontype.php
question/type/essay/questiontype.php
question/type/match/questiontype.php
question/type/missingtype/questiontype.php
question/type/multianswer/questiontype.php
question/type/multichoice/questiontype.php
question/type/numerical/questiontype.php
question/type/questiontype.php
question/type/random/questiontype.php
question/type/randomsamatch/questiontype.php
question/type/rqp/questiontype.php
question/type/shortanswer/questiontype.php
question/type/truefalse/questiontype.php

index f772f04432c9cf601fa2c1ad0bed6129ca16078b..f153365d1dffc76416b98fa85012096d47c63615 100644 (file)
@@ -37,7 +37,7 @@ define('QUESTION_EVENTMANUALGRADE', '9');   // Grade was entered by teacher
 /**#@-*/
 
 /**#@+
- * The core question types - I don't think these constants are used any more. If so, they can be removed.
+ * The core question types.
  */
 define("SHORTANSWER",   "shortanswer");
 define("TRUEFALSE",     "truefalse");
@@ -78,13 +78,13 @@ define('QUESTION_ADAPTIVE', 1);
 /**#@-*/
 
 /// QTYPES INITIATION //////////////////
-
+// These variables get initialised via calls to question_register_questiontype
+// as the question type classes are included.
+global $QTYPES, $QTYPE_MENU, $QTYPE_MANUAL, $QTYPE_EXCLUDE_FROM_RANDOM;
 /**
  * Array holding question type objects
  */
-global $QTYPES;
-$QTYPES = array(); // This array will be populated when the questiontype.php files are loaded below
-
+$QTYPES = array();
 /**
  * Array of question types names translated to the user's language
  *
@@ -92,15 +92,51 @@ $QTYPES = array(); // This array will be populated when the questiontype.php fil
  * be able to create directly. Some internal question types like random questions are excluded.
  * The complete list of question types can be found in {@link $QTYPES}.
  */
-$QTYPE_MENU = array(); // This array will be populated when the questiontype.php files are loaded
+$QTYPE_MENU = array();
+/**
+ * String in the format "'type1','type2'" that can be used in SQL clauses like
+ * "WHERE q.type IN ($QTYPE_MANUAL)".
+ */
+$QTYPE_MANUAL = ''; 
+/**
+ * String in the format "'type1','type2'" that can be used in SQL clauses like
+ * "WHERE q.type NOT IN ($QTYPE_EXCLUDE_FROM_RANDOM)".
+ */
+$QTYPE_EXCLUDE_FROM_RANDOM = '';
+
+/**
+ * Add a new question type to the various global arrays above.
+ * 
+ * @param object $qtype An instance of the new question type class.
+ */
+function question_register_questiontype($qtype) {
+    global $QTYPES, $QTYPE_MENU, $QTYPE_MANUAL, $QTYPE_EXCLUDE_FROM_RANDOM;
+    
+    $name = $qtype->name();
+    $QTYPES[$name] = $qtype;
+    $menuname = $qtype->menu_name();
+    if ($menuname) {
+        $QTYPE_MENU[$name] = $menuname;
+    }
+    if ($qtype->is_manual_graded()) {
+        if ($QTYPE_MANUAL) {
+            $QTYPE_MANUAL .= ',';
+        }
+        $QTYPE_MANUAL .= "'$name'";
+    }
+    if (!$qtype->is_usable_by_random()) {
+        if ($QTYPE_EXCLUDE_FROM_RANDOM) {
+            $QTYPE_EXCLUDE_FROM_RANDOM .= ',';
+        }
+        $QTYPE_EXCLUDE_FROM_RANDOM .= "'$name'";
+    }
+}
 
 require_once("$CFG->dirroot/question/type/questiontype.php");
 
-/*
-* Load the questiontype.php file for each question type
-* These files in turn instantiate the corresponding question type class
-* and add them to the $QTYPES array
-*/
+// Load the questiontype.php file for each question type
+// These files in turn call question_register_questiontype() 
+// with a new instance of each qtype class.
 $qtypenames= get_list_of_plugins('question/type');
 foreach($qtypenames as $qtypename) {
     // Instanciates all plug-in question types
index 539962d9e7534cbd005474165ab5d9660064429f..3131630dec2402cbf2f57acc4db01b8c61153b63 100644 (file)
@@ -1,7 +1,7 @@
 <?php  // $Id$
 
 /////////////////
-/// CALCULATED ///
+// CALCULATED ///
 /////////////////
 
 /// QUESTION TYPE CLASS //////////////////
@@ -704,9 +704,7 @@ class question_calculated_qtype extends question_dataset_dependent_questiontype
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['calculated']= new question_calculated_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['calculated'] = get_string("calculated", "quiz");
+question_register_questiontype(new question_calculated_qtype());
 
 function qtype_calculated_calculate_answer($formula, $individualdata,
         $tolerance, $tolerancetype, $answerlength, $answerformat='1', $unit='') {
index 4038b2f93872e4fb4b58bc6f3005291789cf5b34..6b4005df5a7e114a995ecce12f31b90efc6d3772 100644 (file)
@@ -16,6 +16,10 @@ class description_qtype extends default_questiontype {
     function name() {
         return 'description';
     }
+    
+    function is_usable_by_random() {
+        return false;
+    }
 
     function get_question_options(&$question) {
         // No options to be restored for this question type
@@ -69,9 +73,5 @@ class description_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-// define("DESCRIPTION",   "7"); // already defined in questionlib.php
-$QTYPES['description']= new description_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['description'] = get_string("description", "quiz");
-
+question_register_questiontype(new description_qtype());
 ?>
index a15be1e6329499449e5a4a94ed2c1119384ace79..bd4d541eb2972511dd8e983ee8e479beb4ea3d1f 100644 (file)
@@ -10,6 +10,14 @@ class question_essay_qtype extends default_questiontype {
     function name() {
         return 'essay';
     }
+    
+    function is_manual_graded() {
+        return true;   
+    }
+
+    function is_usable_by_random() {
+        return false;
+    }
 
     function save_question_options($question) {
         if ($answer = get_record("question_answers", "question", $question->id)) {
@@ -104,10 +112,5 @@ class question_essay_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['essay'] = new question_essay_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['essay'] = get_string("essay", "quiz");
-// Add essay to the list of manually graded questions
-$QTYPE_MANUAL = isset($QTYPE_MANUAL) ? $QTYPE_MANUAL.",'essay'" : "'essay'";
-
+question_register_questiontype(new question_essay_qtype());
 ?>
index 17a1707a68eec4e2f0b0d43134cadfc08a7f2c21..93248b2a02037ab572bcdc5895886a624374aa97 100644 (file)
@@ -595,8 +595,5 @@ class question_match_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['match']= new question_match_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['match'] = get_string("match", "quiz");
-
+question_register_questiontype(new question_match_qtype());
 ?>
index 6493f3bd9b85bbc27789129c2e65f5bdf63f9547..94a417d364888450b2d586937f75f8cb570c5e05 100644 (file)
@@ -18,6 +18,14 @@ class question_missingtype_qtype extends default_questiontype {
     function name() {
         return 'missingtype';
     }
+    
+    function menu_name() {
+        return false;
+    }
+    
+    function is_usable_by_random() {
+        return false;
+    }
 
     function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
         global $CFG;
@@ -56,6 +64,6 @@ class question_missingtype_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['missingtype']= new question_missingtype_qtype();
+question_register_questiontype(new question_missingtype_qtype());
 
 ?>
index ece94a77274fb633e6e09469980f631097d3b054..0dd0396333cfb1c16877718d70141d8d5ece8ca5 100644 (file)
@@ -584,9 +584,7 @@ class embedded_cloze_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['multianswer']= new embedded_cloze_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['multianswer'] = get_string("multianswer", "quiz");
+question_register_questiontype(new embedded_cloze_qtype());
 
 /////////////////////////////////////////////////////////////
 //// ADDITIONAL FUNCTIONS
index 86612dfb2191df5c55d8aaca348fe34a68466455..44ccadff9ed7670fed4f11681e183a251a49998e 100644 (file)
@@ -513,8 +513,5 @@ class question_multichoice_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['multichoice']= new question_multichoice_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['multichoice'] = get_string("multichoice", "quiz");
-
+question_register_questiontype(new question_multichoice_qtype());
 ?>
index 70fb65d14a263febdc29f7a30cdf9fb89fb6b847..7d53649a1f996ba8071f134bb36fe9892b613522 100644 (file)
@@ -500,8 +500,5 @@ class question_numerical_qtype extends question_shortanswer_qtype {
 }
 
 // INITIATION - Without this line the question type is not in use.
-$QTYPES['numerical']= new question_numerical_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['numerical'] = get_string("numerical", "quiz");
-
+question_register_questiontype(new question_numerical_qtype());
 ?>
index ab2478fd1b51c39a1e19a2dd78b6e718f66abf72..2bae2e6de9cafaaf704e9633959f6a1828775c39 100644 (file)
 class default_questiontype {
 
     /**
-    * Name of the question type
-    *
-    * The name returned should coincide with the name of the directory
-    * in which this questiontype is located
-    * @ return string
-    */
+     * Name of the question type
+     *
+     * The name returned should coincide with the name of the directory
+     * in which this questiontype is located
+     * 
+     * @ return string the name of this question type.
+     */
     function name() {
         return 'default';
     }
 
+    /**
+     * The name this question should appear as in the create new question 
+     * dropdown.
+     * 
+     * @return mixed the desired string, or false to hide this question type in the menu.
+     */
+    function menu_name() {
+        $name = $this->name();
+        $menu_name = get_string($name, 'qtype_' . $name);
+        if ($menu_name[0] == '[') {
+            // Legacy behavior, if the string was not in the proper qtype_name 
+            // language file, look it up in the quiz one.
+            $menu_name = get_string($this->name(), 'quiz');
+        }
+        return $menu_name;
+    }
+    
+    /**
+     * @return boolean true if this question can only be graded manually.
+     */
+    function is_manual_graded() {
+        return false;
+    }
+
+    /**
+     * @return boolean true if this question type can be used by the random question type.
+     */
+    function is_usable_by_random() {
+        return true;
+    }
+
     /**
     * Saves or updates a question after editing by a teacher
     *
index 2f35ca1415620896df8147f0918f341c4ffdc430..e47369604318a2a4cae607fbb6801633199d011d 100644 (file)
@@ -7,8 +7,6 @@
 /// QUESTION TYPE CLASS //////////////////
 class random_qtype extends default_questiontype {
 
-    var $excludedtypes = array("'random'", "'randomsamatch'", "'essay'", "'description'");
-
     // Carries questions available as randoms sorted by category
     // This array is used when needed only
     var $catrandoms = array();
@@ -17,6 +15,14 @@ class random_qtype extends default_questiontype {
         return 'random';
     }
 
+    function menu_name() {
+        return false;
+    }
+
+    function is_usable_by_random() {
+        return false;
+    }
+
     function get_question_options(&$question) {
         // Don't do anything here, because the random question has no options.
         // Everything is handled by the create- or restore_session_and_responses
@@ -32,6 +38,7 @@ class random_qtype extends default_questiontype {
     }
 
     function create_session_and_responses(&$question, &$state, $cmoptions, $attempt) {
+        global $QTYPE_EXCLUDE_FROM_RANDOM;
         // Choose a random question from the category:
         // We need to make sure that no question is used more than once in the
         // quiz. Therfore the following need to be excluded:
@@ -46,7 +53,6 @@ class random_qtype extends default_questiontype {
             // Need to fetch random questions from category $question->category"
             // (Note: $this refers to the questiontype, not the question.)
             global $CFG;
-            $excludedtypes = implode(',', $this->excludedtypes);
             if ($question->questiontext == "1") {
                 // recurse into subcategories
                 $categorylist = question_categorylist($question->category);
@@ -58,7 +64,7 @@ class random_qtype extends default_questiontype {
                        WHERE category IN ($categorylist)
                          AND parent = '0'
                          AND id NOT IN ($cmoptions->questionsinuse)
-                         AND qtype NOT IN ($excludedtypes)")) {
+                         AND qtype NOT IN ($QTYPE_EXCLUDE_FROM_RANDOM)")) {
                 $this->catrandoms[$question->category][$question->questiontext] =
                   draw_rand_array($catrandoms, count($catrandoms)); // from bug 1889
             } else {
@@ -242,6 +248,6 @@ class random_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES[RANDOM]= new random_qtype();
+question_register_questiontype(new random_qtype());
 
 ?>
index 21f16fd94bd416cfcdd77c8b4a643070ef107a19..e2a968dfbee11e2ee5ae645797575e1acd78d037 100644 (file)
@@ -15,6 +15,10 @@ class question_randomsamatch_qtype extends question_match_qtype {
         return 'randomsamatch';
     }
 
+    function is_usable_by_random() {
+        return false;
+    }
+
     function get_question_options(&$question) {
         if (!$question->options = get_record('question_randomsamatch', 'question', $question->id)) {
             notify('Error: Missing question options for random short answer question '.$question->id.'!');
@@ -351,8 +355,5 @@ class question_randomsamatch_qtype extends question_match_qtype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['randomsamatch']= new question_randomsamatch_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['randomsamatch'] = get_string("randomsamatch", "quiz");
-
+question_register_questiontype(new question_randomsamatch_qtype());
 ?>
index 68d45d5564b03fbc9448807b86214277ecfd0195..605a1514664da068a590f012a6ea8337dc49ca9c 100644 (file)
@@ -18,15 +18,15 @@ require_once($CFG->dirroot . '/question/type/rqp/remote.php');
 */
 class question_rqp_qtype extends default_questiontype {
 
-    /**
-    * Name of the rqp question type
-    *
-    * @ return string 'rqp'
-    */
     function name() {
         return 'rqp';
     }
 
+    function menu_name() {
+        // Does not currently work, so don't include in the menu.
+        return false;
+    }
+
     /**
     * Save the type-specific options
     *
@@ -547,6 +547,6 @@ class question_rqp_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES[RQP]= new question_rqp_qtype();
+question_register_questiontype(new question_rqp_qtype());
 
 ?>
index 061a6c26c6ed6fafb9f6a3b746725f334a435dd0..b34af78e0de35e322f48764d9c493258fb79f14e 100644 (file)
@@ -319,8 +319,5 @@ class question_shortanswer_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['shortanswer']= new question_shortanswer_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['shortanswer'] = get_string("shortanswer", "quiz");
-
+question_register_questiontype(new question_shortanswer_qtype());
 ?>
index 96d20c15150b7e7346356a1f0f60183dd71e03bc..97782ce903c12f3806d0f0183dce1f22be922f78 100644 (file)
@@ -321,8 +321,5 @@ class question_truefalse_qtype extends default_questiontype {
 //////////////////////////////////////////////////////////////////////////
 //// INITIATION - Without this line the question type is not in use... ///
 //////////////////////////////////////////////////////////////////////////
-$QTYPES['truefalse']= new question_truefalse_qtype();
-// The following adds the questiontype to the menu of types shown to teachers
-$QTYPE_MENU['truefalse'] = get_string("truefalse", "quiz");
-
+question_register_questiontype(new question_truefalse_qtype());
 ?>