diff --git a/lib/questionlib.php b/lib/questionlib.php index 13364fe5e15..f68487d23d9 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -98,14 +98,6 @@ global $QTYPES, $QTYPE_MENU, $QTYPE_MANUAL, $QTYPE_EXCLUDE_FROM_RANDOM; * Array holding question type objects */ $QTYPES = array(); -/** - * Array of question types names translated to the user's language - * - * The $QTYPE_MENU array holds the names of all the question types that the user should - * 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(); /** * String in the format "'type1','type2'" that can be used in SQL clauses like * "WHERE q.type IN ($QTYPE_MANUAL)". @@ -127,6 +119,7 @@ function question_register_questiontype($qtype) { $name = $qtype->name(); $QTYPES[$name] = $qtype; + // The $QTYPE_MENU global is not deprecated, but still build it to avoid breaking 3rd-party code that needs it. $menuname = $qtype->menu_name(); if ($menuname) { $QTYPE_MENU[$name] = $menuname; @@ -161,6 +154,34 @@ foreach($qtypenames as $qtypename) { } } +/** + * An array of question type names translated to the user's language, suitable for use when + * creating a drop-down menu of options. + * + * Long-time Moodle programmers will realise that this replaces the old $QTYPE_MENU array. + * The array returned will only hold the names of all the question types that the user should + * be able to create directly. Some internal question types like random questions are excluded. + * + * @return array an array of question type names translated to the user's language. + */ +function question_type_menu() { + global $QTYPES, $QTYPE_MENU; + static $menu_options = null; + if (is_null($menu_options)) { + $menu_options = array(); + foreach ($QTYPES as $name => $qtype) { + $menuname = $qtype->menu_name(); + if ($menuname) { + $menu_options[$name] = $menuname; + } + } + // For now, keep a copy in the old global, + // to try to avoid breaking third party code that relies on it. + $QTYPE_MENU = $menu_options; + } + return $menu_options; +} + /// OTHER CLASSES ///////////////////////////////////////////////////////// /** diff --git a/question/editlib.php b/question/editlib.php index b929e80347f..475be9fcb4a 100644 --- a/question/editlib.php +++ b/question/editlib.php @@ -161,9 +161,9 @@ function question_category_form_checkbox($name, $checked) { function question_list($course, $categoryid, $quizid=0, $recurse=1, $page=0, $perpage=100, $showhidden=false, $sortorder='qtype, name ASC', $showquestiontext = false) { - global $QTYPE_MENU, $USER, $CFG, $THEME; + global $USER, $CFG, $THEME; - $qtypemenu = $QTYPE_MENU; + $qtypemenu = question_type_menu(); if ($rqp_types = get_records('question_rqp_types')) { foreach($rqp_types as $type) { $qtypemenu['rqp_'.$type->id] = $type->name; diff --git a/question/type/multianswer/edit_multianswer_form.php b/question/type/multianswer/edit_multianswer_form.php index 331cb92cc99..1dd086482ff 100644 --- a/question/type/multianswer/edit_multianswer_form.php +++ b/question/type/multianswer/edit_multianswer_form.php @@ -17,7 +17,7 @@ class question_edit_multianswer_form extends question_edit_form { var $questiondisplay ; function definition_inner(&$mform) { - global $QTYPE_MENU; + $question_type_names = question_type_menu(); $mform->addRule('questiontext', null, 'required', null, 'client'); // Remove meaningless defaultgrade field. @@ -49,7 +49,7 @@ class question_edit_multianswer_form extends question_edit_form { $this->editas[$sub] = optional_param('sub_'.$sub."_".'qtype', '', PARAM_RAW); } $mform->addElement('header', 'subhdr', get_string('questionno', 'quiz', - '{#'.$sub.'}').' '.$QTYPE_MENU[$this->questiondisplay->options->questions[$sub]->qtype]); + '{#'.$sub.'}').' '.$question_type_names[$this->questiondisplay->options->questions[$sub]->qtype]); $mform->addElement('static', 'sub_'.$sub."_".'questiontext', "subquestiontext",array('cols'=>60, 'rows'=>3));