diff --git a/admin/generator.php b/admin/generator.php index 0f764107d27..353b3201898 100755 --- a/admin/generator.php +++ b/admin/generator.php @@ -622,7 +622,7 @@ class generator { require_once($CFG->libdir .'/questionlib.php'); require_once($CFG->dirroot .'/mod/quiz/editlib.php'); $questions = array(); - $questionsmenu = question_type_menu(); + $questionsmenu = question_bank::get_creatable_qtypes(); $questiontypes = array(); foreach ($questionsmenu as $qtype => $qname) { $questiontypes[] = $qtype; diff --git a/admin/qtypes.php b/admin/qtypes.php index 5ce88d33340..b02e6b19a36 100644 --- a/admin/qtypes.php +++ b/admin/qtypes.php @@ -1,277 +1,296 @@ libdir . '/questionlib.php'); - require_once($CFG->libdir . '/adminlib.php'); - require_once($CFG->libdir . '/tablelib.php'); +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see . -/// Check permissions. - require_login(); - $systemcontext = get_context_instance(CONTEXT_SYSTEM); - require_capability('moodle/question:config', $systemcontext); - $canviewreports = has_capability('report/questioninstances:view', $systemcontext); +/** + * Allows the admin to manage question types. + * + * @package moodlecore + * @subpackage questionbank + * @copyright 2008 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ - admin_externalpage_setup('manageqtypes'); - $qtypes = question_bank::get_all_qtypes(); +require_once(dirname(__FILE__) . '/../config.php'); +require_once($CFG->libdir . '/questionlib.php'); +require_once($CFG->libdir . '/adminlib.php'); +require_once($CFG->libdir . '/tablelib.php'); -/// Get some data we will need - question counts and which types are needed. - $counts = $DB->get_records_sql(" - SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden - FROM {question} GROUP BY qtype", array()); - $needed = array(); - foreach ($qtypes as $qtypename => $qtype) { - if (!isset($counts[$qtypename])) { - $counts[$qtypename] = new stdClass; - $counts[$qtypename]->numquestions = 0; - $counts[$qtypename]->numhidden = 0; - } - $needed[$qtypename] = $counts[$qtypename]->numquestions > 0; - $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden; +// Check permissions. +require_login(); +$systemcontext = get_context_instance(CONTEXT_SYSTEM); +require_capability('moodle/question:config', $systemcontext); +$canviewreports = has_capability('report/questioninstances:view', $systemcontext); + +admin_externalpage_setup('manageqtypes'); + +$qtypes = question_bank::get_all_qtypes(); + +// Get some data we will need - question counts and which types are needed. +$counts = $DB->get_records_sql(" + SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden + FROM {question} GROUP BY qtype", array()); +$needed = array(); +foreach ($qtypes as $qtypename => $qtype) { + if (!isset($counts[$qtypename])) { + $counts[$qtypename] = new stdClass; + $counts[$qtypename]->numquestions = 0; + $counts[$qtypename]->numhidden = 0; } - $needed['missingtype'] = true; // The system needs the missing question type. - foreach ($qtypes as $qtypename => $qtype) { - foreach ($qtype->requires_qtypes() as $reqtype) { - $needed[$reqtype] = true; - } + $needed[$qtypename] = $counts[$qtypename]->numquestions > 0; + $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden; +} +$needed['missingtype'] = true; // The system needs the missing question type. +foreach ($qtypes as $qtypename => $qtype) { + foreach ($qtype->requires_qtypes() as $reqtype) { + $needed[$reqtype] = true; } - foreach ($counts as $qtypename => $count) { - if (!isset($qtypes[$qtypename])) { - $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden; - $counts['missingtype']->numhidden += $count->numhidden; - } +} +foreach ($counts as $qtypename => $count) { + if (!isset($qtypes[$qtypename])) { + $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden; + $counts['missingtype']->numhidden += $count->numhidden; + } +} + +// Work of the correct sort order. +$config = get_config('question'); +$sortedqtypes = array(); +foreach ($qtypes as $qtypename => $qtype) { + $sortedqtypes[$qtypename] = $qtype->local_name(); +} +$sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config); + +// Process actions ============================================================ + +// Disable. +if (($disable = optional_param('disable', '', PARAM_SAFEDIR)) && confirm_sesskey()) { + if (!isset($qtypes[$disable])) { + print_error('unknownquestiontype', 'question', new moodle_url('/admin/qtypes.php'), $disable); } -/// Work of the correct sort order. - $config = get_config('question'); - $sortedqtypes = array(); - foreach ($qtypes as $qtypename => $qtype) { - $sortedqtypes[$qtypename] = $qtype->local_name(); - } - $sortedqtypes = question_sort_qtype_array($sortedqtypes, $config); + set_config($disable . '_disabled', 1, 'question'); + redirect(admin_url('qtypes.php')); +} -/// Process actions ============================================================ - - // Disable. - if (($disable = optional_param('disable', '', PARAM_SAFEDIR)) && confirm_sesskey()) { - if (!isset($qtypes[$disable])) { - print_error('unknownquestiontype', 'question', admin_url('qtypes.php'), $disable); - } - - set_config($disable . '_disabled', 1, 'question'); - redirect(admin_url('qtypes.php')); +// Enable. +if (($enable = optional_param('enable', '', PARAM_SAFEDIR)) && confirm_sesskey()) { + if (!isset($qtypes[$enable])) { + print_error('unknownquestiontype', 'question', new moodle_url('/admin/qtypes.php'), $enable); } - // Enable. - if (($enable = optional_param('enable', '', PARAM_SAFEDIR)) && confirm_sesskey()) { - if (!isset($qtypes[$enable])) { - print_error('unknownquestiontype', 'question', admin_url('qtypes.php'), $enable); - } - - if (!$qtypes[$enable]->menu_name()) { - print_error('cannotenable', 'question', admin_url('qtypes.php'), $enable); - } - - unset_config($enable . '_disabled', 'question'); - redirect(admin_url('qtypes.php')); + if (!$qtypes[$enable]->menu_name()) { + print_error('cannotenable', 'question', new moodle_url('/admin/qtypes.php'), $enable); } - // Move up in order. - if (($up = optional_param('up', '', PARAM_SAFEDIR)) && confirm_sesskey()) { - if (!isset($qtypes[$up])) { - print_error('unknownquestiontype', 'question', admin_url('qtypes.php'), $up); - } + unset_config($enable . '_disabled', 'question'); + redirect(new moodle_url('/admin/qtypes.php')); +} - $neworder = question_reorder_qtypes($sortedqtypes, $up, -1); - question_save_qtype_order($neworder, $config); - redirect(admin_url('qtypes.php')); +// Move up in order. +if (($up = optional_param('up', '', PARAM_SAFEDIR)) && confirm_sesskey()) { + if (!isset($qtypes[$up])) { + print_error('unknownquestiontype', 'question', new moodle_url('/admin/qtypes.php'), $up); } - // Move down in order. - if (($down = optional_param('down', '', PARAM_SAFEDIR)) && confirm_sesskey()) { - if (!isset($qtypes[$down])) { - print_error('unknownquestiontype', 'question', admin_url('qtypes.php'), $down); - } + $neworder = question_reorder_qtypes($sortedqtypes, $up, -1); + question_save_qtype_order($neworder, $config); + redirect(new moodle_url('/admin/qtypes.php')); +} - $neworder = question_reorder_qtypes($sortedqtypes, $down, +1); - question_save_qtype_order($neworder, $config); - redirect(admin_url('qtypes.php')); +// Move down in order. +if (($down = optional_param('down', '', PARAM_SAFEDIR)) && confirm_sesskey()) { + if (!isset($qtypes[$down])) { + print_error('unknownquestiontype', 'question', admin_url('qtypes.php'), $down); } - // Delete. - if (($delete = optional_param('delete', '', PARAM_SAFEDIR)) && confirm_sesskey()) { - // Check it is OK to delete this question type. - if ($delete == 'missingtype') { - print_error('cannotdeletemissingqtype', 'admin', admin_url('qtypes.php')); - } + $neworder = question_reorder_qtypes($sortedqtypes, $down, +1); + question_save_qtype_order($neworder, $config); + redirect(new moodle_url('/admin/qtypes.php')); +} - if (!isset($qtypes[$delete])) { - print_error('unknownquestiontype', 'question', admin_url('qtypes.php'), $delete); - } +// Delete. +if (($delete = optional_param('delete', '', PARAM_SAFEDIR)) && confirm_sesskey()) { + // Check it is OK to delete this question type. + if ($delete == 'missingtype') { + print_error('cannotdeletemissingqtype', 'admin', new moodle_url('/admin/qtypes.php')); + } + if (!isset($qtypes[$delete])) { + print_error('unknownquestiontype', 'question', new moodle_url('/admin/qtypes.php'), $delete); + } + + $qtypename = $qtypes[$delete]->local_name(); + if ($counts[$delete]->numquestions + $counts[$delete]->numhidden > 0) { + print_error('cannotdeleteqtypeinuse', 'admin', new moodle_url('/admin/qtypes.php'), $qtypename); + } + + if ($needed[$delete] > 0) { + print_error('cannotdeleteqtypeneeded', 'admin', new moodle_url('/admin/qtypes.php'), $qtypename); + } + + // If not yet confirmed, display a confirmation message. + if (!optional_param('confirm', '', PARAM_BOOL)) { $qtypename = $qtypes[$delete]->local_name(); - if ($counts[$delete]->numquestions + $counts[$delete]->numhidden > 0) { - print_error('cannotdeleteqtypeinuse', 'admin', admin_url('qtypes.php'), $qtypename); - } - - if ($needed[$delete] > 0) { - print_error('cannotdeleteqtypeneeded', 'admin', admin_url('qtypes.php'), $qtypename); - } - - // If not yet confirmed, display a confirmation message. - if (!optional_param('confirm', '', PARAM_BOOL)) { - $qtypename = $qtypes[$delete]->local_name(); - echo $OUTPUT->header(); - echo $OUTPUT->heading(get_string('deleteqtypeareyousure', 'admin', $qtypename)); - echo $OUTPUT->confirm(get_string('deleteqtypeareyousuremessage', 'admin', $qtypename), - admin_url('qtypes.php?delete=' . $delete . '&confirm=1'), - admin_url('qtypes.php')); - echo $OUTPUT->footer(); - exit; - } - - // Do the deletion. echo $OUTPUT->header(); - echo $OUTPUT->heading(get_string('deletingqtype', 'admin', $qtypename)); - - // Delete any configuration records. - if (!unset_all_config_for_plugin('qtype_' . $delete)) { - echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qtype_' . $delete)); - } - unset_config($delete . '_disabled', 'question'); - unset_config($delete . '_sortorder', 'question'); - - // Then the tables themselves - drop_plugin_tables($delete, $qtypes[$delete]->plugin_dir() . '/db/install.xml', false); - - // Remove event handlers and dequeue pending events - events_uninstall('qtype/' . $delete); - - $a->qtype = $qtypename; - $a->directory = $qtypes[$delete]->plugin_dir(); - echo $OUTPUT->box(get_string('qtypedeletefiles', 'admin', $a), 'generalbox', 'notice'); - echo $OUTPUT->continue_button(admin_url('qtypes.php')); + echo $OUTPUT->heading(get_string('deleteqtypeareyousure', 'admin', $qtypename)); + echo $OUTPUT->confirm(get_string('deleteqtypeareyousuremessage', 'admin', $qtypename), + new moodle_url('/admin/qtypes.php', array('delete' => $delete, 'confirm' => 1)), + new moodle_url('/admin/qtypes.php')); echo $OUTPUT->footer(); exit; } - // End of process actions ================================================== - -/// Print the page heading. + // Do the deletion. echo $OUTPUT->header(); - echo $OUTPUT->heading(get_string('manageqtypes', 'admin')); + echo $OUTPUT->heading(get_string('deletingqtype', 'admin', $qtypename)); -/// Set up the table. - $table = new flexible_table('qtypeadmintable'); - $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires', - 'availableto', 'delete', 'settings')); - $table->define_headers(array(get_string('questiontype', 'admin'), get_string('numquestions', 'admin'), - get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'), - get_string('delete'), get_string('settings'))); - $table->set_attribute('id', 'qtypes'); - $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide'); - $table->setup(); + // Delete any configuration records. + if (!unset_all_config_for_plugin('qtype_' . $delete)) { + echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qtype_' . $delete)); + } + unset_config($delete . '_disabled', 'question'); + unset_config($delete . '_sortorder', 'question'); -/// Add a row for each question type. - $createabletypes = question_type_menu(); - foreach ($sortedqtypes as $qtypename => $localname) { - $qtype = $qtypes[$qtypename]; - $row = array(); + // Then the tables themselves + drop_plugin_tables($delete, $qtypes[$delete]->plugin_dir() . '/db/install.xml', false); - // Question icon and name. - $fakequestion = new stdClass; - $fakequestion->qtype = $qtypename; - $icon = print_question_icon($fakequestion, true); - $row[] = $icon . ' ' . $localname; + // Remove event handlers and dequeue pending events + events_uninstall('qtype/' . $delete); - // Number of questions of this type. - if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) { - if ($counts[$qtypename]->numhidden > 0) { - $strcount = get_string('numquestionsandhidden', 'admin', $counts[$qtypename]); - } else { - $strcount = $counts[$qtypename]->numquestions; - } - if ($canviewreports) { - $row[] = '' . $strcount . ''; - } else { - $strcount; - } + $a->qtype = $qtypename; + $a->directory = $qtypes[$delete]->plugin_dir(); + echo $OUTPUT->box(get_string('qtypedeletefiles', 'admin', $a), 'generalbox', 'notice'); + echo $OUTPUT->continue_button(new moodle_url('/admin/qtypes.php')); + echo $OUTPUT->footer(); + exit; +} + +// End of process actions ================================================== + +// Print the page heading. +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('manageqtypes', 'admin')); + +// Set up the table. +$table = new flexible_table('qtypeadmintable'); +$table->define_columns(array('questiontype', 'numquestions', 'version', 'requires', + 'availableto', 'delete', 'settings')); +$table->define_headers(array(get_string('questiontype', 'admin'), get_string('numquestions', 'admin'), + get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'), + get_string('delete'), get_string('settings'))); +$table->set_attribute('id', 'qtypes'); +$table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide'); +$table->setup(); + +// Add a row for each question type. +$createabletypes = question_bank::get_creatable_qtypes(); +foreach ($sortedqtypes as $qtypename => $localname) { + $qtype = $qtypes[$qtypename]; + $row = array(); + + // Question icon and name. + $fakequestion = new stdClass; + $fakequestion->qtype = $qtypename; + $icon = print_question_icon($fakequestion, true); + $row[] = $icon . ' ' . $localname; + + // Number of questions of this type. + if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) { + if ($counts[$qtypename]->numhidden > 0) { + $strcount = get_string('numquestionsandhidden', 'admin', $counts[$qtypename]); } else { - $row[] = 0; + $strcount = $counts[$qtypename]->numquestions; } - - // Question version number. - $version = get_config('qtype_' . $qtypename, 'version'); - if ($version) { - $row[] = $version; + if ($canviewreports) { + $row[] = '' . $strcount . ''; } else { - $row[] = '' . get_string('nodatabase', 'admin') . ''; + $strcount; } - - // Other question types required by this one. - $requiredtypes = $qtype->requires_qtypes(); - $strtypes = array(); - if (!empty($requiredtypes)) { - foreach ($requiredtypes as $required) { - $strtypes[] = $qtypes[$required]->local_name(); - } - $row[] = implode(', ', $strtypes); - } else { - $row[] = ''; - } - - // Are people allowed to create new questions of this type? - $rowclass = ''; - if ($qtype->menu_name()) { - $createable = isset($createabletypes[$qtypename]); - $icons = enable_disable_button($qtypename, $createable); - if (!$createable) { - $rowclass = 'dimmed_text'; - } - } else { - $icons = ''; - } - - // Move icons. - $icons .= icon_html('up', $qtypename, 't/up', get_string('up'), ''); - $icons .= icon_html('down', $qtypename, 't/down', get_string('down'), ''); - $row[] = $icons; - - // Delete link, if available. - if ($needed[$qtypename]) { - $row[] = ''; - } else { - $row[] = '' . get_string('delete') . ''; - } - - // Settings link, if available. - $settings = admin_get_root()->locate('qtypesetting' . $qtypename); - if ($settings instanceof admin_externalpage) { - $row[] = '' . get_string('settings') . ''; - } else if ($settings instanceof admin_settingpage) { - $row[] = '' . get_string('settings') . ''; - } else { - $row[] = ''; - } - - $table->add_data($row, $rowclass); + } else { + $row[] = 0; } - $table->finish_output(); + // Question version number. + $version = get_config('qtype_' . $qtypename, 'version'); + if ($version) { + $row[] = $version; + } else { + $row[] = '' . get_string('nodatabase', 'admin') . ''; + } - echo $OUTPUT->footer(); + // Other question types required by this one. + $requiredtypes = $qtype->requires_qtypes(); + $strtypes = array(); + if (!empty($requiredtypes)) { + foreach ($requiredtypes as $required) { + $strtypes[] = $qtypes[$required]->local_name(); + } + $row[] = implode(', ', $strtypes); + } else { + $row[] = ''; + } -function admin_url($endbit) { - global $CFG; - return $CFG->wwwroot . '/' . $CFG->admin . '/' . $endbit; + // Are people allowed to create new questions of this type? + $rowclass = ''; + if ($qtype->menu_name()) { + $createable = isset($createabletypes[$qtypename]); + $icons = enable_disable_button($qtypename, $createable); + if (!$createable) { + $rowclass = 'dimmed_text'; + } + } else { + $icons = ''; + } + + // Move icons. + $icons .= icon_html('up', $qtypename, 't/up', get_string('up'), ''); + $icons .= icon_html('down', $qtypename, 't/down', get_string('down'), ''); + $row[] = $icons; + + // Delete link, if available. + if ($needed[$qtypename]) { + $row[] = ''; + } else { + $row[] = '' . get_string('delete') . ''; + } + + // Settings link, if available. + $settings = admin_get_root()->locate('qtypesetting' . $qtypename); + if ($settings instanceof admin_externalpage) { + $row[] = '' . get_string('settings') . ''; + } else if ($settings instanceof admin_settingpage) { + $row[] = '' . get_string('settings') . ''; + } else { + $row[] = ''; + } + + $table->add_data($row, $rowclass); } +$table->finish_output(); + +echo $OUTPUT->footer(); + function enable_disable_button($qtypename, $createable) { if ($createable) { return icon_html('disable', $qtypename, 'i/hide', get_string('enabled', 'question'), get_string('disable')); @@ -285,7 +304,7 @@ function icon_html($action, $qtypename, $icon, $alt, $tip) { if ($tip) { $tip = 'title="' . $tip . '" '; } - $html = '
'; + $html = '
'; $html .= ''; $html .= ''; diff --git a/admin/report/questioninstances/index.php b/admin/report/questioninstances/index.php index d69098280ff..24299d32a5d 100644 --- a/admin/report/questioninstances/index.php +++ b/admin/report/questioninstances/index.php @@ -22,8 +22,9 @@ echo $OUTPUT->header(); add_to_log(SITEID, "admin", "report questioninstances", "report/questioninstances/index.php?qtype=$requestedqtype", $requestedqtype); // Prepare the list of capabilities to choose from +$qtypes = question_bank::get_all_qtypes(); $qtypechoices = array(); -foreach ($QTYPES as $qtype) { +foreach ($qtypes as $qtype) { $qtypechoices[$qtype->name()] = $qtype->local_name(); } @@ -45,7 +46,7 @@ if ($requestedqtype) { // Work out the bits needed for the SQL WHERE clauses. if ($requestedqtype == 'missingtype') { - $othertypes = array_keys($QTYPES); + $othertypes = array_keys($qtypes); $key = array_search('missingtype', $othertypes); unset($othertypes[$key]); list($sqlqtypetest, $params) = $DB->get_in_or_equal($othertypes, SQL_PARAMS_QM, '', false); @@ -58,7 +59,8 @@ if ($requestedqtype) { } else { $sqlqtypetest = 'WHERE qtype = ?'; $params = array($requestedqtype); - $title = get_string('reportforqtype', 'report_questioninstances', $QTYPES[$requestedqtype]->local_name()); + $title = get_string('reportforqtype', 'report_questioninstances', + question_bank::get_qtype($requestedqtype)->local_name()); } // Get the question counts, and all the context information, for each diff --git a/lib/questionlib.php b/lib/questionlib.php index c640da23c7e..410d1e41a0e 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -82,81 +82,6 @@ define("QUESTION_NUMANS_ADD", 3); */ define('QUESTION_PREVIEW_POPUP_OPTIONS', 'scrollbars=yes,resizable=yes,width=800,height=600'); -/** - * @global array holding question type objects - * @deprecated - */ -global $QTYPES; -$QTYPES = question_bank::get_all_qtypes(); -function question_register_questiontype() { - // TODO kill this. -} -// TODO kill this. -class default_questiontype { - function plugin_dir() { - return ''; - } -} - -/** - * 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() { - static $menuoptions = null; - if (is_null($menuoptions)) { - $config = get_config('question'); - $menuoptions = array(); - foreach (question_bank::get_all_qtypes() as $name => $qtype) { - $menuname = $qtype->menu_name(); - $enabledvar = $name . '_disabled'; - if ($menuname && !isset($config->$enabledvar)) { - $menuoptions[$name] = $menuname; - } - } - - $menuoptions = question_sort_qtype_array($menuoptions, $config); - } - return $menuoptions; -} - -/** - * Sort an array of question type names according to the question type sort order stored in - * config_plugins. Entries for which there is no xxx_sortorder defined will go - * at the end, sorted according to textlib_get_instance()->asort($inarray). - * @param $inarray an array $qtypename => $qtype->local_name(). - * @param $config get_config('question'), if you happen to have it around, to save one DB query. - * @return array the sorted version of $inarray. - */ -function question_sort_qtype_array($inarray, $config = null) { - if (is_null($config)) { - $config = get_config('question'); - } - - $sortorder = array(); - foreach ($inarray as $name => $notused) { - $sortvar = $name . '_sortorder'; - if (isset($config->$sortvar)) { - $sortorder[$config->$sortvar] = $name; - } - } - - ksort($sortorder); - $outarray = array(); - foreach ($sortorder as $name) { - $outarray[$name] = $inarray[$name]; - unset($inarray[$name]); - } - textlib_get_instance()->asort($inarray); - return array_merge($outarray, $inarray); -} - /** * Move one question type in a list of question types. If you try to move one element * off of the end, nothing will change. @@ -692,7 +617,7 @@ function question_delete_activity($cm, $feedback=true) { * @param integer $newcategoryid the id of the category to move to. */ function question_move_questions_to_category($questionids, $newcategoryid) { - global $DB, $QTYPES; + global $DB; $newcontextid = $DB->get_field('question_categories', 'contextid', array('id' => $newcategoryid)); @@ -704,8 +629,8 @@ function question_move_questions_to_category($questionids, $newcategoryid) { WHERE q.id $questionidcondition", $params); foreach ($questions as $question) { if ($newcontextid != $question->contextid) { - $QTYPES[$question->qtype]->move_files($question->id, - $question->contextid, $newcontextid); + question_bank::get_qtype($question->qtype)->move_files( + $question->id, $question->contextid, $newcontextid); } } @@ -729,12 +654,12 @@ function question_move_questions_to_category($questionids, $newcategoryid) { * @param integer $newcontextid the new context id. */ function question_move_category_to_context($categoryid, $oldcontextid, $newcontextid) { - global $DB, $QTYPES; + global $DB; $questionids = $DB->get_records_menu('question', array('category' => $categoryid), '', 'id,qtype'); foreach ($questionids as $questionid => $qtype) { - $QTYPES[$qtype]->move_files($questionid, $oldcontextid, $newcontextid); + question_bank::get_qtype($qtype)->move_files($questionid, $oldcontextid, $newcontextid); } $subcatids = $DB->get_records_menu('question_categories', @@ -851,12 +776,12 @@ function question_load_questions($questionids, $extrafields = '', $join = '') { * @param boolean $loadtags load the question tags from the tags table. Optional, default false. */ function _tidy_question($question, $loadtags = false) { - global $CFG, $QTYPES; - if (!array_key_exists($question->qtype, $QTYPES)) { - $question->qtype = 'missingtype'; - $question->questiontext = '

' . get_string('warningmissingtype', 'quiz') . '

' . $question->questiontext; + global $CFG; + if (question_bank::is_qtype_installed($question->qtype)) { + $question->questiontext = html_writer::tag('p', get_string('warningmissingtype', + 'qtype_missingtype')) . $question->questiontext; } - $QTYPES[$question->qtype]->get_question_options($question); + question_bank::get_qtype($question->qtype)->get_question_options($question); if (isset($question->_partiallyloaded)) { unset($question->_partiallyloaded); } @@ -940,9 +865,7 @@ function question_get_editing_head_contributions($question) { * Simply calls the question type specific save_question_options() method. */ function save_question_options($question) { - global $QTYPES; - - $QTYPES[$question->qtype]->save_question_options($question); + question_bank::get_qtype($question->qtype)->save_question_options($question); } /// CATEGORY FUNCTIONS ///////////////////////////////////////////////////////////////// diff --git a/lib/simpletest/testquestionlib.php b/lib/simpletest/testquestionlib.php index 61e0f183985..703d5cbc4c1 100644 --- a/lib/simpletest/testquestionlib.php +++ b/lib/simpletest/testquestionlib.php @@ -41,22 +41,6 @@ class questionlib_test extends UnitTestCase { public static $includecoverage = array('lib/questionlib.php'); - function test_question_sort_qtype_array() { - $config = new stdClass(); - $config->multichoice_sortorder = '1'; - $config->calculated_sortorder = '2'; - $qtypes = array( - 'frog' => 'toad', - 'calculated' => 'newt', - 'multichoice' => 'eft', - ); - $this->assertEqual(question_sort_qtype_array($qtypes), array( - 'multichoice' => 'eft', - 'calculated' => 'newt', - 'frog' => 'toad', - )); - } - function test_question_reorder_qtypes() { $this->assertEqual(question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +1), array(0 => 't2', 1 => 't1', 2 => 't3')); diff --git a/question/engine/bank.php b/question/engine/bank.php index 4a6227e8dab..c1e90606aa1 100644 --- a/question/engine/bank.php +++ b/question/engine/bank.php @@ -55,6 +55,15 @@ abstract class question_bank { private static $questionconfig = null; + /** + * @param string $qtypename a question type name, e.g. 'multichoice'. + * @return bool whether that question type is installed in this Moodle. + */ + public static function is_qtype_installed($qtypename) { + $plugindir = get_plugin_directory('qtype', $qtypename); + return $plugindir && is_readable($plugindir . '/questiontype.php'); + } + /** * Get the question type class for a particular question type. * @param string $qtypename the question type name. For example 'multichoice' or 'shortanswer'. @@ -131,12 +140,47 @@ abstract class question_bank { try { $qtypes[$plugin] = self::get_qtype($plugin); } catch (Exception $e) { - // TODO ingore, but reivew this later. + // TODO ingore, but review this later. } } return $qtypes; } + /** + * Sort an array of question types according to the order the admin set up, + * and then alphabetically for the rest. + * @param array qtype->name() => qtype->local_name(). + * @return array sorted array. + */ + public static function sort_qtype_array($qtypes, $config = null) { + if (is_null($config)) { + $config = self::get_config(); + } + + $sortorder = array(); + $otherqtypes = array(); + foreach ($qtypes as $name => $localname) { + $sortvar = $name . '_sortorder'; + if (isset($config->$sortvar)) { + $sortorder[$config->$sortvar] = $name; + } else { + $otherqtypes[$name] = $localname; + } + } + + ksort($sortorder); + textlib_get_instance()->asort($otherqtypes); + + $sortedqtypes = array(); + foreach ($sortorder as $name) { + $sortedqtypes[$name] = $qtypes[$name]; + } + foreach ($otherqtypes as $name => $notused) { + $sortedqtypes[$name] = $qtypes[$name]; + } + return $sortedqtypes; + } + /** * @return array all the question types that users are allowed to create, * sorted into the preferred order set on the admin screen. @@ -145,29 +189,17 @@ abstract class question_bank { $config = self::get_config(); $allqtypes = self::get_all_qtypes(); - $sortorder = array(); - $otherqtypes = array(); + $qtypenames = array(); foreach ($allqtypes as $name => $qtype) { - if (!self::qtype_enabled($name)) { - unset($allqtypes[$name]); - continue; - } - $sortvar = $name . '_sortorder'; - if (isset($config->$sortvar)) { - $sortorder[$config->$sortvar] = $name; - } else { - $otherqtypes[$name] = $qtype->local_name(); + if (self::qtype_enabled($name)) { + $qtypenames[$name] = $qtype->local_name(); } } - ksort($sortorder); - textlib_get_instance()->asort($otherqtypes); + $qtypenames = self::sort_qtype_array($qtypenames); $creatableqtypes = array(); - foreach ($sortorder as $name) { - $creatableqtypes[$name] = $allqtypes[$name]; - } - foreach ($otherqtypes as $name => $notused) { + foreach ($qtypenames as $name => $notused) { $creatableqtypes[$name] = $allqtypes[$name]; } return $creatableqtypes; diff --git a/question/engine/simpletest/testquestionbank.php b/question/engine/simpletest/testquestionbank.php new file mode 100644 index 00000000000..5043c3f92de --- /dev/null +++ b/question/engine/simpletest/testquestionbank.php @@ -0,0 +1,62 @@ +. + +/** + * This file contains tests for the question_bank class. + * + * @package moodlecore + * @subpackage questionbank + * @copyright 2011 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +defined('MOODLE_INTERNAL') || die(); + +require_once(dirname(__FILE__) . '/../lib.php'); + + +/** + *Unit tests for the {@link question_bank} class. + * + * @copyright 2011 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class question_bank_test extends UnitTestCase { + + public function setUp() { + } + + public function tearDown() { + } + + function test_sort_qtype_array() { + $config = new stdClass(); + $config->multichoice_sortorder = '1'; + $config->calculated_sortorder = '2'; + $qtypes = array( + 'frog' => 'toad', + 'calculated' => 'newt', + 'multichoice' => 'eft', + ); + $this->assertEqual(question_bank::sort_qtype_array($qtypes, $config), array( + 'multichoice' => 'eft', + 'calculated' => 'newt', + 'frog' => 'toad', + )); + } +} \ No newline at end of file diff --git a/question/engine/upgradefromoldqe/upgrade.php b/question/engine/upgradefromoldqe/upgrade.php index e630b600dcb..2c708c204c9 100644 --- a/question/engine/upgradefromoldqe/upgrade.php +++ b/question/engine/upgradefromoldqe/upgrade.php @@ -506,7 +506,7 @@ class question_engine_upgrade_question_loader { } protected function load_question($questionid, $quizid) { - global $CFG, $QTYPES; + global $CFG; if ($quizid) { $question = get_record_sql(" @@ -531,14 +531,14 @@ class question_engine_upgrade_question_loader { unset($question->defaultgrade); } - if (!array_key_exists($question->qtype, $QTYPES)) { + $qtype = question_bank::get_qtype($question->qtype, false); + if ($qtype->name() === 'missingtype') { $this->logger->log_assumption("Dealing with question id {$question->id} that is of an unknown type {$question->qtype}."); - $question->qtype = 'missingtype'; $question->questiontext = '

' . get_string('warningmissingtype', 'quiz') . '

' . $question->questiontext; } - $QTYPES[$question->qtype]->get_question_options($question); + $qtype->get_question_options($question); return $question; } diff --git a/question/format.php b/question/format.php index a45b2e38841..514d70581c0 100644 --- a/question/format.php +++ b/question/format.php @@ -273,7 +273,7 @@ class qformat_default { * @return bool success */ function importprocess($category) { - global $USER, $CFG, $DB, $OUTPUT, $QTYPES; + global $USER, $CFG, $DB, $OUTPUT; $context = $category->context; $this->importcontext = $context; @@ -378,12 +378,14 @@ class qformat_default { $question->id = $DB->insert_record('question', $question); if (isset($question->questiontextfiles)) { foreach ($question->questiontextfiles as $file) { - $QTYPES[$question->qtype]->import_file($context, 'question', 'questiontext', $question->id, $file); + question_bank::get_qtype($question->qtype)->import_file( + $context, 'question', 'questiontext', $question->id, $file); } } if (isset($question->generalfeedbackfiles)) { foreach ($question->generalfeedbackfiles as $file) { - $QTYPES[$question->qtype]->import_file($context, 'question', 'generalfeedback', $question->id, $file); + question_bank::get_qtype($question->qtype)->import_file( + $context, 'question', 'generalfeedback', $question->id, $file); } } @@ -391,7 +393,7 @@ class qformat_default { // Now to save all the answers and type-specific options - $result = $QTYPES[$question->qtype]->save_question_options($question); + $result = question_bank::get_qtype($question->qtype)->save_question_options($question); if (!empty($CFG->usetags) && isset($question->tags)) { require_once($CFG->dirroot . '/tag/lib.php'); @@ -626,19 +628,13 @@ class qformat_default { * @return string the data to append to export or false if error (or unhandled) */ function try_exporting_using_qtypes($name, $question, $extra=null) { - global $QTYPES; - // work out the name of format in use $formatname = substr(get_class($this), strlen('qformat_')); $methodname = "export_to_$formatname"; - if (array_key_exists($name, $QTYPES)) { - $qtype = $QTYPES[ $name ]; - if (method_exists($qtype, $methodname)) { - if ($data = $qtype->$methodname($question, $this, $extra)) { - return $data; - } - } + $qtype = question_bank::get_qtype($name, false); + if (method_exists($qtype, $methodname)) { + return $qtype->$methodname($question, $this, $extra); } return false; } diff --git a/question/format/blackboard_six/format.php b/question/format/blackboard_six/format.php index 27911f87ccf..d875a7df7f9 100644 --- a/question/format/blackboard_six/format.php +++ b/question/format/blackboard_six/format.php @@ -833,11 +833,9 @@ function process_essay($quest, &$questions) { // Process Matching Questions //---------------------------------------- function process_matching($quest, &$questions) { - global $QTYPES; - // renderedmatch is an optional plugin, so we need to check if it is defined - if (array_key_exists('renderedmatch', $QTYPES)) { - $question = $this->process_common( $quest ); + if (question_bank::is_qtype_installed('renderedmatch')) { + $question = $this->process_common($quest); $question->valid = true; $question->qtype = 'renderedmatch'; diff --git a/question/format/gift/format.php b/question/format/gift/format.php index 8e96191b23a..08436d404e1 100644 --- a/question/format/gift/format.php +++ b/question/format/gift/format.php @@ -619,7 +619,7 @@ class qformat_gift extends qformat_default { } function writequestion($question) { - global $QTYPES, $OUTPUT; + global $OUTPUT; // Start with a comment $expout = "// question: $question->id name: $question->name\n"; @@ -747,7 +747,7 @@ class qformat_gift extends qformat_default { } else { $expout .= "Question type $question->qtype is not supported\n"; echo $OUTPUT->notification(get_string('nohandler', 'qformat_gift', - $QTYPES[$question->qtype]->local_name())); + question_bank::get_qtype_name($question->qtype))); } } diff --git a/question/format/webct/format.php b/question/format/webct/format.php index 2897f961c5a..5fe00887da9 100644 --- a/question/format/webct/format.php +++ b/question/format/webct/format.php @@ -174,8 +174,6 @@ class qformat_webct extends qformat_default { } function readquestions ($lines) { - global $QTYPES ; - // $qtypecalculated = new qformat_webct_modified_calculated_qtype(); $webctnumberregex = '[+-]?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)((e|E|\\*10\\*\\*)([+-]?[0-9]+|\\([+-]?[0-9]+\\)))?'; @@ -344,7 +342,7 @@ class qformat_webct extends qformat_default { case CALCULATED: foreach ($question->answers as $answer) { - if ($formulaerror =qtype_calculated_find_formula_errors($answer)) { //$QTYPES['calculated']-> + if ($formulaerror = qtype_calculated_find_formula_errors($answer)) { $warnings[] = "'$question->name': ". $formulaerror; $QuestionOK = FALSE; } @@ -549,7 +547,8 @@ class qformat_webct extends qformat_default { $question->feedback[$currentchoice] = ''; $question->correctanswerlength[$currentchoice] = 4; - $datasetnames = $QTYPES[CALCULATED]->find_dataset_names($webct_options[1]); + $datasetnames = question_bank::get_qtype('calculated')-> + find_dataset_names($webct_options[1]); foreach ($datasetnames as $datasetname) { $question->dataset[$datasetname] = new stdClass(); $question->dataset[$datasetname]->datasetitem = array(); diff --git a/question/format/xml/format.php b/question/format/xml/format.php index 4e5af07bfe3..9c22c7d750d 100644 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -1082,7 +1082,7 @@ class qformat_xml extends qformat_default { * @return string xml segment */ function writequestion($question) { - global $CFG, $QTYPES, $OUTPUT; + global $CFG, $OUTPUT; $fs = get_file_storage(); $contextid = $question->contextid; @@ -1168,7 +1168,7 @@ class qformat_xml extends qformat_default { // not a qtype really - dummy used for category switching break; - case TRUEFALSE: + case 'truefalse': $trueanswer = $question->options->answers[$question->options->trueanswer]; $trueanswer->answer = 'true'; $expout .= $this->write_answer($trueanswer); @@ -1178,7 +1178,7 @@ class qformat_xml extends qformat_default { $expout .= $this->write_answer($falseanswer); break; - case MULTICHOICE: + case 'multichoice': $expout .= " " . $this->get_single($question->options->single) . "\n"; $expout .= " " . $this->get_single($question->options->shuffleanswers) . "\n"; $expout .= " {$question->options->answernumbering}\n"; @@ -1186,12 +1186,12 @@ class qformat_xml extends qformat_default { $expout .= $this->write_answers($question->options->answers); break; - case SHORTANSWER: + case 'shortanswer': $expout .= " {$question->options->usecase}\n"; $expout .= $this->write_answers($question->options->answers); break; - case NUMERICAL: + case 'numerical': foreach ($question->options->answers as $answer) { $expout .= $this->write_answer($answer, " $answer->tolerance\n"); @@ -1229,7 +1229,7 @@ class qformat_xml extends qformat_default { } break; - case MATCH: + case 'match': $expout .= " " . $this->get_single($question->options->shuffleanswers) . "\n"; $expout .= $this->write_combined_feedback($question->options); foreach ($question->options->subquestions as $subquestion) { @@ -1244,11 +1244,11 @@ class qformat_xml extends qformat_default { } break; - case DESCRIPTION: + case 'description': // Nothing else to do. break; - case MULTIANSWER: + case 'multianswer': $acount = 1; foreach ($question->options->questions as $question) { $thispattern = addslashes("{#".$acount."}"); @@ -1258,13 +1258,13 @@ class qformat_xml extends qformat_default { } break; - case ESSAY: + case 'essay': // Nothing else to do. break; - case CALCULATED: - case CALCULATEDSIMPLE: - case CALCULATEDMULTI: + case 'calculated': + case 'calculatedsimple': + case 'calculatedmulti': $expout .= " {$question->options->synchronize}\n"; $expout .= " {$question->options->single}\n"; $expout .= " {$question->options->answernumbering}\n"; @@ -1342,9 +1342,11 @@ class qformat_xml extends qformat_default { $expout .= "\n"; } } - //The tag $question->export_process has been set so we get all the data items in the database - // from the function $QTYPES['calculated']->get_question_options(&$question); - // calculatedsimple defaults to calculated + + // The tag $question->export_process has been set so we get all the + // data items in the database from the function + // qtype_calculated::get_question_options calculatedsimple defaults + // to calculated if( isset($question->options->datasets)&&count($question->options->datasets)){// there should be $expout .= "\n"; foreach ($question->options->datasets as $def) { diff --git a/question/type/calculated/questiontype.php b/question/type/calculated/questiontype.php index bd74a65b49d..e6022549420 100644 --- a/question/type/calculated/questiontype.php +++ b/question/type/calculated/questiontype.php @@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die(); * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class question_calculated_qtype extends default_questiontype { +class question_calculated_qtype extends question_type { public $fileoptionsa = array( 'subdirs' => false, @@ -2163,11 +2163,6 @@ class question_calculated_qtype extends default_questiontype { } //// END OF CLASS //// -////////////////////////////////////////////////////////////////////////// -//// INITIATION - Without this line the question type is not in use... /// -////////////////////////////////////////////////////////////////////////// -question_register_questiontype(new question_calculated_qtype()); - if ( ! defined ("CALCULATEDQUESTIONMAXITEMNUMBER")) { define("CALCULATEDQUESTIONMAXITEMNUMBER", 100); } diff --git a/question/type/calculatedmulti/questiontype.php b/question/type/calculatedmulti/questiontype.php index cc6ffbbea1d..0abd637f0de 100644 --- a/question/type/calculatedmulti/questiontype.php +++ b/question/type/calculatedmulti/questiontype.php @@ -277,7 +277,7 @@ class question_calculatedmulti_qtype extends question_calculated_qtype { } function create_runtime_question($question, $form) { - $question = default_questiontype::create_runtime_question($question, $form); + $question = parent::create_runtime_question($question, $form); $question->options->answers = array(); foreach ($form->answers as $key => $answer) { $a->answer = trim($form->answer[$key]); @@ -621,11 +621,6 @@ class question_calculatedmulti_qtype extends question_calculated_qtype { //// END OF CLASS //// -////////////////////////////////////////////////////////////////////////// -//// INITIATION - Without this line the question type is not in use... /// -////////////////////////////////////////////////////////////////////////// -question_register_questiontype(new question_calculatedmulti_qtype()); - if ( ! defined ("CALCULATEDMULTI")) { define("CALCULATEDMULTI", "calculatedmulti"); } diff --git a/question/type/calculatedsimple/questiontype.php b/question/type/calculatedsimple/questiontype.php index d5dce4c387a..380ed933700 100644 --- a/question/type/calculatedsimple/questiontype.php +++ b/question/type/calculatedsimple/questiontype.php @@ -405,11 +405,3 @@ class question_calculatedsimple_qtype extends question_calculated_qtype { } //// END OF CLASS //// -////////////////////////////////////////////////////////////////////////// -//// INITIATION - Without this line the question type is not in use... /// -////////////////////////////////////////////////////////////////////////// -question_register_questiontype(new question_calculatedsimple_qtype()); - -if ( ! defined ("CALCULATEDSIMPLE")) { - define("CALCULATEDSIMPLE", "calculatedsimple"); -} diff --git a/question/type/multianswer/edit_multianswer_form.php b/question/type/multianswer/edit_multianswer_form.php index 0ca189648f4..13229fb8d75 100644 --- a/question/type/multianswer/edit_multianswer_form.php +++ b/question/type/multianswer/edit_multianswer_form.php @@ -80,7 +80,6 @@ class question_edit_multianswer_form extends question_edit_form { $mform->addElement('hidden', 'reload', 1); // $mform->addElement('hidden', 'generalfeedback',''); $mform->setType('reload', PARAM_INT); - $question_type_names = question_type_menu(); // Remove meaningless defaultgrade field. $mform->removeElement('defaultgrade'); @@ -156,11 +155,11 @@ class question_edit_multianswer_form extends question_edit_form { if(isset($this->savedquestiondisplay->options->questions[$sub]->qtype) && $this->savedquestiondisplay->options->questions[$sub]->qtype != $this->questiondisplay->options->questions[$sub]->qtype ){ $this->qtype_change = true ; - $storemess = " STORED QTYPE ".$question_type_names[$this->savedquestiondisplay->options->questions[$sub]->qtype].""; + $storemess = " STORED QTYPE ".question_bank::get_qtype_name($this->savedquestiondisplay->options->questions[$sub]->qtype).""; } $mform->addElement('header', 'subhdr'.$sub, get_string('questionno', 'question', - '{#'.$sub.'}').' '.$question_type_names[$this->questiondisplay->options->questions[$sub]->qtype].$storemess); + '{#'.$sub.'}').' '.question_bank::get_qtype_name($this->questiondisplay->options->questions[$sub]->qtype).$storemess); $mform->addElement('static', 'sub_'.$sub."_".'questiontext', get_string('questiondefinition','qtype_multianswer'),array('cols'=>60, 'rows'=>3)); @@ -388,7 +387,7 @@ class question_edit_multianswer_form extends question_edit_form { $maxfraction = -1; if(isset($this->savedquestiondisplay->options->questions[$sub]->qtype) && $this->savedquestiondisplay->options->questions[$sub]->qtype != $questiondisplay->options->questions[$sub]->qtype ){ - $storemess = " STORED QTYPE ".$question_type_names[$this->savedquestiondisplay->options->questions[$sub]->qtype]; + $storemess = " STORED QTYPE ".question_bank::get_qtype_name($this->savedquestiondisplay->options->questions[$sub]->qtype); } foreach ( $subquestion->answer as $key=>$answer) { $trimmedanswer = trim($answer); diff --git a/question/type/multianswer/questiontype.php b/question/type/multianswer/questiontype.php index cc635b1dc65..ca2b15dac22 100644 --- a/question/type/multianswer/questiontype.php +++ b/question/type/multianswer/questiontype.php @@ -34,7 +34,7 @@ defined('MOODLE_INTERNAL') || die(); * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class embedded_cloze_qtype extends default_questiontype { +class embedded_cloze_qtype extends question_type { function name() { return 'multianswer'; @@ -710,12 +710,6 @@ Good luck! } //// END OF CLASS //// - -////////////////////////////////////////////////////////////////////////// -//// INITIATION - Without this line the question type is not in use... /// -////////////////////////////////////////////////////////////////////////// -question_register_questiontype(new embedded_cloze_qtype()); - ///////////////////////////////////////////////////////////// //// ADDITIONAL FUNCTIONS //// The functions below deal exclusivly with editing diff --git a/question/type/multichoice/db/upgrade.php b/question/type/multichoice/db/upgrade.php index a8c1d3357d0..4b869a52798 100644 --- a/question/type/multichoice/db/upgrade.php +++ b/question/type/multichoice/db/upgrade.php @@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die(); * @param int $oldversion the version we are upgrading from. */ function xmldb_qtype_multichoice_upgrade($oldversion) { - global $CFG, $DB, $QTYPES; + global $CFG, $DB; $dbman = $DB->get_manager(); @@ -42,7 +42,9 @@ function xmldb_qtype_multichoice_upgrade($oldversion) { // is doing it. // Rename random questions to give them more helpful names. if ($oldversion < 2008021800) { - require_once($CFG->libdir . '/questionlib.php'); + require_once($CFG->dirroot . '/question/type/random/questiontype.php'); + $randomqtype = new qtype_random(); + // Get all categories containing random questions. $categories = $DB->get_recordset_sql(" SELECT qc.id, qc.name @@ -55,10 +57,10 @@ function xmldb_qtype_multichoice_upgrade($oldversion) { $where = "qtype = 'random' AND category = ? AND " . $DB->sql_compare_text('questiontext') . " = " . $DB->sql_compare_text('?'); foreach ($categories as $cat) { - $randomqname = $QTYPES[RANDOM]->question_name($cat, false); + $randomqname = $randomqtype->question_name($cat, false); $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '0')); - $randomqname = $QTYPES[RANDOM]->question_name($cat, true); + $randomqname = $randomqtype->question_name($cat, true); $DB->set_field_select('question', 'name', $randomqname, $where, array($cat->id, '1')); } diff --git a/question/type/numerical/questiontype.php b/question/type/numerical/questiontype.php index 5267ae90329..587a9c9ea09 100644 --- a/question/type/numerical/questiontype.php +++ b/question/type/numerical/questiontype.php @@ -1310,7 +1310,7 @@ class question_numerical_qtype extends qtype_shortanswer { */ function generate_test($name, $courseid = null) { global $DB; - list($form, $question) = default_questiontype::generate_test($name, $courseid); + list($form, $question) = parent::generate_test($name, $courseid); $question->category = $form->category; $form->questiontext = "What is 674 * 36?"; @@ -1379,8 +1379,6 @@ class question_numerical_qtype extends qtype_shortanswer { } } -// INITIATION - Without this line the question type is not in use. -question_register_questiontype(new question_numerical_qtype()); if ( ! defined ("NUMERICALQUESTIONUNITTEXTINPUTDISPLAY")) { define("NUMERICALQUESTIONUNITTEXTINPUTDISPLAY", 0); } diff --git a/question/type/randomsamatch/questiontype.php b/question/type/randomsamatch/questiontype.php index 0102215a14b..39e884afe5d 100644 --- a/question/type/randomsamatch/questiontype.php +++ b/question/type/randomsamatch/questiontype.php @@ -360,11 +360,3 @@ class question_randomsamatch_qtype extends qtype_match { return 1/$question->options->choose; } } - -//// END OF CLASS //// - -////////////////////////////////////////////////////////////////////////// -//// INITIATION - Without this line the question type is not in use... /// -////////////////////////////////////////////////////////////////////////// -question_register_questiontype(new question_randomsamatch_qtype()); -